Skip to content

Commit 9574b1d

Browse files
author
Oskar Lundström
committed
2 parents 2e4d78a + 1aed1c7 commit 9574b1d

File tree

12 files changed

+305
-147
lines changed

12 files changed

+305
-147
lines changed

Book/build.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def build_index(sources):
9999
("Introduction", [
100100
("Introduction'", "Physics/src/Introduction/Introduction.lhs"),
101101
]),
102+
("Calculus", [
103+
("Introduction", "Physics/src/Calculus/Intro.lhs"),
104+
("Function expressions", "Physics/src/Calculus/FunExpr.lhs"),
105+
("Differential calculus", "Physics/src/Calculus/DifferentialCalc.lhs"),
106+
("Integral calculus", "Physics/src/Calculus/IntegralCalc.lhs"),
107+
("Visualization", "Physics/src/Calculus/VisVerApp.lhs"),
108+
]),
109+
("Vectors", [
110+
("Vector", "Physics/src/Vector/Vector.lhs")
111+
]),
102112
("Dimensions", [
103113
("Introduction", "Physics/src/Dimensions/Intro.lhs"),
104114
("Value-level dimensions", "Physics/src/Dimensions/ValueLevel.lhs"),
@@ -110,16 +120,6 @@ def build_index(sources):
110120
"Physics/src/Dimensions/Quantity/Test.lhs"),
111121
("Usage", "Physics/src/Dimensions/Usage.lhs"),
112122
]),
113-
("Vectors", [
114-
("Vector", "Physics/src/Vector/Vector.lhs")
115-
]),
116-
("Calculus", [
117-
("Introduction", "Physics/src/Calculus/Intro.lhs"),
118-
("Function expressions", "Physics/src/Calculus/FunExpr.lhs"),
119-
("Differential calculus", "Physics/src/Calculus/DifferentialCalc.lhs"),
120-
("Integral calculus", "Physics/src/Calculus/IntegralCalc.lhs"),
121-
("Visualization, Verification, and Application", "Physics/src/Calculus/VisVerApp.lhs"),
122-
]),
123123
("Examples", [
124124
("Gungbräda", "Physics/src/Examples/Gungbraeda.lhs"),
125125
("krafter på lådor", "Physics/src/Examples/krafter_pa_lador.lhs"),

Book/style.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ blockquote {
126126
border-left: 3px solid #BABABA;
127127
}
128128

129+
details {
130+
margin-bottom: 30px;
131+
}
132+
133+
/* Exercise solutions / spoilers */
134+
details > div {
135+
margin-top: 10px;
136+
margin-left: 30px;
137+
padding: 10px 20px;
138+
border-radius: 10px;
139+
background-color: #fcfbf0;
140+
}
141+
129142
/* VVVV GENERATED BY PANDOC BELOW VVV */
130143

131144
code {

Physics/src/Calculus/DifferentialCalc.lhs

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
281281
mathematically.
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

295295
Oh, 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

312322
Then, 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

327337
Again, 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

357373
Keep it simple
358374
----------------------------------------------------------------------
@@ -377,7 +393,7 @@ But still, we shouldn't have to do that manually! Let's have
377393
Mr. Computer help us out, by writing a function to simplify
378394
expressions.
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
381397
simpler, equivalent expression. Sounds good, only... what exactly does
382398
"simpler" mean? Is $10$ simpler than $2 + 2 * 4$? Well, yes obviously,
383399
but 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
400416
arithmetic operations, most interesting is the cases of one operand
401417
being 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

458506
A sight for sore eyes!
507+
508+
**Exercise.** Think of more ways an expression can be "simplified", and add your cases to the implementation.

Physics/src/Calculus/FunExpr.lhs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ which is really equivalent to
3333

3434
$$D(x \mapsto x^2) = x \mapsto 2x$$
3535

36-
or
36+
or with more descriptive function names
3737

3838
$$D(square) = double$$.
3939

@@ -136,12 +136,12 @@ left-associative, and set the precedence.
136136
> -- Medium precedence
137137
> infixl 6 :+
138138
> infixl 6 :-
139-
> -- Higher
139+
> -- High precedence
140140
> infixl 7 :*
141141
> infixl 7 :/
142-
> -- Higherer
142+
> -- Higher precedence
143143
> infixl 8 :^
144-
> -- High as a kite
144+
> -- Higherer precedence
145145
> infixl 9 :.
146146

147147

@@ -198,7 +198,7 @@ expressions in a much more human friendly way!
198198
< ghci> carPosition
199199
< (10 + ((50 + (20 * id)) * id))
200200

201-
Still a bit noisy with all the parens, but much better!
201+
Still a bit noisy with all the parentheses, but much better!
202202

203203
Another class we need to instance for our `FunExpr` is
204204
`Arbitrary`. This class is associated with the testing library

0 commit comments

Comments
 (0)