python - sympy:如何在评估之前打印矩阵产品?
问题描述
我有两个矩阵,比如说a, b
我想打印(以可读的方式)
<a> @ <b>
,没有实际执行矩阵乘法- 乘法的实际结果(由 sympy 执行)
有什么办法吗?
解决方案
您可以使用以下方法创建未评估的符号产品MatMul
:
https ://docs.sympy.org/latest/modules/matrices/expressions.html#sympy.matrices.expressions.MatMul
In [16]: from sympy import *
In [17]: A = Matrix([[1, 2], [3, 4]])
In [18]: B = Matrix([[5, 6], [7, 8]])
In [19]: product = MatMul(A, B)
In [20]: product
Out[20]:
⎡1 2⎤ ⎡5 6⎤
⎢ ⎥⋅⎢ ⎥
⎣3 4⎦ ⎣7 8⎦
In [21]: product.doit()
Out[21]:
⎡19 22⎤
⎢ ⎥
⎣43 50⎦
In [22]: Eq(product, product.doit())
Out[22]:
⎡1 2⎤ ⎡5 6⎤ ⎡19 22⎤
⎢ ⎥⋅⎢ ⎥ = ⎢ ⎥
⎣3 4⎦ ⎣7 8⎦ ⎣43 50⎦
推荐阅读
- powershell - 如何从电子邮件地址的输入文件中获取 sAMAccountName 和其他用户属性?
- r - 在不指定完整路径的情况下无法在 r markdown 中找到标题图像
- java - 询问运行时权限后应用程序启动/崩溃
- python - PySpark - 根据另一列值的降序添加递增的整数排名值
- python-3.x - 如何调试 yt-dlp
- java - 我应该如何使用带有套接字的 flush() 方法(客户端、服务器通信)
- r - 如何使用 try() 和 map() 函数来解释可能不在 args 列表中的值
- google-chrome-devtools - Chrome DevTools 协议上的拖放行为
- amazon-web-services - AWS Elemental MediaConvert .createJob() 不在 JS SDK 中工作,但在 AWS 控制台中工作
- elasticsearch - 如何在elasticsearch中进行子聚合?