SymPy.jl icon indicating copy to clipboard operation
SymPy.jl copied to clipboard

conversion of SymPy integer to Julia Int fails

Open mzaffalon opened this issue 7 years ago • 19 comments

Using Julia v0.5.2, the following conversion fails: Int(Sym(0)). It works fine in v0.6-rc2.

mzaffalon avatar May 25 '17 11:05 mzaffalon

I'm not seeing this:

julia> Int(Sym(0))
0

julia> VERSION
v"0.5.1"

Are you on v"0.5.2"?

jverzani avatar May 25 '17 13:05 jverzani

Yes, I updated the initial comment.

mzaffalon avatar May 25 '17 18:05 mzaffalon

Can you paste the error message? If it is a hassle, I could install that version, but I've been working with v"0.6.0" for a while now so it would be a bit of trouble. I'm not sure there will be an easy workaround, but hopefully there is.

jverzani avatar May 25 '17 18:05 jverzani

My apologies. Here is comes.

julia> Int(Sym(0))
ERROR: PyError (PyInt_AsSsize_t) <class 'TypeError'>
TypeError('an integer is required',)

 in pyerr_check at C:\Users\Michele.Zaffalon\.julia\v0.5\PyCall\src\exception.jl:56 [inlined]
 in convert(::Type{Int64}, ::PyCall.PyObject) at C:\Users\Michele.Zaffalon\.julia\v0.5\PyCall\src\conversions.jl:31
 in Int64(::SymPy.Sym) at .\sysimg.jl:53

mzaffalon avatar May 25 '17 19:05 mzaffalon

This might be a change in pycall? I wonder, as we did make any changes in SymPy. What version of PyCall do you have installed?

jverzani avatar May 25 '17 19:05 jverzani

Pkg.status() reports version 1.12.0.

mzaffalon avatar May 25 '17 19:05 mzaffalon

Yeah, that's what I have too. Does this raise an error:

convert(Int, PyCall.PyObject(1))

I'm guessing it will, but if not we might be able to do something. Otherwise, I'm stuck like I am with the other issue about converting PI and E.

jverzani avatar May 25 '17 19:05 jverzani

No error on the conversion:

julia> convert(Int, PyCall.PyObject(1))
1

mzaffalon avatar May 25 '17 19:05 mzaffalon

That's strange, but it seems that there have been some changes to the definition (::Type{T})(arg) where {T} = convert(T, arg). Basically with the old definition, we had

Int(x) -> convert(Int, x) -> convert(Int, PyObject(x)) which is equivalent to convert(Int, PyObject(1))

I'll need to update my 2-week old julia to look further.

jverzani avatar May 25 '17 20:05 jverzani

I am totally lost on the new type system. At the moment, the workaround I use is Int(Float64(Sym(0))).

mzaffalon avatar May 25 '17 20:05 mzaffalon

Hmm, it is something else:

julia> Int(Sym(0))
0

julia> VERSION
v"0.5.2"

I'm not sure where to go with this, sorry.

jverzani avatar May 25 '17 21:05 jverzani

I get the same error both on Windows 7 and on Debian with Julia v0.5.2 and on Debian with Julia v0.5.1; I use Anaconda 3. What python distribution do you have?

mzaffalon avatar May 26 '17 04:05 mzaffalon

One from Conda.jl: "/Users/verzani/.julia/v0.6/Conda/deps/usr/lib/libpython2.7". That might be it, but I'm not sure how to test.

jverzani avatar May 26 '17 13:05 jverzani

This is with Julia v0.6-rc2 on Debian:

julia> Int(Sym(0))
ERROR: PyError (ccall(@pysym(PyInt_AsSsize_t), Int, (PyPtr,), asscalar(po))) <class 'TypeError'>
TypeError('an integer is required',)

Stacktrace:
 [1] pyerr_check at /home/michele/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]
 [2] convert(::Type{Int64}, ::PyCall.PyObject) at /home/michele/.julia/v0.6/PyCall/src/conversions.jl:31
 [3] Int64(::SymPy.Sym) at ./sysimg.jl:24

mzaffalon avatar Jun 04 '17 13:06 mzaffalon

I just can't replicate to debug. I just did a fresh install on an Ubuntu vm using PyCall via ENV["PYTHON"]="" and I get no error, just the conversion.

On Sun, Jun 4, 2017 at 9:58 AM, Michele Zaffalon [email protected] wrote:

This is with Julia v0.6-rc2 on Debian:

julia> Int(Sym(0)) ERROR: PyError (ccall(@pysym(PyInt_AsSsize_t), Int, (PyPtr,), asscalar(po))) <class 'TypeError'> TypeError('an integer is required',)

Stacktrace: [1] pyerr_check at /home/michele/.julia/v0.6/PyCall/src/exception.jl:56 [inlined] [2] convert(::Type{Int64}, ::PyCall.PyObject) at /home/michele/.julia/v0.6/PyCall/src/conversions.jl:31 [3] Int64(::SymPy.Sym) at ./sysimg.jl:24

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/159#issuecomment-306041660, or mute the thread https://github.com/notifications/unsubscribe-auth/AAZvTOtpMZjZEnUZBEjXJM2XOYLWGMqvks5sArgbgaJpZM4NmSKj .

-- John Verzani Chair, Department of Mathematics College of Staten Island, CUNY [email protected]

jverzani avatar Jun 04 '17 14:06 jverzani

Can you see if there is some convert method that works on your setup and we can try to see if that works on mine?

On Sun, Jun 4, 2017 at 10:37 AM, John Verzani [email protected] wrote:

I just can't replicate to debug. I just did a fresh install on an Ubuntu vm using PyCall via ENV["PYTHON"]="" and I get no error, just the conversion.

On Sun, Jun 4, 2017 at 9:58 AM, Michele Zaffalon <[email protected]

wrote:

This is with Julia v0.6-rc2 on Debian:

julia> Int(Sym(0)) ERROR: PyError (ccall(@pysym(PyInt_AsSsize_t), Int, (PyPtr,), asscalar(po))) <class 'TypeError'> TypeError('an integer is required',)

Stacktrace: [1] pyerr_check at /home/michele/.julia/v0.6/PyCall/src/exception.jl:56 [inlined] [2] convert(::Type{Int64}, ::PyCall.PyObject) at /home/michele/.julia/v0.6/PyCall/src/conversions.jl:31 [3] Int64(::SymPy.Sym) at ./sysimg.jl:24

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/159#issuecomment-306041660, or mute the thread https://github.com/notifications/unsubscribe-auth/AAZvTOtpMZjZEnUZBEjXJM2XOYLWGMqvks5sArgbgaJpZM4NmSKj .

-- John Verzani Chair, Department of Mathematics College of Staten Island, CUNY [email protected]

-- John Verzani Chair, Department of Mathematics College of Staten Island, CUNY [email protected]

jverzani avatar Jun 04 '17 14:06 jverzani

@jverzani I am not sure I know what to do.

In the meanwhile, I have tried your suggestion above ENV["PYTHON"]="": with Conda.jl Python2.7, the conversion works. I will be re-installing Miniconda2 and 3 to see if there is any difference there.

mzaffalon avatar Jun 04 '17 19:06 mzaffalon

It seems it is a Miniconda3 issue only. The conversion works in Miniconda2.

mzaffalon avatar Jun 04 '17 20:06 mzaffalon

Can this be closed as

julia> typeof(convert(Int, Sym(0)))
Int64

on

SymPy v1.1.6


julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

?

jamblejoe avatar Aug 16 '22 15:08 jamblejoe