X-Git-Url: http://gitweb.michael.orlitzky.com/?p=numerical-analysis.git;a=blobdiff_plain;f=src%2FLinear%2FMatrix.hs;h=119d77089461dfae968320c79bd1375c31d46b41;hp=4d1dceb417c07e63992ec7afefd981a23d484c39;hb=342fb80c0a34bd362a51fd62989eeef462b58721;hpb=64e43e504a8716cb1784de5fc33d7f02e915e2ac 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@. --