Skip to content

Commit fdc5612

Browse files
committed
Fix some problems in Calc discovered when testing w/ QuickCheck
1 parent af4da44 commit fdc5612

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Physics/src/Calculus/DifferentialCalc.lhs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,17 @@ Again, trivial definition in Haskell
368368
> :* (g :* derive f :+ f :* (Log :. f) :* derive g)
369369
> derive Id = Const 1
370370
> derive (Const _) = Const 0
371-
> derive (f :. g) = derive g :* (derive f :. g)
371+
372+
For a function composition $f \circ g$ we have to handle the case
373+
where $f$ is a constant function, as we may get division by zero if we
374+
naively apply the chain rule. We'll make use of the `simplify` function,
375+
which we'll define later, to reduce compositions of constant functions to
376+
just constant functions.
377+
378+
> derive (f :. g) =
379+
> case simplify f of
380+
> Const a -> Const 0
381+
> sf -> (derive sf :. g) :* derive g
372382
> derive (Delta h f) = Delta h (derive f)
373383
> derive (D f) = derive (derive f)
374384

@@ -441,7 +451,7 @@ For addition and subtraction, it's $0$
441451
> -> simplify (Const (a + b) :* f')
442452
> (f', g') -> f' :+ g'
443453
> simplify (f :- g) = case (simplify f, simplify g) of
444-
> (f', Const 0 :- g') -> f' :+ g'
454+
> (f', Const 0 :- g') -> simplify (f' :+ g')
445455
> (f', Const 0) -> f'
446456
> (Const a, Const b) -> if a > b
447457
> then Const (a - b)
@@ -498,9 +508,10 @@ $$b^{-n} = \frac{1}{b^n}$$
498508
Intuitively, the identity function is the identity element for function composition
499509

500510
> simplify (f :. g) = case (simplify f, simplify g) of
501-
> (Id, g') -> g'
502-
> (f', Id) -> f'
503-
> (f', g') -> f' :. g'
511+
> (Const a, _) -> Const a
512+
> (Id, g') -> g'
513+
> (f', Id) -> f'
514+
> (f', g') -> f' :. g'
504515

505516
> simplify (Delta h f) = Delta h (simplify f)
506517
> simplify (D (I f')) = simplify f'

Physics/src/Calculus/IntegralCalc.lhs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,5 @@ figure out the integral further.
502502

503503
> eval (I f) = let _F = simplify (integrate f)
504504
> in if _F == (I f)
505-
> then integrateApprox (eval f) 0.01 0
505+
> then integrateApprox (eval f) 0.001 0
506506
> else eval _F

0 commit comments

Comments
 (0)