diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 635eb9cf..66e7cbd6 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -430,21 +430,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The L1 norm of the matrix. public override double L1Norm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, _rowCount, _columnCount, _values); } /// Calculates the infinity norm of this matrix. /// The infinity norm of this matrix. public override double InfinityNorm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, _rowCount, _columnCount, _values); } /// Calculates the Frobenius norm of this matrix. /// The Frobenius norm of this matrix. public override double FrobeniusNorm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 021eae2c..10f5cbe7 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -425,21 +425,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The L1 norm of the matrix. public override double L1Norm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, _rowCount, _columnCount, _values); } /// Calculates the infinity norm of this matrix. /// The infinity norm of this matrix. public override double InfinityNorm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, _rowCount, _columnCount, _values); } /// Calculates the Frobenius norm of this matrix. /// The Frobenius norm of this matrix. public override double FrobeniusNorm() { - return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values).Real; + return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values); } /// diff --git a/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs index 4443bdf2..1ebcf99f 100644 --- a/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs +++ b/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs @@ -201,7 +201,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - T MatrixNorm(Norm norm, int rows, int columns, T[] matrix); + double MatrixNorm(Norm norm, int rows, int columns, T[] matrix); /// /// Computes the requested of the matrix. @@ -215,7 +215,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - T MatrixNorm(Norm norm, int rows, int columns, T[] matrix, TNorm[] work); + double MatrixNorm(Norm norm, int rows, int columns, T[] matrix, TNorm[] work); /// /// Multiples two matrices. result = x * y diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs index 770ffc4b..4d59ae68 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs @@ -413,12 +413,12 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - public virtual Complex MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) + public virtual double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) { - var ret = 0.0; switch (norm) { case Norm.OneNorm: + var norm1 = 0d; for (var j = 0; j < columns; j++) { var s = 0.0; @@ -426,23 +426,21 @@ namespace MathNet.Numerics.Providers.LinearAlgebra { s += matrix[(j*rows) + i].Magnitude; } - - ret = Math.Max(ret, s); + norm1 = Math.Max(norm1, s); } - - break; + return norm1; case Norm.LargestAbsoluteValue: - + var normMax = 0d; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { - ret = Math.Max(matrix[(j*rows) + i].Magnitude, ret); + normMax = Math.Max(matrix[(j * rows) + i].Magnitude, normMax); } } - - break; + return normMax; case Norm.InfinityNorm: + var normInf = 0d; for (var i = 0; i < rows; i++) { var s = 0.0; @@ -450,25 +448,21 @@ namespace MathNet.Numerics.Providers.LinearAlgebra { s += matrix[(j*rows) + i].Magnitude; } - - ret = Math.Max(ret, s); + normInf = Math.Max(normInf, s); } - - break; + return normInf; case Norm.FrobeniusNorm: var aat = new Complex[rows*rows]; MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.ConjugateTranspose, 1.0, matrix, rows, columns, matrix, rows, columns, 0.0, aat); - + var normF = 0d; for (var i = 0; i < rows; i++) { - ret += aat[(i*rows) + i].Magnitude; + normF += aat[(i * rows) + i].Magnitude; } - - ret = Math.Sqrt(ret); - break; + return Math.Sqrt(normF); + default: + throw new NotSupportedException(); } - - return ret; } /// @@ -483,7 +477,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - public virtual Complex MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) + public virtual double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) { return MatrixNorm(norm, rows, columns, matrix); } diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index d986454f..925cd2f9 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs @@ -408,65 +408,58 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// The number of rows. /// The number of columns. /// The matrix to compute the norm from. - /// - /// The requested of the matrix. - /// - public virtual Complex32 MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) + /// The requested of the matrix. + public virtual double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) { - var ret = 0.0; switch (norm) { case Norm.OneNorm: + var norm1 = 0d; for (var j = 0; j < columns; j++) { - var s = 0.0; + var s = 0d; for (var i = 0; i < rows; i++) { s += matrix[(j*rows) + i].Magnitude; } - ret = Math.Max(ret, s); + norm1 = Math.Max(norm1, s); } - - break; + return norm1; case Norm.LargestAbsoluteValue: - + var normMax = 0d; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { - ret = Math.Max(matrix[(j*rows) + i].Magnitude, ret); + normMax = Math.Max(matrix[(j * rows) + i].Magnitude, normMax); } } - - break; + return normMax; case Norm.InfinityNorm: + var normInf = 0d; for (var i = 0; i < rows; i++) { - var s = 0.0; + var s = 0d; for (var j = 0; j < columns; j++) { s += matrix[(j*rows) + i].Magnitude; } - - ret = Math.Max(ret, s); + normInf = Math.Max(normInf, s); } - - break; + return normInf; case Norm.FrobeniusNorm: var aat = new Complex32[rows*rows]; MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.ConjugateTranspose, 1.0f, matrix, rows, columns, matrix, rows, columns, 0.0f, aat); - + var normF = 0d; for (var i = 0; i < rows; i++) { - ret += aat[(i*rows) + i].Magnitude; + normF += aat[(i * rows) + i].Magnitude; } - - ret = Math.Sqrt(ret); - break; + return Math.Sqrt(normF); + default: + throw new NotSupportedException(); } - - return Convert.ToSingle(ret); } /// @@ -478,10 +471,8 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// The matrix to compute the norm from. /// The work array. Only used when /// and needs to be have a length of at least M (number of rows of . - /// - /// The requested of the matrix. - /// - public virtual Complex32 MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) + /// The requested of the matrix. + public virtual double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) { return MatrixNorm(norm, rows, columns, matrix); } diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 10a0dfbc..40fe42f0 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs @@ -410,10 +410,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// public virtual double MatrixNorm(Norm norm, int rows, int columns, double[] matrix) { - var ret = 0.0; switch (norm) { case Norm.OneNorm: + var norm1 = 0d; for (var j = 0; j < columns; j++) { var s = 0.0; @@ -421,23 +421,21 @@ namespace MathNet.Numerics.Providers.LinearAlgebra { s += Math.Abs(matrix[(j*rows) + i]); } - - ret = Math.Max(ret, s); + norm1 = Math.Max(norm1, s); } - - break; + return norm1; case Norm.LargestAbsoluteValue: - + var normMax = 0d; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { - ret = Math.Max(Math.Abs(matrix[(j*rows) + i]), ret); + normMax = Math.Max(Math.Abs(matrix[(j * rows) + i]), normMax); } } - - break; + return normMax; case Norm.InfinityNorm: + var normInf = 0d; for (var i = 0; i < rows; i++) { var s = 0.0; @@ -445,25 +443,21 @@ namespace MathNet.Numerics.Providers.LinearAlgebra { s += Math.Abs(matrix[(j*rows) + i]); } - - ret = Math.Max(ret, s); + normInf = Math.Max(normInf, s); } - - break; + return normInf; case Norm.FrobeniusNorm: var aat = new double[rows*rows]; MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.Transpose, 1.0, matrix, rows, columns, matrix, rows, columns, 0.0, aat); - + var normF = 0d; for (var i = 0; i < rows; i++) { - ret += Math.Abs(aat[(i*rows) + i]); + normF += Math.Abs(aat[(i * rows) + i]); } - - ret = Math.Sqrt(ret); - break; + return Math.Sqrt(normF); + default: + throw new NotSupportedException(); } - - return ret; } /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index 87e7860b..0c935b43 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs @@ -408,62 +408,56 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - public virtual float MatrixNorm(Norm norm, int rows, int columns, float[] matrix) + public virtual double MatrixNorm(Norm norm, int rows, int columns, float[] matrix) { - var ret = 0.0; switch (norm) { case Norm.OneNorm: + var norm1 = 0d; for (var j = 0; j < columns; j++) { - var s = 0.0; + var s = 0d; for (var i = 0; i < rows; i++) { s += Math.Abs(matrix[(j*rows) + i]); } - - ret = Math.Max(ret, s); + norm1 = Math.Max(norm1, s); } - - break; + return norm1; case Norm.LargestAbsoluteValue: - + var normMax = 0d; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { - ret = Math.Max(Math.Abs(matrix[(j*rows) + i]), ret); + normMax = Math.Max(Math.Abs(matrix[(j * rows) + i]), normMax); } } - - break; + return normMax; case Norm.InfinityNorm: + var normInf = 0d; for (var i = 0; i < rows; i++) { - var s = 0.0; + var s = 0d; for (var j = 0; j < columns; j++) { s += Math.Abs(matrix[(j*rows) + i]); } - - ret = Math.Max(ret, s); + normInf = Math.Max(normInf, s); } - - break; + return normInf; case Norm.FrobeniusNorm: var aat = new float[rows*rows]; MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.Transpose, 1.0f, matrix, rows, columns, matrix, rows, columns, 0.0f, aat); - + var normF = 0d; for (var i = 0; i < rows; i++) { - ret += Math.Abs(aat[(i*rows) + i]); + normF += Math.Abs(aat[(i * rows) + i]); } - - ret = Math.Sqrt(ret); - break; + return Math.Sqrt(normF); + default: + throw new NotSupportedException(); } - - return Convert.ToSingle(ret); } /// @@ -478,7 +472,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// /// The requested of the matrix. /// - public virtual float MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) + public virtual double MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) { return MatrixNorm(norm, rows, columns, matrix); } diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs index 1da96c36..98881453 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs @@ -53,7 +53,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override float MatrixNorm(Norm norm, int rows, int columns, float[] matrix) + public override double MatrixNorm(Norm norm, int rows, int columns, float[] matrix) { if (matrix == null) { @@ -92,7 +92,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override float MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) + public override double MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) { if (matrix == null) { @@ -213,7 +213,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override Complex32 MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) + public override double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) { if (matrix == null) { @@ -252,7 +252,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override Complex32 MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) + public override double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) { if (matrix == null) { @@ -293,7 +293,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override Complex MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) + public override double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) { if (matrix == null) { @@ -332,7 +332,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// The requested of the matrix. /// [SecuritySafeCritical] - public override Complex MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) + public override double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) { if (matrix == null) { diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs index 02c5545c..0eee25cf 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs @@ -229,7 +229,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex var matrix = _matrices["Square3x3"]; var work = new double[matrix.RowCount]; var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - Assert.AreEqual(16.5, norm.Real); + Assert.AreEqual(16.5, norm); } /// @@ -265,7 +265,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex var matrix = _matrices["Square3x3"]; var work = new double[18]; var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - Assert.AreEqual(16.5, norm.Real); + Assert.AreEqual(16.5, norm); } /// diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs index 88dc2659..aa5acc83 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs @@ -225,7 +225,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 var matrix = _matrices["Square3x3"]; var work = new float[matrix.RowCount]; var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - Assert.AreEqual(16.5, norm.Real); + Assert.AreEqual(16.5, norm); } /// @@ -261,7 +261,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 var matrix = _matrices["Square3x3"]; var work = new float[18]; var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); - Assert.AreEqual(16.5, norm.Real); + Assert.AreEqual(16.5, norm); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs index 5fb5214e..fa68cad5 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs @@ -124,13 +124,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public virtual void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - AssertHelpers.AlmostEqualRelative(12.5401248f, matrix.L1Norm(), 7); + AssertHelpers.AlmostEqualRelative(12.5401248f, matrix.L1Norm(), 6); matrix = TestMatrices["Wide2x3"]; - AssertHelpers.AlmostEqualRelative(5.8647971f, matrix.L1Norm(), 7); + AssertHelpers.AlmostEqualRelative(5.8647971f, matrix.L1Norm(), 6); matrix = TestMatrices["Tall3x2"]; - AssertHelpers.AlmostEqualRelative(9.4933860f, matrix.L1Norm(), 7); + AssertHelpers.AlmostEqualRelative(9.4933860f, matrix.L1Norm(), 6); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs index 1f1aeb09..9fc6b067 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs @@ -66,13 +66,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public virtual void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; - AssertHelpers.AlmostEqualRelative(10.77775486824598f, matrix.FrobeniusNorm(), 7); + AssertHelpers.AlmostEqualRelative(10.77775486824598f, matrix.FrobeniusNorm(), 6); matrix = TestMatrices["Wide2x3"]; - AssertHelpers.AlmostEqualRelative(4.79478883789474f, matrix.FrobeniusNorm(), 7); + AssertHelpers.AlmostEqualRelative(4.79478883789474f, matrix.FrobeniusNorm(), 6); matrix = TestMatrices["Tall3x2"]; - AssertHelpers.AlmostEqualRelative(7.54122006044115f, matrix.FrobeniusNorm(), 7); + AssertHelpers.AlmostEqualRelative(7.54122006044115f, matrix.FrobeniusNorm(), 6); } /// @@ -98,13 +98,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public virtual void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - Assert.AreEqual(12.1f, matrix.L1Norm()); + AssertHelpers.AlmostEqualRelative(12.1f, matrix.L1Norm(), 6); matrix = TestMatrices["Wide2x3"]; - Assert.AreEqual(5.5f, matrix.L1Norm()); + AssertHelpers.AlmostEqualRelative(5.5f, matrix.L1Norm(), 6); matrix = TestMatrices["Tall3x2"]; - Assert.AreEqual(8.8f, matrix.L1Norm()); + AssertHelpers.AlmostEqualRelative(8.8f, matrix.L1Norm(), 6); } ///