symbolic icon indicating copy to clipboard operation
symbolic copied to clipboard

deprecation warning using `solve`: "non-Expr objects in a Matrix is deprecated"

Open CarloWood opened this issue 1 year ago • 5 comments

When trying the following MATLAB example code:

syms u v
eqns = [2*u^2 + v^2 == 0, u - v == 1];
vars = [v u];
[solv, solu] = solve(eqns,vars)

we get a deprecation warning, the output is:

Symbolic pkg v3.2.1: Python communication link active, SymPy v1.13.3.
<stdin>:7: SymPyDeprecationWarning: 

non-Expr objects in a Matrix is deprecated. Matrix represents
a mathematical matrix. To represent a container of non-numeric
entities, Use a list of lists, TableForm, NumPy array, or some
other data structure instead.

See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-non-expr-in-matrix
for details.

This has been deprecated since SymPy version 1.9. It
will be removed in a future version of SymPy.

solv = (sym 2×1 matrix)

  ⎡  2   √2⋅ⅈ⎤
  ⎢- ─ + ────⎥
  ⎢  3    3  ⎥
  ⎢          ⎥
  ⎢  2   √2⋅ⅈ⎥
  ⎢- ─ - ────⎥
  ⎣  3    3  ⎦

solu = (sym 2×1 matrix)

  ⎡1   √2⋅ⅈ⎤
  ⎢─ + ────⎥
  ⎢3    3  ⎥
  ⎢        ⎥
  ⎢1   √2⋅ⅈ⎥
  ⎢─ - ────⎥
  ⎣3    3  ⎦

If there is another way to solve a linear system without getting the deprecation then I'd be happy too - but I can't find any.

CarloWood avatar Oct 19 '24 16:10 CarloWood

Hi @cbm755 and @CarloWood,

Regarding the deprecation warning for this issue, I ran the MATLAB example code with the following versions:

  • Octave: 9.2.0
  • SymPy: 1.10.1
  • Symbolic package: 3.2.1

Here’s the code I executed:

octave:1> pkg load symbolic
octave:2> version
ans = 9.2.0
octave:3> syms u v
Symbolic pkg v3.2.1: Python communication link active, SymPy v1.10.1.
octave:4> eqns = [2*u^2 + v^2 == 0, u - v == 1];
octave:5> vars = [v u];
octave:6> [solv, solu] = solve(eqns,vars)
solv = (sym 2x1 matrix)

  [      ___  ]
  [1   \/ 2 *I]
  [- + -------]
  [3      3   ]
  [           ]
  [      ___  ]
  [1   \/ 2 *I]
  [- - -------]
  [3      3   ]

solu = (sym 2x1 matrix)

  [        ___  ]
  [  2   \/ 2 *I]
  [- - + -------]
  [  3      3   ]
  [             ]
  [        ___  ]
  [  2   \/ 2 *I]
  [- - - -------]
  [  3      3   ]

Even after enabling all warnings with warning('on', 'all'), I did not encounter any deprecation warnings. However, I did see several other warnings related to boolean values and string conversions:

octave:7> warning('on', 'all')
octave:8> [solv, solu] = solve(eqns,vars)
warning: Using an object of size 3x1 as a boolean value implies all().
warning: called from
    compare_versions at line 115 column 7
    python_ipc_driver at line 36 column 9
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: Using an object of size 2x1 as a boolean value implies all().
warning: called from
    compare_versions at line 119 column 7
    python_ipc_driver at line 36 column 9
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: implicit conversion from string to real N-D array
warning: called from
    compare_versions at line 159 column 8
    python_ipc_driver at line 36 column 9
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: implicit conversion from string to real N-D array
warning: called from
    compare_versions at line 159 column 8
    python_ipc_driver at line 36 column 9
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: concatenation of different character string types may have unintended consequences
warning: called from
    strjoin at line 84 column 10
    write_lines at line 28 column 3
    python_ipc_popen2 at line 145 column 3
    python_ipc_driver at line 62 column 15
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: concatenation of different character string types may have unintended consequences
warning: called from
    strjoin at line 84 column 10
    write_lines at line 28 column 3
    python_ipc_popen2 at line 180 column 3
    python_ipc_driver at line 62 column 15
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

warning: concatenation of different character string types may have unintended consequences
warning: called from
    strjoin at line 84 column 10
    write_lines at line 28 column 3
    python_ipc_popen2 at line 182 column 3
    python_ipc_driver at line 62 column 15
    pycall_sympy__ at line 164 column 11
    solve at line 197 column 9

solv = (sym 2x1 matrix)

  [      ___  ]
  [1   \/ 2 *I]
  [- + -------]
  [3      3   ]
  [           ]
  [      ___  ]
  [1   \/ 2 *I]
  [- - -------]
  [3      3   ]

solu = (sym 2x1 matrix)

  [        ___  ]
  [  2   \/ 2 *I]
  [- - + -------]
  [  3      3   ]
  [             ]
  [        ___  ]
  [  2   \/ 2 *I]
  [- - - -------]
  [  3      3   ]

Next, I tested with the following versions:

  • Octave: 9.2.0
  • SymPy: 1.13.3
  • Symbolic package: 3.2.1

Again, I ran the same code:

octave:3> clear all;
octave:4> version  
ans = 9.2.0
octave:5> pkg load symbolic
octave:6> syms u v
Symbolic pkg v3.2.1: Python communication link active, SymPy v1.13.3.
octave:7> eqns = [2*u^2 + v^2 == 0, u - v == 1];
octave:8> vars = [v u];
octave:9> [solv, solu] = solve(eqns,vars)
solv = (sym 2x1 matrix)

  [        ___  ]
  [  2   \/ 2 *I]
  [- - - -------]
  [  3      3   ]
  [             ]
  [        ___  ]
  [  2   \/ 2 *I]
  [- - + -------]
  [  3      3   ]

solu = (sym 2x1 matrix)

  [      ___  ]
  [1   \/ 2 *I]
  [- - -------]
  [3      3   ]
  [           ]
  [      ___  ]
  [1   \/ 2 *I]
  [- + -------]
  [3      3   ]

Again, no deprecation warnings appeared after enabling all warnings.

Additional Note on Using vpasolve:

If you're looking for an alternative to solve, consider using the vpasolve function, which requires an initial guess for each variable. Here’s the basic syntax:

syms x1 x2 x3 ... xn
eqns = [eqn1; eqn2; eqn3; ...; eqnn];
initial_guess = [g1; g2; g3; ...; gn];
vpasolve(eqns, [x1; x2; x3; ...; xn], initial_guess)

Here’s an example with my previous equations:

octave:21> clear all;
octave:22> version
ans = 9.2.0
octave:23> pkg load symbolic
octave:24> syms u v
Symbolic pkg v3.2.1: Python communication link active, SymPy v1.13.3.
octave:25> eqns = [2*u^2 + v^2 == 0; u - v == 1] 
eqns = (sym 2x1 matrix)

  [   2    2    ]
  [2*u  + v  = 0]
  [             ]
  [  u - v = 1  ]

octave:26> vpasolve(eqns, [u; v], [1i; 1i])
ans = (sym 2x1 matrix)

  [0.33333333333333333333333333333333 + 0.47140452079103168293389624140323*I ]
  [                                                                          ]
  [-0.66666666666666666666666666666667 + 0.47140452079103168293389624140323*I]

octave:27> vpasolve(eqns, [u; v], [4i; 4i])
ans = (sym 2x1 matrix)

  [0.33333333333333333333333333333333 + 0.47140452079103168293389624140323*I ]
  [                                                                          ]
  [-0.66666666666666666666666666666667 + 0.47140452079103168293389624140323*I]

octave:28> vpasolve(eqns, [u; v], [-4i; -4i])
ans = (sym 2x1 matrix)

  [0.33333333333333333333333333333333 - 0.47140452079103168293389624140323*I ]
  [                                                                          ]
  [-0.66666666666666666666666666666667 - 0.47140452079103168293389624140323*I]

Thank you for your attention to this matter. I appreciate any advice, corrections, or guidance you can provide moving forward.

Sonu0305 avatar Nov 13 '24 18:11 Sonu0305

Hi @cbm755 , Could you kindly review the comment on this issue when you have a chance? Thank you for your time and any guidance you can provide.

Sonu0305 avatar Dec 01 '24 13:12 Sonu0305

This looks like a different bug:

warning: Using an object of size 3x1 as a boolean value implies all().

can you file a different issue about it?

cbm755 avatar Dec 01 '24 16:12 cbm755

@Sonu0305, maybe one issue to cover things about by warning('on', 'all')?

The reason (I think) that this is unrelated to the Non-Expr warning is that the Non-Expr warning comes from Python (SymPy) but the Octave warning command controls Octave's warnings.

cbm755 avatar Dec 01 '24 16:12 cbm755

@cbm755 Noted, Done. Thank You

Sonu0305 avatar Dec 03 '24 07:12 Sonu0305