diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 17 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 13 |
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 | |||
176 | asColumn :: Element a => Vector a -> Matrix a | 176 | asColumn :: Element a => Vector a -> Matrix a |
177 | asColumn v = reshape 1 v | 177 | asColumn 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 | -} | ||
189 | buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a | ||
190 | buildMatrix 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 | ||
181 | fromArray2D :: (Element e) => Array (Int, Int) e -> Matrix e | 196 | fromArray2D :: (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 | ||
15 | module Data.Packed.Vector ( | 15 | module 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 | |||
59 | constant :: Element a => a -> Int -> Vector a | 59 | constant :: Element a => a -> Int -> Vector a |
60 | -- constant x n = runSTVector (newVector x n) | 60 | -- constant x n = runSTVector (newVector x n) |
61 | constant = constantD -- about 2x faster | 61 | constant = 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 | ||
67 | 4 |> [0.0,1.0,2.0,3.0]@ | ||
68 | |||
69 | -} | ||
70 | buildVector :: Element a => Int -> (Int -> a) -> Vector a | ||
71 | buildVector len f = | ||
72 | fromList $ map f [0 .. (len - 1)] | ||