Inequality Solvers

For general cases reduce_inequalities() should be used. Other functions are the subcategories useful for special dedicated operations, and will be called internally as needed by reduce_inequalities.

Note

For a beginner-friendly guide focused on solving inequalities, refer to Reduce One or a System of Inequalities for a Single Variable Algebraically.

Note

Some of the examples below use poly(), which simply transforms an expression into a polynomial; it does not change the mathematical meaning of the expression.

sympy.solvers.inequalities.solve_rational_inequalities(eqs)[source]

Solve a system of rational inequalities with rational coefficients. eqs is a list of lists where the contents of each inner list are joined with And and the resulting conjunctions are joined with Or. The contents of the inner lists are given as ((n, d), op), with n and d being Poly instances, e.g. ((Poly(x - 1, x), Poly(1, x)), ">") for x - 1 > 0.

Examples

>>> from sympy.abc import x
>>> from sympy import solve_rational_inequalities, Poly
>>> solve_rational_inequalities([[
... ((Poly(-x + 1), Poly(1, x)), '>='),
... ((Poly(-x + 1), Poly(1, x)), '<=')]])
{1}
>>> solve_rational_inequalities([[
... ((Poly(x), Poly(1, x)), '!='),
... ((Poly(-x + 1), Poly(1, x)), '>=')]])
Union(Interval.open(-oo, 0), Interval.Lopen(0, 1))
sympy.solvers.inequalities.solve_poly_inequality(poly, rel)[source]

Solve a polynomial inequality with rational coefficients.

Examples

>>> from sympy import solve_poly_inequality, Poly
>>> from sympy.abc import x
>>> solve_poly_inequality(Poly(x, x, domain='ZZ'), '==')
[{0}]
>>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '!=')
[Interval.open(-oo, -1), Interval.open(-1, 1), Interval.open(1, oo)]
>>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '==')
[{-1}, {1}]
sympy.solvers.inequalities.solve_poly_inequalities(polys)[source]

Solve polynomial inequalities with rational coefficients, returning the union of the intervals on which each inequality is true. An element like (Poly(x + 1, x), "<") represents x + 1 < 0.

Examples

>>> from sympy import Poly
>>> from sympy.solvers.inequalities import solve_poly_inequalities
>>> from sympy.abc import x
>>> solve_poly_inequalities(((
... Poly(x**2 - 3), ">"), (
... Poly(-x**2 + 1), ">")))
Union(Interval.open(-oo, -sqrt(3)), Interval.open(-1, 1), Interval.open(sqrt(3), oo))
sympy.solvers.inequalities.reduce_rational_inequalities(
exprs,
gen,
relational=True,
)[source]

Reduce a system of rational inequalities with rational coefficients. exprs is a list of lists where the contents of each inner list are joined with And and the resulting conjunctions are joined with Or.

Examples

>>> from sympy import Symbol
>>> from sympy.solvers.inequalities import reduce_rational_inequalities
>>> x = Symbol('x', real=True)
>>> reduce_rational_inequalities([[x**2 <= 0]], x)
Eq(x, 0)
>>> reduce_rational_inequalities([[x + 2 > 0]], x)
-2 < x
>>> reduce_rational_inequalities([[(x + 2, ">")]], x)
-2 < x
>>> reduce_rational_inequalities([[x + 2]], x)
Eq(x, -2)

The contents of each inner list are joined with And. For example, And(x > 0, x > 1) is passed as follows:

>>> reduce_rational_inequalities([[x > 0, x > 1]], x)
1 < x

The resulting conjunctions are joined with Or. Thus, Or(And(x > 1, x < 3), x > 0) is passed as follows:

>>> reduce_rational_inequalities([[x > 1, x < 3], [x > 0]], x)
0 < x

The result may include finiteness conditions even if the symbol is identified as being an extended real:

>>> y = Symbol('y', extended_real=True)
>>> reduce_rational_inequalities([[y + 2 > 0]], y)
(-2 < y) & (y < oo)
sympy.solvers.inequalities.reduce_abs_inequality(expr, rel, gen)[source]

Reduce an inequality with nested absolute values.

Examples

>>> from sympy import reduce_abs_inequality, Abs, Symbol
>>> x = Symbol('x', real=True)
>>> reduce_abs_inequality(Abs(x - 5) - 3, '<', x)
(2 < x) & (x < 8)
>>> reduce_abs_inequality(Abs(x + 2)*3 - 13, '<', x)
(-19/3 < x) & (x < 7/3)
sympy.solvers.inequalities.reduce_abs_inequalities(exprs, gen)[source]

Reduce a system of inequalities with nested absolute values, returning an expression describing where all the inequalities are true. An element like (Abs(x) - 1, "<") represents Abs(x) - 1 < 0.

Examples

>>> from sympy import reduce_abs_inequalities, Abs, Symbol
>>> x = Symbol('x', extended_real=True)
>>> reduce_abs_inequalities([(Abs(3*x - 5) - 7, '<'),
... (Abs(x + 25) - 13, '>')], x)
(-2/3 < x) & (x < 4) & (((-oo < x) & (x < -38)) | ((-12 < x) & (x < oo)))
>>> reduce_abs_inequalities([(Abs(x - 4) + Abs(3*x - 5) - 7, '<')], x)
(1/2 < x) & (x < 4)
sympy.solvers.inequalities.reduce_inequalities(inequalities, symbols=[])[source]

Reduce a system of inequalities with rational coefficients.

Examples

>>> from sympy.abc import x, y
>>> from sympy import reduce_inequalities
>>> reduce_inequalities(0 <= x + 3, [])
(-3 <= x) & (x < oo)
>>> reduce_inequalities(0 <= x + y*2 - 1, [x])
(x < oo) & (x >= 1 - 2*y)
sympy.solvers.inequalities.solve_univariate_inequality(
expr,
gen,
relational=True,
domain=Reals,
continuous=False,
)[source]

Solves a real univariate inequality.

Parameters:

expr : Relational

The target inequality

gen : Symbol

The variable for which the inequality is solved

relational : bool

A Relational type output is expected or not

domain : Set

The domain over which the equation is solved

continuous: bool

True if expr is known to be continuous over the given domain (and so continuous_domain() does not need to be called on it)

Raises:

NotImplementedError

The solution of the inequality cannot be determined due to limitation in sympy.solvers.solveset.solvify().

Notes

The input is treated as a relation whose truth set is being determined, not merely as an equation container. Transformations that form lhs - rhs internally must still preserve the semantics of the original relation.

Currently, we cannot solve all the inequalities due to limitations in sympy.solvers.solveset.solvify(). Also, the solution returned for trigonometric inequalities are restricted in its periodic interval.

Examples

>>> from sympy import solve_univariate_inequality, Symbol, sin, Interval, S
>>> x = Symbol('x')
>>> solve_univariate_inequality(x**2 >= 4, x)
((2 <= x) & (x < oo)) | ((-oo < x) & (x <= -2))
>>> solve_univariate_inequality(x**2 >= 4, x, relational=False)
Union(Interval(-oo, -2), Interval(2, oo))
>>> domain = Interval(0, S.Infinity)
>>> solve_univariate_inequality(x**2 >= 4, x, False, domain)
Interval(2, oo)
>>> solve_univariate_inequality(sin(x) > 0, x, relational=False)
Interval.open(0, pi)

See also

sympy.solvers.solveset.solvify

solver returning solveset solutions with solve’s output API