In [ ]:
%matplotlib inline
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
In [ ]:
sp.init_printing() # Turns on pretty printing
In [ ]:
np.sqrt(8)
In [ ]:
sp.sqrt(8)
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
In [ ]:
sp.simplify(my_equation / x)
In [ ]:
another_equation = (x + 2) * (x - 3)
another_equation
In [ ]:
sp.expand(another_equation)
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)
In [ ]:
yet_another_equation = 2 * x**2 - 5 * x + 30
yet_another_equation
In [ ]:
sp.solve(yet_another_equation,x)
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
In [ ]:
still_another_equation = sp.sin(x) * sp.exp(-x)
still_another_equation
In [ ]:
sp.series(still_another_equation, x)
In [ ]:
sp.series(still_another_equation, x, x0 = 0, n = 8)
In [ ]:
sp.series(still_another_equation, x, x0 = sp.pi, n = 8)
In [ ]:
limit_equation = (1 + (1 / x)) ** x
limit_equation
In [ ]:
sp.limit(limit_equation, x, sp.oo) # sp.oo = infinity
In [ ]:
AA = sp.Matrix([[1,3,5],[2,5,1],[2,3,8]])
bb = sp.Matrix([[10],[8],[3]])
In [ ]:
AA, bb
In [ ]:
AA**-1
In [ ]:
AA**-1 * AA
In [ ]:
AA**-1 * bb
In [ ]:
my_x = np.linspace(0,2*np.pi,100)
In [ ]:
my_y1 = 10 * np.sin(5*my_x) * np.exp(-my_x)
my_y2 = 6 - np.exp(0.75 * my_x)
In [ ]:
fig,ax = plt.subplots(1,1)
fig.set_size_inches(10,4)
fig.tight_layout()
ax.set_ylim(-5,8)
ax.set_xlim(0,3)
ax.set_xlabel("This is X")
ax.set_ylabel("This is Y")
ax.plot(my_x, my_y1, color='b', marker='None', linestyle='--')
ax.plot(my_x, my_y2, color='r', marker='None', linestyle='-');
In [ ]:
equation_one = 10 * sp.sin(5*x) * sp.exp(-x)
equation_two = 6 - sp.exp(0.75 * x)
In [ ]:
equation_one, equation_two
In [ ]:
my_guess = 0.5
sp.nsolve(equation_one - equation_two, x, my_guess)
In [ ]:
all_guesses = (0.1, 0.5, 2.5)
for i in all_guesses:
result = sp.nsolve(equation_one - equation_two, x, i)
print(result)
In [ ]:
my_guess = 200
sp.nsolve(equation_one - equation_two, x, my_guess)
In [ ]:
f = sp.Function('f')
In [ ]:
equation_ode = sp.Derivative(f(x), x, x) + 9*f(x)
equation_ode
In [ ]:
sp.dsolve(equation_ode, f(x))
SymPy
can do so much more. It really is magic.
In [ ]:
plt.style.use('ggplot')
my_a = np.linspace(0,2*np.pi,100)
my_b = np.sin(5*my_a) * np.exp(-my_a)
In [ ]:
fig,ax = plt.subplots(1,1)
fig.set_size_inches(10,4)
fig.tight_layout()
ax.plot(my_a, my_b, color='r', marker='None', linestyle='-');
ax.set_title("A gratuitous use of $σύμβολον$")
ax.set_xlabel("This is in units of 2$\pi$")
ax.set_ylabel("This is B")
ax.text(2.0, 0.4, '$y\ =\ \sin(5a)\ e^{-a}$', color='green', fontsize=36);
In [ ]:
a = 1/( ( z + 2 ) * ( z + 1 ) )
print(sp.latex(a))
In [ ]:
print(sp.latex(sp.Integral(z**2,z)))
In [ ]:
from astropy.io import ascii
from astropy.table import QTable
In [ ]:
my_table = QTable.read('./MyData/Zodiac.csv', format='ascii.csv')
In [ ]:
my_table[0:3]
In [ ]:
ascii.write(my_table, format='latex')