summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-23 20:21:03 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-23 20:21:03 +0000
commit3d5d6f06598aac00906c93ac5358e68697c47fc7 (patch)
tree77a76afcd561b8beee33c39b4eafe72b4fa10b86 /lib/Data/Packed/Matrix.hs
parent978e6d038239af50d70bae2c303f4e45b1879b7a (diff)
more refactoring
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index c7d5cfa..0f9d998 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -14,8 +14,8 @@
14 14
15module Data.Packed.Matrix ( 15module Data.Packed.Matrix (
16 Matrix(rows,cols), Field, 16 Matrix(rows,cols), Field,
17 toLists, (><), (>|<), (@@>), 17 fromLists, toLists, (><), (>|<), (@@>),
18 trans, 18 trans, conjTrans,
19 reshape, flatten, 19 reshape, flatten,
20 fromRows, toRows, fromColumns, toColumns, 20 fromRows, toRows, fromColumns, toColumns,
21 joinVert, joinHoriz, 21 joinVert, joinHoriz,
@@ -29,6 +29,9 @@ module Data.Packed.Matrix (
29) where 29) where
30 30
31import Data.Packed.Internal 31import Data.Packed.Internal
32import Foreign(Storable)
33import Complex
34import Data.Packed.Vector
32 35
33-- | creates a matrix from a vertical list of matrices 36-- | creates a matrix from a vertical list of matrices
34joinVert :: Field t => [Matrix t] -> Matrix t 37joinVert :: Field t => [Matrix t] -> Matrix t
@@ -50,6 +53,7 @@ fliprl m = fromColumns . reverse . toColumns $ m
50 53
51------------------------------------------------------------ 54------------------------------------------------------------
52 55
56diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t
53diagRect s r c 57diagRect s r c
54 | dim s < min r c = error "diagRect" 58 | dim s < min r c = error "diagRect"
55 | r == c = diag s 59 | r == c = diag s
@@ -57,16 +61,20 @@ diagRect s r c
57 | r > c = joinVert [diag s , zeros (r-c,c)] 61 | r > c = joinVert [diag s , zeros (r-c,c)]
58 where zeros (r,c) = reshape c $ constant 0 (r*c) 62 where zeros (r,c) = reshape c $ constant 0 (r*c)
59 63
64takeDiag :: (Storable t) => Matrix t -> Vector t
60takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] 65takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
61 66
67ident :: (Num t, Field t) => Int -> Matrix t
62ident n = diag (constant 1 n) 68ident n = diag (constant 1 n)
63 69
70(><) :: (Field a) => Int -> Int -> [a] -> Matrix a
64r >< c = f where 71r >< c = f where
65 f l | dim v == r*c = matrixFromVector RowMajor c v 72 f l | dim v == r*c = matrixFromVector RowMajor c v
66 | otherwise = error $ "inconsistent list size = " 73 | otherwise = error $ "inconsistent list size = "
67 ++show (dim v) ++"in ("++show r++"><"++show c++")" 74 ++show (dim v) ++"in ("++show r++"><"++show c++")"
68 where v = fromList l 75 where v = fromList l
69 76
77(>|<) :: (Field a) => Int -> Int -> [a] -> Matrix a
70r >|< c = f where 78r >|< c = f where
71 f l | dim v == r*c = matrixFromVector ColumnMajor c v 79 f l | dim v == r*c = matrixFromVector ColumnMajor c v
72 | otherwise = error $ "inconsistent list size = " 80 | otherwise = error $ "inconsistent list size = "
@@ -90,4 +98,12 @@ dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat
90 98
91---------------------------------------------------------------- 99----------------------------------------------------------------
92 100
101flatten :: Matrix t -> Vector t
93flatten = cdat 102flatten = cdat
103
104-- | Creates a 'Matrix' from a list of lists (considered as rows).
105fromLists :: Field t => [[t]] -> Matrix t
106fromLists = fromRows . map fromList
107
108conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double)
109conjTrans = trans . liftMatrix conj \ No newline at end of file