@@ -159,12 +159,12 @@ function.
159159
160160...
161161
162- Wait, didn't we just look at instantaneous rates of changes (blarh my
163- tounge is getting tired) in the previous section on differences? Well
164- yes, and the difference quotient for a function at a point with a very
165- small step $ h $ is indeed a good way to numerically approximate the
166- derivative of a function. From what we found then, we can derive a
167- general expression for instantaneous rate of change
162+ Wait, didn't we just look at instantaneous rates of changes in the
163+ previous section on differences? Well yes, and the difference quotient
164+ for a function at a point with a very small step $ h $ is indeed a good
165+ way to numerically approximate the derivative of a function. From what
166+ we found then, we can derive a general expression for instantaneous
167+ rate of change
168168
169169$$ \frac {(\Delta f)(h, x)}{(\Delta id)(h, x)} = \frac {f(x + h) - f(x)}{h}$$
170170
@@ -281,15 +281,15 @@ For example, how do we derive `f :+ g`? Let's start by doing it
281281mathematically.
282282
283283\begin {align* }
284- D(f + g) &= a \mapsto lim_{h \to 0} \frac {(f + g)[a + h] - (f + g)[a]}{h} \\
285- & \text { \{ Addition of functions \} } \\
286- &= a \mapsto lim_{h \to 0} \frac {f(a + h) + g(a + h) - (f(a) + g(a))}{h} \\
287- &= a \mapsto lim_{h \to 0} \frac {f(a + h) + g(a + h) - f(a) - g(a)}{h} \\
288- &= a \mapsto lim_{h \to 0} (\frac {f(a + h) - f(a)}{h} + \frac {g(a + h) - g(a)}{h}) \\
289- &= a \mapsto ((lim_{h \to 0} \frac {f(a + h) - f(a)}{h}) + (lim_{h \to 0} \frac {g(a + h) - g(a)}{h})) \\
290- &= (a \mapsto lim_{h \to 0} \frac {f(a + h) - f(a)}{h}) + (a \mapsto lim_{h \to 0} \frac {g(a + h) - g(a)}{h}) \\
291- & \text { \{ Definition of derivative \} } \\
292- &= D(f) + D(g)
284+ D(f + g) = & a \mapsto lim_{h \to 0} \frac {(f + g)[a + h] - (f + g)[a]}{h} \\
285+ & \text { \{ Addition of functions \} } \\
286+ = & a \mapsto lim_{h \to 0} \frac {f(a + h) + g(a + h) - (f(a) + g(a))}{h} \\
287+ = & a \mapsto lim_{h \to 0} \frac {f(a + h) + g(a + h) - f(a) - g(a)}{h} \\
288+ = & a \mapsto lim_{h \to 0} (\frac {f(a + h) - f(a)}{h} + \frac {g(a + h) - g(a)}{h}) \\
289+ = & a \mapsto ((lim_{h \to 0} \frac {f(a + h) - f(a)}{h}) + (lim_{h \to 0} \frac {g(a + h) - g(a)}{h})) \\
290+ = & (a \mapsto lim_{h \to 0} \frac {f(a + h) - f(a)}{h}) + (a \mapsto lim_{h \to 0} \frac {g(a + h) - g(a)}{h}) \\
291+ & \text { \{ Definition of derivative \} } \\
292+ = & D(f) + D(g)
293293\end {align* }
294294
295295Oh, it's just the sum of the derivatives of both functions! The
@@ -306,29 +306,43 @@ And the limit
306306
307307$$ \lim _{x \to 0} \frac {sin x}{x} = 1 $$
308308
309- which can be proved using the unit circle and squeeze theorem, but we
310- won't do that here.
309+ the proof of which is left as an exercise to the reader
310+
311+ **Exercise.** Prove the limit $ \lim _{x \to 0} \frac {sin x}{x} = 1 $
312+
313+ <details>
314+ <summary>**Hint**</summary>
315+ <div>
316+
317+ This limit can be proven using the [unit circle](https://en.wikipedia.org/wiki/Unit_circle) and [squeeze theorem](https://en.wikipedia.org/wiki/Squeeze_theorem)
318+
319+ </div>
320+ </details>
311321
312322Then, the differentiation
313323
314324\begin {align* }
315- D(sin) &= a \mapsto lim_{h \to 0} \frac {sin(a + h) - sin(a)}{h} \\
316- & \text { \{ trig. sum-to-product \} } \\
317- &= a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {a + h - a}{2}\right ) \cos\left (\frac {a + h + a}{2}\right )}{h} \\
318- &= a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {h}{2}\right ) \cos\left (\frac {2a + h}{2}\right )}{h} \\
319- &= a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {h}{2}\right ) \cos\left (\frac {2a + h}{2}\right )}{h} \\
320- &= a \mapsto lim_{h \to 0} \frac {\sin\left (\frac {h}{2}\right )}{\frac {h}{2}} \cos\left (\frac {2a + h}{2}\right ) \\
321- & \text {\{ } h \text { approaches } 0 \text {\} } \\
322- &= a \mapsto 1 \cos\left (\frac {2a + 0}{2}\right ) \\
323- &= a \mapsto \cos (a) \\
324- &= \cos \\
325+ D(sin) = & a \mapsto lim_{h \to 0} \frac {sin(a + h) - sin(a)}{h} \\
326+ & \text { \{ trig. sum-to-product \} } \\
327+ = & a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {a + h - a}{2}\right ) \cos\left (\frac {a + h + a}{2}\right )}{h} \\
328+ = & a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {h}{2}\right ) \cos\left (\frac {2a + h}{2}\right )}{h} \\
329+ = & a \mapsto lim_{h \to 0} \frac {2 \sin\left (\frac {h}{2}\right ) \cos\left (\frac {2a + h}{2}\right )}{h} \\
330+ = & a \mapsto lim_{h \to 0} \frac {\sin\left (\frac {h}{2}\right )}{\frac {h}{2}} \cos\left (\frac {2a + h}{2}\right ) \\
331+ & \text {\{ } h \text { approaches } 0 \text {\} } \\
332+ = & a \mapsto 1 \cos\left (\frac {2a + 0}{2}\right ) \\
333+ = & a \mapsto \cos (a) \\
334+ = & \cos \\
325335\end {align* }
326336
327337Again, trivial definition in Haskell
328338
329339> derive Sin = Cos
330340
331- I'll leave the proving of the rest of the implementations as an exercise to you, the reader.
341+ **Exercise.** Derive the rest of the cases using the definition of the derivative
342+
343+ <details>
344+ <summary>**Solution**</summary>
345+ <div>
332346
333347> derive Exp = Exp
334348> derive Log = Const 1 :/ Id
@@ -338,7 +352,8 @@ I'll leave the proving of the rest of the implementations as an exercise to you,
338352> derive (f :- g) = derive f :- derive g
339353> derive (f :* g) = derive f :* g :+ f :* derive g
340354> derive (f :/ g) = (derive f :* g :- f :* derive g) :/ (g:^ (Const 2 ))
341- > derive (f :^ g) = f:^ (g :- Const 1 ) :* (g :* derive f :+ f :* (Log :. f) :* derive g)
355+ > derive (f :^ g) = f:^ (g :- Const 1 )
356+ > :* (g :* derive f :+ f :* (Log :. f) :* derive g)
342357> derive Id = Const 1
343358> derive (Const _) = Const 0
344359> derive (f :. g) = derive g :* (derive f :. g)
@@ -352,7 +367,8 @@ for integral is *Antiderivative*...
352367
353368> derive (I c f) = f
354369
355-
370+ </div>
371+ </details>
356372
357373Keep it simple
358374----------------------------------------------------------------------
@@ -377,7 +393,7 @@ But still, we shouldn't have to do that manually! Let's have
377393Mr. Computer help us out, by writing a function to simplify
378394expressions.
379395
380- We'll write a `simplify` function will reduce an expression to a
396+ We'll write a `simplify` function which will reduce an expression to a
381397simpler, equivalent expression. Sounds good, only... what exactly does
382398"simpler" mean? Is $ 10 $ simpler than $ 2 + 2 * 4 $ ? Well, yes obviously,
383399but there are other expressions where this is not the case. For
@@ -400,24 +416,34 @@ be, so we don't have to simplify those. When it comes to the
400416arithmetic operations, most interesting is the cases of one operand
401417being the identity element.
402418
419+ For addition and subtraction, it's $ 0 $
420+
403421> simplify (f :+ g) = case (simplify f, simplify g) of
404422> (Const 0 , g') -> g'
405423> (f', Const 0 ) -> f'
406424> (Const a, Const b) -> Const (a + b)
407425> (f', g') | f' == g' -> simplify (Const 2 :* f')
408426> (Const a :* f', g') | f' == g' -> simplify (Const (a + 1 ) :* f')
409427> (f', Const a :* g') | f' == g' -> simplify (Const (a + 1 ) :* f')
410- > (Const a :* f', Const b :* g') | f' == g' -> simplify (Const (a + b) :* f')
428+ > (Const a :* f', Const b :* g') | f' == g'
429+ > -> simplify (Const (a + b) :* f')
411430> (f', g') -> f' :+ g'
412431> simplify (f :- g) = case (simplify f, simplify g) of
413432> (f', Const 0 :- g') -> f' :+ g'
414433> (f', Const 0 ) -> f'
415- > (Const a, Const b) -> if a > b then Const (a - b) else Const 0 :- Const (b - a)
434+ > (Const a, Const b) -> if a > b
435+ > then Const (a - b)
436+ > else Const 0 :- Const (b - a)
416437> (f', g') | f' == g' -> Const 0
417438> (Const a :* f', g') | f' == g' -> simplify (Const (a - 1 ) :* f')
418- > (f', Const a :* g') | f' == g' -> Const 0 :- simplify (Const (a - 1 ) :* f')
419- > (Const a :* f', Const b :* g') | f' == g' -> simplify ((Const a :- Const b) :* f')
439+ > (f', Const a :* g') | f' == g'
440+ > -> Const 0 :- simplify (Const (a - 1 ) :* f')
441+ > (Const a :* f', Const b :* g') | f' == g'
442+ > -> simplify ((Const a :- Const b) :* f')
420443> (f', g') -> f' :- g'
444+
445+ For multiplication and division, the identity element is $ 1 $ , but the case of one operand being $ 0 $ is also interesting
446+
421447> simplify (f :* g) = case (simplify f, simplify g) of
422448> (Const 0 , g') -> Const 0
423449> (f', Const 0 ) -> Const 0
@@ -435,13 +461,35 @@ being the identity element.
435461> (f', Const 1 ) -> f'
436462> (f', g') | f' == g' -> Const 1
437463> (f', g') -> f' :/ g'
464+
465+ Exponentiation is not commutative, and further has no (two-sided) identity element. However, it does have an "asymmetric" identity element: the right identity $ 1 $ !
466+
438467> simplify (f :^ g) = case (simplify f, simplify g) of
439468> (f', Const 1 ) -> f'
440469> (f', g') -> f' :^ g'
470+
471+ **Exercises.** Look up (or prove by yourself) more identities (of
472+ expressions, not identity elements) for exponentiation and implement
473+ them.
474+
475+ <details>
476+ <summary>**Solution**</summary>
477+ <div>
478+
479+ For example, there is the identity of negative exponents. For any integer $ n$ and nonzero $ b$
480+
481+ $$ b^{-n} = \frac {1}{b^n}$$
482+
483+ </div>
484+ </details>
485+
486+ Intuitively, the identity function is the identity element for function composition
487+
441488> simplify (f :. g) = case (simplify f, simplify g) of
442489> (Id , g') -> g'
443490> (f', Id ) -> f'
444491> (f', g') -> f' :. g'
492+
445493> simplify (Delta h f) = Delta h (simplify f)
446494> simplify (D f) = D (simplify f)
447495> simplify (I c f) = I c (simplify f)
@@ -456,3 +504,5 @@ With this new function, many expressions become much more readable!
456504< (cos + (2 * id ))
457505
458506A sight for sore eyes!
507+
508+ **Exercise.** Think of more ways an expression can be "simplified" , and add your cases to the implementation.
0 commit comments