From d05e7a5cec0e76a6ebb4cbc49651211c5a7cf18e Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 21 Jul 2013 10:13:50 -0400 Subject: [PATCH] Add 'diagonal' function. --- src/Linear/Matrix.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 -- 2.43.2