diff --git a/semester3/numcs/numcs-summary.pdf b/semester3/numcs/numcs-summary.pdf index 8ef6128..b0043db 100644 Binary files a/semester3/numcs/numcs-summary.pdf and b/semester3/numcs/numcs-summary.pdf differ diff --git a/semester3/numcs/parts/06_python/03_sympy-ex.py b/semester3/numcs/parts/06_python/03_sympy-ex.py index 7eaed70..d6c4e4e 100644 --- a/semester3/numcs/parts/06_python/03_sympy-ex.py +++ b/semester3/numcs/parts/06_python/03_sympy-ex.py @@ -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() diff --git a/semester3/numcs/parts/06_python/03_sympy-hessian.py b/semester3/numcs/parts/06_python/03_sympy-hessian.py new file mode 100644 index 0000000..3049ad9 --- /dev/null +++ b/semester3/numcs/parts/06_python/03_sympy-hessian.py @@ -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) diff --git a/semester3/numcs/parts/06_python/03_sympy.tex b/semester3/numcs/parts/06_python/03_sympy.tex index abad9b0..18599f1 100644 --- a/semester3/numcs/parts/06_python/03_sympy.tex +++ b/semester3/numcs/parts/06_python/03_sympy.tex @@ -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}