\documentclass{article}
\usepackage{listings}
\usepackage{graphicx}

\author{Nada Amin\\
\texttt{namin@mit.edu}
}

\renewcommand{\labelenumi}{(\alph{enumi})}

\date{Due: 19 September 2008}
\title{6.946 Assignment 2}

\lstset{
  language=Lisp,
  %labelstep=5,
  %frame=trbl,
  %indent=15pt,
  %indent=0pt,
  xleftmargin=-10pt,
  xrightmargin=10pt,
  showlines=false,
  basicstyle=\small\tt,
  stringstyle=\itshape,
  showstringspaces=false%,
}

\begin{document}

\maketitle

\section*{8.1: Chain rule}

$F(x,y) = x^2 y^3$\\
$G(x,y) = (F(x,y), y)$\\
$H(x,y) = F(F(x,y), y)$\\

\begin{enumerate}
\item
$\partial_0F(x,y) = 2 x y^3$\\
$\partial_1F(x,y) = 3 x^2 y^2$\\

\item
$\partial_0F(F(x,y),y) = 2 x^2 y^6$\\
$\partial_1F(F(x,y),y) = 3 x^4 y^8$\\

\item
$\partial_0G(x,y) = (2 x y^3, 0)$\\
$\partial_1G(x,y) = (3 x^2 y^2, 1)$\\

\item
$DF(a,b) = (2 a b^3, 3 a^2 b^2)$\\
$DG(3,5) = ((750,0), (675,1))$\\
$DH(3a^2,5b^3) = (210937500 a^6 b^27, 284765625 a^8 b^24)$\\
\end{enumerate}

\section*{8.2: Computing derivatives}
Representation 1: Identify procedure arguments with function's arguments.
\begin{lstlisting}
(define (f1 x y)
  (* (square x) (cube y)))

(define (g1 x y)
  (up (f1 x y) y))

(define (h1 x y)
  (f (f x y) y))
\end{lstlisting}

Representation 2: Represent the function arguments as slots of a tuple datastructure.
\begin{lstlisting}
(define (f2 v)
  (let ((x (ref v 0))
	(y (ref v 1)))
    (* (square x) (cube y))))

(define (g2 v)
  (let ((x (ref v 0))
	(y (ref v 1)))
    (up (f2 v) y)))

(define h2 (compose f2 g2))
\end{lstlisting}

\begin{enumerate}

\item
\begin{lstlisting}
(print-expression ((D f1) 'x 'y))
(show-expression ((D f2) (up 'x 'y)))
; (down (* 2 x (expt y 3)) (* 3 (expt x 2) (expt y 2)))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ 2 x y^{3}} \cr \cr \displaystyle{ 3 x^{2} y^{2}}} \right]$$

\item
\begin{lstlisting}
(print-expression ((D f1) (f1 'x 'y) 'y))
(show-expression ((D f2) (up (f2 (up 'x 'y)) 'y)))
; (down (* 2 (expt x 2) (expt y 6)) (* 3 (expt x 4) (expt y 8)))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ 2 x^{2} y^{6}} \cr \cr \displaystyle{ 3 x^{4} y^{8}}} \right]$$

\item
\begin{lstlisting}
(print-expression ((D g1) 'x 'y))
(show-expression ((D g2) (up 'x 'y)))
; (down (up (* 2 x (expt y 3)) 0) 
;       (up (* 3 (expt x 2) (expt y 2)) 1))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ \left( \matrix{ \displaystyle{ 2 x y^{3}} \cr \cr \displaystyle{ 0}} \right)} \cr \cr \displaystyle{ \left( \matrix{ \displaystyle{ 3 x^{2} y^{2}} \cr \cr \displaystyle{ 1}} \right)}} \right]$$

\item
\begin{lstlisting}
(print-expression ((D f1) 'a 'b))
(show-expression ((D f2) (up 'a 'b)))
; (down (* 2 a (expt b 3)) (* 3 (expt a 2) (expt b 2)))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ 2 a b^{3}} \cr \cr \displaystyle{ 3 a^{2} b^{2}}} \right]$$

\begin{lstlisting}
(print-expression ((D g1) 3 5))
(show-expression ((D g2) (up 3 5)))
; (down (up 750 0) (up 675 1))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ \left( \matrix{ \displaystyle{ 750} \cr \cr \displaystyle{ 0}} \right)} \cr \cr \displaystyle{ \left( \matrix{ \displaystyle{ 675} \cr \cr \displaystyle{ 1}} \right)}} \right]$$

\begin{lstlisting}
(print-expression ((D h1) (* 3 (square 'a)) (* 5 (cube 'b))))
(show-expression ((D h2) (up (* 3 (square 'a)) (* 5 (cube 'b)))))
; (down (* 210937500 (expt a 6) (expt b 27)) 
;       (* 284765625 (expt a 8) (expt b 24)))
\end{lstlisting}
$$\left[ \matrix{ \displaystyle{ 210937500 a^{6} b^{27}} \cr \cr \displaystyle{ 284765625 a^{8} b^{24}}} \right]$$

\end{enumerate}

\end{document}