Chapter 9: Our Notation

(require '[sicmutils.env :refer :all])

Notation Appendix. This is all about getting cozy with scheme, and with the various idiosyncracies of the tuple and functional notation.

Exercise 9.1 Chain Rule

You're supposed to do these by hand, so I'll do that in the textbook. But here, let's redo them on the machine.

Compute inline_formula not implemented and inline_formula not implemented

First, let's define the functions we need.

(defn F [x y]
  (* (square x)
     (cube y)))
(defn G [x y]
  (up (F x y) y))
(defn H [x y]
  (F (F x y) y))

You can do this with explicit partials:

(let [f (down ((partial 0) F) ((partial 1) F))]
  (f 'x 'y))

Or with the inline_formula not implemented symbol:

((D F) 'x 'y)

Or, we could show that they're equivalent this way:

(let [f (down ((partial 0) F) ((partial 1) F))]
  (- ((D F) 'x 'y)
     (f 'x 'y)))
Compute inline_formula not implemented and inline_formula not implemented

inline_formula not implemented is already that composition, so:

((D H) 'x 'y)
Compute inline_formula not implemented and inline_formula not implemented
((D G) 'x 'y)
Compute inline_formula not implemented, inline_formula not implemented and inline_formula not implemented
(up ((D F) 'a 'b)
    ((D G) 3 5)
    ((D H) (* 3 (square 'a)) (* 5 (cube 'b))))

Exercise 9.2: Computing Derivatives

A further exercise is to try defining the functions so that they use explicit tuples, so you can compose them:

(defn F* [[x y]]
  (* (square x) (cube y)))
(defn G* [[_ y :as v]]
  (up (F* v) y))
(def H* (compose F* G*))

to be really pro, I'd make a function that takes these as arguments and prints a nice formatted exercise output. Let's do the final exercise, for fun:

(up ((D F*) (up 'a 'b))
    ((D G*) (up 3 5))
    ((D H*) (up (* 3 (square 'a)) (* 5 (cube 'b)))))
Runtimes (1)