Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sympy throws on returning a tuple out of diophantine call #561

Closed
DanielFabian opened this issue Feb 19, 2025 · 2 comments
Closed

Sympy throws on returning a tuple out of diophantine call #561

DanielFabian opened this issue Feb 19, 2025 · 2 comments

Comments

@DanielFabian
Copy link

DanielFabian commented Feb 19, 2025

I respect that most bugs are in SymPyCore, but this one looks like a serialization / type error, so maybe it belongs here (happy to move if you think no).

When I tried following a sympy tutorial, I consistently get a type error. Here's the python snippet

from sympy import symbols
from sympy.solvers.diophantine import diophantine
x, y, z = symbols("x, y, z", integer=True)
print(diophantine(x**2 + y**2 - z**2))

and this is my translation to julia:

using SymPy

@syms a::integer, b::integer, c::integer
pythag_eq = Eq(a^2 + b^2, c^2)

typeof(pythag_eq)

sympy.solvers.diophantine(pythag_eq)

And I get this error:

I'm trying to learn how to translate python examples to julia. In particulae, I have a little bit of python code:

```python
from sympy import symbols
from sympy.solvers.diophantine import diophantine
x, y, z = symbols("x, y, z", integer=True)
print(diophantine(2*x + 3*y - 5))

and I'm trying to translate it to julia as this:

using SymPy

@syms a::integer, b::integer, c::integer
pythag_eq = Eq(a^2 + b^2, c^2)

typeof(pythag_eq)

sympy.solvers.diophantine(pythag_eq)

and I'm getting crashes at the last call:

ERROR: MethodError: Cannot `convert` an object of type 
  Tuple{PyCall.PyObject, PyCall.PyObject, PyCall.PyObject} to an object of type 
  Sym
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  Sym(::Tuple)
   @ SymPyCore ~/.julia/packages/SymPyCore/Dg45i/src/types.jl:38
  Sym(::T) where T
   @ SymPyCore ~/.julia/packages/SymPyCore/Dg45i/src/types.jl:30
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:126
  ...

Stacktrace:
 [1] push!(a::Vector{Sym}, item::Tuple{PyCall.PyObject, PyCall.PyObject, PyCall.PyObject})
   @ Base ./array.jl:1260
 [2] collect(::Type{Sym}, o::PyCall.PyObject)
   @ PyCall ~/.julia/packages/PyCall/1gn3u/src/pyiterator.jl:93
 [3] ↑(::Type{PyCall.PyObject}, x::PyCall.PyObject)
   @ SymPy ~/.julia/packages/SymPy/qBv8D/src/python_connection.jl:52
 [4] ↑(x::PyCall.PyObject)
   @ SymPyCore ~/.julia/packages/SymPyCore/Dg45i/src/gen_methods.jl:191
 [5] (::SymPyCore.SymbolicCallable{PyCall.PyObject})(::Sym{PyCall.PyObject}, ::Vararg{Sym{…}}; kwargs::@Kwargs{})

I have verified that `diophantine` is found as a python callable object. It looks more like an interop issue. But I have no idea what.

I have verified that diophantine is found as a python callable object. It looks more like an interop issue. But I have no idea what.

I even asked on the julia discord and someone else looked at the issue too:

https://discord.com/channels/762167454973296644/1341536826501890198

and when they added a type conversion like this:

Base.convert(::Type{Sym}, pos::NTuple{N, SymPy.PyCall.PyObject}) where N = Sym(Ref(pos));

we do indeed see a result, which is why they recommended to file an upstream bug. Apparently the result from the diophantine call throws every time (fairly independent of the input).

@jverzani jverzani mentioned this issue Feb 19, 2025
@jverzani
Copy link
Collaborator

Thanks. There are a few ways to handle this. This function returns a python object which is a set of tuples. The conversion of this type wasn't begin picked up properly as the tuple wasn't being converted. That should be fixed now with #562 Let me know if not.

@DanielFabian
Copy link
Author

thx, can confirm it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants