|
|
|
@ -49,7 +49,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
/// <returns>A matrix with the given dimensions.</returns>
|
|
|
|
protected override Matrix<double> CreateMatrix(int rows, int columns) |
|
|
|
{ |
|
|
|
return new SparseMatrix(rows, columns); |
|
|
|
return Matrix<double>.Build.Sparse(rows, columns); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -59,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
/// <returns>A matrix with the given values.</returns>
|
|
|
|
protected override Matrix<double> CreateMatrix(double[,] data) |
|
|
|
{ |
|
|
|
return SparseMatrix.OfArray(data); |
|
|
|
return Matrix<double>.Build.SparseOfArray(data); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -70,7 +70,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
/// <returns>The new vector. </returns>
|
|
|
|
protected override Vector<double> CreateVector(int size) |
|
|
|
{ |
|
|
|
return new SparseVector(size); |
|
|
|
return Vector<double>.Build.Sparse(size); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -80,7 +80,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
/// <returns>The new vector. </returns>
|
|
|
|
protected override Vector<double> CreateVector(double[] data) |
|
|
|
{ |
|
|
|
return SparseVector.OfEnumerable(data); |
|
|
|
return Vector<double>.Build.SparseOfEnumerable(data); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -91,11 +91,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
{ |
|
|
|
var testData = new Dictionary<string, Matrix<double>> |
|
|
|
{ |
|
|
|
{"Singular3x3", SparseMatrix.OfColumnMajor(3, 3, new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2})}, |
|
|
|
{"Square3x3", SparseMatrix.OfColumnMajor(3, 3, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6})}, |
|
|
|
{"Square4x4", SparseMatrix.OfColumnMajor(4, 4, new[] {-1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7})}, |
|
|
|
{"Tall3x2", SparseMatrix.OfColumnMajor(3, 2, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5})}, |
|
|
|
{"Wide2x3", SparseMatrix.OfColumnMajor(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})} |
|
|
|
{"Singular3x3", Matrix<double>.Build.SparseOfColumnMajor(3, 3, new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2})}, |
|
|
|
{"Square3x3", Matrix<double>.Build.SparseOfColumnMajor(3, 3, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6})}, |
|
|
|
{"Square4x4", Matrix<double>.Build.SparseOfColumnMajor(4, 4, new[] {-1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7})}, |
|
|
|
{"Tall3x2", Matrix<double>.Build.SparseOfColumnMajor(3, 2, new[] {-1.1, 0.0, -4.4, -2.2, 1.1, 5.5})}, |
|
|
|
{"Wide2x3", Matrix<double>.Build.SparseOfColumnMajor(2, 3, new[] {-1.1, 0.0, -2.2, 1.1, -3.3, 2.2})} |
|
|
|
}; |
|
|
|
|
|
|
|
foreach (var name in testData.Keys) |
|
|
|
@ -112,7 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
{ |
|
|
|
// Sparse Matrix copies values from double[], but no remember reference.
|
|
|
|
var data = new double[] {1, 1, 1, 1, 1, 1, 2, 2, 2}; |
|
|
|
var matrix = SparseMatrix.OfColumnMajor(3, 3, data); |
|
|
|
var matrix = Matrix<double>.Build.SparseOfColumnMajor(3, 3, data); |
|
|
|
matrix[0, 0] = 10.0; |
|
|
|
Assert.AreNotEqual(10.0, data[0]); |
|
|
|
} |
|
|
|
@ -123,7 +123,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
[Test] |
|
|
|
public void MatrixFrom2DArrayIsCopy() |
|
|
|
{ |
|
|
|
var matrix = SparseMatrix.OfArray(TestData2D["Singular3x3"]); |
|
|
|
var matrix = Matrix<double>.Build.SparseOfArray(TestData2D["Singular3x3"]); |
|
|
|
matrix[0, 0] = 10.0; |
|
|
|
Assert.AreEqual(1.0, TestData2D["Singular3x3"][0, 0]); |
|
|
|
} |
|
|
|
@ -140,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
[TestCase("Wide2x3")] |
|
|
|
public void CanCreateMatrixFrom2DArray(string name) |
|
|
|
{ |
|
|
|
var matrix = SparseMatrix.OfArray(TestData2D[name]); |
|
|
|
var matrix = Matrix<double>.Build.SparseOfArray(TestData2D[name]); |
|
|
|
for (var i = 0; i < TestData2D[name].GetLength(0); i++) |
|
|
|
{ |
|
|
|
for (var j = 0; j < TestData2D[name].GetLength(1); j++) |
|
|
|
@ -156,7 +156,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
[Test] |
|
|
|
public void CanCreateIdentity() |
|
|
|
{ |
|
|
|
var matrix = SparseMatrix.CreateIdentity(5); |
|
|
|
var matrix = Matrix<double>.Build.SparseIdentity(5); |
|
|
|
Assert.That(matrix, Is.TypeOf<SparseMatrix>()); |
|
|
|
for (var i = 0; i < matrix.RowCount; i++) |
|
|
|
{ |
|
|
|
for (var j = 0; j < matrix.ColumnCount; j++) |
|
|
|
@ -174,7 +175,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
[TestCase(-1)] |
|
|
|
public void IdentityWithWrongOrderThrowsArgumentOutOfRangeException(int order) |
|
|
|
{ |
|
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => SparseMatrix.CreateIdentity(order)); |
|
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => Matrix<double>.Build.SparseIdentity(order)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -211,37 +212,37 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
public void CanAddSparseMatricesBothWays() |
|
|
|
{ |
|
|
|
var m1 = new SparseMatrix(1, 3); |
|
|
|
var m2 = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var m2 = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var sum1 = m1 + m2; |
|
|
|
var sum2 = m2 + m1; |
|
|
|
Assert.IsTrue(sum1.Equals(m2)); |
|
|
|
Assert.IsTrue(sum1.Equals(sum2)); |
|
|
|
|
|
|
|
var sparseResult = new SparseMatrix(1, 3); |
|
|
|
Matrix<double> sparseResult = new SparseMatrix(1, 3); |
|
|
|
sparseResult.Add(m2, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(sum1)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult.Add(m1, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(sum1)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
m1.Add(sparseResult, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(sum1)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult.Add(sparseResult, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(2*sum1)); |
|
|
|
|
|
|
|
var denseResult = new DenseMatrix(1, 3); |
|
|
|
Matrix<double> denseResult = new DenseMatrix(1, 3); |
|
|
|
denseResult.Add(m2, denseResult); |
|
|
|
Assert.IsTrue(denseResult.Equals(sum1)); |
|
|
|
|
|
|
|
denseResult = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); |
|
|
|
denseResult = Matrix<double>.Build.DenseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
denseResult.Add(m1, denseResult); |
|
|
|
Assert.IsTrue(denseResult.Equals(sum1)); |
|
|
|
|
|
|
|
var m3 = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); |
|
|
|
var m3 = Matrix<double>.Build.DenseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var sum3 = m1 + m3; |
|
|
|
var sum4 = m3 + m1; |
|
|
|
Assert.IsTrue(sum3.Equals(m3)); |
|
|
|
@ -255,37 +256,37 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
public void CanSubtractSparseMatricesBothWays() |
|
|
|
{ |
|
|
|
var m1 = new SparseMatrix(1, 3); |
|
|
|
var m2 = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var m2 = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var diff1 = m1 - m2; |
|
|
|
var diff2 = m2 - m1; |
|
|
|
Assert.IsTrue(diff1.Equals(m2.Negate())); |
|
|
|
Assert.IsTrue(diff1.Equals(diff2.Negate())); |
|
|
|
|
|
|
|
var sparseResult = new SparseMatrix(1, 3); |
|
|
|
Matrix<double> sparseResult = new SparseMatrix(1, 3); |
|
|
|
sparseResult.Subtract(m2, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(diff1)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult.Subtract(m1, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(diff2)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
m1.Subtract(sparseResult, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(diff1)); |
|
|
|
|
|
|
|
sparseResult = SparseMatrix.OfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult = Matrix<double>.Build.SparseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
sparseResult.Subtract(sparseResult, sparseResult); |
|
|
|
Assert.IsTrue(sparseResult.Equals(0*diff1)); |
|
|
|
|
|
|
|
var denseResult = new DenseMatrix(1, 3); |
|
|
|
Matrix<double> denseResult = new DenseMatrix(1, 3); |
|
|
|
denseResult.Subtract(m2, denseResult); |
|
|
|
Assert.IsTrue(denseResult.Equals(diff1)); |
|
|
|
|
|
|
|
denseResult = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); |
|
|
|
denseResult = Matrix<double>.Build.DenseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
denseResult.Subtract(m1, denseResult); |
|
|
|
Assert.IsTrue(denseResult.Equals(diff2)); |
|
|
|
|
|
|
|
var m3 = DenseMatrix.OfArray(new double[,] {{0, 1, 1}}); |
|
|
|
var m3 = Matrix<double>.Build.DenseOfArray(new double[,] { { 0, 1, 1 } }); |
|
|
|
var diff3 = m1 - m3; |
|
|
|
var diff4 = m3 - m1; |
|
|
|
Assert.IsTrue(diff3.Equals(m3.Negate())); |
|
|
|
@ -299,7 +300,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|
|
|
public void CanCreateLargeMatrix() |
|
|
|
{ |
|
|
|
const int Order = 1000000; |
|
|
|
var matrix = new SparseMatrix(Order); |
|
|
|
var matrix = Matrix<double>.Build.Sparse(Order, Order); |
|
|
|
Assert.AreEqual(Order, matrix.RowCount); |
|
|
|
Assert.AreEqual(Order, matrix.ColumnCount); |
|
|
|
Assert.DoesNotThrow(() => matrix[0, 0] = 1); |
|
|
|
|