Key Word(s): Forwward mode, Reverse mode
Exercise 1¶
Consider the function \begin{align*} f\left(x,y\right) = \begin{bmatrix} x^{2} + y^{2} \\ e^{x+y} \end{bmatrix} . \end{align*}
Part A¶
Calculate the Jacobian and evaluate it at the point $\left(1,1\right)$.
\begin{align*} J &= \begin{bmatrix} \partial f_{1} / \partial x & \partial f_{1} / \partial y \\ \partial f_{2} / \partial x & \partial f_{2} / \partial y \end{bmatrix} \\ &= \begin{bmatrix} 2x & 2y \\ e^{x+y} & e^{x+y} \end{bmatrix} . \end{align*}At the point $\left(1,1\right)$, \begin{align*} J &= \begin{bmatrix} 2 & 2 \\ e & e \end{bmatrix} . \end{align*}
Part B¶
Calculate $Jp$ for
i.) $$p = \begin{bmatrix}1 \\ -2\end{bmatrix}$$
\begin{align*} Jp = \begin{bmatrix} 2 & 2 \\ e & e \end{bmatrix} \begin{bmatrix} 1 \\ -2 \end{bmatrix} = \begin{bmatrix} -2 \\ -e \end{bmatrix} \end{align*}ii.) $$p = \begin{bmatrix}1 \\ 1\end{bmatrix}$$
\begin{align*} Jp = \begin{bmatrix} 2 & 2 \\ e & e \end{bmatrix} \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 4 \\ 2e \end{bmatrix} \end{align*}Part C¶
Repeat Part B using the evaluation trace provided below. Take note of how the seed vectors enter the process! Compare the end of the AD process with the results from Part B.
Exercise 2¶
Write some code to do forward mode on the function $$f\left(x\right) = x^{4}$$ where $r\in\mathbb{R}$. Test your implementation by evaluating the derivative and function at the point $x = 3$ for $r=4$.
You have broad freedom on how you implement this. Here are some options:
- Write a function called
my_pow
that returns a datastructure (e.g.list
ortuple
or something else) containing $f\left(3\right)$ and $f^{\prime}\left(3\right)$. This is the simplest approach but it's also the least reusable. - Implement with a closure. The outer function will take in the value of $r$. The inner function will take in a datastructure representing the point to evaluate the function at and the seed for the derivative.
- Implement with a class. Overload the
__pow__
method to accomplish your goals!
The goal of this exercise is for you to start thinking about how you would go about implementing some elemental functions for your final project.
Deliverables¶
exercise_2.py
- Contains your code
- Contains a demo inside ablock
if __name__ == "__main__":