diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-02-02 09:14:55 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-02-02 09:14:55 +0000 |
commit | 7e736bc767942e38f4c0cf4a4d8a324b17c0316d (patch) | |
tree | 98f4f500d52314714eb089c3b4bb95392e9bb23c /lib | |
parent | 2a3baa18b508851a9d30e4abc7d7de7978146557 (diff) |
table, splitEvery
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Internal/Common.hs | 23 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 15 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 2 |
4 files changed, 23 insertions, 19 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 | ||
25 | import Foreign | 26 | import Foreign |
@@ -27,11 +28,12 @@ import Control.Monad(when) | |||
27 | import Foreign.C.String(peekCString) | 28 | import Foreign.C.String(peekCString) |
28 | import Foreign.C.Types | 29 | import Foreign.C.Types |
29 | import Foreign.Storable.Complex() | 30 | import Foreign.Storable.Complex() |
31 | import 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]]@ |
32 | partit :: Int -> [a] -> [[a]] | 34 | splitEvery :: Int -> [a] -> [[a]] |
33 | partit _ [] = [] | 35 | splitEvery _ [] = [] |
34 | partit n l = take n l : partit n (drop n l) | 36 | splitEvery 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 |
37 | common :: (Eq a) => (b->a) -> [b] -> Maybe a | 39 | common :: (Eq a) => (b->a) -> [b] -> Maybe a |
@@ -47,6 +49,15 @@ compatdim [] = Nothing | |||
47 | compatdim [a] = Just a | 49 | compatdim [a] = Just a |
48 | compatdim (a:b:xs) = if a==b || a==1 || b==1 then compatdim (max a b:xs) else Nothing | 50 | compatdim (a:b:xs) = if a==b || a==1 || b==1 then compatdim (max a b:xs) else Nothing |
49 | 51 | ||
52 | -- | Formatting tool | ||
53 | table :: String -> [[String]] -> String | ||
54 | table 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 |
52 | infixl 0 // | 63 | infixl 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' |
141 | toLists :: (Element t) => Matrix t -> [[t]] | 141 | toLists :: (Element t) => Matrix t -> [[t]] |
142 | toLists m = partit (cols m) . toList . flatten $ m | 142 | toLists 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, |
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 66e5082..a8e998f 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -36,10 +36,10 @@ module Data.Packed.Matrix ( | |||
36 | import Data.Packed.Internal | 36 | import Data.Packed.Internal |
37 | import qualified Data.Packed.ST as ST | 37 | import qualified Data.Packed.ST as ST |
38 | import Data.Packed.Vector | 38 | import Data.Packed.Vector |
39 | import Data.List(transpose,intersperse) | ||
40 | import Data.Array | 39 | import Data.Array |
41 | import System.Process(readProcess) | 40 | import System.Process(readProcess) |
42 | import Text.Printf(printf) | 41 | import Text.Printf(printf) |
42 | import Data.List(transpose) | ||
43 | 43 | ||
44 | -- | creates a matrix from a vertical list of matrices | 44 | -- | creates a matrix from a vertical list of matrices |
45 | joinVert :: Element t => [Matrix t] -> Matrix t | 45 | joinVert :: Element t => [Matrix t] -> Matrix t |
@@ -85,7 +85,7 @@ adaptBlocks ms = ms' where | |||
85 | rs = map (compatdim . map rows) ms | 85 | rs = map (compatdim . map rows) ms |
86 | cs = map (compatdim . map cols) (transpose ms) | 86 | cs = map (compatdim . map cols) (transpose ms) |
87 | szs = sequence [rs,cs] | 87 | szs = sequence [rs,cs] |
88 | ms' = partit bc $ zipWith g szs (concat ms) | 88 | ms' = splitEvery bc $ zipWith g szs (concat ms) |
89 | 89 | ||
90 | g [Just nr,Just nc] m | 90 | g [Just nr,Just nc] m |
91 | | nr == r && nc == c = m | 91 | | nr == r && nc == c = m |
@@ -249,13 +249,6 @@ shfc n z@ (a:+b) | |||
249 | 249 | ||
250 | -} | 250 | -} |
251 | 251 | ||
252 | dsp' :: String -> [[String]] -> String | ||
253 | dsp' sep as = unlines . map unwords' $ transpose mtp where | ||
254 | mt = transpose as | ||
255 | longs = map (maximum . map length) mt | ||
256 | mtp = zipWith (\a b -> map (pad a) b) longs mt | ||
257 | pad n str = replicate (n - length str) ' ' ++ str | ||
258 | unwords' = concat . intersperse sep | ||
259 | 252 | ||
260 | {- | Creates a string from a matrix given a separator and a function to show each entry. Using | 253 | {- | Creates a string from a matrix given a separator and a function to show each entry. Using |
261 | this function the user can easily define any desired display function: | 254 | this function the user can easily define any desired display function: |
@@ -266,7 +259,7 @@ this function the user can easily define any desired display function: | |||
266 | 259 | ||
267 | -} | 260 | -} |
268 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String | 261 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String |
269 | format sep f m = dsp' sep . map (map f) . toLists $ m | 262 | format sep f m = table sep . map (map f) . toLists $ m |
270 | 263 | ||
271 | {- | 264 | {- |
272 | disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m | 265 | disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m |
@@ -385,7 +378,7 @@ extractRows l m = fromRows $ extract (toRows $ m) l | |||
385 | 378 | ||
386 | -} | 379 | -} |
387 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t | 380 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t |
388 | repmat m r c = fromBlocks $ partit c $ replicate (r*c) m | 381 | repmat m r c = fromBlocks $ splitEvery c $ replicate (r*c) m |
389 | 382 | ||
390 | -- | A version of 'liftMatrix2' which automatically adapt matrices with a single row or column to match the dimensions of the other matrix. | 383 | -- | A version of 'liftMatrix2' which automatically adapt matrices with a single row or column to match the dimensions of the other matrix. |
391 | liftMatrix2Auto :: (Element t, Element a, Element b) | 384 | liftMatrix2Auto :: (Element t, Element a, Element b) |
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 51e0922..6b0fb08 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -729,7 +729,7 @@ m2=(4><3) | |||
729 | -} | 729 | -} |
730 | kronecker :: (Field t) => Matrix t -> Matrix t -> Matrix t | 730 | kronecker :: (Field t) => Matrix t -> Matrix t -> Matrix t |
731 | kronecker a b = fromBlocks | 731 | kronecker a b = fromBlocks |
732 | . partit (cols a) | 732 | . splitEvery (cols a) |
733 | . map (reshape (cols b)) | 733 | . map (reshape (cols b)) |
734 | . toRows | 734 | . toRows |
735 | $ flatten a `outer` flatten b | 735 | $ flatten a `outer` flatten b |