From: Michael Orlitzky Date: Sun, 21 Jul 2013 14:13:50 +0000 (-0400) Subject: Add 'diagonal' function. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=d05e7a5cec0e76a6ebb4cbc49651211c5a7cf18e;p=numerical-analysis.git Add 'diagonal' function. --- diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 8490b22..20769b4 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -520,3 +520,23 @@ angle v1 v2 = where theta = (recip norms) NP.* (v1 `dot` v2) norms = (norm v1) NP.* (norm v2) + + + +-- | 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 m +-- ((1,0,0),(0,5,0),(0,0,9)) +-- +diagonal :: (Arity m, Ring.C a) + => Mat m m a + -> Mat m m a +diagonal matrix = + construct lambda + where + lambda i j = if i == j then matrix !!! (i,j) else 0