diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-15 17:55:50 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-15 17:55:50 +0000 |
commit | e0528e1a1e9ada67a39a0494f7dfccc2b6aefcad (patch) | |
tree | 7ee028012294a6d48b800c7d00d1e583833a7241 | |
parent | f901d49d1392327c79f1d4c63932fa350cfb506a (diff) |
code refactoring
-rw-r--r-- | HSSL.cabal | 4 | ||||
-rw-r--r-- | examples/tests.hs | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 2 | ||||
-rw-r--r-- | lib/GSLHaskell.hs | 158 | ||||
-rw-r--r-- | lib/LinearAlgebra/Instances.hs | 140 | ||||
-rw-r--r-- | lib/LinearAlgebra/Linear.hs | 29 |
6 files changed, 177 insertions, 158 deletions
@@ -48,8 +48,10 @@ Exposed-modules: Data.Packed.Internal, | |||
48 | LAPACK, | 48 | LAPACK, |
49 | LinearAlgebra, | 49 | LinearAlgebra, |
50 | LinearAlgebra.Linear, | 50 | LinearAlgebra.Linear, |
51 | LinearAlgebra.Instances, | ||
51 | LinearAlgebra.Algorithms | 52 | LinearAlgebra.Algorithms |
52 | , GSLHaskell, Graphics.Plot | 53 | , Graphics.Plot |
54 | -- , GSLHaskell | ||
53 | Other-modules: | 55 | Other-modules: |
54 | C-sources: lib/Data/Packed/Internal/aux.c, | 56 | C-sources: lib/Data/Packed/Internal/aux.c, |
55 | lib/LAPACK/lapack-aux.c, | 57 | lib/LAPACK/lapack-aux.c, |
diff --git a/examples/tests.hs b/examples/tests.hs index 2f3b3d8..f352b71 100644 --- a/examples/tests.hs +++ b/examples/tests.hs | |||
@@ -20,7 +20,7 @@ import Test.QuickCheck | |||
20 | import Test.HUnit hiding ((~:)) | 20 | import Test.HUnit hiding ((~:)) |
21 | import Complex | 21 | import Complex |
22 | import LinearAlgebra.Algorithms | 22 | import LinearAlgebra.Algorithms |
23 | import LinearAlgebra.Linear | 23 | import LinearAlgebra.Linear hiding ((<>)) |
24 | import GSL.Matrix | 24 | import GSL.Matrix |
25 | import GSLHaskell hiding ((<>),constant) | 25 | import GSLHaskell hiding ((<>),constant) |
26 | 26 | ||
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 8bca510..951cec6 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -172,7 +172,7 @@ compat m1 m2 = rows m1 == rows m2 && cols m1 == cols m2 | |||
172 | ---------------------------------------------------------------- | 172 | ---------------------------------------------------------------- |
173 | 173 | ||
174 | -- | Optimized matrix computations are provided for elements in the Field class. | 174 | -- | Optimized matrix computations are provided for elements in the Field class. |
175 | class (Storable a, Num a) => Field a where | 175 | class (Storable a, Floating a) => Field a where |
176 | constantD :: a -> Int -> Vector a | 176 | constantD :: a -> Int -> Vector a |
177 | transdata :: Int -> Vector a -> Int -> Vector a | 177 | transdata :: Int -> Vector a -> Int -> Vector a |
178 | multiplyD :: Matrix a -> Matrix a -> Matrix a | 178 | multiplyD :: Matrix a -> Matrix a -> Matrix a |
diff --git a/lib/GSLHaskell.hs b/lib/GSLHaskell.hs index 3158458..254a957 100644 --- a/lib/GSLHaskell.hs +++ b/lib/GSLHaskell.hs | |||
@@ -9,7 +9,7 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) | |||
9 | Stability : provisional | 9 | Stability : provisional |
10 | Portability : uses -fffi and -fglasgow-exts | 10 | Portability : uses -fffi and -fglasgow-exts |
11 | 11 | ||
12 | GSLHaskell interface, with reasonable numeric instances for Vectors and Matrices. In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. | 12 | Old GSLHaskell interface. |
13 | 13 | ||
14 | -} | 14 | -} |
15 | ----------------------------------------------------------------------------- | 15 | ----------------------------------------------------------------------------- |
@@ -46,7 +46,6 @@ import GSL.Special(setErrorHandlerOff, | |||
46 | bessel_J0_e, | 46 | bessel_J0_e, |
47 | exp_e10_e, | 47 | exp_e10_e, |
48 | gamma) | 48 | gamma) |
49 | --import Data.Packed.Internal hiding (dsp,comp) | ||
50 | import Data.Packed.Vector | 49 | import Data.Packed.Vector |
51 | import Data.Packed.Matrix | 50 | import Data.Packed.Matrix |
52 | import Data.Packed.Matrix hiding ((><)) | 51 | import Data.Packed.Matrix hiding ((><)) |
@@ -55,163 +54,14 @@ import qualified LinearAlgebra.Algorithms | |||
55 | import LAPACK | 54 | import LAPACK |
56 | import GSL.Matrix | 55 | import GSL.Matrix |
57 | import LinearAlgebra.Algorithms hiding (pnorm) | 56 | import LinearAlgebra.Algorithms hiding (pnorm) |
58 | import LinearAlgebra.Linear | 57 | import LinearAlgebra.Linear hiding (Mul,(<>)) |
58 | import Data.Packed.Internal.Matrix(multiply) | ||
59 | import Complex | 59 | import Complex |
60 | import Numeric(showGFloat) | 60 | import Numeric(showGFloat) |
61 | import Data.List(transpose,intersperse) | 61 | import Data.List(transpose,intersperse) |
62 | import Foreign(Storable) | 62 | import Foreign(Storable) |
63 | import Data.Array | 63 | import Data.Array |
64 | 64 | import LinearAlgebra.Instances | |
65 | |||
66 | adaptScalar f1 f2 f3 x y | ||
67 | | dim x == 1 = f1 (x@>0) y | ||
68 | | dim y == 1 = f3 x (y@>0) | ||
69 | | otherwise = f2 x y | ||
70 | |||
71 | liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
72 | liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2)) | ||
73 | | otherwise = error "nonconformant matrices in liftMatrix2'" | ||
74 | |||
75 | compat' :: Matrix a -> Matrix b -> Bool | ||
76 | compat' m1 m2 = rows m1 == 1 && cols m1 == 1 | ||
77 | || rows m2 == 1 && cols m2 == 1 | ||
78 | || rows m1 == rows m2 && cols m1 == cols m2 | ||
79 | |||
80 | instance (Eq a, Field a) => Eq (Vector a) where | ||
81 | a == b = dim a == dim b && toList a == toList b | ||
82 | |||
83 | instance (Linear Vector a) => Num (Vector a) where | ||
84 | (+) = adaptScalar addConstant add (flip addConstant) | ||
85 | negate = scale (-1) | ||
86 | (*) = adaptScalar scale mul (flip scale) | ||
87 | signum = liftVector signum | ||
88 | abs = liftVector abs | ||
89 | fromInteger = fromList . return . fromInteger | ||
90 | |||
91 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
92 | a == b = cols a == cols b && flatten a == flatten b | ||
93 | |||
94 | instance (Field a, Linear Vector a) => Num (Matrix a) where | ||
95 | (+) = liftMatrix2' (+) | ||
96 | (-) = liftMatrix2' (-) | ||
97 | negate = liftMatrix negate | ||
98 | (*) = liftMatrix2' (*) | ||
99 | signum = liftMatrix signum | ||
100 | abs = liftMatrix abs | ||
101 | fromInteger = (1><1) . return . fromInteger | ||
102 | |||
103 | --------------------------------------------------- | ||
104 | |||
105 | instance Fractional (Vector Double) where | ||
106 | fromRational n = fromList [fromRational n] | ||
107 | (/) = adaptScalar f (vectorZipR Div) g where | ||
108 | r `f` v = vectorMapValR Recip r v | ||
109 | v `g` r = scale (recip r) v | ||
110 | |||
111 | ------------------------------------------------------- | ||
112 | |||
113 | instance Fractional (Vector (Complex Double)) where | ||
114 | fromRational n = fromList [fromRational n] | ||
115 | (/) = adaptScalar f (vectorZipC Div) g where | ||
116 | r `f` v = vectorMapValC Recip r v | ||
117 | v `g` r = scale (recip r) v | ||
118 | |||
119 | ------------------------------------------------------ | ||
120 | |||
121 | instance Fractional (Matrix Double) where | ||
122 | fromRational n = (1><1) [fromRational n] | ||
123 | (/) = liftMatrix2' (/) | ||
124 | |||
125 | ------------------------------------------------------- | ||
126 | |||
127 | instance Fractional (Matrix (Complex Double)) where | ||
128 | fromRational n = (1><1) [fromRational n] | ||
129 | (/) = liftMatrix2' (/) | ||
130 | |||
131 | --------------------------------------------------------- | ||
132 | |||
133 | instance Floating (Vector Double) where | ||
134 | sin = vectorMapR Sin | ||
135 | cos = vectorMapR Cos | ||
136 | tan = vectorMapR Tan | ||
137 | asin = vectorMapR ASin | ||
138 | acos = vectorMapR ACos | ||
139 | atan = vectorMapR ATan | ||
140 | sinh = vectorMapR Sinh | ||
141 | cosh = vectorMapR Cosh | ||
142 | tanh = vectorMapR Tanh | ||
143 | asinh = vectorMapR ASinh | ||
144 | acosh = vectorMapR ACosh | ||
145 | atanh = vectorMapR ATanh | ||
146 | exp = vectorMapR Exp | ||
147 | log = vectorMapR Log | ||
148 | sqrt = vectorMapR Sqrt | ||
149 | (**) = adaptScalar (vectorMapValR PowSV) (vectorZipR Pow) (flip (vectorMapValR PowVS)) | ||
150 | pi = fromList [pi] | ||
151 | |||
152 | ----------------------------------------------------------- | ||
153 | |||
154 | instance Floating (Matrix Double) where | ||
155 | sin = liftMatrix sin | ||
156 | cos = liftMatrix cos | ||
157 | tan = liftMatrix tan | ||
158 | asin = liftMatrix asin | ||
159 | acos = liftMatrix acos | ||
160 | atan = liftMatrix atan | ||
161 | sinh = liftMatrix sinh | ||
162 | cosh = liftMatrix cosh | ||
163 | tanh = liftMatrix tanh | ||
164 | asinh = liftMatrix asinh | ||
165 | acosh = liftMatrix acosh | ||
166 | atanh = liftMatrix atanh | ||
167 | exp = liftMatrix exp | ||
168 | log = liftMatrix log | ||
169 | (**) = liftMatrix2' (**) | ||
170 | sqrt = liftMatrix sqrt | ||
171 | pi = (1><1) [pi] | ||
172 | ------------------------------------------------------------- | ||
173 | |||
174 | instance Floating (Vector (Complex Double)) where | ||
175 | sin = vectorMapC Sin | ||
176 | cos = vectorMapC Cos | ||
177 | tan = vectorMapC Tan | ||
178 | asin = vectorMapC ASin | ||
179 | acos = vectorMapC ACos | ||
180 | atan = vectorMapC ATan | ||
181 | sinh = vectorMapC Sinh | ||
182 | cosh = vectorMapC Cosh | ||
183 | tanh = vectorMapC Tanh | ||
184 | asinh = vectorMapC ASinh | ||
185 | acosh = vectorMapC ACosh | ||
186 | atanh = vectorMapC ATanh | ||
187 | exp = vectorMapC Exp | ||
188 | log = vectorMapC Log | ||
189 | sqrt = vectorMapC Sqrt | ||
190 | (**) = adaptScalar (vectorMapValC PowSV) (vectorZipC Pow) (flip (vectorMapValC PowVS)) | ||
191 | pi = fromList [pi] | ||
192 | |||
193 | --------------------------------------------------------------- | ||
194 | |||
195 | instance Floating (Matrix (Complex Double)) where | ||
196 | sin = liftMatrix sin | ||
197 | cos = liftMatrix cos | ||
198 | tan = liftMatrix tan | ||
199 | asin = liftMatrix asin | ||
200 | acos = liftMatrix acos | ||
201 | atan = liftMatrix atan | ||
202 | sinh = liftMatrix sinh | ||
203 | cosh = liftMatrix cosh | ||
204 | tanh = liftMatrix tanh | ||
205 | asinh = liftMatrix asinh | ||
206 | acosh = liftMatrix acosh | ||
207 | atanh = liftMatrix atanh | ||
208 | exp = liftMatrix exp | ||
209 | log = liftMatrix log | ||
210 | (**) = liftMatrix2' (**) | ||
211 | sqrt = liftMatrix sqrt | ||
212 | pi = (1><1) [pi] | ||
213 | |||
214 | --------------------------------------------------------------- | ||
215 | 65 | ||
216 | 66 | ||
217 | class Mul a b c | a b -> c where | 67 | class Mul a b c | a b -> c where |
diff --git a/lib/LinearAlgebra/Instances.hs b/lib/LinearAlgebra/Instances.hs new file mode 100644 index 0000000..3dbe5a7 --- /dev/null +++ b/lib/LinearAlgebra/Instances.hs | |||
@@ -0,0 +1,140 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : LinearAlgebra.Instances | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : portable | ||
11 | |||
12 | Numeric instances for Vector and Matrix. | ||
13 | |||
14 | In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. | ||
15 | |||
16 | -} | ||
17 | ----------------------------------------------------------------------------- | ||
18 | |||
19 | module LinearAlgebra.Instances( | ||
20 | ) where | ||
21 | |||
22 | import LinearAlgebra.Linear | ||
23 | import GSL.Vector | ||
24 | import Data.Packed.Matrix | ||
25 | import Data.Packed.Vector | ||
26 | import Complex | ||
27 | |||
28 | adaptScalar f1 f2 f3 x y | ||
29 | | dim x == 1 = f1 (x@>0) y | ||
30 | | dim y == 1 = f3 x (y@>0) | ||
31 | | otherwise = f2 x y | ||
32 | |||
33 | liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
34 | liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2)) | ||
35 | | otherwise = error "nonconformant matrices in liftMatrix2'" | ||
36 | |||
37 | compat' :: Matrix a -> Matrix b -> Bool | ||
38 | compat' m1 m2 = rows m1 == 1 && cols m1 == 1 | ||
39 | || rows m2 == 1 && cols m2 == 1 | ||
40 | || rows m1 == rows m2 && cols m1 == cols m2 | ||
41 | |||
42 | instance (Eq a, Field a) => Eq (Vector a) where | ||
43 | a == b = dim a == dim b && toList a == toList b | ||
44 | |||
45 | instance (Linear Vector a) => Num (Vector a) where | ||
46 | (+) = adaptScalar addConstant add (flip addConstant) | ||
47 | negate = scale (-1) | ||
48 | (*) = adaptScalar scale mul (flip scale) | ||
49 | signum = liftVector signum | ||
50 | abs = liftVector abs | ||
51 | fromInteger = fromList . return . fromInteger | ||
52 | |||
53 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
54 | a == b = cols a == cols b && flatten a == flatten b | ||
55 | |||
56 | instance (Linear Vector a) => Num (Matrix a) where | ||
57 | (+) = liftMatrix2' (+) | ||
58 | (-) = liftMatrix2' (-) | ||
59 | negate = liftMatrix negate | ||
60 | (*) = liftMatrix2' (*) | ||
61 | signum = liftMatrix signum | ||
62 | abs = liftMatrix abs | ||
63 | fromInteger = (1><1) . return . fromInteger | ||
64 | |||
65 | --------------------------------------------------- | ||
66 | |||
67 | instance (Linear Vector a) => Fractional (Vector a) where | ||
68 | fromRational n = fromList [fromRational n] | ||
69 | (/) = adaptScalar f divide g where | ||
70 | r `f` v = scaleRecip r v | ||
71 | v `g` r = scale (recip r) v | ||
72 | |||
73 | ------------------------------------------------------- | ||
74 | |||
75 | instance (Linear Vector a, Fractional (Vector a)) => Fractional (Matrix a) where | ||
76 | fromRational n = (1><1) [fromRational n] | ||
77 | (/) = liftMatrix2' (/) | ||
78 | |||
79 | --------------------------------------------------------- | ||
80 | |||
81 | instance Floating (Vector Double) where | ||
82 | sin = vectorMapR Sin | ||
83 | cos = vectorMapR Cos | ||
84 | tan = vectorMapR Tan | ||
85 | asin = vectorMapR ASin | ||
86 | acos = vectorMapR ACos | ||
87 | atan = vectorMapR ATan | ||
88 | sinh = vectorMapR Sinh | ||
89 | cosh = vectorMapR Cosh | ||
90 | tanh = vectorMapR Tanh | ||
91 | asinh = vectorMapR ASinh | ||
92 | acosh = vectorMapR ACosh | ||
93 | atanh = vectorMapR ATanh | ||
94 | exp = vectorMapR Exp | ||
95 | log = vectorMapR Log | ||
96 | sqrt = vectorMapR Sqrt | ||
97 | (**) = adaptScalar (vectorMapValR PowSV) (vectorZipR Pow) (flip (vectorMapValR PowVS)) | ||
98 | pi = fromList [pi] | ||
99 | |||
100 | ------------------------------------------------------------- | ||
101 | |||
102 | instance Floating (Vector (Complex Double)) where | ||
103 | sin = vectorMapC Sin | ||
104 | cos = vectorMapC Cos | ||
105 | tan = vectorMapC Tan | ||
106 | asin = vectorMapC ASin | ||
107 | acos = vectorMapC ACos | ||
108 | atan = vectorMapC ATan | ||
109 | sinh = vectorMapC Sinh | ||
110 | cosh = vectorMapC Cosh | ||
111 | tanh = vectorMapC Tanh | ||
112 | asinh = vectorMapC ASinh | ||
113 | acosh = vectorMapC ACosh | ||
114 | atanh = vectorMapC ATanh | ||
115 | exp = vectorMapC Exp | ||
116 | log = vectorMapC Log | ||
117 | sqrt = vectorMapC Sqrt | ||
118 | (**) = adaptScalar (vectorMapValC PowSV) (vectorZipC Pow) (flip (vectorMapValC PowVS)) | ||
119 | pi = fromList [pi] | ||
120 | |||
121 | ----------------------------------------------------------- | ||
122 | |||
123 | instance (Linear Vector a, Floating (Vector a)) => Floating (Matrix a) where | ||
124 | sin = liftMatrix sin | ||
125 | cos = liftMatrix cos | ||
126 | tan = liftMatrix tan | ||
127 | asin = liftMatrix asin | ||
128 | acos = liftMatrix acos | ||
129 | atan = liftMatrix atan | ||
130 | sinh = liftMatrix sinh | ||
131 | cosh = liftMatrix cosh | ||
132 | tanh = liftMatrix tanh | ||
133 | asinh = liftMatrix asinh | ||
134 | acosh = liftMatrix acosh | ||
135 | atanh = liftMatrix atanh | ||
136 | exp = liftMatrix exp | ||
137 | log = liftMatrix log | ||
138 | (**) = liftMatrix2' (**) | ||
139 | sqrt = liftMatrix sqrt | ||
140 | pi = (1><1) [pi] | ||
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 | ||
16 | module LinearAlgebra.Linear ( | 16 | module 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 | ||
28 | class (Field e) => Linear c e where | 29 | class (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 | ||
39 | instance Linear Vector Double where | 42 | instance 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 | ||
50 | instance Linear Vector (Complex Double) where | 55 | instance 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 | ||
61 | instance Linear Matrix Double where | 68 | instance 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 | ||
74 | instance Linear Matrix (Complex Double) where | 83 | instance 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 | -} |
103 | outer :: (Field t) => Vector t -> Vector t -> Matrix t | 114 | outer :: (Field t) => Vector t -> Vector t -> Matrix t |
104 | outer u v = asColumn u `multiply` asRow v | 115 | outer u v = asColumn u `multiply` asRow v |
116 | |||
117 | |||
118 | class Mul a b c | a b -> c where | ||
119 | infixl 7 <> | ||
120 | -- | matrix product | ||
121 | (<>) :: Field t => a t -> b t -> c t | ||
122 | |||
123 | instance Mul Matrix Matrix Matrix where | ||
124 | (<>) = multiply | ||
125 | |||
126 | instance Mul Matrix Vector Vector where | ||
127 | (<>) m v = flatten $ m <> (asColumn v) | ||
128 | |||
129 | instance Mul Vector Matrix Vector where | ||
130 | (<>) v m = flatten $ (asRow v) <> m | ||
131 | |||