From af476c29f1737cf288aa5cbcf4b5e24c3602c3dd Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 7 Apr 2013 01:37:01 +0200 Subject: [PATCH] LA: matrix construction rework #104 --- src/Examples/LinearAlgebra/DirectSolvers.cs | 2 +- .../LinearAlgebra/Factorization/Cholesky.cs | 2 +- .../LinearAlgebra/Factorization/Evd.cs | 2 +- .../LinearAlgebra/Factorization/LU.cs | 2 +- .../LinearAlgebra/Factorization/QR.cs | 2 +- .../LinearAlgebra/Factorization/Svd.cs | 2 +- .../IterativeSolvers/BiCgStabSolver.cs | 2 +- .../CompositeSolverExample.cs | 2 +- .../IterativeSolvers/GpBiCgSolver.cs | 2 +- .../IterativeSolvers/MlkBiCgStabSolver.cs | 2 +- .../IterativeSolvers/TFQMRSolver.cs | 2 +- .../MatrixArithmeticOperations.cs | 4 +- .../LinearAlgebra/MatrixInitialization.cs | 2 +- src/Examples/LinearAlgebra/MatrixNorms.cs | 2 +- src/FSharp/LinearAlgebra.Double.Matrix.fs | 2 +- src/FSharpUnitTests/DenseMatrixTests.fs | 4 +- src/FSharpUnitTests/MatrixTests.fs | 4 +- .../Distributions/Multivariate/Wishart.cs | 2 +- .../LinearAlgebra/Complex/DenseMatrix.cs | 77 ++++++++++----- .../LinearAlgebra/Complex/DenseVector.cs | 2 +- .../Complex/Solvers/Preconditioners/Ilutp.cs | 2 +- .../Solvers/Preconditioners/IncompleteLU.cs | 2 +- .../LinearAlgebra/Complex/SparseMatrix.cs | 95 +++++++++++++++---- .../LinearAlgebra/Complex/SparseVector.cs | 2 +- .../LinearAlgebra/Complex32/DenseMatrix.cs | 77 ++++++++++----- .../LinearAlgebra/Complex32/DenseVector.cs | 2 +- .../Solvers/Preconditioners/Ilutp.cs | 2 +- .../Solvers/Preconditioners/IncompleteLU.cs | 2 +- .../LinearAlgebra/Complex32/SparseMatrix.cs | 95 +++++++++++++++---- .../LinearAlgebra/Complex32/SparseVector.cs | 2 +- .../LinearAlgebra/Double/DenseMatrix.cs | 77 ++++++++++----- .../LinearAlgebra/Double/DenseVector.cs | 2 +- .../Double/Solvers/Preconditioners/Ilutp.cs | 2 +- .../Solvers/Preconditioners/IncompleteLU.cs | 2 +- .../LinearAlgebra/Double/SparseMatrix.cs | 95 +++++++++++++++---- .../LinearAlgebra/Double/SparseVector.cs | 2 +- .../LinearAlgebra/Single/DenseMatrix.cs | 77 ++++++++++----- .../LinearAlgebra/Single/DenseVector.cs | 2 +- .../Single/Solvers/Preconditioners/Ilutp.cs | 2 +- .../Solvers/Preconditioners/IncompleteLU.cs | 2 +- .../LinearAlgebra/Single/SparseMatrix.cs | 95 +++++++++++++++---- .../LinearAlgebra/Single/SparseVector.cs | 2 +- .../Storage/DenseColumnMajorMatrixStorage.cs | 2 +- .../Storage/DenseVectorStorage.cs | 2 +- .../SparseCompressedRowMatrixStorage.cs | 2 +- .../Storage/SparseVectorStorage.cs | 2 +- .../Complex/LinearAlgebraProviderTests.cs | 12 +-- .../Complex32/LinearAlgebraProviderTests.cs | 12 +-- .../Double/LinearAlgebraProviderTests.cs | 12 +-- .../Single/LinearAlgebraProviderTests.cs | 12 +-- .../Complex/DenseMatrixTests.cs | 28 +++--- .../Complex/DiagonalMatrixTests.cs | 81 ++++++++-------- .../Complex/IO/DelimitedWriterTests.cs | 66 +++++++------ .../Complex/MatrixStructureTheory.cs | 57 ++++++++--- .../Complex/SparseMatrixTests.cs | 64 +++++++------ .../Complex32/DenseMatrixTests.cs | 32 ++++--- .../Complex32/DiagonalMatrixTests.cs | 85 ++++++++--------- .../Complex32/IO/DelimitedWriterTests.cs | 66 +++++++------ .../Complex32/MatrixStructureTheory.cs | 57 ++++++++--- .../Complex32/SparseMatrixTests.cs | 64 +++++++------ .../Double/DenseMatrixTests.cs | 28 +++--- .../Double/DiagonalMatrixTests.cs | 94 +++++++++--------- .../Double/IO/DelimitedWriterTests.cs | 66 +++++++------ .../Double/MatrixStructureTheory.cs | 57 ++++++++--- .../Double/SparseMatrixTests.cs | 64 +++++++------ .../Single/DenseMatrixTests.cs | 32 ++++--- .../Single/DiagonalMatrixTests.cs | 85 ++++++++--------- .../Single/IO/DelimitedWriterTests.cs | 67 +++++++------ .../Single/MatrixStructureTheory.cs | 57 ++++++++--- .../Single/SparseMatrixTests.cs | 64 +++++++------ 70 files changed, 1280 insertions(+), 752 deletions(-) diff --git a/src/Examples/LinearAlgebra/DirectSolvers.cs b/src/Examples/LinearAlgebra/DirectSolvers.cs index 3d841289..30d10565 100644 --- a/src/Examples/LinearAlgebra/DirectSolvers.cs +++ b/src/Examples/LinearAlgebra/DirectSolvers.cs @@ -73,7 +73,7 @@ namespace Examples.LinearAlgebraExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/Factorization/Cholesky.cs b/src/Examples/LinearAlgebra/Factorization/Cholesky.cs index e92a0729..b886098e 100644 --- a/src/Examples/LinearAlgebra/Factorization/Cholesky.cs +++ b/src/Examples/LinearAlgebra/Factorization/Cholesky.cs @@ -70,7 +70,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples formatProvider.TextInfo.ListSeparator = " "; // Create square, symmetric, positive definite matrix - var matrix = new DenseMatrix(new[,] { { 2.0, 1.0 }, { 1.0, 2.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 2.0, 1.0 }, { 1.0, 2.0 } }); Console.WriteLine(@"Initial square, symmetric, positive definite matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/Factorization/Evd.cs b/src/Examples/LinearAlgebra/Factorization/Evd.cs index 93576bfe..88ee8dfb 100644 --- a/src/Examples/LinearAlgebra/Factorization/Evd.cs +++ b/src/Examples/LinearAlgebra/Factorization/Evd.cs @@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples formatProvider.TextInfo.ListSeparator = " "; // Create square symmetric matrix - var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } }); Console.WriteLine(@"Initial square symmetric matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/Factorization/LU.cs b/src/Examples/LinearAlgebra/Factorization/LU.cs index a43d2f1d..858f32e3 100644 --- a/src/Examples/LinearAlgebra/Factorization/LU.cs +++ b/src/Examples/LinearAlgebra/Factorization/LU.cs @@ -73,7 +73,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples formatProvider.TextInfo.ListSeparator = " "; // Create square matrix - var matrix = new DenseMatrix(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } }); Console.WriteLine(@"Initial square matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/Factorization/QR.cs b/src/Examples/LinearAlgebra/Factorization/QR.cs index ae55d600..5b7e5f25 100644 --- a/src/Examples/LinearAlgebra/Factorization/QR.cs +++ b/src/Examples/LinearAlgebra/Factorization/QR.cs @@ -72,7 +72,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples formatProvider.TextInfo.ListSeparator = " "; // Create 3 x 2 matrix - var matrix = new DenseMatrix(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } }); Console.WriteLine(@"Initial 3x2 matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/Factorization/Svd.cs b/src/Examples/LinearAlgebra/Factorization/Svd.cs index ba88bbb5..3685b0c0 100644 --- a/src/Examples/LinearAlgebra/Factorization/Svd.cs +++ b/src/Examples/LinearAlgebra/Factorization/Svd.cs @@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples formatProvider.TextInfo.ListSeparator = " "; // Create square matrix - var matrix = new DenseMatrix(new[,] { { 4.0, 1.0 }, { 3.0, 2.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 4.0, 1.0 }, { 3.0, 2.0 } }); Console.WriteLine(@"Initial square matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs index 0132db9a..799f5e7f 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs @@ -77,7 +77,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs index 4d41d7ad..3ed08287 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs @@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs index 438cfb67..d4d0b49d 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs @@ -75,7 +75,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs index 94ec5d7a..3b20aafb 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs @@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs index aa6b4d2e..1b9f908d 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs @@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients - var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs b/src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs index a5437941..13f878fa 100644 --- a/src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs +++ b/src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs @@ -73,13 +73,13 @@ namespace Examples.LinearAlgebraExamples formatProvider.TextInfo.ListSeparator = " "; // Create matrix "A" - var matrixA = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } }); + var matrixA = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } }); Console.WriteLine(@"Matrix A"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // Create matrix "B" - var matrixB = new DenseMatrix(new[,] { { 1.0, 3.0, 5.0 }, { 2.0, 4.0, 6.0 }, { 3.0, 5.0, 7.0 } }); + var matrixB = DenseMatrix.OfArray(new[,] { { 1.0, 3.0, 5.0 }, { 2.0, 4.0, 6.0 }, { 3.0, 5.0, 7.0 } }); Console.WriteLine(@"Matrix B"); Console.WriteLine(matrixB.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/MatrixInitialization.cs b/src/Examples/LinearAlgebra/MatrixInitialization.cs index 09588f9e..e48c919b 100644 --- a/src/Examples/LinearAlgebra/MatrixInitialization.cs +++ b/src/Examples/LinearAlgebra/MatrixInitialization.cs @@ -63,7 +63,7 @@ namespace Examples.LinearAlgebraExamples public void Run() { // 1. Initialize a new instance of the matrix from a 2D array. This constructor will allocate a completely new memory block for storing the dense matrix. - var matrix1 = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }); + var matrix1 = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }); // 2. Initialize a new instance of the empty square matrix with a given order. var matrix2 = new DenseMatrix(3); diff --git a/src/Examples/LinearAlgebra/MatrixNorms.cs b/src/Examples/LinearAlgebra/MatrixNorms.cs index b47e4e18..e6afe985 100644 --- a/src/Examples/LinearAlgebra/MatrixNorms.cs +++ b/src/Examples/LinearAlgebra/MatrixNorms.cs @@ -68,7 +68,7 @@ namespace Examples.LinearAlgebraExamples formatProvider.TextInfo.ListSeparator = " "; // Create square matrix - var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 6.0, 5.0, 4.0 }, { 8.0, 9.0, 7.0 } }); + var matrix = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 6.0, 5.0, 4.0 }, { 8.0, 9.0, 7.0 } }); Console.WriteLine(@"Initial square matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index b1a5e497..f7b1c19f 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -275,7 +275,7 @@ module DenseMatrix = A /// Create a matrix from a 2D array of floating point numbers. - let inline ofArray2 (arr: float[,]) = new DenseMatrix(arr) + let inline ofArray2 (arr: float[,]) = DenseMatrix.OfArray(arr) /// Create a matrix with the given entries. let inline initDense (n: int) (m: int) (es: #seq) = diff --git a/src/FSharpUnitTests/DenseMatrixTests.fs b/src/FSharpUnitTests/DenseMatrixTests.fs index 992fefc8..304e921b 100644 --- a/src/FSharpUnitTests/DenseMatrixTests.fs +++ b/src/FSharpUnitTests/DenseMatrixTests.fs @@ -9,10 +9,10 @@ open MathNet.Numerics.LinearAlgebra.Double module DenseMatrixTests = /// A small uniform vector. - let smallM = new DenseMatrix( Array2D.create 2 2 0.3 ) + let smallM = DenseMatrix.OfArray( Array2D.create 2 2 0.3 ) /// A large vector with increasingly large entries - let largeM = new DenseMatrix( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) + let largeM = DenseMatrix.OfArray( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) [] let ``DenseMatrix.init`` () = diff --git a/src/FSharpUnitTests/MatrixTests.fs b/src/FSharpUnitTests/MatrixTests.fs index 13a74f5c..c9b32d77 100644 --- a/src/FSharpUnitTests/MatrixTests.fs +++ b/src/FSharpUnitTests/MatrixTests.fs @@ -9,11 +9,11 @@ open MathNet.Numerics.LinearAlgebra.Double module MatrixTests = /// A small uniform vector. - let smallM = new DenseMatrix( Array2D.create 2 2 0.3 ) + let smallM = DenseMatrix.OfArray( Array2D.create 2 2 0.3 ) let failingFoldBackM = DenseMatrix.init 2 3 (fun i j -> 1.0) /// A large vector with increasingly large entries - let largeM = new DenseMatrix( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) + let largeM = DenseMatrix.OfArray( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) [] let ``Matrix.GetSlice`` () = diff --git a/src/Numerics/Distributions/Multivariate/Wishart.cs b/src/Numerics/Distributions/Multivariate/Wishart.cs index ee205e91..17822975 100644 --- a/src/Numerics/Distributions/Multivariate/Wishart.cs +++ b/src/Numerics/Distributions/Multivariate/Wishart.cs @@ -313,7 +313,7 @@ namespace MathNet.Numerics.Distributions // First generate a lower triangular matrix with Sqrt(Chi-Squares) on the diagonal // and normal distributed variables in the lower triangle. - var a = new DenseMatrix(count, count, 0.0); + var a = new DenseMatrix(count, count); for (var d = 0; d < count; d++) { a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (nu - d)/2.0, 0.5))); diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 11641275..3db59612 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to be in column-major order (column by column) and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public DenseMatrix(int rows, int columns, Complex[] storage) + : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) + { + } + + /// + /// Create a new dense matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfMatrix(Matrix matrix) + { + var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfArray(Complex[,] array) + { + var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.Data[(j * storage.RowCount) + i] = array[i, j]; + } + } + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable columnMajor) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); + } + /// /// Create a new dense matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to the provided value. /// Zero-length matrices are not supported. /// /// If the row or column count is less than one. + [Obsolete("Scheduled for removal in v3.0.")] public DenseMatrix(int rows, int columns, Complex value) : this(rows, columns) { @@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } - /// - /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. - /// The array is assumed to be in column-major order (column by column) and is used directly without copying. - /// Very efficient, but changes to the array and the matrix will affect each other. - /// - /// - public DenseMatrix(int rows, int columns, Complex[] storage) - : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) - { - } - /// /// Create a new dense matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public DenseMatrix(Complex[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } } - - /// - /// Create a new dense matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in column-major order (column by column). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public DenseMatrix(int rows, int columns, IEnumerable columnMajor) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) - { - } /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index e3292794..37b037b1 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// A new memory block will be allocated for storing the vector. /// public DenseVector(IEnumerable other) - : this(DenseVectorStorage.FromEnumerable(other)) + : this(DenseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs index 1f7f01ec..dddf4e16 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs @@ -314,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix); + var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : SparseMatrix.OfMatrix(matrix); // The creation of the preconditioner follows the following algorithm. // spaceLeft = lfilNnz * nnz(A) diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs index b9357ee7..94631b18 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs @@ -112,7 +112,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - _decompositionLU = new SparseMatrix(matrix); + _decompositionLU = SparseMatrix.OfMatrix(matrix); // M == A // for i = 2, ... , n do diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index f5626fca..7b970775 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -70,6 +70,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex _storage = storage; } + /// + /// Create a new square sparse matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + /// If the order is less than one. + public SparseMatrix(int order) + : this(order, order) + { + } + /// /// Create a new sparse matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to zero. @@ -82,14 +93,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Create a new square sparse matrix with the given number of rows and columns. - /// All cells of the matrix will be initialized to zero. - /// Zero-length matrices are not supported. + /// Create a new sparse matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. /// - /// If the order is less than one. - public SparseMatrix(int order) - : this(order, order) + public static SparseMatrix OfMatrix(Matrix matrix) + { + var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfArray(Complex[,] array) + { + var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.At(i, j, array[i, j]); + } + } + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + /// + public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable rowMajor) { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); + } + + /// + /// Create a new sparse matrix with the given number of rows and columns as a copy of the given array. + /// The array is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + /// + public static SparseMatrix OfColumnMajor(int rows, int columns, Complex[] array) + { + if (rows * columns > array.Length) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); + } + + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns; j++) + { + storage.At(i, j, array[i + (j * rows)]); + } + } + return new SparseMatrix(storage); } /// @@ -137,7 +204,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - /// + /// + [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, Complex[] array) : this(rows, columns) { @@ -160,6 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public SparseMatrix(Complex[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -172,22 +241,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } - /// - /// Create a new sparse matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in row-major order (row by row). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public SparseMatrix(int rows, int columns, IEnumerable rowMajor) - : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) - { - } - /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index c5227e10..322f6b49 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -124,7 +124,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// A new memory block will be allocated for storing the vector. /// public SparseVector(IEnumerable other) - : this(SparseVectorStorage.FromEnumerable(other)) + : this(SparseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 632565cf..af183991 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to be in column-major order (column by column) and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public DenseMatrix(int rows, int columns, Complex32[] storage) + : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) + { + } + + /// + /// Create a new dense matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfMatrix(Matrix matrix) + { + var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfArray(Complex32[,] array) + { + var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.Data[(j * storage.RowCount) + i] = array[i, j]; + } + } + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable columnMajor) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); + } + /// /// Create a new dense matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to the provided value. /// Zero-length matrices are not supported. /// /// If the row or column count is less than one. + [Obsolete("Scheduled for removal in v3.0.")] public DenseMatrix(int rows, int columns, Complex32 value) : this(rows, columns) { @@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } - /// - /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. - /// The array is assumed to be in column-major order (column by column) and is used directly without copying. - /// Very efficient, but changes to the array and the matrix will affect each other. - /// - /// - public DenseMatrix(int rows, int columns, Complex32[] storage) - : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) - { - } - /// /// Create a new dense matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public DenseMatrix(Complex32[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } } - - /// - /// Create a new dense matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in column-major order (column by column). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public DenseMatrix(int rows, int columns, IEnumerable columnMajor) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) - { - } /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index d412d767..02858aa4 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// A new memory block will be allocated for storing the vector. /// public DenseVector(IEnumerable other) - : this(DenseVectorStorage.FromEnumerable(other)) + : this(DenseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs index 9e2f60c4..fcae64d4 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs @@ -314,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix); + var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : SparseMatrix.OfMatrix(matrix); // The creation of the preconditioner follows the following algorithm. // spaceLeft = lfilNnz * nnz(A) diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs index 7c908554..0a919276 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs @@ -112,7 +112,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - _decompositionLU = new SparseMatrix(matrix); + _decompositionLU = SparseMatrix.OfMatrix(matrix); // M == A // for i = 2, ... , n do diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index d61b6b6f..27b833bb 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -70,6 +70,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 _storage = storage; } + /// + /// Create a new square sparse matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + /// If the order is less than one. + public SparseMatrix(int order) + : this(order, order) + { + } + /// /// Create a new sparse matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to zero. @@ -82,14 +93,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Create a new square sparse matrix with the given number of rows and columns. - /// All cells of the matrix will be initialized to zero. - /// Zero-length matrices are not supported. + /// Create a new sparse matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. /// - /// If the order is less than one. - public SparseMatrix(int order) - : this(order, order) + public static SparseMatrix OfMatrix(Matrix matrix) + { + var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfArray(Complex32[,] array) + { + var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.At(i, j, array[i, j]); + } + } + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + /// + public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable rowMajor) { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); + } + + /// + /// Create a new sparse matrix with the given number of rows and columns as a copy of the given array. + /// The array is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + /// + public static SparseMatrix OfColumnMajor(int rows, int columns, Complex32[] array) + { + if (rows * columns > array.Length) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); + } + + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns; j++) + { + storage.At(i, j, array[i + (j * rows)]); + } + } + return new SparseMatrix(storage); } /// @@ -137,7 +204,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - /// + /// + [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, Complex32[] array) : this(rows, columns) { @@ -160,6 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public SparseMatrix(Complex32[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -172,22 +241,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } - /// - /// Create a new sparse matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in row-major order (row by row). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public SparseMatrix(int rows, int columns, IEnumerable rowMajor) - : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) - { - } - /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index cb778fd8..c82be4cb 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -124,7 +124,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// A new memory block will be allocated for storing the vector. /// public SparseVector(IEnumerable other) - : this(SparseVectorStorage.FromEnumerable(other)) + : this(SparseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 50c30a09..78d9b9d9 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to be in column-major order (column by column) and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public DenseMatrix(int rows, int columns, double[] storage) + : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) + { + } + + /// + /// Create a new dense matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfMatrix(Matrix matrix) + { + var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfArray(double[,] array) + { + var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.Data[(j * storage.RowCount) + i] = array[i, j]; + } + } + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable columnMajor) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); + } + /// /// Create a new dense matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to the provided value. /// Zero-length matrices are not supported. /// /// If the row or column count is less than one. + [Obsolete("Scheduled for removal in v3.0.")] public DenseMatrix(int rows, int columns, double value) : this(rows, columns) { @@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. - /// The array is assumed to be in column-major order (column by column) and is used directly without copying. - /// Very efficient, but changes to the array and the matrix will affect each other. - /// - /// - public DenseMatrix(int rows, int columns, double[] storage) - : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) - { - } - /// /// Create a new dense matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public DenseMatrix(double[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } } - - /// - /// Create a new dense matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in column-major order (column by column). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public DenseMatrix(int rows, int columns, IEnumerable columnMajor) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) - { - } /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index b7b0c2dc..6f797b88 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -114,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A new memory block will be allocated for storing the vector. /// public DenseVector(IEnumerable other) - : this(DenseVectorStorage.FromEnumerable(other)) + : this(DenseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs index ae84cacd..ec5e567c 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs @@ -313,7 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix); + var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : SparseMatrix.OfMatrix(matrix); // The creation of the preconditioner follows the following algorithm. // spaceLeft = lfilNnz * nnz(A) diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs index ba592e70..3b97f71c 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs @@ -111,7 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - _decompositionLU = new SparseMatrix(matrix); + _decompositionLU = SparseMatrix.OfMatrix(matrix); // M == A // for i = 2, ... , n do diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 8aff8f54..66992346 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -69,6 +69,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double _storage = storage; } + /// + /// Create a new square sparse matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + /// If the order is less than one. + public SparseMatrix(int order) + : this(order, order) + { + } + /// /// Create a new sparse matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to zero. @@ -81,14 +92,70 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Create a new square sparse matrix with the given number of rows and columns. - /// All cells of the matrix will be initialized to zero. - /// Zero-length matrices are not supported. + /// Create a new sparse matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. /// - /// If the order is less than one. - public SparseMatrix(int order) - : this(order, order) + public static SparseMatrix OfMatrix(Matrix matrix) + { + var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfArray(double[,] array) + { + var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.At(i, j, array[i, j]); + } + } + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + /// + public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable rowMajor) { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); + } + + /// + /// Create a new sparse matrix with the given number of rows and columns as a copy of the given array. + /// The array is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + /// + public static SparseMatrix OfColumnMajor(int rows, int columns, double[] array) + { + if (rows * columns > array.Length) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); + } + + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns; j++) + { + storage.At(i, j, array[i + (j * rows)]); + } + } + return new SparseMatrix(storage); } /// @@ -136,7 +203,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - /// + /// + [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, double[] array) : this(rows, columns) { @@ -159,6 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public SparseMatrix(double[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -171,22 +240,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Create a new sparse matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in row-major order (row by row). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public SparseMatrix(int rows, int columns, IEnumerable rowMajor) - : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) - { - } - /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 962898b9..08be4091 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -124,7 +124,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A new memory block will be allocated for storing the vector. /// public SparseVector(IEnumerable other) - : this(SparseVectorStorage.FromEnumerable(other)) + : this(SparseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 75466261..caaaaf1b 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to be in column-major order (column by column) and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public DenseMatrix(int rows, int columns, float[] storage) + : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) + { + } + + /// + /// Create a new dense matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfMatrix(Matrix matrix) + { + var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfArray(float[,] array) + { + var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.Data[(j * storage.RowCount) + i] = array[i, j]; + } + } + return new DenseMatrix(storage); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable columnMajor) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); + } + /// /// Create a new dense matrix with the given number of rows and columns. /// All cells of the matrix will be initialized to the provided value. /// Zero-length matrices are not supported. /// /// If the row or column count is less than one. + [Obsolete("Scheduled for removal in v3.0.")] public DenseMatrix(int rows, int columns, float value) : this(rows, columns) { @@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. - /// The array is assumed to be in column-major order (column by column) and is used directly without copying. - /// Very efficient, but changes to the array and the matrix will affect each other. - /// - /// - public DenseMatrix(int rows, int columns, float[] storage) - : this(new DenseColumnMajorMatrixStorage(rows, columns, storage)) - { - } - /// /// Create a new dense matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public DenseMatrix(float[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -146,22 +189,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Create a new dense matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in column-major order (column by column). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public DenseMatrix(int rows, int columns, IEnumerable columnMajor) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) - { - } - /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 2567f742..a0e4fd1e 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// A new memory block will be allocated for storing the vector. /// public DenseVector(IEnumerable other) - : this(DenseVectorStorage.FromEnumerable(other)) + : this(DenseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs index 161956f0..8198d799 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs @@ -313,7 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix); + var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : SparseMatrix.OfMatrix(matrix); // The creation of the preconditioner follows the following algorithm. // spaceLeft = lfilNnz * nnz(A) diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs index ade5f1ba..8c720692 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs @@ -111,7 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix"); } - _decompositionLU = new SparseMatrix(matrix); + _decompositionLU = SparseMatrix.OfMatrix(matrix); // M == A // for i = 2, ... , n do diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index ea482214..7729c0c7 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -68,6 +68,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single { _storage = storage; } + + /// + /// Create a new square sparse matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + /// If the order is less than one. + public SparseMatrix(int order) + : this(order, order) + { + } /// /// Create a new sparse matrix with the given number of rows and columns. @@ -81,14 +92,70 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Create a new square sparse matrix with the given number of rows and columns. - /// All cells of the matrix will be initialized to zero. - /// Zero-length matrices are not supported. + /// Create a new sparse matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. /// - /// If the order is less than one. - public SparseMatrix(int order) - : this(order, order) + public static SparseMatrix OfMatrix(Matrix matrix) { + var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.Storage.CopyToUnchecked(storage, skipClearing: true); + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfArray(float[,] array) + { + var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); + for (var i = 0; i < storage.RowCount; i++) + { + for (var j = 0; j < storage.ColumnCount; j++) + { + storage.At(i, j, array[i, j]); + } + } + return new SparseMatrix(storage); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + /// + public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable rowMajor) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); + } + + /// + /// Create a new sparse matrix with the given number of rows and columns as a copy of the given array. + /// The array is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + /// + public static SparseMatrix OfColumnMajor(int rows, int columns, float[] array) + { + if (rows * columns > array.Length) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); + } + + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + for (var i = 0; i < rows; i++) + { + for (var j = 0; j < columns; j++) + { + storage.At(i, j, array[i + (j * rows)]); + } + } + return new SparseMatrix(storage); } /// @@ -136,7 +203,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - /// + /// + [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, float[] array) : this(rows, columns) { @@ -159,6 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")] public SparseMatrix(float[,] array) : this(array.GetLength(0), array.GetLength(1)) { @@ -171,22 +240,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Create a new sparse matrix as a copy of the given enumerable. - /// The enumerable is assumed to be in row-major order (row by row). - /// This new matrix will be independent from the enumerable. - /// A new memory block will be allocated for storing the vector. - /// - public SparseMatrix(int rows, int columns, IEnumerable rowMajor) - : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) - { - } - /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 71806e1c..433b7f24 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -124,7 +124,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// A new memory block will be allocated for storing the vector. /// public SparseVector(IEnumerable other) - : this(SparseVectorStorage.FromEnumerable(other)) + : this(SparseVectorStorage.OfEnumerable(other)) { } diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index 309e9a9a..e81263e0 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -101,7 +101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION - public static DenseColumnMajorMatrixStorage FromColumnMajorEnumerable(int rows, int columns, IEnumerable data) + public static DenseColumnMajorMatrixStorage OfColumnMajorEnumerable(int rows, int columns, IEnumerable data) { if (data == null) { diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index ab019454..0feb5e58 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION - public static DenseVectorStorage FromEnumerable(IEnumerable data) + public static DenseVectorStorage OfEnumerable(IEnumerable data) { if (data == null) { diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 6371d17f..e59b6eff 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -363,7 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION - public static SparseCompressedRowMatrixStorage FromRowMajorEnumerable(int rows, int columns, IEnumerable data) + public static SparseCompressedRowMatrixStorage OfRowMajorEnumerable(int rows, int columns, IEnumerable data) { if (data == null) { diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index d19e1859..92863756 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -284,7 +284,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION - public static SparseVectorStorage FromEnumerable(IEnumerable data) + public static SparseVectorStorage OfEnumerable(IEnumerable data) { if (data == null) { diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs index 989bcb3d..3acecbea 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs @@ -63,12 +63,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex /// readonly IDictionary _matrices = new Dictionary { - {"Singular3x3", new DenseMatrix(new[,] {{new Complex(1.0, 0), 1.0, 2.0}, {1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}})}, - {"Square3x3", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2, -3.3}, {0.0, 1.1, 2.2}, {-4.4, 5.5, 6.6}})}, - {"Square4x4", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2, -3.3, -4.4}, {0.0, 1.1, 2.2, 3.3}, {1.0, 2.1, 6.2, 4.3}, {-4.4, 5.5, 6.6, -7.7}})}, - {"Singular4x4", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}})}, - {"Tall3x2", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, - {"Wide2x3", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2, -3.3}, {0.0, 1.1, 2.2}})}, + {"Singular3x3", DenseMatrix.OfArray(new[,] {{new Complex(1.0, 0), 1.0, 2.0}, {1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}})}, + {"Square3x3", DenseMatrix.OfArray(new[,] {{new Complex(-1.1, 0), -2.2, -3.3}, {0.0, 1.1, 2.2}, {-4.4, 5.5, 6.6}})}, + {"Square4x4", DenseMatrix.OfArray(new[,] {{new Complex(-1.1, 0), -2.2, -3.3, -4.4}, {0.0, 1.1, 2.2, 3.3}, {1.0, 2.1, 6.2, 4.3}, {-4.4, 5.5, 6.6, -7.7}})}, + {"Singular4x4", DenseMatrix.OfArray(new[,] {{new Complex(-1.1, 0), -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}})}, + {"Tall3x2", DenseMatrix.OfArray(new[,] {{new Complex(-1.1, 0), -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, + {"Wide2x3", DenseMatrix.OfArray(new[,] {{new Complex(-1.1, 0), -2.2, -3.3}, {0.0, 1.1, 2.2}})}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} diff --git a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs index 242b9e0e..0db5efc5 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs @@ -63,12 +63,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 /// readonly IDictionary _matrices = new Dictionary { - {"Singular3x3", new DenseMatrix(new[,] {{new Complex32(1.0f, 0.0f), 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}})}, - {"Square3x3", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}})}, - {"Square4x4", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f, -4.4f}, {0.0f, 1.1f, 2.2f, 3.3f}, {1.0f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}})}, - {"Singular4x4", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}})}, - {"Tall3x2", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, - {"Wide2x3", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}})}, + {"Singular3x3", DenseMatrix.OfArray(new[,] {{new Complex32(1.0f, 0.0f), 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}})}, + {"Square3x3", DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}})}, + {"Square4x4", DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f, -4.4f}, {0.0f, 1.1f, 2.2f, 3.3f}, {1.0f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}})}, + {"Singular4x4", DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}})}, + {"Tall3x2", DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, + {"Wide2x3", DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}})}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} diff --git a/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs index ab2247fd..a6c46a71 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs @@ -62,12 +62,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double /// readonly IDictionary _matrices = new Dictionary { - {"Singular3x3", new DenseMatrix(new[,] {{1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}})}, - {"Square3x3", new DenseMatrix(new[,] {{-1.1, -2.2, -3.3}, {0.0, 1.1, 2.2}, {-4.4, 5.5, 6.6}})}, - {"Square4x4", new DenseMatrix(new[,] {{-1.1, -2.2, -3.3, -4.4}, {0.0, 1.1, 2.2, 3.3}, {1.0, 2.1, 6.2, 4.3}, {-4.4, 5.5, 6.6, -7.7}})}, - {"Singular4x4", new DenseMatrix(new[,] {{-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}})}, - {"Tall3x2", new DenseMatrix(new[,] {{-1.1, -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, - {"Wide2x3", new DenseMatrix(new[,] {{-1.1, -2.2, -3.3}, {0.0, 1.1, 2.2}})}, + {"Singular3x3", DenseMatrix.OfArray(new[,] {{1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}})}, + {"Square3x3", DenseMatrix.OfArray(new[,] {{-1.1, -2.2, -3.3}, {0.0, 1.1, 2.2}, {-4.4, 5.5, 6.6}})}, + {"Square4x4", DenseMatrix.OfArray(new[,] {{-1.1, -2.2, -3.3, -4.4}, {0.0, 1.1, 2.2, 3.3}, {1.0, 2.1, 6.2, 4.3}, {-4.4, 5.5, 6.6, -7.7}})}, + {"Singular4x4", DenseMatrix.OfArray(new[,] {{-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}, {-1.1, -2.2, -3.3, -4.4}})}, + {"Tall3x2", DenseMatrix.OfArray(new[,] {{-1.1, -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, + {"Wide2x3", DenseMatrix.OfArray(new[,] {{-1.1, -2.2, -3.3}, {0.0, 1.1, 2.2}})}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} diff --git a/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs b/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs index 49dbd85a..2ca1c506 100644 --- a/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs +++ b/src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs @@ -62,12 +62,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single /// readonly IDictionary _matrices = new Dictionary { - {"Singular3x3", new DenseMatrix(new[,] {{1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}})}, - {"Square3x3", new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}})}, - {"Square4x4", new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {0.0f, 1.1f, 2.2f, 3.3f}, {1.0f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}})}, - {"Singular4x4", new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}})}, - {"Tall3x2", new DenseMatrix(new[,] {{-1.1f, -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, - {"Wide2x3", new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}})}, + {"Singular3x3", DenseMatrix.OfArray(new[,] {{1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}})}, + {"Square3x3", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}})}, + {"Square4x4", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {0.0f, 1.1f, 2.2f, 3.3f}, {1.0f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}})}, + {"Singular4x4", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}})}, + {"Tall3x2", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, + {"Wide2x3", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}})}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs index e8253f6c..3eefa8d1 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// A matrix with the given values. protected override Matrix CreateMatrix(Complex[,] data) { - return new DenseMatrix(data); + return DenseMatrix.OfArray(data); } /// @@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new DenseMatrix(3, 3, new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1) }) }, - { "Square3x3", new DenseMatrix(3, 3, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.6, 1) }) }, - { "Square4x4", new DenseMatrix(4, 4, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(1.0, 1), new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(2.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.2, 1), new Complex(6.6, 1), new Complex(-4.4, 1), new Complex(3.3, 1), new Complex(4.3, 1), new Complex(-7.7, 1) }) }, - { "Tall3x2", new DenseMatrix(3, 2, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1) }) }, - { "Wide2x3", new DenseMatrix(2, 3, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(-3.3, 1), new Complex(2.2, 1) }) } - }; + { + {"Singular3x3", new DenseMatrix(3, 3, new[] {new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1)})}, + {"Square3x3", new DenseMatrix(3, 3, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.6, 1)})}, + {"Square4x4", new DenseMatrix(4, 4, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(1.0, 1), new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(2.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.2, 1), new Complex(6.6, 1), new Complex(-4.4, 1), new Complex(3.3, 1), new Complex(4.3, 1), new Complex(-7.7, 1)})}, + {"Tall3x2", new DenseMatrix(3, 2, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1)})}, + {"Wide2x3", new DenseMatrix(2, 3, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(-3.3, 1), new Complex(2.2, 1)})} + }; foreach (var name in testData.Keys) { @@ -106,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [Test] public void MatrixFrom1DArrayIsReference() { - var data = new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1) }; + var data = new[] {new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1)}; var matrix = new DenseMatrix(3, 3, data); matrix[0, 0] = new Complex(10.0, 1); Assert.AreEqual(new Complex(10.0, 1), data[0]); @@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new DenseMatrix(TestData2D["Singular3x3"]); + var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0; Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]); } @@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new DenseMatrix(TestData2D[name]); + var matrix = DenseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs index 8cb9aa9b..8e3fda6a 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -28,15 +32,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { using System; using System.Collections.Generic; - using System.Linq; using System.Numerics; using LinearAlgebra.Complex; using NUnit.Framework; -#if PORTABLE - using Threading; -#endif - /// /// Diagonal matrix tests. /// @@ -49,14 +48,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public override void SetupMatrices() { TestData2D = new Dictionary - { - { "Singular3x3", new[,] { { new Complex(1.0, 1), Complex.Zero, Complex.Zero }, { Complex.Zero, Complex.Zero, Complex.Zero }, { Complex.Zero, Complex.Zero, new Complex(3.0, 1) } } }, - { "Square3x3", new[,] { { new Complex(-1.1, 1), Complex.Zero, Complex.Zero }, { Complex.Zero, new Complex(1.1, 1), Complex.Zero }, { Complex.Zero, Complex.Zero, new Complex(6.6, 1) } } }, - { "Square4x4", new[,] { { new Complex(-1.1, 1), Complex.Zero, Complex.Zero, Complex.Zero }, { Complex.Zero, new Complex(1.1, 1), Complex.Zero, Complex.Zero }, { Complex.Zero, Complex.Zero, new Complex(6.2, 1), Complex.Zero }, { Complex.Zero, Complex.Zero, Complex.Zero, new Complex(-7.7, 1) } } }, - { "Singular4x4", new[,] { { new Complex(-1.1, 1), Complex.Zero, Complex.Zero, Complex.Zero }, { Complex.Zero, new Complex(-2.2, 1), Complex.Zero, Complex.Zero }, { Complex.Zero, Complex.Zero, Complex.Zero, Complex.Zero }, { Complex.Zero, Complex.Zero, Complex.Zero, new Complex(-4.4, 1) } } }, - { "Tall3x2", new[,] { { new Complex(-1.1, 1), Complex.Zero }, { Complex.Zero, new Complex(1.1, 1) }, { Complex.Zero, Complex.Zero } } }, - { "Wide2x3", new[,] { { new Complex(-1.1, 1), Complex.Zero, Complex.Zero }, { Complex.Zero, new Complex(1.1, 1), Complex.Zero } } } - }; + { + {"Singular3x3", new[,] {{new Complex(1.0, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, Complex.Zero, Complex.Zero}, {Complex.Zero, Complex.Zero, new Complex(3.0, 1)}}}, + {"Square3x3", new[,] {{new Complex(-1.1, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, new Complex(1.1, 1), Complex.Zero}, {Complex.Zero, Complex.Zero, new Complex(6.6, 1)}}}, + {"Square4x4", new[,] {{new Complex(-1.1, 1), Complex.Zero, Complex.Zero, Complex.Zero}, {Complex.Zero, new Complex(1.1, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, Complex.Zero, new Complex(6.2, 1), Complex.Zero}, {Complex.Zero, Complex.Zero, Complex.Zero, new Complex(-7.7, 1)}}}, + {"Singular4x4", new[,] {{new Complex(-1.1, 1), Complex.Zero, Complex.Zero, Complex.Zero}, {Complex.Zero, new Complex(-2.2, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, Complex.Zero, Complex.Zero, Complex.Zero}, {Complex.Zero, Complex.Zero, Complex.Zero, new Complex(-4.4, 1)}}}, + {"Tall3x2", new[,] {{new Complex(-1.1, 1), Complex.Zero}, {Complex.Zero, new Complex(1.1, 1)}, {Complex.Zero, Complex.Zero}}}, + {"Wide2x3", new[,] {{new Complex(-1.1, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, new Complex(1.1, 1), Complex.Zero}}} + }; TestMatrices = new Dictionary(); foreach (var name in TestData2D.Keys) @@ -114,13 +113,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanCreateMatrixFromDiagonalArray() { var testData = new Dictionary - { - { "Singular3x3", new DiagonalMatrix(3, 3, new[] { new Complex(1.0, 1), Complex.Zero, new Complex(3.0, 1) }) }, - { "Square3x3", new DiagonalMatrix(3, 3, new[] { new Complex(-1.1, 1), new Complex(1.1, 1), new Complex(6.6, 1) }) }, - { "Square4x4", new DiagonalMatrix(4, 4, new[] { new Complex(-1.1, 1), new Complex(1.1, 1), new Complex(6.2, 1), new Complex(-7.7, 1) }) }, - { "Tall3x2", new DiagonalMatrix(3, 2, new[] { new Complex(-1.1, 1), new Complex(1.1, 1) }) }, - { "Wide2x3", new DiagonalMatrix(2, 3, new[] { new Complex(-1.1, 1), new Complex(1.1, 1) }) }, - }; + { + {"Singular3x3", new DiagonalMatrix(3, 3, new[] {new Complex(1.0, 1), Complex.Zero, new Complex(3.0, 1)})}, + {"Square3x3", new DiagonalMatrix(3, 3, new[] {new Complex(-1.1, 1), new Complex(1.1, 1), new Complex(6.6, 1)})}, + {"Square4x4", new DiagonalMatrix(4, 4, new[] {new Complex(-1.1, 1), new Complex(1.1, 1), new Complex(6.2, 1), new Complex(-7.7, 1)})}, + {"Tall3x2", new DiagonalMatrix(3, 2, new[] {new Complex(-1.1, 1), new Complex(1.1, 1)})}, + {"Wide2x3", new DiagonalMatrix(2, 3, new[] {new Complex(-1.1, 1), new Complex(1.1, 1)})}, + }; foreach (var name in testData.Keys) { @@ -134,7 +133,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [Test] public void MatrixFrom1DArrayIsReference() { - var data = new[] { new Complex(1.0, 1), new Complex(2.0, 1), new Complex(3.0, 1), new Complex(4.0, 1), new Complex(5.0, 1) }; + var data = new[] {new Complex(1.0, 1), new Complex(2.0, 1), new Complex(3.0, 1), new Complex(4.0, 1), new Complex(5.0, 1)}; var matrix = new DiagonalMatrix(5, 5, data); matrix[0, 0] = new Complex(10.0, 1); Assert.AreEqual(new Complex(10.0, 1), data[0]); @@ -221,7 +220,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { for (var j = 0; j < matrixC.ColumnCount; j++) { - AssertHelpers.AlmostEqual(matrixA.Row(i) * matrixB.Column(j), matrixC[i, j], 15); + AssertHelpers.AlmostEqual(matrixA.Row(i)*matrixB.Column(j), matrixC[i, j], 15); } } } @@ -233,7 +232,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void PermuteMatrixRowsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteRows(permutation)); } @@ -244,7 +243,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void PermuteMatrixColumnsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteColumns(permutation)); } @@ -261,13 +260,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex var min = Math.Min(data.RowCount, data.ColumnCount); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } result = data.PointwiseDivide(other); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } } } @@ -278,15 +277,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public override void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); } @@ -296,15 +295,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public override void CanComputeInfinityNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); } @@ -314,15 +313,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public override void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); } @@ -332,15 +331,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public override void CanComputeL2Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); } @@ -351,11 +350,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanComputeDeterminant() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); matrix = TestMatrices["Square4x4"]; - denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); } diff --git a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs index 0e29a8c8..b6f097c5 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,13 +30,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO { + using LinearAlgebra.Complex; + using LinearAlgebra.IO; + using NUnit.Framework; using System; using System.Globalization; using System.IO; using System.Numerics; - using LinearAlgebra.Complex; - using LinearAlgebra.IO; - using NUnit.Framework; /// /// Delimited writer tests. @@ -46,19 +50,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO [Test] public void CanWriteCommaDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3) }, { new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6) }, { new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); var writer = new DelimitedWriter(',') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; + + "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; Assert.AreEqual(expected, text); } @@ -68,20 +72,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO [Test] public void CanWritePeriodDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3) }, { new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6) }, { new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); var culture = new CultureInfo("tr-TR"); var writer = new DelimitedWriter('.') - { - CultureInfo = culture - }; + { + CultureInfo = culture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine - + "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine - + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; + + "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine + + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; Assert.AreEqual(expected, text); } @@ -91,19 +95,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO [Test] public void CanWriteSpaceDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3) }, { new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6) }, { new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); var writer = new DelimitedWriter(' ') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; + + "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; Assert.AreEqual(expected, text); } @@ -113,22 +117,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO [Test] public void CanWriteTabDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3) }, { new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6) }, { new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9) } }); - var headers = new[] { "a", "b", "c" }; + var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); + var headers = new[] {"a", "b", "c"}; var writer = new DelimitedWriter('\t') - { - ColumnHeaders = headers, - CultureInfo = CultureInfo.InvariantCulture - }; + { + ColumnHeaders = headers, + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "a\tb\tc" + Environment.NewLine - + "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; + + "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine + + "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; Assert.AreEqual(expected, text); } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs index 3a5186e6..ba29bf58 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs @@ -1,12 +1,41 @@ -using System.Linq; -using MathNet.Numerics.Distributions; -using MathNet.Numerics.Random; +// +// 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. +// namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { + using Distributions; using LinearAlgebra.Complex; using LinearAlgebra.Generic; + using Numerics.Random; using NUnit.Framework; + using System.Linq; using System.Numerics; [TestFixture] @@ -15,17 +44,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [Datapoints] Matrix[] _matrices = new Matrix[] { - new DenseMatrix(new[,] {{1d, new Complex(1.1d, -4d), 2d}, {1d, 1d, 2d}, {1d, new Complex(1d, 2d), 2d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, new Complex(2.2d, -1.2d)}, {-4.4d, 5.5d, 6.6d}}), - new DenseMatrix(new[,] {{new Complex(-1.1d, -2d), -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), - new DenseMatrix(new[,] {{-1.1d, new Complex(-2.2d, 3.4d), -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d}, {Complex.Zero, 1.1d}, {-4.4d, 5.5d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, new Complex(1.1d, 0.1d), 2.2d}}), - new DenseMatrix(new[,] {{1d, 2d, 3d}, {2d, new Complex(2d, 2d), 0d}, {3d, Complex.Zero, 3d}}), + DenseMatrix.OfArray(new[,] {{1d, new Complex(1.1d, -4d), 2d}, {1d, 1d, 2d}, {1d, new Complex(1d, 2d), 2d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, new Complex(2.2d, -1.2d)}, {-4.4d, 5.5d, 6.6d}}), + DenseMatrix.OfArray(new[,] {{new Complex(-1.1d, -2d), -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, new Complex(-2.2d, 3.4d), -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d}, {Complex.Zero, 1.1d}, {-4.4d, 5.5d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, new Complex(1.1d, 0.1d), 2.2d}}), + DenseMatrix.OfArray(new[,] {{1d, 2d, 3d}, {2d, new Complex(2d, 2d), 0d}, {3d, Complex.Zero, 3d}}), - new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d + Complex.ImaginaryOne, 2d}}), - new SparseMatrix(new[,] {{7d, 1d, 2d}, {new Complex(1d, 2d), 0d, Complex.Zero}, {-2d, 0d, 0d}}), - new SparseMatrix(new[,] {{-1.1d, 0d, 0d}, {0d, new Complex(1.1d, 2d), 2.2d}}), + SparseMatrix.OfArray(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d + Complex.ImaginaryOne, 2d}}), + SparseMatrix.OfArray(new[,] {{7d, 1d, 2d}, {new Complex(1d, 2d), 0d, Complex.Zero}, {-2d, 0d, 0d}}), + SparseMatrix.OfArray(new[,] {{-1.1d, 0d, 0d}, {0d, new Complex(1.1d, 2d), 2.2d}}), new DiagonalMatrix(3, 3, new[] {new Complex(1d, 1d), -2d, 1.5d}), new DiagonalMatrix(3, 3, new[] {new Complex(1d, 2d), 0d, -1.5d}), @@ -68,4 +97,4 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex get { return Complex.Zero; } } } -} \ No newline at end of file +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs index 20b5c94e..4a2b8020 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,11 +30,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { + using LinearAlgebra.Complex; + using NUnit.Framework; using System; using System.Collections.Generic; using System.Numerics; - using LinearAlgebra.Complex; - using NUnit.Framework; /// /// Sparse matrix tests. @@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// A matrix with the given values. protected override Matrix CreateMatrix(Complex[,] data) { - return new SparseMatrix(data); + return SparseMatrix.OfArray(data); } /// @@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new SparseMatrix(3, 3, new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1) }) }, - { "Square3x3", new SparseMatrix(3, 3, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.6, 1) }) }, - { "Square4x4", new SparseMatrix(4, 4, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(1.0, 1), new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(2.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.2, 1), new Complex(6.6, 1), new Complex(-4.4, 1), new Complex(3.3, 1), new Complex(4.3, 1), new Complex(-7.7, 1) }) }, - { "Tall3x2", new SparseMatrix(3, 2, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1) }) }, - { "Wide2x3", new SparseMatrix(2, 3, new[] { new Complex(-1.1, 1), Complex.Zero, new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(-3.3, 1), new Complex(2.2, 1) }) } - }; + { + {"Singular3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1)})}, + {"Square3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.6, 1)})}, + {"Square4x4", SparseMatrix.OfColumnMajor(4, 4, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(1.0, 1), new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(2.1, 1), new Complex(5.5, 1), new Complex(-3.3, 1), new Complex(2.2, 1), new Complex(6.2, 1), new Complex(6.6, 1), new Complex(-4.4, 1), new Complex(3.3, 1), new Complex(4.3, 1), new Complex(-7.7, 1)})}, + {"Tall3x2", SparseMatrix.OfColumnMajor(3, 2, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-4.4, 1), new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(5.5, 1)})}, + {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {new Complex(-1.1, 1), Complex.Zero, new Complex(-2.2, 1), new Complex(1.1, 1), new Complex(-3.3, 1), new Complex(2.2, 1)})} + }; foreach (var name in testData.Keys) { @@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void MatrixFrom1DArrayIsCopy() { // Sparse Matrix copies values from Complex[], but no remember reference. - var data = new[] { new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1) }; - var matrix = new SparseMatrix(3, 3, data); + var data = new[] {new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(1.0, 1), new Complex(2.0, 1), new Complex(2.0, 1), new Complex(2.0, 1)}; + var matrix = SparseMatrix.OfColumnMajor(3, 3, data); matrix[0, 0] = new Complex(10.0, 1); Assert.AreNotEqual(new Complex(10.0, 1), data[0]); } @@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new SparseMatrix(TestData2D["Singular3x3"]); + var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = new Complex(10.0, 1); Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]); } @@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new SparseMatrix(TestData2D[name]); + var matrix = SparseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) @@ -187,7 +191,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex { for (var j = 0; j < matrix.ColumnCount; j++) { - var value = rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10); + var value = rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10); if (value != 0) { nonzero++; @@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanAddSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); var sum1 = m1 + m2; var sum2 = m2 + m1; Assert.IsTrue(sum1.Equals(m2)); @@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex sparseResult.Add(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); sparseResult.Add(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); m1.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); sparseResult.Add(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(2 * sum1)); + Assert.IsTrue(sparseResult.Equals(2*sum1)); var denseResult = new DenseMatrix(1, 3); denseResult.Add(m2, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - denseResult = new DenseMatrix(new Complex[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); denseResult.Add(m1, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - var m3 = new DenseMatrix(new Complex[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var sum3 = m1 + m3; var sum4 = m3 + m1; Assert.IsTrue(sum3.Equals(m3)); @@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex public void CanSubtractSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); var diff1 = m1 - m2; var diff2 = m2 - m1; Assert.IsTrue(diff1.Equals(m2.Negate())); @@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex sparseResult.Subtract(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); sparseResult.Subtract(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(diff2)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); m1.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new Complex[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex[,] { { 0, 1, 1 } }); sparseResult.Subtract(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(0 * diff1)); + Assert.IsTrue(sparseResult.Equals(0*diff1)); var denseResult = new DenseMatrix(1, 3); denseResult.Subtract(m2, denseResult); Assert.IsTrue(denseResult.Equals(diff1)); - denseResult = new DenseMatrix(new Complex[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); denseResult.Subtract(m1, denseResult); Assert.IsTrue(denseResult.Equals(diff2)); - var m3 = new DenseMatrix(new Complex[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new Complex[,] {{0, 1, 1}}); var diff3 = m1 - m3; var diff4 = m3 - m1; Assert.IsTrue(diff3.Equals(m3.Negate())); diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs index d690b5f2..f92d9616 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Collections.Generic; using LinearAlgebra.Complex32; using NUnit.Framework; + using System; + using System.Collections.Generic; using Complex32 = Numerics.Complex32; /// @@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// A matrix with the given values. protected override Matrix CreateMatrix(Complex32[,] data) { - return new DenseMatrix(data); + return DenseMatrix.OfArray(data); } /// @@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new DenseMatrix(3, 3, new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) }) }, - { "Square3x3", new DenseMatrix(3, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.6f, 1) }) }, - { "Square4x4", new DenseMatrix(4, 4, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(1.0f, 1), new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(2.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.2f, 1), new Complex32(6.6f, 1), new Complex32(-4.4f, 1), new Complex32(3.3f, 1), new Complex32(4.3f, 1), new Complex32(-7.7f, 1) }) }, - { "Tall3x2", new DenseMatrix(3, 2, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1) }) }, - { "Wide2x3", new DenseMatrix(2, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1) }) } - }; + { + {"Singular3x3", new DenseMatrix(3, 3, new[] {new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1)})}, + {"Square3x3", new DenseMatrix(3, 3, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.6f, 1)})}, + {"Square4x4", new DenseMatrix(4, 4, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(1.0f, 1), new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(2.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.2f, 1), new Complex32(6.6f, 1), new Complex32(-4.4f, 1), new Complex32(3.3f, 1), new Complex32(4.3f, 1), new Complex32(-7.7f, 1)})}, + {"Tall3x2", new DenseMatrix(3, 2, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1)})}, + {"Wide2x3", new DenseMatrix(2, 3, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1)})} + }; foreach (var name in testData.Keys) { @@ -106,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Test] public void MatrixFrom1DArrayIsReference() { - var data = new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) }; + var data = new[] {new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1)}; var matrix = new DenseMatrix(3, 3, data); matrix[0, 0] = new Complex32(10.0f, 1); Assert.AreEqual(new Complex32(10.0f, 1), data[0]); @@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new DenseMatrix(TestData2D["Singular3x3"]); + var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0f; Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]); } @@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new DenseMatrix(TestData2D[name]); + var matrix = DenseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs index 64f345c6..2b90bf4a 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,17 +30,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Collections.Generic; - using System.Linq; using LinearAlgebra.Complex32; using NUnit.Framework; + using System; + using System.Collections.Generic; using Complex32 = Numerics.Complex32; -#if PORTABLE - using Threading; -#endif - /// /// Diagonal matrix tests. /// @@ -49,14 +48,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public override void SetupMatrices() { TestData2D = new Dictionary - { - { "Singular3x3", new[,] { { new Complex32(1.0f, 1), Complex32.Zero, Complex32.Zero }, { Complex32.Zero, Complex32.Zero, Complex32.Zero }, { Complex32.Zero, Complex32.Zero, new Complex32(3.0f, 1) } } }, - { "Square3x3", new[,] { { new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero }, { Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero }, { Complex32.Zero, Complex32.Zero, new Complex32(6.6f, 1) } } }, - { "Square4x4", new[,] { { new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero, Complex32.Zero }, { Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero, Complex32.Zero }, { Complex32.Zero, Complex32.Zero, new Complex32(6.2f, 1), Complex32.Zero }, { Complex32.Zero, Complex32.Zero, Complex32.Zero, new Complex32(-7.7f, 1) } } }, - { "Singular4x4", new[,] { { new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero, Complex32.Zero }, { Complex32.Zero, new Complex32(-2.2f, 1), Complex32.Zero, Complex32.Zero }, { Complex32.Zero, Complex32.Zero, Complex32.Zero, Complex32.Zero }, { Complex32.Zero, Complex32.Zero, Complex32.Zero, new Complex32(-4.4f, 1) } } }, - { "Tall3x2", new[,] { { new Complex32(-1.1f, 1), Complex32.Zero }, { Complex32.Zero, new Complex32(1.1f, 1) }, { Complex32.Zero, Complex32.Zero } } }, - { "Wide2x3", new[,] { { new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero }, { Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero } } } - }; + { + {"Singular3x3", new[,] {{new Complex32(1.0f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, Complex32.Zero, Complex32.Zero}, {Complex32.Zero, Complex32.Zero, new Complex32(3.0f, 1)}}}, + {"Square3x3", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero}, {Complex32.Zero, Complex32.Zero, new Complex32(6.6f, 1)}}}, + {"Square4x4", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero, Complex32.Zero}, {Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, Complex32.Zero, new Complex32(6.2f, 1), Complex32.Zero}, {Complex32.Zero, Complex32.Zero, Complex32.Zero, new Complex32(-7.7f, 1)}}}, + {"Singular4x4", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero, Complex32.Zero}, {Complex32.Zero, new Complex32(-2.2f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, Complex32.Zero, Complex32.Zero, Complex32.Zero}, {Complex32.Zero, Complex32.Zero, Complex32.Zero, new Complex32(-4.4f, 1)}}}, + {"Tall3x2", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero}, {Complex32.Zero, new Complex32(1.1f, 1)}, {Complex32.Zero, Complex32.Zero}}}, + {"Wide2x3", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero}}} + }; TestMatrices = new Dictionary(); foreach (var name in TestData2D.Keys) @@ -114,13 +113,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanCreateMatrixFromDiagonalArray() { var testData = new Dictionary - { - { "Singular3x3", new DiagonalMatrix(3, 3, new[] { new Complex32(1.0f, 1), Complex32.Zero, new Complex32(3.0f, 1) }) }, - { "Square3x3", new DiagonalMatrix(3, 3, new[] { new Complex32(-1.1f, 1), new Complex32(1.1f, 1), new Complex32(6.6f, 1) }) }, - { "Square4x4", new DiagonalMatrix(4, 4, new[] { new Complex32(-1.1f, 1), new Complex32(1.1f, 1), new Complex32(6.2f, 1), new Complex32(-7.7f, 1) }) }, - { "Tall3x2", new DiagonalMatrix(3, 2, new[] { new Complex32(-1.1f, 1), new Complex32(1.1f, 1) }) }, - { "Wide2x3", new DiagonalMatrix(2, 3, new[] { new Complex32(-1.1f, 1), new Complex32(1.1f, 1) }) }, - }; + { + {"Singular3x3", new DiagonalMatrix(3, 3, new[] {new Complex32(1.0f, 1), Complex32.Zero, new Complex32(3.0f, 1)})}, + {"Square3x3", new DiagonalMatrix(3, 3, new[] {new Complex32(-1.1f, 1), new Complex32(1.1f, 1), new Complex32(6.6f, 1)})}, + {"Square4x4", new DiagonalMatrix(4, 4, new[] {new Complex32(-1.1f, 1), new Complex32(1.1f, 1), new Complex32(6.2f, 1), new Complex32(-7.7f, 1)})}, + {"Tall3x2", new DiagonalMatrix(3, 2, new[] {new Complex32(-1.1f, 1), new Complex32(1.1f, 1)})}, + {"Wide2x3", new DiagonalMatrix(2, 3, new[] {new Complex32(-1.1f, 1), new Complex32(1.1f, 1)})}, + }; foreach (var name in testData.Keys) { @@ -134,7 +133,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Test] public void MatrixFrom1DArrayIsReference() { - var data = new[] { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1), new Complex32(4.0f, 1), new Complex32(5.0f, 1) }; + var data = new[] {new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1), new Complex32(4.0f, 1), new Complex32(5.0f, 1)}; var matrix = new DiagonalMatrix(5, 5, data); matrix[0, 0] = new Complex32(10.0f, 1); Assert.AreEqual(new Complex32(10.0f, 1), data[0]); @@ -221,7 +220,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { for (var j = 0; j < matrixC.ColumnCount; j++) { - AssertHelpers.AlmostEqual(matrixA.Row(i) * matrixB.Column(j), matrixC[i, j], 15); + AssertHelpers.AlmostEqual(matrixA.Row(i)*matrixB.Column(j), matrixC[i, j], 15); } } } @@ -233,7 +232,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void PermuteMatrixRowsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteRows(permutation)); } @@ -244,7 +243,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void PermuteMatrixColumnsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteColumns(permutation)); } @@ -261,13 +260,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 var min = Math.Min(data.RowCount, data.ColumnCount); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } result = data.PointwiseDivide(other); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } } } @@ -278,15 +277,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public override void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); } @@ -296,15 +295,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public override void CanComputeInfinityNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); } @@ -314,15 +313,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public override void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); } @@ -332,15 +331,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public override void CanComputeL2Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); } @@ -351,11 +350,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanComputeDeterminant() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); matrix = TestMatrices["Square4x4"]; - denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); } diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs index 45f28027..5c51f19f 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,12 +30,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO { - using System; - using System.Globalization; - using System.IO; using LinearAlgebra.Complex32; using LinearAlgebra.IO; using NUnit.Framework; + using System; + using System.Globalization; + using System.IO; using Complex32 = Numerics.Complex32; /// @@ -46,19 +50,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO [Test] public void CanWriteCommaDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f) }, { new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f) }, { new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); var writer = new DelimitedWriter(',') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; + + "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; Assert.AreEqual(expected, text); } @@ -68,20 +72,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO [Test] public void CanWritePeriodDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f) }, { new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f) }, { new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); var culture = new CultureInfo("tr-TR"); var writer = new DelimitedWriter('.') - { - CultureInfo = culture - }; + { + CultureInfo = culture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine - + "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine - + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; + + "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine + + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; Assert.AreEqual(expected, text); } @@ -91,19 +95,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO [Test] public void CanWriteSpaceDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f) }, { new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f) }, { new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f) } }); + var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); var writer = new DelimitedWriter(' ') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; + + "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; Assert.AreEqual(expected, text); } @@ -113,22 +117,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO [Test] public void CanWriteTabDelimitedData() { - var matrix = new DenseMatrix(new[,] { { new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f) }, { new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f) }, { new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f) } }); - var headers = new[] { "a", "b", "c" }; + var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); + var headers = new[] {"a", "b", "c"}; var writer = new DelimitedWriter('\t') - { - ColumnHeaders = headers, - CultureInfo = CultureInfo.InvariantCulture - }; + { + ColumnHeaders = headers, + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "a\tb\tc" + Environment.NewLine - + "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine - + "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine - + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; + + "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine + + "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine + + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; Assert.AreEqual(expected, text); } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs index 95947917..cd192c74 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs @@ -1,12 +1,41 @@ -using System.Linq; -using MathNet.Numerics.Distributions; -using MathNet.Numerics.Random; +// +// 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. +// namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { + using Distributions; using LinearAlgebra.Complex32; using LinearAlgebra.Generic; + using Numerics.Random; using NUnit.Framework; + using System.Linq; using Complex32 = Numerics.Complex32; [TestFixture] @@ -15,17 +44,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Datapoints] Matrix[] _matrices = new Matrix[] { - new DenseMatrix(new[,] {{1f, new Complex32(1.1f, -4f), 2f}, {1f, 1f, 2f}, {1f, new Complex32(1f, 2f), 2f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, new Complex32(2.2f, -1.2f)}, {-4.4f, 5.5f, 6.6f}}), - new DenseMatrix(new[,] {{new Complex32(-1.1f, -2f), -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), - new DenseMatrix(new[,] {{-1.1f, new Complex32(-2.2f, 3.4f), -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f}, {Complex32.Zero, 1.1f}, {-4.4f, 5.5f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, new Complex32(1.1f, 0.1f), 2.2f}}), - new DenseMatrix(new[,] {{1f, 2f, 3f}, {2f, new Complex32(2f, 2f), 0f}, {3f, Complex32.Zero, 3f}}), + DenseMatrix.OfArray(new[,] {{1f, new Complex32(1.1f, -4f), 2f}, {1f, 1f, 2f}, {1f, new Complex32(1f, 2f), 2f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, new Complex32(2.2f, -1.2f)}, {-4.4f, 5.5f, 6.6f}}), + DenseMatrix.OfArray(new[,] {{new Complex32(-1.1f, -2f), -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, new Complex32(-2.2f, 3.4f), -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f}, {Complex32.Zero, 1.1f}, {-4.4f, 5.5f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, new Complex32(1.1f, 0.1f), 2.2f}}), + DenseMatrix.OfArray(new[,] {{1f, 2f, 3f}, {2f, new Complex32(2f, 2f), 0f}, {3f, Complex32.Zero, 3f}}), - new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f + Complex32.ImaginaryOne, 2f}}), - new SparseMatrix(new[,] {{7f, 1f, 2f}, {new Complex32(1f, 2f), 0f, Complex32.Zero}, {-2f, 0f, 0f}}), - new SparseMatrix(new[,] {{-1.1f, 0f, 0f}, {0f, new Complex32(1.1f, 2f), 2.2f}}), + SparseMatrix.OfArray(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f + Complex32.ImaginaryOne, 2f}}), + SparseMatrix.OfArray(new[,] {{7f, 1f, 2f}, {new Complex32(1f, 2f), 0f, Complex32.Zero}, {-2f, 0f, 0f}}), + SparseMatrix.OfArray(new[,] {{-1.1f, 0f, 0f}, {0f, new Complex32(1.1f, 2f), 2.2f}}), new DiagonalMatrix(3, 3, new[] {new Complex32(1f, 1f), -2f, 1.5f}), new DiagonalMatrix(3, 3, new[] {new Complex32(1f, 2f), 0f, -1.5f}), @@ -68,4 +97,4 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 get { return Complex32.Zero; } } } -} \ No newline at end of file +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs index f017fd3f..d74aa861 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { - using System; - using System.Collections.Generic; using LinearAlgebra.Complex32; using NUnit.Framework; + using System; + using System.Collections.Generic; using Complex32 = Numerics.Complex32; /// @@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// A matrix with the given values. protected override Matrix CreateMatrix(Complex32[,] data) { - return new SparseMatrix(data); + return SparseMatrix.OfArray(data); } /// @@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new SparseMatrix(3, 3, new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) }) }, - { "Square3x3", new SparseMatrix(3, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.6f, 1) }) }, - { "Square4x4", new SparseMatrix(4, 4, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(1.0f, 1), new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(2.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.2f, 1), new Complex32(6.6f, 1), new Complex32(-4.4f, 1), new Complex32(3.3f, 1), new Complex32(4.3f, 1), new Complex32(-7.7f, 1) }) }, - { "Tall3x2", new SparseMatrix(3, 2, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1) }) }, - { "Wide2x3", new SparseMatrix(2, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1) }) } - }; + { + {"Singular3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1)})}, + {"Square3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.6f, 1)})}, + {"Square4x4", SparseMatrix.OfColumnMajor(4, 4, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(1.0f, 1), new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(2.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.2f, 1), new Complex32(6.6f, 1), new Complex32(-4.4f, 1), new Complex32(3.3f, 1), new Complex32(4.3f, 1), new Complex32(-7.7f, 1)})}, + {"Tall3x2", SparseMatrix.OfColumnMajor(3, 2, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1)})}, + {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1)})} + }; foreach (var name in testData.Keys) { @@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void MatrixFrom1DArrayIsCopy() { // Sparse Matrix copies values from Complex32[], but no remember reference. - var data = new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) }; - var matrix = new SparseMatrix(3, 3, data); + var data = new[] {new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1)}; + var matrix = SparseMatrix.OfColumnMajor(3, 3, data); matrix[0, 0] = new Complex32(10.0f, 1); Assert.AreNotEqual(new Complex32(10.0f, 1), data[0]); } @@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new SparseMatrix(TestData2D["Singular3x3"]); + var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = new Complex32(10.0f, 1); Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]); } @@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new SparseMatrix(TestData2D[name]); + var matrix = SparseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) @@ -187,7 +191,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 { for (var j = 0; j < matrix.ColumnCount; j++) { - var value = rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10); + var value = rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10); if (value != 0) { nonzero++; @@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanAddSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); var sum1 = m1 + m2; var sum2 = m2 + m1; Assert.IsTrue(sum1.Equals(m2)); @@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 sparseResult.Add(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); sparseResult.Add(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); m1.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); sparseResult.Add(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(2 * sum1)); + Assert.IsTrue(sparseResult.Equals(2*sum1)); var denseResult = new DenseMatrix(1, 3); denseResult.Add(m2, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - denseResult = new DenseMatrix(new Complex32[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}}); denseResult.Add(m1, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - var m3 = new DenseMatrix(new Complex32[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}}); var sum3 = m1 + m3; var sum4 = m3 + m1; Assert.IsTrue(sum3.Equals(m3)); @@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 public void CanSubtractSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); var diff1 = m1 - m2; var diff2 = m2 - m1; Assert.IsTrue(diff1.Equals(m2.Negate())); @@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 sparseResult.Subtract(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); sparseResult.Subtract(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(diff2)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); m1.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new Complex32[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } }); sparseResult.Subtract(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(0 * diff1)); + Assert.IsTrue(sparseResult.Equals(0*diff1)); var denseResult = new DenseMatrix(1, 3); denseResult.Subtract(m2, denseResult); Assert.IsTrue(denseResult.Equals(diff1)); - denseResult = new DenseMatrix(new Complex32[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}}); denseResult.Subtract(m1, denseResult); Assert.IsTrue(denseResult.Equals(diff2)); - var m3 = new DenseMatrix(new Complex32[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}}); var diff3 = m1 - m3; var diff4 = m3 - m1; Assert.IsTrue(diff3.Equals(m3.Negate())); diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs index 39664b1c..0005f003 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// A matrix with the given values. protected override Matrix CreateMatrix(double[,] data) { - return new DenseMatrix(data); + return DenseMatrix.OfArray(data); } /// @@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new DenseMatrix(3, 3, new[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 }) }, - { "Square3x3", new DenseMatrix(3, 3, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6 }) }, - { "Square4x4", new DenseMatrix(4, 4, new[] { -1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7 }) }, - { "Tall3x2", new DenseMatrix(3, 2, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5 }) }, - { "Wide2x3", new DenseMatrix(2, 3, new[] { -1.1, 0.0, -2.2, 1.1, -3.3, 2.2 }) } - }; + { + {"Singular3x3", new DenseMatrix(3, 3, new[] {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0})}, + {"Square3x3", new DenseMatrix(3, 3, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6})}, + {"Square4x4", new DenseMatrix(4, 4, new[] {-1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7})}, + {"Tall3x2", new DenseMatrix(3, 2, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5})}, + {"Wide2x3", new DenseMatrix(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})} + }; foreach (var name in testData.Keys) { @@ -105,7 +109,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void MatrixFrom1DArrayIsReference() { - var data = new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }; + var data = new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2}; var matrix = new DenseMatrix(3, 3, data); matrix[0, 0] = 10.0; Assert.AreEqual(10.0, data[0]); @@ -117,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new DenseMatrix(TestData2D["Singular3x3"]); + var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0; Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]); } @@ -134,7 +138,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new DenseMatrix(TestData2D[name]); + var matrix = DenseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) diff --git a/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs index 1f59db96..a6c5d64d 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -23,19 +27,13 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // - namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Collections.Generic; - using System.Linq; using LinearAlgebra.Double; using LinearAlgebra.Generic; using NUnit.Framework; - -#if PORTABLE - using Threading; -#endif + using System; + using System.Collections.Generic; /// /// Diagonal matrix tests. @@ -49,14 +47,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public override void SetupMatrices() { TestData2D = new Dictionary - { - { "Singular3x3", new[,] { { 1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 3.0 } } }, - { "Square3x3", new[,] { { -1.1, 0.0, 0.0 }, { 0.0, 1.1, 0.0 }, { 0.0, 0.0, 6.6 } } }, - { "Square4x4", new[,] { { -1.1, 0.0, 0.0, 0.0 }, { 0.0, 1.1, 0.0, 0.0 }, { 0.0, 0.0, 6.2, 0.0 }, { 0.0, 0.0, 0.0, -7.7 } } }, - { "Singular4x4", new[,] { { -1.1, 0.0, 0.0, 0.0 }, { 0.0, -2.2, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, -4.4 } } }, - { "Tall3x2", new[,] { { -1.1, 0.0 }, { 0.0, 1.1 }, { 0.0, 0.0 } } }, - { "Wide2x3", new[,] { { -1.1, 0.0, 0.0 }, { 0.0, 1.1, 0.0 } } } - }; + { + {"Singular3x3", new[,] {{1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 3.0}}}, + {"Square3x3", new[,] {{-1.1, 0.0, 0.0}, {0.0, 1.1, 0.0}, {0.0, 0.0, 6.6}}}, + {"Square4x4", new[,] {{-1.1, 0.0, 0.0, 0.0}, {0.0, 1.1, 0.0, 0.0}, {0.0, 0.0, 6.2, 0.0}, {0.0, 0.0, 0.0, -7.7}}}, + {"Singular4x4", new[,] {{-1.1, 0.0, 0.0, 0.0}, {0.0, -2.2, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, -4.4}}}, + {"Tall3x2", new[,] {{-1.1, 0.0}, {0.0, 1.1}, {0.0, 0.0}}}, + {"Wide2x3", new[,] {{-1.1, 0.0, 0.0}, {0.0, 1.1, 0.0}}} + }; TestMatrices = new Dictionary(); foreach (var name in TestData2D.Keys) @@ -114,13 +112,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanCreateMatrixFromDiagonalArray() { var testData = new Dictionary> - { - { "Singular3x3", new DiagonalMatrix(3, 3, new[] { 1.0, 0.0, 3.0 }) }, - { "Square3x3", new DiagonalMatrix(3, 3, new[] { -1.1, 1.1, 6.6 }) }, - { "Square4x4", new DiagonalMatrix(4, 4, new[] { -1.1, 1.1, 6.2, -7.7 }) }, - { "Tall3x2", new DiagonalMatrix(3, 2, new[] { -1.1, 1.1 }) }, - { "Wide2x3", new DiagonalMatrix(2, 3, new[] { -1.1, 1.1 }) }, - }; + { + {"Singular3x3", new DiagonalMatrix(3, 3, new[] {1.0, 0.0, 3.0})}, + {"Square3x3", new DiagonalMatrix(3, 3, new[] {-1.1, 1.1, 6.6})}, + {"Square4x4", new DiagonalMatrix(4, 4, new[] {-1.1, 1.1, 6.2, -7.7})}, + {"Tall3x2", new DiagonalMatrix(3, 2, new[] {-1.1, 1.1})}, + {"Wide2x3", new DiagonalMatrix(2, 3, new[] {-1.1, 1.1})}, + }; foreach (var name in testData.Keys) { @@ -134,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void MatrixFrom1DArrayIsReference() { - var data = new double[] { 1, 2, 3, 4, 5 }; + var data = new double[] {1, 2, 3, 4, 5}; var matrix = new DiagonalMatrix(5, 5, data); matrix[0, 0] = 10.0; Assert.AreEqual(10.0, data[0]); @@ -221,7 +219,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { for (var j = 0; j < matrixC.ColumnCount; j++) { - AssertHelpers.AlmostEqual(matrixA.Row(i) * matrixB.Column(j), matrixC[i, j], 15); + AssertHelpers.AlmostEqual(matrixA.Row(i)*matrixB.Column(j), matrixC[i, j], 15); } } } @@ -233,7 +231,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void PermuteMatrixRowsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteRows(permutation)); } @@ -244,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void PermuteMatrixColumnsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteColumns(permutation)); } @@ -261,13 +259,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double var min = Math.Min(data.RowCount, data.ColumnCount); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } result = data.PointwiseDivide(other); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } } } @@ -278,15 +276,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public override void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); } @@ -296,15 +294,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public override void CanComputeInfinityNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); } @@ -314,15 +312,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public override void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); } @@ -332,15 +330,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public override void CanComputeL2Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); } @@ -351,11 +349,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanComputeDeterminant() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); matrix = TestMatrices["Square4x4"]; - denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); } @@ -400,16 +398,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double // [ 2 0 ] // [ 0 3 ] var subM3 = diagMatrix.SubMatrix(0, 3, 1, 2); - Assert.IsTrue(subM3.Equals(new DenseMatrix(3, 2, new[] { 0d, 2d, 0d, 0d, 0d, 3d }))); + Assert.IsTrue(subM3.Equals(new DenseMatrix(3, 2, new[] {0d, 2d, 0d, 0d, 0d, 3d}))); } [Test] public void DiagonalDenseMatrixMultiplication_IssueCP5706() { Matrix diagonal = DiagonalMatrix.Identity(3); - Matrix dense = new DenseMatrix(new double[,] { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } }); - var test = diagonal * dense; - var test2 = dense * diagonal; + Matrix dense = DenseMatrix.OfArray(new double[,] {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}); + var test = diagonal*dense; + var test2 = dense*diagonal; } } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs index b41611ff..8646e6a5 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,12 +30,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO { - using System; - using System.Globalization; - using System.IO; using LinearAlgebra.Double; using LinearAlgebra.IO; using NUnit.Framework; + using System; + using System.Globalization; + using System.IO; /// /// Delimited writer tests. @@ -45,19 +49,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO [Test] public void CanWriteCommaDelimitedData() { - var matrix = new DenseMatrix(new[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } }); + var matrix = DenseMatrix.OfArray(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); var writer = new DelimitedWriter(',') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1.1,2.2,3.3" + Environment.NewLine - + "4.4,5.5,6.6" + Environment.NewLine - + "7.7,8.8,9.9"; + + "4.4,5.5,6.6" + Environment.NewLine + + "7.7,8.8,9.9"; Assert.AreEqual(expected, text); } @@ -67,20 +71,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO [Test] public void CanWritePeriodDelimitedData() { - var matrix = new DenseMatrix(new[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } }); + var matrix = DenseMatrix.OfArray(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); var culture = new CultureInfo("tr-TR"); var writer = new DelimitedWriter('.') - { - CultureInfo = culture - }; + { + CultureInfo = culture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1,1.2,2.3,3" + Environment.NewLine - + "4,4.5,5.6,6" + Environment.NewLine - + "7,7.8,8.9,9"; + + "4,4.5,5.6,6" + Environment.NewLine + + "7,7.8,8.9,9"; Assert.AreEqual(expected, text); } @@ -90,19 +94,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO [Test] public void CanWriteSpaceDelimitedData() { - var matrix = new SparseMatrix(new[,] { { 1.1, 0, 0 }, { 0, 5.5, 0 }, { 0, 0, 9.9 } }); + var matrix = SparseMatrix.OfArray(new[,] {{1.1, 0, 0}, {0, 5.5, 0}, {0, 0, 9.9}}); var writer = new DelimitedWriter(' ') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1.1 0 0" + Environment.NewLine - + "0 5.5 0" + Environment.NewLine - + "0 0 9.9"; + + "0 5.5 0" + Environment.NewLine + + "0 0 9.9"; Assert.AreEqual(expected, text); } @@ -112,22 +116,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO [Test] public void CanWriteTabDelimitedData() { - var matrix = new UserDefinedMatrix(new[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } }); - var headers = new[] { "a", "b", "c" }; + var matrix = new UserDefinedMatrix(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); + var headers = new[] {"a", "b", "c"}; var writer = new DelimitedWriter('\t') - { - ColumnHeaders = headers, - CultureInfo = CultureInfo.InvariantCulture - }; + { + ColumnHeaders = headers, + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "a\tb\tc" + Environment.NewLine - + "1.1\t2.2\t3.3" + Environment.NewLine - + "4.4\t5.5\t6.6" + Environment.NewLine - + "7.7\t8.8\t9.9"; + + "1.1\t2.2\t3.3" + Environment.NewLine + + "4.4\t5.5\t6.6" + Environment.NewLine + + "7.7\t8.8\t9.9"; Assert.AreEqual(expected, text); } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs index eae7aaf9..9baf01be 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs @@ -1,12 +1,41 @@ -using System.Linq; -using MathNet.Numerics.Distributions; -using MathNet.Numerics.Random; +// +// 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. +// namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { + using Distributions; using LinearAlgebra.Double; using LinearAlgebra.Generic; + using Numerics.Random; using NUnit.Framework; + using System.Linq; [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory @@ -14,17 +43,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Datapoints] Matrix[] _matrices = new Matrix[] { - new DenseMatrix(new[,] {{1d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}, {-4.4d, 5.5d, 6.6d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d}, {0d, 1.1d}, {-4.4d, 5.5d}}), - new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}}), - new DenseMatrix(new[,] {{1d, 2d, 3d}, {2d, 2d, 0d}, {3d, 0d, 3d}}), + DenseMatrix.OfArray(new[,] {{1d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}, {-4.4d, 5.5d, 6.6d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d}, {0d, 1.1d}, {-4.4d, 5.5d}}), + DenseMatrix.OfArray(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}}), + DenseMatrix.OfArray(new[,] {{1d, 2d, 3d}, {2d, 2d, 0d}, {3d, 0d, 3d}}), - new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), - new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 0d, 0d}, {-2d, 0d, 0d}}), - new SparseMatrix(new[,] {{-1.1d, 0d, 0d}, {0d, 1.1d, 2.2d}}), + SparseMatrix.OfArray(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), + SparseMatrix.OfArray(new[,] {{7d, 1d, 2d}, {1d, 0d, 0d}, {-2d, 0d, 0d}}), + SparseMatrix.OfArray(new[,] {{-1.1d, 0d, 0d}, {0d, 1.1d, 2.2d}}), new DiagonalMatrix(3, 3, new[] {1d, -2d, 1.5d}), new DiagonalMatrix(3, 3, new[] {1d, 0d, -1.5d}), @@ -67,4 +96,4 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double get { return 0d; } } } -} \ No newline at end of file +} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs index 41b15293..7de4d953 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,11 +30,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Collections.Generic; using LinearAlgebra.Double; using LinearAlgebra.Double.IO; using NUnit.Framework; + using System; + using System.Collections.Generic; /// /// Sparse matrix tests. @@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// A matrix with the given values. protected override Matrix CreateMatrix(double[,] data) { - return new SparseMatrix(data); + return SparseMatrix.OfArray(data); } /// @@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new SparseMatrix(3, 3, new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) }, - { "Square3x3", new SparseMatrix(3, 3, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6 }) }, - { "Square4x4", new SparseMatrix(4, 4, new[] { -1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7 }) }, - { "Tall3x2", new SparseMatrix(3, 2, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5 }) }, - { "Wide2x3", new SparseMatrix(2, 3, new[] { -1.1, 0.0, -2.2, 1.1, -3.3, 2.2 }) } - }; + { + {"Singular3x3", SparseMatrix.OfColumnMajor(3, 3, new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2})}, + {"Square3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6})}, + {"Square4x4", SparseMatrix.OfColumnMajor(4, 4, new[] {-1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7})}, + {"Tall3x2", SparseMatrix.OfColumnMajor(3, 2, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5})}, + {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})} + }; foreach (var name in testData.Keys) { @@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void MatrixFrom1DArrayIsCopy() { // Sparse Matrix copies values from double[], but no remember reference. - var data = new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }; - var matrix = new SparseMatrix(3, 3, data); + var data = new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2}; + var matrix = SparseMatrix.OfColumnMajor(3, 3, data); matrix[0, 0] = 10.0; Assert.AreNotEqual(10.0, data[0]); } @@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new SparseMatrix(TestData2D["Singular3x3"]); + var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0; Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]); } @@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new SparseMatrix(TestData2D[name]); + var matrix = SparseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) @@ -187,7 +191,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { for (var j = 0; j < matrix.ColumnCount; j++) { - var value = rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10); + var value = rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10); if (value != 0) { nonzero++; @@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanAddSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); var sum1 = m1 + m2; var sum2 = m2 + m1; Assert.IsTrue(sum1.Equals(m2)); @@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double sparseResult.Add(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); sparseResult.Add(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); m1.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); sparseResult.Add(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(2 * sum1)); + Assert.IsTrue(sparseResult.Equals(2*sum1)); var denseResult = new DenseMatrix(1, 3); denseResult.Add(m2, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - denseResult = new DenseMatrix(new double[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); denseResult.Add(m1, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - var m3 = new DenseMatrix(new double[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); var sum3 = m1 + m3; var sum4 = m3 + m1; Assert.IsTrue(sum3.Equals(m3)); @@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void CanSubtractSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); var diff1 = m1 - m2; var diff2 = m2 - m1; Assert.IsTrue(diff1.Equals(m2.Negate())); @@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double sparseResult.Subtract(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); sparseResult.Subtract(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(diff2)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); m1.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new double[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); sparseResult.Subtract(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(0 * diff1)); + Assert.IsTrue(sparseResult.Equals(0*diff1)); var denseResult = new DenseMatrix(1, 3); denseResult.Subtract(m2, denseResult); Assert.IsTrue(denseResult.Equals(diff1)); - denseResult = new DenseMatrix(new double[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); denseResult.Subtract(m1, denseResult); Assert.IsTrue(denseResult.Equals(diff2)); - var m3 = new DenseMatrix(new double[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); var diff3 = m1 - m3; var diff4 = m3 - m1; Assert.IsTrue(diff3.Equals(m3.Negate())); diff --git a/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs index 7285df09..daa7761c 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Collections.Generic; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Collections.Generic; /// /// Dense matrix tests. @@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// A matrix with the given values. protected override Matrix CreateMatrix(float[,] data) { - return new DenseMatrix(data); + return DenseMatrix.OfArray(data); } /// @@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new DenseMatrix(3, 3, new[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f }) }, - { "Square3x3", new DenseMatrix(3, 3, new[] { -1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f, -3.3f, 2.2f, 6.6f }) }, - { "Square4x4", new DenseMatrix(4, 4, new[] { -1.1f, 0.0f, 1.0f, -4.4f, -2.2f, 1.1f, 2.1f, 5.5f, -3.3f, 2.2f, 6.2f, 6.6f, -4.4f, 3.3f, 4.3f, -7.7f }) }, - { "Tall3x2", new DenseMatrix(3, 2, new[] { -1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f }) }, - { "Wide2x3", new DenseMatrix(2, 3, new[] { -1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f }) } - }; + { + {"Singular3x3", new DenseMatrix(3, 3, new[] {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f})}, + {"Square3x3", new DenseMatrix(3, 3, new[] {-1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f, -3.3f, 2.2f, 6.6f})}, + {"Square4x4", new DenseMatrix(4, 4, new[] {-1.1f, 0.0f, 1.0f, -4.4f, -2.2f, 1.1f, 2.1f, 5.5f, -3.3f, 2.2f, 6.2f, 6.6f, -4.4f, 3.3f, 4.3f, -7.7f})}, + {"Tall3x2", new DenseMatrix(3, 2, new[] {-1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f})}, + {"Wide2x3", new DenseMatrix(2, 3, new[] {-1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f})} + }; foreach (var name in testData.Keys) { @@ -105,7 +109,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Test] public void MatrixFrom1DArrayIsReference() { - var data = new float[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }; + var data = new float[] {1, 1, 1, 1, 1, 1, 2, 2, 2}; var matrix = new DenseMatrix(3, 3, data); matrix[0, 0] = 10.0f; Assert.AreEqual(10.0f, data[0]); @@ -117,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new DenseMatrix(TestData2D["Singular3x3"]); + var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0f; Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]); } @@ -134,7 +138,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new DenseMatrix(TestData2D[name]); + var matrix = DenseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) diff --git a/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs index ed90ee8d..42a37ddc 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,15 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Collections.Generic; - using System.Linq; using LinearAlgebra.Single; using NUnit.Framework; - -#if PORTABLE - using Threading; -#endif + using System; + using System.Collections.Generic; /// /// Diagonal matrix tests. @@ -48,14 +47,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public override void SetupMatrices() { TestData2D = new Dictionary - { - { "Singular3x3", new[,] { { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 3.0f } } }, - { "Square3x3", new[,] { { -1.1f, 0.0f, 0.0f }, { 0.0f, 1.1f, 0.0f }, { 0.0f, 0.0f, 6.6f } } }, - { "Square4x4", new[,] { { -1.1f, 0.0f, 0.0f, 0.0f }, { 0.0f, 1.1f, 0.0f, 0.0f }, { 0.0f, 0.0f, 6.2f, 0.0f }, { 0.0f, 0.0f, 0.0f, -7.7f } } }, - { "Singular4x4", new[,] { { -1.1f, 0.0f, 0.0f, 0.0f }, { 0.0f, -2.2f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f, -4.4f } } }, - { "Tall3x2", new[,] { { -1.1f, 0.0f }, { 0.0f, 1.1f }, { 0.0f, 0.0f } } }, - { "Wide2x3", new[,] { { -1.1f, 0.0f, 0.0f }, { 0.0f, 1.1f, 0.0f } } } - }; + { + {"Singular3x3", new[,] {{1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 3.0f}}}, + {"Square3x3", new[,] {{-1.1f, 0.0f, 0.0f}, {0.0f, 1.1f, 0.0f}, {0.0f, 0.0f, 6.6f}}}, + {"Square4x4", new[,] {{-1.1f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.1f, 0.0f, 0.0f}, {0.0f, 0.0f, 6.2f, 0.0f}, {0.0f, 0.0f, 0.0f, -7.7f}}}, + {"Singular4x4", new[,] {{-1.1f, 0.0f, 0.0f, 0.0f}, {0.0f, -2.2f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, -4.4f}}}, + {"Tall3x2", new[,] {{-1.1f, 0.0f}, {0.0f, 1.1f}, {0.0f, 0.0f}}}, + {"Wide2x3", new[,] {{-1.1f, 0.0f, 0.0f}, {0.0f, 1.1f, 0.0f}}} + }; TestMatrices = new Dictionary(); foreach (var name in TestData2D.Keys) @@ -113,13 +112,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanCreateMatrixFromDiagonalArray() { var testData = new Dictionary - { - { "Singular3x3", new DiagonalMatrix(3, 3, new[] { 1.0f, 0.0f, 3.0f }) }, - { "Square3x3", new DiagonalMatrix(3, 3, new[] { -1.1f, 1.1f, 6.6f }) }, - { "Square4x4", new DiagonalMatrix(4, 4, new[] { -1.1f, 1.1f, 6.2f, -7.7f }) }, - { "Tall3x2", new DiagonalMatrix(3, 2, new[] { -1.1f, 1.1f }) }, - { "Wide2x3", new DiagonalMatrix(2, 3, new[] { -1.1f, 1.1f }) }, - }; + { + {"Singular3x3", new DiagonalMatrix(3, 3, new[] {1.0f, 0.0f, 3.0f})}, + {"Square3x3", new DiagonalMatrix(3, 3, new[] {-1.1f, 1.1f, 6.6f})}, + {"Square4x4", new DiagonalMatrix(4, 4, new[] {-1.1f, 1.1f, 6.2f, -7.7f})}, + {"Tall3x2", new DiagonalMatrix(3, 2, new[] {-1.1f, 1.1f})}, + {"Wide2x3", new DiagonalMatrix(2, 3, new[] {-1.1f, 1.1f})}, + }; foreach (var name in testData.Keys) { @@ -133,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Test] public void MatrixFrom1DArrayIsReference() { - var data = new float[] { 1, 2, 3, 4, 5 }; + var data = new float[] {1, 2, 3, 4, 5}; var matrix = new DiagonalMatrix(5, 5, data); matrix[0, 0] = 10.0f; Assert.AreEqual(10.0f, data[0]); @@ -220,7 +219,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { for (var j = 0; j < matrixC.ColumnCount; j++) { - AssertHelpers.AlmostEqual(matrixA.Row(i) * matrixB.Column(j), matrixC[i, j], 15); + AssertHelpers.AlmostEqual(matrixA.Row(i)*matrixB.Column(j), matrixC[i, j], 15); } } } @@ -232,7 +231,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void PermuteMatrixRowsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteRows(permutation)); } @@ -243,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void PermuteMatrixColumnsThrowsInvalidOperationException() { var matrixp = CreateMatrix(TestData2D["Singular3x3"]); - var permutation = new Permutation(new[] { 2, 0, 1 }); + var permutation = new Permutation(new[] {2, 0, 1}); Assert.Throws(() => matrixp.PermuteColumns(permutation)); } @@ -260,13 +259,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single var min = Math.Min(data.RowCount, data.ColumnCount); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } result = data.PointwiseDivide(other); for (var i = 0; i < min; i++) { - Assert.AreEqual(data[i, i] / other[i, i], result[i, i]); + Assert.AreEqual(data[i, i]/other[i, i], result[i, i]); } } } @@ -277,15 +276,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public override void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); } @@ -295,15 +294,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public override void CanComputeInfinityNorm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); } @@ -313,15 +312,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public override void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); } @@ -331,15 +330,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public override void CanComputeL2Norm() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); matrix = TestMatrices["Wide2x3"]; - denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); matrix = TestMatrices["Tall3x2"]; - denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); } @@ -350,11 +349,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanComputeDeterminant() { var matrix = TestMatrices["Square3x3"]; - var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7); matrix = TestMatrices["Square4x4"]; - denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); + denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7); } diff --git a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs index 9bce2dca..6779c876 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -23,14 +27,15 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // + namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO { - using System; - using System.Globalization; - using System.IO; using LinearAlgebra.IO; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Globalization; + using System.IO; /// /// Delimited writer tests. @@ -44,19 +49,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO [Test] public void CanWriteCommaDelimitedData() { - var matrix = new DenseMatrix(new[,] { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f }, { 7.7f, 8.8f, 9.9f } }); + var matrix = DenseMatrix.OfArray(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); var writer = new DelimitedWriter(',') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1.1,2.2,3.3" + Environment.NewLine - + "4.4,5.5,6.6" + Environment.NewLine - + "7.7,8.8,9.9"; + + "4.4,5.5,6.6" + Environment.NewLine + + "7.7,8.8,9.9"; Assert.AreEqual(expected, text); } @@ -66,20 +71,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO [Test] public void CanWritePeriodDelimitedData() { - var matrix = new DenseMatrix(new[,] { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f }, { 7.7f, 8.8f, 9.9f } }); + var matrix = DenseMatrix.OfArray(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); var culture = new CultureInfo("tr-TR"); var writer = new DelimitedWriter('.') - { - CultureInfo = culture - }; + { + CultureInfo = culture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1,1.2,2.3,3" + Environment.NewLine - + "4,4.5,5.6,6" + Environment.NewLine - + "7,7.8,8.9,9"; + + "4,4.5,5.6,6" + Environment.NewLine + + "7,7.8,8.9,9"; Assert.AreEqual(expected, text); } @@ -89,19 +94,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO [Test] public void CanWriteSpaceDelimitedData() { - var matrix = new SparseMatrix(new[,] { { 1.1f, 0, 0 }, { 0, 5.5f, 0 }, { 0, 0, 9.9f } }); + var matrix = SparseMatrix.OfArray(new[,] {{1.1f, 0, 0}, {0, 5.5f, 0}, {0, 0, 9.9f}}); var writer = new DelimitedWriter(' ') - { - CultureInfo = CultureInfo.InvariantCulture - }; + { + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = @"1.1 0 0" + Environment.NewLine - + "0 5.5 0" + Environment.NewLine - + "0 0 9.9"; + + "0 5.5 0" + Environment.NewLine + + "0 0 9.9"; Assert.AreEqual(expected, text); } @@ -111,22 +116,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO [Test] public void CanWriteTabDelimitedData() { - var matrix = new UserDefinedMatrix(new[,] { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f }, { 7.7f, 8.8f, 9.9f } }); - var headers = new[] { "a", "b", "c" }; + var matrix = new UserDefinedMatrix(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); + var headers = new[] {"a", "b", "c"}; var writer = new DelimitedWriter('\t') - { - ColumnHeaders = headers, - CultureInfo = CultureInfo.InvariantCulture - }; + { + ColumnHeaders = headers, + CultureInfo = CultureInfo.InvariantCulture + }; var stream = new MemoryStream(); writer.WriteMatrix(matrix, stream); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); var expected = "a\tb\tc" + Environment.NewLine - + "1.1\t2.2\t3.3" + Environment.NewLine - + "4.4\t5.5\t6.6" + Environment.NewLine - + "7.7\t8.8\t9.9"; + + "1.1\t2.2\t3.3" + Environment.NewLine + + "4.4\t5.5\t6.6" + Environment.NewLine + + "7.7\t8.8\t9.9"; Assert.AreEqual(expected, text); } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs index ad214129..b7594b17 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs @@ -1,12 +1,41 @@ -using System.Linq; -using MathNet.Numerics.Distributions; -using MathNet.Numerics.Random; +// +// 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. +// namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using LinearAlgebra.Single; + using Distributions; using LinearAlgebra.Generic; + using LinearAlgebra.Single; + using Numerics.Random; using NUnit.Framework; + using System.Linq; [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory @@ -14,17 +43,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Datapoints] Matrix[] _matrices = new Matrix[] { - new DenseMatrix(new[,] {{1f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f}, {0f, 1.1f}, {-4.4f, 5.5f}}), - new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}}), - new DenseMatrix(new[,] {{1f, 2f, 3f}, {2f, 2f, 0f}, {3f, 0f, 3f}}), + DenseMatrix.OfArray(new[,] {{1f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f}, {0f, 1.1f}, {-4.4f, 5.5f}}), + DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}}), + DenseMatrix.OfArray(new[,] {{1f, 2f, 3f}, {2f, 2f, 0f}, {3f, 0f, 3f}}), - new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), - new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 0f, 0f}, {-2f, 0f, 0f}}), - new SparseMatrix(new[,] {{-1.1f, 0f, 0f}, {0f, 1.1f, 2.2f}}), + SparseMatrix.OfArray(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), + SparseMatrix.OfArray(new[,] {{7f, 1f, 2f}, {1f, 0f, 0f}, {-2f, 0f, 0f}}), + SparseMatrix.OfArray(new[,] {{-1.1f, 0f, 0f}, {0f, 1.1f, 2.2f}}), new DiagonalMatrix(3, 3, new[] {1f, -2f, 1.5f}), new DiagonalMatrix(3, 3, new[] {1f, 0f, -1.5f}), diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs index 87dafa45..f7744f10 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// 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 @@ -12,8 +14,10 @@ // 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 @@ -26,10 +30,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { - using System; - using System.Collections.Generic; using LinearAlgebra.Single; using NUnit.Framework; + using System; + using System.Collections.Generic; /// /// Sparse matrix tests. @@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// A matrix with the given values. protected override Matrix CreateMatrix(float[,] data) { - return new SparseMatrix(data); + return SparseMatrix.OfArray(data); } /// @@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanCreateMatrixFrom1DArray() { var testData = new Dictionary - { - { "Singular3x3", new SparseMatrix(3, 3, new float[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) }, - { "Square3x3", new SparseMatrix(3, 3, new[] { -1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f, -3.3f, 2.2f, 6.6f }) }, - { "Square4x4", new SparseMatrix(4, 4, new[] { -1.1f, 0.0f, 1.0f, -4.4f, -2.2f, 1.1f, 2.1f, 5.5f, -3.3f, 2.2f, 6.2f, 6.6f, -4.4f, 3.3f, 4.3f, -7.7f }) }, - { "Tall3x2", new SparseMatrix(3, 2, new[] { -1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f }) }, - { "Wide2x3", new SparseMatrix(2, 3, new[] { -1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f }) } - }; + { + {"Singular3x3", SparseMatrix.OfColumnMajor(3, 3, new float[] {1, 1, 1, 1, 1, 1, 2, 2, 2})}, + {"Square3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {-1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f, -3.3f, 2.2f, 6.6f})}, + {"Square4x4", SparseMatrix.OfColumnMajor(4, 4, new[] {-1.1f, 0.0f, 1.0f, -4.4f, -2.2f, 1.1f, 2.1f, 5.5f, -3.3f, 2.2f, 6.2f, 6.6f, -4.4f, 3.3f, 4.3f, -7.7f})}, + {"Tall3x2", SparseMatrix.OfColumnMajor(3, 2, new[] {-1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f})}, + {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {-1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f})} + }; foreach (var name in testData.Keys) { @@ -106,8 +110,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void MatrixFrom1DArrayIsCopy() { // Sparse Matrix copies values from float[], but no remember reference. - var data = new float[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }; - var matrix = new SparseMatrix(3, 3, data); + var data = new float[] {1, 1, 1, 1, 1, 1, 2, 2, 2}; + var matrix = SparseMatrix.OfColumnMajor(3, 3, data); matrix[0, 0] = 10.0f; Assert.AreNotEqual(10.0f, data[0]); } @@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Test] public void MatrixFrom2DArrayIsCopy() { - var matrix = new SparseMatrix(TestData2D["Singular3x3"]); + var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]); matrix[0, 0] = 10.0f; Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]); } @@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [TestCase("Wide2x3")] public void CanCreateMatrixFrom2DArray(string name) { - var matrix = new SparseMatrix(TestData2D[name]); + var matrix = SparseMatrix.OfArray(TestData2D[name]); for (var i = 0; i < TestData2D[name].GetLength(0); i++) { for (var j = 0; j < TestData2D[name].GetLength(1); j++) @@ -186,7 +190,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single { for (var j = 0; j < matrix.ColumnCount; j++) { - var value = rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10); + var value = rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10)*rnd.Next(10); if (value != 0) { nonzero++; @@ -206,7 +210,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanAddSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); var sum1 = m1 + m2; var sum2 = m2 + m1; Assert.IsTrue(sum1.Equals(m2)); @@ -216,27 +220,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single sparseResult.Add(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); sparseResult.Add(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); m1.Add(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(sum1)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); sparseResult.Add(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(2 * sum1)); + Assert.IsTrue(sparseResult.Equals(2*sum1)); var denseResult = new DenseMatrix(1, 3); denseResult.Add(m2, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - denseResult = new DenseMatrix(new float[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new float[,] {{0, 1, 1}}); denseResult.Add(m1, denseResult); Assert.IsTrue(denseResult.Equals(sum1)); - var m3 = new DenseMatrix(new float[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new float[,] {{0, 1, 1}}); var sum3 = m1 + m3; var sum4 = m3 + m1; Assert.IsTrue(sum3.Equals(m3)); @@ -250,7 +254,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single public void CanSubtractSparseMatricesBothWays() { var m1 = new SparseMatrix(1, 3); - var m2 = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + var m2 = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); var diff1 = m1 - m2; var diff2 = m2 - m1; Assert.IsTrue(diff1.Equals(m2.Negate())); @@ -260,27 +264,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single sparseResult.Subtract(m2, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); sparseResult.Subtract(m1, sparseResult); Assert.IsTrue(sparseResult.Equals(diff2)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); m1.Subtract(sparseResult, sparseResult); Assert.IsTrue(sparseResult.Equals(diff1)); - sparseResult = new SparseMatrix(new float[,] { { 0, 1, 1 } }); + sparseResult = SparseMatrix.OfArray(new float[,] { { 0, 1, 1 } }); sparseResult.Subtract(sparseResult, sparseResult); - Assert.IsTrue(sparseResult.Equals(0 * diff1)); + Assert.IsTrue(sparseResult.Equals(0*diff1)); var denseResult = new DenseMatrix(1, 3); denseResult.Subtract(m2, denseResult); Assert.IsTrue(denseResult.Equals(diff1)); - denseResult = new DenseMatrix(new float[,] { { 0, 1, 1 } }); + denseResult = DenseMatrix.OfArray(new float[,] {{0, 1, 1}}); denseResult.Subtract(m1, denseResult); Assert.IsTrue(denseResult.Equals(diff2)); - var m3 = new DenseMatrix(new float[,] { { 0, 1, 1 } }); + var m3 = DenseMatrix.OfArray(new float[,] {{0, 1, 1}}); var diff3 = m1 - m3; var diff4 = m3 - m1; Assert.IsTrue(diff3.Equals(m3.Negate()));