summaryrefslogtreecommitdiff
path: root/lib/Data/Packed
diff options
context:
space:
mode:
authorErik de Castro Lopo <erikd@mega-nerd.com>2009-12-10 10:35:35 +0000
committerErik de Castro Lopo <erikd@mega-nerd.com>2009-12-10 10:35:35 +0000
commit7c2301797e35930298db9a6bff09eefdd4fbd577 (patch)
tree74e332e1d30dad3aa52ff7d588bf9db370354aa0 /lib/Data/Packed
parent2ef7028aab75ee9e96b9f4a429f70d37c63deab5 (diff)
Add functions buildVector and buildMatrix.
Both take a size parameter(s) and a function that maps vector/matrix indices to the values at that position.
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r--lib/Data/Packed/Matrix.hs17
-rw-r--r--lib/Data/Packed/Vector.hs13
2 files changed, 28 insertions, 2 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index ad0776e..6a6942d 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -19,7 +19,7 @@ module Data.Packed.Matrix (
19 (><), 19 (><),
20 trans, 20 trans,
21 reshape, flatten, 21 reshape, flatten,
22 fromLists, toLists, 22 fromLists, toLists, buildMatrix,
23 (@@>), 23 (@@>),
24 asRow, asColumn, 24 asRow, asColumn,
25 fromRows, toRows, fromColumns, toColumns, 25 fromRows, toRows, fromColumns, toColumns,
@@ -176,6 +176,21 @@ asRow v = reshape (dim v) v
176asColumn :: Element a => Vector a -> Matrix a 176asColumn :: Element a => Vector a -> Matrix a
177asColumn v = reshape 1 v 177asColumn v = reshape 1 v
178 178
179
180{- | creates a Matrix of the specified size using the supplied function to
181 to map the row/column position to the value at that row/column position.
182
183@> buildMatrix 3 4 (\ (r,c) -> fromIntegral r * fromIntegral c)
184(3><4)
185 [ 0.0, 0.0, 0.0, 0.0, 0.0
186 , 0.0, 1.0, 2.0, 3.0, 4.0
187 , 0.0, 2.0, 4.0, 6.0, 8.0]@
188-}
189buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a
190buildMatrix rc cc f =
191 fromLists $ map (\x -> map f x)
192 $ map (\ ri -> map (\ ci -> (ri, ci)) [0 .. (cc - 1)]) [0 .. (rc - 1)]
193
179----------------------------------------------------- 194-----------------------------------------------------
180 195
181fromArray2D :: (Element e) => Array (Int, Int) e -> Matrix e 196fromArray2D :: (Element e) => Array (Int, Int) e -> Matrix e
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 21f51e5..457da71 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -14,7 +14,7 @@
14 14
15module Data.Packed.Vector ( 15module Data.Packed.Vector (
16 Vector, 16 Vector,
17 fromList, (|>), toList, 17 fromList, (|>), toList, buildVector,
18 dim, (@>), 18 dim, (@>),
19 subVector, join, 19 subVector, join,
20 constant, linspace, 20 constant, linspace,
@@ -59,3 +59,14 @@ vectorMinIndex = round . toScalarR MinIdx
59constant :: Element a => a -> Int -> Vector a 59constant :: Element a => a -> Int -> Vector a
60-- constant x n = runSTVector (newVector x n) 60-- constant x n = runSTVector (newVector x n)
61constant = constantD -- about 2x faster 61constant = constantD -- about 2x faster
62
63{- | creates a Vector of the specified length using the supplied function to
64 to map the index to the value at that index.
65
66@> buildVector 4 fromIntegral
674 |> [0.0,1.0,2.0,3.0]@
68
69-}
70buildVector :: Element a => Int -> (Int -> a) -> Vector a
71buildVector len f =
72 fromList $ map f [0 .. (len - 1)]