summaryrefslogtreecommitdiff
path: root/lib/LinearAlgebra/Linear.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-15 17:55:50 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-15 17:55:50 +0000
commite0528e1a1e9ada67a39a0494f7dfccc2b6aefcad (patch)
tree7ee028012294a6d48b800c7d00d1e583833a7241 /lib/LinearAlgebra/Linear.hs
parentf901d49d1392327c79f1d4c63932fa350cfb506a (diff)
code refactoring
Diffstat (limited to 'lib/LinearAlgebra/Linear.hs')
-rw-r--r--lib/LinearAlgebra/Linear.hs29
1 files changed, 28 insertions, 1 deletions
diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs
index a2071ed..148bbd3 100644
--- a/lib/LinearAlgebra/Linear.hs
+++ b/lib/LinearAlgebra/Linear.hs
@@ -15,7 +15,8 @@ Portability : uses ffi
15 15
16module LinearAlgebra.Linear ( 16module LinearAlgebra.Linear (
17 Linear(..), 17 Linear(..),
18 multiply, dot, outer 18 dot, outer,
19 Mul(..)
19) where 20) where
20 21
21 22
@@ -27,10 +28,12 @@ import Complex
27 28
28class (Field e) => Linear c e where 29class (Field e) => Linear c e where
29 scale :: e -> c e -> c e 30 scale :: e -> c e -> c e
31 scaleRecip :: e -> c e -> c e
30 addConstant :: e -> c e -> c e 32 addConstant :: e -> c e -> c e
31 add :: c e -> c e -> c e 33 add :: c e -> c e -> c e
32 sub :: c e -> c e -> c e 34 sub :: c e -> c e -> c e
33 mul :: c e -> c e -> c e 35 mul :: c e -> c e -> c e
36 divide :: c e -> c e -> c e
34 toComplex :: RealFloat e => (c e, c e) -> c (Complex e) 37 toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
35 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) 38 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
36 comp :: RealFloat e => c e -> c (Complex e) 39 comp :: RealFloat e => c e -> c (Complex e)
@@ -38,10 +41,12 @@ class (Field e) => Linear c e where
38 41
39instance Linear Vector Double where 42instance Linear Vector Double where
40 scale = vectorMapValR Scale 43 scale = vectorMapValR Scale
44 scaleRecip = vectorMapValR Recip
41 addConstant = vectorMapValR AddConstant 45 addConstant = vectorMapValR AddConstant
42 add = vectorZipR Add 46 add = vectorZipR Add
43 sub = vectorZipR Sub 47 sub = vectorZipR Sub
44 mul = vectorZipR Mul 48 mul = vectorZipR Mul
49 divide = vectorZipR Div
45 toComplex = Data.Packed.Internal.toComplex 50 toComplex = Data.Packed.Internal.toComplex
46 fromComplex = Data.Packed.Internal.fromComplex 51 fromComplex = Data.Packed.Internal.fromComplex
47 comp = Data.Packed.Internal.comp 52 comp = Data.Packed.Internal.comp
@@ -49,10 +54,12 @@ instance Linear Vector Double where
49 54
50instance Linear Vector (Complex Double) where 55instance Linear Vector (Complex Double) where
51 scale = vectorMapValC Scale 56 scale = vectorMapValC Scale
57 scaleRecip = vectorMapValC Recip
52 addConstant = vectorMapValC AddConstant 58 addConstant = vectorMapValC AddConstant
53 add = vectorZipC Add 59 add = vectorZipC Add
54 sub = vectorZipC Sub 60 sub = vectorZipC Sub
55 mul = vectorZipC Mul 61 mul = vectorZipC Mul
62 divide = vectorZipC Div
56 toComplex = undefined -- can't match 63 toComplex = undefined -- can't match
57 fromComplex = undefined 64 fromComplex = undefined
58 comp = undefined 65 comp = undefined
@@ -60,10 +67,12 @@ instance Linear Vector (Complex Double) where
60 67
61instance Linear Matrix Double where 68instance Linear Matrix Double where
62 scale x = liftMatrix (scale x) 69 scale x = liftMatrix (scale x)
70 scaleRecip x = liftMatrix (scaleRecip x)
63 addConstant x = liftMatrix (addConstant x) 71 addConstant x = liftMatrix (addConstant x)
64 add = liftMatrix2 add 72 add = liftMatrix2 add
65 sub = liftMatrix2 sub 73 sub = liftMatrix2 sub
66 mul = liftMatrix2 mul 74 mul = liftMatrix2 mul
75 divide = liftMatrix2 divide
67 toComplex = uncurry $ liftMatrix2 $ curry LinearAlgebra.Linear.toComplex 76 toComplex = uncurry $ liftMatrix2 $ curry LinearAlgebra.Linear.toComplex
68 fromComplex z = (reshape c r, reshape c i) 77 fromComplex z = (reshape c r, reshape c i)
69 where (r,i) = LinearAlgebra.Linear.fromComplex (cdat z) 78 where (r,i) = LinearAlgebra.Linear.fromComplex (cdat z)
@@ -73,10 +82,12 @@ instance Linear Matrix Double where
73 82
74instance Linear Matrix (Complex Double) where 83instance Linear Matrix (Complex Double) where
75 scale x = liftMatrix (scale x) 84 scale x = liftMatrix (scale x)
85 scaleRecip x = liftMatrix (scaleRecip x)
76 addConstant x = liftMatrix (addConstant x) 86 addConstant x = liftMatrix (addConstant x)
77 add = liftMatrix2 add 87 add = liftMatrix2 add
78 sub = liftMatrix2 sub 88 sub = liftMatrix2 sub
79 mul = liftMatrix2 mul 89 mul = liftMatrix2 mul
90 divide = liftMatrix2 divide
80 toComplex = undefined 91 toComplex = undefined
81 fromComplex = undefined 92 fromComplex = undefined
82 comp = undefined 93 comp = undefined
@@ -102,3 +113,19 @@ dot u v = dat (multiply r c) `at` 0
102-} 113-}
103outer :: (Field t) => Vector t -> Vector t -> Matrix t 114outer :: (Field t) => Vector t -> Vector t -> Matrix t
104outer u v = asColumn u `multiply` asRow v 115outer u v = asColumn u `multiply` asRow v
116
117
118class Mul a b c | a b -> c where
119 infixl 7 <>
120 -- | matrix product
121 (<>) :: Field t => a t -> b t -> c t
122
123instance Mul Matrix Matrix Matrix where
124 (<>) = multiply
125
126instance Mul Matrix Vector Vector where
127 (<>) m v = flatten $ m <> (asColumn v)
128
129instance Mul Vector Matrix Vector where
130 (<>) v m = flatten $ (asRow v) <> m
131