+
+
+-- | Compute the trace of a square matrix, the sum of the elements
+-- which lie on its diagonal. We require the matrix to be
+-- square to avoid ambiguity in the return type which would ideally
+-- have dimension min(m,n) supposing an m-by-n matrix.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> trace m
+-- 15
+--
+trace :: (Arity m, Ring.C a) => Mat m m a -> a
+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