0

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    You should be very careful about mixing numpy and sympy like that. Use `a = sym.eye(3) * 3` instead. But I still don't see how you're supposed to make sense of `sym.Eq(a*y, 0)` unless the 0 is supposed to mean a zero matrix of size equal to the lhs of the equation. – Reti43 Oct 13 '21 at 11:25
  • 1
    what did `a*y` print? or show `eq` – hpaulj Oct 13 '21 at 14:42
  • In recent https://stackoverflow.com/questions/69533045/solve-attributeerror-immutabledensendimarray-object-has-no-attribute-as-ind I looked another attempt at use numpy and sympy – hpaulj Oct 13 '21 at 14:47
  • See also [SymPy `solve` fails to solve Matrix equation A*x = b](https://stackoverflow.com/questions/54010664/sympy-solve-fails-to-solve-matrix-equation-ax-b-when-symbols-are-replaced-b) – JohanC Oct 13 '21 at 18:49

1 Answers1

0
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
hpaulj
  • 221,503
  • 14
  • 230
  • 353