]> gitweb.michael.orlitzky.com - numerical-analysis.git/commitdiff
New function: Linear.Matrix.identity_matrix.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 2 Feb 2014 16:33:03 +0000 (11:33 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 2 Feb 2014 16:33:03 +0000 (11:33 -0500)
src/Linear/Matrix.hs

index c48f722265b19e3bd195ce5dc16e3ab44162d137..c0f56b348f4d4d2fbdf272cbac05e7c799eddce7 100644 (file)
@@ -213,6 +213,18 @@ construct lambda = Mat $ generate make_row
     make_row i = generate (lambda i)
 
 
+-- | Create an identity matrix with the right dimensions.
+--
+--   Examples:
+--
+--   >>> identity_matrix :: Mat3 Int
+--   ((1,0,0),(0,1,0),(0,0,1))
+--   >>> identity_matrix :: Mat3 Double
+--   ((1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0))
+--
+identity_matrix :: (Arity m, Ring.C a) => Mat m m a
+identity_matrix =
+  construct (\i j -> if i == j then (fromInteger 1) else (fromInteger 0))
 
 -- | Given a positive-definite matrix @m@, computes the
 --   upper-triangular matrix @r@ with (transpose r)*r == m and all