JuliaPy / SymPy.jl

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

How can I convert tuple to float in SymPy.jl #517

Closed ay111 closed 1 year ago

ay111 commented 1 year ago

How can I convert the result of the computation below to float in Julia. using SymPy @syms x y a = nonlinsolve([x^2 + y^3 - 1, 2x + 3y - 4], (x, y))

I have tried a.evalf(10) and N(a) but a.evalf returned error while N(a) returned the same a.

jverzani commented 1 year ago

So, a is a FiniteSet, so can be converted first with elements:

b = elements(a)  # A Vector{Sym}

The elements are tuples:

julia> first(b).__class__
PyObject <class 'sympy.core.containers.Tuple'>

Containers require broadcasting, but that requires a Julia container, not a python one. I see there is no good conversion here, but this does work:

collect(first(b).__pyobject__) # Vector{Any}

These can have N broadcast over them. So, here we go with all at once:

julia> [N.(collect(bᵢ.__pyobject__)) for bᵢ ∈ elements(a)]
3-element Vector{Vector}:
 Complex{BigFloat}[0.7192952090072009848438408050041592069918887074777771245787120318824758516910227 - 0.2556787519860036796633816425355184837854725255142855627025224516632691084316739im, 0.8538031939951993434374394633305605286720741950148152502808586454116827655393182 + 0.1704525013240024531089210950236789891903150170095237084683483011088460722877841im]
 Complex{BigFloat}[0.7192952090072009848438408050041592069918887074777771245787120318824758516910227 + 0.2556787519860036796633816425355184837854725255142855627025224516632691084316739im, 0.8538031939951993434374394633305605286720741950148152502808586454116827655393182 - 0.1704525013240024531089210950236789891903150170095237084683483011088460722877841im]
 BigFloat[7.936409581985598030312318389991681586016222585044445750842575936235048296617972, -3.957606387990398686874878926661121057344148390029630500561717290823365531078636]

That isn't too satisfactory and it seems I need to add a conversion of tuple, as reaching into pyobject is a break-glass-in-emergency thing.

Let's leave this open until this approach gets cleaned up.

ay111 commented 1 year ago

Thank you for your response.

On Sun, 27 Aug 2023, 6:54 pm john verzani, @.***> wrote:

So, a is a FiniteSet, so can be converted first with elements:

b = elements(a) # A Vector{Sym}

The elements are tuples:

julia> first(b).class PyObject <class 'sympy.core.containers.Tuple'>

Containers require broadcasting, but that requires a Julia container, not a python one. I see there is no good conversion here, but this does work:

collect(first(b).pyobject) # Vector{Any}

These can have N broadcast over them. So, here we go with all at once:

julia> [N.(collect(bᵢ.pyobject)) for bᵢ ∈ elements(a)] 3-element Vector{Vector}: Complex{BigFloat}[0.7192952090072009848438408050041592069918887074777771245787120318824758516910227 - 0.2556787519860036796633816425355184837854725255142855627025224516632691084316739im, 0.8538031939951993434374394633305605286720741950148152502808586454116827655393182 + 0.1704525013240024531089210950236789891903150170095237084683483011088460722877841im] Complex{BigFloat}[0.7192952090072009848438408050041592069918887074777771245787120318824758516910227 + 0.2556787519860036796633816425355184837854725255142855627025224516632691084316739im, 0.8538031939951993434374394633305605286720741950148152502808586454116827655393182 - 0.1704525013240024531089210950236789891903150170095237084683483011088460722877841im] BigFloat[7.936409581985598030312318389991681586016222585044445750842575936235048296617972, -3.957606387990398686874878926661121057344148390029630500561717290823365531078636]

That isn't too satisfactory and it seems I need to add a conversion of tuple, as reaching into pyobject is a break-glass-in-emergency thing.

Let's leave this open until this approach gets cleaned up.

— Reply to this email directly, view it on GitHub https://github.com/JuliaPy/SymPy.jl/issues/517#issuecomment-1694713331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK4NBY5LEHTJUL5F7NJQMALXXN3SZANCNFSM6AAAAAA37XHTPE . You are receiving this because you authored the thread.Message ID: @.***>

JianghuiDu commented 4 months ago

It looks like in the latest version function SymPy.elements() doesn't exist anymore. Is there a new way to extract elements from Finiteset objects?

jverzani commented 4 months ago

Does collect work? (If not, can you share the code that generated the finite set?)

JianghuiDu commented 4 months ago

Yeah, it works. It seems now the type of solveset result has been changed to Set by default? It would be good to have a deprecation warning coming from v1.0.

jverzani commented 4 months ago

Yeah, that should have happened. Sorry.