[NumCS] Add more sympy tricks

This commit is contained in:
2026-01-18 11:38:17 +01:00
parent 3bc264184b
commit 5de9a21514
4 changed files with 19 additions and 0 deletions

Binary file not shown.

View File

@@ -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.hermite_poly(5) # Creates hermite poly of degree 5
sp.chebyshevt_poly(5) # Creates chebychev T (first kind) 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 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()

View 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)

View File

@@ -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: You usually want to follow something like the below code:
\inputcode{python}{parts/06_python/03_sympy-ex.py} \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}