Algebraic equations

This module contain solvers for all kinds of equations, algebraic or transcendental.

diofant.solvers.solvers.solve(f, *symbols, **flags)[source]

Algebraically solves equation or system of equations.

Parameters:
  • f (Expr, Equality or iterable of above) – All expressions are assumed to be equal to 0.

  • *symbols (tuple) – If none symbols given (empty tuple), free symbols of expressions will be used.

  • **flags (dict) – A dictionary of following parameters:

    check : bool, optional

    If False, don’t do any testing of solutions. Default is True, i.e. the solutions are checked and those that doesn’t satisfy given assumptions on symbols solved for or make any denominator zero - are automatically excluded.

    warn : bool, optional

    Show a warning if checksol() could not conclude. Default is False.

    simplify : bool, optional

    Enable simplification (default) for all but polynomials of order 3 or greater before returning them and (if check is not False) use the general simplify function on the solutions and the expression obtained when they are substituted into the function which should be zero.

    rational : bool or None, optional

    If True, recast Floats as Rational. If None (default), Floats will be recast as rationals but the answer will be recast as Floats. If the flag is False then nothing will be done to the Floats.

    cubics, quartics, quintics : bool, optional

    Return explicit solutions (with radicals, which can be quite long) when, respectively, cubic, quartic or quintic expressions are encountered. Default is True. If False, RootOf instances will be returned instead.

Examples

Single equation:

>>> solve(x**2 - y**2)
[{x: -y}, {x: y}]
>>> solve(x**2 - 1)
[{x: -1}, {x: 1}]

We could restrict solutions by using assumptions:

>>> p = Symbol("p", positive=True)
>>> solve(p**2 - 1)
[{p: 1}]

Several equations:

>>> solve((x + 5*y - 2, -3*x + 6*y - 15))
[{x: -3, y: 1}]
>>> solve((x + 5*y - 2, -3*x + 6*y - z))
[{x: -5*z/21 + 4/7, y: z/21 + 2/7}]

No solution:

>>> solve([x + 3, x - 3])
[]

Notes

When an object other than a Symbol is given as a symbol, it is isolated algebraically and an implicit solution may be obtained. This is mostly provided as a convenience to save one from replacing the object with a Symbol and solving for that Symbol. It will only work if the specified object can be replaced with a Symbol using the subs method.

>>> solve(f(x) - x, f(x))
[{f(x): x}]
>>> solve(f(x).diff(x) - f(x) - x, f(x).diff(x))
[{Derivative(f(x), x): x + f(x)}]

See also

diofant.solvers.recurr.rsolve()
solving recurrence equations
diofant.solvers.ode.dsolve()
solving differential equations
diofant.solvers.inequalities.reduce_inequalities()
solving inequalities
diofant.solvers.solvers.solve_linear(f, x)[source]

Solve equation f wrt variable x.

Returns:tuple(x, solution), if there is a linear solution, (0, 1) if f is independent of the symbol x, (0, 0) if solution set any denominator of f to zero or (numerator, denominator) of f, if it’s a nonlinear expression wrt x.

Examples

>>> solve_linear(1/x - y**2, x)
(x, y**(-2))
>>> solve_linear(x**2/y**2 - 3, x)
(x**2 - 3*y**2, y**2)
>>> solve_linear(y, x)
(0, 1)
>>> solve_linear(1/(1/x - 2), x)
(0, 0)
diofant.solvers.solvers.minsolve_linear_system(system, *symbols, **flags)[source]

Find a particular solution to a linear system.

In particular, try to find a solution with the minimal possible number of non-zero variables. This is a very computationally hard problem.

Parameters:
  • system (Matrix) – Nx(M+1) matrix, which means it has to be in augmented form.

  • *symbols (list) – List of M Symbol’s.

  • **flags (dict) – A dictionary of following parameters:

    quick : boolean, optional

    If True, a heuristic is used. Otherwise (default) a naive algorithm with exponential complexity is used.

diofant.solvers.solvers.checksol(f, sol, **flags)[source]

Checks whether sol is a solution of equations f.

Examples

>>> checksol(x**4 - 1, {x: 1})
True
>>> checksol(x**4 - 1, {x: 0})
False
>>> checksol(x**2 + y**2 - 5**2, {x: 3, y: 4})
True
Returns:

bool or None – Return True, if solution satisfy all equations in f. Return False, if a solution doesn’t satisfy any equation. Else (i.e. one or more checks are inconclusive), return None.

Parameters:
  • f (Expr or iterable of Expr’s) – Equations to substitute solutions in.

  • sol (dict of Expr’s) – Mapping of symbols to values.

  • **flags (dict) – A dictionary of following parameters:

    minimal : bool, optional

    Do a very fast, minimal testing. Default is False.

    warn : bool, optional

    Show a warning if checksol() could not conclude. Default is False.

    simplify : bool, optional

    Simplify solution before substituting into function and simplify the function before trying specific simplifications. Default is True.

    force : bool, optional

    Make positive all symbols without assumptions regarding sign. Default is False.

Systems of Polynomial Equations

Solvers of systems of polynomial equations.

diofant.solvers.polysys.solve_linear_system(system, *symbols, **flags)[source]

Solve system of linear equations.

Both under- and overdetermined systems are supported. The possible number of solutions is zero, one or infinite.

Parameters:
  • system (Matrix) – Nx(M+1) matrix, which means it has to be in augmented form. This matrix will not be modified.
  • *symbols (list) – List of M Symbol’s
Returns:

solution (dict or None) – Respectively, this procedure will return None or a dictionary with solutions. In the case of underdetermined systems, all arbitrary parameters are skipped. This may cause a situation in which an empty dictionary is returned. In that case, all symbols can be assigned arbitrary values.

Examples

Solve the following system:

   x + 4 y ==  2
-2 x +   y == 14
>>> system = Matrix(((1, 4, 2), (-2, 1, 14)))
>>> solve_linear_system(system, x, y)
{x: -6, y: 2}

A degenerate system returns an empty dictionary.

>>> system = Matrix(((0, 0, 0), (0, 0, 0)))
>>> solve_linear_system(system, x, y)
{}
diofant.solvers.polysys.solve_poly_system(eqs, *gens, **args)[source]

Solve a system of polynomial equations.

Polynomial system may have finite number of solutions or infinitely many (positive-dimensional systems).

References

Examples

>>> solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y)
[{x: 0, y: 0}, {x: 2, y: -sqrt(2)}, {x: 2, y: sqrt(2)}]
>>> solve_poly_system([x*y], x, y)
[{x: 0}, {y: 0}]