-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertTBandDA.hs
85 lines (70 loc) · 2.08 KB
/
ConvertTBandDA.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
{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module ConvertTBandDA (ConvertTBandDA, toDA, toTB)
where
import Prelude.Unicode
import DifferentialAlgebra (DA)
import qualified DifferentialAlgebra as DA (lift, primal)
import TangentBundle (TB)
import Numeric.Dual (Dual)
-- type ℝ = Double
-- This is not (==).
-- (⩵)∷Eq a⇒a→a→Bool
-- (⩵)=(≡)
-- Need to define converters DA to/from TB, in order to implement DG
-- via DA.
class (TB a a' ta, DA a da ba) ⇒ ConvertTBandDA a a' ta da ba
where
toDA ∷ ta → ba
toTB ∷ ba → ta
instance (DA a a (Dual a), TB a a (Dual a)) ⇒ ConvertTBandDA a a (Dual a) a (Dual a)
where
toDA = id
toTB = id
instance (ConvertTBandDA a a' ta da ba,
ConvertTBandDA b b' tb db bb)
⇒
ConvertTBandDA (a→b) (a→b') (a→tb) (ba→db) (ba→bb)
where
toTB f = toTB ∘ f ∘ DA.lift
toDA f = toDA ∘ f ∘ DA.primal -- *unsafe* unless DA tangent is zero
instance (ConvertTBandDA a a' ta da ba,
ConvertTBandDA b b' tb db bb)
⇒
ConvertTBandDA (a,b) (a',b') (ta,tb) (da,db) (ba,bb)
where
toTB (x,y) = (toTB x, toTB y)
toDA (x,y) = (toDA x, toDA y)
instance (ConvertTBandDA a a' ta da ba)
⇒
ConvertTBandDA [a] [a'] [ta] [da] [ba]
where
toTB = fmap toTB
toDA = fmap toDA
instance (ConvertTBandDA a a' ta da ba)
⇒
ConvertTBandDA (Maybe a) (Maybe a') (Maybe ta) (Maybe da) (Maybe ba)
where
toTB = fmap toTB
toDA = fmap toDA
instance (ConvertTBandDA a a' ta da ba,
ConvertTBandDA b b' tb db bb)
⇒
ConvertTBandDA (Either a b)
(Either a' b') (Either ta tb)
(Either da db) (Either ba bb)
where
toTB (Left da) = Left (toTB da)
toTB (Right db) = Right (toTB db)
toDA (Left ta) = Left (toDA ta)
toDA (Right tb) = Right (toDA tb)
instance ConvertTBandDA Bool () Bool () Bool
where
toTB = id
toDA = id
instance ConvertTBandDA () () () () ()
where
toTB = id
toDA = id