-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculus.hs
196 lines (153 loc) · 4.91 KB
/
Calculus.hs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module Calculus where
import Data.Maybe
data UnOp = Neg | Sin | Cos | Log
deriving (Eq, Ord, Show)
data BinOp = Add | Mul | Div
deriving (Eq, Ord, Show)
data Exp = Val Double | Id String | UnApp UnOp Exp | BinApp BinOp Exp Exp
deriving (Eq, Ord, Show)
type Env = [(String, Double)]
lookUp :: Eq a => a -> [(a, b)] -> b
lookUp a elems
= fromJust (lookup a elems)
eval :: Exp -> Env -> Double
eval (Val a) _
= a
eval (Id s) vals
= lookUp s vals
eval (UnApp op exp) vals
| op == Neg = (-) (0) (eval exp vals)
| otherwise = (findUnFunc op) (eval exp vals)
eval (BinApp op exp exp') vals
= (findBinFunc op) (eval exp vals) (eval exp' vals)
findUnFunc :: UnOp -> (Double -> Double)
findUnFunc op
= lookUp op unMapping
where
unMapping = [(Sin, sin),
(Cos, cos),
(Log, log)]
findBinFunc :: BinOp -> (Double -> Double -> Double)
findBinFunc op
= lookUp op binMapping
where
binMapping = [(Add, (+)),
(Mul, (*)),
(Div, (/))]
diff :: Exp -> String -> Exp
diff (Val a) _
= Val 0.0
diff (Id exp) s
| exp == s = Val 1.0
| otherwise = Val 0.0
diff (BinApp Add e e') s
= add (chain e s) (chain e' s)
diff (BinApp Mul e e') s
| e == Val 0.0 || e' == Val 0.0 = Val 0.0
| otherwise = add (mult (e) (chain e' s)) (mult (chain e s) (e'))
diff (BinApp Div e e') s
= divi (sub (mult (chain e s) (e')) (mult (e) (chain e' s))) (mult (e') (e'))
diff (UnApp Sin e) s
| s == show(e) = UnApp Cos e
| otherwise = chain (UnApp Sin e) s
diff (UnApp Cos e) s
| s == show(e) = UnApp Sin e
| otherwise = chain (UnApp Cos e) s
diff exp@(UnApp Log e) s
| s == show(e) = divi (Val 1.0) (e)
| otherwise = chain exp s
diff (UnApp Neg e) s
= UnApp Neg (diff e s)
chain :: Exp -> String -> Exp
chain exp@(UnApp (Neg) e) s
= diff exp s
chain (UnApp (Log) e) s
= divi (diff e s) (e)
chain exp@(UnApp (Cos) e) s
= UnApp Neg (mult (diff exp (show e)) (diff e s))
chain exp@(UnApp op e) s
= mult (diff exp (show e)) (diff e s)
chain exp s
= diff exp s
mult = BinApp Mul
divi = BinApp Div
add = BinApp Add
sub e e' = BinApp Add e (UnApp Neg e')
maclaurin :: Exp -> Double -> Int -> Double
maclaurin exp val n
= sumN n (map (flip eval [("x", val)]) (zipWith3 (createTerm) fs ds ps))
where
fs = scanl (*) (1) [1, 2 ..]
ds = iterate (flip diff "x") exp
ps = ((Val 1) : iterate (mult (Id "x")) (Id "x"))
createTerm f d p
= divi (mult (Val (eval d [("x", 0.0)])) (p)) (Val f)
sumN :: Int -> [Double] -> Double
sumN n xs = sum (take n xs)
showExp :: Exp -> String
showExp (Val a)
= show(a)
showExp (Id s)
= s
showExp (UnApp op exp)
= (findUnStr op) ++ (bracket (showExp exp))
showExp (BinApp op exp exp')
= bracket ((showExp exp) ++ (findBinStr op) ++ (showExp exp'))
bracket :: String -> String
bracket a = "(" ++ a ++ ")"
findUnStr :: UnOp -> String
findUnStr op
= lookUp op unMapping
where
unMapping = [(Sin, "sin"),
(Cos, "cos"),
(Log, "log"),
(Neg, "-")]
findBinStr :: BinOp -> String
findBinStr op
= lookUp op binMapping
where
binMapping = [(Add, "+"),
(Mul, "*"),
(Div, "/")]
---------------------------------------------------------------------------
-- Test cases from the spec.
e1, e2, e3, e4, e5, e6 :: Exp
-- > 5*x
e1 = BinApp Mul (Val 5.0) (Id "x")
-- > x*x + y - 7
e2 = BinApp Add (BinApp Add (BinApp Mul (Id "x") (Id "x")) (Id "y"))
(UnApp Neg (Val 7.0))
-- > x-y^2/(4*x*y-y^2)::Exp
e3 = BinApp Add (Id "x")
(UnApp Neg (BinApp Div (BinApp Mul (Id "y") (Id "y"))
(BinApp Add (BinApp Mul (BinApp Mul (Val 4.0) (Id "x")) (Id "y"))
(UnApp Neg (BinApp Mul (Id "y") (Id "y"))))))
-- > -cos x::Exp
e4 = UnApp Neg (UnApp Cos (Id "x"))
-- > sin (1+log(2*x))::Exp
e5 = UnApp Sin (BinApp Add (Val 1.0)
(UnApp Log (BinApp Mul (Val 2.0) (Id "x"))))
-- > log(3*x^2+2)::Exp
e6 = UnApp Log (BinApp Add (BinApp Mul (Val 3.0) (BinApp Mul (Id "x") (Id "x")))
(Val 2.0))
----------------------------------------------------------------------
-- EXTENSION: Uncomment and complete these...
instance Num Exp where
negate = UnApp Neg
(+) = BinApp Add
(*) = BinApp Mul
fromInteger x = Val (fromInteger x)
instance Fractional Exp where
(/) = BinApp Div
instance Floating Exp where
sin = UnApp Sin
cos = UnApp Cos
log = UnApp Log
-- instance (Eq a, Num a) => Num (Maybe a) where
-- instance (Eq a, Fractional a) => Fractional (Maybe a) where
-- diff2 :: Exp -> String -> Maybe Exp
-- The following makes it much easier to input expressions, e.g. sin x, log(x*x) etc.
x, y :: Exp
x = Id "x"
y = Id "y"