简体   繁体   English

sympy:“转置”对象没有属性列表

[英]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. 您的情况下, vI的优先级分别为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.

相关问题 获取“列表”object 在 python 中没有属性“tolist” - Getting 'list' object has no attribute 'tolist' in python AttributeError: &#39;NDArray&#39; 对象没有属性 &#39;ravel&#39; 或 &#39;tolist&#39; - AttributeError: 'NDArray' object has no attribute 'ravel' or 'tolist' Bokeh:AttributeError:&#39;DataFrame&#39;对象没有属性&#39;tolist&#39; - Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError:“ DataFrame”对象没有属性“ tolist” - AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError:“SeriesGroupBy”对象没有属性“tolist” - AttributeError: 'SeriesGroupBy' object has no attribute 'tolist' sympy AttributeError:&#39;Pow&#39;对象没有属性&#39;sin&#39; - sympy AttributeError: 'Pow' object has no attribute 'sin' sympy错误:“ Symbol”对象没有属性“ pi” - sympy error: 'Symbol' object has no attribute 'pi' sympy:AttributeError: 'list' object 没有属性 'subs'? - sympy:AttributeError: 'list' object has no attribute 'subs'? 获取 AttributeError: 'builtin_function_or_method' object 没有属性 'tolist' - Getting AttributeError: 'builtin_function_or_method' object has no attribute 'tolist' AttributeError:&#39;BooleanFalse&#39;对象没有属性&#39;evalf&#39;(使用sympy进行分段绘图) - AttributeError: 'BooleanFalse' object has no attribute 'evalf' (piecewise plotting using sympy)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM