Browse Source

LA: matrix construction rework #104

v2
Christoph Ruegg 14 years ago
parent
commit
af476c29f1
  1. 2
      src/Examples/LinearAlgebra/DirectSolvers.cs
  2. 2
      src/Examples/LinearAlgebra/Factorization/Cholesky.cs
  3. 2
      src/Examples/LinearAlgebra/Factorization/Evd.cs
  4. 2
      src/Examples/LinearAlgebra/Factorization/LU.cs
  5. 2
      src/Examples/LinearAlgebra/Factorization/QR.cs
  6. 2
      src/Examples/LinearAlgebra/Factorization/Svd.cs
  7. 2
      src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs
  8. 2
      src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs
  9. 2
      src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs
  10. 2
      src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs
  11. 2
      src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs
  12. 4
      src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs
  13. 2
      src/Examples/LinearAlgebra/MatrixInitialization.cs
  14. 2
      src/Examples/LinearAlgebra/MatrixNorms.cs
  15. 2
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  16. 4
      src/FSharpUnitTests/DenseMatrixTests.fs
  17. 4
      src/FSharpUnitTests/MatrixTests.fs
  18. 2
      src/Numerics/Distributions/Multivariate/Wishart.cs
  19. 77
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  20. 2
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  21. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs
  22. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs
  23. 95
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  24. 2
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  25. 77
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  26. 2
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  27. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs
  28. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs
  29. 95
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  30. 2
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  31. 77
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  32. 2
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  33. 2
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs
  34. 2
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs
  35. 95
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  36. 2
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  37. 77
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  38. 2
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  39. 2
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs
  40. 2
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs
  41. 95
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  42. 2
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  43. 2
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  44. 2
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  45. 2
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
  46. 2
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs
  47. 12
      src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs
  48. 12
      src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs
  49. 12
      src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs
  50. 12
      src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs
  51. 28
      src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs
  52. 81
      src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs
  53. 66
      src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs
  54. 57
      src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs
  55. 64
      src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs
  56. 32
      src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs
  57. 85
      src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs
  58. 66
      src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs
  59. 57
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs
  60. 64
      src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs
  61. 28
      src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs
  62. 94
      src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs
  63. 66
      src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs
  64. 57
      src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs
  65. 64
      src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs
  66. 32
      src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs
  67. 85
      src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs
  68. 67
      src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs
  69. 57
      src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs
  70. 64
      src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs

2
src/Examples/LinearAlgebra/DirectSolvers.cs

@ -73,7 +73,7 @@ namespace Examples.LinearAlgebraExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/Factorization/Cholesky.cs

@ -70,7 +70,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create square, symmetric, positive definite matrix // 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(@"Initial square, symmetric, positive definite matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/Factorization/Evd.cs

@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create square symmetric matrix // 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(@"Initial square symmetric matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/Factorization/LU.cs

@ -73,7 +73,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create square matrix // 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(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/Factorization/QR.cs

@ -72,7 +72,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create 3 x 2 matrix // 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(@"Initial 3x2 matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/Factorization/Svd.cs

@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.FactorizationExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create square matrix // 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(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs

@ -77,7 +77,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs

@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs

@ -75,7 +75,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs

@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs

@ -76,7 +76,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples
// 4*x + 1*y + 5*z = 43 // 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients // 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(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

4
src/Examples/LinearAlgebra/MatrixArithmeticOperations.cs

@ -73,13 +73,13 @@ namespace Examples.LinearAlgebraExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create matrix "A" // 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(@"Matrix A");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();
// Create matrix "B" // 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(@"Matrix B");
Console.WriteLine(matrixB.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrixB.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/Examples/LinearAlgebra/MatrixInitialization.cs

@ -63,7 +63,7 @@ namespace Examples.LinearAlgebraExamples
public void Run() 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. // 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. // 2. Initialize a new instance of the empty square matrix with a given order.
var matrix2 = new DenseMatrix(3); var matrix2 = new DenseMatrix(3);

2
src/Examples/LinearAlgebra/MatrixNorms.cs

@ -68,7 +68,7 @@ namespace Examples.LinearAlgebraExamples
formatProvider.TextInfo.ListSeparator = " "; formatProvider.TextInfo.ListSeparator = " ";
// Create square matrix // 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(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine(); Console.WriteLine();

2
src/FSharp/LinearAlgebra.Double.Matrix.fs

@ -275,7 +275,7 @@ module DenseMatrix =
A A
/// Create a matrix from a 2D array of floating point numbers. /// 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. /// Create a matrix with the given entries.
let inline initDense (n: int) (m: int) (es: #seq<int * int * float>) = let inline initDense (n: int) (m: int) (es: #seq<int * int * float>) =

4
src/FSharpUnitTests/DenseMatrixTests.fs

@ -9,10 +9,10 @@ open MathNet.Numerics.LinearAlgebra.Double
module DenseMatrixTests = module DenseMatrixTests =
/// A small uniform vector. /// 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 /// 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) )
[<Test>] [<Test>]
let ``DenseMatrix.init`` () = let ``DenseMatrix.init`` () =

4
src/FSharpUnitTests/MatrixTests.fs

@ -9,11 +9,11 @@ open MathNet.Numerics.LinearAlgebra.Double
module MatrixTests = module MatrixTests =
/// A small uniform vector. /// 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) let failingFoldBackM = DenseMatrix.init 2 3 (fun i j -> 1.0)
/// A large vector with increasingly large entries /// 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) )
[<Test>] [<Test>]
let ``Matrix.GetSlice`` () = let ``Matrix.GetSlice`` () =

2
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 // First generate a lower triangular matrix with Sqrt(Chi-Squares) on the diagonal
// and normal distributed variables in the lower triangle. // 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++) for (var d = 0; d < count; d++)
{ {
a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (nu - d)/2.0, 0.5))); a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (nu - d)/2.0, 0.5)));

77
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, Complex[] storage)
: this(new DenseColumnMajorMatrixStorage<Complex>(rows, columns, storage))
{
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfMatrix(Matrix<Complex> matrix)
{
var storage = new DenseColumnMajorMatrixStorage<Complex>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new DenseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfArray(Complex[,] array)
{
var storage = new DenseColumnMajorMatrixStorage<Complex>(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);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable<Complex> columnMajor)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnMajorEnumerable(rows, columns, columnMajor));
}
/// <summary> /// <summary>
/// Create a new dense matrix with the given number of rows and columns. /// 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. /// All cells of the matrix will be initialized to the provided value.
/// Zero-length matrices are not supported. /// Zero-length matrices are not supported.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the row or column count is less than one.</exception> /// <exception cref="ArgumentException">If the row or column count is less than one.</exception>
[Obsolete("Scheduled for removal in v3.0.")]
public DenseMatrix(int rows, int columns, Complex value) public DenseMatrix(int rows, int columns, Complex value)
: this(rows, columns) : this(rows, columns)
{ {
@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, Complex[] storage)
: this(new DenseColumnMajorMatrixStorage<Complex>(rows, columns, storage))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given two-dimensional array. /// Create a new dense matrix as a copy of the given two-dimensional array.
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Complex[,] array) public DenseMatrix(Complex[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
} }
} }
/// <summary>
/// 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.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex> columnMajor)
: this(DenseColumnMajorMatrixStorage<Complex>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given other matrix. /// Create a new dense matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Matrix<Complex> matrix) public DenseMatrix(Matrix<Complex> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public DenseVector(IEnumerable<Complex> other) public DenseVector(IEnumerable<Complex> other)
: this(DenseVectorStorage<Complex>.FromEnumerable(other)) : this(DenseVectorStorage<Complex>.OfEnumerable(other))
{ {
} }

2
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"); 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. // The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A) // spaceLeft = lfilNnz * nnz(A)

2
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"); throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
} }
_decompositionLU = new SparseMatrix(matrix); _decompositionLU = SparseMatrix.OfMatrix(matrix);
// M == A // M == A
// for i = 2, ... , n do // for i = 2, ... , n do

95
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -70,6 +70,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_storage = storage; _storage = storage;
} }
/// <summary>
/// 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.
/// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception>
public SparseMatrix(int order)
: this(order, order)
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix with the given number of rows and columns. /// Create a new sparse matrix with the given number of rows and columns.
/// All cells of the matrix will be initialized to zero. /// All cells of the matrix will be initialized to zero.
@ -82,14 +93,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
/// <summary> /// <summary>
/// Create a new square sparse matrix with the given number of rows and columns. /// Create a new sparse matrix as a copy of the given other matrix.
/// All cells of the matrix will be initialized to zero. /// This new matrix will be independent from the other matrix.
/// Zero-length matrices are not supported. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception> public static SparseMatrix OfMatrix(Matrix<Complex> matrix)
public SparseMatrix(int order) {
: this(order, order) var storage = new SparseCompressedRowMatrixStorage<Complex>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new SparseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfArray(Complex[,] array)
{
var storage = new SparseCompressedRowMatrixStorage<Complex>(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);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable<Complex> rowMajor)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowMajorEnumerable(rows, columns, rowMajor));
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfColumnMajor(int rows, int columns, Complex[] array)
{
if (rows * columns > array.Length)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
var storage = new SparseCompressedRowMatrixStorage<Complex>(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);
} }
/// <summary> /// <summary>
@ -137,7 +204,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/> /// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
[Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")]
public SparseMatrix(int rows, int columns, Complex[] array) public SparseMatrix(int rows, int columns, Complex[] array)
: this(rows, columns) : this(rows, columns)
{ {
@ -160,6 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Complex[,] array) public SparseMatrix(Complex[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -172,22 +241,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
} }
/// <summary>
/// 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.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<Complex> rowMajor)
: this(SparseCompressedRowMatrixStorage<Complex>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given other matrix. /// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Matrix<Complex> matrix) public SparseMatrix(Matrix<Complex> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public SparseVector(IEnumerable<Complex> other) public SparseVector(IEnumerable<Complex> other)
: this(SparseVectorStorage<Complex>.FromEnumerable(other)) : this(SparseVectorStorage<Complex>.OfEnumerable(other))
{ {
} }

77
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, Complex32[] storage)
: this(new DenseColumnMajorMatrixStorage<Complex32>(rows, columns, storage))
{
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfMatrix(Matrix<Complex32> matrix)
{
var storage = new DenseColumnMajorMatrixStorage<Complex32>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new DenseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfArray(Complex32[,] array)
{
var storage = new DenseColumnMajorMatrixStorage<Complex32>(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);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable<Complex32> columnMajor)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnMajorEnumerable(rows, columns, columnMajor));
}
/// <summary> /// <summary>
/// Create a new dense matrix with the given number of rows and columns. /// 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. /// All cells of the matrix will be initialized to the provided value.
/// Zero-length matrices are not supported. /// Zero-length matrices are not supported.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the row or column count is less than one.</exception> /// <exception cref="ArgumentException">If the row or column count is less than one.</exception>
[Obsolete("Scheduled for removal in v3.0.")]
public DenseMatrix(int rows, int columns, Complex32 value) public DenseMatrix(int rows, int columns, Complex32 value)
: this(rows, columns) : this(rows, columns)
{ {
@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, Complex32[] storage)
: this(new DenseColumnMajorMatrixStorage<Complex32>(rows, columns, storage))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given two-dimensional array. /// Create a new dense matrix as a copy of the given two-dimensional array.
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Complex32[,] array) public DenseMatrix(Complex32[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
} }
} }
/// <summary>
/// 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.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex32> columnMajor)
: this(DenseColumnMajorMatrixStorage<Complex32>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given other matrix. /// Create a new dense matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Matrix<Complex32> matrix) public DenseMatrix(Matrix<Complex32> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public DenseVector(IEnumerable<Complex32> other) public DenseVector(IEnumerable<Complex32> other)
: this(DenseVectorStorage<Complex32>.FromEnumerable(other)) : this(DenseVectorStorage<Complex32>.OfEnumerable(other))
{ {
} }

2
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"); 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. // The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A) // spaceLeft = lfilNnz * nnz(A)

2
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"); throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
} }
_decompositionLU = new SparseMatrix(matrix); _decompositionLU = SparseMatrix.OfMatrix(matrix);
// M == A // M == A
// for i = 2, ... , n do // for i = 2, ... , n do

95
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -70,6 +70,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
_storage = storage; _storage = storage;
} }
/// <summary>
/// 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.
/// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception>
public SparseMatrix(int order)
: this(order, order)
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix with the given number of rows and columns. /// Create a new sparse matrix with the given number of rows and columns.
/// All cells of the matrix will be initialized to zero. /// All cells of the matrix will be initialized to zero.
@ -82,14 +93,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
/// <summary> /// <summary>
/// Create a new square sparse matrix with the given number of rows and columns. /// Create a new sparse matrix as a copy of the given other matrix.
/// All cells of the matrix will be initialized to zero. /// This new matrix will be independent from the other matrix.
/// Zero-length matrices are not supported. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception> public static SparseMatrix OfMatrix(Matrix<Complex32> matrix)
public SparseMatrix(int order) {
: this(order, order) var storage = new SparseCompressedRowMatrixStorage<Complex32>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new SparseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfArray(Complex32[,] array)
{
var storage = new SparseCompressedRowMatrixStorage<Complex32>(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);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable<Complex32> rowMajor)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowMajorEnumerable(rows, columns, rowMajor));
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfColumnMajor(int rows, int columns, Complex32[] array)
{
if (rows * columns > array.Length)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
var storage = new SparseCompressedRowMatrixStorage<Complex32>(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);
} }
/// <summary> /// <summary>
@ -137,7 +204,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/> /// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
[Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")]
public SparseMatrix(int rows, int columns, Complex32[] array) public SparseMatrix(int rows, int columns, Complex32[] array)
: this(rows, columns) : this(rows, columns)
{ {
@ -160,6 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Complex32[,] array) public SparseMatrix(Complex32[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -172,22 +241,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
} }
/// <summary>
/// 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.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<Complex32> rowMajor)
: this(SparseCompressedRowMatrixStorage<Complex32>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given other matrix. /// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Matrix<Complex32> matrix) public SparseMatrix(Matrix<Complex32> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public SparseVector(IEnumerable<Complex32> other) public SparseVector(IEnumerable<Complex32> other)
: this(SparseVectorStorage<Complex32>.FromEnumerable(other)) : this(SparseVectorStorage<Complex32>.OfEnumerable(other))
{ {
} }

77
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, double[] storage)
: this(new DenseColumnMajorMatrixStorage<double>(rows, columns, storage))
{
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfMatrix(Matrix<double> matrix)
{
var storage = new DenseColumnMajorMatrixStorage<double>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new DenseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfArray(double[,] array)
{
var storage = new DenseColumnMajorMatrixStorage<double>(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);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable<double> columnMajor)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnMajorEnumerable(rows, columns, columnMajor));
}
/// <summary> /// <summary>
/// Create a new dense matrix with the given number of rows and columns. /// 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. /// All cells of the matrix will be initialized to the provided value.
/// Zero-length matrices are not supported. /// Zero-length matrices are not supported.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the row or column count is less than one.</exception> /// <exception cref="ArgumentException">If the row or column count is less than one.</exception>
[Obsolete("Scheduled for removal in v3.0.")]
public DenseMatrix(int rows, int columns, double value) public DenseMatrix(int rows, int columns, double value)
: this(rows, columns) : this(rows, columns)
{ {
@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, double[] storage)
: this(new DenseColumnMajorMatrixStorage<double>(rows, columns, storage))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given two-dimensional array. /// Create a new dense matrix as a copy of the given two-dimensional array.
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public DenseMatrix(double[,] array) public DenseMatrix(double[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -145,23 +188,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
} }
} }
/// <summary>
/// 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.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<double> columnMajor)
: this(DenseColumnMajorMatrixStorage<double>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given other matrix. /// Create a new dense matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Matrix<double> matrix) public DenseMatrix(Matrix<double> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public DenseVector(IEnumerable<double> other) public DenseVector(IEnumerable<double> other)
: this(DenseVectorStorage<double>.FromEnumerable(other)) : this(DenseVectorStorage<double>.OfEnumerable(other))
{ {
} }

2
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"); 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. // The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A) // spaceLeft = lfilNnz * nnz(A)

2
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"); throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
} }
_decompositionLU = new SparseMatrix(matrix); _decompositionLU = SparseMatrix.OfMatrix(matrix);
// M == A // M == A
// for i = 2, ... , n do // for i = 2, ... , n do

95
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -69,6 +69,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
_storage = storage; _storage = storage;
} }
/// <summary>
/// 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.
/// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception>
public SparseMatrix(int order)
: this(order, order)
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix with the given number of rows and columns. /// Create a new sparse matrix with the given number of rows and columns.
/// All cells of the matrix will be initialized to zero. /// All cells of the matrix will be initialized to zero.
@ -81,14 +92,70 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Create a new square sparse matrix with the given number of rows and columns. /// Create a new sparse matrix as a copy of the given other matrix.
/// All cells of the matrix will be initialized to zero. /// This new matrix will be independent from the other matrix.
/// Zero-length matrices are not supported. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception> public static SparseMatrix OfMatrix(Matrix<double> matrix)
public SparseMatrix(int order) {
: this(order, order) var storage = new SparseCompressedRowMatrixStorage<double>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new SparseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfArray(double[,] array)
{
var storage = new SparseCompressedRowMatrixStorage<double>(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);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable<double> rowMajor)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowMajorEnumerable(rows, columns, rowMajor));
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfColumnMajor(int rows, int columns, double[] array)
{
if (rows * columns > array.Length)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
var storage = new SparseCompressedRowMatrixStorage<double>(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);
} }
/// <summary> /// <summary>
@ -136,7 +203,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/> /// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
[Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")]
public SparseMatrix(int rows, int columns, double[] array) public SparseMatrix(int rows, int columns, double[] array)
: this(rows, columns) : this(rows, columns)
{ {
@ -159,6 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public SparseMatrix(double[,] array) public SparseMatrix(double[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -171,22 +240,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
} }
/// <summary>
/// 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.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<double> rowMajor)
: this(SparseCompressedRowMatrixStorage<double>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given other matrix. /// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Matrix<double> matrix) public SparseMatrix(Matrix<double> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public SparseVector(IEnumerable<double> other) public SparseVector(IEnumerable<double> other)
: this(SparseVectorStorage<double>.FromEnumerable(other)) : this(SparseVectorStorage<double>.OfEnumerable(other))
{ {
} }

77
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -103,12 +103,65 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, float[] storage)
: this(new DenseColumnMajorMatrixStorage<float>(rows, columns, storage))
{
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfMatrix(Matrix<float> matrix)
{
var storage = new DenseColumnMajorMatrixStorage<float>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new DenseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfArray(float[,] array)
{
var storage = new DenseColumnMajorMatrixStorage<float>(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);
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnMajor(int rows, int columns, IEnumerable<float> columnMajor)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnMajorEnumerable(rows, columns, columnMajor));
}
/// <summary> /// <summary>
/// Create a new dense matrix with the given number of rows and columns. /// 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. /// All cells of the matrix will be initialized to the provided value.
/// Zero-length matrices are not supported. /// Zero-length matrices are not supported.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the row or column count is less than one.</exception> /// <exception cref="ArgumentException">If the row or column count is less than one.</exception>
[Obsolete("Scheduled for removal in v3.0.")]
public DenseMatrix(int rows, int columns, float value) public DenseMatrix(int rows, int columns, float value)
: this(rows, columns) : this(rows, columns)
{ {
@ -118,22 +171,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
} }
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/>
public DenseMatrix(int rows, int columns, float[] storage)
: this(new DenseColumnMajorMatrixStorage<float>(rows, columns, storage))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given two-dimensional array. /// Create a new dense matrix as a copy of the given two-dimensional array.
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public DenseMatrix(float[,] array) public DenseMatrix(float[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -146,22 +189,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
} }
/// <summary>
/// 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.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<float> columnMajor)
: this(DenseColumnMajorMatrixStorage<float>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given other matrix. /// Create a new dense matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use DenseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public DenseMatrix(Matrix<float> matrix) public DenseMatrix(Matrix<float> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public DenseVector(IEnumerable<float> other) public DenseVector(IEnumerable<float> other)
: this(DenseVectorStorage<float>.FromEnumerable(other)) : this(DenseVectorStorage<float>.OfEnumerable(other))
{ {
} }

2
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"); 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. // The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A) // spaceLeft = lfilNnz * nnz(A)

2
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"); throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
} }
_decompositionLU = new SparseMatrix(matrix); _decompositionLU = SparseMatrix.OfMatrix(matrix);
// M == A // M == A
// for i = 2, ... , n do // for i = 2, ... , n do

95
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -68,6 +68,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
_storage = storage; _storage = storage;
} }
/// <summary>
/// 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.
/// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception>
public SparseMatrix(int order)
: this(order, order)
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix with the given number of rows and columns. /// Create a new sparse matrix with the given number of rows and columns.
@ -81,14 +92,70 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
/// <summary> /// <summary>
/// Create a new square sparse matrix with the given number of rows and columns. /// Create a new sparse matrix as a copy of the given other matrix.
/// All cells of the matrix will be initialized to zero. /// This new matrix will be independent from the other matrix.
/// Zero-length matrices are not supported. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <exception cref="ArgumentException">If the order is less than one.</exception> public static SparseMatrix OfMatrix(Matrix<float> matrix)
public SparseMatrix(int order)
: this(order, order)
{ {
var storage = new SparseCompressedRowMatrixStorage<float>(matrix.RowCount, matrix.ColumnCount);
matrix.Storage.CopyToUnchecked(storage, skipClearing: true);
return new SparseMatrix(storage);
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfArray(float[,] array)
{
var storage = new SparseCompressedRowMatrixStorage<float>(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);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfRowMajor(int rows, int columns, IEnumerable<float> rowMajor)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowMajorEnumerable(rows, columns, rowMajor));
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static SparseMatrix OfColumnMajor(int rows, int columns, float[] array)
{
if (rows * columns > array.Length)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
var storage = new SparseCompressedRowMatrixStorage<float>(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);
} }
/// <summary> /// <summary>
@ -136,7 +203,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Row-major_order"/> /// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
[Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")]
public SparseMatrix(int rows, int columns, float[] array) public SparseMatrix(int rows, int columns, float[] array)
: this(rows, columns) : this(rows, columns)
{ {
@ -159,6 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// This new matrix will be independent from the provided array. /// This new matrix will be independent from the provided array.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfArray instead. Scheduled for removal in v3.0.")]
public SparseMatrix(float[,] array) public SparseMatrix(float[,] array)
: this(array.GetLength(0), array.GetLength(1)) : this(array.GetLength(0), array.GetLength(1))
{ {
@ -171,22 +240,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
} }
/// <summary>
/// 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.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<float> rowMajor)
: this(SparseCompressedRowMatrixStorage<float>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given other matrix. /// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix. /// This new matrix will be independent from the other matrix.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
[Obsolete("Use SparseMatrix.OfMatrix instead. Scheduled for removal in v3.0.")]
public SparseMatrix(Matrix<float> matrix) public SparseMatrix(Matrix<float> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {

2
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. /// A new memory block will be allocated for storing the vector.
/// </summary> /// </summary>
public SparseVector(IEnumerable<float> other) public SparseVector(IEnumerable<float> other)
: this(SparseVectorStorage<float>.FromEnumerable(other)) : this(SparseVectorStorage<float>.OfEnumerable(other))
{ {
} }

2
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -101,7 +101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// INITIALIZATION // INITIALIZATION
public static DenseColumnMajorMatrixStorage<T> FromColumnMajorEnumerable(int rows, int columns, IEnumerable<T> data) public static DenseColumnMajorMatrixStorage<T> OfColumnMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{ {
if (data == null) if (data == null)
{ {

2
src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// INITIALIZATION // INITIALIZATION
public static DenseVectorStorage<T> FromEnumerable(IEnumerable<T> data) public static DenseVectorStorage<T> OfEnumerable(IEnumerable<T> data)
{ {
if (data == null) if (data == null)
{ {

2
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -363,7 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// INITIALIZATION // INITIALIZATION
public static SparseCompressedRowMatrixStorage<T> FromRowMajorEnumerable(int rows, int columns, IEnumerable<T> data) public static SparseCompressedRowMatrixStorage<T> OfRowMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{ {
if (data == null) if (data == null)
{ {

2
src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

@ -284,7 +284,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// INITIALIZATION // INITIALIZATION
public static SparseVectorStorage<T> FromEnumerable(IEnumerable<T> data) public static SparseVectorStorage<T> OfEnumerable(IEnumerable<T> data)
{ {
if (data == null) if (data == null)
{ {

12
src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs

@ -63,12 +63,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex
/// </summary> /// </summary>
readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix> readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix>
{ {
{"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}})}, {"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", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2, -3.3}, {0.0, 1.1, 2.2}, {-4.4, 5.5, 6.6}})}, {"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", 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}})}, {"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", 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}})}, {"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", new DenseMatrix(new[,] {{new Complex(-1.1, 0), -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, {"Tall3x2", DenseMatrix.OfArray(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}})}, {"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)}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)},
{"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)},
{"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)}

12
src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs

@ -63,12 +63,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32
/// </summary> /// </summary>
readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix> readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix>
{ {
{"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}})}, {"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", 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}})}, {"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", 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}})}, {"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", 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}})}, {"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", new DenseMatrix(new[,] {{new Complex32(-1.1f, 0.0f), -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, {"Tall3x2", DenseMatrix.OfArray(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}})}, {"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)}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)},
{"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)},
{"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)}

12
src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs

@ -62,12 +62,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double
/// </summary> /// </summary>
readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix> readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix>
{ {
{"Singular3x3", new DenseMatrix(new[,] {{1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}, {1.0, 1.0, 2.0}})}, {"Singular3x3", DenseMatrix.OfArray(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}})}, {"Square3x3", DenseMatrix.OfArray(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}})}, {"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", 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}})}, {"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", new DenseMatrix(new[,] {{-1.1, -2.2}, {0.0, 1.1}, {-4.4, 5.5}})}, {"Tall3x2", DenseMatrix.OfArray(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}})}, {"Wide2x3", DenseMatrix.OfArray(new[,] {{-1.1, -2.2, -3.3}, {0.0, 1.1, 2.2}})},
{"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)},
{"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)},
{"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)}

12
src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs

@ -62,12 +62,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single
/// </summary> /// </summary>
readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix> readonly IDictionary<string, DenseMatrix> _matrices = new Dictionary<string, DenseMatrix>
{ {
{"Singular3x3", new DenseMatrix(new[,] {{1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}, {1.0f, 1.0f, 2.0f}})}, {"Singular3x3", DenseMatrix.OfArray(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}})}, {"Square3x3", DenseMatrix.OfArray(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}})}, {"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", 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}})}, {"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", new DenseMatrix(new[,] {{-1.1f, -2.2f}, {0.0f, 1.1f}, {-4.4f, 5.5f}})}, {"Tall3x2", DenseMatrix.OfArray(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}})}, {"Wide2x3", DenseMatrix.OfArray(new[,] {{-1.1f, -2.2f, -3.3f}, {0.0f, 1.1f, 2.2f}})},
{"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)}, {"Tall50000x10", DenseMatrix.CreateRandom(50000, 10, Dist)},
{"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)}, {"Wide10x50000", DenseMatrix.CreateRandom(10, 50000, Dist)},
{"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)} {"Square1000x1000", DenseMatrix.CreateRandom(1000, 1000, Dist)}

28
src/UnitTests/LinearAlgebraTests/Complex/DenseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(Complex[,] data) protected override Matrix CreateMatrix(Complex[,] data)
{ {
return new DenseMatrix(data); return DenseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "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) }) }, {"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) }) }, {"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) }) }, {"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) }) }, {"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) }) } {"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) foreach (var name in testData.Keys)
{ {
@ -106,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DenseMatrix(3, 3, data);
matrix[0, 0] = new Complex(10.0, 1); matrix[0, 0] = new Complex(10.0, 1);
Assert.AreEqual(new Complex(10.0, 1), data[0]); Assert.AreEqual(new Complex(10.0, 1), data[0]);
@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new DenseMatrix(TestData2D["Singular3x3"]); var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]);
} }
@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) for (var j = 0; j < TestData2D[name].GetLength(1); j++)

81
src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -28,15 +32,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
using LinearAlgebra.Complex; using LinearAlgebra.Complex;
using NUnit.Framework; using NUnit.Framework;
#if PORTABLE
using Threading;
#endif
/// <summary> /// <summary>
/// Diagonal matrix tests. /// Diagonal matrix tests.
/// </summary> /// </summary>
@ -49,14 +48,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public override void SetupMatrices() public override void SetupMatrices()
{ {
TestData2D = new Dictionary<string, Complex[,]> TestData2D = new Dictionary<string, Complex[,]>
{ {
{ "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) } } }, {"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) } } }, {"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) } } }, {"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) } } }, {"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 } } }, {"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 } } } {"Wide2x3", new[,] {{new Complex(-1.1, 1), Complex.Zero, Complex.Zero}, {Complex.Zero, new Complex(1.1, 1), Complex.Zero}}}
}; };
TestMatrices = new Dictionary<string, Matrix>(); TestMatrices = new Dictionary<string, Matrix>();
foreach (var name in TestData2D.Keys) foreach (var name in TestData2D.Keys)
@ -114,13 +113,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanCreateMatrixFromDiagonalArray() public void CanCreateMatrixFromDiagonalArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new DiagonalMatrix(3, 3, new[] { new Complex(1.0, 1), Complex.Zero, new Complex(3.0, 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) }) }, {"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) }) }, {"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) }) }, {"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) }) }, {"Wide2x3", new DiagonalMatrix(2, 3, new[] {new Complex(-1.1, 1), new Complex(1.1, 1)})},
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -134,7 +133,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DiagonalMatrix(5, 5, data);
matrix[0, 0] = new Complex(10.0, 1); matrix[0, 0] = new Complex(10.0, 1);
Assert.AreEqual(new Complex(10.0, 1), data[0]); 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++) 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() public void PermuteMatrixRowsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation));
} }
@ -244,7 +243,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void PermuteMatrixColumnsThrowsInvalidOperationException() public void PermuteMatrixColumnsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation));
} }
@ -261,13 +260,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
var min = Math.Min(data.RowCount, data.ColumnCount); var min = Math.Min(data.RowCount, data.ColumnCount);
for (var i = 0; i < min; i++) 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); result = data.PointwiseDivide(other);
for (var i = 0; i < min; i++) 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() public override void CanComputeFrobeniusNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
} }
@ -296,15 +295,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public override void CanComputeInfinityNorm() public override void CanComputeInfinityNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
} }
@ -314,15 +313,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public override void CanComputeL1Norm() public override void CanComputeL1Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
} }
@ -332,15 +331,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public override void CanComputeL2Norm() public override void CanComputeL2Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
} }
@ -351,11 +350,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanComputeDeterminant() public void CanComputeDeterminant()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
matrix = TestMatrices["Square4x4"]; matrix = TestMatrices["Square4x4"];
denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
} }

66
src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,13 +30,13 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
{ {
using LinearAlgebra.Complex;
using LinearAlgebra.IO;
using NUnit.Framework;
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using LinearAlgebra.Complex;
using LinearAlgebra.IO;
using NUnit.Framework;
/// <summary> /// <summary>
/// Delimited writer tests. /// Delimited writer tests.
@ -46,19 +50,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
[Test] [Test]
public void CanWriteCommaDelimitedData() 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(',') var writer = new DelimitedWriter(',')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine 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 + "(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)"; + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -68,20 +72,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
[Test] [Test]
public void CanWritePeriodDelimitedData() 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 culture = new CultureInfo("tr-TR");
var writer = new DelimitedWriter('.') var writer = new DelimitedWriter('.')
{ {
CultureInfo = culture CultureInfo = culture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine 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 + "(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)"; + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -91,19 +95,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
[Test] [Test]
public void CanWriteSpaceDelimitedData() 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(' ') var writer = new DelimitedWriter(' ')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine 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 + "(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)"; + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -113,22 +117,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
[Test] [Test]
public void CanWriteTabDelimitedData() 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 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 headers = new[] {"a", "b", "c"};
var writer = new DelimitedWriter('\t') var writer = new DelimitedWriter('\t')
{ {
ColumnHeaders = headers, ColumnHeaders = headers,
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "a\tb\tc" + Environment.NewLine var expected = "a\tb\tc" + Environment.NewLine
+ "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + 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 + "(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)"; + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
} }

57
src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs

@ -1,12 +1,41 @@
using System.Linq; // <copyright file="MatrixStructureTheory.cs" company="Math.NET">
using MathNet.Numerics.Distributions; // Math.NET Numerics, part of the Math.NET Project
using MathNet.Numerics.Random; // 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.
// </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {
using Distributions;
using LinearAlgebra.Complex; using LinearAlgebra.Complex;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
using System.Numerics; using System.Numerics;
[TestFixture] [TestFixture]
@ -15,17 +44,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Datapoints] [Datapoints]
Matrix<Complex>[] _matrices = new Matrix<Complex>[] Matrix<Complex>[] _matrices = new Matrix<Complex>[]
{ {
new DenseMatrix(new[,] {{1d, new Complex(1.1d, -4d), 2d}, {1d, 1d, 2d}, {1d, new Complex(1d, 2d), 2d}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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}}), 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}}),
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}}), 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}}),
new DenseMatrix(new[,] {{-1.1d, -2.2d}, {Complex.Zero, 1.1d}, {-4.4d, 5.5d}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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, 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}}), SparseMatrix.OfArray(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}}), SparseMatrix.OfArray(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[,] {{-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, 1d), -2d, 1.5d}),
new DiagonalMatrix(3, 3, new[] {new Complex(1d, 2d), 0d, -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; } get { return Complex.Zero; }
} }
} }
} }

64
src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,11 +30,11 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {
using LinearAlgebra.Complex;
using NUnit.Framework;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using LinearAlgebra.Complex;
using NUnit.Framework;
/// <summary> /// <summary>
/// Sparse matrix tests. /// Sparse matrix tests.
@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(Complex[,] data) protected override Matrix CreateMatrix(Complex[,] data)
{ {
return new SparseMatrix(data); return SparseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "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) }) }, {"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", 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) }) }, {"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", 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) }) }, {"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", 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) }) }, {"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", 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) }) } {"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) foreach (var name in testData.Keys)
{ {
@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void MatrixFrom1DArrayIsCopy() public void MatrixFrom1DArrayIsCopy()
{ {
// Sparse Matrix copies values from Complex[], but no remember reference. // 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 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 matrix = SparseMatrix.OfColumnMajor(3, 3, data);
matrix[0, 0] = new Complex(10.0, 1); matrix[0, 0] = new Complex(10.0, 1);
Assert.AreNotEqual(new Complex(10.0, 1), data[0]); Assert.AreNotEqual(new Complex(10.0, 1), data[0]);
} }
@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new SparseMatrix(TestData2D["Singular3x3"]); var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = new Complex(10.0, 1); matrix[0, 0] = new Complex(10.0, 1);
Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(new Complex(1.0, 1), TestData2D["Singular3x3"][0, 0]);
} }
@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) 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++) 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) if (value != 0)
{ {
nonzero++; nonzero++;
@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanAddSparseMatricesBothWays() public void CanAddSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 sum1 = m1 + m2;
var sum2 = m2 + m1; var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2)); Assert.IsTrue(sum1.Equals(m2));
@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
sparseResult.Add(m2, sparseResult); sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2 * sum1)); Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult); denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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); denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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 sum3 = m1 + m3;
var sum4 = m3 + m1; var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3)); Assert.IsTrue(sum3.Equals(m3));
@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
public void CanSubtractSparseMatricesBothWays() public void CanSubtractSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 diff1 = m1 - m2;
var diff2 = m2 - m1; var diff2 = m2 - m1;
Assert.IsTrue(diff1.Equals(m2.Negate())); Assert.IsTrue(diff1.Equals(m2.Negate()));
@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
sparseResult.Subtract(m2, sparseResult); sparseResult.Subtract(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff2)); 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); m1.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(0 * diff1)); Assert.IsTrue(sparseResult.Equals(0*diff1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Subtract(m2, denseResult); denseResult.Subtract(m2, denseResult);
Assert.IsTrue(denseResult.Equals(diff1)); 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); denseResult.Subtract(m1, denseResult);
Assert.IsTrue(denseResult.Equals(diff2)); 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 diff3 = m1 - m3;
var diff4 = m3 - m1; var diff4 = m3 - m1;
Assert.IsTrue(diff3.Equals(m3.Negate())); Assert.IsTrue(diff3.Equals(m3.Negate()));

32
src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,10 +30,10 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {
using System;
using System.Collections.Generic;
using LinearAlgebra.Complex32; using LinearAlgebra.Complex32;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
/// <summary> /// <summary>
@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(Complex32[,] data) protected override Matrix CreateMatrix(Complex32[,] data)
{ {
return new DenseMatrix(data); return DenseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "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) }) }, {"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) }) }, {"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) }) }, {"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) }) }, {"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) }) } {"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) foreach (var name in testData.Keys)
{ {
@ -106,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DenseMatrix(3, 3, data);
matrix[0, 0] = new Complex32(10.0f, 1); matrix[0, 0] = new Complex32(10.0f, 1);
Assert.AreEqual(new Complex32(10.0f, 1), data[0]); Assert.AreEqual(new Complex32(10.0f, 1), data[0]);
@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new DenseMatrix(TestData2D["Singular3x3"]); var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]);
} }
@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) for (var j = 0; j < TestData2D[name].GetLength(1); j++)

85
src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,17 +30,12 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {
using System;
using System.Collections.Generic;
using System.Linq;
using LinearAlgebra.Complex32; using LinearAlgebra.Complex32;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
#if PORTABLE
using Threading;
#endif
/// <summary> /// <summary>
/// Diagonal matrix tests. /// Diagonal matrix tests.
/// </summary> /// </summary>
@ -49,14 +48,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public override void SetupMatrices() public override void SetupMatrices()
{ {
TestData2D = new Dictionary<string, Complex32[,]> TestData2D = new Dictionary<string, Complex32[,]>
{ {
{ "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) } } }, {"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) } } }, {"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) } } }, {"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) } } }, {"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 } } }, {"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 } } } {"Wide2x3", new[,] {{new Complex32(-1.1f, 1), Complex32.Zero, Complex32.Zero}, {Complex32.Zero, new Complex32(1.1f, 1), Complex32.Zero}}}
}; };
TestMatrices = new Dictionary<string, Matrix>(); TestMatrices = new Dictionary<string, Matrix>();
foreach (var name in TestData2D.Keys) foreach (var name in TestData2D.Keys)
@ -114,13 +113,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanCreateMatrixFromDiagonalArray() public void CanCreateMatrixFromDiagonalArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new DiagonalMatrix(3, 3, new[] { new Complex32(1.0f, 1), Complex32.Zero, new Complex32(3.0f, 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) }) }, {"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) }) }, {"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) }) }, {"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) }) }, {"Wide2x3", new DiagonalMatrix(2, 3, new[] {new Complex32(-1.1f, 1), new Complex32(1.1f, 1)})},
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -134,7 +133,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DiagonalMatrix(5, 5, data);
matrix[0, 0] = new Complex32(10.0f, 1); matrix[0, 0] = new Complex32(10.0f, 1);
Assert.AreEqual(new Complex32(10.0f, 1), data[0]); 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++) 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() public void PermuteMatrixRowsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation));
} }
@ -244,7 +243,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void PermuteMatrixColumnsThrowsInvalidOperationException() public void PermuteMatrixColumnsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation));
} }
@ -261,13 +260,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
var min = Math.Min(data.RowCount, data.ColumnCount); var min = Math.Min(data.RowCount, data.ColumnCount);
for (var i = 0; i < min; i++) 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); result = data.PointwiseDivide(other);
for (var i = 0; i < min; i++) 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() public override void CanComputeFrobeniusNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
} }
@ -296,15 +295,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public override void CanComputeInfinityNorm() public override void CanComputeInfinityNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
} }
@ -314,15 +313,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public override void CanComputeL1Norm() public override void CanComputeL1Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
} }
@ -332,15 +331,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public override void CanComputeL2Norm() public override void CanComputeL2Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
} }
@ -351,11 +350,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanComputeDeterminant() public void CanComputeDeterminant()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
matrix = TestMatrices["Square4x4"]; matrix = TestMatrices["Square4x4"];
denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
} }

66
src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,12 +30,12 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
{ {
using System;
using System.Globalization;
using System.IO;
using LinearAlgebra.Complex32; using LinearAlgebra.Complex32;
using LinearAlgebra.IO; using LinearAlgebra.IO;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Globalization;
using System.IO;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
/// <summary> /// <summary>
@ -46,19 +50,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
[Test] [Test]
public void CanWriteCommaDelimitedData() 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(',') var writer = new DelimitedWriter(',')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine 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 + "(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)"; + "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -68,20 +72,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
[Test] [Test]
public void CanWritePeriodDelimitedData() 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 culture = new CultureInfo("tr-TR");
var writer = new DelimitedWriter('.') var writer = new DelimitedWriter('.')
{ {
CultureInfo = culture CultureInfo = culture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine 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 + "(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)"; + "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -91,19 +95,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
[Test] [Test]
public void CanWriteSpaceDelimitedData() 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(' ') var writer = new DelimitedWriter(' ')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine 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 + "(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)"; + "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -113,22 +117,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
[Test] [Test]
public void CanWriteTabDelimitedData() 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 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 headers = new[] {"a", "b", "c"};
var writer = new DelimitedWriter('\t') var writer = new DelimitedWriter('\t')
{ {
ColumnHeaders = headers, ColumnHeaders = headers,
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "a\tb\tc" + Environment.NewLine var expected = "a\tb\tc" + Environment.NewLine
+ "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + 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 + "(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)"; + "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
} }

57
src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs

@ -1,12 +1,41 @@
using System.Linq; // <copyright file="MatrixStructureTheory.cs" company="Math.NET">
using MathNet.Numerics.Distributions; // Math.NET Numerics, part of the Math.NET Project
using MathNet.Numerics.Random; // 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.
// </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {
using Distributions;
using LinearAlgebra.Complex32; using LinearAlgebra.Complex32;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
[TestFixture] [TestFixture]
@ -15,17 +44,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Datapoints] [Datapoints]
Matrix<Complex32>[] _matrices = new Matrix<Complex32>[] Matrix<Complex32>[] _matrices = new Matrix<Complex32>[]
{ {
new DenseMatrix(new[,] {{1f, new Complex32(1.1f, -4f), 2f}, {1f, 1f, 2f}, {1f, new Complex32(1f, 2f), 2f}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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}}), 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}}),
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}}), 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}}),
new DenseMatrix(new[,] {{-1.1f, -2.2f}, {Complex32.Zero, 1.1f}, {-4.4f, 5.5f}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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, 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}}), SparseMatrix.OfArray(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}}), SparseMatrix.OfArray(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[,] {{-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, 1f), -2f, 1.5f}),
new DiagonalMatrix(3, 3, new[] {new Complex32(1f, 2f), 0f, -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; } get { return Complex32.Zero; }
} }
} }
} }

64
src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,10 +30,10 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {
using System;
using System.Collections.Generic;
using LinearAlgebra.Complex32; using LinearAlgebra.Complex32;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
/// <summary> /// <summary>
@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(Complex32[,] data) protected override Matrix CreateMatrix(Complex32[,] data)
{ {
return new SparseMatrix(data); return SparseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "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) }) }, {"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", 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) }) }, {"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", 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) }) }, {"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", 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) }) }, {"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", 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) }) } {"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) foreach (var name in testData.Keys)
{ {
@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void MatrixFrom1DArrayIsCopy() public void MatrixFrom1DArrayIsCopy()
{ {
// Sparse Matrix copies values from Complex32[], but no remember reference. // 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 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 matrix = SparseMatrix.OfColumnMajor(3, 3, data);
matrix[0, 0] = new Complex32(10.0f, 1); matrix[0, 0] = new Complex32(10.0f, 1);
Assert.AreNotEqual(new Complex32(10.0f, 1), data[0]); Assert.AreNotEqual(new Complex32(10.0f, 1), data[0]);
} }
@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new SparseMatrix(TestData2D["Singular3x3"]); var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = new Complex32(10.0f, 1); matrix[0, 0] = new Complex32(10.0f, 1);
Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]);
} }
@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) 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++) 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) if (value != 0)
{ {
nonzero++; nonzero++;
@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanAddSparseMatricesBothWays() public void CanAddSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 sum1 = m1 + m2;
var sum2 = m2 + m1; var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2)); Assert.IsTrue(sum1.Equals(m2));
@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
sparseResult.Add(m2, sparseResult); sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2 * sum1)); Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult); denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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); denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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 sum3 = m1 + m3;
var sum4 = m3 + m1; var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3)); Assert.IsTrue(sum3.Equals(m3));
@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanSubtractSparseMatricesBothWays() public void CanSubtractSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 diff1 = m1 - m2;
var diff2 = m2 - m1; var diff2 = m2 - m1;
Assert.IsTrue(diff1.Equals(m2.Negate())); Assert.IsTrue(diff1.Equals(m2.Negate()));
@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
sparseResult.Subtract(m2, sparseResult); sparseResult.Subtract(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff2)); 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); m1.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(0 * diff1)); Assert.IsTrue(sparseResult.Equals(0*diff1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Subtract(m2, denseResult); denseResult.Subtract(m2, denseResult);
Assert.IsTrue(denseResult.Equals(diff1)); 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); denseResult.Subtract(m1, denseResult);
Assert.IsTrue(denseResult.Equals(diff2)); 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 diff3 = m1 - m3;
var diff4 = m3 - m1; var diff4 = m3 - m1;
Assert.IsTrue(diff3.Equals(m3.Negate())); Assert.IsTrue(diff3.Equals(m3.Negate()));

28
src/UnitTests/LinearAlgebraTests/Double/DenseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(double[,] data) protected override Matrix CreateMatrix(double[,] data)
{ {
return new DenseMatrix(data); return DenseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new DenseMatrix(3, 3, new[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 }) }, {"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 }) }, {"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 }) }, {"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 }) }, {"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 }) } {"Wide2x3", new DenseMatrix(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})}
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -105,7 +109,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DenseMatrix(3, 3, data);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreEqual(10.0, data[0]); Assert.AreEqual(10.0, data[0]);
@ -117,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new DenseMatrix(TestData2D["Singular3x3"]); var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]);
} }
@ -134,7 +138,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) for (var j = 0; j < TestData2D[name].GetLength(1); j++)

94
src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // 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 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
using System;
using System.Collections.Generic;
using System.Linq;
using LinearAlgebra.Double; using LinearAlgebra.Double;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using NUnit.Framework; using NUnit.Framework;
using System;
#if PORTABLE using System.Collections.Generic;
using Threading;
#endif
/// <summary> /// <summary>
/// Diagonal matrix tests. /// Diagonal matrix tests.
@ -49,14 +47,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public override void SetupMatrices() public override void SetupMatrices()
{ {
TestData2D = new Dictionary<string, double[,]> TestData2D = new Dictionary<string, double[,]>
{ {
{ "Singular3x3", new[,] { { 1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 3.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 } } }, {"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 } } }, {"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 } } }, {"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 } } }, {"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 } } } {"Wide2x3", new[,] {{-1.1, 0.0, 0.0}, {0.0, 1.1, 0.0}}}
}; };
TestMatrices = new Dictionary<string, Matrix>(); TestMatrices = new Dictionary<string, Matrix>();
foreach (var name in TestData2D.Keys) foreach (var name in TestData2D.Keys)
@ -114,13 +112,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanCreateMatrixFromDiagonalArray() public void CanCreateMatrixFromDiagonalArray()
{ {
var testData = new Dictionary<string, Matrix<double>> var testData = new Dictionary<string, Matrix<double>>
{ {
{ "Singular3x3", new DiagonalMatrix(3, 3, new[] { 1.0, 0.0, 3.0 }) }, {"Singular3x3", new DiagonalMatrix(3, 3, new[] {1.0, 0.0, 3.0})},
{ "Square3x3", new DiagonalMatrix(3, 3, new[] { -1.1, 1.1, 6.6 }) }, {"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 }) }, {"Square4x4", new DiagonalMatrix(4, 4, new[] {-1.1, 1.1, 6.2, -7.7})},
{ "Tall3x2", new DiagonalMatrix(3, 2, new[] { -1.1, 1.1 }) }, {"Tall3x2", new DiagonalMatrix(3, 2, new[] {-1.1, 1.1})},
{ "Wide2x3", new DiagonalMatrix(2, 3, new[] { -1.1, 1.1 }) }, {"Wide2x3", new DiagonalMatrix(2, 3, new[] {-1.1, 1.1})},
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -134,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DiagonalMatrix(5, 5, data);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreEqual(10.0, data[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++) 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() public void PermuteMatrixRowsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation));
} }
@ -244,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void PermuteMatrixColumnsThrowsInvalidOperationException() public void PermuteMatrixColumnsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation));
} }
@ -261,13 +259,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
var min = Math.Min(data.RowCount, data.ColumnCount); var min = Math.Min(data.RowCount, data.ColumnCount);
for (var i = 0; i < min; i++) 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); result = data.PointwiseDivide(other);
for (var i = 0; i < min; i++) 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() public override void CanComputeFrobeniusNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14);
} }
@ -296,15 +294,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public override void CanComputeInfinityNorm() public override void CanComputeInfinityNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14);
} }
@ -314,15 +312,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public override void CanComputeL1Norm() public override void CanComputeL1Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14);
} }
@ -332,15 +330,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public override void CanComputeL2Norm() public override void CanComputeL2Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14);
} }
@ -351,11 +349,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanComputeDeterminant() public void CanComputeDeterminant()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
matrix = TestMatrices["Square4x4"]; matrix = TestMatrices["Square4x4"];
denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 14);
} }
@ -400,16 +398,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
// [ 2 0 ] // [ 2 0 ]
// [ 0 3 ] // [ 0 3 ]
var subM3 = diagMatrix.SubMatrix(0, 3, 1, 2); 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] [Test]
public void DiagonalDenseMatrixMultiplication_IssueCP5706() public void DiagonalDenseMatrixMultiplication_IssueCP5706()
{ {
Matrix<double> diagonal = DiagonalMatrix.Identity(3); Matrix<double> diagonal = DiagonalMatrix.Identity(3);
Matrix<double> dense = new DenseMatrix(new double[,] { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } }); Matrix<double> dense = DenseMatrix.OfArray(new double[,] {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}});
var test = diagonal * dense; var test = diagonal*dense;
var test2 = dense * diagonal; var test2 = dense*diagonal;
} }
} }
} }

66
src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,12 +30,12 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
{ {
using System;
using System.Globalization;
using System.IO;
using LinearAlgebra.Double; using LinearAlgebra.Double;
using LinearAlgebra.IO; using LinearAlgebra.IO;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Globalization;
using System.IO;
/// <summary> /// <summary>
/// Delimited writer tests. /// Delimited writer tests.
@ -45,19 +49,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
[Test] [Test]
public void CanWriteCommaDelimitedData() 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(',') var writer = new DelimitedWriter(',')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1.1,2.2,3.3" + Environment.NewLine var expected = @"1.1,2.2,3.3" + Environment.NewLine
+ "4.4,5.5,6.6" + Environment.NewLine + "4.4,5.5,6.6" + Environment.NewLine
+ "7.7,8.8,9.9"; + "7.7,8.8,9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -67,20 +71,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
[Test] [Test]
public void CanWritePeriodDelimitedData() 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 culture = new CultureInfo("tr-TR");
var writer = new DelimitedWriter('.') var writer = new DelimitedWriter('.')
{ {
CultureInfo = culture CultureInfo = culture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1,1.2,2.3,3" + Environment.NewLine var expected = @"1,1.2,2.3,3" + Environment.NewLine
+ "4,4.5,5.6,6" + Environment.NewLine + "4,4.5,5.6,6" + Environment.NewLine
+ "7,7.8,8.9,9"; + "7,7.8,8.9,9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -90,19 +94,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
[Test] [Test]
public void CanWriteSpaceDelimitedData() 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(' ') var writer = new DelimitedWriter(' ')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1.1 0 0" + Environment.NewLine var expected = @"1.1 0 0" + Environment.NewLine
+ "0 5.5 0" + Environment.NewLine + "0 5.5 0" + Environment.NewLine
+ "0 0 9.9"; + "0 0 9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -112,22 +116,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO
[Test] [Test]
public void CanWriteTabDelimitedData() 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 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 headers = new[] {"a", "b", "c"};
var writer = new DelimitedWriter('\t') var writer = new DelimitedWriter('\t')
{ {
ColumnHeaders = headers, ColumnHeaders = headers,
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "a\tb\tc" + Environment.NewLine var expected = "a\tb\tc" + Environment.NewLine
+ "1.1\t2.2\t3.3" + Environment.NewLine + "1.1\t2.2\t3.3" + Environment.NewLine
+ "4.4\t5.5\t6.6" + Environment.NewLine + "4.4\t5.5\t6.6" + Environment.NewLine
+ "7.7\t8.8\t9.9"; + "7.7\t8.8\t9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
} }

57
src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs

@ -1,12 +1,41 @@
using System.Linq; // <copyright file="MatrixStructureTheory.cs" company="Math.NET">
using MathNet.Numerics.Distributions; // Math.NET Numerics, part of the Math.NET Project
using MathNet.Numerics.Random; // 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.
// </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
using Distributions;
using LinearAlgebra.Double; using LinearAlgebra.Double;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
[TestFixture] [TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<double> public class MatrixStructureTheory : MatrixStructureTheory<double>
@ -14,17 +43,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Datapoints] [Datapoints]
Matrix<double>[] _matrices = new Matrix<double>[] Matrix<double>[] _matrices = new Matrix<double>[]
{ {
new DenseMatrix(new[,] {{1d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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}}), 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}}),
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}}), 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}}),
new DenseMatrix(new[,] {{-1.1d, -2.2d}, {0d, 1.1d}, {-4.4d, 5.5d}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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, 2d, 3d}, {2d, 2d, 0d}, {3d, 0d, 3d}}),
new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), SparseMatrix.OfArray(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}),
new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 0d, 0d}, {-2d, 0d, 0d}}), SparseMatrix.OfArray(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[,] {{-1.1d, 0d, 0d}, {0d, 1.1d, 2.2d}}),
new DiagonalMatrix(3, 3, new[] {1d, -2d, 1.5d}), new DiagonalMatrix(3, 3, new[] {1d, -2d, 1.5d}),
new DiagonalMatrix(3, 3, new[] {1d, 0d, -1.5d}), new DiagonalMatrix(3, 3, new[] {1d, 0d, -1.5d}),
@ -67,4 +96,4 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
get { return 0d; } get { return 0d; }
} }
} }
} }

64
src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,11 +30,11 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
using System;
using System.Collections.Generic;
using LinearAlgebra.Double; using LinearAlgebra.Double;
using LinearAlgebra.Double.IO; using LinearAlgebra.Double.IO;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
/// <summary> /// <summary>
/// Sparse matrix tests. /// Sparse matrix tests.
@ -55,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(double[,] data) protected override Matrix CreateMatrix(double[,] data)
{ {
return new SparseMatrix(data); return SparseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -86,13 +90,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new SparseMatrix(3, 3, new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) }, {"Singular3x3", SparseMatrix.OfColumnMajor(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 }) }, {"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", 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 }) }, {"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", new SparseMatrix(3, 2, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5 }) }, {"Tall3x2", SparseMatrix.OfColumnMajor(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 }) } {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})}
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -107,8 +111,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void MatrixFrom1DArrayIsCopy() public void MatrixFrom1DArrayIsCopy()
{ {
// Sparse Matrix copies values from double[], but no remember reference. // Sparse Matrix copies values from double[], but no remember reference.
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 SparseMatrix(3, 3, data); var matrix = SparseMatrix.OfColumnMajor(3, 3, data);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreNotEqual(10.0, data[0]); Assert.AreNotEqual(10.0, data[0]);
} }
@ -119,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new SparseMatrix(TestData2D["Singular3x3"]); var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0; matrix[0, 0] = 10.0;
Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]);
} }
@ -136,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) 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++) 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) if (value != 0)
{ {
nonzero++; nonzero++;
@ -207,7 +211,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanAddSparseMatricesBothWays() public void CanAddSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 sum1 = m1 + m2;
var sum2 = m2 + m1; var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2)); Assert.IsTrue(sum1.Equals(m2));
@ -217,27 +221,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
sparseResult.Add(m2, sparseResult); sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2 * sum1)); Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult); denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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); denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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 sum3 = m1 + m3;
var sum4 = m3 + m1; var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3)); Assert.IsTrue(sum3.Equals(m3));
@ -251,7 +255,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void CanSubtractSparseMatricesBothWays() public void CanSubtractSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 diff1 = m1 - m2;
var diff2 = m2 - m1; var diff2 = m2 - m1;
Assert.IsTrue(diff1.Equals(m2.Negate())); Assert.IsTrue(diff1.Equals(m2.Negate()));
@ -261,27 +265,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
sparseResult.Subtract(m2, sparseResult); sparseResult.Subtract(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff2)); 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); m1.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(0 * diff1)); Assert.IsTrue(sparseResult.Equals(0*diff1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Subtract(m2, denseResult); denseResult.Subtract(m2, denseResult);
Assert.IsTrue(denseResult.Equals(diff1)); 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); denseResult.Subtract(m1, denseResult);
Assert.IsTrue(denseResult.Equals(diff2)); 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 diff3 = m1 - m3;
var diff4 = m3 - m1; var diff4 = m3 - m1;
Assert.IsTrue(diff3.Equals(m3.Negate())); Assert.IsTrue(diff3.Equals(m3.Negate()));

32
src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,10 +30,10 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
using System;
using System.Collections.Generic;
using LinearAlgebra.Single; using LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
/// <summary> /// <summary>
/// Dense matrix tests. /// Dense matrix tests.
@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(float[,] data) protected override Matrix CreateMatrix(float[,] data)
{ {
return new DenseMatrix(data); return DenseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new DenseMatrix(3, 3, new[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f }) }, {"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 }) }, {"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 }) }, {"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 }) }, {"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 }) } {"Wide2x3", new DenseMatrix(2, 3, new[] {-1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f})}
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -105,7 +109,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DenseMatrix(3, 3, data);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreEqual(10.0f, data[0]); Assert.AreEqual(10.0f, data[0]);
@ -117,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new DenseMatrix(TestData2D["Singular3x3"]); var matrix = DenseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]);
} }
@ -134,7 +138,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) for (var j = 0; j < TestData2D[name].GetLength(1); j++)

85
src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,15 +30,10 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
using System;
using System.Collections.Generic;
using System.Linq;
using LinearAlgebra.Single; using LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
using System;
#if PORTABLE using System.Collections.Generic;
using Threading;
#endif
/// <summary> /// <summary>
/// Diagonal matrix tests. /// Diagonal matrix tests.
@ -48,14 +47,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public override void SetupMatrices() public override void SetupMatrices()
{ {
TestData2D = new Dictionary<string, float[,]> TestData2D = new Dictionary<string, float[,]>
{ {
{ "Singular3x3", new[,] { { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 3.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 } } }, {"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 } } }, {"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 } } }, {"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 } } }, {"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 } } } {"Wide2x3", new[,] {{-1.1f, 0.0f, 0.0f}, {0.0f, 1.1f, 0.0f}}}
}; };
TestMatrices = new Dictionary<string, Matrix>(); TestMatrices = new Dictionary<string, Matrix>();
foreach (var name in TestData2D.Keys) foreach (var name in TestData2D.Keys)
@ -113,13 +112,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanCreateMatrixFromDiagonalArray() public void CanCreateMatrixFromDiagonalArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new DiagonalMatrix(3, 3, new[] { 1.0f, 0.0f, 3.0f }) }, {"Singular3x3", new DiagonalMatrix(3, 3, new[] {1.0f, 0.0f, 3.0f})},
{ "Square3x3", new DiagonalMatrix(3, 3, new[] { -1.1f, 1.1f, 6.6f }) }, {"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 }) }, {"Square4x4", new DiagonalMatrix(4, 4, new[] {-1.1f, 1.1f, 6.2f, -7.7f})},
{ "Tall3x2", new DiagonalMatrix(3, 2, new[] { -1.1f, 1.1f }) }, {"Tall3x2", new DiagonalMatrix(3, 2, new[] {-1.1f, 1.1f})},
{ "Wide2x3", new DiagonalMatrix(2, 3, new[] { -1.1f, 1.1f }) }, {"Wide2x3", new DiagonalMatrix(2, 3, new[] {-1.1f, 1.1f})},
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -133,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Test] [Test]
public void MatrixFrom1DArrayIsReference() 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); var matrix = new DiagonalMatrix(5, 5, data);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreEqual(10.0f, data[0]); Assert.AreEqual(10.0f, data[0]);
@ -220,7 +219,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
for (var j = 0; j < matrixC.ColumnCount; j++) 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() public void PermuteMatrixRowsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteRows(permutation));
} }
@ -243,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void PermuteMatrixColumnsThrowsInvalidOperationException() public void PermuteMatrixColumnsThrowsInvalidOperationException()
{ {
var matrixp = CreateMatrix(TestData2D["Singular3x3"]); var matrixp = CreateMatrix(TestData2D["Singular3x3"]);
var permutation = new Permutation(new[] { 2, 0, 1 }); var permutation = new Permutation(new[] {2, 0, 1});
Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation)); Assert.Throws<InvalidOperationException>(() => matrixp.PermuteColumns(permutation));
} }
@ -260,13 +259,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
var min = Math.Min(data.RowCount, data.ColumnCount); var min = Math.Min(data.RowCount, data.ColumnCount);
for (var i = 0; i < min; i++) 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); result = data.PointwiseDivide(other);
for (var i = 0; i < min; i++) 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() public override void CanComputeFrobeniusNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 7);
} }
@ -295,15 +294,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public override void CanComputeInfinityNorm() public override void CanComputeInfinityNorm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7); AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 7);
} }
@ -313,15 +312,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public override void CanComputeL1Norm() public override void CanComputeL1Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 7);
} }
@ -331,15 +330,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public override void CanComputeL2Norm() public override void CanComputeL2Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Wide2x3"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Tall3x2"]);
AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 7);
} }
@ -350,11 +349,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanComputeDeterminant() public void CanComputeDeterminant()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); var denseMatrix = DenseMatrix.OfArray(TestData2D["Square3x3"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7);
matrix = TestMatrices["Square4x4"]; matrix = TestMatrices["Square4x4"];
denseMatrix = new DenseMatrix(TestData2D["Square4x4"]); denseMatrix = DenseMatrix.OfArray(TestData2D["Square4x4"]);
AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7); AssertHelpers.AlmostEqual(denseMatrix.Determinant(), matrix.Determinant(), 7);
} }

67
src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // 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 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
{ {
using System;
using System.Globalization;
using System.IO;
using LinearAlgebra.IO; using LinearAlgebra.IO;
using LinearAlgebra.Single; using LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Globalization;
using System.IO;
/// <summary> /// <summary>
/// Delimited writer tests. /// Delimited writer tests.
@ -44,19 +49,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
[Test] [Test]
public void CanWriteCommaDelimitedData() 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(',') var writer = new DelimitedWriter(',')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1.1,2.2,3.3" + Environment.NewLine var expected = @"1.1,2.2,3.3" + Environment.NewLine
+ "4.4,5.5,6.6" + Environment.NewLine + "4.4,5.5,6.6" + Environment.NewLine
+ "7.7,8.8,9.9"; + "7.7,8.8,9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -66,20 +71,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
[Test] [Test]
public void CanWritePeriodDelimitedData() 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 culture = new CultureInfo("tr-TR");
var writer = new DelimitedWriter('.') var writer = new DelimitedWriter('.')
{ {
CultureInfo = culture CultureInfo = culture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1,1.2,2.3,3" + Environment.NewLine var expected = @"1,1.2,2.3,3" + Environment.NewLine
+ "4,4.5,5.6,6" + Environment.NewLine + "4,4.5,5.6,6" + Environment.NewLine
+ "7,7.8,8.9,9"; + "7,7.8,8.9,9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -89,19 +94,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
[Test] [Test]
public void CanWriteSpaceDelimitedData() 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(' ') var writer = new DelimitedWriter(' ')
{ {
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = @"1.1 0 0" + Environment.NewLine var expected = @"1.1 0 0" + Environment.NewLine
+ "0 5.5 0" + Environment.NewLine + "0 5.5 0" + Environment.NewLine
+ "0 0 9.9"; + "0 0 9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
@ -111,22 +116,22 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO
[Test] [Test]
public void CanWriteTabDelimitedData() 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 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 headers = new[] {"a", "b", "c"};
var writer = new DelimitedWriter('\t') var writer = new DelimitedWriter('\t')
{ {
ColumnHeaders = headers, ColumnHeaders = headers,
CultureInfo = CultureInfo.InvariantCulture CultureInfo = CultureInfo.InvariantCulture
}; };
var stream = new MemoryStream(); var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream); writer.WriteMatrix(matrix, stream);
var data = stream.ToArray(); var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data)); var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd(); var text = reader.ReadToEnd();
var expected = "a\tb\tc" + Environment.NewLine var expected = "a\tb\tc" + Environment.NewLine
+ "1.1\t2.2\t3.3" + Environment.NewLine + "1.1\t2.2\t3.3" + Environment.NewLine
+ "4.4\t5.5\t6.6" + Environment.NewLine + "4.4\t5.5\t6.6" + Environment.NewLine
+ "7.7\t8.8\t9.9"; + "7.7\t8.8\t9.9";
Assert.AreEqual(expected, text); Assert.AreEqual(expected, text);
} }
} }

57
src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs

@ -1,12 +1,41 @@
using System.Linq; // <copyright file="MatrixStructureTheory.cs" company="Math.NET">
using MathNet.Numerics.Distributions; // Math.NET Numerics, part of the Math.NET Project
using MathNet.Numerics.Random; // 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.
// </copyright>
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
using LinearAlgebra.Single; using Distributions;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using LinearAlgebra.Single;
using Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
[TestFixture] [TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<float> public class MatrixStructureTheory : MatrixStructureTheory<float>
@ -14,17 +43,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Datapoints] [Datapoints]
Matrix<float>[] _matrices = new Matrix<float>[] Matrix<float>[] _matrices = new Matrix<float>[]
{ {
new DenseMatrix(new[,] {{1f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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}}), 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}}),
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}}), 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}}),
new DenseMatrix(new[,] {{-1.1f, -2.2f}, {0f, 1.1f}, {-4.4f, 5.5f}}), DenseMatrix.OfArray(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}}), DenseMatrix.OfArray(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, 2f, 3f}, {2f, 2f, 0f}, {3f, 0f, 3f}}),
new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), SparseMatrix.OfArray(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}),
new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 0f, 0f}, {-2f, 0f, 0f}}), SparseMatrix.OfArray(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[,] {{-1.1f, 0f, 0f}, {0f, 1.1f, 2.2f}}),
new DiagonalMatrix(3, 3, new[] {1f, -2f, 1.5f}), new DiagonalMatrix(3, 3, new[] {1f, -2f, 1.5f}),
new DiagonalMatrix(3, 3, new[] {1f, 0f, -1.5f}), new DiagonalMatrix(3, 3, new[] {1f, 0f, -1.5f}),

64
src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // 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 // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,10 +30,10 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
using System;
using System.Collections.Generic;
using LinearAlgebra.Single; using LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
/// <summary> /// <summary>
/// Sparse matrix tests. /// Sparse matrix tests.
@ -54,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
/// <returns>A matrix with the given values.</returns> /// <returns>A matrix with the given values.</returns>
protected override Matrix CreateMatrix(float[,] data) protected override Matrix CreateMatrix(float[,] data)
{ {
return new SparseMatrix(data); return SparseMatrix.OfArray(data);
} }
/// <summary> /// <summary>
@ -85,13 +89,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanCreateMatrixFrom1DArray() public void CanCreateMatrixFrom1DArray()
{ {
var testData = new Dictionary<string, Matrix> var testData = new Dictionary<string, Matrix>
{ {
{ "Singular3x3", new SparseMatrix(3, 3, new float[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) }, {"Singular3x3", SparseMatrix.OfColumnMajor(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 }) }, {"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", 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 }) }, {"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", new SparseMatrix(3, 2, new[] { -1.1f, 0.0f, -4.4f, -2.2f, 1.1f, 5.5f }) }, {"Tall3x2", SparseMatrix.OfColumnMajor(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 }) } {"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {-1.1f, 0.0f, -2.2f, 1.1f, -3.3f, 2.2f})}
}; };
foreach (var name in testData.Keys) foreach (var name in testData.Keys)
{ {
@ -106,8 +110,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void MatrixFrom1DArrayIsCopy() public void MatrixFrom1DArrayIsCopy()
{ {
// Sparse Matrix copies values from float[], but no remember reference. // Sparse Matrix copies values from float[], but no remember reference.
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 SparseMatrix(3, 3, data); var matrix = SparseMatrix.OfColumnMajor(3, 3, data);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreNotEqual(10.0f, data[0]); Assert.AreNotEqual(10.0f, data[0]);
} }
@ -118,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Test] [Test]
public void MatrixFrom2DArrayIsCopy() public void MatrixFrom2DArrayIsCopy()
{ {
var matrix = new SparseMatrix(TestData2D["Singular3x3"]); var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]);
matrix[0, 0] = 10.0f; matrix[0, 0] = 10.0f;
Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]); Assert.AreEqual(1.0f, TestData2D["Singular3x3"][0, 0]);
} }
@ -135,7 +139,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[TestCase("Wide2x3")] [TestCase("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name) 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 i = 0; i < TestData2D[name].GetLength(0); i++)
{ {
for (var j = 0; j < TestData2D[name].GetLength(1); j++) 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++) 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) if (value != 0)
{ {
nonzero++; nonzero++;
@ -206,7 +210,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanAddSparseMatricesBothWays() public void CanAddSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 sum1 = m1 + m2;
var sum2 = m2 + m1; var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2)); Assert.IsTrue(sum1.Equals(m2));
@ -216,27 +220,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
sparseResult.Add(m2, sparseResult); sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1)); 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); sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2 * sum1)); Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult); denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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); denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1)); 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 sum3 = m1 + m3;
var sum4 = m3 + m1; var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3)); Assert.IsTrue(sum3.Equals(m3));
@ -250,7 +254,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public void CanSubtractSparseMatricesBothWays() public void CanSubtractSparseMatricesBothWays()
{ {
var m1 = new SparseMatrix(1, 3); 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 diff1 = m1 - m2;
var diff2 = m2 - m1; var diff2 = m2 - m1;
Assert.IsTrue(diff1.Equals(m2.Negate())); Assert.IsTrue(diff1.Equals(m2.Negate()));
@ -260,27 +264,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
sparseResult.Subtract(m2, sparseResult); sparseResult.Subtract(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff2)); 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); m1.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(diff1)); 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); sparseResult.Subtract(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(0 * diff1)); Assert.IsTrue(sparseResult.Equals(0*diff1));
var denseResult = new DenseMatrix(1, 3); var denseResult = new DenseMatrix(1, 3);
denseResult.Subtract(m2, denseResult); denseResult.Subtract(m2, denseResult);
Assert.IsTrue(denseResult.Equals(diff1)); 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); denseResult.Subtract(m1, denseResult);
Assert.IsTrue(denseResult.Equals(diff2)); 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 diff3 = m1 - m3;
var diff4 = m3 - m1; var diff4 = m3 - m1;
Assert.IsTrue(diff3.Equals(m3.Negate())); Assert.IsTrue(diff3.Equals(m3.Negate()));

Loading…
Cancel
Save