cs107-lecture-examples

Example codes used during Harvard CS107 lectures
git clone https://git.0xfab.ch/cs107-lecture-examples.git
Log | Files | Refs | README | LICENSE

jacobian.py (431B)


      1 #!/usr/bin/env python3
      2 # File       : jacobian.py
      3 # Description: Compute the Jacobian of f(x) symbolically
      4 # Copyright 2021 Harvard University. All Rights Reserved.
      5 import sympy as sym
      6 
      7 x = sym.symbols('x')  # define the symbolic variable
      8 sym.init_printing(use_unicode=True)  # for pretty terminal printing
      9 
     10 f = x - sym.exp(-2 * sym.sin(4 * x)**2)  # our function f(x)
     11 J = sym.diff(f, x)  # compute the derivative w/r/t x
     12 print(J)