On Nov 26, 2008, at 12:21 PM, Jason Grout wrote:
> Eli wrote:
>> Hello,
>> In the sage tutorial, I found how to solve equations:
>>
>> sage: x, b, c = var('x b c')
>> sage: solve([x^2 + b*x + c == 0],x)
>> [x == (-sqrt(b^2 - 4*c) - b)/2, x == (sqrt(b^2 - 4*c) - b)/2]
>>
>> However, I could not find how to assign the solution to some
>> variable.
>> That is, something that will do:
>> assign the value (-sqrt(b^2 - 4*c) - b)/2 (first solution of the
>> equation) to the variable X
>> assign the value (sqrt(b^2 - 4*c) - b)/2 (second solution of the
>> equation) to the variable Y
>>
>> How can this be done ?
>>
>
> Here is a session showing one way to accomplish that. The key is the
> solution_dict argument.
>
>
> sage: f=x^2+b*x+c == 0
> sage: soln = f.solve(x,solution_dict=True)
> sage: soln
> [{x: (-sqrt(b^2 - 4*c) - b)/2}, {x: (sqrt(b^2 - 4*c) - b)/2}]
> sage: soln[0][x]
> (-sqrt(b^2 - 4*c) - b)/2
> sage: soln[1][x]
> (sqrt(b^2 - 4*c) - b)/2
> sage: X=soln[0][x]
> sage: Y=soln[1][x]
> sage: X
> (-sqrt(b^2 - 4*c) - b)/2
> sage: Y
> (sqrt(b^2 - 4*c) - b)/2
You can also use the fact that it's a list of equations, which have
rhs() and lhs() methods.
sage: sage: solve([x^2 + b*x + c == 0],x)
[x == (-sqrt(b^2 - 4*c) - b)/2, x == (sqrt(b^2 - 4*c) - b)/2]
sage: all = solve([x^2 + b*x + c == 0],x)
sage: all[0]
x == (-sqrt(b^2 - 4*c) - b)/2
sage: all[0].rhs()
(-sqrt(b^2 - 4*c) - b)/2
- Robert
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---