diff options
Diffstat (limited to 'packages/base/src/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | packages/base/src/Data/Packed/Internal/Matrix.hs | 422 |
1 files changed, 422 insertions, 0 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Matrix.hs b/packages/base/src/Data/Packed/Internal/Matrix.hs new file mode 100644 index 0000000..9b831cc --- /dev/null +++ b/packages/base/src/Data/Packed/Internal/Matrix.hs | |||
@@ -0,0 +1,422 @@ | |||
1 | {-# LANGUAGE ForeignFunctionInterface #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | {-# LANGUAGE BangPatterns #-} | ||
5 | |||
6 | -- | | ||
7 | -- Module : Data.Packed.Internal.Matrix | ||
8 | -- Copyright : (c) Alberto Ruiz 2007 | ||
9 | -- License : BSD3 | ||
10 | -- Maintainer : Alberto Ruiz | ||
11 | -- Stability : provisional | ||
12 | -- | ||
13 | -- Internal matrix representation | ||
14 | -- | ||
15 | |||
16 | module Data.Packed.Internal.Matrix( | ||
17 | Matrix(..), rows, cols, cdat, fdat, | ||
18 | MatrixOrder(..), orderOf, | ||
19 | createMatrix, mat, | ||
20 | cmat, fmat, | ||
21 | toLists, flatten, reshape, | ||
22 | Element(..), | ||
23 | trans, | ||
24 | fromRows, toRows, fromColumns, toColumns, | ||
25 | matrixFromVector, | ||
26 | subMatrix, | ||
27 | liftMatrix, liftMatrix2, | ||
28 | (@@>), atM', | ||
29 | singleton, | ||
30 | emptyM, | ||
31 | size, shSize, conformVs, conformMs, conformVTo, conformMTo | ||
32 | ) where | ||
33 | |||
34 | import Data.Packed.Internal.Common | ||
35 | import Data.Packed.Internal.Signatures | ||
36 | import Data.Packed.Internal.Vector | ||
37 | |||
38 | import Foreign.Marshal.Alloc(alloca, free) | ||
39 | import Foreign.Marshal.Array(newArray) | ||
40 | import Foreign.Ptr(Ptr, castPtr) | ||
41 | import Foreign.Storable(Storable, peekElemOff, pokeElemOff, poke, sizeOf) | ||
42 | import Data.Complex(Complex) | ||
43 | import Foreign.C.Types | ||
44 | import System.IO.Unsafe(unsafePerformIO) | ||
45 | import Control.DeepSeq | ||
46 | |||
47 | ----------------------------------------------------------------- | ||
48 | |||
49 | {- Design considerations for the Matrix Type | ||
50 | ----------------------------------------- | ||
51 | |||
52 | - we must easily handle both row major and column major order, | ||
53 | for bindings to LAPACK and GSL/C | ||
54 | |||
55 | - we'd like to simplify redundant matrix transposes: | ||
56 | - Some of them arise from the order requirements of some functions | ||
57 | - some functions (matrix product) admit transposed arguments | ||
58 | |||
59 | - maybe we don't really need this kind of simplification: | ||
60 | - more complex code | ||
61 | - some computational overhead | ||
62 | - only appreciable gain in code with a lot of redundant transpositions | ||
63 | and cheap matrix computations | ||
64 | |||
65 | - we could carry both the matrix and its (lazily computed) transpose. | ||
66 | This may save some transpositions, but it is necessary to keep track of the | ||
67 | data which is actually computed to be used by functions like the matrix product | ||
68 | which admit both orders. | ||
69 | |||
70 | - but if we need the transposed data and it is not in the structure, we must make | ||
71 | sure that we touch the same foreignptr that is used in the computation. | ||
72 | |||
73 | - a reasonable solution is using two constructors for a matrix. Transposition just | ||
74 | "flips" the constructor. Actual data transposition is not done if followed by a | ||
75 | matrix product or another transpose. | ||
76 | |||
77 | -} | ||
78 | |||
79 | data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) | ||
80 | |||
81 | transOrder RowMajor = ColumnMajor | ||
82 | transOrder ColumnMajor = RowMajor | ||
83 | {- | Matrix representation suitable for GSL and LAPACK computations. | ||
84 | |||
85 | The elements are stored in a continuous memory array. | ||
86 | |||
87 | -} | ||
88 | |||
89 | data Matrix t = Matrix { irows :: {-# UNPACK #-} !Int | ||
90 | , icols :: {-# UNPACK #-} !Int | ||
91 | , xdat :: {-# UNPACK #-} !(Vector t) | ||
92 | , order :: !MatrixOrder } | ||
93 | -- RowMajor: preferred by C, fdat may require a transposition | ||
94 | -- ColumnMajor: preferred by LAPACK, cdat may require a transposition | ||
95 | |||
96 | cdat = xdat | ||
97 | fdat = xdat | ||
98 | |||
99 | rows :: Matrix t -> Int | ||
100 | rows = irows | ||
101 | |||
102 | cols :: Matrix t -> Int | ||
103 | cols = icols | ||
104 | |||
105 | orderOf :: Matrix t -> MatrixOrder | ||
106 | orderOf = order | ||
107 | |||
108 | |||
109 | -- | Matrix transpose. | ||
110 | trans :: Matrix t -> Matrix t | ||
111 | trans Matrix {irows = r, icols = c, xdat = d, order = o } = Matrix { irows = c, icols = r, xdat = d, order = transOrder o} | ||
112 | |||
113 | cmat :: (Element t) => Matrix t -> Matrix t | ||
114 | cmat m@Matrix{order = RowMajor} = m | ||
115 | cmat Matrix {irows = r, icols = c, xdat = d, order = ColumnMajor } = Matrix { irows = r, icols = c, xdat = transdata r d c, order = RowMajor} | ||
116 | |||
117 | fmat :: (Element t) => Matrix t -> Matrix t | ||
118 | fmat m@Matrix{order = ColumnMajor} = m | ||
119 | fmat Matrix {irows = r, icols = c, xdat = d, order = RowMajor } = Matrix { irows = r, icols = c, xdat = transdata c d r, order = ColumnMajor} | ||
120 | |||
121 | -- C-Haskell matrix adapter | ||
122 | -- mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r | ||
123 | |||
124 | mat :: (Storable t) => Matrix t -> (((CInt -> CInt -> Ptr t -> t1) -> t1) -> IO b) -> IO b | ||
125 | mat a f = | ||
126 | unsafeWith (xdat a) $ \p -> do | ||
127 | let m g = do | ||
128 | g (fi (rows a)) (fi (cols a)) p | ||
129 | f m | ||
130 | |||
131 | {- | Creates a vector by concatenation of rows. If the matrix is ColumnMajor, this operation requires a transpose. | ||
132 | |||
133 | >>> flatten (ident 3) | ||
134 | fromList [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0] | ||
135 | |||
136 | -} | ||
137 | flatten :: Element t => Matrix t -> Vector t | ||
138 | flatten = xdat . cmat | ||
139 | |||
140 | {- | ||
141 | type Mt t s = Int -> Int -> Ptr t -> s | ||
142 | |||
143 | infixr 6 ::> | ||
144 | type t ::> s = Mt t s | ||
145 | -} | ||
146 | |||
147 | -- | the inverse of 'Data.Packed.Matrix.fromLists' | ||
148 | toLists :: (Element t) => Matrix t -> [[t]] | ||
149 | toLists m = splitEvery (cols m) . toList . flatten $ m | ||
150 | |||
151 | -- | Create a matrix from a list of vectors. | ||
152 | -- All vectors must have the same dimension, | ||
153 | -- or dimension 1, which is are automatically expanded. | ||
154 | fromRows :: Element t => [Vector t] -> Matrix t | ||
155 | fromRows [] = emptyM 0 0 | ||
156 | fromRows vs = case compatdim (map dim vs) of | ||
157 | Nothing -> error $ "fromRows expects vectors with equal sizes (or singletons), given: " ++ show (map dim vs) | ||
158 | Just 0 -> emptyM r 0 | ||
159 | Just c -> matrixFromVector RowMajor r c . vjoin . map (adapt c) $ vs | ||
160 | where | ||
161 | r = length vs | ||
162 | adapt c v | ||
163 | | c == 0 = fromList[] | ||
164 | | dim v == c = v | ||
165 | | otherwise = constantD (v@>0) c | ||
166 | |||
167 | -- | extracts the rows of a matrix as a list of vectors | ||
168 | toRows :: Element t => Matrix t -> [Vector t] | ||
169 | toRows m | ||
170 | | c == 0 = replicate r (fromList[]) | ||
171 | | otherwise = toRows' 0 | ||
172 | where | ||
173 | v = flatten m | ||
174 | r = rows m | ||
175 | c = cols m | ||
176 | toRows' k | k == r*c = [] | ||
177 | | otherwise = subVector k c v : toRows' (k+c) | ||
178 | |||
179 | -- | Creates a matrix from a list of vectors, as columns | ||
180 | fromColumns :: Element t => [Vector t] -> Matrix t | ||
181 | fromColumns m = trans . fromRows $ m | ||
182 | |||
183 | -- | Creates a list of vectors from the columns of a matrix | ||
184 | toColumns :: Element t => Matrix t -> [Vector t] | ||
185 | toColumns m = toRows . trans $ m | ||
186 | |||
187 | -- | Reads a matrix position. | ||
188 | (@@>) :: Storable t => Matrix t -> (Int,Int) -> t | ||
189 | infixl 9 @@> | ||
190 | m@Matrix {irows = r, icols = c} @@> (i,j) | ||
191 | | safe = if i<0 || i>=r || j<0 || j>=c | ||
192 | then error "matrix indexing out of range" | ||
193 | else atM' m i j | ||
194 | | otherwise = atM' m i j | ||
195 | {-# INLINE (@@>) #-} | ||
196 | |||
197 | -- Unsafe matrix access without range checking | ||
198 | atM' Matrix {icols = c, xdat = v, order = RowMajor} i j = v `at'` (i*c+j) | ||
199 | atM' Matrix {irows = r, xdat = v, order = ColumnMajor} i j = v `at'` (j*r+i) | ||
200 | {-# INLINE atM' #-} | ||
201 | |||
202 | ------------------------------------------------------------------ | ||
203 | |||
204 | matrixFromVector o r c v | ||
205 | | r * c == dim v = m | ||
206 | | otherwise = error $ "can't reshape vector dim = "++ show (dim v)++" to matrix " ++ shSize m | ||
207 | where | ||
208 | m = Matrix { irows = r, icols = c, xdat = v, order = o } | ||
209 | |||
210 | -- allocates memory for a new matrix | ||
211 | createMatrix :: (Storable a) => MatrixOrder -> Int -> Int -> IO (Matrix a) | ||
212 | createMatrix ord r c = do | ||
213 | p <- createVector (r*c) | ||
214 | return (matrixFromVector ord r c p) | ||
215 | |||
216 | {- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. (GNU-Octave groups by columns. To do it you can define @reshapeF r = trans . reshape r@ | ||
217 | where r is the desired number of rows.) | ||
218 | |||
219 | >>> reshape 4 (fromList [1..12]) | ||
220 | (3><4) | ||
221 | [ 1.0, 2.0, 3.0, 4.0 | ||
222 | , 5.0, 6.0, 7.0, 8.0 | ||
223 | , 9.0, 10.0, 11.0, 12.0 ] | ||
224 | |||
225 | -} | ||
226 | reshape :: Storable t => Int -> Vector t -> Matrix t | ||
227 | reshape 0 v = matrixFromVector RowMajor 0 0 v | ||
228 | reshape c v = matrixFromVector RowMajor (dim v `div` c) c v | ||
229 | |||
230 | singleton x = reshape 1 (fromList [x]) | ||
231 | |||
232 | -- | application of a vector function on the flattened matrix elements | ||
233 | liftMatrix :: (Storable a, Storable b) => (Vector a -> Vector b) -> Matrix a -> Matrix b | ||
234 | liftMatrix f Matrix { irows = r, icols = c, xdat = d, order = o } = matrixFromVector o r c (f d) | ||
235 | |||
236 | -- | application of a vector function on the flattened matrices elements | ||
237 | liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
238 | liftMatrix2 f m1 m2 | ||
239 | | not (compat m1 m2) = error "nonconformant matrices in liftMatrix2" | ||
240 | | otherwise = case orderOf m1 of | ||
241 | RowMajor -> matrixFromVector RowMajor (rows m1) (cols m1) (f (xdat m1) (flatten m2)) | ||
242 | ColumnMajor -> matrixFromVector ColumnMajor (rows m1) (cols m1) (f (xdat m1) ((xdat.fmat) m2)) | ||
243 | |||
244 | |||
245 | compat :: Matrix a -> Matrix b -> Bool | ||
246 | compat m1 m2 = rows m1 == rows m2 && cols m1 == cols m2 | ||
247 | |||
248 | ------------------------------------------------------------------ | ||
249 | |||
250 | {- | Supported matrix elements. | ||
251 | |||
252 | This class provides optimized internal | ||
253 | operations for selected element types. | ||
254 | It provides unoptimised defaults for any 'Storable' type, | ||
255 | so you can create instances simply as: | ||
256 | @instance Element Foo@. | ||
257 | -} | ||
258 | class (Storable a) => Element a where | ||
259 | subMatrixD :: (Int,Int) -- ^ (r0,c0) starting position | ||
260 | -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix | ||
261 | -> Matrix a -> Matrix a | ||
262 | subMatrixD = subMatrix' | ||
263 | transdata :: Int -> Vector a -> Int -> Vector a | ||
264 | transdata = transdataP -- transdata' | ||
265 | constantD :: a -> Int -> Vector a | ||
266 | constantD = constantP -- constant' | ||
267 | |||
268 | |||
269 | instance Element Float where | ||
270 | transdata = transdataAux ctransF | ||
271 | constantD = constantAux cconstantF | ||
272 | |||
273 | instance Element Double where | ||
274 | transdata = transdataAux ctransR | ||
275 | constantD = constantAux cconstantR | ||
276 | |||
277 | instance Element (Complex Float) where | ||
278 | transdata = transdataAux ctransQ | ||
279 | constantD = constantAux cconstantQ | ||
280 | |||
281 | instance Element (Complex Double) where | ||
282 | transdata = transdataAux ctransC | ||
283 | constantD = constantAux cconstantC | ||
284 | |||
285 | ------------------------------------------------------------------- | ||
286 | |||
287 | transdataAux fun c1 d c2 = | ||
288 | if noneed | ||
289 | then d | ||
290 | else unsafePerformIO $ do | ||
291 | v <- createVector (dim d) | ||
292 | unsafeWith d $ \pd -> | ||
293 | unsafeWith v $ \pv -> | ||
294 | fun (fi r1) (fi c1) pd (fi r2) (fi c2) pv // check "transdataAux" | ||
295 | return v | ||
296 | where r1 = dim d `div` c1 | ||
297 | r2 = dim d `div` c2 | ||
298 | noneed = dim d == 0 || r1 == 1 || c1 == 1 | ||
299 | |||
300 | transdataP :: Storable a => Int -> Vector a -> Int -> Vector a | ||
301 | transdataP c1 d c2 = | ||
302 | if noneed | ||
303 | then d | ||
304 | else unsafePerformIO $ do | ||
305 | v <- createVector (dim d) | ||
306 | unsafeWith d $ \pd -> | ||
307 | unsafeWith v $ \pv -> | ||
308 | ctransP (fi r1) (fi c1) (castPtr pd) (fi sz) (fi r2) (fi c2) (castPtr pv) (fi sz) // check "transdataP" | ||
309 | return v | ||
310 | where r1 = dim d `div` c1 | ||
311 | r2 = dim d `div` c2 | ||
312 | sz = sizeOf (d @> 0) | ||
313 | noneed = dim d == 0 || r1 == 1 || c1 == 1 | ||
314 | |||
315 | foreign import ccall unsafe "transF" ctransF :: TFMFM | ||
316 | foreign import ccall unsafe "transR" ctransR :: TMM | ||
317 | foreign import ccall unsafe "transQ" ctransQ :: TQMQM | ||
318 | foreign import ccall unsafe "transC" ctransC :: TCMCM | ||
319 | foreign import ccall unsafe "transP" ctransP :: CInt -> CInt -> Ptr () -> CInt -> CInt -> CInt -> Ptr () -> CInt -> IO CInt | ||
320 | |||
321 | ---------------------------------------------------------------------- | ||
322 | |||
323 | constantAux fun x n = unsafePerformIO $ do | ||
324 | v <- createVector n | ||
325 | px <- newArray [x] | ||
326 | app1 (fun px) vec v "constantAux" | ||
327 | free px | ||
328 | return v | ||
329 | |||
330 | foreign import ccall unsafe "constantF" cconstantF :: Ptr Float -> TF | ||
331 | |||
332 | foreign import ccall unsafe "constantR" cconstantR :: Ptr Double -> TV | ||
333 | |||
334 | foreign import ccall unsafe "constantQ" cconstantQ :: Ptr (Complex Float) -> TQV | ||
335 | |||
336 | foreign import ccall unsafe "constantC" cconstantC :: Ptr (Complex Double) -> TCV | ||
337 | |||
338 | constantP :: Storable a => a -> Int -> Vector a | ||
339 | constantP a n = unsafePerformIO $ do | ||
340 | let sz = sizeOf a | ||
341 | v <- createVector n | ||
342 | unsafeWith v $ \p -> do | ||
343 | alloca $ \k -> do | ||
344 | poke k a | ||
345 | cconstantP (castPtr k) (fi n) (castPtr p) (fi sz) // check "constantP" | ||
346 | return v | ||
347 | foreign import ccall unsafe "constantP" cconstantP :: Ptr () -> CInt -> Ptr () -> CInt -> IO CInt | ||
348 | |||
349 | ---------------------------------------------------------------------- | ||
350 | |||
351 | -- | Extracts a submatrix from a matrix. | ||
352 | subMatrix :: Element a | ||
353 | => (Int,Int) -- ^ (r0,c0) starting position | ||
354 | -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix | ||
355 | -> Matrix a -- ^ input matrix | ||
356 | -> Matrix a -- ^ result | ||
357 | subMatrix (r0,c0) (rt,ct) m | ||
358 | | 0 <= r0 && 0 <= rt && r0+rt <= (rows m) && | ||
359 | 0 <= c0 && 0 <= ct && c0+ct <= (cols m) = subMatrixD (r0,c0) (rt,ct) m | ||
360 | | otherwise = error $ "wrong subMatrix "++ | ||
361 | show ((r0,c0),(rt,ct))++" of "++show(rows m)++"x"++ show (cols m) | ||
362 | |||
363 | subMatrix'' (r0,c0) (rt,ct) c v = unsafePerformIO $ do | ||
364 | w <- createVector (rt*ct) | ||
365 | unsafeWith v $ \p -> | ||
366 | unsafeWith w $ \q -> do | ||
367 | let go (-1) _ = return () | ||
368 | go !i (-1) = go (i-1) (ct-1) | ||
369 | go !i !j = do x <- peekElemOff p ((i+r0)*c+j+c0) | ||
370 | pokeElemOff q (i*ct+j) x | ||
371 | go i (j-1) | ||
372 | go (rt-1) (ct-1) | ||
373 | return w | ||
374 | |||
375 | subMatrix' (r0,c0) (rt,ct) (Matrix { icols = c, xdat = v, order = RowMajor}) = Matrix rt ct (subMatrix'' (r0,c0) (rt,ct) c v) RowMajor | ||
376 | subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m) | ||
377 | |||
378 | -------------------------------------------------------------------------- | ||
379 | |||
380 | maxZ xs = if minimum xs == 0 then 0 else maximum xs | ||
381 | |||
382 | conformMs ms = map (conformMTo (r,c)) ms | ||
383 | where | ||
384 | r = maxZ (map rows ms) | ||
385 | c = maxZ (map cols ms) | ||
386 | |||
387 | |||
388 | conformVs vs = map (conformVTo n) vs | ||
389 | where | ||
390 | n = maxZ (map dim vs) | ||
391 | |||
392 | conformMTo (r,c) m | ||
393 | | size m == (r,c) = m | ||
394 | | size m == (1,1) = matrixFromVector RowMajor r c (constantD (m@@>(0,0)) (r*c)) | ||
395 | | size m == (r,1) = repCols c m | ||
396 | | size m == (1,c) = repRows r m | ||
397 | | otherwise = error $ "matrix " ++ shSize m ++ " cannot be expanded to (" ++ show r ++ "><"++ show c ++")" | ||
398 | |||
399 | conformVTo n v | ||
400 | | dim v == n = v | ||
401 | | dim v == 1 = constantD (v@>0) n | ||
402 | | otherwise = error $ "vector of dim=" ++ show (dim v) ++ " cannot be expanded to dim=" ++ show n | ||
403 | |||
404 | repRows n x = fromRows (replicate n (flatten x)) | ||
405 | repCols n x = fromColumns (replicate n (flatten x)) | ||
406 | |||
407 | size m = (rows m, cols m) | ||
408 | |||
409 | shSize m = "(" ++ show (rows m) ++"><"++ show (cols m)++")" | ||
410 | |||
411 | emptyM r c = matrixFromVector RowMajor r c (fromList[]) | ||
412 | |||
413 | ---------------------------------------------------------------------- | ||
414 | |||
415 | instance (Storable t, NFData t) => NFData (Matrix t) | ||
416 | where | ||
417 | rnf m | d > 0 = rnf (v @> 0) | ||
418 | | otherwise = () | ||
419 | where | ||
420 | d = dim v | ||
421 | v = xdat m | ||
422 | |||