X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FLinear%2FMatrix.hs;h=20769b4708950d103bd0ec1fd18737d9616cb0ca;hb=4a1589178a2928710f13e0cb18efb34ed8c0d883;hp=8490b229921becb127a7a299be47efd16ddec7e4;hpb=4bd7e17d1c9315e30881ad697371640acbfa1fb5;p=numerical-analysis.git 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