From: Michael Orlitzky Date: Wed, 5 Feb 2014 01:33:09 +0000 (-0500) Subject: Add zipcol and matmap functions in Linear.Matrix. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=numerical-analysis.git;a=commitdiff_plain;h=26b7d3e4333fd2f36961b00861952f2a63a0eaba Add zipcol and matmap functions in Linear.Matrix. --- diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 34920b4..7c0f84a 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -767,3 +767,34 @@ trace matrix = let (Mat rows) = diagonal matrix in element_sum $ V.map V.head rows + + +-- | Zip together two column matrices. +-- +-- Examples: +-- +-- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int +-- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int +-- >>> zipcol m1 m2 +-- (((1,1)),((1,2)),((1,3))) +-- +zipcol :: Arity m => Col m a -> Col m a -> Col m (a,a) +zipcol c1 c2 = + construct lambda + where + lambda i j = (c1 !!! (i,j), c2 !!! (i,j)) + + +-- | Map a function over a matrix of any dimensions. +-- +-- Examples: +-- +-- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int +-- >>> matmap (^2) m +-- ((1,4),(9,16)) +-- +matmap :: (a -> b) -> Mat m n a -> Mat m n b +matmap f (Mat rows) = + Mat $ V.map g rows + where + g = V.map f