JuliaPy / SymPy.jl

Julia interface to SymPy via PyCall
http://juliapy.github.io/SymPy.jl/
MIT License
269 stars 61 forks source link

Implement inv(::MatrixSymbol) #408

Closed schneiderfelipe closed 3 years ago

schneiderfelipe commented 3 years ago

Hi, I think inv(A::MatrixSymbol) should be defined as A.inverse() (see here). Currently, the following error is thrown:

julia> using SymPy

julia> @syms n integer=true positive=true
(n,)

julia> Aₙ₋₁ = sympy.MatrixSymbol("Aₙ₋₁", n - 1, n - 1)
Aₙ₋₁

julia> Aₙ₋₁.I
    -1
Aₙ₋₁  

julia> inv(Aₙ₋₁)
ERROR: PyError ($(Expr(:escape, :(ccall(#= /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'NotImplementedError'>
NotImplementedError()
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/decorators.py", line 96, in __sympifyit_wrapper
    return func(a, b)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/numbers.py", line 1774, in __truediv__
    return Number.__truediv__(self, other)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/decorators.py", line 96, in __sympifyit_wrapper
    return func(a, b)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/numbers.py", line 778, in __truediv__
    return AtomicExpr.__truediv__(self, other)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/decorators.py", line 267, in _func
    return func(self, other)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/decorators.py", line 136, in binary_op_wrapper
    return f(self)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/matrices/expressions/matexpr.py", line 28, in __sympifyit_wrapper
    return func(a, b)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/core/decorators.py", line 137, in binary_op_wrapper
    return func(self, other)
  File "/home/schneider/.local/lib/python3.8/site-packages/sympy/matrices/expressions/matexpr.py", line 160, in __rtruediv__
    raise NotImplementedError()

Stacktrace:
 [1] pyerr_check at /home/schneider/.julia/packages/PyCall/tqyST/src/exception.jl:62 [inlined]
 [2] pyerr_check at /home/schneider/.julia/packages/PyCall/tqyST/src/exception.jl:66 [inlined]
 [3] _handle_error(::String) at /home/schneider/.julia/packages/PyCall/tqyST/src/exception.jl:83
 [4] macro expansion at /home/schneider/.julia/packages/PyCall/tqyST/src/exception.jl:97 [inlined]
 [5] #109 at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:43 [inlined]
 [6] disable_sigint at ./c.jl:446 [inlined]
 [7] __pycall! at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:42 [inlined]
 [8] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{Sym}, ::Int64, ::Ptr{Nothing}) at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:29
 [9] _pycall! at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:11 [inlined]
 [10] #_#116 at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:86 [inlined]
 [11] (::PyCall.PyObject)(::Sym) at /home/schneider/.julia/packages/PyCall/tqyST/src/pyfncall.jl:86
 [12] /(::Sym, ::Sym) at /home/schneider/.julia/packages/SymPy/Ja40T/src/mathops.jl:13
 [13] inv(::Sym) at ./number.jl:199
 [14] top-level scope at REPL[13]:1

EDIT: this might be related to #200 in general as well.

jverzani commented 3 years ago

Thanks! This was falling back to a generic definition that didn't work for this type.