forked from tsai/mathnet-numerics
3 changed files with 290 additions and 281 deletions
@ -0,0 +1,289 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using MathNet.Numerics.LinearAlgebra.Double; |
||||
|
using MbUnit.Framework; |
||||
|
|
||||
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
||||
|
{ |
||||
|
public abstract partial class MatrixTests |
||||
|
{ |
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
public void AddMatrix(string mtxA, string mtxB) |
||||
|
{ |
||||
|
var A = testMatrices[mtxA]; |
||||
|
var B = testMatrices[mtxB]; |
||||
|
|
||||
|
Matrix matrix = A.Clone(); |
||||
|
matrix.Add(B); |
||||
|
for (int i = 0; i < matrix.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(matrix[i, j], A[i, j] + B[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void AddMatrixThrowsExceptionWhenArgumentIsNull() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular4x4"]; |
||||
|
Matrix other = null; |
||||
|
matrix.Add(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void AddMatrixThrowsExceptionArgumentHasTooFewColumns() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Tall3x2"]; |
||||
|
matrix.Add(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void AddMatrixThrowsExceptionArgumentHasTooFewRows() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
matrix.Add(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
public void AddOperator(string mtxA, string mtxB) |
||||
|
{ |
||||
|
var A = testMatrices[mtxA]; |
||||
|
var B = testMatrices[mtxB]; |
||||
|
|
||||
|
Matrix result = A + B; |
||||
|
for (int i = 0; i < A.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < A.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(result[i,j], A[i, j] + B[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void AddOperatorThrowsExceptionWhenLeftsideIsNull() |
||||
|
{ |
||||
|
Matrix matrix = null; |
||||
|
Matrix other = testMatrices["Singular3x3"]; |
||||
|
Matrix result = matrix + other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void AddOperatorThrowsExceptionWhenRightsideIsNull() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = null; |
||||
|
Matrix result = matrix + other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void AddOperatorThrowsExceptionWhenRightsideHasTooFewColumns() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Tall3x2"]; |
||||
|
Matrix result = matrix + other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void AddOperatorThrowsExceptionWhenRightsideHasTooFewRows() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
Matrix result = matrix + other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
public void SubtractMatrix(string mtxA, string mtxB) |
||||
|
{ |
||||
|
var A = testMatrices[mtxA]; |
||||
|
var B = testMatrices[mtxB]; |
||||
|
|
||||
|
Matrix matrix = A.Clone(); |
||||
|
matrix.Subtract(B); |
||||
|
for (int i = 0; i < matrix.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(matrix[i, j], A[i, j] - B[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void SubtractMatrixThrowsExceptionWhenRightSideIsNull() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular4x4"]; |
||||
|
Matrix other = null; |
||||
|
matrix.Subtract(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void SubtractMatrixThrowsExceptionWhenRightSideHasTooFewColumns() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Tall3x2"]; |
||||
|
matrix.Subtract(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void SubtractMatrixThrowsExceptionWhenRightSideHasTooFewRows() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
matrix.Subtract(other); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
public void SubtractOperator(string mtxA, string mtxB) |
||||
|
{ |
||||
|
var A = testMatrices[mtxA]; |
||||
|
var B = testMatrices[mtxB]; |
||||
|
|
||||
|
Matrix result = A - B; |
||||
|
for (int i = 0; i < A.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < A.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(result[i, j], A[i, j] - B[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void SubtractOperatorThrowsExceptionWhenLeftsideIsNull() |
||||
|
{ |
||||
|
Matrix matrix = null; |
||||
|
Matrix other = testMatrices["Singular3x3"]; |
||||
|
Matrix result = matrix - other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void SubtractOperatorThrowsExceptionWhenRightsideIsNull() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = null; |
||||
|
Matrix result = matrix - other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void SubtractOperatorThrowsExceptionWhenRightsideHasTooFewColumns() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Tall3x2"]; |
||||
|
Matrix result = matrix - other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
||||
|
public void SubtractOperatorThrowsExceptionWhenRightsideHasTooFewRows() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
Matrix result = matrix - other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
[Row("Wide2x3", "Square3x3")] |
||||
|
[Row("Wide2x3", "Tall3x2")] |
||||
|
[Row("Tall3x2", "Wide2x3")] |
||||
|
[MultipleAsserts] |
||||
|
public void CanMultiplyMatrixWithMatrix(string nameA, string nameB) |
||||
|
{ |
||||
|
Matrix A = testMatrices[nameA]; |
||||
|
Matrix B = testMatrices[nameB]; |
||||
|
Matrix C = A * B; |
||||
|
|
||||
|
Assert.AreEqual(C.RowCount, A.RowCount); |
||||
|
Assert.AreEqual(C.ColumnCount, B.ColumnCount); |
||||
|
|
||||
|
for (int i = 0; i < C.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < C.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(A.GetRow(i) * B.GetColumn(j), C[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentException))] |
||||
|
public void MultiplyMatrixMatrixFailsWhenSizesAreIncompatible() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Singular3x3"]; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
Matrix result = matrix * other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void MultiplyMatrixMatrixFailsWhenLeftArgumentIsNull() |
||||
|
{ |
||||
|
Matrix matrix = null; |
||||
|
Matrix other = testMatrices["Wide2x3"]; |
||||
|
Matrix result = matrix * other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[ExpectedException(typeof(ArgumentNullException))] |
||||
|
public void MultiplyMatrixMatrixFailsWhenRightArgumentIsNull() |
||||
|
{ |
||||
|
Matrix matrix = testMatrices["Wide2x3"]; |
||||
|
Matrix other = null; |
||||
|
Matrix result = matrix * other; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
[Row("Singular3x3", "Square3x3")] |
||||
|
[Row("Singular4x4", "Square4x4")] |
||||
|
[Row("Wide2x3", "Square3x3")] |
||||
|
[Row("Wide2x3", "Tall3x2")] |
||||
|
[Row("Tall3x2", "Wide2x3")] |
||||
|
[MultipleAsserts] |
||||
|
public void CanMultiplyMatrixWithMatrixIntoResult(string nameA, string nameB) |
||||
|
{ |
||||
|
Matrix A = testMatrices[nameA]; |
||||
|
Matrix B = testMatrices[nameB]; |
||||
|
Matrix C = CreateMatrix(A.RowCount, B.ColumnCount); |
||||
|
A.Multiply(B, C); |
||||
|
|
||||
|
Assert.AreEqual(C.RowCount, A.RowCount); |
||||
|
Assert.AreEqual(C.ColumnCount, B.ColumnCount); |
||||
|
|
||||
|
for (int i = 0; i < C.RowCount; i++) |
||||
|
{ |
||||
|
for (int j = 0; j < C.ColumnCount; j++) |
||||
|
{ |
||||
|
Assert.AreEqual(A.GetRow(i) * B.GetColumn(j), C[i, j]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue