# can sage return a list of numbers from solve() instead of expressions?

is there any builtin methods to have solve() return a list of numbers instead of the sequence of expressions that it does? which is to say solve(eqn, x) would return:

$[x_1, x_2, ...,x_n]$

$[x == x_1, x == x_2, ... , x == x_n]$

or if there are not any built in functions recommended approaches? this is in a sage script im rating that at this point in the program is taking polynomials, computing their first and second derivatives, and adding the roots of those derivatives to a list, which needs to be iterable. if there does not exist some sage method or options for this, i believe i would need to revert to regex, which feels like im doing something wrong if thats what im having to do here

edit retag close merge delete

Sort by » oldest newest most voted

There's a good reason for sage to return a list of equations as it does, because that clearly labels for which variable the equations were solved. When you're solving equations in multiple variables, this labelling is even more important.

It's easy to process the return value of solve (and indeed you should not do that with regexes!):

[s.rhs() for s in solve(x^3+x+1,x)]


or

[x.subs(s) for s in solve(x^3+x+1,x)]


Alternatively, if you're actually interested in, say, float approximations to the roots perhaps you should define your polynomials in terms of a polynomial ring. If you're interested in the real roots:

sage: P.<x>=RR[]
sage: f=x^3-2*x+1
sage: f.roots(RR)
[(-1.61803398874989, 1), (0.618033988749895, 1), (1.00000000000000, 1)]
sage: f.roots(RR,multiplicities=false)
[-1.61803398874989, 0.618033988749895, 1.00000000000000]

more

how would the above work for multivariate functions? ive tried

[a.subs(w) for w b.subs(x) for x c.subs(y) for y d.subs(z) for z in sage.solve(((eqn1 == cb1), (eqn2 == cb2), (eqn3 == cb3), (eqn4 == cb4)), (a, b, c, d))]


and

[a.subs(w), b.subs(x), c.subs(y), d.subs(z) for w, x, y, z in sage.solve(((eqn1 == cb1), (eqn2 == cb2), (eqn3 == cb3), (eqn4 == cb4)), (a, b, c, d))]


and neither are valid syntax

this is how,

[(a.subs(w), b.subs(x), c.subs(y), d.subs(z)) for (w, x, y, z) in sage.solve(((eqn1 == cb1), (eqn2 == cb2), (eqn3 == cb3), (eqn4 == cb4)), (a, b, c, d))]


yet better:

solns = sage.solve([eqn1 == cb1, eqn2 == cb2, eqn3 == cb3,
eqn4 == cb4], a, b, c, d, solution_dict=True)
return [[s[a].n(30), s[b].n(30), s[c].n(30), s[d].n(30)] for s in solns]