From 8a95a1ccdcdd505a3a86ca60c6c835dc9cc5f764 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Wed, 9 Mar 2011 23:55:28 +0200 Subject: [PATCH] Unit Tests for Complex. --- .../Complex/MatrixTests.Arithmetic.cs | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs index 8f6d8d4c..06b13065 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs @@ -625,6 +625,160 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex } } + /// + /// Can multiply transposed matrix with a vector. + /// + [Test] + public void CanTransposeThisAndMultiplyWithVector() + { + var matrix = TestMatrices["Singular3x3"]; + var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + var y = matrix.TransposeThisAndMultiply(x); + + Assert.AreEqual(matrix.ColumnCount, y.Count); + + for (var j = 0; j < matrix.ColumnCount; j++) + { + var ar = matrix.Column(j); + var dot = ar * x; + Assert.AreEqual(dot, y[j]); + } + } + + /// + /// Can multiply transposed matrix with a vector into a result. + /// + [Test] + public void CanTransposeThisAndMultiplyWithVectorIntoResult() + { + var matrix = TestMatrices["Singular3x3"]; + var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + var y = new DenseVector(3); + matrix.TransposeThisAndMultiply(x, y); + + for (var j = 0; j < matrix.ColumnCount; j++) + { + var ar = matrix.Column(j); + var dot = ar * x; + Assert.AreEqual(dot, y[j]); + } + } + + /// + /// Can multiply transposed matrix with a vector into result when updating input argument. + /// + [Test] + public void CanTransposeThisAndMultiplyWithVectorIntoResultWhenUpdatingInputArgument() + { + var matrix = TestMatrices["Singular3x3"]; + var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + var y = x; + matrix.TransposeThisAndMultiply(x, x); + + Assert.AreSame(y, x); + + y = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + for (var j = 0; j < matrix.ColumnCount; j++) + { + var ar = matrix.Column(j); + var dot = ar * y; + Assert.AreEqual(dot, x[j]); + } + } + + /// + /// Multiply transposed matrix with vector into result fails when result is null. + /// + [Test] + public void TransposeThisAndMultiplyWithVectorIntoNullResultThrowsArgumentNullException() + { + var matrix = TestMatrices["Singular3x3"]; + var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + Vector y = null; + Assert.Throws(() => matrix.TransposeThisAndMultiply(x, y)); + } + + /// + /// Multiply transposed matrix with a vector into too large result throws ArgumentException. + /// + [Test] + public void TransposeThisAndMultiplyWithVectorIntoLargerResultThrowsArgumentException() + { + var matrix = TestMatrices["Singular3x3"]; + var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) }); + Vector y = new DenseVector(4); + Assert.Throws(() => matrix.TransposeThisAndMultiply(x, y)); + } + + /// + /// Can multiply transposed matrix with another matrix. + /// + /// Matrix name. + [Test, Sequential] + public void CanTransposeThisAndMultiplyMatrixWithMatrix([Values("Singular3x3", "Singular4x4", "Wide2x3", "Tall3x2")] string nameA) + { + var matrixA = TestMatrices[nameA]; + var matrixB = TestMatrices[nameA]; + var matrixC = matrixA.TransposeThisAndMultiply(matrixB); + + Assert.AreEqual(matrixC.RowCount, matrixA.ColumnCount); + Assert.AreEqual(matrixC.ColumnCount, matrixB.ColumnCount); + + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + AssertHelpers.AlmostEqual(matrixA.Column(i) * matrixB.Column(j), matrixC[i, j], 15); + } + } + } + + /// + /// Multiply the transpose of matrix with another matrix of incompatible size throws ArgumentException. + /// + [Test] + public void TransposeThisAndMultiplyMatrixMatrixWithIncompatibleSizesThrowsArgumentException() + { + var matrix = TestMatrices["Wide2x3"]; + var other = TestMatrices["Singular3x3"]; + Assert.Throws(() => matrix.TransposeThisAndMultiply(other)); + } + + /// + /// Multiply the transpose of matrix with another matrix null matrix throws ArgumentNullException. + /// + [Test] + public void TransposeThisAndMultiplyMatrixWithNullMatrixThrowsArgumentNullException() + { + var matrix = TestMatrices["Wide2x3"]; + Matrix other = null; + Assert.Throws(() => matrix.TransposeThisAndMultiply(other)); + } + + /// + /// Multiply transpose of this matrix with another matrix into a result matrix. + /// + /// Matrix name. + [Test, Sequential] + public void CanTransposeThisAndMultiplyMatrixWithMatrixIntoResult([Values("Singular3x3", "Singular4x4", "Wide2x3", "Tall3x2")] string nameA) + { + var matrixA = TestMatrices[nameA]; + var matrixB = TestMatrices[nameA]; + var matrixC = CreateMatrix(matrixA.ColumnCount, matrixB.ColumnCount); + matrixA.TransposeThisAndMultiply(matrixB, matrixC); + + Assert.AreEqual(matrixC.RowCount, matrixA.ColumnCount); + Assert.AreEqual(matrixC.ColumnCount, matrixB.ColumnCount); + + for (var i = 0; i < matrixC.RowCount; i++) + { + for (var j = 0; j < matrixC.ColumnCount; j++) + { + AssertHelpers.AlmostEqual(matrixA.Column(i) * matrixB.Column(j), matrixC[i, j], 15); + } + } + } + /// /// Multiply a matrix with incompatible size matrix throws ArgumentException. ///