Create a Python virtual environment.
andrei@css-tech-x:~/math-fun$ python3 -m venv venv
Enter the virtual environment.
andrei@css-tech-x:~/math-fun$ . venv/bin/activate
And simply install the package with pip.
(venv) andrei@css-tech-x:~/math-fun$ pip install sympy Collecting sympy Collecting mpmath>=0.19 (from sympy) Installing collected packages: mpmath, sympy Successfully installed mpmath-1.0.0 sympy-1.1.1
Run Python inside the virtual environment.
(venv) andrei@css-tech-x:~/math-fun$ python3 Python 3.6.3 (default, Oct 3 2017, 21:45:48) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.
With SymPy, you can manipulate abstract symbols.
>>> from sympy.abc import x >>> x ** 2 x**2
And you can already start doing Math! Here's a derivative.
>>> (x ** 2).diff() 2*x
You can also use SymPy in a Jupyter notebook with beautifully rendered equations.
from sympy import init_printing
init_printing()
from sympy import Derivative
from sympy.abc import x
Derivative(x ** 2)
Fully embedded inside a real programing language.
from sympy.abc import a, b
sq = (a + b) ** 2
To reference later.
sq
And manipulate.
sq.expand()
from sympy import Integral, cot
from sympy.abc import x
int = Integral(cot(x))
int
int.doit()
from sympy import exp, cos, Derivative
from sympy.abc import x
complicated = Derivative(exp(-x * cos(x)) * (2 * x - 1) / (cos(x * 2)))
complicated
complicated.doit()
from sympy import exp, pi, I
from sympy.abc import theta
circular = exp(I * theta)
circular
circular.subs(theta, pi / 2)
circular.subs(theta, pi)
from sympy import Matrix
a_matrix = Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
a_matrix
a_matrix.det()
How I use it.
tick_tock = Matrix([
[0, 1],
[1, 0]
])
tick_tock
t0 = Matrix([1, 0])
t0
t1 = tick_tock * t0
t1
Not just with numbers!
from sympy.abc import a, b
tick_tock * Matrix([a, b])
$$ I(X;Y) = \sum_{x,y} p(x,y) \log \frac{p(x,y)}{p(x)p(y)}\\ $$
def mi(pxy):
''' Takes a joint probability P(X, Y) and returns I(X; Y). '''
px, py = marginalize(pxy)
return sum(pxy[x, y] * log2(pxy[x, y] / (px[x] * py[y]))
if pxy[x, y] else 0
for x in range(len(px)) for y in range(len(py)))
import process
cycle_matrix = process.mixed_cycles()
cycle_matrix
particular_matrix = cycle_matrix.subs(process.mixed_cycles.SIGMA, 0.7)
particular_matrix
import util
util.draw_graph(particular_matrix)
import it
i = it.mi(it.to_joint(process.mixed_cycles(), process.uniform(4)))
i
# ...
plot_over_sigma(i)
from sympy import diff, simplify
sigma = process.mixed_cycles.SIGMA
di = simplify(diff(i, sigma))
di
plot_over_sigma(di)
from sympy import Limit
l0 = Limit(di, sigma, 0)
l0
l0.doit()
l1 = Limit(di, sigma, 1)
l1
l1.doit()