From 2eb986c6a9b5015013fe0bf2712103dd858300bf Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Sun, 24 Jan 2010 01:15:09 +0800 Subject: [PATCH] Added unit tests for matrix multiply. --- .../LinearAlgebraTests/Double/MatrixTests.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index 22a2476d..588eb257 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs @@ -606,6 +606,85 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double 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 CanMultiplyMatrixWithMatrix(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