diff options
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 3 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 15 |
2 files changed, 16 insertions, 2 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 58b325c..8bca510 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -152,11 +152,12 @@ reshape c v = matrixFromVector RowMajor c v | |||
152 | 152 | ||
153 | singleton x = reshape 1 (fromList [x]) | 153 | singleton x = reshape 1 (fromList [x]) |
154 | 154 | ||
155 | -- | application of a vector function on the flattened matrix elements | ||
155 | liftMatrix :: (Field a, Field b) => (Vector a -> Vector b) -> Matrix a -> Matrix b | 156 | liftMatrix :: (Field a, Field b) => (Vector a -> Vector b) -> Matrix a -> Matrix b |
156 | liftMatrix f MC { cols = c, cdat = d } = matrixFromVector RowMajor c (f d) | 157 | liftMatrix f MC { cols = c, cdat = d } = matrixFromVector RowMajor c (f d) |
157 | liftMatrix f MF { cols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) | 158 | liftMatrix f MF { cols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) |
158 | 159 | ||
159 | 160 | -- | application of a vector function on the flattened matrices elements | |
160 | liftMatrix2 :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | 161 | liftMatrix2 :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t |
161 | liftMatrix2 f m1 m2 | 162 | liftMatrix2 f m1 m2 |
162 | | not (compat m1 m2) = error "nonconformant matrices in liftMatrix2" | 163 | | not (compat m1 m2) = error "nonconformant matrices in liftMatrix2" |
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index fc08ce4..4f2ad90 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -24,7 +24,7 @@ module Data.Packed.Matrix ( | |||
24 | fromBlocks, joinVert, joinHoriz, | 24 | fromBlocks, joinVert, joinHoriz, |
25 | flipud, fliprl, | 25 | flipud, fliprl, |
26 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, | 26 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, |
27 | diag, takeDiag, diagRect, ident, | 27 | ident, diag, diagRect, takeDiag, |
28 | liftMatrix, liftMatrix2, | 28 | liftMatrix, liftMatrix2, |
29 | ) where | 29 | ) where |
30 | 30 | ||
@@ -69,6 +69,14 @@ fliprl m = fromColumns . reverse . toColumns $ m | |||
69 | 69 | ||
70 | ------------------------------------------------------------ | 70 | ------------------------------------------------------------ |
71 | 71 | ||
72 | {- | creates a rectangular diagonal matrix | ||
73 | |||
74 | @> diagRect (constant 5 3) 3 4 | ||
75 | (3><4) | ||
76 | [ 5.0, 0.0, 0.0, 0.0 | ||
77 | , 0.0, 5.0, 0.0, 0.0 | ||
78 | , 0.0, 0.0, 5.0, 0.0 ]@ | ||
79 | -} | ||
72 | diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t | 80 | diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t |
73 | diagRect s r c | 81 | diagRect s r c |
74 | | dim s < min r c = error "diagRect" | 82 | | dim s < min r c = error "diagRect" |
@@ -77,9 +85,11 @@ diagRect s r c | |||
77 | | r > c = joinVert [diag s , zeros (r-c,c)] | 85 | | r > c = joinVert [diag s , zeros (r-c,c)] |
78 | where zeros (r,c) = reshape c $ constantD 0 (r*c) | 86 | where zeros (r,c) = reshape c $ constantD 0 (r*c) |
79 | 87 | ||
88 | -- | extracts the diagonal from a rectangular matrix | ||
80 | takeDiag :: (Field t) => Matrix t -> Vector t | 89 | takeDiag :: (Field t) => Matrix t -> Vector t |
81 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] | 90 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] |
82 | 91 | ||
92 | -- | creates the identity matrix of given dimension | ||
83 | ident :: Int -> Matrix Double | 93 | ident :: Int -> Matrix Double |
84 | ident n = diag (constant 1 n) | 94 | ident n = diag (constant 1 n) |
85 | 95 | ||
@@ -138,12 +148,15 @@ flatten = cdat | |||
138 | fromLists :: Field t => [[t]] -> Matrix t | 148 | fromLists :: Field t => [[t]] -> Matrix t |
139 | fromLists = fromRows . map fromList | 149 | fromLists = fromRows . map fromList |
140 | 150 | ||
151 | -- | conjugate transpose | ||
141 | conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double) | 152 | conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double) |
142 | conjTrans = trans . liftMatrix conj | 153 | conjTrans = trans . liftMatrix conj |
143 | 154 | ||
155 | -- | creates a 1-row matrix from a vector | ||
144 | asRow :: Field a => Vector a -> Matrix a | 156 | asRow :: Field a => Vector a -> Matrix a |
145 | asRow v = reshape (dim v) v | 157 | asRow v = reshape (dim v) v |
146 | 158 | ||
159 | -- | creates a 1-column matrix from a vector | ||
147 | asColumn :: Field a => Vector a -> Matrix a | 160 | asColumn :: Field a => Vector a -> Matrix a |
148 | asColumn v = reshape 1 v | 161 | asColumn v = reshape 1 v |
149 | 162 | ||