diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs new file mode 100644 index 00000000..019a6669 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs @@ -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]); + } + } + } + } +} \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index 80909d09..0cccc8a7 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs @@ -405,286 +405,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Vector result = CreateVector(matrix.RowCount - 1); matrix.GetColumn(0, 0, matrix.RowCount, result); } - - #region Elementary operations - [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]); - } - } - } - - #endregion } } \ No newline at end of file diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index da4ec07a..e78afe4c 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -90,6 +90,7 @@ +