Browse Source

LA: matrix OfColumns/OfRows more usable, basic tests

v2
Christoph Ruegg 14 years ago
parent
commit
5f5d29cb6a
  1. 20
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  2. 28
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  3. 26
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 28
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  5. 26
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  6. 28
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  7. 26
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  8. 2
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  9. 28
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  10. 26
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  11. 10
      src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs
  12. 10
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs
  13. 10
      src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs
  14. 52
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs
  15. 20
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs
  16. 272
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs
  17. 10
      src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs
  18. 78
      src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs
  19. 1
      src/UnitTests/UnitTests.csproj

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

@ -269,26 +269,26 @@ module DenseMatrix =
let inline ofSeq (fss: #seq<#seq<float>>) =
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<float>>) = DenseMatrix.OfRows(rows, cols, fss)
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<float>>) = DenseMatrix.OfColumns(rows, cols, fss)
let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, fs)
@ -351,16 +351,16 @@ module SparseMatrix =
let inline ofList (rows: int) (cols: int) (fl: list<int * int * float>) = 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<float>>) = SparseMatrix.OfRows(rows, cols, fss)
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<float>>) = SparseMatrix.OfColumns(rows, cols, fss)
let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, fs)

28
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.
/// </summary>
public static DenseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
where TColumn : IEnumerable<Complex>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.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.
/// </summary>
public static DenseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<Complex>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));

26
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.
/// </summary>
public static SparseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
public static SparseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<Complex>
{
@ -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.
/// </summary>
public static SparseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<Complex>
{

28
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.
/// </summary>
public static DenseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
where TColumn : IEnumerable<Complex32>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.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.
/// </summary>
public static DenseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<Complex32>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));

26
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.
/// </summary>
public static SparseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
public static SparseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<Complex32>
{
@ -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.
/// </summary>
public static SparseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<Complex32>
{

28
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.
/// </summary>
public static DenseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<double>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
where TColumn : IEnumerable<double>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.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.
/// </summary>
public static DenseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<double>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data));

26
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.
/// </summary>
public static SparseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
public static SparseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<double>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<double>
{
@ -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.
/// </summary>
public static SparseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<double>
{

2
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -84,6 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="columnVectors">The vectors to construct the matrix from.</param>
/// <returns>The matrix constructed from the list of column vectors.</returns>
/// <remarks>Creates a matrix of size Max(<paramref name="columnVectors"/>[i].Count) x <paramref name="columnVectors"/>.Count</remarks>
[Obsolete("Use DenseMatrix.OfColumns or SparseMatrix.OfColumns instead. Scheduled for removal in v3.0.")]
public static Matrix<T> CreateFromColumns(IList<Vector<T>> columnVectors)
{
if (columnVectors == null)
@ -122,6 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="rowVectors">The vectors to construct the matrix from.</param>
/// <returns>The matrix constructed from the list of row vectors.</returns>
/// <remarks>Creates a matrix of size Max(<paramref name="rowVectors"/>.Count) x <paramref name="rowVectors"/>[i].Count</remarks>
[Obsolete("Use DenseMatrix.OfRows or SparseMatrix.OfRows instead. Scheduled for removal in v3.0.")]
public static Matrix<T> CreateFromRows(IList<Vector<T>> rowVectors)
{
if (rowVectors == null)

28
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.
/// </summary>
public static DenseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<float>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
where TColumn : IEnumerable<float>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.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.
/// </summary>
public static DenseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<float>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data));

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

@ -144,6 +144,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnMajorList(rows, columns, columnMajor));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfColumns(int rows, int columns, IEnumerable<IEnumerable<float>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfColumns<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<float>
{
@ -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.
/// </summary>
public static SparseMatrix OfRows<TRow>(int rows, int columns, IEnumerable<TRow> data)
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// 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.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<float>
{

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

@ -41,6 +41,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<Complex>
{
public MatrixStructureTheory()
: base(Complex.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<Complex>[] _matrices = new Matrix<Complex>[]
{
@ -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; }
}
}
}

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

@ -41,6 +41,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<Complex32>
{
public MatrixStructureTheory()
: base(Complex32.Zero, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<Complex32>[] _matrices = new Matrix<Complex32>[]
{
@ -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; }
}
}
}

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

@ -40,6 +40,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<double>
{
public MatrixStructureTheory()
: base(0d, typeof (DenseMatrix), typeof (SparseMatrix), typeof (DiagonalMatrix), typeof (DenseVector), typeof (SparseVector))
{
}
[Datapoints]
Matrix<double>[] _matrices = new Matrix<double>[]
{
@ -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; }
}
}
}

52
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Access.cs

@ -6,7 +6,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
partial class MatrixStructureTheory<T>
{
[Theory, Timeout(500)]
[Theory]
public void CanGetFieldsByIndex(Matrix<T> 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<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetRow(Matrix<T> matrix)
{
// First Row
@ -42,7 +42,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.Row(matrix.RowCount), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetRowIntoResult(Matrix<T> matrix)
{
var row = CreateVectorZero(matrix.ColumnCount);
@ -58,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.Row(matrix.RowCount, row), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetRowWithRange(Matrix<T> 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<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetRowWithRangeIntoResult(Matrix<T> 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<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetColumn(Matrix<T> matrix)
{
// First Column
@ -134,7 +134,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.Column(matrix.ColumnCount), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetColumnIntoResult(Matrix<T> matrix)
{
var col = CreateVectorZero(matrix.RowCount);
@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.Column(matrix.ColumnCount, col), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetColumnWithRange(Matrix<T> 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<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanGetColumnWithRangeIntoResult(Matrix<T> 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<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanSetRow(Matrix<T> 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<T> 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<T> 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<T> 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<T> matrix)
{
var upper = matrix.UpperTriangle();
@ -351,7 +351,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanGetUpperTriangleIntoResult(Matrix<T> 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<T> matrix)
{
var upper = matrix.LowerTriangle();
@ -392,7 +392,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanGetLowerTriangleIntoResult(Matrix<T> 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<T> matrix)
{
var upper = matrix.StrictlyUpperTriangle();
@ -433,7 +433,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanGetStrictlyUpperTriangleIntoResult(Matrix<T> 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<T> matrix)
{
var upper = matrix.StrictlyLowerTriangle();
@ -474,7 +474,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanGetStrictlyLowerTriangleIntoResult(Matrix<T> 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<T> matrix)
{
var diag = matrix.Diagonal();
@ -513,7 +513,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanSetDiagonal(Matrix<T> 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<T> 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<T> matrix)
{
// Top Left Corner 2x2
@ -594,7 +594,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.SubMatrix(0, 1, 0, 0), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanSetSubmatrix(Matrix<T> matrix)
{
// Top Left Corner 2x2

20
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs

@ -7,7 +7,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
partial class MatrixStructureTheory<T>
{
[Theory, Timeout(500)]
[Theory]
public void CanPermuteRows(Matrix<T> matrix)
{
var m = matrix.Clone();
@ -36,7 +36,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanPermuteColumns(Matrix<T> matrix)
{
var m = matrix.Clone();
@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(500)]
[Theory]
public void CanInsertRow(Matrix<T> 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<T> 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<T> left, Matrix<T> right)
{
// IF
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => left.Append(default(Matrix<T>)), Throws.InstanceOf<ArgumentNullException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanAppendIntoResult(Matrix<T> left, Matrix<T> 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<T> top, Matrix<T> bottom)
{
// IF
@ -208,7 +208,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => top.Stack(default(Matrix<T>)), Throws.InstanceOf<ArgumentNullException>());
}
[Theory, Timeout(500)]
[Theory]
public void CanStackIntoResult(Matrix<T> top, Matrix<T> 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<T> left, Matrix<T> right)
{
var result = left.DiagonalStack(right);
@ -261,7 +261,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => left.DiagonalStack(default(Matrix<T>)), Throws.InstanceOf<ArgumentNullException>(), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType());
}
[Theory, Timeout(500)]
[Theory]
public void CanDiagonalStackIntoResult(Matrix<T> left, Matrix<T> right)
{
var result = CreateDenseZero(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount);

272
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

@ -1,4 +1,34 @@
using System.Collections.Generic;
// <copyright file="MatrixStructureTheory.cs" company="Math.NET">
// 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.
// </copyright>
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
@ -15,7 +45,23 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
protected abstract Matrix<T> CreateSparseZero(int rows, int columns);
protected abstract Vector<T> CreateVectorZero(int size);
protected abstract Vector<T> 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<T> CreateDenseFor(Matrix<T> 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<T> 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<T> left, Matrix<T> 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<T> 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<T> 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<T> matrix)
{
var clone = (Matrix<T>) ((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<T> 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<T> matrix)
{
Assert.That(matrix.GetHashCode(), Is.Not.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount).GetHashCode()));
}
[Theory, Timeout(200)]
[Theory]
public void CanClear(Matrix<T> 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<T> 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<T> matrix)
{
var array = matrix.ToArray();
@ -149,29 +195,29 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
}
}
[Theory, Timeout(200)]
[Theory]
public void CanToColumnWiseArray(Matrix<T> 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<T> 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<T> matrix)
{
var empty = matrix.CreateMatrix(5, 6);
@ -183,85 +229,155 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.CreateMatrix(-1, -1), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[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<T>.CreateFromColumns(new List<Vector<T>>
{
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<T>.CreateFromRows(new List<Vector<T>>
[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);
}
}
}
}
}
}

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

@ -40,6 +40,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[TestFixture]
public class MatrixStructureTheory : MatrixStructureTheory<float>
{
public MatrixStructureTheory()
: base(0f, typeof(DenseMatrix), typeof(SparseMatrix), typeof(DiagonalMatrix), typeof(DenseVector), typeof(SparseVector))
{
}
[Datapoints]
Matrix<float>[] _matrices = new Matrix<float>[]
{
@ -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; }
}
}
}

78
src/UnitTests/LinearAlgebraTests/StaticDynamicWrapper.cs

@ -0,0 +1,78 @@
// <copyright file="StaticDynamicWrapper.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using System.Dynamic;
using System.Reflection;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
/// <summary>
/// Helper to allow calling static members of dynamic types.
/// Usefull to test static methods of e.g. all dense matrix types.
/// </summary>
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;
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -745,6 +745,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="LinearAlgebraTests\Double\VectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\StaticDynamicWrapper.cs" />
<Compile Include="LinearAlgebraTests\VectorArithmeticTheory.cs" />
<Compile Include="MatrixHelpers.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTest.cs" />

Loading…
Cancel
Save