Math and Physics Using Sympy: July 8, 2015
Math and Physics Using Sympy: July 8, 2015
July 8, 2015
SymPy
Computer Algebra System
This imports all of SymPy’s methods and variables and you can
start using it right away.
SymPy
In [1]:
In Python there are two types of number objects: int and float.
>>> 3
3 # an int
>>> 3.0
3.0 # a float
# Python 3
1/2 --> 0.5
You can also use the global SymPy method N() to get numerical
values. By providing an integer as argument you can easily change
the number of digits of precision the approximations should return.
>>> pi . n (400) # equivalent to N ( pi , 400)
3.141592653589793238462643383279502884
19716939937510582097494459230781640628
62089986280348253421170679821480865132
82306647093844609550582231725359408128
48111745028410270193852110555964462294
89549303819644288109756659334461284756
48233786783165271201909145648566923460
34861045432664821339360726024914127372
45870066063155881748815209209628292540
91715364367892590360011330530548820466
521384146951941511609
SymPy
Symbols - from sympy import Symbol, symbols
You can basically name your variables anything you want, but you
need to avoid overriding SymPy’s built in names, such as Q, C, O,
S, I, N, and E. I is the unit imaginary number, E is the base of
the natural logarithm (exp(x) == E**x), and so on. . .
SymPy
Symbols - from sympy import Symbol, symbols
The result is a list of solutions for x that satisfy the above equation.
SymPy
Solving equations - from sympy import solve
The best part of solve is, that it also works with symbolic
expressions. For example let us look for the solution of
ax 2 + bx + c = 0.
>>> a , b , c = symbols ( ’a b c ’)
>>> solve ( a * x **2+ b * x +c , x )
[( - b + sqrt ( -4* a * c + b **2))/(2* a ) ,
-( b + sqrt ( -4* a * c + b **2))/(2* a )]
Remember that the roots of the polynomial P(x) are defined as the
solutions to the equation P(x) = 0. We can use the solve
function to find the roots of the polynomial:
>>> roots = solve (P , x )
>>> roots
[1 , 2 , 3]
# let ’s check i P equals (x -1)( x -2)( x -3)
>>> simplify ( P - (x - roots [0])*( x - roots [1])*( x - roots [2]) )
0
SymPy
Trigonometry from sympy import sin, cos, tan, trigsimp, expand_trig
Infinity
from numpy import oo
The symbol for infinity in SymPy is denoted as two lowercase os.
SymPy knows how to treat infinity correctly in expressions.
>>> oo +1
oo
>>> 5000 < oo
True
>>> 1/ oo
0
SymPy
Calculus - Limits - from sympy import limit, oo
>>> f = x **2
>>> F = integrate (f , x )
>>> F
x **3/3
>>> diff (F , x )
x **2
SymPy
Calculus - Fundamental theorem of calculus
>>> f = x **2
>>> df = diff (f , x )
>>> df
2* x
>>> integrate ( df , x )
x **2 # + C
SymPy
Calculus - Fundamental theorem of calculus
f (n) (c)
an (x) = (x − c)n ,
n!
where f (n) (x) is the value of the nth derivative of f (x) evaluated at
x = c.
A Taylor series expansion at x = 0 is called Maclaurin series.
SymPy
Calculus - Taylor series
Not only can we use series to approximate numbers, we can use
them to approximate functions!
A power series is a series whose terms contain different powers of
the variable x. For example, the power series of the function
exp(x) = e x :
∞
x2 x3 x4 x5 X xn
exp(x) ≡ 1 + x + + + + + ··· =
2 3! 4! 5! n!
n=0
To get rid of the (x − 1) terms and to get a familiar result for the
Taylor series we can do the following trick. Instead of expanding
ln(x) around x = 1, we obtain a more readable expression by
expanding ln(x + 1) around x = 0.
>>> series ( ln ( x +1) , x , 0 , 6)
x - x **2/2 + x **3/3 - x **4/4 + x **5/5 + O ( x **6)
SymPy
Vectors and Matrices
~v = (v1 , v2 , v3 ) ∈ (R, R, R) ≡ R3 .
u~ · ~v ≡ ux vx + uy vy + uz vz ≡ ||~
u || ||~v || cos(ϕ) ∈ R,
u~ × ~v = (uy vz − uz vy , uz vx − ux vz , ux vy − uy vx )
0 0 λn | |