Sympy


In [ ]:
import sympy as sp

# See: http://docs.sympy.org/latest/tutorial/printing.html
sp.init_printing()

Live shell

Make symbols

Make one symbol:


In [ ]:
x = sp.symbols("x")

Make several symbols at once:


In [ ]:
x, y, z = sp.symbols("x y z")

Substitute


In [ ]:
x = sp.symbols("x")

In [ ]:
expr = sp.cos(x) + 1
expr.subs(x, 0)

Simplify


In [ ]:
x = sp.symbols("x")

In [ ]:
sp.simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))

In [ ]:
sp.simplify(sp.exp(sp.I * sp.pi) + sp.exp(sp.I * sp.pi))

Factor


In [ ]:
x = sp.symbols("x")

In [ ]:
sp.factor(x**3 - x**2 + x - 1)

Expand


In [ ]:
x = sp.symbols("x")

In [ ]:
sp.expand((x - 1)*(x**2 + 1))

Solve


In [ ]:
x = sp.symbols("x")

In [ ]:
eq = sp.Eq(x**2, 1)

In [ ]:
sp.solveset(eq, x)

Derivatives


In [ ]:
x, y, z = sp.symbols("x y z")

Create an unevaluated derivative


In [ ]:
sp.Derivative(sp.cos(x), x)

Evaluate an unevaluated derivative


In [ ]:
diff = sp.Derivative(sp.cos(x), x)
diff

In [ ]:
diff.doit()

Directly compute an integral


In [ ]:
sp.diff(sp.cos(x), x)

In [ ]:
expr = sp.exp(x*y*z)
diff = sp.Derivative(expr, x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())

First derivatives


In [ ]:
diff = sp.Derivative(sp.cos(x), x)
sp.Eq(diff, diff.doit())

In [ ]:
diff = sp.Derivative(3*sp.cos(x)**2, x)
sp.Eq(diff, diff.doit())

In [ ]:
diff = sp.Derivative(sp.exp(x**2), x)
sp.Eq(diff, diff.doit())

Second derivatives


In [ ]:
diff = sp.Derivative(x**4, x, 2)
sp.Eq(diff, diff.doit())

or


In [ ]:
diff = sp.Derivative(x**4, x, x)
sp.Eq(diff, diff.doit())

Third derivatives


In [ ]:
diff = sp.Derivative(x**4, x, 3)
sp.Eq(diff, diff.doit())

or


In [ ]:
diff = sp.Derivative(x**4, x, x, x)
sp.Eq(diff, diff.doit())

Derivatives with respect to several variables at once


In [ ]:
diff = sp.Derivative(sp.exp(x*y), x, y)
sp.Eq(diff, diff.doit())

Multiple derivatives with respect to several variables at once


In [ ]:
diff = sp.Derivative(sp.exp(x*y*z), x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())

Integrals


In [ ]:
x, y, z = sp.symbols("x y z")

Create an unevaluated derivative


In [ ]:
sp.Integral(sp.cos(x), x)

Evaluate an unevaluated derivative


In [ ]:
integ = sp.Integral(sp.cos(x), x)
integ

In [ ]:
integ.doit()

Directly compute an integral


In [ ]:
sp.integrate(sp.cos(x), x)

In [ ]:
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())

Create an indefinite integral (i.e. an antiderivative or primitive)


In [ ]:
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())

Create a definite integral

sp.oo means infinity.


In [ ]:
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())

In [ ]:
integ = sp.Integral(sp.cos(x), (x, -sp.pi, sp.pi))
sp.Eq(integ, integ.doit())

In [ ]:
integ = sp.Integral(sp.exp(-x), (x, 0, sp.oo))
sp.Eq(integ, integ.doit())

Multiple integrals


In [ ]:
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())

Multiple variables integrals


In [ ]:
integ = sp.Integral(sp.cos(x**2 + y**2), (x, -sp.oo, sp.oo), (y, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())

Limits

Series expansion

Finite differences