- solve(f, *args, **kwds)
- Algebraically solve an equation or system of equations (over the
complex numbers) for given variables. Inequalities and systems
of inequalities are also supported.
INPUT:
- ``f`` - equation or system of equations (given by a
list or tuple)
- ``*args`` - variables to solve for.
- ``solution_dict`` - bool (default: False); if True,
return a list of dictionaries containing the solutions.
EXAMPLES::
sage: x, y = var('x, y')
sage: solve([x+y==6, x-y==4], x, y)
[[x == 5, y == 1]]
sage: solve([x^2+y^2 == 1, y^2 == x^3 + x + 1], x, y)
[[x == -1/2*I*sqrt(3) - 1/2, y == -1/2*sqrt(-I*sqrt(3) + 3)*sqrt(2)],
[x == -1/2*I*sqrt(3) - 1/2, y == 1/2*sqrt(-I*sqrt(3) + 3)*sqrt(2)],
[x == 1/2*I*sqrt(3) - 1/2, y == -1/2*sqrt(I*sqrt(3) + 3)*sqrt(2)],
[x == 1/2*I*sqrt(3) - 1/2, y == 1/2*sqrt(I*sqrt(3) + 3)*sqrt(2)],
[x == 0, y == -1],
[x == 0, y == 1]]
sage: solve([sqrt(x) + sqrt(y) == 5, x + y == 10], x, y)
[[x == -5/2*I*sqrt(5) + 5, y == 5/2*I*sqrt(5) + 5], [x == 5/2*I*sqrt(5) + 5, y == -5/2*I*sqrt(5) + 5]]
sage: solutions=solve([x^2+y^2 == 1, y^2 == x^3 + x + 1], x, y, solution_dict=True)
sage: for solution in solutions: print solution[x].n(digits=3), ",", solution[y].n(digits=3)
-0.500 - 0.866*I , -1.27 + 0.341*I
-0.500 - 0.866*I , 1.27 - 0.341*I
-0.500 + 0.866*I , -1.27 - 0.341*I
-0.500 + 0.866*I , 1.27 + 0.341*I
0.000 , -1.00
0.000 , 1.00
Whenever possible, answers will be symbolic, but with systems of
equations, at times approximations will be given, due to the
underlying algorithm in Maxima::
sage: sols = solve([x^3==y,y^2==x],[x,y]); sols[-1], sols[0]
([x == 0, y == 0], [x == (0.309016994375 + 0.951056516295*I), y == (-0.809016994375 - 0.587785252292*I)])
sage: sols[0][0].rhs().pyobject().parent()
Complex Double Field
If ``f`` is only one equation or expression, we use the solve method
for symbolic expressions, which defaults to exact answers only::
sage: solve([y^6==y],y)
[y == e^(2/5*I*pi), y == e^(4/5*I*pi), y == e^(-4/5*I*pi), y == e^(-2/5*I*pi), y == 1, y == 0]
sage: solve( [y^6 == y], y)==solve( y^6 == y, y)
True
.. note::
For more details about solving a single equations, see
the documentation for its solve.
::
sage: from sage.symbolic.expression import Expression
sage: Expression.solve(x^2==1,x)
[x == -1, x == 1]
We must solve with respect to actual variables::
sage: z = 5
sage: solve([8*z + y == 3, -z +7*y == 0],y,z)
Traceback (most recent call last):
...
TypeError: 5 is not a valid variable.
If we ask for a dictionary for the solutions, we get it::
sage: solve([x^2-1],x,solution_dict=True)
[{x: -1}, {x: 1}]
sage: solve([x^2-4*x+4],x,solution_dict=True)
[{x: 2}]
sage: res = solve([x^2 == y, y == 4],x,y,solution_dict=True)
sage: for soln in res: print "x: %s, y: %s"%(soln[x], soln[y])
x: 2, y: 4
x: -2, y: 4
If there is a parameter in the answer, that will show up as
a new variable. In the following example, ``r1`` is a real free
variable (because of the ``r``)::
sage: solve([x+y == 3, 2*x+2*y == 6],x,y)
[[x == -r1 + 3, y == r1]]
Especially with trigonometric functions, the dummy variable may
be implicitly an integer (hence the ``z``)::
sage: solve([cos(x)*sin(x) == 1/2, x+y == 0],x,y)
[[x == 1/4*pi + pi*z38, y == -1/4*pi - pi*z38]]
Expressions which are not equations are assumed to be set equal
to zero, as with `x` in the following example::
sage: solve([x, y == 2],x,y)
[[x == 0, y == 2]]
If ``True`` appears in the list of equations it is
ignored, and if ``False`` appears in the list then no
solutions are returned. E.g., note that the first
``3==3`` evaluates to ``True``, not to a
symbolic equation.
::
sage: solve([3==3, 1.00000000000000*x^3 == 0], x)
[x == 0]
sage: solve([1.00000000000000*x^3 == 0], x)
[x == 0]
Here, the first equation evaluates to ``False``, so
there are no solutions::
sage: solve([1==3, 1.00000000000000*x^3 == 0], x)
[]
Completely symbolic solutions are supported::
sage: var('s,j,b,m,g')
(s, j, b, m, g)
sage: sys = [ m*(1-s) - b*s*j, b*s*j-g*j ];
sage: solve(sys,s,j)
[[s == 1, j == 0], [s == g/b, j == (b - g)*m/(b*g)]]
sage: solve(sys,(s,j))
[[s == 1, j == 0], [s == g/b, j == (b - g)*m/(b*g)]]
sage: solve(sys,[s,j])
[[s == 1, j == 0], [s == g/b, j == (b - g)*m/(b*g)]]
Inequalities can be also solved::
sage: solve(x^2>8,x)
[[x < -2*sqrt(2)], [x > 2*sqrt(2)]]
TESTS::
sage: solve([sin(x)==x,y^2==x],x,y)
[sin(x) == x, y^2 == x]