// // 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.Complex { using System.Numerics; using NUnit.Framework; /// /// Abstract class with the common set of matrix tests /// public abstract partial class MatrixTests : MatrixLoader { /// /// Can transpose a matrix. /// /// Matrix name. [TestCase("Singular3x3")] [TestCase("Square3x3")] [TestCase("Square4x4")] [TestCase("Tall3x2")] [TestCase("Wide2x3")] public void CanTransposeMatrix(string name) { var matrix = CreateMatrix(TestData2D[name]); var transpose = matrix.Transpose(); Assert.AreNotSame(matrix, transpose); Assert.AreEqual(matrix.RowCount, transpose.ColumnCount); Assert.AreEqual(matrix.ColumnCount, transpose.RowCount); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], transpose[j, i]); } } } /// /// Can conjugate transpose a matrix. /// /// Matrix name. [TestCase("Singular3x3")] [TestCase("Square3x3")] [TestCase("Square4x4")] [TestCase("Tall3x2")] [TestCase("Wide2x3")] public void CanConjugateTransposeMatrix(string name) { var matrix = CreateMatrix(TestData2D[name]); var transpose = matrix.ConjugateTranspose(); Assert.AreNotSame(matrix, transpose); Assert.AreEqual(matrix.RowCount, transpose.ColumnCount); Assert.AreEqual(matrix.ColumnCount, transpose.RowCount); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], transpose[j, i].Conjugate()); } } } /// /// Can compute Frobenius norm. /// [Test] public virtual void CanComputeFrobeniusNorm() { var matrix = TestMatrices["Square3x3"]; AssertHelpers.AlmostEqual(11.1427106217473, matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Wide2x3"]; AssertHelpers.AlmostEqual(5.29055762656452, matrix.FrobeniusNorm(), 14); matrix = TestMatrices["Tall3x2"]; AssertHelpers.AlmostEqual(7.86574853399217, matrix.FrobeniusNorm(), 14); } /// /// Can compute Infinity norm. /// [Test] public virtual void CanComputeInfinityNorm() { var matrix = TestMatrices["Square3x3"]; AssertHelpers.AlmostEqual(16.7777033201323, matrix.InfinityNorm(), 14); matrix = TestMatrices["Wide2x3"]; AssertHelpers.AlmostEqual(7.3514039993641, matrix.InfinityNorm(), 14); matrix = TestMatrices["Tall3x2"]; AssertHelpers.AlmostEqual(10.1023756128209, matrix.InfinityNorm(), 14); } /// /// Can compute L1 norm. /// [Test] public virtual void CanComputeL1Norm() { var matrix = TestMatrices["Square3x3"]; AssertHelpers.AlmostEqual(12.5401248319437, matrix.L1Norm(), 14); matrix = TestMatrices["Wide2x3"]; AssertHelpers.AlmostEqual(5.86479712463225, matrix.L1Norm(), 14); matrix = TestMatrices["Tall3x2"]; AssertHelpers.AlmostEqual(9.49338601320024, matrix.L1Norm(), 14); } /// /// Can compute L2 norm. /// [Test] public virtual void CanComputeL2Norm() { var matrix = TestMatrices["Square3x3"]; AssertHelpers.AlmostEqual(10.638175225153, matrix.L2Norm(), 14); matrix = TestMatrices["Wide2x3"]; AssertHelpers.AlmostEqual(5.2058554445283, matrix.L2Norm(), 14); matrix = TestMatrices["Tall3x2"]; AssertHelpers.AlmostEqual(7.35826643761172, matrix.L2Norm(), 14); } /// /// Can check if a matrix is symmetric. /// [Test] public virtual void CanCheckIfMatrixIsSymmetric() { var matrix = TestMatrices["Symmetric3x3"]; Assert.IsTrue(matrix.IsSymmetric); matrix = TestMatrices["Square3x3"]; Assert.IsFalse(matrix.IsSymmetric); } } }