+
+
+-- | Retrieve the diagonal elements of the given matrix as a \"column
+-- vector,\" i.e. a m-by-1 matrix. 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
+-- >>> diagonal m
+-- ((1),(5),(9))
+--
+diagonal :: (Arity m) => Mat m m a -> Col m a
+diagonal matrix =
+ construct lambda
+ where
+ lambda i _ = matrix !!! (i,i)
+
+
+-- | Given a square @matrix@, return a new matrix of the same size
+-- containing only the on-diagonal entries of @matrix@. The
+-- off-diagonal entries are set to zero.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> diagonal_part m
+-- ((1,0,0),(0,5,0),(0,0,9))
+--
+diagonal_part :: (Arity m, Ring.C a)
+ => Mat m m a
+ -> Mat m m a
+diagonal_part matrix =
+ construct lambda
+ where
+ lambda i j = if i == j then matrix !!! (i,j) else 0
+
+
+-- | Given a square @matrix@, return a new matrix of the same size
+-- containing only the on-diagonal and below-diagonal entries of
+-- @matrix@. The above-diagonal entries are set to zero.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> lt_part m
+-- ((1,0,0),(4,5,0),(7,8,9))
+--
+lt_part :: (Arity m, Ring.C a)
+ => Mat m m a
+ -> Mat m m a
+lt_part matrix =
+ construct lambda
+ where
+ lambda i j = if i >= j then matrix !!! (i,j) else 0
+
+
+-- | Given a square @matrix@, return a new matrix of the same size
+-- containing only the below-diagonal entries of @matrix@. The on-
+-- and above-diagonal entries are set to zero.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> lt_part_strict m
+-- ((0,0,0),(4,0,0),(7,8,0))
+--
+lt_part_strict :: (Arity m, Ring.C a)
+ => Mat m m a
+ -> Mat m m a
+lt_part_strict matrix =
+ construct lambda
+ where
+ lambda i j = if i > j then matrix !!! (i,j) else 0
+
+
+-- | Given a square @matrix@, return a new matrix of the same size
+-- containing only the on-diagonal and above-diagonal entries of
+-- @matrix@. The below-diagonal entries are set to zero.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> ut_part m
+-- ((1,2,3),(0,5,6),(0,0,9))
+--
+ut_part :: (Arity m, Ring.C a)
+ => Mat m m a
+ -> Mat m m a
+ut_part = transpose . lt_part . transpose
+
+
+-- | Given a square @matrix@, return a new matrix of the same size
+-- containing only the above-diagonal entries of @matrix@. The on-
+-- and below-diagonal entries are set to zero.
+--
+-- Examples:
+--
+-- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
+-- >>> ut_part_strict m
+-- ((0,2,3),(0,0,6),(0,0,0))
+--
+ut_part_strict :: (Arity m, Ring.C a)
+ => Mat m m a
+ -> Mat m m a
+ut_part_strict = transpose . lt_part_strict . transpose
+
+
+-- | 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
+-- >>> colzip m1 m2
+-- (((1,1)),((1,2)),((1,3)))
+--
+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:
+--
+-- >>> 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