mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-03-14 10:50:05 +01:00
[NumCS] Add more sympy tricks
This commit is contained in:
Binary file not shown.
@@ -13,3 +13,5 @@ roots = sp.roots(f) # Computes the roots of function f analytically
|
||||
sp.hermite_poly(5) # Creates hermite poly of degree 5
|
||||
sp.chebyshevt_poly(5) # Creates chebychev T (first kind) poly of degree 5
|
||||
sp.chebyshevu_poly(5) # Creates chebychev U (second kind) poly of degree 5
|
||||
# To print nicely rendered math expressions, you can use
|
||||
sp.init_printing()
|
||||
|
||||
14
semester3/numcs/parts/06_python/03_sympy-hessian.py
Normal file
14
semester3/numcs/parts/06_python/03_sympy-hessian.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import sympy as sp
|
||||
|
||||
sym = sp.symbols("x, y")
|
||||
x, y = sym
|
||||
f = x**2 + y**2
|
||||
|
||||
# Compute gradient and hessian
|
||||
grad = [sp.diff(f, k) for k in sym]
|
||||
hess = [[sp.diff(f, k).diff(j) for j in sym] for k in sym]
|
||||
|
||||
# Compute jacobian of vector function
|
||||
x_m = sp.MatrixSymbol('x', 2, 1)
|
||||
two_d_func = sp.Matrix([[x_m[0]**2], [x_m[1]**2]])
|
||||
jacobi = two_d_func.jacobian(x_m)
|
||||
@@ -4,3 +4,6 @@ Sympy can be used to do symbolic operations, i.e. similar to what we would do in
|
||||
|
||||
You usually want to follow something like the below code:
|
||||
\inputcode{python}{parts/06_python/03_sympy-ex.py}
|
||||
|
||||
It is also possible to compute the hessian and gradient using sympy (or also the jacobian):
|
||||
\inputcode{python}{parts/06_python/03_sympy-hessian.py}
|
||||
|
||||
Reference in New Issue
Block a user