-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.hs
More file actions
44 lines (37 loc) · 1.2 KB
/
Math.hs
File metadata and controls
44 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Math (
expm1,
coshm1,
squareEqSolutions,
infinity,
nan
) where
infinity :: (RealFloat a, Read a) => a
infinity = read "Infinity"
nan :: (RealFloat a, Read a) => a
nan = read "NaN"
-- expm1 x = e^x - 1, accurate for small x
expm1 x | u == 1 = x
| um1 == -1 = -1
| otherwise = um1*x/(log u)
where
u = exp x
um1 = u - 1
-- coshm1 x = cosh(x) - 1, accurate for small x
-- cosh(x) = (e^x + e^(-x))/2-1 =
-- = (e^x - 2 + e^(-x))/2 =
-- = (e^(2x) - 2e^x + 1)/(2e^x) =
-- = (e^x - 1)^2 / (2e^x) =
-- = expm1(x)^2 / (2*(expm1(x)+1))
coshm1 :: Double -> Double
coshm1 x = let exm1 = expm1 x in exm1^2 / (2*(exm1+1))
-- | Solve a x^2 + b x + c == 0
squareEqSolutions :: (Double, Double, Double) -> [Double]
squareEqSolutions (a,b,c) | a == 0 = if b /= 0 then [-c / (2*b)] else []
| d > 0 = [parabolaTop + intersectionOffset, parabolaTop - intersectionOffset]
| d == 0 = [parabolaTop]
| d < 0 = []
| otherwise = error $ "d is NaN in a square equation"
where
d = b^2 - 4*a*c
intersectionOffset = sqrt d / (2*a)
parabolaTop = -b / (2*a)