19 changed files with 428 additions and 1365 deletions
@ -0,0 +1,50 @@ |
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex |
|||
{ |
|||
using LinearAlgebra.Complex; |
|||
using LinearAlgebra.Generic; |
|||
using NUnit.Framework; |
|||
using System.Numerics; |
|||
|
|||
[TestFixture] |
|||
public class MatrixStructureTheory : MatrixStructureTheory<Complex> |
|||
{ |
|||
[Datapoints] |
|||
Matrix<Complex>[] _matrices = new Matrix<Complex>[] |
|||
{ |
|||
new DenseMatrix(new[,] {{1d, new Complex(1.1d, -4d), 2d}, {1d, 1d, 2d}, {1d, new Complex(1d,2d), 2d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, new Complex(2.2d, -1.2d)}, {-4.4d, 5.5d, 6.6d}}), |
|||
new DenseMatrix(new[,] {{new Complex(-1.1d, -2d), -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, new Complex(-2.2d, 3.4d), -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d}, {Complex.Zero, 1.1d}, {-4.4d, 5.5d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, new Complex(1.1d, 0.1d), 2.2d}}), |
|||
new DenseMatrix(new[,] {{1d, 2d, 3d}, {2d, new Complex(2d, 2d), 0d}, {3d, Complex.Zero, 3d}}), |
|||
|
|||
new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d + Complex.ImaginaryOne, 2d}}), |
|||
new SparseMatrix(new[,] {{7d, 1d, 2d}, {new Complex(1d,2d), 0d, Complex.Zero}, {-2d, 0d, 0d}}), |
|||
new SparseMatrix(new[,] {{-1.1d, 0d, 0d}, {0d, new Complex(1.1d, 2d), 2.2d}}), |
|||
|
|||
new DiagonalMatrix(3, 3, new[] {new Complex(1d,1d), -2d, 1.5d}), |
|||
new DiagonalMatrix(3, 3, new[] {new Complex(1d,2d), 0d, -1.5d}), |
|||
|
|||
new UserDefinedMatrix(new[,] {{0d, 1d, 2d}, {-1d, 7.7d, 0d}, {-2d, Complex.Zero, 0d}}) |
|||
}; |
|||
|
|||
[Datapoints] |
|||
Complex[] scalars = new[] {new Complex(2d, 0d), new Complex(-1.5d, 3.5d), Complex.Zero}; |
|||
|
|||
protected override Matrix<Complex> CreateDense(int rows, int columns) |
|||
{ |
|||
return new DenseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Matrix<Complex> CreateSparse(int rows, int columns) |
|||
{ |
|||
return new SparseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Vector<Complex> CreateVector(int size) |
|||
{ |
|||
return new DenseVector(size); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 |
|||
{ |
|||
using LinearAlgebra.Complex32; |
|||
using LinearAlgebra.Generic; |
|||
using NUnit.Framework; |
|||
using Complex32 = Numerics.Complex32; |
|||
|
|||
[TestFixture] |
|||
public class MatrixStructureTheory : MatrixStructureTheory<Complex32> |
|||
{ |
|||
[Datapoints] |
|||
Matrix<Complex32>[] _matrices = new Matrix<Complex32>[] |
|||
{ |
|||
new DenseMatrix(new[,] {{1f, new Complex32(1.1f, -4f), 2f}, {1f, 1f, 2f}, {1f, new Complex32(1f,2f), 2f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, new Complex32(2.2f, -1.2f)}, {-4.4f, 5.5f, 6.6f}}), |
|||
new DenseMatrix(new[,] {{new Complex32(-1.1f, -2f), -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, new Complex32(-2.2f, 3.4f), -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f}, {Complex32.Zero, 1.1f}, {-4.4f, 5.5f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, new Complex32(1.1f, 0.1f), 2.2f}}), |
|||
new DenseMatrix(new[,] {{1f, 2f, 3f}, {2f, new Complex32(2f, 2f), 0f}, {3f, Complex32.Zero, 3f}}), |
|||
|
|||
new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f + Complex32.ImaginaryOne, 2f}}), |
|||
new SparseMatrix(new[,] {{7f, 1f, 2f}, {new Complex32(1f,2f), 0f, Complex32.Zero}, {-2f, 0f, 0f}}), |
|||
new SparseMatrix(new[,] {{-1.1f, 0f, 0f}, {0f, new Complex32(1.1f, 2f), 2.2f}}), |
|||
|
|||
new DiagonalMatrix(3, 3, new[] {new Complex32(1f,1f), -2f, 1.5f}), |
|||
new DiagonalMatrix(3, 3, new[] {new Complex32(1f,2f), 0f, -1.5f}), |
|||
|
|||
new UserDefinedMatrix(new[,] {{0f, 1f, 2f}, {-1f, 7.7f, 0f}, {-2f, Complex32.Zero, 0f}}) |
|||
}; |
|||
|
|||
[Datapoints] |
|||
Complex32[] scalars = new[] { new Complex32(2f, 0f), new Complex32(-1.5f, 3.5f), Complex32.Zero }; |
|||
|
|||
protected override Matrix<Complex32> CreateDense(int rows, int columns) |
|||
{ |
|||
return new DenseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Matrix<Complex32> CreateSparse(int rows, int columns) |
|||
{ |
|||
return new SparseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Vector<Complex32> CreateVector(int size) |
|||
{ |
|||
return new DenseVector(size); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|||
{ |
|||
using LinearAlgebra.Double; |
|||
using LinearAlgebra.Generic; |
|||
using NUnit.Framework; |
|||
|
|||
[TestFixture] |
|||
public class MatrixStructureTheory : MatrixStructureTheory<double> |
|||
{ |
|||
[Datapoints] |
|||
Matrix<double>[] _matrices = new Matrix<double>[] |
|||
{ |
|||
new DenseMatrix(new[,] {{1d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}, {-4.4d, 5.5d, 6.6d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {0d, 1.1d, 2.2d, 3.3d}, {1d, 2.1d, 6.2d, 4.3d}, {-4.4d, 5.5d, 6.6d, -7.7d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}, {-1.1d, -2.2d, -3.3d, -4.4d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d}, {0d, 1.1d}, {-4.4d, 5.5d}}), |
|||
new DenseMatrix(new[,] {{-1.1d, -2.2d, -3.3d}, {0d, 1.1d, 2.2d}}), |
|||
new DenseMatrix(new[,] {{1d, 2d, 3d}, {2d, 2d, 0d}, {3d, 0d, 3d}}), |
|||
|
|||
new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 1d, 2d}, {1d, 1d, 2d}}), |
|||
new SparseMatrix(new[,] {{7d, 1d, 2d}, {1d, 0d, 0d}, {-2d, 0d, 0d}}), |
|||
new SparseMatrix(new[,] {{-1.1d, 0d, 0d}, {0d, 1.1d, 2.2d}}), |
|||
|
|||
new DiagonalMatrix(3, 3, new[] {1d, -2d, 1.5d}), |
|||
new DiagonalMatrix(3, 3, new[] {1d, 0d, -1.5d}), |
|||
|
|||
new UserDefinedMatrix(new[,] {{0d, 1d, 2d}, {-1d, 7.7d, 0d}, {-2d, 0d, 0d}}) |
|||
}; |
|||
|
|||
[Datapoints] |
|||
double[] _scalars = new[] {2d, -1.5d, 0d}; |
|||
|
|||
protected override Matrix<double> CreateDense(int rows, int columns) |
|||
{ |
|||
return new DenseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Matrix<double> CreateSparse(int rows, int columns) |
|||
{ |
|||
return new SparseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Vector<double> CreateVector(int size) |
|||
{ |
|||
return new DenseVector(size); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,159 @@ |
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests |
|||
{ |
|||
using System; |
|||
using LinearAlgebra.Generic; |
|||
using NUnit.Framework; |
|||
|
|||
[TestFixture] |
|||
public abstract class MatrixStructureTheory<T> |
|||
where T : struct, IEquatable<T>, IFormattable |
|||
{ |
|||
protected abstract Matrix<T> CreateDense(int rows, int columns); |
|||
protected abstract Matrix<T> CreateSparse(int rows, int columns); |
|||
protected abstract Vector<T> CreateVector(int size); |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void IsEqualToItself(Matrix<T> matrix) |
|||
{ |
|||
Assert.That(matrix, Is.EqualTo(matrix)); |
|||
Assert.IsTrue(matrix.Equals(matrix)); |
|||
Assert.IsTrue(matrix.Equals((object) matrix)); |
|||
Assert.IsTrue(((object) matrix).Equals(matrix)); |
|||
Assert.IsTrue(matrix == (object) matrix); |
|||
Assert.IsTrue((object) matrix == matrix); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void IsNotEqualToOthers(Matrix<T> left, Matrix<T> right) |
|||
{ |
|||
// IF (assuming we don't have duplicate data points)
|
|||
Assume.That(left, Is.Not.SameAs(right)); |
|||
|
|||
// THEN
|
|||
Assert.That(left, Is.Not.EqualTo(right)); |
|||
Assert.IsFalse(left.Equals(right)); |
|||
Assert.IsFalse(left.Equals((object) right)); |
|||
Assert.IsFalse(((object) left).Equals(right)); |
|||
Assert.IsFalse(left == (object) right); |
|||
Assert.IsFalse((object) left == right); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void IsNotEqualToNonMatrixType(Matrix<T> matrix) |
|||
{ |
|||
Assert.That(matrix, Is.Not.EqualTo(2)); |
|||
Assert.IsFalse(matrix.Equals(2)); |
|||
Assert.IsFalse(matrix.Equals((object)2)); |
|||
Assert.IsFalse(((object)matrix).Equals(2)); |
|||
Assert.IsFalse(matrix == (object)2); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanClone(Matrix<T> matrix) |
|||
{ |
|||
var clone = matrix.Clone(); |
|||
Assert.That(clone, Is.Not.SameAs(matrix)); |
|||
Assert.That(clone, Is.EqualTo(matrix)); |
|||
Assert.That(clone.RowCount, Is.EqualTo(matrix.RowCount)); |
|||
Assert.That(clone.ColumnCount, Is.EqualTo(matrix.ColumnCount)); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanCloneUsingICloneable(Matrix<T> matrix) |
|||
{ |
|||
var clone = (Matrix<T>) ((ICloneable) matrix).Clone(); |
|||
Assert.That(clone, Is.Not.SameAs(matrix)); |
|||
Assert.That(clone, Is.EqualTo(matrix)); |
|||
Assert.That(clone.RowCount, Is.EqualTo(matrix.RowCount)); |
|||
Assert.That(clone.ColumnCount, Is.EqualTo(matrix.ColumnCount)); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanCopyTo(Matrix<T> matrix) |
|||
{ |
|||
var dense = CreateDense(matrix.RowCount, matrix.ColumnCount); |
|||
matrix.CopyTo(dense); |
|||
Assert.That(dense, Is.EqualTo(matrix)); |
|||
|
|||
var sparse = CreateSparse(matrix.RowCount, matrix.ColumnCount); |
|||
matrix.CopyTo(sparse); |
|||
Assert.That(sparse, Is.EqualTo(matrix)); |
|||
|
|||
// null arg
|
|||
Assert.That(() => matrix.CopyTo(null), Throws.InstanceOf<ArgumentNullException>()); |
|||
|
|||
// bad arg
|
|||
Assert.That(() => matrix.CopyTo(CreateDense(matrix.RowCount + 1, matrix.ColumnCount)), Throws.ArgumentException); |
|||
Assert.That(() => matrix.CopyTo(CreateDense(matrix.RowCount, matrix.ColumnCount + 1)), Throws.ArgumentException); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanCreateSameType(Matrix<T> matrix) |
|||
{ |
|||
var empty = matrix.CreateMatrix(5, 6); |
|||
Assert.That(empty, Is.EqualTo(CreateDense(5, 6))); |
|||
Assert.That(empty.GetType(), Is.EqualTo(matrix.GetType())); |
|||
|
|||
Assert.That(() => matrix.CreateMatrix(0, 2), Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => matrix.CreateMatrix(2, 0), Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => matrix.CreateMatrix(-1, -1), Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanGetHashCode(Matrix<T> matrix) |
|||
{ |
|||
Assert.That(matrix.GetHashCode(), Is.Not.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount).GetHashCode())); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanClear(Matrix<T> matrix) |
|||
{ |
|||
var cleared = matrix.Clone(); |
|||
cleared.Clear(); |
|||
Assert.That(cleared, Is.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount))); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanGetFieldsByIndex(Matrix<T> matrix) |
|||
{ |
|||
Assert.That(() => { var x = matrix[0, 0]; }, Throws.Nothing); |
|||
Assert.That(() => { var x = matrix[0, matrix.ColumnCount - 1]; }, Throws.Nothing); |
|||
Assert.That(() => { var x = matrix[matrix.RowCount - 1, 0]; }, Throws.Nothing); |
|||
|
|||
Assert.That(() => { var x = matrix[-1, 1]; }, Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => { var x = matrix[1, -1]; }, Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => { var x = matrix[0, matrix.ColumnCount]; }, Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanGetRow(Matrix<T> matrix, [Values(0, 1, 1000)]int rowIndex) |
|||
{ |
|||
var actualRow = Math.Min(rowIndex, matrix.RowCount - 1); |
|||
var row = matrix.Row(actualRow); |
|||
Assert.That(row.Count, Is.EqualTo(matrix.ColumnCount)); |
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
Assert.AreEqual(matrix[actualRow, j], row[j]); |
|||
} |
|||
|
|||
Assert.That(() => { matrix.Row(-1); }, Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => { matrix.Row(matrix.RowCount); }, Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
} |
|||
|
|||
[Theory, Timeout(200)] |
|||
public void CanGetRowIntoResult(Matrix<T> matrix) |
|||
{ |
|||
var row = CreateVector(matrix.ColumnCount); |
|||
matrix.Row(0, row); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
Assert.AreEqual(matrix[0, j], row[j]); |
|||
} |
|||
|
|||
Assert.That(() => matrix.Row(0, null), Throws.InstanceOf<ArgumentNullException>()); |
|||
Assert.That(() => matrix.Row(-1, row), Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
Assert.That(() => matrix.Row(matrix.RowCount, row), Throws.InstanceOf<ArgumentOutOfRangeException>()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single |
|||
{ |
|||
using LinearAlgebra.Single; |
|||
using LinearAlgebra.Generic; |
|||
using NUnit.Framework; |
|||
|
|||
[TestFixture] |
|||
public class MatrixStructureTheory : MatrixStructureTheory<float> |
|||
{ |
|||
[Datapoints] |
|||
Matrix<float>[] _matrices = new Matrix<float>[] |
|||
{ |
|||
new DenseMatrix(new[,] {{1f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}, {-4.4f, 5.5f, 6.6f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {0f, 1.1f, 2.2f, 3.3f}, {1f, 2.1f, 6.2f, 4.3f}, {-4.4f, 5.5f, 6.6f, -7.7f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}, {-1.1f, -2.2f, -3.3f, -4.4f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f}, {0f, 1.1f}, {-4.4f, 5.5f}}), |
|||
new DenseMatrix(new[,] {{-1.1f, -2.2f, -3.3f}, {0f, 1.1f, 2.2f}}), |
|||
new DenseMatrix(new[,] {{1f, 2f, 3f}, {2f, 2f, 0f}, {3f, 0f, 3f}}), |
|||
|
|||
new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 1f, 2f}, {1f, 1f, 2f}}), |
|||
new SparseMatrix(new[,] {{7f, 1f, 2f}, {1f, 0f, 0f}, {-2f, 0f, 0f}}), |
|||
new SparseMatrix(new[,] {{-1.1f, 0f, 0f}, {0f, 1.1f, 2.2f}}), |
|||
|
|||
new DiagonalMatrix(3, 3, new[] {1f, -2f, 1.5f}), |
|||
new DiagonalMatrix(3, 3, new[] {1f, 0f, -1.5f}), |
|||
|
|||
new UserDefinedMatrix(new[,] {{0f, 1f, 2f}, {-1f, 7.7f, 0f}, {-2f, 0f, 0f}}) |
|||
}; |
|||
|
|||
[Datapoints] |
|||
float[] _scalars = new[] {2f, -1.5f, 0f}; |
|||
|
|||
protected override Matrix<float> CreateDense(int rows, int columns) |
|||
{ |
|||
return new DenseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Matrix<float> CreateSparse(int rows, int columns) |
|||
{ |
|||
return new SparseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Vector<float> CreateVector(int size) |
|||
{ |
|||
return new DenseVector(size); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue