diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index ab68618..0b23b2f 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -36,6 +36,7 @@ module Data.Packed.Matrix ( | |||
36 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, | 36 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, |
37 | extractRows, | 37 | extractRows, |
38 | diagRect, takeDiag, | 38 | diagRect, takeDiag, |
39 | mapMatrix, mapMatrixWithIndex, mapMatrixWithIndexM, mapMatrixWithIndexM_, | ||
39 | liftMatrix, liftMatrix2, liftMatrix2Auto,fromArray2D | 40 | liftMatrix, liftMatrix2, liftMatrix2Auto,fromArray2D |
40 | ) where | 41 | ) where |
41 | 42 | ||
@@ -44,6 +45,7 @@ import qualified Data.Packed.ST as ST | |||
44 | import Data.List(transpose,intersperse) | 45 | import Data.List(transpose,intersperse) |
45 | import Data.Array | 46 | import Data.Array |
46 | import Foreign.Storable | 47 | import Foreign.Storable |
48 | import Control.Arrow((***)) | ||
47 | 49 | ||
48 | ------------------------------------------------------------------- | 50 | ------------------------------------------------------------------- |
49 | 51 | ||
@@ -348,3 +350,56 @@ toBlocksEvery r c m = toBlocks rs cs m where | |||
348 | cs = replicate qc c ++ if rc > 0 then [rc] else [] | 350 | cs = replicate qc c ++ if rc > 0 then [rc] else [] |
349 | 351 | ||
350 | ------------------------------------------------------------------- | 352 | ------------------------------------------------------------------- |
353 | |||
354 | mk c g = \k v -> g ((fromIntegral *** fromIntegral) (divMod k c)) v | ||
355 | |||
356 | {- | | ||
357 | |||
358 | @ghci> mapMatrixWithIndexM_ (\\(i,j) v -> printf \"m[%.0f,%.0f] = %.f\\n\" i j v :: IO()) ((2><3)[1 :: Double ..]) | ||
359 | m[0,0] = 1 | ||
360 | m[0,1] = 2 | ||
361 | m[0,2] = 3 | ||
362 | m[1,0] = 4 | ||
363 | m[1,1] = 5 | ||
364 | m[1,2] = 6@ | ||
365 | -} | ||
366 | mapMatrixWithIndexM_ | ||
367 | :: (Element a, Num a, | ||
368 | Functor f, Monad f) => | ||
369 | ((a, a) -> a -> f ()) -> Matrix a -> f () | ||
370 | mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m | ||
371 | where | ||
372 | c = cols m | ||
373 | |||
374 | {- | | ||
375 | |||
376 | @ghci> mapMatrixWithIndexM (\\(i,j) v -> Just $ 100*v + 10*i + j) (ident 3:: Matrix Double) | ||
377 | Just (3><3) | ||
378 | [ 100.0, 1.0, 2.0 | ||
379 | , 10.0, 111.0, 12.0 | ||
380 | , 20.0, 21.0, 122.0 ]@ | ||
381 | -} | ||
382 | mapMatrixWithIndexM | ||
383 | :: (Foreign.Storable.Storable t, | ||
384 | Element a, Num a, | ||
385 | Functor f, Monad f) => | ||
386 | ((a, a) -> a -> f t) -> Matrix a -> f (Matrix t) | ||
387 | mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m | ||
388 | where | ||
389 | c = cols m | ||
390 | |||
391 | {- | | ||
392 | @ghci> mapMatrixWithIndex (\\(i,j) v -> 100*v + 10*i + j) (ident 3:: Matrix Double) | ||
393 | (3><3) | ||
394 | [ 100.0, 1.0, 2.0 | ||
395 | , 10.0, 111.0, 12.0 | ||
396 | , 20.0, 21.0, 122.0 ]@ | ||
397 | -} | ||
398 | mapMatrixWithIndex :: (Foreign.Storable.Storable t, | ||
399 | Element a, Num a) => | ||
400 | ((a, a) -> a -> t) -> Matrix a -> Matrix t | ||
401 | mapMatrixWithIndex g = head . mapMatrixWithIndexM (\a b -> [g a b]) | ||
402 | |||
403 | mapMatrix :: (Storable a, Storable b) => (a -> b) -> Matrix a -> Matrix b | ||
404 | mapMatrix f = liftMatrix (mapVector f) | ||
405 | |||