On Sep 23, 3:58 pm, Andy <[EMAIL PROTECTED]> wrote:
> I am programming for a project in sage and I want to make use of the
> solve() function. However, I noticed that solve() does not return the
> value of the solved variable but only a statement instead.
>
> For instance, if I wanted to do solve([x^2 - 1], x), I would likely
> get a result containing the strings "x == 1" and "x == -1". This is
> all fine if I just want to view the result, but what if I wanted to
> store the resulting possible values of x in a list? Like have the
> list returned [-1, 1].
>
> Any tricks to solve() that allow the results to be stored in a useable
> form?
There is also the solution_dict keyword.
sage: solve?
...
INPUT:
f -- equation or system of equations (given by a list or
tuple)
*args -- variables to solve for.
solution_dict = True -- return a list of dictionaries
containing the solutions.
...
EXAMPLES:
sage: x, y = var('x, y')
...
sage: solutions=solve([x^2+y^2 == 1, y^2 == x^3 + x + 1], x, y,
solution_dict=True); solutions
[{y: -sqrt(3 - sqrt(3)*I)/sqrt(2), x: (-sqrt(3)*I - 1)/2},
{y: sqrt(3 - sqrt(3)*I)/sqrt(2), x: (-sqrt(3)*I - 1)/2},
{y: -sqrt(sqrt(3)*I + 3)/sqrt(2), x: (sqrt(3)*I - 1)/2},
{y: sqrt(sqrt(3)*I + 3)/sqrt(2), x: (sqrt(3)*I - 1)/2},
{y: -1, x: 0},
{y: 1, x: 0}]
...
Support for this appears pretty flaky, though. The way of stating
your problem that works is
sage: solutions = solve(x^2-1==0, x, solution_dict=True); solutions
[{x: -1}, {x: 1}]
sage: [sol[x] for sol in solutions]
[-1, 1]
There are many ways of stating your problem that don't work:
sage: solve(x^2-1, x, solution_dict=True)
Traceback (most recent call last)
...
TypeError: solve() got an unexpected keyword argument 'solution_dict'
sage: solve([x^2-1], x, solution_dict=True)
Traceback (most recent call last)
...
AttributeError: 'SymbolicVariable' object has no attribute 'left'
sage: solve([x^2-1==0], x, solution_dict=True)
Traceback (most recent call last)
...
AttributeError: 'SymbolicVariable' object has no attribute 'left'
Here's another example that does work:
sage: solutions=solve([x^2+y^2-1, y^2-x^3-x-1], x, y,
solution_dict=True); solutions
[{y: -sqrt(3 - sqrt(3)*I)/sqrt(2), x: (-sqrt(3)*I - 1)/2},
{y: sqrt(3 - sqrt(3)*I)/sqrt(2), x: (-sqrt(3)*I - 1)/2},
{y: -sqrt(sqrt(3)*I + 3)/sqrt(2), x: (sqrt(3)*I - 1)/2},
{y: sqrt(sqrt(3)*I + 3)/sqrt(2), x: (sqrt(3)*I - 1)/2},
{y: -1, x: 0},
{y: 1, x: 0}]
So it looks like when you are solving for a single variable, you can
ask for a solution_dict only with a symbolic equation, and not a
symbolic expression or a list. With two variables, you can apparently
use a list of symbolic equations, or a list of symbolic expressions.
Regards,
JM
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---