diff options
author | Erik de Castro Lopo <erikd@mega-nerd.com> | 2009-12-10 10:35:35 +0000 |
---|---|---|
committer | Erik de Castro Lopo <erikd@mega-nerd.com> | 2009-12-10 10:35:35 +0000 |
commit | 7c2301797e35930298db9a6bff09eefdd4fbd577 (patch) | |
tree | 74e332e1d30dad3aa52ff7d588bf9db370354aa0 /lib/Data/Packed/Matrix.hs | |
parent | 2ef7028aab75ee9e96b9f4a429f70d37c63deab5 (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/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 17 |
1 files changed, 16 insertions, 1 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 |