Trying to assign Matrix expressions to N or Q fails
ivanistheone opened this issue · 5 comments
While browsing this page in the docs, I saw that trying to assign a Matrix
object to N
fails.
>>> M = Matrix([0, 0, 1]) # works
>>> N = Matrix([0, 1, 1]) # fails
Exception in SymPy Live of type
<class 'sympy.core.sympify.SympifyError'>
for reference the last 5 stack trace entries are
Traceback (most recent call last):
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 781, in post
live.evaluate(statement, session, printer, stream)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 453, in evaluate
if name not in old_globals or val != old_globals[name]:
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/matrices.py", line 2360, in __ne__
return not self == other
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/dense.py", line 47, in __eq__
other = sympify(other)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/core/sympify.py", line 387, in sympify
raise SympifyError('could not parse %r' % a, exc)
SympifyError: Sympify of expression 'could not parse u'<function N at 0x3e1746e06668>'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
Interestingly if you try to assign something simpler like 2
or 2*x
it works:
>>> N = 2
>>> N
2
and also after the initial assignment, you can assign the Matrix expression to N
w/o a problem:
>>> N = Matrix([0, 1, 1]) # now it works
So it seems there is some issue only when parsing the initial assignment statement N = Matrix([0, 1, 1])
when N
is the builtin function.
Further testing
I tried some of the other one-letter builtins and these work fine:
>>> C = Matrix([0, 1, 1]) # ok
... O = Matrix([0, 1, 1]) # ok
... S = Matrix([0, 1, 1]) # ok
... I = Matrix([0, 1, 1]) # ok
... E = Matrix([0, 1, 1]) # ok
But Q
fails the same way as N
:
>>> Q = Matrix([0, 1, 1]) # fails
Exception in SymPy Live of type
<class 'sympy.core.sympify.SympifyError'>
for reference the last 5 stack trace entries are
Traceback (most recent call last):
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 781, in post
live.evaluate(statement, session, printer, stream)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 453, in evaluate
if name not in old_globals or val != old_globals[name]:
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/matrices.py", line 2360, in __ne__
return not self == other
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/dense.py", line 47, in __eq__
other = sympify(other)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/core/sympify.py", line 387, in sympify
raise SympifyError('could not parse %r' % a, exc)
SympifyError: Sympify of expression 'could not parse u'<sympy.assumptions.ask.AssumptionKeys object at 0x3e1746cabfd0>'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
Even more weirdness
Trying to define a regular Python function called N
when N
is currently defined as a Matrix fails:
>>> N = 2 # workaround to make the next line work
>>> N = Matrix([0, 1, 1]) # works since N=2 just before
>>> N
⎡0⎤
⎢1⎥
⎣1⎦
>>> def N(x): # fails
... return 42
Exception in SymPy Live of type
<class 'sympy.core.sympify.SympifyError'>
for reference the last 5 stack trace entries are
Traceback (most recent call last):
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 781, in post
live.evaluate(statement, session, printer, stream)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 453, in evaluate
if name not in old_globals or val != old_globals[name]:
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/matrices.py", line 2360, in __ne__
return not self == other
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/dense.py", line 47, in __eq__
other = sympify(other)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/core/sympify.py", line 387, in sympify
raise SympifyError('could not parse %r' % a, exc)
SympifyError: Sympify of expression 'could not parse u'<function N at 0x3e25e073fb90>'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
When N
is a regular int, defining a function works of course
>>> N = 2 # works
>>> N
2
>>> def N(x): # works too
... return 42
>>> N
<function N at 0x3e25e0741aa0>
Summary
It seems some part of the live shell wrapper logic is trying to parse inputs and simpify them, which works in general, but is causing bad interactions when either the left or the right side of assignment statements are Matrix
objects.
yes,if we use N at first its not giving any error,later its giving error
N,Q=symbols('N Q')
N = Matrix([0, 1, 1])
if we do like this then its giving no errors for both N,Q
after these lines i have used
from sympy import *
then its showing error like this
Exception in SymPy Live of type
<class 'sympy.core.sympify.SympifyError'>
for reference the last 5 stack trace entries are
Traceback (most recent call last):
File "/base/data/home/apps/ssympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 781, in postsympy-live-hrd/20200105t193609.423659059328302322/shell.py", line 453, in evaluate
live.evaluate(statement, session, printer, stream)
File "/base/data/home/apps/s
if name not in old_globals or val != old_globals[name]:
File "/base/data/home/apps/ssympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/matrices.py", line 2360, in nesympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/matrices/dense.py", line 47, in eq
return not self == other
File "/base/data/home/apps/s
other = sympify(other)
File "/base/data/home/apps/s~sympy-live-hrd/20200105t193609.423659059328302322/sympy/sympy/core/sympify.py", line 387, in sympify
raise SympifyError('could not parse %r' % a, exc)
SympifyError: Sympify of expression 'could not parse u'<sympy.assumptions.ask.AssumptionKeys object at 0x3e4318a27f90>'' failed, because of exception being raised:
SyntaxError: invalid syntax (, line 1)
i would like to work towards it ,can someone help me..
I guess it doesn't handle redefining names that are built in to SymPy correctly.
Just tested, and It's not just for N
and Q
it seems, but for any funtion/callable.
This fails:
>>> def Z(x):
return 42
>>> Z = Matrix([1,2,3])
so it's when left side of assignment is function-like, and right is Matrix