import sympy as sp
sp.init_printing() # to get pretty output
from IPython.display import display # can use display instead of print for nice output
x, y, z = sp.symbols('x, y, z')
ex1 = x*sp.cos(x) # note, expressions are immutable
ex2 = x**2 + y**2 + z
ex3 = sp.sqrt(x+y/2)
display(ex1)
display(ex2)
ex3
ans1 = ex1.subs(x, 1.0) # give 1.0 evaluates numerically
ans2 = ex1.subs(x, 1) # treats 1 as a symbol
ans3 = ex1.subs(x, y)
ans4 = ex1.subs(x, y**2+z)
display(ans1)
display(ans2)
display(ans3)
display(ans4)
ex = x**2 + 2*x*z - y
ex.subs( [(x,4.5), (y,2.2)] )
evalf
function to get numeric results.ex = x**2 + y
ex.evalf(subs={x: 3, y: 4})
sp.pi.evalf(100)
ex = (x**2 + 3*x + 2)/(x+1)
ex.simplify() # these are all the same
sp.simplify(ex)
sp.simplify((x**2+3*x+2)/(x+1))
ex = (x+2)*(x-3)
ex.expand() # these are all the same
sp.expand(ex)
sp.expand( (x+2)*(x-3) )
ex = x**3 - x**2 + x - 1
ex.factor()
sp.factor(ex)
sp.factor( x**3 - x**2 + x - 1 )
collect
cancel
apart
trigsimp
expand_trig
powsimp
expand_power_exp
expand_power_base
pow_denest
expand_log
logcombine
ex = sp.exp(x**2)
ex.diff(x) # these are the same
sp.diff(ex, x)
sp.diff(sp.exp(x**2), x)
sp.diff(x**4, x,x,x) # take the third derivative (note extra x's)
Take the following derivative: $$\frac{\partial^3}{\partial x\partial y\partial z}e^{xyz}.$$
ex = sp.exp(x*y*z)
sp.diff(ex, x,y,z)
Derivative
function doesn't evaluate the derivative, it is just used for making an expression.doit
function.ex = sp.exp(x*y*z)
my_der = sp.Derivative(ex, x, y, z) # ex.Derivative(x,y,z) doesn't work
my_der
my_der.doit() # now evaluate the derivative
integrate
function evaluates and integralIntegral
function creates an integral expressiondoit
function evaluates an integral expressionsp.integrate(x**2, x)
Integrate this: $$\int_0^{\infty}e^{-x}dx.$$
sp.integrate(sp.exp(-x), (x,0,sp.oo)) # infinity is "oo": double o's
sp.integrate(x*sp.exp(-x), (x,y,sp.oo))
Do a double integral: $$\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}e^{-x^2-y^2}dxdy.$$
sp.integrate(sp.exp(-x**2-y**2), (x,-sp.oo,sp.oo), (y,-sp.oo,sp.oo))
ex = sp.Integral(sp.log(x)**2, x)
ex
ex.doit()
ex = sp.Integral(sp.log(x)**2, (x,0,2))
ex
ex.doit()
ex.doit().evalf()
limit
function to evaluate a limit,Limit
to create a limit expression,doit
to evaluate a limit expressionEvaluate $$\lim_{x\rightarrow 0}\frac{\sin(x)}{x}.$$
sp.limit(sp.sin(x)/x, x,0)
sp.solve(x**2-y, x)
The above was in the form $f(x)=0$. If we have instead $x^2=1$, then use the Eq
function
ex = sp.Eq(x**2,y) # creates expression x^2 = 1
sp.solve(ex, x) # these are the same
sp.solve(sp.Eq(x**2,y), x)
Solve this: \begin{align} x-y+2 &=0 \\ x+y-3 &=0 \end{align}
sp.solve( (x-y+2, x+y-3), (x, y))
Solve this: \begin{align} xy-7 &=0 \\ x+y-6 &= 0 \end{align}
sp.solve( (x*y-7, x+y-6), (x,y) )
dsolve
f = sp.Function('f') # define a symbol for holding a function
f(x) # can also use f,g = sp.symbols('f,g', cls=Function)
f(x).diff(x) # these are the same
sp.diff(f(x), x)
f(x).diff(x,x) # second derivative
Setup and solve this equation: $$f^{\prime\prime}(x) - 2f^{\prime}(x)+f(x)= \sin(x).$$
my_diffeq = sp.Eq(f(x).diff(x,x) - 2*f(x).diff(x) + f(x), sp.sin(x))
my_diffeq
sp.dsolve(my_diffeq, f(x))
A = sp.Matrix( [ [1,-1], [3,4], [0,2] ] )
A
b = sp.Matrix([1,2]) # assumes a column vector
b
A.shape
A.row(1)
A.row_del(0)
A
x = A**-1 * b
x
A*x # should equal b
m11, m12, m21, m22 = sp.symbols("m11, m12, m21, m22")
b1, b2 = sp.symbols("b1, b2")
A = sp.Matrix([ [m11, m12], [m21, m22] ])
b = sp.Matrix([b1,b2])
A
b
A.inv()*b # these are the same
A**-1 * b