[英]sympy: 'Transpose' object has no attribute tolist
I'm trying to do some symbolic matrix calculations with sympy. 我正在尝试使用sympy进行一些符号矩阵计算。 My goal is to obtain a symbolic representation of the result of some matrix computations.
我的目标是获得一些矩阵计算结果的符号表示。 I've run into some problems which I have boiled down to this simple example, in which I try to evaluate the result of a exponentiating a specified matrix and multiplying it by an arbitrary vector.
我遇到了一些问题,可以归结为这个简单的示例,在该示例中,我尝试评估对指定矩阵求幂并将其乘以任意向量的结果。
>>> import sympy
>>> v = sympy.MatrixSymbol('v', 2, 1)
>>> Z = sympy.zeros(2, 2) # create 2x2 zero matrix
>>> I = sympy.exp(Z) # exponentiate zero matrix to get identity matrix
>>> I * v
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sympy/matrices/matrices.py", line 507, in __mul__
blst = B.T.tolist()
AttributeError: 'Transpose' object has no attribute 'tolist'
In contrast, if I directly create the identity matrix and then multiply it by v, then there is no problem: 相反,如果我直接创建单位矩阵,然后将其乘以v,则没有问题:
>>> I_ = sympy.eye(2) # directly create the identity matrix
>>> I_ == I # check the two matrices are equal
True
>>> I_ * v
v
One thing that I've noted is that the two identity matrices are of different classes: 我注意到的一件事是两个身份矩阵属于不同的类别:
>>> I.__class__
sympy.matrices.immutable.ImmutableMatrix
>>> I_.__class__
sympy.matrices.dense.MutableDenseMatrix
I also found that calling the as_mutable()
method provided a work-around. 我还发现调用
as_mutable()
方法提供了一种解决方法。
>>> I.as_mutable() * v
v
Is it always necessary to put as_mutable()
calls throughout one's linear algebra calculations? 是否始终需要在整个线性代数计算中放置
as_mutable()
调用? I'm guessing not, and that instead these errors suggest that I'm using the wrong strategy to solve my problem, but I can't figure out what the right strategy would be. 我猜不是,而是这些错误表明我使用了错误的策略来解决我的问题,但我无法弄清楚什么是正确的策略。 Does anyone have any pointers?
有人有指针吗?
I have read the documentation page on Immutable Matrices but I could still use some help understanding how their differences with standard mutable matrices are important here, and why some operations (eg sympy.exp) convert between these different classes. 我已经阅读了不可变矩阵的文档页面,但是我仍然可以使用一些帮助来理解它们与标准可变矩阵的区别在这里很重要,以及为什么某些操作(例如sympy.exp)在这些不同的类之间转换。
I'd claim that this is a bug in Sympy: 我声称这是Sympy中的错误:
In Python, you can overload the multiplication operator from both sides . 在Python中,您可以从两侧重载乘法运算符 。
A*B
may internally be handled by either calling A.__mul__(B)
, or B.__rmul__(A)
. 可以通过调用
A.__mul__(B)
或B.__rmul__(A)
内部处理A*B
Python first calls A.__mul__
, and if this method does not exist or returns NotImplemented
, then Python tries B.__rmul__
automatically. Python首先调用
A.__mul__
,如果此方法不存在或返回NotImplemented
,则Python B.__rmul__
自动尝试B.__rmul__
。 SymPy instead uses a decorator called call_highest_priority to decide which of both implementations to use. 相反,SymPy使用一个称为call_highest_priority的装饰器来决定使用这两种实现中的哪一种。 It looks up the
_op_priority
of the involved classes and calls the function of the implementation with higher priority. 它查找所涉及类的
_op_priority
并以更高的优先级调用实现的函数。 The priorities in your case are 11 for v
and I
and 10.01 for I_
, so I
is preferred. 您的情况下,
v
和I
的优先级分别为11和I_
的优先级为10.01,因此I
是首选。 Also, the base implementation of __mul__
, which I
uses, lacks the decorator. 另外,
I
使用的__mul__
的基本实现缺少装饰器。
Long story short, I*v
ends up always calling I.__mul__
, and __mul__
cannot handle MatrixSymbol
s but does not return NotImplemented
either. 简而言之,
I*v
最终总是调用I.__mul__
,并且__mul__
无法处理MatrixSymbol
但是也不返回NotImplemented
。 v.__rmul__(I)
works as expected. v.__rmul__(I)
可以正常工作。
The proper fix would be to capture the AttributeError
in matrices.py
and return NotImplemented
, ie 正确的解决方法是在
matrices.py
捕获AttributeError
并返回NotImplemented
,即
try:
blst = B.T.tolist()
except AttributeError:
return NotImplemented
Python would then automatically fallback to __rmul__
. 然后,Python将自动回
__rmul__
。 The hack'ish fix would be to adjust _op_priority
. hack'ish解决方法是调整
_op_priority
。 Either way, you should file a bug report: If the error was by design (that is, if you accidentally tried something that's not supposed to work), then the error message would say so. 无论哪种方式,您都应该提交错误报告:如果错误是设计使然的(也就是说,如果您不小心尝试了某些不起作用的操作),则错误消息将表明是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.