From 48d03189be7204178b04139d9157601a82187fb4 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 20 Oct 2013 15:00:30 +0200 Subject: [PATCH] Providers: drop unused matrix-norm work-array version, simplifications --- src/Numerics/Numerics.csproj | 1 - .../LinearAlgebra/ILinearAlgebraProvider.cs | 27 +- .../ManagedLinearAlgebraProvider.Complex.cs | 17 - .../ManagedLinearAlgebraProvider.Complex32.cs | 15 - .../ManagedLinearAlgebraProvider.Double.cs | 16 - .../ManagedLinearAlgebraProvider.Single.cs | 17 - .../Mkl/MklLinearAlgebraProvider.Complex.cs | 39 +- .../Mkl/MklLinearAlgebraProvider.Complex32.cs | 39 +- .../Mkl/MklLinearAlgebraProvider.Single.cs | 39 +- .../Mkl/MklLinearAlgebraProvider.cs | 367 ------------------ .../Mkl/MklLinearAlgebraProvider.double.cs | 39 +- .../Complex/LinearAlgebraProviderTests.cs | 45 +-- .../Complex32/LinearAlgebraProviderTests.cs | 43 +- .../Double/LinearAlgebraProviderTests.cs | 45 +-- .../Single/LinearAlgebraProviderTests.cs | 45 +-- 15 files changed, 168 insertions(+), 626 deletions(-) delete mode 100644 src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 58b7398b..161cacb3 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -154,7 +154,6 @@ - diff --git a/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs index 1ebcf99f..cda85ac6 100644 --- a/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs +++ b/src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs @@ -35,7 +35,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra #if !NOSYSNUMERICS using Complex = System.Numerics.Complex; - #endif /// @@ -90,10 +89,10 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// Interface to linear algebra algorithms that work off 1-D arrays. /// public interface ILinearAlgebraProvider : - ILinearAlgebraProvider, - ILinearAlgebraProvider, - ILinearAlgebraProvider, - ILinearAlgebraProvider + ILinearAlgebraProvider, + ILinearAlgebraProvider, + ILinearAlgebraProvider, + ILinearAlgebraProvider { } @@ -101,10 +100,8 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// Interface to linear algebra algorithms that work off 1-D arrays. /// /// Supported data types are Double, Single, Complex, and Complex32. - /// Supported data types are Double and Single, must correspond to T. - public interface ILinearAlgebraProvider + public interface ILinearAlgebraProvider where T : struct - where TNorm : struct { /*/// /// Queries the provider for the optimal, workspace block size @@ -203,20 +200,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra /// double MatrixNorm(Norm norm, int rows, int columns, T[] matrix); - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows. - /// The number of columns. - /// 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. - /// - 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 4d59ae68..1fb2f39d 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs @@ -465,23 +465,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } } - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows. - /// The number of columns. - /// 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 double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) - { - return MatrixNorm(norm, rows, columns, matrix); - } - /// /// Multiples two matrices. result = x * y /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index 925cd2f9..c177e110 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs @@ -462,21 +462,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } } - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows. - /// The number of columns. - /// 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 double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) - { - return MatrixNorm(norm, rows, columns, matrix); - } - /// /// Multiples two matrices. result = x * y /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 40fe42f0..9271512f 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs @@ -460,22 +460,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } } - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows. - /// The number of columns. - /// The matrix to compute the norm from. - /// The work array. Not used in the managed provider. - /// - /// The requested of the matrix. - /// - public virtual double MatrixNorm(Norm norm, int rows, int columns, double[] matrix, double[] work) - { - return MatrixNorm(norm, rows, columns, matrix); - } - /// /// Multiples two matrices. result = x * y /// diff --git a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index 0c935b43..e7f5de64 100644 --- a/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs @@ -460,23 +460,6 @@ namespace MathNet.Numerics.Providers.LinearAlgebra } } - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows. - /// The number of columns. - /// 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 double MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) - { - return MatrixNorm(norm, rows, columns, matrix); - } - /// /// Multiples two matrices. result = x * y /// diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex.cs index 40478892..031a55e5 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex.cs @@ -41,8 +41,45 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// /// Intel's Math Kernel Library (MKL) linear algebra provider. /// - public partial class MklLinearAlgebraProvider + public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { + /// + /// Computes the requested of the matrix. + /// + /// The type of norm to compute. + /// The number of rows in the matrix. + /// The number of columns in the matrix. + /// The matrix to compute the norm from. + /// + /// The requested of the matrix. + /// + [SecuritySafeCritical] + public override double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) + { + if (matrix == null) + { + throw new ArgumentNullException("matrix"); + } + + if (rows <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); + } + + if (columns <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); + } + + if (matrix.Length < rows * columns) + { + throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows * columns), "matrix"); + } + + var work = new double[rows]; + return SafeNativeMethods.z_matrix_norm((byte)norm, rows, columns, matrix, work); + } + /// /// Computes the dot product of x and y. /// diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex32.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex32.cs index ea051a08..982e9700 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Complex32.cs @@ -40,8 +40,45 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// /// Intel's Math Kernel Library (MKL) linear algebra provider. /// - public partial class MklLinearAlgebraProvider + public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { + /// + /// Computes the requested of the matrix. + /// + /// The type of norm to compute. + /// The number of rows in the matrix. + /// The number of columns in the matrix. + /// The matrix to compute the norm from. + /// + /// The requested of the matrix. + /// + [SecuritySafeCritical] + public override double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) + { + if (matrix == null) + { + throw new ArgumentNullException("matrix"); + } + + if (rows <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); + } + + if (columns <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); + } + + if (matrix.Length < rows * columns) + { + throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows * columns), "matrix"); + } + + var work = new float[rows]; + return SafeNativeMethods.c_matrix_norm((byte)norm, rows, columns, matrix, work); + } + /// /// Computes the dot product of x and y. /// diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Single.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Single.cs index 7007d983..01118ece 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.Single.cs @@ -41,8 +41,45 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// /// Intel's Math Kernel Library (MKL) linear algebra provider. /// - public partial class MklLinearAlgebraProvider + public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { + /// + /// Computes the requested of the matrix. + /// + /// The type of norm to compute. + /// The number of rows in the matrix. + /// The number of columns in the matrix. + /// The matrix to compute the norm from. + /// + /// The requested of the matrix. + /// + [SecuritySafeCritical] + public override double MatrixNorm(Norm norm, int rows, int columns, float[] matrix) + { + if (matrix == null) + { + throw new ArgumentNullException("matrix"); + } + + if (rows <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); + } + + if (columns <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); + } + + if (matrix.Length < rows * columns) + { + throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows * columns), "matrix"); + } + + var work = new float[rows]; + return SafeNativeMethods.s_matrix_norm((byte)norm, rows, columns, matrix, work); + } + /// /// Computes the dot product of x and y. /// diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs deleted file mode 100644 index 98881453..00000000 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs +++ /dev/null @@ -1,367 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -#if NATIVEMKL - -using MathNet.Numerics.Properties; -using System; -using System.Numerics; -using System.Security; - -namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl -{ - /// - /// Intel's Math Kernel Library (MKL) linear algebra provider. - /// - public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider - { - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// The matrix to compute the norm from. - /// - /// The requested of the matrix. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, float[] matrix) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - var work = new float[rows]; - return MatrixNorm(norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// 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. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, float[] matrix, float[] work) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - if (work.Length < rows) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows), "work"); - } - - return SafeNativeMethods.s_matrix_norm((byte) norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// The matrix to compute the norm from. - /// - /// The requested of the matrix. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, double[] matrix) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - var work = new double[rows]; - return MatrixNorm(norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// 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. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, double[] matrix, double[] work) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - if (work.Length < rows) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows), "work"); - } - - return SafeNativeMethods.d_matrix_norm((byte) norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// The matrix to compute the norm from. - /// - /// The requested of the matrix. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - var work = new float[rows]; - return MatrixNorm(norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// 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. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, Complex32[] matrix, float[] work) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - if (work.Length < rows) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows), "work"); - } - - return SafeNativeMethods.c_matrix_norm((byte) norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// The matrix to compute the norm from. - /// - /// The requested of the matrix. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - var work = new double[rows]; - return MatrixNorm(norm, rows, columns, matrix, work); - } - - /// - /// Computes the requested of the matrix. - /// - /// The type of norm to compute. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// 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. - /// - [SecuritySafeCritical] - public override double MatrixNorm(Norm norm, int rows, int columns, Complex[] matrix, double[] work) - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (rows <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); - } - - if (columns <= 0) - { - throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); - } - - if (matrix.Length < rows*columns) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows*columns), "matrix"); - } - - if (work.Length < rows) - { - throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows), "work"); - } - - return SafeNativeMethods.z_matrix_norm((byte) norm, rows, columns, matrix, work); - } - } -} - -#endif diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.double.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.double.cs index a1092399..5994ff75 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.double.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.double.cs @@ -41,8 +41,45 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// /// Intel's Math Kernel Library (MKL) linear algebra provider. /// - public partial class MklLinearAlgebraProvider + public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { + /// + /// Computes the requested of the matrix. + /// + /// The type of norm to compute. + /// The number of rows in the matrix. + /// The number of columns in the matrix. + /// The matrix to compute the norm from. + /// + /// The requested of the matrix. + /// + [SecuritySafeCritical] + public override double MatrixNorm(Norm norm, int rows, int columns, double[] matrix) + { + if (matrix == null) + { + throw new ArgumentNullException("matrix"); + } + + if (rows <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "rows"); + } + + if (columns <= 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "columns"); + } + + if (matrix.Length < rows * columns) + { + throw new ArgumentException(string.Format(Resources.ArrayTooSmall, rows * columns), "matrix"); + } + + var work = new double[rows]; + return SafeNativeMethods.d_matrix_norm((byte)norm, rows, columns, matrix, work); + } + /// /// Computes the dot product of x and y. /// diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs index 0eee25cf..999fa4b9 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs @@ -203,8 +203,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex public void CanComputeMatrixL1Norm() { var matrix = _matrices["Square3x3"]; - var work = new double[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(12.1, norm, 6); } @@ -215,8 +214,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex public void CanComputeMatrixFrobeniusNorm() { var matrix = _matrices["Square3x3"]; - var work = new double[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(10.777754868246, norm, 8); } @@ -227,44 +225,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex public void CanComputeMatrixInfinityNorm() { 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); - } - - /// - /// Can compute L1 norm using a work array. - /// - [Test] - public void CanComputeMatrixL1NormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(12.1, norm, 6); - } - - /// - /// Can compute Frobenius norm using a work array. - /// - [Test] - public void CanComputeMatrixFrobeniusNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(10.777754868246, norm, 8); - } - - /// - /// Can compute Infinity norm using a work array. - /// - [Test] - public void CanComputeMatrixInfinityNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); Assert.AreEqual(16.5, norm); } diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs index aa5acc83..c7995268 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs @@ -199,8 +199,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 public void CanComputeMatrixL1Norm() { var matrix = _matrices["Square3x3"]; - var work = new float[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(12.1f, norm, 5); } @@ -211,8 +210,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 public void CanComputeMatrixFrobeniusNorm() { var matrix = _matrices["Square3x3"]; - var work = new float[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(10.777754868246f, norm, 5); } @@ -223,43 +221,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 public void CanComputeMatrixInfinityNorm() { 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); - } - - /// - /// Can compute L1 norm using a work array. - /// - [Test] - public void CanComputeMatrixL1NormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new float[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(12.1f, norm, 5); - } - - /// - /// Can compute Frobenius norm using a work array. - /// - [Test] - public void CanComputeMatrixFrobeniusNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new float[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(10.777754868246f, norm, 5); - } - - /// - /// Can compute Infinity norm using a work array. - /// - [Test] - public void CanComputeMatrixInfinityNormWithWorkArray() - { - 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); } diff --git a/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs index 346dd5f0..32a75a8b 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs @@ -197,8 +197,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double public void CanComputeMatrixL1Norm() { var matrix = _matrices["Square3x3"]; - var work = new double[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(12.1, norm, 6); } @@ -209,8 +208,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double public void CanComputeMatrixFrobeniusNorm() { var matrix = _matrices["Square3x3"]; - var work = new double[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(10.777754868246, norm, 8); } @@ -221,44 +219,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double public void CanComputeMatrixInfinityNorm() { 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); - } - - /// - /// Can compute L1 norm using a work array. - /// - [Test] - public void CanComputeMatrixL1NormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(12.1, norm, 6); - } - - /// - /// Can compute Frobenius norm using a work array. - /// - [Test] - public void CanComputeMatrixFrobeniusNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(10.777754868246, norm, 8); - } - - /// - /// Can compute Infinity norm using a work array. - /// - [Test] - public void CanComputeMatrixInfinityNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new double[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); Assert.AreEqual(16.5, norm); } diff --git a/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs index 4480759c..0a339840 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs @@ -197,8 +197,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single public void CanComputeMatrixL1Norm() { var matrix = _matrices["Square3x3"]; - var work = new float[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqualRelative(12.1, norm, 5); } @@ -209,8 +208,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single public void CanComputeMatrixFrobeniusNorm() { var matrix = _matrices["Square3x3"]; - var work = new float[matrix.RowCount]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); AssertHelpers.AlmostEqual(10.777754868246, norm, 5); } @@ -221,44 +219,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single public void CanComputeMatrixInfinityNorm() { 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); - } - - /// - /// Can compute L1 norm using a work array. - /// - [Test] - public void CanComputeMatrixL1NormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new float[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.OneNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqualRelative(12.1, norm, 5); - } - - /// - /// Can compute Frobenius norm using a work array. - /// - [Test] - public void CanComputeMatrixFrobeniusNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new float[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); - AssertHelpers.AlmostEqual(10.777754868246, norm, 5); - } - - /// - /// Can compute Infinity norm using a work array. - /// - [Test] - public void CanComputeMatrixInfinityNormWithWorkArray() - { - var matrix = _matrices["Square3x3"]; - var work = new float[18]; - var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values, work); + var norm = Control.LinearAlgebraProvider.MatrixNorm(Norm.InfinityNorm, matrix.RowCount, matrix.ColumnCount, matrix.Values); Assert.AreEqual(16.5, norm); }