diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-29 17:57:57 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-29 17:57:57 +0000 |
commit | 04c76641caa9e1184fe504c584ddeb5420e994d6 (patch) | |
tree | c76d8742575c99ce9668a3cb8cb62f3441452720 | |
parent | e36c04dca536caa42b41a4280d3f21375219970d (diff) |
more work on tensors
-rw-r--r-- | examples/pru.hs | 149 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Tensor.hs | 57 |
2 files changed, 22 insertions, 184 deletions
diff --git a/examples/pru.hs b/examples/pru.hs deleted file mode 100644 index 71f33bf..0000000 --- a/examples/pru.hs +++ /dev/null | |||
@@ -1,149 +0,0 @@ | |||
1 | --{-# OPTIONS_GHC #-} | ||
2 | --module Main where | ||
3 | {- | ||
4 | import Data.Packed.Internal | ||
5 | import Data.Packed.Internal.Vector | ||
6 | import Data.Packed.Internal.Matrix | ||
7 | import Data.Packed.Tensor | ||
8 | import Data.Packed.Matrix | ||
9 | import GSL.Vector | ||
10 | import LAPACK | ||
11 | import Data.List(foldl') | ||
12 | |||
13 | import Complex | ||
14 | import Numeric(showGFloat) | ||
15 | |||
16 | import Foreign.Storable | ||
17 | -} | ||
18 | |||
19 | import GSL | ||
20 | import Data.List(foldl', foldl1') | ||
21 | import Data.Packed.Internal.Tensor | ||
22 | import Data.Packed.Tensor | ||
23 | import Complex | ||
24 | |||
25 | |||
26 | main = do | ||
27 | print $ foldl part t [("p",1),("q",0),("r",2)] | ||
28 | print $ foldl part t [("p",1),("r",2),("q",0)] | ||
29 | print $ foldl part t $ reverse [("p",1),("r",2),("q",0)] | ||
30 | pru | ||
31 | |||
32 | t = T [IdxDesc 4 Covariant "p",IdxDesc 2 Covariant "q" ,IdxDesc 3 Contravariant "r"] | ||
33 | $ fromList [1..24::Double] | ||
34 | |||
35 | |||
36 | t1 = T [IdxDesc 4 Covariant "p",IdxDesc 4 Contravariant "q" ,IdxDesc 2 Covariant "r"] | ||
37 | $ fromList [1..32::Double] | ||
38 | t2 = T [IdxDesc 4 Covariant "p",IdxDesc 4 Contravariant "q"] | ||
39 | $ fromList [1..16::Double] | ||
40 | |||
41 | vector n v = tvector n (fromList v) :: Tensor Double | ||
42 | |||
43 | kron n = tensorFromMatrix (Contravariant,"k1") (Covariant,"k2") (ident n) | ||
44 | |||
45 | tensorFromTrans m = tensorFromMatrix (Contravariant,"1") (Covariant,"2") m | ||
46 | |||
47 | tam = (3><3) [1..9] | ||
48 | tbm = (3><3) [11..19] | ||
49 | |||
50 | ta = tensorFromMatrix (Contravariant,"a1") (Covariant,"a2") tam :: Tensor Double | ||
51 | tb = tensorFromMatrix (Contravariant,"b1") (Covariant,"b2") tbm :: Tensor Double | ||
52 | |||
53 | delta i j | i==j = 1 | ||
54 | | otherwise = 0 | ||
55 | |||
56 | e i n = fromList [ delta k i | k <- [1..n]] | ||
57 | |||
58 | diagl = diag.fromList | ||
59 | |||
60 | td = tensorFromMatrix (Contravariant,"i") (Covariant,"j") $ diagl [1..4] :: Tensor Double | ||
61 | |||
62 | tn = tensorFromMatrix (Contravariant,"i") (Covariant,"j") $ (2><3) [1..6] :: Tensor Double | ||
63 | tt = tensorFromMatrix (Contravariant,"i") (Covariant,"j") $ (2><3) [1..6] :: Tensor Double | ||
64 | |||
65 | tq = T [IdxDesc 3 Covariant "p",IdxDesc 2 Covariant "q" ,IdxDesc 2 Covariant "r"] | ||
66 | $ fromList [11 .. 22] :: Tensor Double | ||
67 | |||
68 | r1 = contraction tt "j" tq "p" | ||
69 | r1' = contraction' tt "j" tq "p" | ||
70 | |||
71 | pru = do | ||
72 | mapM_ (putStrLn.shdims.dims.normal) (possibleContractions t1 t2) | ||
73 | let t1 = contraction tt "i" tq "q" | ||
74 | print $ normal t1 | ||
75 | print $ foldl part t1 [("j",0),("p'",1),("r'",1)] | ||
76 | let t2 = contraction' tt "i" tq "q" | ||
77 | print $ normal t2 | ||
78 | print $ foldl part t2 [("j",0),("p'",1),("r'",1)] | ||
79 | let t1 = contraction tq "q" tt "i" | ||
80 | print $ normal t1 | ||
81 | print $ foldl part t1 [("j'",0),("p",1),("r",1)] | ||
82 | let t2 = contraction' tq "q" tt "i" | ||
83 | print $ normal t2 | ||
84 | print $ foldl part t2 [("j'",0),("p",1),("r",1)] | ||
85 | putStrLn "--------------------------------------------" | ||
86 | print $ flatten $ tam <> tbm | ||
87 | print $ contractions (ta <*> tb <*> kron 3) [("a2","k1'"),("b1'","k2'")] | ||
88 | print $ contraction ta "a2" tb "b1" | ||
89 | print $ normal $ contractions (ta <*> tb) [("a2","b1'")] | ||
90 | print $ normal $ contraction' ta "a2" tb "b1" | ||
91 | putStrLn "--------------------------------------------" | ||
92 | print $ raise $ dualMV $ raise $ dualMV (x1/\x2) /\ dualV [x3,x4] | ||
93 | putStrLn "--------------------------------------------" | ||
94 | print $ foldl' contractionF (leviCivita 4) [y1,y2] | ||
95 | print $ contractions (leviCivita 4 <*> (y1/\y2)) [("1","p'"),("2'","q''")] <*> (scalar (recip $ fact 2)) | ||
96 | print $ foldl' contractionF (leviCivita 4) [y1,y2,y3,y5] | ||
97 | print $ contractions (leviCivita 4 <*> (y1/\y2/\y3/\y5)) [("1","p'"),("2'","q''"),("3'","r''"),("4'","t''")] <*> (scalar (recip $ fact 4)) | ||
98 | print $ dim $ ten $ leviCivita 4 <*> (y1/\y2/\y3/\y5) | ||
99 | print $ innerAT (leviCivita 4) (y1/\y2/\y3/\y5) | ||
100 | |||
101 | |||
102 | y5 = vector "t" [0,1,-2,0] | ||
103 | |||
104 | u = vector "p" [1,1,0] | ||
105 | v = vector "q" [0,1,1] | ||
106 | w = vector "r" [1,0,1] | ||
107 | |||
108 | uv = u /\ v | ||
109 | uw = u /\ w | ||
110 | |||
111 | |||
112 | l1 = vector "p" [0,0,0,1] | ||
113 | l2 = vector "q" [1,0,0,1] | ||
114 | l3 = vector "r" [0,1,0,1] | ||
115 | |||
116 | dual1 = foldl' contractionF (leviCivita 3) [u,v] | ||
117 | dual2 = foldl' contractionF (leviCivita 3) [u,v,w] | ||
118 | |||
119 | |||
120 | |||
121 | dual1' = prod (foldl' contract1b ((leviCivita 3) <*> (u /\ v)) [("1","p'"),("2'","q''")]) (scalar (recip $ fact 2)) | ||
122 | dual2' = prod (foldl' contract1b ((leviCivita 3) <*> (u /\ v /\ w)) [("1","p'"),("2'","q''"),("3'","r''")]) (scalar (recip $ fact 3)) | ||
123 | |||
124 | |||
125 | x1 = vector "p" [0,0,1] | ||
126 | x2 = vector "q" [2,2,2] | ||
127 | x3 = vector "r" [-3,-1,-1] | ||
128 | x4 = vector "s" [12,0,3] | ||
129 | |||
130 | |||
131 | -- intersection of two lines :-) | ||
132 | -- > raise $ dualMV $ raise $ dualMV (x1/\x2) /\ dualV [x3,x4] | ||
133 | --(3'^[3]) [24.0,24.0,12.0] | ||
134 | |||
135 | y1 = vector "p" [0,0,0,1] | ||
136 | y2 = vector "q" [2,2,0,2] | ||
137 | y3 = vector "r" [-3,-1,0,-1] | ||
138 | y4 = vector "s" [12,0,0,3] | ||
139 | |||
140 | -- why not in R^4? | ||
141 | -- > raise $ dualMV $ raise $ dualMV (y1/\y2) /\ dualV [y3,y4] | ||
142 | -- scalar 0.0 | ||
143 | -- it seems that the sum of ranks must be greater than n :( | ||
144 | |||
145 | |||
146 | z1 = vector "p" [0,0,0,1] | ||
147 | z2 = vector "q" [1,0,0,1] | ||
148 | z3 = vector "r" [0,1,0,1] | ||
149 | z4 = vector "s" [0,0,1,1] | ||
diff --git a/lib/Data/Packed/Internal/Tensor.hs b/lib/Data/Packed/Internal/Tensor.hs index dedbb9c..4430ebc 100644 --- a/lib/Data/Packed/Internal/Tensor.hs +++ b/lib/Data/Packed/Internal/Tensor.hs | |||
@@ -168,10 +168,7 @@ compatIdx t1 n1 t2 n2 = compatIdxAux d1 d2 where | |||
168 | d2 = head $ snd $ fst $ findIdx n2 t2 | 168 | d2 = head $ snd $ fst $ findIdx n2 t2 |
169 | 169 | ||
170 | names :: Tensor t -> [IdxName] | 170 | names :: Tensor t -> [IdxName] |
171 | names t = sort $ map idxName (dims t) | 171 | names t = map idxName (dims t) |
172 | |||
173 | normal :: (Field t) => Tensor t -> Tensor t | ||
174 | normal t = tridx (names t) t | ||
175 | 172 | ||
176 | 173 | ||
177 | -- sent to Haskell-Cafe by Sebastian Sylvan | 174 | -- sent to Haskell-Cafe by Sebastian Sylvan |
@@ -192,42 +189,34 @@ signature l | length (nub l) < length l = 0 | |||
192 | | otherwise = -1 | 189 | | otherwise = -1 |
193 | 190 | ||
194 | scalar x = T [] (fromList [x]) | 191 | scalar x = T [] (fromList [x]) |
195 | tensorFromVector (tp,nm) v = T {dims = [IdxDesc (dim v) tp nm] | 192 | tensorFromVector tp v = T {dims = [IdxDesc (dim v) tp "1"], ten = v} |
196 | , ten = v} | 193 | tensorFromMatrix tpr tpc m = T {dims = [IdxDesc (rows m) tpr "1",IdxDesc (cols m) tpc "2"] |
197 | tensorFromMatrix (tpr,nmr) (tpc,nmc) m = T {dims = [IdxDesc (rows m) tpr nmr,IdxDesc (cols m) tpc nmc] | 194 | , ten = cdat m} |
198 | , ten = cdat m} | ||
199 | |||
200 | tvector n v = tensorFromVector (Contravariant,n) v | ||
201 | tcovector n v = tensorFromVector (Covariant,n) v | ||
202 | |||
203 | |||
204 | antisym t = T (dims t) (ten (antisym' (auxrename t))) | ||
205 | where | ||
206 | scsig t = scalar (signature (nms t)) `rawProduct` t | ||
207 | where nms = map idxName . dims | ||
208 | 195 | ||
209 | antisym' t = addT $ map (scsig . flip tridx t) (perms (names t)) | 196 | tvector v = tensorFromVector Contravariant v |
210 | 197 | tcovector v = tensorFromVector Covariant v | |
211 | auxrename (T d v) = T d' v | ||
212 | where d' = [IdxDesc n c (show (pos q)) | IdxDesc n c q <- d] | ||
213 | pos n = i where Just i = elemIndex n nms | ||
214 | nms = map idxName d | ||
215 | 198 | ||
199 | antisym t = T (dims t) (ten (antisym' (withIdx t seqind))) | ||
200 | where antisym' t = addT $ map (scsig . flip tridx t) (perms (names t)) | ||
201 | scsig t = scalar (signature (nms t)) `rawProduct` t | ||
202 | where nms = map idxName . dims | ||
216 | 203 | ||
217 | norper t = rawProduct t (scalar (recip $ fromIntegral $ fact (rank t))) | 204 | norper t = rawProduct t (scalar (recip $ fromIntegral $ fact (rank t))) |
218 | antinorper t = rawProduct t (scalar (fromIntegral $ fact (rank t))) | 205 | antinorper t = rawProduct t (scalar (fromIntegral $ fact (rank t))) |
219 | 206 | ||
220 | wedge a b = antisym (rawProduct (norper a) (norper b)) | 207 | wedge a b = antisym (rawProduct (norper a) (norper b)) |
221 | 208 | ||
222 | a /\ b = wedge a b | ||
223 | |||
224 | normAT t = sqrt $ innerAT t t | 209 | normAT t = sqrt $ innerAT t t |
225 | 210 | ||
226 | innerAT t1 t2 = dot (ten t1) (ten t2) / fromIntegral (fact $ rank t1) | 211 | innerAT t1 t2 = dot (ten t1) (ten t2) / fromIntegral (fact $ rank t1) |
227 | 212 | ||
228 | fact n = product [1..n] | 213 | fact n = product [1..n] |
229 | 214 | ||
230 | leviCivita n = antisym $ foldl1 rawProduct $ zipWith tcovector (map show [1,2..]) (toRows (ident n)) | 215 | seqind' = map return seqind |
216 | seqind = map show [1..] | ||
217 | |||
218 | leviCivita n = antisym $ foldl1 rawProduct $ zipWith withIdx auxbase seqind' | ||
219 | where auxbase = map tcovector (toRows (ident n)) | ||
231 | 220 | ||
232 | -- | obtains de dual of the exterior product of a list of X? | 221 | -- | obtains de dual of the exterior product of a list of X? |
233 | dualV vs = foldl' contractionF (leviCivita n) vs | 222 | dualV vs = foldl' contractionF (leviCivita n) vs |
@@ -240,17 +229,15 @@ raise (T d v) = T (map raise' d) v | |||
240 | -- | raises or lowers all the indices of a tensor with a given an (inverse) metric | 229 | -- | raises or lowers all the indices of a tensor with a given an (inverse) metric |
241 | raiseWith = undefined | 230 | raiseWith = undefined |
242 | 231 | ||
232 | dualg f t = f (leviCivita n) `okContract` withIdx t seqind `rawProduct` x | ||
233 | where n = idxDim . head . dims $ t | ||
234 | x = scalar (recip $ fromIntegral $ fact (rank t)) | ||
235 | |||
243 | -- | obtains the dual of a multivector | 236 | -- | obtains the dual of a multivector |
244 | dualMV t = rawProduct (contractions lct ds) x | 237 | dual t = dualg id t |
245 | where | ||
246 | lc = leviCivita n | ||
247 | lct = rawProduct lc t | ||
248 | nms1 = map idxName (dims lc) | ||
249 | nms2 = map idxName (dims t) | ||
250 | ds = zip nms1 nms2 | ||
251 | n = idxDim . head . dims $ t | ||
252 | x = scalar (recip $ fromIntegral $ fact (rank t)) | ||
253 | 238 | ||
239 | -- | obtains the dual of a multicovector (with euclidean metric) | ||
240 | codual t = dualg raise t | ||
254 | 241 | ||
255 | -- | shows only the relevant components of an antisymmetric tensor | 242 | -- | shows only the relevant components of an antisymmetric tensor |
256 | niceAS t = filter ((/=0.0).fst) $ zip vals base | 243 | niceAS t = filter ((/=0.0).fst) $ zip vals base |