Can you tell me why
import sympy as sym
import numpy as np
a=np.eye(3)*3
eq = sym.Eq(a*y,0)
sym.solve(eq, y)
Eror code is:
---------------------------------------------------------------------------
SympifyError Traceback (most recent call last)
<ipython-input-100-13919806323c> in <module>()
4 a=np.eye(3)*3
5 print(a)
----> 6 eq = sym.Eq(a*y,0)
7 sym.solve(eq, y)
2 frames
/usr/local/lib/python3.7/dist-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
432
433 if strict:
--> 434 raise SympifyError(a)
435
436 if iterable(a):
SympifyError: SympifyError: array([[3.0*y, 0, 0],
[0, 3.0*y, 0],
[0, 0, 3.0*y]], dtype=object)
Doesn't work but
import sympy as sym
eq = sym.Eq(x**3 + 3*x**2 + 3*x + 1,0)
sym.solve(eq, x)
Works properly. And how can I solve easy matrix equations with similar code like up.
Of course both codes are my excises to understand this library
In [101]: a = np.eye(3)*3
In [103]: a*y
Out[103]:
array([[3.0*y, 0, 0],
[0, 3.0*y, 0],
[0, 0, 3.0*y]], dtype=object)
It can be made into a sympy.Array
:
In [104]: Array(a*y)
Out[104]:
⎡3.0⋅y 0 0 ⎤
⎢ ⎥
⎢ 0 3.0⋅y 0 ⎥
⎢ ⎥
⎣ 0 0 3.0⋅y⎦
and Eq
works, though it's unnecessary, since an expression is equal 0 (in solve) by default):
In [105]: Eq(_, 0)
Out[105]:
⎡3.0⋅y 0 0 ⎤
⎢ ⎥
⎢ 0 3.0⋅y 0 ⎥ = 0
⎢ ⎥
⎣ 0 0 3.0⋅y⎦
But apply solve
to this results in the same error as the one I addressed previously:
In [106]: solve(Array(a*y))
---------------------------------------------------------------------------
...
AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_independent'
solve(a*y)
produces the same error.
SOLVE: AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_independent'
That [105]
array is not the same as: x**3 + 3*x**2 + 3*x + 1
. Why did you expect it to work?
Also why did you use np.eye
. sympy
has an eye
In [128]: eq = eye(3)*3*y
In [129]: eq
Out[129]:
⎡3⋅y 0 0 ⎤
⎢ ⎥
⎢ 0 3⋅y 0 ⎥
⎢ ⎥
⎣ 0 0 3⋅y⎦
In [130]: Eq(eq,0)
Out[130]: False
In [131]: solve(eq,y)
Out[131]: {y: 0}
I only have a superficial understanding of what solve
can use, but the difference in type
might well be the key:
In [132]: type(eq)
Out[132]: sympy.matrices.dense.MutableDenseMatrix
In [133]: type(Out[104])
Out[133]: sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray
In [134]: Matrix(a*y)
Out[134]:
⎡3.0⋅y 0 0 ⎤
⎢ ⎥
⎢ 0 3.0⋅y 0 ⎥
⎢ ⎥
⎣ 0 0 3.0⋅y⎦
In [135]: type(Matrix(a*y))
Out[135]: sympy.matrices.dense.MutableDenseMatrix
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.