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.hs21
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index f72bd15..b92d60f 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -74,8 +74,10 @@ instance (Binary a, Element a, Storable a) => Binary (Matrix a) where
74------------------------------------------------------------------- 74-------------------------------------------------------------------
75 75
76instance (Show a, Element a) => (Show (Matrix a)) where 76instance (Show a, Element a) => (Show (Matrix a)) where
77 show m = (sizes++) . dsp . map (map show) . toLists $ m 77 show m | rows m == 0 || cols m == 0 = sizes m ++" []"
78 where sizes = "("++show (rows m)++"><"++show (cols m)++")\n" 78 show m = (sizes m++) . dsp . map (map show) . toLists $ m
79
80sizes m = "("++show (rows m)++"><"++show (cols m)++")\n"
79 81
80dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp 82dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp
81 where 83 where
@@ -104,7 +106,7 @@ breakAt c l = (a++[c],tail b) where
104joinVert :: Element t => [Matrix t] -> Matrix t 106joinVert :: Element t => [Matrix t] -> Matrix t
105joinVert ms = case common cols ms of 107joinVert ms = case common cols ms of
106 Nothing -> error "(impossible) joinVert on matrices with different number of columns" 108 Nothing -> error "(impossible) joinVert on matrices with different number of columns"
107 Just c -> reshape c $ vjoin (map flatten ms) 109 Just c -> matrixFromVector RowMajor (sum (map rows ms)) c $ vjoin (map flatten ms)
108 110
109-- | creates a matrix from a horizontal list of matrices 111-- | creates a matrix from a horizontal list of matrices
110joinHoriz :: Element t => [Matrix t] -> Matrix t 112joinHoriz :: Element t => [Matrix t] -> Matrix t
@@ -147,7 +149,7 @@ adaptBlocks ms = ms' where
147 149
148 g [Just nr,Just nc] m 150 g [Just nr,Just nc] m
149 | nr == r && nc == c = m 151 | nr == r && nc == c = m
150 | r == 1 && c == 1 = reshape nc (constantD x (nr*nc)) 152 | r == 1 && c == 1 = matrixFromVector RowMajor nr nc (constantD x (nr*nc))
151 | r == 1 = fromRows (replicate nr (flatten m)) 153 | r == 1 = fromRows (replicate nr (flatten m))
152 | otherwise = fromColumns (replicate nc (flatten m)) 154 | otherwise = fromColumns (replicate nc (flatten m))
153 where 155 where
@@ -237,7 +239,7 @@ safely be used with lists that are too long (like infinite lists).
237-} 239-}
238(><) :: (Storable a) => Int -> Int -> [a] -> Matrix a 240(><) :: (Storable a) => Int -> Int -> [a] -> Matrix a
239r >< c = f where 241r >< c = f where
240 f l | dim v == r*c = matrixFromVector RowMajor c v 242 f l | dim v == r*c = matrixFromVector RowMajor r c v
241 | otherwise = error $ "inconsistent list size = " 243 | otherwise = error $ "inconsistent list size = "
242 ++show (dim v) ++" in ("++show r++"><"++show c++")" 244 ++show (dim v) ++" in ("++show r++"><"++show c++")"
243 where v = fromList $ take (r*c) l 245 where v = fromList $ take (r*c) l
@@ -291,7 +293,7 @@ asRow v = reshape (dim v) v
291-- , 5.0 ] 293-- , 5.0 ]
292-- 294--
293asColumn :: Storable a => Vector a -> Matrix a 295asColumn :: Storable a => Vector a -> Matrix a
294asColumn v = reshape 1 v 296asColumn = trans . asRow
295 297
296 298
297 299
@@ -358,7 +360,12 @@ liftMatrix2Auto f m1 m2
358 m1' = conformMTo (r,c) m1 360 m1' = conformMTo (r,c) m1
359 m2' = conformMTo (r,c) m2 361 m2' = conformMTo (r,c) m2
360 362
361lM f m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2)) 363-- FIXME do not flatten if equal order
364lM f m1 m2 = matrixFromVector
365 RowMajor
366 (max (rows m1) (rows m2))
367 (max (cols m1) (cols m2))
368 (f (flatten m1) (flatten m2))
362 369
363compat' :: Matrix a -> Matrix b -> Bool 370compat' :: Matrix a -> Matrix b -> Bool
364compat' m1 m2 = s1 == (1,1) || s2 == (1,1) || s1 == s2 371compat' m1 m2 = s1 == (1,1) || s2 == (1,1) || s1 == s2