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

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

Closed
ay111 opened this issue Aug 26, 2023 · 6 comments
Closed

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

ay111 opened this issue Aug 26, 2023 · 6 comments

Comments

@ay111
Copy link

ay111 commented Aug 26, 2023

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
Copy link
Collaborator

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
Copy link
Author

ay111 commented Aug 27, 2023 via email

@JianghuiDu
Copy link

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
Copy link
Collaborator

jverzani commented Jun 2, 2024

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

@JianghuiDu
Copy link

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
Copy link
Collaborator

jverzani commented Jun 2, 2024

Yeah, that should have happened. Sorry.

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

3 participants