diff options
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 7d2c564..c56bf3d 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -33,6 +33,7 @@ module Data.Packed.Matrix ( | |||
33 | ) where | 33 | ) where |
34 | 34 | ||
35 | import Data.Packed.Internal | 35 | import Data.Packed.Internal |
36 | import qualified Data.Packed.ST as ST | ||
36 | import Data.Packed.Vector | 37 | import Data.Packed.Vector |
37 | import Data.List(transpose,intersperse) | 38 | import Data.List(transpose,intersperse) |
38 | import Data.Array | 39 | import Data.Array |
@@ -73,6 +74,14 @@ fliprl m = fromColumns . reverse . toColumns $ m | |||
73 | 74 | ||
74 | ------------------------------------------------------------ | 75 | ------------------------------------------------------------ |
75 | 76 | ||
77 | -- | Creates a square matrix with a given diagonal. | ||
78 | diag :: Element a => Vector a -> Matrix a | ||
79 | diag v = ST.runSTMatrix $ do | ||
80 | let d = dim v | ||
81 | m <- ST.newMatrix 0 d d | ||
82 | mapM_ (\k -> ST.writeMatrix m k k (v@>k)) [0..d-1] | ||
83 | return m | ||
84 | |||
76 | {- | creates a rectangular diagonal matrix | 85 | {- | creates a rectangular diagonal matrix |
77 | 86 | ||
78 | @> diagRect (constant 5 3) 3 4 | 87 | @> diagRect (constant 5 3) 3 4 |
@@ -87,7 +96,7 @@ diagRect s r c | |||
87 | | r == c = diag s | 96 | | r == c = diag s |
88 | | r < c = trans $ diagRect s c r | 97 | | r < c = trans $ diagRect s c r |
89 | | otherwise = joinVert [diag s , zeros (r-c,c)] | 98 | | otherwise = joinVert [diag s , zeros (r-c,c)] |
90 | where zeros (r',c') = reshape c' $ constantD 0 (r'*c') | 99 | where zeros (r',c') = reshape c' $ constant 0 (r'*c') |
91 | 100 | ||
92 | -- | extracts the diagonal from a rectangular matrix | 101 | -- | extracts the diagonal from a rectangular matrix |
93 | takeDiag :: (Element t) => Matrix t -> Vector t | 102 | takeDiag :: (Element t) => Matrix t -> Vector t |