diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index 285c1f39..2d8d565d 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -269,26 +269,26 @@ module DenseMatrix = let inline ofSeq (fss: #seq<#seq>) = let n = Seq.length fss let m = Seq.length (Seq.head fss) - DenseMatrix.OfRows(n, m, fss) + DenseMatrix.OfRowsCovariant(n, m, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. /// If the dimensions are known, consider to use ofRowList instead to avoid multiple enumeration. let inline ofList (fll: float list list) = let n = List.length fll let m = List.length (List.head fll) - DenseMatrix.OfRows(n, m, fll) + DenseMatrix.OfRowsCovariant(n, m, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfRows(rows, cols, fss) + let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. - let inline ofRowList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRows(rows, cols, fll) + let inline ofRowList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfColumns(rows, cols, fss) + let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a column. - let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumns(rows, cols, fll) + let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll) /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (fs: #seq) = DenseMatrix.OfIndexed(rows, cols, fs) @@ -351,16 +351,16 @@ module SparseMatrix = let inline ofList (rows: int) (cols: int) (fl: list) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfRows(rows, cols, fss) + let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. - let inline ofRowList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRows(rows, cols, fll) + let inline ofRowList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfColumns(rows, cols, fss) + let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a column. - let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumns(rows, cols, fll) + let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (fs: #seq) = SparseMatrix.OfIndexed(rows, cols, fs) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index c125dbc1..9addf2aa 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -162,8 +162,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfColumns(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) where TColumn : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); @@ -175,8 +185,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) where TRow : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 6bed52cb..f051f433 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -152,7 +152,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfColumns(int rows, int columns, IEnumerable data) + public static SparseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TColumn : IEnumerable { @@ -165,7 +176,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable data) + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TRow : IEnumerable { diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 186230f0..414d8dc7 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -162,8 +162,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfColumns(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) where TColumn : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); @@ -175,8 +185,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) where TRow : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 503fcb24..f6c63a3f 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -152,7 +152,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfColumns(int rows, int columns, IEnumerable data) + public static SparseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TColumn : IEnumerable { @@ -165,7 +176,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable data) + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TRow : IEnumerable { diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 3057ffbf..8b1b9edf 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -164,8 +164,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfColumns(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) where TColumn : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); @@ -177,8 +187,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) where TRow : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 1985391c..b0165585 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -151,7 +151,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfColumns(int rows, int columns, IEnumerable data) + public static SparseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TColumn : IEnumerable { @@ -164,7 +175,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable data) + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TRow : IEnumerable { diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index f55e73c7..f91221b0 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -84,6 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The vectors to construct the matrix from. /// The matrix constructed from the list of column vectors. /// Creates a matrix of size Max([i].Count) x .Count + [Obsolete("Use DenseMatrix.OfColumns or SparseMatrix.OfColumns instead. Scheduled for removal in v3.0.")] public static Matrix CreateFromColumns(IList> columnVectors) { if (columnVectors == null) @@ -122,6 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The vectors to construct the matrix from. /// The matrix constructed from the list of row vectors. /// Creates a matrix of size Max(.Count) x [i].Count + [Obsolete("Use DenseMatrix.OfRows or SparseMatrix.OfRows instead. Scheduled for removal in v3.0.")] public static Matrix CreateFromRows(IList> rowVectors) { if (rowVectors == null) diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 27653973..5365e415 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -162,8 +162,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfColumns(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) where TColumn : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); @@ -175,8 +185,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) where TRow : IEnumerable { return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 411c261b..28449551 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -144,6 +144,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single { return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumns(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + } /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. @@ -151,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfColumns(int rows, int columns, IEnumerable data) + public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TColumn : IEnumerable { @@ -164,7 +175,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable data) + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TRow : IEnumerable { diff --git a/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs index ba29bf58..c50719e3 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs @@ -41,6 +41,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory { + public MatrixStructureTheory() + : base(Complex.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector)) + { + } + [Datapoints] Matrix[] _matrices = new Matrix[] { @@ -91,10 +96,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex var dist = new Normal {RandomSource = new MersenneTwister(seed)}; return new DenseVector(Enumerable.Range(0, size).Select(k => new Complex(dist.Sample(), dist.Sample())).ToArray()); } - - protected override Complex Zero - { - get { return Complex.Zero; } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs index cd192c74..7ad99f8a 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs @@ -41,6 +41,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory { + public MatrixStructureTheory() + : base(Complex32.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector)) + { + } + [Datapoints] Matrix[] _matrices = new Matrix[] { @@ -91,10 +96,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 var dist = new Normal {RandomSource = new MersenneTwister(seed)}; return new DenseVector(Enumerable.Range(0, size).Select(k => new Complex32((float) dist.Sample(), (float) dist.Sample())).ToArray()); } - - protected override Complex32 Zero - { - get { return Complex32.Zero; } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs index 9baf01be..77ea7da0 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs @@ -40,6 +40,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory { + public MatrixStructureTheory() + : base(0d, typeof (DenseMatrix), typeof (SparseMatrix), typeof (DiagonalMatrix), typeof (DenseVector), typeof (SparseVector)) + { + } + [Datapoints] Matrix[] _matrices = new Matrix[] { @@ -90,10 +95,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double var dist = new Normal {RandomSource = new MersenneTwister(seed)}; return new DenseVector(dist.Samples().Take(size).ToArray()); } - - protected override double Zero - { - get { return 0d; } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs index 7264e1e4..d027f335 100644 --- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs +++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs @@ -6,7 +6,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests { partial class MatrixStructureTheory { - [Theory, Timeout(500)] + [Theory] public void CanGetFieldsByIndex(Matrix matrix) { Assert.That(() => matrix[0, 0], Throws.Nothing); @@ -18,7 +18,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix[0, matrix.ColumnCount], Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetRow(Matrix matrix) { // First Row @@ -42,7 +42,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Row(matrix.RowCount), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetRowIntoResult(Matrix matrix) { var row = CreateVectorZero(matrix.ColumnCount); @@ -58,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Row(matrix.RowCount, row), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetRowWithRange(Matrix matrix) { // First Row, Columns 0..1 @@ -93,7 +93,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Row(0, 0, matrix.ColumnCount + 1), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetRowWithRangeIntoResult(Matrix matrix) { var row = CreateVectorZero(matrix.ColumnCount - 1); @@ -110,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Row(0, 0, matrix.ColumnCount, row), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetColumn(Matrix matrix) { // First Column @@ -134,7 +134,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Column(matrix.ColumnCount), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetColumnIntoResult(Matrix matrix) { var col = CreateVectorZero(matrix.RowCount); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Column(matrix.ColumnCount, col), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetColumnWithRange(Matrix matrix) { // First Column, Rows 0..1 @@ -185,7 +185,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Column(0, 0, matrix.RowCount + 1), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanGetColumnWithRangeIntoResult(Matrix matrix) { var col = CreateVectorZero(matrix.RowCount - 1); @@ -202,7 +202,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.Column(0, 0, matrix.RowCount, col), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanSetRow(Matrix matrix) { // First Row @@ -237,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetRow(0, CreateVectorZero(matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanSetRowArray(Matrix matrix) { // First Row @@ -270,7 +270,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetRow(0, new T[matrix.ColumnCount + 1]), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanSetColumn(Matrix matrix) { // First Column @@ -305,7 +305,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetColumn(0, CreateVectorZero(matrix.RowCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanSetColumnArray(Matrix matrix) { // First Column @@ -338,7 +338,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetColumn(0, new T[matrix.RowCount + 1]), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetUpperTriangle(Matrix matrix) { var upper = matrix.UpperTriangle(); @@ -351,7 +351,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanGetUpperTriangleIntoResult(Matrix matrix) { var dense = CreateDenseZero(matrix.RowCount, matrix.ColumnCount); @@ -379,7 +379,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.UpperTriangle(CreateDenseZero(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetLowerTriangle(Matrix matrix) { var upper = matrix.LowerTriangle(); @@ -392,7 +392,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanGetLowerTriangleIntoResult(Matrix matrix) { var dense = CreateDenseZero(matrix.RowCount, matrix.ColumnCount); @@ -420,7 +420,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.LowerTriangle(CreateDenseZero(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetStrictlyUpperTriangle(Matrix matrix) { var upper = matrix.StrictlyUpperTriangle(); @@ -433,7 +433,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanGetStrictlyUpperTriangleIntoResult(Matrix matrix) { var dense = CreateDenseZero(matrix.RowCount, matrix.ColumnCount); @@ -461,7 +461,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.StrictlyUpperTriangle(CreateDenseZero(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetStrictlyLowerTriangle(Matrix matrix) { var upper = matrix.StrictlyLowerTriangle(); @@ -474,7 +474,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanGetStrictlyLowerTriangleIntoResult(Matrix matrix) { var dense = CreateDenseZero(matrix.RowCount, matrix.ColumnCount); @@ -502,7 +502,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.StrictlyLowerTriangle(CreateDenseZero(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetDiagonal(Matrix matrix) { var diag = matrix.Diagonal(); @@ -513,7 +513,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanSetDiagonal(Matrix matrix) { var m = matrix.Clone(); @@ -533,7 +533,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetDiagonal(CreateVectorZero(Math.Min(matrix.RowCount, matrix.ColumnCount) + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanSetDiagonalArray(Matrix matrix) { var m = matrix.Clone(); @@ -552,7 +552,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SetDiagonal(new T[Math.Min(matrix.RowCount, matrix.ColumnCount) + 1]), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanGetSubmatrix(Matrix matrix) { // Top Left Corner 2x2 @@ -594,7 +594,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.SubMatrix(0, 1, 0, 0), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanSetSubmatrix(Matrix matrix) { // Top Left Corner 2x2 diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs index 3c6b7039..c33765e4 100644 --- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs +++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs @@ -7,7 +7,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests { partial class MatrixStructureTheory { - [Theory, Timeout(500)] + [Theory] public void CanPermuteRows(Matrix matrix) { var m = matrix.Clone(); @@ -36,7 +36,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanPermuteColumns(Matrix matrix) { var m = matrix.Clone(); @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(500)] + [Theory] public void CanInsertRow(Matrix matrix) { var row = CreateVectorRandom(matrix.ColumnCount, 0); @@ -101,7 +101,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.InsertRow(0, CreateVectorZero(matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanInsertColumn(Matrix matrix) { var column = CreateVectorRandom(matrix.RowCount, 0); @@ -137,7 +137,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.InsertColumn(0, CreateVectorZero(matrix.RowCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanAppend(Matrix left, Matrix right) { // IF @@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => left.Append(default(Matrix)), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanAppendIntoResult(Matrix left, Matrix right) { // IF @@ -186,7 +186,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => left.Append(right, CreateDenseZero(left.RowCount, left.ColumnCount + right.ColumnCount - 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanStack(Matrix top, Matrix bottom) { // IF @@ -208,7 +208,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => top.Stack(default(Matrix)), Throws.InstanceOf()); } - [Theory, Timeout(500)] + [Theory] public void CanStackIntoResult(Matrix top, Matrix bottom) { // IF @@ -235,7 +235,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => top.Stack(bottom, CreateDenseZero(top.RowCount + bottom.RowCount, top.ColumnCount - 1)), Throws.ArgumentException); } - [Theory, Timeout(500)] + [Theory] public void CanDiagonalStack(Matrix left, Matrix right) { var result = left.DiagonalStack(right); @@ -261,7 +261,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => left.DiagonalStack(default(Matrix)), Throws.InstanceOf(), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType()); } - [Theory, Timeout(500)] + [Theory] public void CanDiagonalStackIntoResult(Matrix left, Matrix right) { var result = CreateDenseZero(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount); diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs index 29bbf4aa..3454d9bb 100644 --- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs @@ -1,4 +1,34 @@ -using System.Collections.Generic; +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2013 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Collections.Generic; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests { @@ -15,7 +45,23 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests protected abstract Matrix CreateSparseZero(int rows, int columns); protected abstract Vector CreateVectorZero(int size); protected abstract Vector CreateVectorRandom(int size, int seed); - protected abstract T Zero { get; } + + protected readonly T Zero; + protected readonly dynamic Dense; + protected readonly dynamic Sparse; + protected readonly dynamic Diagonal; + protected readonly dynamic DenseVector; + protected readonly dynamic SparseVector; + + protected MatrixStructureTheory(T zero, Type dense, Type sparse, Type diagonal, Type denseVector, Type sparseVector) + { + Zero = zero; + Dense = new StaticDynamicWrapper(dense); + Sparse = new StaticDynamicWrapper(sparse); + Diagonal = new StaticDynamicWrapper(diagonal); + DenseVector = new StaticDynamicWrapper(denseVector); + SparseVector = new StaticDynamicWrapper(sparseVector); + } protected Matrix CreateDenseFor(Matrix m, int rows = -1, int columns = -1, int seed = 1) { @@ -31,7 +77,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests : CreateVectorZero(size); } - [Theory, Timeout(200)] + [Theory] public void IsEqualToItself(Matrix matrix) { Assert.That(matrix, Is.EqualTo(matrix)); @@ -42,7 +88,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.IsTrue((object) matrix == matrix); } - [Theory, Timeout(200)] + [Theory] public void IsNotEqualToOthers(Matrix left, Matrix right) { // IF (assuming we don't have duplicate data points) @@ -57,7 +103,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.IsFalse((object) left == right); } - [Theory, Timeout(200)] + [Theory] public void IsNotEqualToNonMatrixType(Matrix matrix) { Assert.That(matrix, Is.Not.EqualTo(2)); @@ -67,7 +113,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.IsFalse(matrix == (object) 2); } - [Theory, Timeout(200)] + [Theory] public void CanClone(Matrix matrix) { var clone = matrix.Clone(); @@ -77,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(clone.ColumnCount, Is.EqualTo(matrix.ColumnCount)); } - [Theory, Timeout(200)] + [Theory] public void CanCloneUsingICloneable(Matrix matrix) { var clone = (Matrix) ((ICloneable) matrix).Clone(); @@ -87,7 +133,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(clone.ColumnCount, Is.EqualTo(matrix.ColumnCount)); } - [Theory, Timeout(200)] + [Theory] public void CanCopyTo(Matrix matrix) { var dense = CreateDenseZero(matrix.RowCount, matrix.ColumnCount); @@ -106,13 +152,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.CopyTo(CreateDenseZero(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); } - [Theory, Timeout(200)] + [Theory] public void CanGetHashCode(Matrix matrix) { Assert.That(matrix.GetHashCode(), Is.Not.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount).GetHashCode())); } - [Theory, Timeout(200)] + [Theory] public void CanClear(Matrix matrix) { var cleared = matrix.Clone(); @@ -120,21 +166,21 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(cleared, Is.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount))); } - [Theory, Timeout(200)] + [Theory] public void CanClearSubMatrix(Matrix matrix) { var cleared = matrix.Clone(); Assume.That(cleared.RowCount, Is.GreaterThanOrEqualTo(2)); Assume.That(cleared.ColumnCount, Is.GreaterThanOrEqualTo(2)); - cleared.Storage.Clear(0,2,1,1); + cleared.Storage.Clear(0, 2, 1, 1); Assert.That(cleared.At(0, 0), Is.EqualTo(matrix.At(0, 0))); Assert.That(cleared.At(1, 0), Is.EqualTo(matrix.At(1, 0))); Assert.That(cleared.At(0, 1), Is.EqualTo(Zero)); Assert.That(cleared.At(1, 1), Is.EqualTo(Zero)); } - [Theory, Timeout(200)] + [Theory] public void CanToArray(Matrix matrix) { var array = matrix.ToArray(); @@ -149,29 +195,29 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests } } - [Theory, Timeout(200)] + [Theory] public void CanToColumnWiseArray(Matrix matrix) { var array = matrix.ToColumnWiseArray(); - Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount)); + Assert.That(array.Length, Is.EqualTo(matrix.RowCount*matrix.ColumnCount)); for (int i = 0; i < array.Length; i++) { - Assert.That(array[i], Is.EqualTo(matrix[i % matrix.RowCount, i / matrix.RowCount])); + Assert.That(array[i], Is.EqualTo(matrix[i%matrix.RowCount, i/matrix.RowCount])); } } - [Theory, Timeout(200)] + [Theory] public void CanToRowWiseArray(Matrix matrix) { var array = matrix.ToRowWiseArray(); - Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount)); + Assert.That(array.Length, Is.EqualTo(matrix.RowCount*matrix.ColumnCount)); for (int i = 0; i < array.Length; i++) { - Assert.That(array[i], Is.EqualTo(matrix[i / matrix.ColumnCount, i % matrix.ColumnCount])); + Assert.That(array[i], Is.EqualTo(matrix[i/matrix.ColumnCount, i%matrix.ColumnCount])); } } - [Theory, Timeout(200)] + [Theory] public void CanCreateSameType(Matrix matrix) { var empty = matrix.CreateMatrix(5, 6); @@ -183,85 +229,155 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.CreateMatrix(-1, -1), Throws.InstanceOf()); } - [Test, Timeout(200)] - public void CanCreateFromColumns() + [Test] + public void CanCreateDenseFromMultiDimArray() { - var column1 = CreateVectorRandom(1, 0); - var column2 = CreateVectorRandom(4, 1); - var column3 = CreateVectorRandom(2, 3); - - var matrix = Matrix.CreateFromColumns(new List> - { - column1, - column2, - column3 - }); + T[,] array = CreateDenseRandom(4, 3, 0).ToArray(); + var matrix = Dense.OfArray(array); + Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(4)); + Assert.That(matrix.ColumnCount, Is.EqualTo(3)); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 3; j++) + Assert.That(matrix[i, j], Is.EqualTo(array[i, j])); + } + [Test] + public void CanCreateSparseFromMultiDimArray() + { + T[,] array = CreateDenseRandom(4, 3, 0).ToArray(); + var matrix = Sparse.OfArray(array); + Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix")); Assert.That(matrix.RowCount, Is.EqualTo(4)); Assert.That(matrix.ColumnCount, Is.EqualTo(3)); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 3; j++) + Assert.That(matrix[i, j], Is.EqualTo(array[i, j])); + } - Assert.That(matrix[0, 0], Is.EqualTo(column1[0])); - Assert.That(matrix[0, 1], Is.EqualTo(column2[0])); - Assert.That(matrix[1, 1], Is.EqualTo(column2[1])); - Assert.That(matrix[2, 1], Is.EqualTo(column2[2])); - Assert.That(matrix[3, 1], Is.EqualTo(column2[3])); - Assert.That(matrix[0, 2], Is.EqualTo(column3[0])); - Assert.That(matrix[1, 2], Is.EqualTo(column3[1])); - - Assert.That(matrix[1, 0], Is.EqualTo(Zero)); - Assert.That(matrix[2, 0], Is.EqualTo(Zero)); - Assert.That(matrix[3, 0], Is.EqualTo(Zero)); - Assert.That(matrix[2, 2], Is.EqualTo(Zero)); - Assert.That(matrix[3, 2], Is.EqualTo(Zero)); + [Test] + public void CanCreateDenseFromJaggedArray() + { + T[][] array = new[] + { + CreateVectorRandom(4, 0).ToArray(), + CreateVectorRandom(4, 1).ToArray(), + CreateVectorRandom(4, 3).ToArray() + }; + var matrix = Dense.OfRows(3, 4, array); + Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(3)); + Assert.That(matrix.ColumnCount, Is.EqualTo(4)); + for (int i = 0; i < 3; i++) + for (int j = 0; j < 4; j++) + Assert.That(matrix[i, j], Is.EqualTo(array[i][j])); } - [Test, Timeout(200)] - public void CanCreateFromRows() + [Test] + public void CanCreateSparseFromJaggedArray() { - var row1 = CreateVectorRandom(1, 0); - var row2 = CreateVectorRandom(4, 1); - var row3 = CreateVectorRandom(2, 3); + T[][] array = new[] + { + CreateVectorRandom(4, 0).ToArray(), + CreateVectorRandom(4, 1).ToArray(), + CreateVectorRandom(4, 3).ToArray() + }; + var matrix = Sparse.OfRows(3, 4, array); + Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(3)); + Assert.That(matrix.ColumnCount, Is.EqualTo(4)); + for (int i = 0; i < 3; i++) + for (int j = 0; j < 4; j++) + Assert.That(matrix[i, j], Is.EqualTo(array[i][j])); + } - var matrix = Matrix.CreateFromRows(new List> + [Test] + public void CanCreateDenseFromColumnVectors() + { + var columns = new[] { - row1, - row2, - row3 - }); + CreateVectorRandom(4, 0), + CreateVectorRandom(4, 1), + CreateVectorRandom(4, 3) + }; + var matrix = Dense.OfColumns(4, 3, columns); + Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(4)); + Assert.That(matrix.ColumnCount, Is.EqualTo(3)); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 3; j++) + Assert.That(matrix[i, j], Is.EqualTo(columns[j][i])); + } + + [Test] + public void CanCreateSparseFromColumnVectors() + { + var columns = new[] + { + CreateVectorRandom(4, 0), + CreateVectorRandom(4, 1), + CreateVectorRandom(4, 3) + }; + var matrix = Sparse.OfColumns(4, 3, columns); + Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(4)); + Assert.That(matrix.ColumnCount, Is.EqualTo(3)); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 3; j++) + Assert.That(matrix[i, j], Is.EqualTo(columns[j][i])); + } + [Test] + public void CanCreateDenseFromRowVectors() + { + var rows = new[] + { + CreateVectorRandom(4, 0), + CreateVectorRandom(4, 1), + CreateVectorRandom(4, 3) + }; + var matrix = Dense.OfRows(3, 4, rows); + Assert.That(matrix.GetType().Name, Is.EqualTo("DenseMatrix")); Assert.That(matrix.RowCount, Is.EqualTo(3)); Assert.That(matrix.ColumnCount, Is.EqualTo(4)); + for (int j = 0; j < 4; j++) + for (int i = 0; i < 3; i++) + Assert.That(matrix[i, j], Is.EqualTo(rows[i][j])); + } - Assert.That(matrix[0, 0], Is.EqualTo(row1[0])); - Assert.That(matrix[1, 0], Is.EqualTo(row2[0])); - Assert.That(matrix[1, 1], Is.EqualTo(row2[1])); - Assert.That(matrix[1, 2], Is.EqualTo(row2[2])); - Assert.That(matrix[1, 3], Is.EqualTo(row2[3])); - Assert.That(matrix[2, 0], Is.EqualTo(row3[0])); - Assert.That(matrix[2, 1], Is.EqualTo(row3[1])); - - Assert.That(matrix[0, 1], Is.EqualTo(Zero)); - Assert.That(matrix[0, 2], Is.EqualTo(Zero)); - Assert.That(matrix[0, 3], Is.EqualTo(Zero)); - Assert.That(matrix[2, 2], Is.EqualTo(Zero)); - Assert.That(matrix[2, 3], Is.EqualTo(Zero)); + [Test] + public void CanCreateSparseFromRowVectors() + { + var rows = new[] + { + CreateVectorRandom(4, 0), + CreateVectorRandom(4, 1), + CreateVectorRandom(4, 3) + }; + var matrix = Sparse.OfRows(3, 4, rows); + Assert.That(matrix.GetType().Name, Is.EqualTo("SparseMatrix")); + Assert.That(matrix.RowCount, Is.EqualTo(3)); + Assert.That(matrix.ColumnCount, Is.EqualTo(4)); + for (int j = 0; j < 4; j++) + for (int i = 0; i < 3; i++) + Assert.That(matrix[i, j], Is.EqualTo(rows[i][j])); } - [Test, Timeout(200)] + [Test] public void CanEnumerateWithIndex() { var dense = CreateDenseRandom(2, 3, 0); - using(var enumerator = dense.IndexedEnumerator().GetEnumerator()) - for (int i = 0; i < 2; i++) - { - for (int j = 0; j < 3; j++) + using (var enumerator = dense.IndexedEnumerator().GetEnumerator()) + for (int i = 0; i < 2; i++) { - enumerator.MoveNext(); - Assert.AreEqual(i, enumerator.Current.Item1); - Assert.AreEqual(j, enumerator.Current.Item2); - Assert.AreEqual(dense[i, j], enumerator.Current.Item3); + for (int j = 0; j < 3; j++) + { + enumerator.MoveNext(); + Assert.AreEqual(i, enumerator.Current.Item1); + Assert.AreEqual(j, enumerator.Current.Item2); + Assert.AreEqual(dense[i, j], enumerator.Current.Item3); + } } - } } } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs index b7594b17..952aa1f8 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs @@ -40,6 +40,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [TestFixture] public class MatrixStructureTheory : MatrixStructureTheory { + public MatrixStructureTheory() + : base(0f, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector)) + { + } + [Datapoints] Matrix[] _matrices = new Matrix[] { @@ -90,10 +95,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single var dist = new Normal {RandomSource = new MersenneTwister(seed)}; return new DenseVector(dist.Samples().Select(d => (float) d).Take(size).ToArray()); } - - protected override float Zero - { - get { return 0f; } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs b/src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs new file mode 100644 index 00000000..1d83f942 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs @@ -0,0 +1,78 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2013 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Dynamic; +using System.Reflection; + +namespace MathNet.Numerics.UnitTests.LinearAlgebraTests +{ + /// + /// Helper to allow calling static members of dynamic types. + /// Usefull to test static methods of e.g. all dense matrix types. + /// + public class StaticDynamicWrapper : DynamicObject + { + readonly Type _type; + + public StaticDynamicWrapper(Type type) + { + _type = type; + } + + // Handle static properties + public override bool TryGetMember(GetMemberBinder binder, out object result) + { + PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); + if (prop == null) + { + result = null; + return false; + } + + result = prop.GetValue(null, null); + return true; + } + + // Handle static methods + public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) + { + MethodInfo method = _type.GetMethod(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); + if (method == null) + { + result = null; + return false; + } + + result = method.Invoke(null, args); + return true; + } + } +} \ No newline at end of file diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 487e3c6f..1307db01 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -745,6 +745,7 @@ Code +