Browse Source

added force symmetric helper methods so we can get the EVD solve tests to pass

pull/65/head
Marcus Cuda 14 years ago
parent
commit
fb7b2dcd82
  1. 3
      src/NativeWrappers/Windows/MKLWrapperTests/MKLWrapperTests.csproj
  2. 14
      src/UnitTests/LinearAlgebraTests/Complex/Factorization/EvdTests.cs
  3. 13
      src/UnitTests/LinearAlgebraTests/Complex32/Factorization/EvdTests.cs
  4. 17
      src/UnitTests/LinearAlgebraTests/Double/Factorization/EvdTests.cs
  5. 18
      src/UnitTests/LinearAlgebraTests/Single/Factorization/EvdTests.cs
  6. 97
      src/UnitTests/MatrixHelpers.cs
  7. 1
      src/UnitTests/UnitTests.csproj

3
src/NativeWrappers/Windows/MKLWrapperTests/MKLWrapperTests.csproj

@ -497,6 +497,9 @@
<Compile Include="..\..\..\UnitTests\LinearAlgebraTests\VectorArithmeticTheory.cs">
<Link>LinearAlgebraTests\VectorArithmeticTheory.cs</Link>
</Compile>
<Compile Include="..\..\..\UnitTests\MatrixHelpers.cs">
<Link>MatrixHelpers.cs</Link>
</Compile>
<Compile Include="..\..\..\UnitTests\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>

14
src/UnitTests/LinearAlgebraTests/Complex/Factorization/EvdTests.cs

@ -122,8 +122,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
public void CanFactorizeRandomSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var factorEvd = matrixA.Evd();
var eigenValues = factorEvd.EigenValues();
var eigenVectors = factorEvd.EigenVectors();
var d = factorEvd.D();
@ -207,13 +207,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -247,13 +248,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -294,13 +296,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
@ -339,13 +342,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();

13
src/UnitTests/LinearAlgebraTests/Complex32/Factorization/EvdTests.cs

@ -119,6 +119,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var factorEvd = matrixA.Evd();
var eigenVectors = factorEvd.EigenVectors();
var d = factorEvd.D();
@ -203,13 +204,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
[Test, Ignore]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -244,13 +246,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -292,13 +295,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
[Test, Ignore]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
@ -338,13 +342,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteHermitianDenseMatrix(order);
MatrixHelpers.ForceConjugateSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();

17
src/UnitTests/LinearAlgebraTests/Double/Factorization/EvdTests.cs

@ -24,13 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.LinearAlgebra.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
{
using System;
using System.Numerics;
using LinearAlgebra.Double;
using LinearAlgebra.Double.Factorization;
using LinearAlgebra.Generic.Factorization;
using NUnit.Framework;
/// <summary>
@ -123,6 +124,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
public void CanFactorizeRandomSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var factorEvd = matrixA.Evd();
var eigenVectors = factorEvd.EigenVectors();
var d = factorEvd.D();
@ -207,13 +209,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -240,6 +243,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
}
}
//private
/// <summary>
/// Can solve a system of linear equations for a random matrix and symmetric matrix (AX=B).
/// </summary>
@ -247,13 +251,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -295,13 +300,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
@ -340,13 +346,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();

18
src/UnitTests/LinearAlgebraTests/Single/Factorization/EvdTests.cs

@ -28,7 +28,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
{
using System;
using System.Numerics;
using LinearAlgebra.Generic.Factorization;
using LinearAlgebra.Single;
using LinearAlgebra.Single.Factorization;
using NUnit.Framework;
@ -113,6 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
public void CanFactorizeRandomSymmetricMatrix([Values(1, 2, 5, 10, 50, 100)] int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var factorEvd = matrixA.Evd();
var eigenVectors = factorEvd.EigenVectors();
var d = factorEvd.D();
@ -197,13 +197,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -237,13 +238,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrix(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -263,7 +265,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1e-2);
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1e-1);
}
}
@ -284,13 +286,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
@ -329,13 +332,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
[Test]
[TestCase(1)]
[TestCase(2)]
[TestCase(5, Ignore = true, IgnoreReason = "Problem with native providers determining if the matrix is symmetric.")]
[TestCase(5)]
[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void CanSolveForRandomMatrixAndSymmetricMatrixWhenResultMatrixGiven(int order)
{
var matrixA = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
MatrixHelpers.ForceSymmetric(matrixA);
var matrixACopy = matrixA.Clone();
var factorEvd = matrixA.Evd();
@ -358,7 +362,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1e-2);
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1e-1);
}
}

97
src/UnitTests/MatrixHelpers.cs

@ -0,0 +1,97 @@
// <copyright file="AssertHelpers.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using System.Numerics;
using MathNet.Numerics.LinearAlgebra.Generic;
namespace MathNet.Numerics.UnitTests
{
/// <summary>
/// Matrix utility functions to simplify tests.
/// </summary>
static public class MatrixHelpers
{
/// <summary>
/// Forces a matrix elements to symmetric. Copies the lower triangle to the upper triangle.
/// </summary>
/// <typeparam name="T">The matrix type.</typeparam>
/// <param name="matrix">The matrix to make symmetric.</param>
static public void ForceSymmetric<T>(Matrix<T> matrix) where T : struct, IEquatable<T>, IFormattable
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException("matrix must be square.", "matrix");
}
for (var row = 0; row < matrix.RowCount; row++)
{
for (var column = 0; column < row; column++)
{
matrix.At(column, row, matrix.At(row, column));
}
}
}
/// <summary>
/// Forces a matrix elements to conjugate symmetric. Copies the conjugate of the values
/// from the lower triangle to the upper triangle.
/// </summary>
/// <param name="matrix">The matrix to make conjugate symmetric.</param>
static public void ForceConjugateSymmetric(Matrix<Complex> matrix)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException("matrix must be square.", "matrix");
}
for (var row = 0; row < matrix.RowCount; row++)
{
for (var column = 0; column < row; column++)
{
matrix.At(column, row, matrix.At(row, column).Conjugate());
}
}
}
/// <summary>
/// Forces a matrix elements to conjugate symmetric. Copies the conjugate of the values
/// from the lower triangle to the upper triangle.
/// </summary>
/// <param name="matrix">The matrix to make conjugate symmetric.</param>
public static void ForceConjugateSymmetric(Matrix<Complex32> matrix)
{
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException("matrix must be square.", "matrix");
}
for (var row = 0; row < matrix.RowCount; row++)
{
for (var column = 0; column < row; column++)
{
matrix.At(column, row, matrix.At(row, column).Conjugate());
}
}
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -742,6 +742,7 @@
</Compile>
<Compile Include="LinearAlgebraTests\Double\VectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\VectorArithmeticTheory.cs" />
<Compile Include="MatrixHelpers.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTest.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTestBigInteger.cs" />
<Compile Include="NumberTheoryTests\IntegerTheoryTest.cs" />

Loading…
Cancel
Save