From 4889537d508f3944fc058562505c02dfc4898f2c Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 12 May 2011 23:35:57 +0800 Subject: [PATCH] [Single/Double.Matrix] Remove redundant calculation of abs in frobenius norm The formulas found in various references do not have abs. Furthermore, when multiplying A*A^T or A^T*A (for the frobenius norm we only care about the diagonal so it doesn't matter which product is used) the i-ith diagonal element of the final matrix comes from the multiplication of line i with itself. Therefore, all numbers involved will be multiplied with themselves resulting in numbers that are always non-negative. Signed-off-by: Alexander Karatarakis --- src/Numerics/LinearAlgebra/Double/Matrix.cs | 2 +- src/Numerics/LinearAlgebra/Single/Matrix.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 0eec7f0b..bf46f7f3 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double var norm = 0.0; for (var i = 0; i < RowCount; i++) { - norm += Math.Abs(aat.At(i, i)); + norm += aat.At(i, i); } norm = Math.Sqrt(norm); diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index e33088b9..685fe010 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single var norm = 0.0f; for (var i = 0; i < RowCount; i++) { - norm += Math.Abs(aat.At(i, i)); + norm += aat.At(i, i); } norm = Convert.ToSingle(Math.Sqrt(norm));