Quick examples
From SymPy
This page gives quick examples of common symbolic calculations in SymPy. Print it and keep it under your pillow!
Contents |
Elementary operations
Construct a symbolic expression
Construct the formula <math>\frac{3 \pi}{2} + \frac{e^{ix}}{x^2 + y}</math>:
>>> var('x y')
>>> Rational(3,2)*pi + exp(I*x) / (x**2 + y)
(3/2)*pi + 1/(y + x**2)*exp(I*x)
Evaluate a symbolic expression
Calculate the value of <math>e^{ix}</math> for <math>x=\pi</math>:
>>> x = Symbol('x')
>>> exp(I*x).subs(x,pi).evalf()
-1
Deconstruct an expression
>>> expr = x + 2*y >>> expr.__class__ <class 'sympy.core.add.Add'> >>> expr.args (x, 2*y)
Calculate a numerical value
Calculate 50 digits of <math>e^{\pi \sqrt{163}}</math>:
>>> exp(pi * sqrt(163)).evalf(50) 262537412640768743.99999999999925007259719818568888
Algebra
Expand products and powers
Expand <math>(x+y)^2 (x+1)</math>:
>>> ((x+y)**2 * (x+1)).expand() x**2 + x**3 + y**2 + x*y**2 + 2*x*y + 2*y*x**2
Simplify a formula
Simplify <math>\frac{1}{x} + \frac{x \sin x - 1}{x}</math>:
>>> a = 1/x + (x*sin(x) - 1)/x >>> simplify(a) sin(x)
Solve a polynomial equation
Find the roots of <math>x^3 + 2x^2 + 4x + 8</math>:
>>> solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x) [-2*I, 2*I, -2]
or more easily:
>>> solve(x**3 + 2*x**2 + 4*x + 8, x) [-2*I, 2*I, -2]
For details, see: Finding roots of polynomials.
Solve an equation system
Solve the equation system <math>\left(x+5y=2, -3x+6y=15\right)</math>:
>>> solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y])
{y: 1, x: -3}
or
>>> solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
{y: 1, x: -3}
Calculate a sum
Evaluate <math>\sum_{n=a}^b 6 n^2 + 2^n</math>:
>>> sum(6*n**2 + 2**n, (n, a, b)) b + 2**(1 + b) - a - 2**a - 2*a**3 + 2*b**3 + 3*a**2 + 3*b**2
Calculate a product
Evaluate <math>\prod_{n=1}^b n (n+1)</math>:
>>> product(n*(n+1), (n, 1, b)) RisingFactorial(2, b)*gamma(1 + b)
Calculus
Calculate a limit
Evaluate <math>\lim_{x\to 0} \frac{\sin x - x}{x^3}</math>:
>>> limit((sin(x)-x)/x**3, x, 0) -1/6
Calculate a Taylor series
Find the Maclaurin series of <math>\frac{1}{\cos x}</math> up to the <math>O(x^6)</math> term:
>>> (1/cos(x)).series(x, 0, 6) 1 + (1/2)*x**2 + (5/24)*x**4 + O(x**6)
Calculate a derivative
Differentiate <math>\frac{\cos(x^2)^2}{1+x}</math>:
>>> diff(cos(x**2)**2 / (1+x), x) -(1 + x)**(-2)*cos(x**2)**2 - 4*x/(1 + x)*cos(x**2)*sin(x**2)
Calculate an integral
Calculate the indefinite integral <math>\int x^2 \cos x \, dx</math>
>>> integrate(x**2 * cos(x), x) -2*sin(x) + x**2*sin(x) + 2*x*cos(x)
Calculate the definite integral <math>\int_0^{\pi/2} x^2 \cos x \, dx</math>:
>>> integrate(x**2 * cos(x), (x, 0, pi/2)) (-2) + (1/4)*pi**2
Solve an ordinary differential equation
Solve <math>f(x) + 9 f(x) + 1 = 1\,\!</math>:
>>> f = Function('f')
>>> dsolve(Eq(Derivative(f(x),x,x) + 9*f(x) + 1, 1), f(x))
C1*sin(3*x) + C2*cos(3*x)
You can also use .diff(), like here (an example in isympy)
In [1]: f = Function("f")
In [2]: Eq(f(x).diff(x, x) + 9*f(x) + 1, 1)
Out[2]:
2
d
1 + 9*f(x) + ─────(f(x)) = 1
dx dx
In [3]: dsolve(_, f(x))
Out[3]: Câ‚*sin(3*x) + Câ‚‚*cos(3*x)
