Julia how to get coefficients out of SymPy solveset

Viewed 238

I'm using symbolic math in Julia. When I do the differentiation it comes out very nicely, but I cant get the coefficients out

using SymPy
@vars x y
z = x*(10 + 5*x + 4*y)
dz = diff(z,x)
x_s = solveset(dz,x)

how do I get the the coefficients out of x_s?

2 Answers

You can use elements to get the elements of a finite sets as an array:

julia> elements(s)
1-element Array{Sym,1}:
 -2*y/5 - 1

To get the coefficients can be done different ways, but here we convert the value to a Polynomial type, then use its coeffs method:

julia> p = sympy.Poly(elements(s)[1], y)
Poly(-2/5*y - 1, y, domain='QQ')

julia> p.coeffs()
2-element Array{Sym,1}:
 -2/5
   -1

As per my comment, the following works but isn't exactly what I'd describe as pretty:

julia> x_s.__pyobject__.args[1].__pyobject__.args[1] 
-1  

julia> x_s.__pyobject__.args[1].__pyobject__.args[2] 
-2⋅y 
─────
  5  

julia> x_s.__pyobject__.args[1].__pyobject__.args[2].__pyobject__.args[1]
 -2/5

I couldn't find an accessor function in Sympy.jl that simplifies this, but as you say this could be the basis for rolling your own.

Related