From: Michael Orlitzky Date: Wed, 12 Feb 2014 00:21:53 +0000 (-0500) Subject: Add Linear.Matrix.ifoldr2. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=numerical-analysis.git;a=commitdiff_plain;h=342fb80c0a34bd362a51fd62989eeef462b58721;hp=64e43e504a8716cb1784de5fc33d7f02e915e2ac Add Linear.Matrix.ifoldr2. --- 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@. --