Python, SymPy, and $\LaTeX$


In [ ]:
%matplotlib inline

import sympy as sp
import numpy as np
import matplotlib.pyplot as plt

Symbolic Mathematics (SymPy)


In [ ]:
sp.init_printing()     # Turns on pretty printing

In [ ]:
np.sqrt(8)

In [ ]:
sp.sqrt(8)

You have to explicitly tell SymPy what symbols you want to use


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

In [ ]:
my_equation = 2 * x + y
my_equation

In [ ]:
my_equation + 3

In [ ]:
my_equation - x

In [ ]:
my_equation / x

SymPy has all sorts of ways to manipulates symbolic equations


In [ ]:
sp.simplify(my_equation / x)

In [ ]:
another_equation = (x + 2) * (x - 3)
another_equation

In [ ]:
sp.expand(another_equation)

In [ ]:
yet_another_equation = 2 * x**2 + 5 * x + 3

sp.factor(yet_another_equation)

In [ ]:
sp.solve(yet_another_equation,x)

In [ ]:
long_equation = 2*y*x**3 + 12*x**2 - x + 3 - 8*x**2 + 4*x + x**3 + 5 + 2*y*x**2 + x*y
long_equation

In [ ]:
sp.collect(long_equation,x)

In [ ]:
sp.collect(long_equation,y)

SymPy can do your calculus homework.


In [ ]:
yet_another_equation

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

In [ ]:
sp.diff(yet_another_equation,x,2)

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

In [ ]:
sp.integrate(yet_another_equation,(x,0,5))   # limits x = 0 to 5

System of 3 equations example

$$ \begin{array}{c} x + 3y + 5z = 10 \\ 2x + 5y + z = 8 \\ 2x + 3y + 8z = 3 \\ \end{array} \hspace{3cm} \left[ \begin{array}{ccc} 1 & 3 & 5 \\ 2 & 5 & 1 \\ 2 & 3 & 8 \end{array} \right] \left[ \begin{array}{c} x\\ y\\ z \end{array} \right] = \left[ \begin{array}{c} 10\\ 8\\ 3 \end{array} \right] $$

In [ ]:
AA = sp.Matrix([[1,3,5],[2,5,1],[2,3,8]])
bb = sp.Matrix([[10],[8],[3]])

print(AA**-1)

print(AA**-1 * bb)

SymPy can do so much more. It really is magic. Complete documentation can be found here


Python uses the $\LaTeX$ language to typeset equations.

Most LaTeX commands are prefixed with a "\". For example \pi is the command to produce the lower case Greek letter pi. The characters # $ % & ~ _ ^ \ { } are special characters in LaTeX. If you want to typeset them you need to put a \ in front of them. For example \$ will typeset the symbol $ % - comment. Everything is ignored after a % $ - Math mode. Start and Stop math mode. $\pi$ ^ - Superscript in Math mode. $2^2$ _ - Subscript in Math mode. $2_2$

Use a single set of $ to make your $\LaTeX$ inline and a double set $$ to center

$$ \int \cos(x)\ dx = \sin(x) $$

This code will produce the output:

$$ \int \cos(x)\ dx = \sin(x) $$

Use can use $\LaTeX$ in plots:


In [ ]:
plt.style.use('ggplot')

x = np.linspace(0,2*np.pi,100)
y = np.sin(5*x) * np.exp(-x)

plt.plot(x,y)
plt.title("The function $y\ =\ \sin(5x)\ e^{-x}$")
plt.xlabel("This is in units of 2$\pi$")

plt.text(2.0, 0.4, '$\Delta t = \gamma\, \Delta t$', color='green', fontsize=36)

Use can use SymPy to make $\LaTeX$ equations for you!


In [ ]:
a =  1/( ( z + 2 ) * ( z + 1 ) )

print(sp.latex(a))
$$ \frac{1}{\left(z + 1\right) \left(z + 2\right)} $$

In [ ]:
print(sp.latex(sp.Integral(z**2,z)))
$$ \int z^{2}\, dz $$

Astropy can output $\LaTeX$ tables


In [ ]:
from astropy.io import ascii
from astropy.table import QTable

In [ ]:
my_table = QTable.read('Zodiac.csv', format='ascii.csv')

In [ ]:
my_table[0:3]

In [ ]:
ascii.write(my_table, format='latex')

Some websites to open up for class:


Assignment for Week 9

----------------------------------------------------------------------------- LaTeX homework - Create a LaTeX document with references. ----------------------------------------------------------------------------- Start with the file: Example.tex Minimum required elements: * Between 2 and 4 pages in length (pages > 4 will be ignored). * At least two paragraphs of text (the text should be coherent). * At least 5 references from ADS * http://adsabs.harvard.edu/abstract_service.html * Make sure to \cite{} the references in your paper * The equation on the back of the handout * One (or more) equation(s) of your choice. * One Compulsory plot generated with python (png format) * Start with t = np.linspace(0,12*np.pi,2000) * generate a butterfly plot: http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental) * I have started it for you im the last cell of this notebook * One other plot/image (do not reuse and old one!) * One table of at least 4 columns and 4 rows. * Bonus points given for interesting content! ----------------------------------------------------------------------------- Create a PDF file: Save the file as FirstLast.pdf (i.e. TobySmith.pdf) Upload the PDF to the class canvas site ----------------------------------------------------------------------------- Deadline: Tuesday Mar 07 - 5pm (-5 pts for each 30 minutes late) -----------------------------------------------------------------------------

In [ ]:
t = np.linspace(0,12*np.pi,2000)

fig,ax = plt.subplots(1,1)                    # One window
fig.set_size_inches(11,8.5)                   # (width,height) - letter paper landscape

fig.tight_layout()                            # Make better use of space on plot