summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs72
1 files changed, 71 insertions, 1 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 8d1c8b6..6ea76c0 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -1 +1,71 @@
1 1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Matrix
4-- Copyright : (c) Alberto Ruiz 2007
5-- License : GPL-style
6--
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional
9-- Portability : portable
10--
11-- Matrices
12--
13-----------------------------------------------------------------------------
14
15module Data.Packed.Matrix (
16 Matrix(rows,cols), Field,
17 toLists, (><), (>|<),
18 trans,
19 reshape,
20 fromRows, toRows, fromColumns, toColumns,
21 joinVert, joinHoriz,
22 flipud, fliprl,
23 liftMatrix, liftMatrix2,
24 multiply,
25 subMatrix, diag, takeDiag, diagRect, ident
26) where
27
28import Data.Packed.Internal
29
30-- | creates a matrix from a vertical list of matrices
31joinVert :: Field t => [Matrix t] -> Matrix t
32joinVert ms = case common cols ms of
33 Nothing -> error "joinVert on matrices with different number of columns"
34 Just c -> reshape c $ join (map cdat ms)
35
36-- | creates a matrix from a horizontal list of matrices
37joinHoriz :: Field t => [Matrix t] -> Matrix t
38joinHoriz ms = trans. joinVert . map trans $ ms
39
40-- | Reverse rows
41flipud :: Field t => Matrix t -> Matrix t
42flipud m = fromRows . reverse . toRows $ m
43
44-- | Reverse columns
45fliprl :: Field t => Matrix t -> Matrix t
46fliprl m = fromColumns . reverse . toColumns $ m
47
48------------------------------------------------------------
49
50diagRect s r c
51 | dim s < min r c = error "diagRect"
52 | r == c = diag s
53 | r < c = trans $ diagRect s c r
54 | r > c = joinVert [diag s , zeros (r-c,c)]
55 where zeros (r,c) = reshape c $ constant (r*c) 0
56
57takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
58
59ident n = diag (constant n 1)
60
61r >< c = f where
62 f l | dim v == r*c = matrixFromVector RowMajor c v
63 | otherwise = error $ "inconsistent list size = "
64 ++show (dim v) ++"in ("++show r++"><"++show c++")"
65 where v = fromList l
66
67r >|< c = f where
68 f l | dim v == r*c = matrixFromVector ColumnMajor c v
69 | otherwise = error $ "inconsistent list size = "
70 ++show (dim v) ++"in ("++show r++"><"++show c++")"
71 where v = fromList l