From 148ac01b6f4d698d99ced565eb8ac26c45030935 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 26 Nov 2012 21:50:06 +0200 Subject: [PATCH] Add Unit tests for Complex32 version of Symmetric matrix and fix bugs Signed-off-by: Alexander Karatarakis --- .../Complex32/SymmetricDenseMatrix.cs | 2 +- .../Complex32/SymmetricMatrix.cs | 18 ++ .../Complex32/SymmetricDenseMatrixTests.cs | 217 ++++++++++++++++++ .../SymmetricMatrixTests.Arithmetic.cs | 192 ++++++++++++++++ .../Complex32/SymmetricMatrixTests.cs | 125 ++++++++++ src/UnitTests/UnitTests.csproj | 3 + 6 files changed, 556 insertions(+), 1 deletion(-) create mode 100644 src/UnitTests/LinearAlgebraTests/Complex32/SymmetricDenseMatrixTests.cs create mode 100644 src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.Arithmetic.cs create mode 100644 src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.cs diff --git a/src/Numerics/LinearAlgebra/Complex32/SymmetricDenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SymmetricDenseMatrix.cs index 7e62c4d7..38cbc136 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SymmetricDenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SymmetricDenseMatrix.cs @@ -233,7 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 var m = new SymmetricDenseMatrix(order); for (var i = 0; i < order; i++) { - m.At(i, i, 1.0f); + m.At(i, i, Complex32.One); } return m; diff --git a/src/Numerics/LinearAlgebra/Complex32/SymmetricMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SymmetricMatrix.cs index 6d84785b..e283fd63 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SymmetricMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SymmetricMatrix.cs @@ -107,6 +107,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return this.Clone(); } + /// + /// Returns the conjugate transpose of this matrix. + /// + /// The conjugate transpose of this matrix. + public override Matrix ConjugateTranspose() + { + var ret = CreateMatrix(ColumnCount, RowCount); + for (var row = 0; row < RowCount; row++) + { + for (var column = row; column < ColumnCount; column++) + { + ret.At(row, column, At(column, row).Conjugate()); + } + } + + return ret; + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricDenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricDenseMatrixTests.cs new file mode 100644 index 00000000..75747b33 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricDenseMatrixTests.cs @@ -0,0 +1,217 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// Copyright (c) 2009-2010 Math.NET +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 +{ + using System; + using System.Collections.Generic; + using Numerics; + + using MathNet.Numerics.LinearAlgebra.Complex32; + + using NUnit.Framework; + + /// + /// Symmetric Dense matrix tests. + /// + public class SymmetricDenseMatrixTests : SymmetricMatrixTests + { + /// + /// Creates a matrix for the given number of rows and columns. + /// + /// + /// The number of rows. + /// + /// + /// The number of columns. + /// + /// + /// A matrix with the given dimensions. + /// + protected override Matrix CreateMatrix(int rows, int columns) + { + return new DenseMatrix(rows, columns); + } + + /// + /// Creates a matrix from a 2D array. + /// + /// + /// The 2D array to create this matrix from. + /// + /// + /// A matrix with the given values. + /// + protected override Matrix CreateMatrix(Complex32[,] data) + { + if (SymmetricMatrix.CheckIfSymmetric(data)) + { + return new SymmetricDenseMatrix(data); + } + + return new DenseMatrix(data); + } + + /// + /// Creates a vector of the given size. + /// + /// + /// The size of the vector to create. + /// + /// + /// The new vector. + /// + protected override Vector CreateVector(int size) + { + return new DenseVector(size); + } + + /// + /// Creates a vector from an array. + /// + /// + /// The array to create this vector from. + /// + /// + /// The new vector. + /// + protected override Vector CreateVector(Complex32[] data) + { + return new DenseVector(data); + } + + /// + /// Can create a matrix form array. + /// + [Test] + public void CanCreateMatrixFrom1DArray() + { + var testData = new Dictionary + { + { "Singular3x3", new SymmetricDenseMatrix(3, new[] { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(0.0f, 1), new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) }) }, + { "Square3x3", new SymmetricDenseMatrix(3, new[] { new Complex32(-1.1f, 1), new Complex32(2.0f, 1), new Complex32(1.1f, 1), new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(6.6f, 1) }) }, + { "Square4x4", new SymmetricDenseMatrix(4, new[] { new Complex32(1.1f, 1), new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(-3.0f, 1), new Complex32(-6.0f, 1), new Complex32(8.0f, 1), new Complex32(4.4f, 1), new Complex32(7.0f, 1), new Complex32(9.0f, 1), new Complex32(10.0f, 1) }) }, + { "Singular4x4", new SymmetricDenseMatrix(4, new[] { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(4.0f, 1), new Complex32(7.0f, 1), new Complex32(0.0f, 1), new Complex32(10.0f, 1) }) }, + { "Symmetric3x3", new SymmetricDenseMatrix(3, new[] { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(3.0f, 1) }) }, + { "IndexTester4x4", new SymmetricDenseMatrix(4, new [] { new Complex32(0, 1), new Complex32(1, 1), new Complex32(2, 1), new Complex32(3, 1), new Complex32(4, 1), new Complex32(5, 1), new Complex32(6, 1), new Complex32(7, 1), new Complex32(8, 1), new Complex32(9, 1) }) } + }; + + foreach (var name in testData.Keys) + { + Assert.AreEqual(TestMatrices[name], testData[name]); + } + } + + /// + /// Matrix from array is a reference. + /// + [Test] + public void MatrixFrom1DArrayIsReference() + { + var data = new Complex32[] { new Complex32(1, 1), new Complex32(1, 1), new Complex32(1, 1), new Complex32(1, 1), new Complex32(1, 1), new Complex32(1, 1) }; + var matrix = new SymmetricDenseMatrix(3, data); + matrix[0, 0] = new Complex32(10.0f, 2); + Assert.AreEqual(new Complex32(10.0f, 2), data[0]); + } + + /// + /// Can create a matrix form array. + /// + [Test] + public void CanCreateMatrixFrom2DArray() + { + var testData = new Dictionary + { + { "Singular3x3", new SymmetricDenseMatrix(new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) } }) }, + { "Square3x3", new SymmetricDenseMatrix(new[,] { { new Complex32(-1.1f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(1.1f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(6.6f, 1) } }) }, + { "Square4x4", new SymmetricDenseMatrix(new[,] { { new Complex32(1.1f, 1), new Complex32(2.0f, 1), new Complex32(-3.0f, 1), new Complex32(4.4f, 1) }, { new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(-6.0f, 1), new Complex32(7.0f, 1) }, { new Complex32(-3.0f, 1), new Complex32(-6.0f, 1), new Complex32(8.0f, 1), new Complex32(9.0f, 1) }, { new Complex32(4.4f, 1), new Complex32(7.0f, 1), new Complex32(9.0f, 1), new Complex32(10.0f, 1) } }) }, + { "Singular4x4", new SymmetricDenseMatrix(new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(0.0f, 1), new Complex32(4.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(0.0f, 1), new Complex32(7.0f, 1) }, { new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(4.0f, 1), new Complex32(7.0f, 1), new Complex32(0.0f, 1), new Complex32(10.0f, 1) } }) }, + { "Symmetric3x3", new SymmetricDenseMatrix(new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(3.0f, 1) } }) }, + { "IndexTester4x4", new SymmetricDenseMatrix(new [,] { { new Complex32(0, 1), new Complex32(1, 1), new Complex32(3, 1), new Complex32(6, 1) }, { new Complex32(1, 1), new Complex32(2, 1), new Complex32(4, 1), new Complex32(7, 1) }, { new Complex32(3, 1), new Complex32(4, 1), new Complex32(5, 1), new Complex32(8, 1) }, { new Complex32(6, 1), new Complex32(7, 1), new Complex32(8, 1), new Complex32(9, 1) } }) } + }; + + foreach (var name in testData.Keys) + { + Assert.AreEqual(TestMatrices[name], testData[name]); + } + } + + /// + /// Matrix from two-dimensional array is a copy. + /// + [Test] + public void MatrixFrom2DArrayIsCopy() + { + var matrix = new DenseMatrix(TestData2D["Singular3x3"]); + matrix[0, 0] = new Complex32(10.0f, 2); + Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]); + } + + /// + /// Can create a matrix with uniform values. + /// + [Test] + public void CanCreateMatrixWithUniformValues() + { + var matrix = new SymmetricDenseMatrix(10, new Complex32(10.0f, 2)); + var value = new Complex32(10.0f, 2); + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[i, j], value); + } + } + } + + /// + /// Can create an identity matrix. + /// + [Test] + public void CanCreateIdentity() + { + var matrix = SymmetricDenseMatrix.Identity(5); + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(i == j ? Complex32.One : Complex32.Zero, matrix[i, j]); + } + } + } + + /// + /// Identity with wrong order throws ArgumentOutOfRangeException. + /// + /// The size of the square matrix + [TestCase(0)] + [TestCase(-1)] + public void IdentityWithWrongOrderThrowsArgumentOutOfRangeException(int order) + { + Assert.Throws(() => SymmetricDenseMatrix.Identity(order)); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.Arithmetic.cs new file mode 100644 index 00000000..3d924a53 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.Arithmetic.cs @@ -0,0 +1,192 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// Copyright (c) 2009-2010 Math.NET +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 +{ + using System.Collections.Generic; + using LinearAlgebra.Complex32; + using NUnit.Framework; + using Numerics; + + /// + /// Abstract class with the common set of matrix tests for symmetric matrices + /// + public abstract partial class SymmetricMatrixTests + { + /// + /// Setup test matrices. + /// Singular and Square matrices are overridden here with symmetric ones so that calls to base methods work as intended. + /// Additional NonSymmetric matrices are defined for some tests. + /// + [SetUp] + public override void SetupMatrices() + { + TestData2D = new Dictionary + { + { "Singular3x3", new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) } } }, + { "Square3x3", new[,] { { new Complex32(-1.1f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(1.1f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(6.6f, 1) } } }, + { "Square4x4", new[,] { { new Complex32(1.1f, 1), new Complex32(2.0f, 1), new Complex32(-3.0f, 1), new Complex32(4.4f, 1) }, { new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(-6.0f, 1), new Complex32(7.0f, 1) }, { new Complex32(-3.0f, 1), new Complex32(-6.0f, 1), new Complex32(8.0f, 1), new Complex32(9.0f, 1) }, { new Complex32(4.4f, 1), new Complex32(7.0f, 1), new Complex32(9.0f, 1), new Complex32(10.0f, 1) } } }, + { "Singular4x4", new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(0.0f, 1), new Complex32(4.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(5.0f, 1), new Complex32(0.0f, 1), new Complex32(7.0f, 1) }, { new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(4.0f, 1), new Complex32(7.0f, 1), new Complex32(0.0f, 1), new Complex32(10.0f, 1) } } }, + { "Tall3x2", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1) }, { new Complex32(0.0f, 1), new Complex32(1.1f, 1) }, { new Complex32(-4.4f, 1), new Complex32(5.5f, 1) } } }, + { "Wide2x3", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1) }, { new Complex32(0.0f, 1), new Complex32(1.1f, 1), new Complex32(2.2f, 1) } } }, + { "Symmetric3x3", new[,] { { new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(3.0f, 1) }, { new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(0.0f, 1) }, { new Complex32(3.0f, 1), new Complex32(0.0f, 1), new Complex32(3.0f, 1) } } }, + { "NonSymmetric3x3", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1) }, { new Complex32(0.0f, 1), new Complex32(1.1f, 1), new Complex32(2.2f, 1) }, { new Complex32(-4.4f, 1), new Complex32(5.5f, 1), new Complex32(6.6f, 1) } } }, + { "NonSymmetric4x4", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) }, { new Complex32(0.0f, 1), new Complex32(1.1f, 1), new Complex32(2.2f, 1), new Complex32(3.3f, 1) }, { new Complex32(1.0f, 1), new Complex32(2.1f, 1), new Complex32(6.2f, 1), new Complex32(4.3f, 1) }, { new Complex32(-4.4f, 1), new Complex32(5.5f, 1), new Complex32(6.6f, 1), new Complex32(-7.7f, 1) } } }, + { "IndexTester4x4", new [,] { { new Complex32(0, 1), new Complex32(1, 1), new Complex32(3, 1), new Complex32(6, 1) }, { new Complex32(1, 1), new Complex32(2, 1), new Complex32(4, 1), new Complex32(7, 1) }, { new Complex32(3, 1), new Complex32(4, 1), new Complex32(5, 1), new Complex32(8, 1) }, { new Complex32(6, 1), new Complex32(7, 1), new Complex32(8, 1), new Complex32(9, 1) } } } + }; + + TestMatrices = new Dictionary(); + + foreach (var name in TestData2D.Keys) + { + TestMatrices.Add(name, CreateMatrix(TestData2D[name])); + } + } + + /// + /// Can add a non-symmetric matrix to this symmetric matrix. + /// + /// Matrix A name. + /// Matrix B name. + [Test, Sequential] + public void CanAddNonSymmetricMatrix([Values("Square3x3", "Square4x4")] string mtxA, [Values("NonSymmetric3x3", "NonSymmetric4x4")] string mtxB) + { + var matrixA = TestMatrices[mtxA]; + var matrixB = TestMatrices[mtxB]; + + var matrix = matrixA.Clone(); + matrix = matrix.Add(matrixB); + + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[i, j], matrixA[i, j] + matrixB[i, j]); + } + } + } + + /// + /// Can subtract a non-symmetric matrix from this symmetric matrix. + /// + /// Matrix A name. + /// Matrix B name. + [Test, Sequential] + public void CanSubtractNonSymmetricMatrix([Values("Square3x3", "Square4x4")] string mtxA, [Values("NonSymmetric3x3", "NonSymmetric4x4")] string mtxB) + { + var matrixA = TestMatrices[mtxA]; + var matrixB = TestMatrices[mtxB]; + + var matrix = matrixA.Clone(); + matrix = matrix.Subtract(matrixB); + for (var i = 0; i < matrix.RowCount; i++) + { + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[i, j], matrixA[i, j] - matrixB[i, j]); + } + } + } + + /// + /// Can compute Frobenius norm. + /// + public override void CanComputeFrobeniusNorm() + { + var matrix = TestMatrices["Square3x3"]; + var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); + + matrix = TestMatrices["Wide2x3"]; + denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); + + matrix = TestMatrices["Tall3x2"]; + denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + AssertHelpers.AlmostEqual(denseMatrix.FrobeniusNorm(), matrix.FrobeniusNorm(), 14); + } + + + + /// + /// Can compute Infinity norm. + /// + public override void CanComputeInfinityNorm() + { + var matrix = TestMatrices["Square3x3"]; + var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); + + matrix = TestMatrices["Wide2x3"]; + denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); + + matrix = TestMatrices["Tall3x2"]; + denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + AssertHelpers.AlmostEqual(denseMatrix.InfinityNorm(), matrix.InfinityNorm(), 14); + } + + + + /// + /// Can compute L1 norm. + /// + public override void CanComputeL1Norm() + { + var matrix = TestMatrices["Square3x3"]; + var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); + + matrix = TestMatrices["Wide2x3"]; + denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); + + matrix = TestMatrices["Tall3x2"]; + denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + AssertHelpers.AlmostEqual(denseMatrix.L1Norm(), matrix.L1Norm(), 14); + } + + + + /// + /// Can compute L2 norm. + /// + public override void CanComputeL2Norm() + { + var matrix = TestMatrices["Square3x3"]; + var denseMatrix = new DenseMatrix(TestData2D["Square3x3"]); + AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); + + matrix = TestMatrices["Wide2x3"]; + denseMatrix = new DenseMatrix(TestData2D["Wide2x3"]); + AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); + + matrix = TestMatrices["Tall3x2"]; + denseMatrix = new DenseMatrix(TestData2D["Tall3x2"]); + AssertHelpers.AlmostEqual(denseMatrix.L2Norm(), matrix.L2Norm(), 14); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.cs new file mode 100644 index 00000000..03b4877d --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SymmetricMatrixTests.cs @@ -0,0 +1,125 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// Copyright (c) 2009-2010 Math.NET +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 +{ + using MathNet.Numerics.LinearAlgebra.Complex32; + + using Numerics; + using NUnit.Framework; + + /// + /// Abstract class with the common set of matrix tests for symmetric matrices. + /// + public abstract partial class SymmetricMatrixTests : MatrixTests + { + /// + /// Can check if a matrix is symmetric. + /// + [Test] + public override void CanCheckIfMatrixIsSymmetric() + { + var matrix = TestMatrices["Square3x3"]; + Assert.IsTrue(matrix.IsSymmetric); + + matrix = TestMatrices["NonSymmetric3x3"]; + Assert.IsFalse(matrix.IsSymmetric); + } + + /// + /// Can check if a [,] array is symmetric. + /// + [Test] + public void CanCheckIfArrayIsSymmetric() + { + Assert.IsTrue(SymmetricMatrix.CheckIfSymmetric(TestData2D["Square3x3"])); + Assert.IsFalse(SymmetricMatrix.CheckIfSymmetric(TestData2D["NonSymmetric3x3"])); + } + + /// + /// Test whether the index enumerator returns the correct values. + /// + [Test] + public void CanUseIndexedEnumerator() + { + var matrix = TestMatrices["Singular3x3"]; + var enumerator = matrix.IndexedEnumerator().GetEnumerator(); + enumerator.MoveNext(); + var item = enumerator.Current; + Assert.AreEqual(0, item.Item1); + Assert.AreEqual(0, item.Item2); + Assert.AreEqual(new Complex32(1.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(0, item.Item1); + Assert.AreEqual(1, item.Item2); + Assert.AreEqual(new Complex32(2.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(0, item.Item1); + Assert.AreEqual(2, item.Item2); + Assert.AreEqual(new Complex32(3.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(1, item.Item1); + Assert.AreEqual(0, item.Item2); + Assert.AreEqual(new Complex32(2.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(1, item.Item1); + Assert.AreEqual(1, item.Item2); + Assert.AreEqual(new Complex32(0.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(1, item.Item1); + Assert.AreEqual(2, item.Item2); + Assert.AreEqual(new Complex32(0.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(2, item.Item1); + Assert.AreEqual(0, item.Item2); + Assert.AreEqual(new Complex32(3.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(2, item.Item1); + Assert.AreEqual(1, item.Item2); + Assert.AreEqual(new Complex32(0.0f, 1), item.Item3); + + enumerator.MoveNext(); + item = enumerator.Current; + Assert.AreEqual(2, item.Item1); + Assert.AreEqual(2, item.Item2); + Assert.AreEqual(new Complex32(0.0f, 1), item.Item3); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 7ce13c00..7baa4f4b 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -272,6 +272,9 @@ Code + + + Code