# Tutorial Brief

SymPy is symbolic mathematics library written completely in Python and doesn't require any dependencies.

Finding Help:

#### NumPy

Base N-dimensional array package

#### SciPy

Fundamental library for scientific computing

#### Matplotlib

Comprehensive 2D Plotting

#### IPython

Enhanced Interactive Console

#### SymPy

Symbolic mathematics

#### Pandas

Data structures & analysis

http://sympy.org/

# Import SymPy



In [1]:

from sympy import *
import math




In [2]:

3 + math.sqrt(3)




Out[2]:

4.732050807568877




In [3]:

expr = 3 * sqrt(3)
expr




Out[3]:

3*sqrt(3)




In [4]:

init_printing(use_latex='mathjax')




In [5]:

expr




Out[5]:

3*sqrt(3)




In [6]:

expr = sqrt(8)
expr




Out[6]:

2*sqrt(2)



# symbols() & Symbol()



In [ ]:

x, y = symbols("x y")




In [ ]:

expr = x**2 + y**2
expr




In [ ]:

expr = (x+y)**3
expr



## Assumptions for symbols



In [ ]:

a = Symbol("a")




In [ ]:

a.is_imaginary




In [ ]:

b = Symbol("b", integer=True)




In [ ]:

b.is_imaginary




In [ ]:

c = Symbol("c", positive=True)




In [ ]:

c.is_positive




In [ ]:

c.is_imaginary



# Imaginary Numbers



In [ ]:

I




In [ ]:

I ** 2



# Rational()



In [ ]:

Rational(1,3)




In [ ]:

Rational(1,3) + Rational(1,2)



# Numerical evaluation



In [ ]:

expr = Rational(1,3) + Rational(1,2)
N(expr)




In [ ]:

N(pi, 100)




In [ ]:

pi.evalf(100)



# subs()



In [ ]:

expr = x**2 + 2*x + 1
expr




In [ ]:

expr.subs(x, 1)




In [ ]:

expr = pi * x**2
expr




In [ ]:

expr.subs(x, 3)




In [ ]:

N(_)



# factor() and expand()



In [ ]:

expr = (x + y) ** 2
expr




In [ ]:

expand(expr)




In [ ]:

factor(_)



# simplify()



In [ ]:

expr = (2*x + Rational(1,3)*x + 4) / x
expr




In [ ]:

simplify(expr)




In [ ]:

expr = "(2*x + 1/3*x + 4)/x"
simplify(expr)




In [ ]:

expr = sin(x)/cos(x)
expr




In [ ]:

simplify(expr)



# apart() and together()



In [ ]:

expr = 1/(x**2 + 2*x)
expr




In [ ]:

apart(expr)




In [ ]:

together(_)



# Calculus



In [ ]:

diff(sin(x), x)




In [ ]:

diff(log(x**2 + 1) + 2*x, x)




In [ ]:

integrate(cos(x), x)




In [ ]:

Integral(sin(x), (x,0,pi))




In [ ]:

N(_)



# Sum()



In [ ]:

expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr




In [ ]:

expr.doit()



# Product()



In [ ]:

expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr




In [ ]:

expr.doit()



# Solve()



In [ ]:

expr = 2*x + 1
solve(expr)




In [ ]:

expr = x**2 - 1
solve(expr)




In [ ]:

expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))



# Units



In [ ]:

from sympy.physics import units as u




In [ ]:

5. * u.milligram




In [ ]:

1./2 * u.inch




In [ ]:

1. * u.nano




In [ ]:

u.watt




In [ ]:

u.ohm



## Converting from Kilometers/hours to Miles/hours



In [ ]:

kmph = u.km / u.hour
mph = u.mile / u.hour

N(mph / kmph)




In [ ]:

80 * N(mph / kmph)



# Working with NumPy / Pandas and Matplotlib



In [ ]:

def sympy_expr(x_val):
expr = x**2 + sqrt(3)*x - Rational(1,3)
return expr.subs(x, x_val)




In [ ]:

sympy_expr(3)




In [ ]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt




In [ ]:

list1 = np.arange(1,1000)
list2 = pd.Series(list1)




In [ ]:

%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]




In [ ]:

%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)




In [ ]:

expr = x**2 + sqrt(3)*x - Rational(1,3)

lf = lambdify(x, expr)




In [ ]:

%timeit lf(list1)
%timeit lf(list2)




In [ ]:

fig = plt.figure()

x_vals = np.linspace(-5.,5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

plt.show();




In [ ]: