From 342fb80c0a34bd362a51fd62989eeef462b58721 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 11 Feb 2014 19:21:53 -0500 Subject: [PATCH] Add Linear.Matrix.ifoldr2. --- src/Linear/Matrix.hs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 4d1dceb..119d770 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -37,6 +37,7 @@ import qualified Data.Vector.Fixed as V ( fromList, head, ifoldl, + ifoldr, imap, map, maximum, @@ -911,7 +912,8 @@ map2 f (Mat rows) = -- | Fold over the entire matrix passing the coordinates @i@ and @j@ --- (of the row/column) to the accumulation function. +-- (of the row/column) to the accumulation function. The fold occurs +-- from top-left to bottom-right. -- -- Examples: -- @@ -938,6 +940,36 @@ ifoldl2 f initial (Mat rows) = row_function rowinit idx r = V.ifoldl (g idx) rowinit r +-- | Fold over the entire matrix passing the coordinates @i@ and @j@ +-- (of the row/column) to the accumulation function. The fold occurs +-- from bottom-right to top-left. +-- +-- The order of the arguments in the supplied function are different +-- from those in V.ifoldr; we keep them similar to ifoldl2. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> ifoldr2 (\i j cur _ -> cur + i + j) 0 m +-- 18 +-- +ifoldr2 :: forall a b m n. + (Int -> Int -> b -> a -> b) + -> b + -> Mat m n a + -> b +ifoldr2 f initial (Mat rows) = + V.ifoldr row_function initial rows + where + -- | Swap the order of arguments in @f@ so that it agrees with the + -- @f@ passed to ifoldl2. + g :: Int -> Int -> a -> b -> b + g w x y z = f w x z y + + row_function :: Int -> Vec n a -> b -> b + row_function idx r rowinit = V.ifoldr (g idx) rowinit r + + -- | Map a function over a matrix of any dimensions, passing the -- coordinates @i@ and @j@ to the function @f@. -- -- 2.43.2