diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed/Instances.hs | 391 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 18 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 6 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 35 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 13 |
5 files changed, 452 insertions, 11 deletions
diff --git a/lib/Data/Packed/Instances.hs b/lib/Data/Packed/Instances.hs new file mode 100644 index 0000000..4478469 --- /dev/null +++ b/lib/Data/Packed/Instances.hs | |||
@@ -0,0 +1,391 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Data.Packed.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 : uses -fffi and -fglasgow-exts | ||
11 | |||
12 | Creates 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. | ||
13 | |||
14 | -} | ||
15 | ----------------------------------------------------------------------------- | ||
16 | |||
17 | module Data.Packed.Instances( | ||
18 | Contractible(..) | ||
19 | ) where | ||
20 | |||
21 | import Data.Packed.Internal | ||
22 | import Data.Packed.Vector | ||
23 | import Data.Packed.Matrix | ||
24 | import GSL.Vector | ||
25 | import GSL.Matrix | ||
26 | import LinearAlgebra.Algorithms | ||
27 | import Complex | ||
28 | |||
29 | instance (Eq a, Field a) => Eq (Vector a) where | ||
30 | a == b = dim a == dim b && toList a == toList b | ||
31 | |||
32 | instance (Num a, Field a) => Num (Vector a) where | ||
33 | (+) = add | ||
34 | (-) = sub | ||
35 | (*) = mul | ||
36 | signum = liftVector signum | ||
37 | abs = liftVector abs | ||
38 | fromInteger = fromList . return . fromInteger | ||
39 | |||
40 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
41 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b | ||
42 | |||
43 | instance (Num a, Field a) => Num (Matrix a) where | ||
44 | (+) = liftMatrix2 add | ||
45 | (-) = liftMatrix2 sub | ||
46 | (*) = liftMatrix2 mul | ||
47 | signum = liftMatrix signum | ||
48 | abs = liftMatrix abs | ||
49 | fromInteger = (1><1) . return . fromInteger | ||
50 | |||
51 | --------------------------------------------------- | ||
52 | |||
53 | adaptScalar f1 f2 f3 x y | ||
54 | | dim x == 1 = f1 (x@>0) y | ||
55 | | dim y == 1 = f3 x (y@>0) | ||
56 | | otherwise = f2 x y | ||
57 | |||
58 | {- | ||
59 | subvv = vectorZip 4 | ||
60 | subvc v c = addConstant (-c) v | ||
61 | subcv c v = addConstant c (scale (-1) v) | ||
62 | |||
63 | mul = vectorZip 1 | ||
64 | |||
65 | instance Num (Vector Double) where | ||
66 | (+) = adaptScalar addConstant add (flip addConstant) | ||
67 | (-) = adaptScalar subcv subvv subvc | ||
68 | (*) = adaptScalar scale mul (flip scale) | ||
69 | abs = vectorMap 3 | ||
70 | signum = vectorMap 15 | ||
71 | fromInteger n = fromList [fromInteger n] | ||
72 | |||
73 | ---------------------------------------------------- | ||
74 | |||
75 | --addConstantC a = gmap (+a) | ||
76 | --subCvv u v = u `add` scale (-1) v | ||
77 | subCvv = vectorZipComplex 4 -- faster? | ||
78 | subCvc v c = addConstantC (-c) v | ||
79 | subCcv c v = addConstantC c (scale (-1) v) | ||
80 | |||
81 | |||
82 | instance Num (Vector (Complex Double)) where | ||
83 | (+) = adaptScalar addConstantC add (flip addConstantC) | ||
84 | (-) = adaptScalar subCcv subCvv subCvc | ||
85 | (*) = adaptScalar scale (vectorZipComplex 1) (flip scale) | ||
86 | abs = gmap abs | ||
87 | signum = gmap signum | ||
88 | fromInteger n = fromList [fromInteger n] | ||
89 | |||
90 | |||
91 | -- | adapts a function on two vectors to work on all the elements of two matrices | ||
92 | liftMatrix2' :: (Vector a -> Vector b -> Vector c) -> Matrix a -> Matrix b -> Matrix c | ||
93 | liftMatrix2' f m1@(M r1 c1 _) m2@(M r2 c2 _) | ||
94 | | sameShape m1 m2 || r1*c1==1 || r2*c2==1 | ||
95 | = reshape (max c1 c2) $ f (flatten m1) (flatten m2) | ||
96 | | otherwise = error "inconsistent matrix dimensions" | ||
97 | |||
98 | --------------------------------------------------- | ||
99 | |||
100 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
101 | a == b = rows a == rows b && cdat a == cdat b | ||
102 | |||
103 | instance Num (Matrix Double) where | ||
104 | (+) = liftMatrix2' (+) | ||
105 | (-) = liftMatrix2' (-) | ||
106 | (*) = liftMatrix2' (*) | ||
107 | abs = liftMatrix abs | ||
108 | signum = liftMatrix signum | ||
109 | fromInteger n = fromLists [[fromInteger n]] | ||
110 | |||
111 | ---------------------------------------------------- | ||
112 | |||
113 | instance Num (Matrix (Complex Double)) where | ||
114 | (+) = liftMatrix2' (+) | ||
115 | (-) = liftMatrix2' (-) | ||
116 | (*) = liftMatrix2' (*) | ||
117 | abs = liftMatrix abs | ||
118 | signum = liftMatrix signum | ||
119 | fromInteger n = fromLists [[fromInteger n]] | ||
120 | |||
121 | ------------------------------------------------------ | ||
122 | |||
123 | instance Fractional (Vector Double) where | ||
124 | fromRational n = fromList [fromRational n] | ||
125 | (/) = adaptScalar f (vectorZip 2) g where | ||
126 | r `f` v = vectorZip 2 (constant r (dim v)) v | ||
127 | v `g` r = scale (recip r) v | ||
128 | |||
129 | ------------------------------------------------------- | ||
130 | |||
131 | instance Fractional (Vector (Complex Double)) where | ||
132 | fromRational n = fromList [fromRational n] | ||
133 | (/) = adaptScalar f (vectorZipComplex 2) g where | ||
134 | r `f` v = gmap ((*r).recip) v | ||
135 | v `g` r = gmap (/r) v | ||
136 | |||
137 | ------------------------------------------------------ | ||
138 | |||
139 | instance Fractional (Matrix Double) where | ||
140 | fromRational n = fromLists [[fromRational n]] | ||
141 | (/) = liftMatrix2' (/) | ||
142 | |||
143 | ------------------------------------------------------- | ||
144 | |||
145 | instance Fractional (Matrix (Complex Double)) where | ||
146 | fromRational n = fromLists [[fromRational n]] | ||
147 | (/) = liftMatrix2' (/) | ||
148 | |||
149 | --------------------------------------------------------- | ||
150 | |||
151 | instance Floating (Vector Double) where | ||
152 | sin = vectorMap 0 | ||
153 | cos = vectorMap 1 | ||
154 | tan = vectorMap 2 | ||
155 | asin = vectorMap 4 | ||
156 | acos = vectorMap 5 | ||
157 | atan = vectorMap 6 | ||
158 | sinh = vectorMap 7 | ||
159 | cosh = vectorMap 8 | ||
160 | tanh = vectorMap 9 | ||
161 | asinh = vectorMap 10 | ||
162 | acosh = vectorMap 11 | ||
163 | atanh = vectorMap 12 | ||
164 | exp = vectorMap 13 | ||
165 | log = vectorMap 14 | ||
166 | sqrt = vectorMap 16 | ||
167 | (**) = adaptScalar f (vectorZip 5) g where f s v = constant s (dim v) ** v | ||
168 | g v s = v ** constant s (dim v) | ||
169 | pi = fromList [pi] | ||
170 | |||
171 | ----------------------------------------------------------- | ||
172 | |||
173 | instance Floating (Matrix Double) where | ||
174 | sin = liftMatrix sin | ||
175 | cos = liftMatrix cos | ||
176 | tan = liftMatrix tan | ||
177 | asin = liftMatrix asin | ||
178 | acos = liftMatrix acos | ||
179 | atan = liftMatrix atan | ||
180 | sinh = liftMatrix sinh | ||
181 | cosh = liftMatrix cosh | ||
182 | tanh = liftMatrix tanh | ||
183 | asinh = liftMatrix asinh | ||
184 | acosh = liftMatrix acosh | ||
185 | atanh = liftMatrix atanh | ||
186 | exp = liftMatrix exp | ||
187 | log = liftMatrix log | ||
188 | sqrt = liftMatrix sqrt | ||
189 | (**) = liftMatrix2 (**) | ||
190 | pi = fromLists [[pi]] | ||
191 | |||
192 | ------------------------------------------------------------- | ||
193 | |||
194 | instance Floating (Vector (Complex Double)) where | ||
195 | sin = vectorMapComplex 0 | ||
196 | cos = vectorMapComplex 1 | ||
197 | tan = vectorMapComplex 2 | ||
198 | asin = vectorMapComplex 4 | ||
199 | acos = vectorMapComplex 5 | ||
200 | atan = vectorMapComplex 6 | ||
201 | sinh = vectorMapComplex 7 | ||
202 | cosh = vectorMapComplex 8 | ||
203 | tanh = vectorMapComplex 9 | ||
204 | asinh = vectorMapComplex 10 | ||
205 | acosh = vectorMapComplex 11 | ||
206 | atanh = vectorMapComplex 12 | ||
207 | exp = vectorMapComplex 13 | ||
208 | log = vectorMapComplex 14 | ||
209 | sqrt = vectorMapComplex 16 | ||
210 | (**) = adaptScalar f (vectorZipComplex 5) g where f s v = constantC s (dim v) ** v | ||
211 | g v s = v ** constantC s (dim v) | ||
212 | pi = fromList [pi] | ||
213 | |||
214 | --------------------------------------------------------------- | ||
215 | |||
216 | instance Floating (Matrix (Complex Double)) where | ||
217 | sin = liftMatrix sin | ||
218 | cos = liftMatrix cos | ||
219 | tan = liftMatrix tan | ||
220 | asin = liftMatrix asin | ||
221 | acos = liftMatrix acos | ||
222 | atan = liftMatrix atan | ||
223 | sinh = liftMatrix sinh | ||
224 | cosh = liftMatrix cosh | ||
225 | tanh = liftMatrix tanh | ||
226 | asinh = liftMatrix asinh | ||
227 | acosh = liftMatrix acosh | ||
228 | atanh = liftMatrix atanh | ||
229 | exp = liftMatrix exp | ||
230 | log = liftMatrix log | ||
231 | (**) = liftMatrix2 (**) | ||
232 | sqrt = liftMatrix sqrt | ||
233 | pi = fromLists [[pi]] | ||
234 | |||
235 | --------------------------------------------------------------- | ||
236 | -} | ||
237 | |||
238 | class Contractible a b c | a b -> c where | ||
239 | infixl 7 <> | ||
240 | {- | An overloaded operator for matrix products, matrix-vector and vector-matrix products, dot products and scaling of vectors and matrices. Type consistency is statically checked. Alternatively, you can use the specific functions described below, but using this operator you can automatically combine real and complex objects. | ||
241 | |||
242 | @v = 'fromList' [1,2,3] :: Vector Double | ||
243 | cv = 'fromList' [1+'i',2] | ||
244 | m = 'fromLists' [[1,2,3], | ||
245 | [4,5,7]] :: Matrix Double | ||
246 | cm = 'fromLists' [[ 1, 2], | ||
247 | [3+'i',7*'i'], | ||
248 | [ 'i', 1]] | ||
249 | \ | ||
250 | \> m \<\> v | ||
251 | 14. 35. | ||
252 | \ | ||
253 | \> cv \<\> m | ||
254 | 9.+1.i 12.+2.i 17.+3.i | ||
255 | \ | ||
256 | \> m \<\> cm | ||
257 | 7.+5.i 5.+14.i | ||
258 | 19.+12.i 15.+35.i | ||
259 | \ | ||
260 | \> v \<\> 'i' | ||
261 | 1.i 2.i 3.i | ||
262 | \ | ||
263 | \> v \<\> v | ||
264 | 14.0 | ||
265 | \ | ||
266 | \> cv \<\> cv | ||
267 | 4.0 :+ 2.0@ | ||
268 | |||
269 | -} | ||
270 | (<>) :: a -> b -> c | ||
271 | |||
272 | |||
273 | instance Contractible Double Double Double where | ||
274 | (<>) = (*) | ||
275 | |||
276 | instance Contractible Double (Complex Double) (Complex Double) where | ||
277 | a <> b = (a:+0) * b | ||
278 | |||
279 | instance Contractible (Complex Double) Double (Complex Double) where | ||
280 | a <> b = a * (b:+0) | ||
281 | |||
282 | instance Contractible (Complex Double) (Complex Double) (Complex Double) where | ||
283 | (<>) = (*) | ||
284 | |||
285 | --------------------------------- matrix matrix | ||
286 | |||
287 | instance Contractible (Matrix Double) (Matrix Double) (Matrix Double) where | ||
288 | (<>) = mXm | ||
289 | |||
290 | instance Contractible (Matrix (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
291 | (<>) = mXm | ||
292 | |||
293 | instance Contractible (Matrix (Complex Double)) (Matrix Double) (Matrix (Complex Double)) where | ||
294 | c <> r = c <> liftMatrix comp r | ||
295 | |||
296 | instance Contractible (Matrix Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
297 | r <> c = liftMatrix comp r <> c | ||
298 | |||
299 | --------------------------------- (Matrix Double) (Vector Double) | ||
300 | |||
301 | instance Contractible (Matrix Double) (Vector Double) (Vector Double) where | ||
302 | (<>) = mXv | ||
303 | |||
304 | instance Contractible (Matrix (Complex Double)) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
305 | (<>) = mXv | ||
306 | |||
307 | instance Contractible (Matrix (Complex Double)) (Vector Double) (Vector (Complex Double)) where | ||
308 | m <> v = m <> comp v | ||
309 | |||
310 | instance Contractible (Matrix Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
311 | m <> v = liftMatrix comp m <> v | ||
312 | |||
313 | --------------------------------- (Vector Double) (Matrix Double) | ||
314 | |||
315 | instance Contractible (Vector Double) (Matrix Double) (Vector Double) where | ||
316 | (<>) = vXm | ||
317 | |||
318 | instance Contractible (Vector (Complex Double)) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
319 | (<>) = vXm | ||
320 | |||
321 | instance Contractible (Vector (Complex Double)) (Matrix Double) (Vector (Complex Double)) where | ||
322 | v <> m = v <> liftMatrix comp m | ||
323 | |||
324 | instance Contractible (Vector Double) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
325 | v <> m = comp v <> m | ||
326 | |||
327 | --------------------------------- dot product | ||
328 | |||
329 | instance Contractible (Vector Double) (Vector Double) Double where | ||
330 | (<>) = dot | ||
331 | |||
332 | instance Contractible (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where | ||
333 | (<>) = dot | ||
334 | |||
335 | instance Contractible (Vector Double) (Vector (Complex Double)) (Complex Double) where | ||
336 | a <> b = comp a <> b | ||
337 | |||
338 | instance Contractible (Vector (Complex Double)) (Vector Double) (Complex Double) where | ||
339 | (<>) = flip (<>) | ||
340 | |||
341 | --------------------------------- scaling vectors | ||
342 | |||
343 | instance Contractible Double (Vector Double) (Vector Double) where | ||
344 | (<>) = scale | ||
345 | |||
346 | instance Contractible (Vector Double) Double (Vector Double) where | ||
347 | (<>) = flip (<>) | ||
348 | |||
349 | instance Contractible (Complex Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
350 | (<>) = scale | ||
351 | |||
352 | instance Contractible (Vector (Complex Double)) (Complex Double) (Vector (Complex Double)) where | ||
353 | (<>) = flip (<>) | ||
354 | |||
355 | instance Contractible Double (Vector (Complex Double)) (Vector (Complex Double)) where | ||
356 | a <> v = (a:+0) <> v | ||
357 | |||
358 | instance Contractible (Vector (Complex Double)) Double (Vector (Complex Double)) where | ||
359 | (<>) = flip (<>) | ||
360 | |||
361 | instance Contractible (Complex Double) (Vector Double) (Vector (Complex Double)) where | ||
362 | a <> v = a <> comp v | ||
363 | |||
364 | instance Contractible (Vector Double) (Complex Double) (Vector (Complex Double)) where | ||
365 | (<>) = flip (<>) | ||
366 | |||
367 | --------------------------------- scaling matrices | ||
368 | |||
369 | instance Contractible Double (Matrix Double) (Matrix Double) where | ||
370 | (<>) a = liftMatrix (a <>) | ||
371 | |||
372 | instance Contractible (Matrix Double) Double (Matrix Double) where | ||
373 | (<>) = flip (<>) | ||
374 | |||
375 | instance Contractible (Complex Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
376 | (<>) a = liftMatrix (a <>) | ||
377 | |||
378 | instance Contractible (Matrix (Complex Double)) (Complex Double) (Matrix (Complex Double)) where | ||
379 | (<>) = flip (<>) | ||
380 | |||
381 | instance Contractible Double (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
382 | a <> m = (a:+0) <> m | ||
383 | |||
384 | instance Contractible (Matrix (Complex Double)) Double (Matrix (Complex Double)) where | ||
385 | (<>) = flip (<>) | ||
386 | |||
387 | instance Contractible (Complex Double) (Matrix Double) (Matrix (Complex Double)) where | ||
388 | a <> m = a <> liftMatrix comp m | ||
389 | |||
390 | instance Contractible (Matrix Double) (Complex Double) (Matrix (Complex Double)) where | ||
391 | (<>) = flip (<>) | ||
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 9309d1d..dd33943 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -93,6 +93,15 @@ createMatrix order r c = do | |||
93 | p <- createVector (r*c) | 93 | p <- createVector (r*c) |
94 | return (matrixFromVector order c p) | 94 | return (matrixFromVector order c p) |
95 | 95 | ||
96 | {- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. | ||
97 | |||
98 | @\> reshape 4 ('fromList' [1..12]) | ||
99 | (3><4) | ||
100 | [ 1.0, 2.0, 3.0, 4.0 | ||
101 | , 5.0, 6.0, 7.0, 8.0 | ||
102 | , 9.0, 10.0, 11.0, 12.0 ]@ | ||
103 | |||
104 | -} | ||
96 | reshape :: (Field t) => Int -> Vector t -> Matrix t | 105 | reshape :: (Field t) => Int -> Vector t -> Matrix t |
97 | reshape c v = matrixFromVector RowMajor c v | 106 | reshape c v = matrixFromVector RowMajor c v |
98 | 107 | ||
@@ -140,7 +149,6 @@ liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes | |||
140 | 149 | ||
141 | liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | 150 | liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t |
142 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes | 151 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes |
143 | |||
144 | ------------------------------------------------------------------ | 152 | ------------------------------------------------------------------ |
145 | 153 | ||
146 | dotL a b = sum (zipWith (*) a b) | 154 | dotL a b = sum (zipWith (*) a b) |
@@ -200,6 +208,14 @@ multiplyD order a b | |||
200 | 208 | ||
201 | outer' u v = dat (outer u v) | 209 | outer' u v = dat (outer u v) |
202 | 210 | ||
211 | {- | Outer product of two vectors. | ||
212 | |||
213 | @\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3] | ||
214 | (3><3) | ||
215 | [ 5.0, 2.0, 3.0 | ||
216 | , 10.0, 4.0, 6.0 | ||
217 | , 15.0, 6.0, 9.0 ]@ | ||
218 | -} | ||
203 | outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t | 219 | outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t |
204 | outer u v = multiply RowMajor r c | 220 | outer u v = multiply RowMajor r c |
205 | where r = matrixFromVector RowMajor 1 u | 221 | where r = matrixFromVector RowMajor 1 u |
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 25e848d..f1addf4 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -48,7 +48,7 @@ fromList l = unsafePerformIO $ do | |||
48 | toList :: Storable a => Vector a -> [a] | 48 | toList :: Storable a => Vector a -> [a] |
49 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) | 49 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) |
50 | 50 | ||
51 | n # l = if length l == n then fromList l else error "# with wrong size" | 51 | n |> l = if length l == n then fromList l else error "|> with wrong size" |
52 | 52 | ||
53 | at' :: Storable a => Vector a -> Int -> a | 53 | at' :: Storable a => Vector a -> Int -> a |
54 | at' v n = unsafePerformIO $ peekElemOff (ptr v) n | 54 | at' v n = unsafePerformIO $ peekElemOff (ptr v) n |
@@ -58,7 +58,7 @@ at v n | n >= 0 && n < dim v = at' v n | |||
58 | | otherwise = error "vector index out of range" | 58 | | otherwise = error "vector index out of range" |
59 | 59 | ||
60 | instance (Show a, Storable a) => (Show (Vector a)) where | 60 | instance (Show a, Storable a) => (Show (Vector a)) where |
61 | show v = (show (dim v))++" # " ++ show (toList v) | 61 | show v = (show (dim v))++" |> " ++ show (toList v) |
62 | 62 | ||
63 | -- | creates a Vector taking a number of consecutive toList from another Vector | 63 | -- | creates a Vector taking a number of consecutive toList from another Vector |
64 | subVector :: Storable t => Int -- ^ index of the starting element | 64 | subVector :: Storable t => Int -- ^ index of the starting element |
@@ -129,3 +129,5 @@ constant x n | isReal id x = scast $ constantR (scast x) n | |||
129 | | isComp id x = scast $ constantC (scast x) n | 129 | | isComp id x = scast $ constantC (scast x) n |
130 | | otherwise = constantG x n | 130 | | otherwise = constantG x n |
131 | 131 | ||
132 | liftVector f = fromList . map f . toList | ||
133 | liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) | ||
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 0f9d998..36bf32e 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -13,11 +13,11 @@ | |||
13 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
14 | 14 | ||
15 | module Data.Packed.Matrix ( | 15 | module Data.Packed.Matrix ( |
16 | Matrix(rows,cols), Field, | 16 | Matrix(rows,cols), |
17 | fromLists, toLists, (><), (>|<), (@@>), | 17 | fromLists, toLists, (><), (>|<), (@@>), |
18 | trans, conjTrans, | 18 | trans, conjTrans, |
19 | reshape, flatten, | 19 | reshape, flatten, asRow, asColumn, |
20 | fromRows, toRows, fromColumns, toColumns, | 20 | fromRows, toRows, fromColumns, toColumns, fromBlocks, |
21 | joinVert, joinHoriz, | 21 | joinVert, joinHoriz, |
22 | flipud, fliprl, | 22 | flipud, fliprl, |
23 | liftMatrix, liftMatrix2, | 23 | liftMatrix, liftMatrix2, |
@@ -43,6 +43,22 @@ joinVert ms = case common cols ms of | |||
43 | joinHoriz :: Field t => [Matrix t] -> Matrix t | 43 | joinHoriz :: Field t => [Matrix t] -> Matrix t |
44 | joinHoriz ms = trans. joinVert . map trans $ ms | 44 | joinHoriz ms = trans. joinVert . map trans $ ms |
45 | 45 | ||
46 | {- | Creates a matrix from blocks given as a list of lists of matrices: | ||
47 | |||
48 | @\> let a = 'diag' $ 'fromList' [5,7,2] | ||
49 | \> let b = 'reshape' 4 $ 'constant' (-1) 12 | ||
50 | \> fromBlocks [[a,b],[b,a]] | ||
51 | (6><7) | ||
52 | [ 5.0, 0.0, 0.0, -1.0, -1.0, -1.0, -1.0 | ||
53 | , 0.0, 7.0, 0.0, -1.0, -1.0, -1.0, -1.0 | ||
54 | , 0.0, 0.0, 2.0, -1.0, -1.0, -1.0, -1.0 | ||
55 | , -1.0, -1.0, -1.0, -1.0, 5.0, 0.0, 0.0 | ||
56 | , -1.0, -1.0, -1.0, -1.0, 0.0, 7.0, 0.0 | ||
57 | , -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 2.0 ]@ | ||
58 | -} | ||
59 | fromBlocks :: Field t => [[Matrix t]] -> Matrix t | ||
60 | fromBlocks = joinVert . map joinHoriz | ||
61 | |||
46 | -- | Reverse rows | 62 | -- | Reverse rows |
47 | flipud :: Field t => Matrix t -> Matrix t | 63 | flipud :: Field t => Matrix t -> Matrix t |
48 | flipud m = fromRows . reverse . toRows $ m | 64 | flipud m = fromRows . reverse . toRows $ m |
@@ -98,6 +114,11 @@ dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat | |||
98 | 114 | ||
99 | ---------------------------------------------------------------- | 115 | ---------------------------------------------------------------- |
100 | 116 | ||
117 | {- | Creates a vector by concatenation of rows | ||
118 | |||
119 | @\> flatten ('ident' 3) | ||
120 | 9 # [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@ | ||
121 | -} | ||
101 | flatten :: Matrix t -> Vector t | 122 | flatten :: Matrix t -> Vector t |
102 | flatten = cdat | 123 | flatten = cdat |
103 | 124 | ||
@@ -106,4 +127,10 @@ fromLists :: Field t => [[t]] -> Matrix t | |||
106 | fromLists = fromRows . map fromList | 127 | fromLists = fromRows . map fromList |
107 | 128 | ||
108 | conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double) | 129 | conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double) |
109 | conjTrans = trans . liftMatrix conj \ No newline at end of file | 130 | conjTrans = trans . liftMatrix conj |
131 | |||
132 | asRow :: Field a => Vector a -> Matrix a | ||
133 | asRow v = reshape (dim v) v | ||
134 | |||
135 | asColumn :: Field a => Vector a -> Matrix a | ||
136 | asColumn v = reshape 1 v | ||
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index 9d9d879..94f70be 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -15,7 +15,7 @@ | |||
15 | module Data.Packed.Vector ( | 15 | module Data.Packed.Vector ( |
16 | Vector(dim), Field, | 16 | Vector(dim), Field, |
17 | fromList, toList, | 17 | fromList, toList, |
18 | at, | 18 | (@>), |
19 | subVector, join, | 19 | subVector, join, |
20 | constant, | 20 | constant, |
21 | toComplex, comp, | 21 | toComplex, comp, |
@@ -26,6 +26,7 @@ module Data.Packed.Vector ( | |||
26 | 26 | ||
27 | import Data.Packed.Internal | 27 | import Data.Packed.Internal |
28 | import Complex | 28 | import Complex |
29 | import GSL.Vector | ||
29 | 30 | ||
30 | -- | creates a complex vector from vectors with real and imaginary parts | 31 | -- | creates a complex vector from vectors with real and imaginary parts |
31 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) | 32 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) |
@@ -41,10 +42,14 @@ comp v = toComplex (v,constant 0 (dim v)) | |||
41 | 42 | ||
42 | {- | Creates a real vector containing a range of values: | 43 | {- | Creates a real vector containing a range of values: |
43 | 44 | ||
44 | > > linspace 10 (-2,2) | 45 | @\> linspace 5 (-3,7) |
45 | >-2. -1.556 -1.111 -0.667 -0.222 0.222 0.667 1.111 1.556 2. | 46 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ |
46 | |||
47 | -} | 47 | -} |
48 | linspace :: Int -> (Double, Double) -> Vector Double | 48 | linspace :: Int -> (Double, Double) -> Vector Double |
49 | linspace n (a,b) = fromList [a::Double,a+delta .. b] | 49 | linspace n (a,b) = fromList [a::Double,a+delta .. b] |
50 | where delta = (b-a)/(fromIntegral n -1) | 50 | where delta = (b-a)/(fromIntegral n -1) |
51 | |||
52 | -- | Reads a vector position. | ||
53 | (@>) :: Field t => Vector t -> Int -> t | ||
54 | infixl 9 @> | ||
55 | (@>) = at | ||