From 1c920c8bed07646272d859c8d5ad1a77cbf98cd9 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 17 Mar 2013 21:38:13 -0400 Subject: Data.Packed.Matrix: mapMatrix* should pass indices as Ints --- lib/Data/Packed/Matrix.hs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index ca96c50..48b2279 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -46,7 +46,6 @@ import Data.Array import Data.List(transpose,intersperse) import Foreign.Storable(Storable) -import Control.Arrow((***)) ------------------------------------------------------------------- @@ -352,7 +351,7 @@ toBlocksEvery r c m = toBlocks rs cs m where ------------------------------------------------------------------- -mk c g = \k v -> g ((fromIntegral *** fromIntegral) (divMod k c)) v +mk c g = \k v -> g (divMod k c) v {- | @@ -367,7 +366,7 @@ m[1,2] = 6@ mapMatrixWithIndexM_ :: (Element a, Num a, Functor f, Monad f) => - ((a, a) -> a -> f ()) -> Matrix a -> f () + ((Int, Int) -> a -> f ()) -> Matrix a -> f () mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m where c = cols m @@ -384,7 +383,7 @@ mapMatrixWithIndexM :: (Foreign.Storable.Storable t, Element a, Num a, Functor f, Monad f) => - ((a, a) -> a -> f t) -> Matrix a -> f (Matrix t) + ((Int, Int) -> a -> f t) -> Matrix a -> f (Matrix t) mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m where c = cols m @@ -398,7 +397,7 @@ mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flat -} mapMatrixWithIndex :: (Foreign.Storable.Storable t, Element a, Num a) => - ((a, a) -> a -> t) -> Matrix a -> Matrix t + ((Int, Int) -> a -> t) -> Matrix a -> Matrix t mapMatrixWithIndex g = head . mapMatrixWithIndexM (\a b -> [g a b]) mapMatrix :: (Storable a, Storable b) => (a -> b) -> Matrix a -> Matrix b -- cgit v1.2.3 From a1b41bc023c0ac44b130c6092e886525d6411b86 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 17 Mar 2013 23:11:08 -0400 Subject: Matrix: Implement mapMatrixWithIndex independently Previously this was piggybacking off of mapMatrixWithIndexM which would overflow the stack with large input --- lib/Data/Packed/Matrix.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 48b2279..68971ff 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -395,11 +395,12 @@ mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flat , 10.0, 111.0, 12.0 , 20.0, 21.0, 122.0 ]@ -} -mapMatrixWithIndex :: (Foreign.Storable.Storable t, +mapMatrixWithIndex :: (Foreign.Storable.Storable t, Element a, Num a) => ((Int, Int) -> a -> t) -> Matrix a -> Matrix t -mapMatrixWithIndex g = head . mapMatrixWithIndexM (\a b -> [g a b]) +mapMatrixWithIndex g m = reshape c $ mapVectorWithIndex (mk c g) $ flatten m + where + c = cols m mapMatrix :: (Storable a, Storable b) => (a -> b) -> Matrix a -> Matrix b mapMatrix f = liftMatrix (mapVector f) - -- cgit v1.2.3 From 167ee54f28248d55da72bcb22c3335f8448efcfa Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 17 Mar 2013 23:12:05 -0400 Subject: Matrix: Whitespace cleanup --- lib/Data/Packed/Matrix.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 68971ff..c1a9b24 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -157,7 +157,7 @@ adaptBlocks ms = ms' where ----------------------------------------------------------- --- | Reverse rows +-- | Reverse rows flipud :: Element t => Matrix t -> Matrix t flipud m = fromRows . reverse . toRows $ m @@ -272,7 +272,7 @@ Hilbert matrix of order N: buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a buildMatrix rc cc f = fromLists $ map (map f) - $ map (\ ri -> map (\ ci -> (ri, ci)) [0 .. (cc - 1)]) [0 .. (rc - 1)] + $ map (\ ri -> map (\ ci -> (ri, ci)) [0 .. (cc - 1)]) [0 .. (rc - 1)] ----------------------------------------------------- @@ -283,7 +283,7 @@ fromArray2D m = (r> [Int] -> Matrix t -> Matrix t extractRows l m = fromRows $ extract (toRows m) l where extract l' is = [l'!!i |i<-is] @@ -351,9 +351,9 @@ toBlocksEvery r c m = toBlocks rs cs m where ------------------------------------------------------------------- -mk c g = \k v -> g (divMod k c) v +mk c g = \k v -> g (divMod k c) v -{- | +{- | @ghci> mapMatrixWithIndexM_ (\\(i,j) v -> printf \"m[%.0f,%.0f] = %.f\\n\" i j v :: IO()) ((2><3)[1 :: Double ..]) m[0,0] = 1 @@ -367,7 +367,7 @@ mapMatrixWithIndexM_ :: (Element a, Num a, Functor f, Monad f) => ((Int, Int) -> a -> f ()) -> Matrix a -> f () -mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m +mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m where c = cols m @@ -380,11 +380,11 @@ Just (3><3) , 20.0, 21.0, 122.0 ]@ -} mapMatrixWithIndexM - :: (Foreign.Storable.Storable t, + :: (Foreign.Storable.Storable t, Element a, Num a, Functor f, Monad f) => ((Int, Int) -> a -> f t) -> Matrix a -> f (Matrix t) -mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m +mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m where c = cols m -- cgit v1.2.3