X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FLinear%2FMatrix.hs;h=660330245b330b840bcb848af512b7d6561e217f;hb=4b5f216ca9e1890c40bbf8053cfed94e3961849f;hp=7c0f84a41042744b636c416dc8c3043e6b76abe6;hpb=26b7d3e4333fd2f36961b00861952f2a63a0eaba;p=numerical-analysis.git diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 7c0f84a..6603302 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -37,6 +37,7 @@ import qualified Data.Vector.Fixed as V ( and, fromList, head, + ifoldl, length, map, maximum, @@ -91,6 +92,11 @@ type Col3 a = Col N3 a type Col4 a = Col N4 a type Col5 a = Col N5 a +-- We need a big column for Gaussian quadrature. +type N10 = S (S (S (S (S N5)))) +type Col10 a = Col N10 a + + instance (Eq a) => Eq (Mat m n a) where -- | Compare a row at a time. -- @@ -775,16 +781,36 @@ trace matrix = -- -- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int -- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int --- >>> zipcol m1 m2 +-- >>> colzip m1 m2 -- (((1,1)),((1,2)),((1,3))) -- -zipcol :: Arity m => Col m a -> Col m a -> Col m (a,a) -zipcol c1 c2 = +colzip :: Arity m => Col m a -> Col m a -> Col m (a,a) +colzip c1 c2 = construct lambda where lambda i j = (c1 !!! (i,j), c2 !!! (i,j)) +-- | Zip together two column matrices using the supplied function. +-- +-- Examples: +-- +-- >>> let c1 = fromList [[1],[2],[3]] :: Col3 Integer +-- >>> let c2 = fromList [[4],[5],[6]] :: Col3 Integer +-- >>> colzipwith (^) c1 c2 +-- ((1),(32),(729)) +-- +colzipwith :: Arity m + => (a -> a -> b) + -> Col m a + -> Col m a + -> Col m b +colzipwith f c1 c2 = + construct lambda + where + lambda i j = f (c1 !!! (i,j)) (c2 !!! (i,j)) + + -- | Map a function over a matrix of any dimensions. -- -- Examples: @@ -798,3 +824,33 @@ matmap f (Mat rows) = Mat $ V.map g rows where g = V.map f + + +-- | Fold over the entire matrix passing the coordinates @i@ and @j@ +-- (of the row/column) to the accumulation function. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int +-- >>> ifoldl2 (\i j cur _ -> cur + i + j) 0 m +-- 18 +-- +ifoldl2 :: forall a b m n. + (Int -> Int -> b -> a -> b) + -> b + -> Mat m n a + -> b +ifoldl2 f initial (Mat rows) = + V.ifoldl row_function initial rows + where + -- | The order that we need this in (so that @g idx@ makes sense) + -- is a little funny. So that we don't need to pass weird + -- functions into ifoldl2, we swap the second and third + -- arguments of @f@ calling the result @g@. + g :: Int -> b -> Int -> a -> b + g w x y = f w y x + + row_function :: b -> Int -> Vec n a -> b + row_function rowinit idx r = V.ifoldl (g idx) rowinit r + +