summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs11
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
35import Data.Packed.Internal 35import Data.Packed.Internal
36import qualified Data.Packed.ST as ST
36import Data.Packed.Vector 37import Data.Packed.Vector
37import Data.List(transpose,intersperse) 38import Data.List(transpose,intersperse)
38import Data.Array 39import 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.
78diag :: Element a => Vector a -> Matrix a
79diag 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
93takeDiag :: (Element t) => Matrix t -> Vector t 102takeDiag :: (Element t) => Matrix t -> Vector t