summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index a2a70dd..ec6657a 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -65,6 +65,15 @@ partit :: Int -> [a] -> [[a]]
65partit _ [] = [] 65partit _ [] = []
66partit n l = take n l : partit n (drop n l) 66partit n l = take n l : partit n (drop n l)
67 67
68-- | obtains the common value of a property of a list
69common :: (Eq a) => (b->a) -> [b] -> Maybe a
70common f = commonval . map f where
71 commonval :: (Eq a) => [a] -> Maybe a
72 commonval [] = Nothing
73 commonval [a] = Just a
74 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing
75
76
68toLists m | fortran m = transpose $ partit (rows m) . toList . dat $ m 77toLists m | fortran m = transpose $ partit (rows m) . toList . dat $ m
69 | otherwise = partit (cols m) . toList . dat $ m 78 | otherwise = partit (cols m) . toList . dat $ m
70 79
@@ -115,7 +124,7 @@ transdataAux fun c1 d c2 =
115 else unsafePerformIO $ do 124 else unsafePerformIO $ do
116 v <- createVector (dim d) 125 v <- createVector (dim d)
117 fun r1 c1 (ptr d) r2 c2 (ptr v) // check "transdataAux" [d] 126 fun r1 c1 (ptr d) r2 c2 (ptr v) // check "transdataAux" [d]
118 putStrLn "---> transdataAux" 127 --putStrLn "---> transdataAux"
119 return v 128 return v
120 where r1 = dim d `div` c1 129 where r1 = dim d `div` c1
121 r2 = dim d `div` c2 130 r2 = dim d `div` c2
@@ -136,6 +145,12 @@ transdata c1 d c2 | isReal baseOf d = scast $ transdataR c1 (scast d) c2
136--{-# RULES "transdataR" transdata=transdataR #-} 145--{-# RULES "transdataR" transdata=transdataR #-}
137--{-# RULES "transdataC" transdata=transdataC #-} 146--{-# RULES "transdataC" transdata=transdataC #-}
138 147
148-- | creates a Matrix from a list of vectors
149fromRows :: Field t => [Vector t] -> Matrix t
150fromRows vs = case common dim vs of
151 Nothing -> error "fromRows applied to [] or to vectors with different sizes"
152 Just c -> reshape c (join vs)
153
139-- | extracts the rows of a matrix as a list of vectors 154-- | extracts the rows of a matrix as a list of vectors
140toRows :: Storable t => Matrix t -> [Vector t] 155toRows :: Storable t => Matrix t -> [Vector t]
141toRows m = toRows' 0 where 156toRows m = toRows' 0 where