from sympy import symbols, solve, Poly, And
# Define the variable
x = symbols('x')
# Define the inequality
inequality = 3*x - 5 < 4*x + 7
# Solve the inequality
solution = solve(inequality, x)
# Display the solution
print("The solution to the inequality 3x - 5 < 4x + 7 is:")
print(solution)SymPy is a computer algebra system for Python. It works with symbols and
exact numbers such as sqrt(2) and Rational(1, 3) instead of rounded
decimals, so it can solve equations, factor expressions and take
derivatives, integrals and limits the way you would on paper. Students use
it to check algebra and calculus homework, and engineers use it to derive
a formula before turning it into numeric code. This page is an online SymPy compiler: the code runs in your browser,
so you can try it without installing anything.
Run the example first, then paste any snippet below into a new cell to try it.
symbols('x') creates a symbol named x. Because x is a SymPy object,
3*x - 5 < 4*x + 7 does not evaluate to True or False. It builds an
inequality object that SymPy can rearrange. solve(inequality, x)
returns the values of x that satisfy it as a logical expression,
(-12 < x) & (x < oo). That is an And of two conditions, and oo is
SymPy's infinity, so the answer is every x greater than -12.
solve() treats an expression as equal to zero. Use Eq() when both
sides have terms. It returns a list for one equation and a dict for a
system. solveset() returns a set, which suits inequalities:
from sympy import symbols, solve, solveset, Eq, S
x, y = symbols("x y")
print(solve(x**2 - 5*x + 6, x)) # expression = 0
print(solve(Eq(2*x + 1, 7), x)) # left side = right side
print(solve([x + y - 10, x - y - 2], [x, y])) # a system of equations
print(solve(x**2 + 1, x)) # complex roots
print(solveset(x**2 - 4 < 0, x, domain=S.Reals)) # an inequality as an interval
I in the output is the imaginary unit, and Interval.open(-2, 2) means
-2 < x < 2.
expand() multiplies out products and powers, and factor() does the
reverse. simplify() looks for a shorter equivalent form. Rational
keeps fractions exact:
from sympy import symbols, expand, factor, simplify, sin, cos, Rational
x = symbols("x")
print(expand((x + 1) ** 3))
print(factor(x**3 - x**2 - 4*x + 4))
print(simplify(sin(x) ** 2 + cos(x) ** 2))
print(simplify((x**2 - 1) / (x - 1)))
print(Rational(1, 3) + Rational(1, 6))
integrate() with only a variable returns an antiderivative, without the
constant of integration. Pass a tuple (x, a, b) for a definite
integral:
from sympy import symbols, diff, integrate, limit, sin, exp, oo
x = symbols("x")
print(diff(x**3 * sin(x), x)) # derivative
print(integrate(x * exp(x), x)) # antiderivative, no + C
print(integrate(exp(-x), (x, 0, oo))) # definite integral
print(limit(sin(x) / x, x, 0))
subs() puts a value in place of a symbol, and the result stays exact.
evalf() turns an exact value into a decimal with as many digits as you
ask for. latex() returns LaTeX source. lambdify() turns an expression
into a plain Python function, which is much faster than subs() when you
need many values:
from sympy import symbols, sqrt, pi, latex, lambdify
x = symbols("x")
f = x**2 + 3*x - 1
print(f.subs(x, 2)) # exact: 9
print(sqrt(8)) # stays exact: 2*sqrt(2)
print(sqrt(8).evalf()) # decimal approximation
print(pi.evalf(30)) # 30 significant digits
print(latex(sqrt(x) / 2)) # LaTeX source for a report
g = lambdify(x, f) # a plain Python function
print(g(2.5))
**. In Python ^ means XOR, so x^2 raises a
TypeError.1/3 before SymPy sees it, so x + 1/3 becomes
x + 0.333333333333333. Write Rational(1, 3) or S(1)/3 to keep it
exact.== compares how expressions are written, not their value:
(x + 1)**2 == x**2 + 2*x + 1 is False. Build equations with Eq()
and test equality with simplify(a - b) == 0.