diff options
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 15 |
1 files changed, 14 insertions, 1 deletions
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 | ||