JuliaPy / SymPy.jl

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

eigvals `PyError` #452

Closed jamblejoe closed 2 years ago

jamblejoe commented 2 years ago

Consider

μ = Sym("μ")
A = Sym[
    -μ 1 0 0 0 0 0 0 0 0;
    0 -2μ 1 1 0 0 0 0 0 0;
    0 0 -μ 0 1 0 0 0 0 0;
    0 0 0 -2μ 1 0 1 0 0 0;
    0 0 0 0 -2μ 1 0 1 0 0;
    0 0 0 0 0 -μ 0 0 1 0;
    0 0 0 0 0 0 -μ 1 0 0;
    1 0 0 0 0 0 0 -2μ 1 0;
    0 1 0 0 0 0 0 0 -2μ 1;
    0 0 0 1 0 0 0 0 0 -μ;
]
eigvals(A)

This results in the error

PyCall.PyError("\$(Expr(:escape, :(ccall(#= C:\\Users\\16185722\\.julia\\packages\\PyCall\\L0fLP\\src\\pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))", PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00000000782b9760), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x000000000144fd60), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x000000000130cdc0))

Stacktrace:
  [1] pyerr_check
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\exception.jl:62 [inlined]
  [2] pyerr_check
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\exception.jl:66 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\exception.jl:83
  [4] macro expansion
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\exception.jl:97 [inlined]
  [5] #107
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ .\c.jl:458 [inlined]
  [7] __pycall!
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{}, nargs::Int64, kw::Ptr{Nothing})
    @ PyCall C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:29
  [9] _pycall!
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:11 [inlined]
 [10] #_#114
    @ C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:86 [inlined]
 [11] (::PyCall.PyObject)()
    @ PyCall C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:86
 [12] eigvals(a::Matrix{Sym})
    @ SymPy C:\Users\16185722\.julia\packages\SymPy\7xLLa\src\matrix.jl:80

The error message is cryptic to me so I cannot provide further insigths yet.

Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

and SymPy v1.1.2.

jamblejoe commented 2 years ago

Turns out that the eigenvalues are not expressable as radicals of polynomials with rational coefficients (or at least SymPy thinks that). See the following:

Traceback (most recent call last):
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 261, in _eigenvals_dict
    eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)}
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 261, in <dictcomp>
    eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)}
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\polys\rootoftools.py", line 352, in __new__
    raise NotImplementedError("CRootOf is not supported over %s" % dom)
NotImplementedError: CRootOf is not supported over ZZ[x]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\matrices.py", line 376, in eigenvals
    return _eigenvals(self, error_when_incomplete=error_when_incomplete, **flags)
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 173, in _eigenvals
    return _eigenvals_dict(
  File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 264, in _eigenvals_dict
    raise MatrixError(eigenvals_error_message)
sympy.matrices.common.MatrixError: It is not always possible to express the eigenvalues of a matrix of size 5x5 or higher in radicals. We have CRootOf, but domains other than the rationals are not currently supported. If there are no symbols in the matrix, it should still be possible to compute numeric approximations of the eigenvalues using M.evalf().eigenvals() or M.charpoly().nroots().

Could we at least forward that error message to Julia?

EDIT: Code I ran:

from sympy import *
x = Symbol('x')
 A = Matrix([[-x, 1, 0, 0, 0, 0, 0, 0, 0, 0],[ 0, -2*x, 1, 1, 0,0, 0, 0, 0, 0],[ 0, 0, -x, 0, 1, 0, 0, 0, 0, 0],[0, 0, 0, -2*x, 1, 0, 1, 0, 0, 0],[0, 0, 0, 0, -2*x, 1, 0, 1, 0, 0],[ 0, 0, 0, 0, 0, -x, 0, 0, 1, 0],[ 0, 0, 0, 0, 0, 0, -x, 1, 0, 0],[ 1, 0, 0, 0, 0, 0, 0, -2*x, 1, 0],[ 0, 1, 0, 0, 0, 0, 0, 0, -2*x, 1],[ 0, 0, 0, 1, 0, 0, 0, 0, 0, -x]])
A.eigenvals()
jverzani commented 2 years ago

Interesting, I do see it in the terminal as part of the error stack. What interface are you using?

On Thu, Jan 20, 2022 at 10:47 AM Goran Nakerst @.***> wrote:

Turns out that the eigenvalues are not expressable as radicals of polynomials with rational coefficients (or at least SymPy thinks that). See the following:

Traceback (most recent call last): File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 261, in _eigenvals_dict eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)} File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 261, in eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)} File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\polys\rootoftools.py", line 352, in new raise NotImplementedError("CRootOf is not supported over %s" % dom) NotImplementedError: CRootOf is not supported over ZZ[x]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\matrices.py", line 376, in eigenvals return _eigenvals(self, error_when_incomplete=error_when_incomplete, **flags) File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 173, in _eigenvals return _eigenvals_dict( File "C:\Users\16185722\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\matrices\eigen.py", line 264, in _eigenvals_dict raise MatrixError(eigenvals_error_message) sympy.matrices.common.MatrixError: It is not always possible to express the eigenvalues of a matrix of size 5x5 or higher in radicals. We have CRootOf, but domains other than the rationals are not currently supported. If there are no symbols in the matrix, it should still be possible to compute numeric approximations of the eigenvalues using M.evalf().eigenvals() or M.charpoly().nroots().

Could we at least forward that error message to Julia?

— Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/452#issuecomment-1017643791, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADG6TAMT5YBYCAUYXCBY2LUXAVBXANCNFSM5MNB2RNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- John Verzani Department of Mathematics College of Staten Island, CUNY

jamblejoe commented 2 years ago

Thanks for checking. I am using vscode with Jupyter notebooks. Running the code in the terminal gives me the additional error message from SymPy as well.

I had that the error messages in vscode's Jupyter notebooks sometimes don't get printed at all, but never that some part is missing. Is there some minimal example of an error message from SymPy I can post to the vscode github?

jverzani commented 2 years ago

This tries to call an object incorrectly. Obscure, but short:


julia> μ.__class__()

I see:


ERROR: PyError ($(Expr(:escape, :(ccall(#=
/Users/verzani/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 =#
@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw)))))
<class 'TypeError'>

TypeError("__new__() missing 1 required positional argument: 'name'")

Stacktrace:

  [1] pyerr_check

    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:62 [inlined]

  [2] pyerr_check

    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:66 [inlined]

  [3] _handle_error(msg::String)

    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/exception.jl:83

  [4] macro expansion

    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:97 [inlined]

  [5] #107

    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 [inlined]

  [6] disable_sigint

    @ ./c.jl:458 [inlined]

  [7] __pycall!

    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:42 [inlined]

  [8] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{},
nargs::Int64, kw::Ptr{Nothing})

    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:29

  [9] _pycall!

    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:11 [inlined]

 [10] #_#114

    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86 [inlined]

 [11] (::PyCall.PyObject)()

    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86

 [12] top-level scope

    @ REPL[20]:1

On Thu, Jan 20, 2022 at 11:31 AM Goran Nakerst @.***> wrote:

Thanks for checking. I am using vscode with Jupyter notebooks. Running the code in the terminal gives me the additional error message from SymPy as well.

I had that the error messages in vscode's Jupyter notebooks sometimes don't get printed at all, but never that some part is missing. Is there some minimal example of an error message from SymPy I can post to the vscode github?

— Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/452#issuecomment-1017688957, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADG6TGBTTLNGPDKWXOWTZTUXA2F3ANCNFSM5MNB2RNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: <JuliaPy/SymPy. @.***>

-- John Verzani Department of Mathematics College of Staten Island, CUNY

jamblejoe commented 2 years ago

Thanks. I only see the PyCall.PyError and not the additional error text from SymPy

PyCall.PyError("\$(Expr(:escape, :(ccall(#= C:\\Users\\16185722\\.julia\\packages\\PyCall\\L0fLP\\src\\pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))", PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00007ffc2b8b7790), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00000000074ae4f0), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x0000000000000000))

@jverzani Thanks for your quick help!

jverzani commented 2 years ago

Oops, maybe just simplify your matrix construction:

eigvals(μ*I(6) .+ rand())

That has the same error message.

On Thu, Jan 20, 2022 at 11:57 AM Goran Nakerst @.***> wrote:

Thanks. I only see the PyCall.PyError and not the additional error text from SymPy

PyCall.PyError("\$(Expr(:escape, :(ccall(#= C:\Users\16185722\.julia\packages\PyCall\L0fLP\src\pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))", PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00007ffc2b8b7790), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x00000000074ae4f0), PyCall.PyObject(Ptr{PyCall.PyObject_struct} @0x0000000000000000))

@jverzani https://github.com/jverzani Thanks for your quick help!

— Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/452#issuecomment-1017714458, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADG6TEEND4KYIY6X4YBK33UXA5HDANCNFSM5MNB2RNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

-- John Verzani Department of Mathematics College of Staten Island, CUNY