From 26b7d3e4333fd2f36961b00861952f2a63a0eaba Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 4 Feb 2014 20:33:09 -0500 Subject: [PATCH] Add zipcol and matmap functions in Linear.Matrix. --- src/Linear/Matrix.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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 -- 2.44.2