summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-01-20 12:05:34 +0000
committerAlberto Ruiz <aruiz@um.es>2010-01-20 12:05:34 +0000
commit408997ea02dec897934b176fe57115bdb4f60f63 (patch)
tree3a9a4a86328ec36d5c6ad31eb8e6609cfb7a05ea /lib/Data/Packed/Internal
parent102d6b19beadd76b28d32e1aa40844fad19af756 (diff)
more defined fromRows/fromColumns/fromBlocks
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r--lib/Data/Packed/Internal/Common.hs8
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs11
2 files changed, 15 insertions, 4 deletions
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs
index 629016d..972cd7d 100644
--- a/lib/Data/Packed/Internal/Common.hs
+++ b/lib/Data/Packed/Internal/Common.hs
@@ -18,7 +18,7 @@ module Data.Packed.Internal.Common(
18 Adapt, 18 Adapt,
19 app1, app2, app3, app4, 19 app1, app2, app3, app4,
20 (//), check, 20 (//), check,
21 partit, common, 21 partit, common, compatdim,
22 fi 22 fi
23) where 23) where
24 24
@@ -41,6 +41,12 @@ common f = commonval . map f where
41 commonval [a] = Just a 41 commonval [a] = Just a
42 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing 42 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing
43 43
44-- | common value with \"adaptable\" 1
45compatdim :: [Int] -> Maybe Int
46compatdim [] = Nothing
47compatdim [a] = Just a
48compatdim (a:b:xs) = if a==b || a==1 || b==1 then compatdim (max a b:xs) else Nothing
49
44-- | postfix function application (@flip ($)@) 50-- | postfix function application (@flip ($)@)
45(//) :: x -> (x -> y) -> y 51(//) :: x -> (x -> y) -> y
46infixl 0 // 52infixl 0 //
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index a220f1a..33c9324 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -135,11 +135,16 @@ type Mt t s = Int -> Int -> Ptr t -> s
135toLists :: (Element t) => Matrix t -> [[t]] 135toLists :: (Element t) => Matrix t -> [[t]]
136toLists m = partit (cols m) . toList . flatten $ m 136toLists m = partit (cols m) . toList . flatten $ m
137 137
138-- | creates a Matrix from a list of vectors 138-- | Create a matrix from a list of vectors.
139-- All vectors must have the same dimension,
140-- or dimension 1, which is are automatically expanded.
139fromRows :: Element t => [Vector t] -> Matrix t 141fromRows :: Element t => [Vector t] -> Matrix t
140fromRows vs = case common dim vs of 142fromRows vs = case compatdim (map dim vs) of
141 Nothing -> error "fromRows applied to [] or to vectors with different sizes" 143 Nothing -> error "fromRows applied to [] or to vectors with different sizes"
142 Just c -> reshape c (join vs) 144 Just c -> reshape c . join . map (adapt c) $ vs
145 where
146 adapt c v | dim v == c = v
147 | otherwise = constantD (v@>0) c
143 148
144-- | extracts the rows of a matrix as a list of vectors 149-- | extracts the rows of a matrix as a list of vectors
145toRows :: Element t => Matrix t -> [Vector t] 150toRows :: Element t => Matrix t -> [Vector t]