From: Michael Orlitzky Date: Sun, 21 Jul 2013 21:19:35 +0000 (-0400) Subject: Rename 'diagonal' to 'diagonal_part'. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=c160f3101ddb9797fd30007d6f628da394628538;p=numerical-analysis.git Rename 'diagonal' to 'diagonal_part'. Add functions to pull out the (strict) lower/upper triangular parts of a matrix. --- diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 105acef..e4743b5 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -530,13 +530,83 @@ angle v1 v2 = -- Examples: -- -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int --- >>> diagonal m +-- >>> diagonal_part m -- ((1,0,0),(0,5,0),(0,0,9)) -- -diagonal :: (Arity m, Ring.C a) +diagonal_part :: (Arity m, Ring.C a) => Mat m m a -> Mat m m a -diagonal matrix = +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