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
subs
functionExercise
ex1=x*sp.cos(x)
make the following substitutions:
Exercise
ex = x**2 + 2*x*z - y
make the substitution $x\rightarrow 4.5$, $y\rightarrow 2.2$.ex = x**2 + 2*x*z - y
evalf
function to get numeric results.ex = x**2 + y
ex.evalf(subs={x: 3, y: 4})
sp.pi.evalf(1000)
ex = (x**2 + 3*x + 2)/(x+1)
ex = (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)
Question: how would you take the third derivative of $x^4$?
Exercise
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 expressionExercise
integrate
on these:Integrate
and doit
on the previous expression, with and without the bounds.
Do a double integral: $$\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}e^{-x^2-y^2}dxdy.$$
ex = sp.Integral(sp.log(x)**2, (x,0,2))
ex
ex.doit() # numbers retained
ex.doit().evalf() # chain up results and evaluate final expression
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) # unlike integrate we don't combine var and bounds with a tuple (!?)
Exercise
Solve $$x^2-y=0$$ with respect to $x$ using the solve
function.
The above was in the form $f(x)=0$. If we have instead $x^2=1$, then use the Eq
function to create the expression.
ex = sp.Eq(x**2,y) # creates expression x^2 = 1
Exercise
Exercise
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) # note: we refer to f(x) not f
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] ]) # syntax is similar to numpy
b = sp.Matrix([b1,b2])
A
b
A.inv()*b # these are the same
A**-1 * b