我正在寻找一种方法,通过使用Sympy将矩阵中的所有元素与while循环中相同大小的另一个矩阵进行比较。
我知道Sympy矩阵的奇异元素可以通过以下方式进行比较(例如,比较第一个元素(:
import sympy as sp
from sympy.interactive import printing
from sympy import Eq, solve_linear_system, Matrix
s = Matrix([2, 2])
e = Matrix([1, 1])
while s[0] > e[0]: #This works
print('Working')
else:
print('Not working')
但我希望将所有矩阵元素相互比较。我试着做这个代码,但我得到了一个错误:
import sympy as sp
from sympy.interactive import printing
from sympy import Eq, solve_linear_system, Matrix
s = Matrix([2, 2])
e = Matrix([1, 1])
while s > e: #This does not work and gives an error
print('Working')
else:
print('Not working')
收到的错误为:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-203350991a18> in <module>
7
8
----> 9 while s > e: #Works
10 print('Working')
11 else:
TypeError: '>' not supported between instances of 'MutableDenseMatrix' and 'MutableDenseMatrix'
有人能帮我指引正确的方向吗?
在Sympy中,将一个矩阵中的所有元素与另一个矩阵进行比较的唯一方法似乎是使用while循环。请参阅下面的代码来了解它是如何工作的,我使用了两个矩阵,每个矩阵有三个元素,这样你就可以看到它确实是这样工作的,而不管元素的数量如何。
import sympy as sp
from sympy.interactive import printing
from sympy import Matrix
s = Matrix([2, 2, 2])
e = Matrix([1, 1, 1])
while (s[0, 0] > e[0, 0]) and (s[1,0] > e[1, 0]) and (s[2,0] > e[2, 0]): #This works
print('Working')
break
else:
print('Not working')
希望这能有所帮助。非常感谢@OscarBenjamin帮助找到这个解决方案!:(