diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/lie.hs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/lie.hs b/examples/lie.hs new file mode 100644 index 0000000..87f7ed1 --- /dev/null +++ b/examples/lie.hs | |||
@@ -0,0 +1,66 @@ | |||
1 | -- The magic of Lie Algebra | ||
2 | |||
3 | import Numeric.LinearAlgebra | ||
4 | import Text.Printf(printf) | ||
5 | |||
6 | disp = putStrLn . format " " (printf "%.5f") | ||
7 | |||
8 | rot1 :: Double -> Matrix Double | ||
9 | rot1 a = (3><3) | ||
10 | [ 1, 0, 0 | ||
11 | , 0, c, s | ||
12 | , 0,-s, c ] | ||
13 | where c = cos a | ||
14 | s = sin a | ||
15 | |||
16 | g1,g2,g3 :: Matrix Double | ||
17 | |||
18 | g1 = (3><3) [0, 0,0 | ||
19 | ,0, 0,1 | ||
20 | ,0,-1,0] | ||
21 | |||
22 | rot2 :: Double -> Matrix Double | ||
23 | rot2 a = (3><3) | ||
24 | [ c, 0, s | ||
25 | , 0, 1, 0 | ||
26 | ,-s, 0, c ] | ||
27 | where c = cos a | ||
28 | s = sin a | ||
29 | |||
30 | g2 = (3><3) [ 0,0,1 | ||
31 | , 0,0,0 | ||
32 | ,-1,0,0] | ||
33 | |||
34 | rot3 :: Double -> Matrix Double | ||
35 | rot3 a = (3><3) | ||
36 | [ c, s, 0 | ||
37 | ,-s, c, 0 | ||
38 | , 0, 0, 1 ] | ||
39 | where c = cos a | ||
40 | s = sin a | ||
41 | |||
42 | g3 = (3><3) [ 0,1,0 | ||
43 | ,-1,0,0 | ||
44 | , 0,0,0] | ||
45 | |||
46 | deg=pi/180 | ||
47 | |||
48 | -- commutator | ||
49 | infix 8 & | ||
50 | a & b = a <> b - b <> a | ||
51 | |||
52 | infixl 6 |+| | ||
53 | a |+| b = a + b + a & b */2 + (a-b) & (a & b) */12 | ||
54 | |||
55 | main = do | ||
56 | let a = 45*deg | ||
57 | b = 50*deg | ||
58 | c = -30*deg | ||
59 | exact = rot3 a <> rot1 b <> rot2 c | ||
60 | lie = a.*g3 |+| b.*g1 |+| c.*g2 | ||
61 | putStrLn "position in the tangent space:" | ||
62 | disp lie | ||
63 | putStrLn "exponential map back to the group (2 terms):" | ||
64 | disp (expm lie) | ||
65 | putStrLn "exact position:" | ||
66 | disp exact | ||