/
Dual.purs
397 lines (341 loc) · 9.88 KB
/
Dual.purs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
module Data.Number.Dual where
import Prelude hiding (negate, add, mul, recip, div)
import Prelude (negate) as Ring
import Prelude (recip) as DivisionRing
import Prelude (div) as EuclideanRing
import Data.Number (acos, asin, atan, cos, exp, log, sin, sqrt) as Math
import Data.Tuple (fst, snd, uncurry)
import Data.Tuple.Nested ((/\), type (/\))
import Prim.Int (class Add)
import Type.Proxy (Proxy(..))
newtype Dual a b = D (a -> b /\ (a -> b))
class Monoidal k where
cross ::
forall a b c d.
k a c ->
k b d ->
k (a /\ b) (c /\ d)
instance Monoidal (->) where
cross f g = \(a /\ b) -> f a /\ g b
instance Monoidal Dual where
cross (D f) (D g) = D $ \(a /\ b) ->
let
c /\ f' = f a
d /\ g' = g b
in
(c /\ d) /\ cross f' g'
linearPropagation ::
forall a b.
(a -> b) ->
(a -> b) ->
Dual a b
linearPropagation f f' = D $ \a -> f a /\ f'
instance Semigroupoid Dual where
compose (D g) (D f) = D $ \a ->
let
b /\ f' = f a
c /\ g' = g b
in
c /\ compose g' f'
instance Category Dual where
identity = linearPropagation identity identity
class Category k <= Cartesian k where
exl :: forall a b. k (a /\ b) a
exr :: forall a b. k (a /\ b) b
dup :: forall a. k a (a /\ a)
instance Cartesian (->) where
exl = fst
exr = snd
dup x = x /\ x
instance Cartesian Dual where
exl = linearPropagation exl exl
exr = linearPropagation exr exr
dup = linearPropagation dup dup
class Space a where
scale :: a -> (a -> a)
accum :: (a /\ a) -> a
instance Semiring s => Space s where
scale a = \da -> a * da
accum = \(a /\ b) -> a + b
class RingCat k s where
negate :: k s s
add :: k (s /\ s) s
mul :: k (s /\ s) s
instance Ring s => RingCat (->) s where
negate = Ring.negate
add = uncurry (+)
mul = uncurry (*)
instance Ring s => RingCat Dual s where
negate = linearPropagation negate negate
add = linearPropagation add add
mul = D $ \(a /\ b) ->
(a * b) /\ (accum <<< cross (scale b) (scale a))
class DivisionRingCat k s where
recip :: k s s
div :: k (s /\ s) s
instance (DivisionRing s, EuclideanRing s) => DivisionRingCat (->) s where
recip = DivisionRing.recip
div = uncurry EuclideanRing.div
recipImpl :: forall b. EuclideanRing b => DivisionRing b => Dual b b
recipImpl = D \x -> recip x /\ (_ * (-(recip $ x * x)))
instance
( DivisionRing s
, EuclideanRing s
, RingCat Dual s
) =>
DivisionRingCat Dual s where
recip = recipImpl
div = (exl .:. exr >>> recipImpl) >>> mul
expImpl :: Dual Number Number
expImpl = D \a -> Math.exp a /\ scale (Math.exp a)
lnImpl :: Dual Number Number
lnImpl = D \a -> Math.log a /\ scale (1.0 / a)
sinImpl :: Dual Number Number
sinImpl = D \a -> Math.sin a /\ scale (Math.cos a)
cosImpl :: Dual Number Number
cosImpl = D \a -> Math.cos a /\ scale (-Math.sin a)
sqrtImpl :: Dual Number Number
sqrtImpl = D \a -> Math.sqrt a /\ scale (0.5 / Math.sqrt a)
absImpl :: Dual Number Number
absImpl = dup >>> mul >>> sqrtImpl
atanImpl :: Dual Number Number
atanImpl = D \a -> Math.atan a /\ scale (1.0 / (1.0 + a * a))
coshImpl :: Dual Number Number
coshImpl = (dup >>> (exl >>> expImpl .:. exr >>> negate >>> expImpl) >>> add .:. cst 0.5) >>> mul
sinhImpl :: Dual Number Number
sinhImpl = (dup >>> (exl >>> expImpl .:. exr >>> negate >>> expImpl >>> negate) >>> add .:. cst 0.5) >>> mul
class NumCat k where
exp :: k Number Number
sqrt :: k Number Number
sin :: k Number Number
cos :: k Number Number
tan :: k Number Number
asin :: k Number Number
acos :: k Number Number
atan :: k Number Number
sinh :: k Number Number
cosh :: k Number Number
tanh :: k Number Number
asinh :: k Number Number
acosh :: k Number Number
atanh :: k Number Number
ln :: k Number Number
abs :: k Number Number
sign :: k Number Number
atan2 :: k (Number /\ Number) Number
pow :: k (Number /\ Number) Number
min :: k (Number /\ Number) Number
max :: k (Number /\ Number) Number
instance NumCat Dual where
exp = expImpl
ln = lnImpl
pow = (exl >>> lnImpl .:. exr) >>> mul >>> expImpl
sqrt = sqrtImpl
sin = sinImpl
cos = cosImpl
tan = dup >>> (exl >>> sinImpl .:. exr >>> cosImpl) >>> div
asin = D \a -> Math.asin a /\ scale (1.0 / Math.sqrt (1.0 - a * a))
acos = D \a -> Math.acos a /\ scale (-1.0 / Math.sqrt (1.0 - a * a))
atan = atanImpl
atan2 = div >>> atanImpl
abs = absImpl
sign = dup >>> (exl .:. exr >>> absImpl) >>> div
min = ((add .:. (exl .:. exr >>> negate) >>> add >>> absImpl >>> negate) >>> add .:. cst 0.5) >>> mul
max = ((add .:. (exl .:. exr >>> negate) >>> add >>> absImpl) >>> add .:. cst 0.5) >>> mul
cosh = coshImpl
sinh = sinhImpl
tanh = dup >>> (exl >>> sinhImpl .:. exr >>> coshImpl) >>> div
asinh = dup >>> (exl .:. (exr >>> dup >>> mul .:. cst 1.0) >>> add >>> sqrtImpl) >>> add >>> lnImpl
acosh = dup >>> (exl .:. (exr >>> dup >>> mul .:. cst (-1.0)) >>> add >>> sqrtImpl) >>> add >>> lnImpl
atanh =
( dup
>>>
( (cst 1.0 .:. exl) >>> add .:. (cst 1.0 .:. exr >>> negate) >>> add
)
>>> div
>>> lnImpl
.:. cst 0.5
) >>> mul
-- | Generalized pairing operator
-- | such that
-- | exl >>> f .:. exr >>> g
-- | is equivalent to
-- | cross f g
pair ::
forall a c d k.
Cartesian k =>
Monoidal k =>
k a c ->
k a d ->
k a (c /\ d)
pair f g = cross f g <<< dup
infixr 6 pair as .:.
cst :: forall a s. Semiring s => s -> Dual a s
cst n = D $ const $ n /\ const zero
axes :: forall @n a. Axes n a => a
axes = axesImpl @n Proxy
class Axes :: Int -> Type -> Constraint
class Axes n a | n -> a where
axesImpl :: Proxy n -> a
instance Axes 1 Number where
axesImpl _ = 1.0
else instance Axes 2 ((Number /\ Number) /\ (Number /\ Number)) where
axesImpl _ = (1.0 /\ 0.0) /\ (0.0 /\ 1.0)
else instance
( Axes n (h /\ t)
, Add n 1 n1
, Fmapable h (h /\ t) (Number /\ h) (h' /\ t')
, Fmapable Number h Number h
) =>
Axes n1 ((Number /\ h) /\ h' /\ t') where
axesImpl _ = h'' /\ h' /\ t'
where
h /\ t = axesImpl (Proxy :: Proxy n)
h' /\ t' =
let
D graph =
fmap (dup >>> (cst 0.0 .:. exr) :: Dual h (Number /\ h))
f /\ _ = graph (h /\ t)
in
f
zeros =
let
D graph =
fmap (cst 0.0 :: Dual Number Number)
f /\ _ = graph h
in
f
h'' = 1.0 /\ zeros
class Transposable a b | a -> b where
transpose :: Dual a b
instance
( Transposable ((b /\ x) /\ (d /\ y)) e
) =>
Transposable ((a /\ (b /\ x)) /\ (c /\ (d /\ y))) ((a /\ c) /\ e) where
transpose = cross exl exl .:. cross exr exr >>> transpose
else instance Transposable ((a /\ b) /\ (c /\ d)) ((a /\ c) /\ (b /\ d)) where
transpose = cross exl exl .:. cross exr exr
else instance Transposable a a where
transpose = identity
class Cumulative c a where
cumulate :: Dual c a
instance
( Ring a
, Cumulative c a
) =>
Cumulative (a /\ c) a where
cumulate = cross identity cumulate >>> add
else instance Ring a => Cumulative (a /\ a) a where
cumulate = add
else instance Cumulative a a where
cumulate = identity
class Fmapable a c b k | b c -> k where
fmap :: Dual a b -> Dual c k
instance Fmapable a a b b where
fmap f = f
else instance
( Fmapable a c b k
) =>
Fmapable a (a /\ c) b (b /\ k) where
fmap f = cross f (fmap f)
minimize ::
forall a c u t v axs.
Fmapable a axs Number v =>
Transposable (a /\ v) c =>
Fmapable (Number /\ Number) c Number a =>
Transposable (v /\ v) u =>
Fmapable (Number /\ Number) u Number t =>
Cumulative t Number =>
axs ->
Dual a Number ->
Number ->
Number ->
a ->
a
minimize axs (D cost) lambda epsilon z0 =
let
go z =
let
_ /\ f' = cost z
fz =
let
D graph = fmap (linearPropagation f' (const 0.0) :: Dual a Number)
f /\ _ = graph axs
in
f
comb xs k ys =
let
D graph =
transpose
>>>
fmap
( linearPropagation
(\(x /\ y) -> x + k * y)
(const 0.0) ::
Dual (Number /\ Number) Number
)
f /\ _ = graph $ xs /\ ys
in
f
_ /\ u' = cost $ comb z lambda fz
fu =
let
D graph = fmap (linearPropagation u' (const 0.0) :: Dual a Number)
f /\ _ = graph axs
in
f
scal xs ys =
let
D graph =
transpose
>>>
fmap
( linearPropagation
( \(x /\ y) -> x * y
)
(const 0.0) ::
Dual (Number /\ Number) Number
)
>>> cumulate
f /\ _ = graph $ xs /\ ys
in
f
in
if scal fz fz < epsilon then z
else
let
k = -scal fz fu / scal fu fu
in
go $ comb z k fz
in
go z0
distance2 ::
forall a b c t.
Transposable a b =>
Fmapable (Number /\ Number) b (Number /\ Number) c =>
Fmapable (Number /\ Number) c Number t =>
Cumulative t Number =>
Dual a Number
distance2 =
transpose
>>> fmap (cross identity negate :: Dual (Number /\ Number) (Number /\ Number))
>>> fmap (add >>> dup >>> mul :: Dual (Number /\ Number) Number)
>>> cumulate
norm2 ::
forall a s t u v.
Fmapable Number a Number s =>
Transposable (a /\ s) t =>
Fmapable (Number /\ Number) t (Number /\ Number) u =>
Fmapable (Number /\ Number) u Number v =>
Cumulative v Number =>
Dual a Number
norm2 =
dup >>> (exl .:. exr >>> fmap (cst 0.0 :: Dual Number Number)) >>> distance2
distance ::
forall a b c t.
Transposable a b =>
Fmapable (Number /\ Number) b (Number /\ Number) c =>
Fmapable (Number /\ Number) c Number (Number /\ t) =>
Cumulative t Number =>
Dual a Number
distance = distance2 >>> sqrt