Package sage :: Package calculus :: Module equations
[hide private]
[frames] | no frames]

Module equations

source code


Symbolic Equations and Inequalities.

\sage can solve symbolic equations and express inequalities.
For example, we derive the quadratic formula as follows:

    sage: a,b,c = var('a,b,c')
    sage: qe = (a*x^2 + b*x + c == 0)
    sage: print qe
                                     2
                                  a x  + b x + c == 0
    sage: print solve(qe, x)
    [
                                          2
                                  - sqrt(b  - 4 a c) - b
                              x == ----------------------
                                           2 a,
                                         2
                                   sqrt(b  - 4 a c) - b
                               x == --------------------
                                           2 a
    ]

AUTHORS:
    -- Bobby Moretti: initial version (based on a trick that
       Robert Bradshaw suggested).
    -- William Stein: second version
    -- William Stein (2007-07-16): added arithmetic with symbolic equations

EXAMPLES:
    sage: x,y,a = var('x,y,a')
    sage: f = x^2 + y^2 == 1
    sage: f.solve(x)
    [x == -sqrt(1 - y^2), x == sqrt(1 - y^2)]

    sage: f = x^5 + a
    sage: solve(f==0,x)
    [x == e^(2*I*pi/5)*(-a)^(1/5), x == e^(4*I*pi/5)*(-a)^(1/5), x == e^(-(4*I*pi/5))*(-a)^(1/5), x == e^(-(2*I*pi/5))*(-a)^(1/5), x == (-a)^(1/5)]



You can also do arithmetic with inequalities, as illustrated below:
    sage: var('x y')
    (x, y)
    sage: f = x + 3 == y - 2
    sage: f
    x + 3 == y - 2
    sage: g = f - 3; g
    x == y - 5
    sage: h =  x^3 + sqrt(2) == x*y*sin(x)
    sage: h
    x^3 + sqrt(2) == x*sin(x)*y
    sage: h - sqrt(2)
    x^3 == x*sin(x)*y - sqrt(2)
    sage: h + f
    x^3 + x + sqrt(2) + 3 == x*sin(x)*y + y - 2
    sage: f = x + 3 < y - 2
    sage: g = 2 < x+10
    sage: f - g
    x + 1 < y - x - 12
    sage: f + g
    x + 5 < y + x + 8
    sage: f*(-1)
    -x - 3 > 2 - y

TESTS:
We test serializing symbolic equations:
    sage: eqn = x^3 + 2/3 >= x
    sage: loads(dumps(eqn))
    x^3 + 2/3 >= x
    sage: loads(dumps(eqn)) == eqn 
    True



Classes [hide private]
  SymbolicEquation
A symbolic equation, which consists of a left hand side, an operator and a right hand side.
  GenericDeclaration
Functions [hide private]
 
var_cmp(x, y)
Return comparison of the two variables x and y, which is just the comparison of the underlying string representations of the variables.
source code
 
is_SymbolicEquation(x)
Return True if x is a symbolic equation.
source code
 
preprocess_assumptions(args)
Turns a list of the form (var1, var2, ..., 'property') into a sequence of declarations (var1 is property), (var2 is property), ...
source code
 
assume(*args)
Make the given assumptions.
source code
 
forget(*args)
Forget the given assumption, or call with no arguments to forget all assumptions.
source code
 
assumptions()
List all current symbolic assumptions.
source code
 
_forget_all()
Forget all symbolic assumptions.
source code
 
solve(f, *args, **kwds)
Algebraically solve an equation of system of equations for given variables.
source code
 
string_to_list_of_solutions(s)
Used internally by the symbolic solve command to convert the output of Maxima's solve command to a list of solutions in Sage's symbolic package.
source code
 
solve_mod(eqns, modulus)
Return all solutions to an equation or list of equations modulo the given integer modulus.
source code
Variables [hide private]
  _assumptions = []
  symbols = {<built-in function eq>: ' == ', <built-in function ...
  maxima_symbols = {<built-in function eq>: '=', <built-in funct...
  latex_symbols = {<built-in function eq>: ' = ', <built-in func...
  comparisons = {<built-in function eq>: set([0]), <built-in fun...
  opposite_op = {<built-in function eq>: <built-in function eq>,...
  objs = Category of objects
Function Details [hide private]

var_cmp(x, y)

source code 

Return comparison of the two variables x and y, which is just the
comparison of the underlying string representations of the
variables.  This is used internally by the Calculus package.

INPUT:
    x, y -- symbolic variables

OUTPUT:
    Python integer; either -1, 0, or 1.

EXAMPLES:
    sage: sage.calculus.equations.var_cmp(x,x)
    0
    sage: sage.calculus.equations.var_cmp(x,var('z'))
    -1
    sage: sage.calculus.equations.var_cmp(x,var('a'))
    1

is_SymbolicEquation(x)

source code 

Return True if x is a symbolic equation.

EXAMPLES:
The following two examples are symbolic equations:
    sage: is_SymbolicEquation(sin(x) == x)
    True
    sage: is_SymbolicEquation(sin(x) < x)
    True
    
This is not, since \code{2==3} evaluates to the boolean \code{False}:
    sage: is_SymbolicEquation(2 == 3)
    False

However here since both 2 and 3 are coerced to be symbolic, we obtain
a symbolic equation:
    sage: is_SymbolicEquation(SR(2) == SR(3))
    True

preprocess_assumptions(args)

source code 

Turns a list of the form (var1, var2, ..., 'property') into a sequence
of declarations (var1 is property), (var2 is property), ...

EXAMPLES: 
    sage: from sage.calculus.equations import preprocess_assumptions
    sage: preprocess_assumptions([x, 'integer', x > 4])
    [x is integer, x > 4]
    sage: var('x,y')
    (x, y)
    sage: preprocess_assumptions([x, y, 'integer', x > 4, y, 'even'])
    [x is integer, y is integer, x > 4, y is even]

assume(*args)

source code 

Make the given assumptions.

INPUT:
    *args -- assumptions

EXAMPLES:
    sage: assume(x > 0)
    sage: bool(sqrt(x^2) == x)
    True
    sage: forget()
    sage: bool(sqrt(x^2) == x)
    False

An integer constraint:
    sage: var('n, P, r, r2')
    (n, P, r, r2)
    sage: assume(n, 'integer')
    sage: c = P*e^(r*n)
    sage: d = P*(1+r2)^n
    sage: solve(c==d,r2)
    [r2 == e^r - 1]
    
    sage: sin(n*pi)
    0
    sage: forget()
    sage: sin(n*pi)
    sin(pi*n)

forget(*args)

source code 

Forget the given assumption, or call with no arguments to forget
all assumptions.

Here an assumption is some sort of symbolic constraint. 

INPUT:
    *args -- assumptions (default: forget all assumptions)

EXAMPLES:
We define and forget multiple assumptions:
    sage: var('x,y,z')
    (x, y, z)
    sage: assume(x>0, y>0, z == 1, y>0)
    sage: assumptions()
    [x > 0, y > 0, z == 1]
    sage: forget(x>0, z==1)
    sage: assumptions()
    [y > 0]
    sage: assume(y, 'even')
    sage: assumptions()
    [y > 0, y is even]
    sage: cos(y*pi)
    1
    sage: forget()
    sage: cos(y*pi)
    cos(pi*y)
    sage: assumptions()
    []

assumptions()

source code 

List all current symbolic assumptions.

EXAMPLES:
    sage: var('x,y,z, w')
    (x, y, z, w)
    sage: forget()
    sage: assume(x^2+y^2 > 0)
    sage: assumptions()
    [y^2 + x^2 > 0]
    sage: forget(x^2+y^2 > 0)
    sage: assumptions()
    []
    sage: assume(x > y)
    sage: assume(z > w)
    sage: assumptions()
    [x > y, z > w]
    sage: forget()
    sage: assumptions()
    []

_forget_all()

source code 

Forget all symbolic assumptions.

This is called by \code{forget()}.

EXAMPLES:
    sage: var('x,y')
    (x, y)
    sage: assume(x > 0, y < 0)
    sage: bool(x*y < 0)      # means definitely true
    True
    sage: bool(x*y > 0)      # might not be true
    False
    sage: forget()    # implicitly calls _forget_all
    sage: bool(x*y < 0)      # might not be true
    False
    sage: bool(x*y > 0)      # might not be true
    False

solve(f, *args, **kwds)

source code 

Algebraically solve an equation of system of equations for given variables.

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: 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 == (-sqrt(3)*I - 1)/2, 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 == 0, y == -1],
     [x == 0, y == 1]]
    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}]
    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

If \code{True} appears in the list of equations it is ignored, and if
\code{False} appears in the list then no solutions are returned.  E.g.,
note that the first \code{3==3} evaluates to \code{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 \code{False}, so there are no solutions:
    sage: solve([1==3, 1.00000000000000*x^3 == 0], x)
    []

    sage: var('s,i,b,m,g')
    (s, i, b, m, g)
    sage: sys = [ m*(1-s) - b*s*i, b*s*i-g*i ];
    sage: solve(sys,s,i);
    [[s == 1, i == 0], [s == g/b, i == (b - g)*m/(b*g)]]
    sage: solve(sys,[s,i]);
    [[s == 1, i == 0], [s == g/b, i == (b - g)*m/(b*g)]]
    

string_to_list_of_solutions(s)

source code 

Used internally by the symbolic solve command to convert the
output of Maxima's solve command to a list of solutions in
Sage's symbolic package. 

EXAMPLES:
We derive the (monic) quadratic formula:
    sage: var('x,a,b')
    (x, a, b)
    sage: solve(x^2 + a*x + b == 0, x)
    [x == (-sqrt(a^2 - 4*b) - a)/2, x == (sqrt(a^2 - 4*b) - a)/2]

Behind the scenes when the above is evaluated the function
\code{string_to_list_of_solutions} is called with input the
string $s$ below:
    sage: s = '[x=-(sqrt(a^2-4*b)+a)/2,x=(sqrt(a^2-4*b)-a)/2]'
    sage: sage.calculus.equations.string_to_list_of_solutions(s)
    [x == (-sqrt(a^2 - 4*b) - a)/2, x == (sqrt(a^2 - 4*b) - a)/2]

solve_mod(eqns, modulus)

source code 

Return all solutions to an equation or list of equations modulo 
the given integer modulus.  Each equation must involve only 
polynomials in 1 or many variables. 

The solutions are returned as $n$-tuples, where $n$ is the 
number of variables appearing anywhere in the given equations.  
The variables are in alphabetical order. 


INPUT:
    eqns -- equation or list of equations
    modulus -- an integer 

EXAMPLES:
    sage: var('x,y')
    (x, y)
    sage: solve_mod([x^2 + 2 == x, x^2 + y == y^2], 14)
    [(2, 4), (6, 4), (9, 4), (13, 4)]
    sage: solve_mod([x^2 == 1, 4*x  == 11], 15)
    [(14,)]

Fermat's equation modulo 3 with exponent 5:
    sage: var('x,y,z')
    (x, y, z)
    sage: solve_mod([x^5 + y^5 == z^5], 3)
    [(0, 0, 0), (0, 1, 1), (0, 2, 2), (1, 0, 1), (1, 1, 2), (1, 2, 0), (2, 0, 2), (2, 1, 0), (2, 2, 1)]
    
WARNING:
    Currently this naively enumerates all possible solutions.
    The interface is good, but the algorithm is horrible if the
    modulus is at all large!   \sage \strong{does} have the ability to do
    something much faster in certain cases at least by using
    the Chinese Remainder Theorem, Gr\"obner basis, linear algebra
    techniques, etc.  But for a lot of toy problems this function
    as is might be useful.  At least it establishes an interface.


Variables Details [hide private]

symbols

Value:
{<built-in function eq>: ' == ',
 <built-in function ge>: ' >= ',
 <built-in function gt>: ' > ',
 <built-in function le>: ' <= ',
 <built-in function lt>: ' < ',
 <built-in function ne>: ' != '}

maxima_symbols

Value:
{<built-in function eq>: '=',
 <built-in function ge>: ' >= ',
 <built-in function gt>: ' > ',
 <built-in function le>: ' <= ',
 <built-in function lt>: ' < ',
 <built-in function ne>: '#'}

latex_symbols

Value:
{<built-in function eq>: ' = ',
 <built-in function ge>: ' \\geq ',
 <built-in function gt>: ' > ',
 <built-in function le>: ' \\leq ',
 <built-in function lt>: ' < ',
 <built-in function ne>: ' \\neq '}

comparisons

Value:
{<built-in function eq>: set([0]),
 <built-in function ge>: set([0, 1]),
 <built-in function gt>: set([1]),
 <built-in function le>: set([-1, 0]),
 <built-in function lt>: set([-1]),
 <built-in function ne>: set([-1, 1])}

opposite_op

Value:
{<built-in function eq>: <built-in function eq>,
 <built-in function ge>: <built-in function le>,
 <built-in function gt>: <built-in function lt>,
 <built-in function le>: <built-in function ge>,
 <built-in function lt>: <built-in function gt>,
 <built-in function ne>: <built-in function ne>}