diff options
author | Alberto Ruiz <aruiz@um.es> | 2011-05-02 17:31:09 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2011-05-02 17:31:09 +0000 |
commit | b48c876b4b9ba37e45104fc7b54d8024c6bd1eb5 (patch) | |
tree | 6fcd01cf5f14332f425dbe2fda9e6de1a1f04853 /lib | |
parent | e2dde2e24f581d37eb862392aee1cb2e09cf0951 (diff) |
mapMatrixWithIndex, Mul, multiply.hs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 55 | ||||
-rw-r--r-- | lib/Numeric/Container.hs | 2 |
2 files changed, 56 insertions, 1 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 | |||
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs index 621574e..90155fe 100644 --- a/lib/Numeric/Container.hs +++ b/lib/Numeric/Container.hs | |||
@@ -36,7 +36,7 @@ module Numeric.Container ( | |||
36 | -- * Matrix product | 36 | -- * Matrix product |
37 | Product(..), | 37 | Product(..), |
38 | optimiseMult, | 38 | optimiseMult, |
39 | mXm,mXv,vXm,(<.>),(<>),(<\>), | 39 | mXm,mXv,vXm,(<.>),Mul(..),(<\>), |
40 | outer, kronecker, | 40 | outer, kronecker, |
41 | -- * Random numbers | 41 | -- * Random numbers |
42 | RandDist(..), | 42 | RandDist(..), |