summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-02 09:14:55 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-02 09:14:55 +0000
commit7e736bc767942e38f4c0cf4a4d8a324b17c0316d (patch)
tree98f4f500d52314714eb089c3b4bb95392e9bb23c /lib/Data/Packed/Internal
parent2a3baa18b508851a9d30e4abc7d7de7978146557 (diff)
table, splitEvery
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r--lib/Data/Packed/Internal/Common.hs23
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs2
2 files changed, 18 insertions, 7 deletions
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs
index 972cd7d..bfa63f1 100644
--- a/lib/Data/Packed/Internal/Common.hs
+++ b/lib/Data/Packed/Internal/Common.hs
@@ -18,8 +18,9 @@ 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, compatdim, 21 splitEvery, common, compatdim,
22 fi 22 fi,
23 table
23) where 24) where
24 25
25import Foreign 26import Foreign
@@ -27,11 +28,12 @@ import Control.Monad(when)
27import Foreign.C.String(peekCString) 28import Foreign.C.String(peekCString)
28import Foreign.C.Types 29import Foreign.C.Types
29import Foreign.Storable.Complex() 30import Foreign.Storable.Complex()
31import Data.List(transpose,intersperse)
30 32
31-- | @partit 3 [1..9] == [[1,2,3],[4,5,6],[7,8,9]]@ 33-- | @splitEvery 3 [1..9] == [[1,2,3],[4,5,6],[7,8,9]]@
32partit :: Int -> [a] -> [[a]] 34splitEvery :: Int -> [a] -> [[a]]
33partit _ [] = [] 35splitEvery _ [] = []
34partit n l = take n l : partit n (drop n l) 36splitEvery k l = take k l : splitEvery k (drop k l)
35 37
36-- | obtains the common value of a property of a list 38-- | obtains the common value of a property of a list
37common :: (Eq a) => (b->a) -> [b] -> Maybe a 39common :: (Eq a) => (b->a) -> [b] -> Maybe a
@@ -47,6 +49,15 @@ compatdim [] = Nothing
47compatdim [a] = Just a 49compatdim [a] = Just a
48compatdim (a:b:xs) = if a==b || a==1 || b==1 then compatdim (max a b:xs) else Nothing 50compatdim (a:b:xs) = if a==b || a==1 || b==1 then compatdim (max a b:xs) else Nothing
49 51
52-- | Formatting tool
53table :: String -> [[String]] -> String
54table sep as = unlines . map unwords' $ transpose mtp where
55 mt = transpose as
56 longs = map (maximum . map length) mt
57 mtp = zipWith (\a b -> map (pad a) b) longs mt
58 pad n str = replicate (n - length str) ' ' ++ str
59 unwords' = concat . intersperse sep
60
50-- | postfix function application (@flip ($)@) 61-- | postfix function application (@flip ($)@)
51(//) :: x -> (x -> y) -> y 62(//) :: x -> (x -> y) -> y
52infixl 0 // 63infixl 0 //
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 8bd6fb2..a6b6215 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -139,7 +139,7 @@ type Mt t s = Int -> Int -> Ptr t -> s
139 139
140-- | the inverse of 'Data.Packed.Matrix.fromLists' 140-- | the inverse of 'Data.Packed.Matrix.fromLists'
141toLists :: (Element t) => Matrix t -> [[t]] 141toLists :: (Element t) => Matrix t -> [[t]]
142toLists m = partit (cols m) . toList . flatten $ m 142toLists m = splitEvery (cols m) . toList . flatten $ m
143 143
144-- | Create a matrix from a list of vectors. 144-- | Create a matrix from a list of vectors.
145-- All vectors must have the same dimension, 145-- All vectors must have the same dimension,