Sympy

In [1]:
import sympy as sp

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

Live shell

Make symbols

Make one symbol:

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

Make several symbols at once:

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

Derivatives

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

Create an unevaluated derivative

In [5]:
sp.Derivative(sp.cos(x), x)
Out[5]:
$$\frac{d}{d x} \cos{\left (x \right )}$$

Evaluate an unevaluated derivative

In [6]:
diff = sp.Derivative(sp.cos(x), x)
diff
Out[6]:
$$\frac{d}{d x} \cos{\left (x \right )}$$
In [7]:
diff.doit()
Out[7]:
$$- \sin{\left (x \right )}$$

Directly compute an integral

In [8]:
sp.diff(sp.cos(x), x)
Out[8]:
$$- \sin{\left (x \right )}$$
In [9]:
expr = sp.exp(x*y*z)
diff = sp.Derivative(expr, x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())
Out[9]:
$$\frac{\partial^{7}}{\partial x\partial y^{2}\partial z^{4}} e^{x y z} = x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$$

First derivatives

In [10]:
diff = sp.Derivative(sp.cos(x), x)
sp.Eq(diff, diff.doit())
Out[10]:
$$\frac{d}{d x} \cos{\left (x \right )} = - \sin{\left (x \right )}$$
In [11]:
diff = sp.Derivative(3*sp.cos(x)**2, x)
sp.Eq(diff, diff.doit())
Out[11]:
$$\frac{d}{d x}\left(3 \cos^{2}{\left (x \right )}\right) = - 6 \sin{\left (x \right )} \cos{\left (x \right )}$$
In [12]:
diff = sp.Derivative(sp.exp(x**2), x)
sp.Eq(diff, diff.doit())
Out[12]:
$$\frac{d}{d x} e^{x^{2}} = 2 x e^{x^{2}}$$

Second derivatives

In [13]:
diff = sp.Derivative(x**4, x, 2)
sp.Eq(diff, diff.doit())
Out[13]:
$$\frac{d^{2}}{d x^{2}} x^{4} = 12 x^{2}$$

or

In [14]:
diff = sp.Derivative(x**4, x, x)
sp.Eq(diff, diff.doit())
Out[14]:
$$\frac{d^{2}}{d x^{2}} x^{4} = 12 x^{2}$$

Third derivatives

In [15]:
diff = sp.Derivative(x**4, x, 3)
sp.Eq(diff, diff.doit())
Out[15]:
$$\frac{d^{3}}{d x^{3}} x^{4} = 24 x$$

or

In [16]:
diff = sp.Derivative(x**4, x, x, x)
sp.Eq(diff, diff.doit())
Out[16]:
$$\frac{d^{3}}{d x^{3}} x^{4} = 24 x$$

Derivatives with respect to several variables at once

In [17]:
diff = sp.Derivative(sp.exp(x*y), x, y)
sp.Eq(diff, diff.doit())
Out[17]:
$$\frac{\partial^{2}}{\partial x\partial y} e^{x y} = \left(x y + 1\right) e^{x y}$$

Multiple derivatives with respect to several variables at once

In [18]:
diff = sp.Derivative(sp.exp(x*y*z), x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())
Out[18]:
$$\frac{\partial^{7}}{\partial x\partial y^{2}\partial z^{4}} e^{x y z} = x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$$

Integrals

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

Create an unevaluated derivative

In [20]:
sp.Integral(sp.cos(x), x)
Out[20]:
$$\int \cos{\left (x \right )}\, dx$$

Evaluate an unevaluated derivative

In [21]:
integ = sp.Integral(sp.cos(x), x)
integ
Out[21]:
$$\int \cos{\left (x \right )}\, dx$$
In [22]:
integ.doit()
Out[22]:
$$\sin{\left (x \right )}$$

Directly compute an integral

In [23]:
sp.integrate(sp.cos(x), x)
Out[23]:
$$\sin{\left (x \right )}$$
In [24]:
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())
Out[24]:
$$\int \cos{\left (x \right )}\, dx = \sin{\left (x \right )}$$

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

In [25]:
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())
Out[25]:
$$\int \cos{\left (x \right )}\, dx = \sin{\left (x \right )}$$

Create a definite integral

sp.oo means infinity.

In [26]:
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
Out[26]:
$$\int_{-\infty}^{\infty} \cos{\left (x \right )}\, dx = \langle -2, 2\rangle$$
In [27]:
integ = sp.Integral(sp.cos(x), (x, -sp.pi, sp.pi))
sp.Eq(integ, integ.doit())
Out[27]:
$$\int_{- \pi}^{\pi} \cos{\left (x \right )}\, dx = 0$$
In [28]:
integ = sp.Integral(sp.exp(-x), (x, 0, sp.oo))
sp.Eq(integ, integ.doit())
Out[28]:
$$\int_{0}^{\infty} e^{- x}\, dx = 1$$

Multiple integrals

In [29]:
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
Out[29]:
$$\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} \cos{\left (x \right )}\, dx\, dx = \langle -4, 4\rangle$$

Multiple variables integrals

In [30]:
integ = sp.Integral(sp.cos(x**2 + y**2), (x, -sp.oo, sp.oo), (y, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
Out[30]:
$$\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} \cos{\left (x^{2} + y^{2} \right )}\, dx\, dy = 0$$

Limits

Series expansion

Finite differences