Ask Your Question

Revision history [back]

Hi RAC,

You're almost there -- you just need to save the output of the solve command:

sage: solutions = solve([eq1, eq2, eq3], x, y, z)
sage: print solutions
[
[x == 1, y == 2, z == 3]
]
sage: print solutions[0]
[x == 1, y == 2, z == 3]
sage: soln = solutions[0]
sage: print soln[0]
x == 1

The output is a list, whose contents are solutions and multiplicities. In this case the solution has multiplicity 1, so the list contains a single item, which is another list -- that list is the list of expressions which constitutes a solution. solutions[0] is the first item of solutions -- that is, the solution. soln[0] is the first item of that solution -- that is, the expression which gives the value of x:

sage: type(soln[0])
<type 'sage.symbolic.expression.Expression'>

Now to get the value itself, you need to just take the right hand side of that expression:

sage: expr = soln[0]
sage: expr.rhs()
1
sage: expr.lhs()
x

Note that you don't have to define all these intermediate variables if you don't want to -- I just did it to make the explanation a little clearer:

sage: solve([eq1, eq2, eq3], x, y, z)[0][0].rhs()
1

Using the other entries in the soln list will give the values of the other variables:

sage: soln[1].rhs()
2
sage: soln[2].rhs()
3