diff options
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 20 |
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 | ||
15 | module Data.Packed.Matrix ( | 15 | module 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 | ||
31 | import Data.Packed.Internal | 31 | import Data.Packed.Internal |
32 | import Foreign(Storable) | ||
33 | import Complex | ||
34 | import 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 |
34 | joinVert :: Field t => [Matrix t] -> Matrix t | 37 | joinVert :: Field t => [Matrix t] -> Matrix t |
@@ -50,6 +53,7 @@ fliprl m = fromColumns . reverse . toColumns $ m | |||
50 | 53 | ||
51 | ------------------------------------------------------------ | 54 | ------------------------------------------------------------ |
52 | 55 | ||
56 | diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t | ||
53 | diagRect s r c | 57 | diagRect 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 | ||
64 | takeDiag :: (Storable t) => Matrix t -> Vector t | ||
60 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] | 65 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] |
61 | 66 | ||
67 | ident :: (Num t, Field t) => Int -> Matrix t | ||
62 | ident n = diag (constant 1 n) | 68 | ident n = diag (constant 1 n) |
63 | 69 | ||
70 | (><) :: (Field a) => Int -> Int -> [a] -> Matrix a | ||
64 | r >< c = f where | 71 | r >< 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 | ||
70 | r >|< c = f where | 78 | r >|< 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 | ||
101 | flatten :: Matrix t -> Vector t | ||
93 | flatten = cdat | 102 | flatten = cdat |
103 | |||
104 | -- | Creates a 'Matrix' from a list of lists (considered as rows). | ||
105 | fromLists :: Field t => [[t]] -> Matrix t | ||
106 | fromLists = fromRows . map fromList | ||
107 | |||
108 | conjTrans :: Matrix (Complex Double) -> Matrix (Complex Double) | ||
109 | conjTrans = trans . liftMatrix conj \ No newline at end of file | ||