import sympy
# from sympy import *
sympy.init_printing()
# nicely formatted
from sympy import I, pi, oo
# different from np.pi
x = sympy.Symbol("x")
y = sympy.Symbol("y", real=True)
y.is_real
True
x.is_real is None
True
Assumption Keyword Arguments | Attributes | Description |
---|---|---|
real, imaginary | is_real, is_imaginary | Specify that a symbol represents a real or imaginary number. |
positive, negative | is_positive, is_negative | Specify that a symbol is positive or negative. |
integer | is_integer | The symbol represents an integer. |
odd, even | is_odd, is_even | The symbol represents an odd or even integer. |
prime | is_prime | The symbol is a prime number and therefore also an integer. |
finite, infinite | is_finite, is_infinite | The symbol represents a quantity that is finite or infinite. |
x = sympy.Symbol("x")
y = sympy.Symbol("y", positive=True)
sympy.sqrt(x ** 2)
sympy.sqrt(y ** 2)
# Simplify the expression
n1 = sympy.Symbol("n")
n2 = sympy.Symbol("n", integer=True)
n3 = sympy.Symbol("n", odd=True)
sympy.cos(n1 * pi)
sympy.cos(n2 * pi)
sympy.cos(n3 * pi)
a, b, c = sympy.symbols("a, b, c", negative=True)
d, e, f = sympy.symbols("d, e, f", positive=True)
we cannot directly use the built-in Python objects for integers, int, and floating-point numbers, float, and so on.
i = sympy.Integer(19)
type(i)
sympy.core.numbers.Integer
i.is_Integer, i.is_real, i.is_odd
(True, True, True)
f = sympy.Float(2.3)
type(f)
sympy.core.numbers.Float
i, f = sympy.sympify(19), sympy.sympify(2.3)
# integer and Integer are different
n = sympy.Symbol("n", integer=True)
n.is_integer, n.is_Integer, n.is_positive, n.is_Symbol
(True, False, None, True)
i = sympy.Integer(19)
i.is_integer, i.is_Integer, i.is_positive, i.is_Symbol
(True, True, True, False)
sympy.Float(0.3, 25)
sympy.Float('0.3', 25)
sympy.Rational(11, 13)
r1 = sympy.Rational(2, 3)
r2 = sympy.Rational(4, 5)
r1 * r2
Mathematical Symbol | SymPy Symbol | Description |
---|---|---|
π | sympy.pi | Ratio of the circumference to the diameter of a circle. |
e | sympy.E | The base of the natural logarithm, e = exp (1). |
γ | sympy.EulerGamma | Euler’s constant. |
i | sympy.I | The imaginary unit. |
∞ | sympy.oo | Infinity. |
x, y, z = sympy.symbols("x, y, z")
f = sympy.Function("f")
f(x)
g = sympy.Function("g")(x, y, z)
g
g.free_symbols
Naturally, SymPy has built-in functions for many standard mathematical functions that are available in the global SymPy namespace
sympy.sin(x)
h = sympy.Lambda(x, x**2)
h
h(5)
h(1 + x)
x = sympy.Symbol("x")
expr = 1 + 2 * x**2 + 3 * x**3
expr
For an operator, the args attribute is a tuple of subexpressions.
expr.args
expr.args[1]
expr.args[1].args[1]
expr.args[1].args[1].args[0]
expr = 2 * (x**2 - x) - x * (x + 1)
expr
sympy.simplify(expr)
expr = sympy.exp(x) * sympy.exp(y)
expr
sympy.simplify(expr)
Function | Description |
---|---|
sympy.simplify | Attempt various methods and approaches to obtain a simpler form of a given expression. |
sympy.trigsimp | Attempt to simplify an expression using trigonometric identities. |
sympy.powsimp | Attempt to simplify an expression using laws of powers. |
sympy.compsimp | Simplify combinatorial expressions. |
sympy.ratsimp | Simplify an expression by writing on a common denominator. |
expr = (x + 1) * (x + 2)
sympy.expand(expr)
sympy.sin(x + y).expand(trig=True)
a, b = sympy.symbols("a, b", positive=True)
sympy.log(a * b).expand(log=True)
sympy.expand((a * b)**x, power_base=True)
sympy.factor(x**2 - 1)
# the opposite to sympy.expand with mul=True.
sympy.factor(x * sympy.cos(y) + sympy.sin(z) * x)
expr = x + y + x * y * z
expr.collect(x)
expr.collect(y)
sympy.apart(1/(x**2 + 3*x + 2), x)
sympy.together(1 / (y * x + y) + 1 / (1+x))
sympy.cancel(y / (y * x + y))
(x + y).subs(x, y)
sympy.sin(x * sympy.exp(x)).subs(x, y)
sympy.sin(x * z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos})
expr = x * y + z**2 *x
values = {x: 1.25, y: 0.4, z: 3.2}
expr.subs(values)
sympy.N(1 + pi)
(x + 1/pi).evalf(10)
expr = sympy.sin(pi * x * sympy.exp(x))
expr_func = sympy.lambdify(x, expr)
expr_func(1.0)
expr_func = sympy.lambdify(x, expr, 'numpy')
import numpy as np
xvalues = np.arange(0, 10)
expr_func(xvalues)
array([ 0. , 0.77394269, 0.64198244, 0.72163867, 0.94361635, 0.20523391, 0.97398794, 0.97734066, -0.87034418, -0.69512687])
f = sympy.Function('f')(x)
sympy.diff(f, x)
# equivalent to f.diff(x)
sympy.diff(f, x, x)
sympy.diff(f, x, 3)
g = sympy.Function('g')(x, y)
g.diff(x, y)
g.diff(x, 3, y, 2)
expr = x**4 + x**3 + x**2 + x + 1
expr.diff(x)
expr.diff(x, x)
expr = (x + 1)**3 * y ** 2 * (z - 1)
expr.diff(x, y, z)
d = sympy.Derivative(sympy.exp(sympy.cos(x)), x)
d
d.doit()
a, b, x, y = sympy.symbols("a, b, x, y")
f = sympy.Function("f")(x)
sympy.integrate(f)
sympy.integrate(f, (x, a, b))
sympy.integrate(sympy.sin(x))
sympy.integrate(sympy.sin(x), (x, a, b))
sympy.integrate(sympy.exp(-x**2), (x, 0, oo))
a, b, c = sympy.symbols("a, b, c", positive=True)
sympy.integrate(a * sympy.exp(-((x-b)/c)**2), (x, -oo, oo))
sympy.integrate(sympy.sin(x * sympy.cos(x)))
expr = sympy.sin(x*sympy.exp(y))
sympy.integrate(expr, x)
expr = (x + y)**2
sympy.integrate(expr, x)
sympy.integrate(expr, x, y)
sympy.integrate(expr, (x, 0, 1), (y, 0, 1))
# We omit Series and Limits!
n = sympy.symbols("n", integer=True)
x = sympy.Sum(1/(n**2), (n, 1, oo))
x
x.doit()
x = sympy.Product(n, (n, 1, 7))
x
x.doit()
x = sympy.Symbol("x")
sympy.solve(x**2 + 2*x - 3)
a, b, c = sympy.symbols("a, b, c")
sympy.solve(a * x**2 + b * x + c, x)
sympy.solve(sympy.sin(x) - sympy.cos(x), x)
sympy.solve(sympy.exp(x) + 2 * x, x)
# LambertW function
It is not uncommon to encounter equations that are not solvable algebraically or which SymPy is unable to solve.
sympy.solve(x**5 - x**2 + 1, x)
eq1 = x + 2 * y - 1
eq2 = x - y + 1
sympy.solve([eq1, eq2], [x, y], dict=True)
eq1 = x**2 - y
eq2 = y**2 - x
sols = sympy.solve([eq1, eq2], [x, y], dict=True)
sols
sympy.Matrix([1, 2])
sympy.Matrix([[1, 2]])
sympy.Matrix([[1, 2], [3, 4]])
sympy.Matrix(3, 4, lambda m, n: 10 * m + n)
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M
M * M
x = sympy.Matrix(sympy.symbols("x_1, x_2"))
M * x
p, q = sympy.symbols("p, q")
M = sympy.Matrix([[1, p], [q, 1]])
M
b = sympy.Matrix(sympy.symbols("b_1, b_2"))
b
# computing the inverse of a matrix is more difficult
x = M.LUsolve(b)
x
x = sympy.Symbol("x")
# Greek alphabet
print(sympy.latex(sympy.Integral(sympy.sqrt(1/x), x)))
# use print() !!!
\int \sqrt{\frac{1}{x}}\, dx
print(sympy.latex(sympy.Integral(sympy.sqrt(1/x), x).doit()))
2 x \sqrt{\frac{1}{x}}
alpha, beta, gamma= sympy.symbols("alpha beta gamma")
alpha, beta, gamma
print(sympy.latex(alpha+beta+gamma))
\alpha + \beta + \gamma