SymPy

SymPy is a python library for symbolic computing. Perhaps the most well known software that embraces symbolic computing is Maple). SymPy although significantly less powerul enables numerous symbolic areas of mathematics: algebra, arithmetic, calculus, discrete mathematics and quantum physics. It is capable of formatting the result of the computations as LaTeX code.

SymPy is free software and is licensed under New BSD License. The lead developers are Ondřej Čertík and Aaron Meurer. The following code will import everything from the package and enable latex rendering of output.


In [14]:
from sympy import *
from sympy import init_printing
init_printing()

Symbolic Algebra

In the same way we do algebra on paper we can use a computer. Symbolic computation or algebraic computation, is a scientific area that refers to the study and development of algorithms and software for manipulating mathematical expressions and other mathematical objects.

For example


In [15]:
sqrt(8)


Out[15]:
$$2 \sqrt{2}$$

We need to define our variables symbolically before we use them


In [18]:
x,y,z = symbols('x y z')
x


Out[18]:
$$x$$

once the variables are available we can define expressions and perform actions on them


In [24]:
expr = x + 2*y
print(expr)
expr = expr + 9*x
print(expr)


x + 2*y
10*x + 2*y

you may want to expand the algebric representation


In [27]:
expr2 = x*expr
print(expr2)
print(expand(expr2))


x*(10*x + 2*y)
10*x**2 + 2*x*y

Recall that factoring is the process of finding the factors--that is finding the terms to multiply together to get an expression. If you need a refresher check out the Khan academy materials on factoring. Sympy can factorize


In [40]:
expr = x + x**2
factor(expr)


Out[40]:
$$x \left(x + 1\right)$$

Symbolic calculus - derivatives

Definition of a derivative

From wiki

Derivatives are a fundamental tool of calculus. For example, the derivative of the position of a moving object with respect to time is the object's velocity: this measures how quickly the position of the object changes when time advances.

The derivative of f with respect to x is given by

$ f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$

when this limit exits.

Basic derivative properties

Assuming c and n to be real constants, then these theorems hold true:

and if you want proofs.

1. The derivative of a constant is 0

$ \frac{d}{dx}c = 0 $


In [65]:
c = 1
diff(c)


Out[65]:
$$0$$

2. The derivative of a variable is 1

$ \frac{d}{dx}x = 1 $


In [74]:
diff(x)


Out[74]:
$$1$$

3. The derivative of a constant times a function is the same as the deriviative of that function times the constant

$ \frac{d}{dx}(c \cdot f(x)) = c \cdot \frac{d}{dx}f(x) = c \cdot f'(x) $


In [77]:
diff(1/x * c)


Out[77]:
$$- \frac{1}{x^{2}}$$

In [84]:
c * diff(1/x)


-1/x**2

4. The derivative of a function plus or minus another function is the same as if we took the derivative separately

$ \frac{d}{dx}(f(x) + g(x)) = f'(x) + g'(x) $ (true for minus as well)


In [85]:
diff(1/x + ln(x))


Out[85]:
$$\frac{1}{x} - \frac{1}{x^{2}}$$

In [ ]:

5. The derivative

$ \frac{d}{dx}x^n = n \cdot x^{n-1} $ the power rule


In [80]:
expr = Derivative(1/x+y,x)
expr


Out[80]:
$$\frac{\partial}{\partial x}\left(y + \frac{1}{x}\right)$$

In [ ]: