如果您显式调用
MatAdd
构造函数,则不会进行评估。要创建一个等式,请使用
Eq
。在Unicode漂亮打印中加号似乎存在渲染问题,我为此打开了
https://github.com/sympy/sympy/issues/2747。
In [14]: Eq(y.as_explicit(), MatAdd((x*b).as_explicit(), (e).as_explicit()))
Out[14]:
⎡y₀₀⎤ = ⎡x₀₀⋅b₀₀ + x₀₁⋅b₁₀⎤ + ⎡ε₀₀⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₁₀⎥ ⎢x₁₀⋅b₀₀ + x₁₁⋅b₁₀⎥ ⎢ε₁₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₂₀⎥ ⎢x₂₀⋅b₀₀ + x₂₁⋅b₁₀⎥ ⎢ε₂₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₃₀⎥ ⎢x₃₀⋅b₀₀ + x₃₁⋅b₁₀⎥ ⎢ε₃₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣y₄₀⎦ ⎣x₄₀⋅b₀₀ + x₄₁⋅b₁₀⎦ ⎣ε₄₀⎦
看起来LaTeX打印是正确的:
In [16]: print(latex(Eq(y.as_explicit(), MatAdd((x*b).as_explicit(), (e).as_explicit()))))
\left[\begin{matrix}y_{0, 0}\\y_{1, 0}\\y_{2, 0}\\y_{3, 0}\\y_{4, 0}\end{matrix}\right] = \left[\begin{matrix}x_{0, 0} b_{0, 0} + x_{0, 1} b_{1, 0}\\x_{1, 0} b_{0, 0} + x_{1, 1} b_{1, 0}\\x_{2, 0} b_{0, 0} + x_{2, 1} b_{1, 0}\\x_{3, 0} b_{0, 0} + x_{3, 1} b_{1, 0}\\x_{4, 0} b_{0, 0} + x_{4, 1} b_{1, 0}\end{matrix}\right] + \left[\begin{matrix}\epsilon_{0, 0}\\\epsilon_{1, 0}\\\epsilon_{2, 0}\\\epsilon_{3, 0}\\\epsilon_{4, 0}\end{matrix}\right]
如果您不希望计算 x*b
,您可以使用 MatMul
:
In [18]: Eq(y.as_explicit(), MatAdd(MatMul(x.as_explicit(),b.as_explicit()), (e).as_explicit()))
Out[18]:
⎡y₀₀⎤ = ⎡x₀₀ x₀₁⎤⋅⎡b₀₀⎤ + ⎡ε₀₀⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₁₀⎥ ⎢x₁₀ x₁₁⎥ ⎣b₁₀⎦ ⎢ε₁₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₂₀⎥ ⎢x₂₀ x₂₁⎥ ⎢ε₂₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢y₃₀⎥ ⎢x₃₀ x₃₁⎥ ⎢ε₃₀⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣y₄₀⎦ ⎣x₄₀ x₄₁⎦ ⎣ε₄₀⎦
In [19]: print(latex(Eq(y.as_explicit(), MatAdd(MatMul(x.as_explicit(),b.as_explicit()), (e).as_explicit()))))
\left\[\begin{matrix}y_{0, 0}\\y_{1, 0}\\y_{2, 0}\\y_{3, 0}\\y_{4, 0}\end{matrix}\right\] = \left\[\begin{matrix}x_{0, 0} & x_{0, 1}\\x_{1, 0} & x_{1, 1}\\x_{2, 0} & x_{2, 1}\\x_{3, 0} & x_{3, 1}\\x_{4, 0} & x_{4, 1}\end{matrix}\right\] \left\[\begin{matrix}b_{0, 0}\\b_{1, 0}\end{matrix}\right\] + \left\[\begin{matrix}\epsilon_{0, 0}\\\epsilon_{1, 0}\\\epsilon_{2, 0}\\\epsilon_{3, 0}\\\epsilon_{4, 0}\end{matrix}\right\]
b*x
没有对齐。 - asmeurer