Ask Your Question

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

asked 2019-12-29 00:02:07 -0600

alienfetuseater gravatar image

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]$

instead of:

$[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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-12-29 02:47:12 -0600

nbruin gravatar image

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)]


[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]
edit flag offensive delete link 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))]


[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

alienfetuseater gravatar imagealienfetuseater ( 2020-01-02 22:02:15 -0600 )edit

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))]
alienfetuseater gravatar imagealienfetuseater ( 2020-01-02 22:27:52 -0600 )edit

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]
alienfetuseater gravatar imagealienfetuseater ( 2020-01-02 22:43:44 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-12-29 00:02:07 -0600

Seen: 90 times

Last updated: Dec 29 '19