fromList,
head,
ifoldl,
+ ifoldr,
imap,
map,
maximum,
-- | 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:
--
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@.
--