Browse Source

Added Permutation class.

Added Matrix.PermuteRows and Matrix.PermuteColumns.
Further implementation of LU decomposition.
la-knuth
Jurgen Van Gael 17 years ago
committed by Christoph Ruegg
parent
commit
6242f534fd
  1. 29
      src/Numerics/LinearAlgebra/Double/Factorization/LU.cs
  2. 58
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  3. 1
      src/Numerics/Numerics.csproj
  4. 9
      src/Numerics/Properties/Resources.Designer.cs
  5. 3
      src/Numerics/Properties/Resources.resx
  6. 3
      src/Silverlight/Silverlight.csproj
  7. 3
      src/UnitTests/LinearAlgebraTests/Double/Factorization/LUTests.cs
  8. 50
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs
  9. 2
      src/UnitTests/TrigonometryTest.cs
  10. 1
      src/UnitTests/UnitTests.csproj

29
src/Numerics/LinearAlgebra/Double/Factorization/LU.cs

@ -95,6 +95,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
get { return mFactors.GetUpperTriangle(); }
}
/// <summary>
/// Return the permutation applied to LU factorization.
/// </summary>
public virtual Permutation P
{
get { return Permutation.FromInversions(mPivots); }
}
/// <summary>
/// The determinant of the matrix for which the LU factorization was computed.
/// </summary>
@ -167,26 +175,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="input">The right hand side vector, <b>b</b>.</param>
/// <param name="result">The left hand side <see cref="Matrix"/>, <b>x</b>.</param>
public abstract void Solve(Vector input, Vector result);
/// <summary>
/// Pivot a matrix according to this LU decomposition.
/// </summary>
/// <param name="data">The matrix to pivot.</param>
public void Pivot(Matrix data)
{
for (int i = 0; i < mPivots.Length; i++)
{
if (mPivots[i] != i)
{
int p = mPivots[i];
for (int j = 0; j < data.ColumnCount; j++)
{
double temp = data.At(p, j);
data.At(p, j, data.At(i, j));
data.At(i, j, temp);
}
}
}
}
}
}

58
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -760,5 +760,63 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ret;
}
/// <summary>
/// Permute the rows of a matrix according to a permutation.
/// </summary>
/// <param name="p">The row permutation to apply to this matrix.</param>
public virtual void PermuteRows(Permutation p)
{
if (p.Dimension != this.RowCount)
{
throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
}
// Get a sequence of inversions from the permutation.
int[] inv = p.ToInversions();
for (int i = 0; i < p.Dimension; i++)
{
if (inv[i] != i)
{
int q = inv[i];
for (int j = 0; j < this.ColumnCount; j++)
{
double temp = At(q, j);
At(q, j, At(i, j));
At(i, j, temp);
}
}
}
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>
/// <param name="p">The column permutation to apply to this matrix.</param>
public virtual void PermuteColumns(Permutation p)
{
if (p.Dimension != this.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
}
// Get a sequence of inversions from the permutation.
int[] inv = p.ToInversions();
for (int i = 0; i < p.Dimension; i++)
{
if (inv[i] != i)
{
int q = inv[i];
for (int j = 0; j < this.RowCount; j++)
{
double temp = At(j, q);
At(j, q, At(j, i));
At(j, i, temp);
}
}
}
}
}
}

1
src/Numerics/Numerics.csproj

@ -101,6 +101,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Control.cs" />
<Compile Include="Complex32.cs" />
<Compile Include="Permutation.cs" />
<Compile Include="Distributions\Continuous\Beta.cs" />
<Compile Include="Distributions\Continuous\ContinuousUniform.cs" />
<Compile Include="Distributions\Continuous\LogNormal.cs" />

9
src/Numerics/Properties/Resources.Designer.cs

@ -573,6 +573,15 @@ namespace MathNet.Numerics.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to The integer array does not represent a valid permutation..
/// </summary>
internal static string PermutationAsIntArrayInvalid {
get {
return ResourceManager.GetString("PermutationAsIntArrayInvalid", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The sampler&apos;s proposal distribution is not upper bounding the target density..
/// </summary>

3
src/Numerics/Properties/Resources.resx

@ -300,4 +300,7 @@
<data name="ArgumentReferenceDifferent" xml:space="preserve">
<value>Arguments must be different objects.</value>
</data>
<data name="PermutationAsIntArrayInvalid" xml:space="preserve">
<value>The integer array does not represent a valid permutation.</value>
</data>
</root>

3
src/Silverlight/Silverlight.csproj

@ -245,6 +245,9 @@
<Compile Include="..\Numerics\NumberTheory\IntegerTheory.Euclid.cs">
<Link>NumberTheory\IntegerTheory.Euclid.cs</Link>
</Compile>
<Compile Include="..\Numerics\Permutation.cs">
<Link>Permutation.cs</Link>
</Compile>
<Compile Include="..\Numerics\Precision.cs">
<Link>Precision.cs</Link>
</Compile>

3
src/UnitTests/LinearAlgebraTests/Double/Factorization/LUTests.cs

@ -148,7 +148,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
// Make sure the cholesky factor times it's transpose is the original matrix.
var XfromLU = L * U;
lu.Pivot(XfromLU);
var Pinv = lu.P.Inverse();
XfromLU.PermuteRows(Pinv);
for (int i = 0; i < XfromLU.RowCount; i++)
{
for (int j = 0; j < XfromLU.ColumnCount; j++)

50
src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs

@ -481,5 +481,55 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
}
[Test]
[Row("Singular3x3")]
[Row("Square3x3")]
[Row("Tall3x2")]
[MultipleAsserts]
public void CanPermuteMatrixRows(string name)
{
var matrix = CreateMatrix(testData2D[name]);
var matrixp = CreateMatrix(testData2D[name]);
var permutation = new Permutation(new int[] { 2, 0, 1 });
matrixp.PermuteRows(permutation);
Assert.AreNotSame(matrix, matrixp);
Assert.AreEqual(matrix.RowCount, matrixp.RowCount);
Assert.AreEqual(matrix.ColumnCount, matrixp.ColumnCount);
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
Assert.AreEqual(matrix[i, j], matrixp[permutation[i], j]);
}
}
}
[Test]
[Row("Singular3x3")]
[Row("Square3x3")]
[Row("Wide2x3")]
[MultipleAsserts]
public void CanPermuteMatrixColumns(string name)
{
var matrix = CreateMatrix(testData2D[name]);
var matrixp = CreateMatrix(testData2D[name]);
var permutation = new Permutation(new int[] { 2, 0, 1 });
matrixp.PermuteColumns(permutation);
Assert.AreNotSame(matrix, matrixp);
Assert.AreEqual(matrix.RowCount, matrixp.RowCount);
Assert.AreEqual(matrix.ColumnCount, matrixp.ColumnCount);
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
Assert.AreEqual(matrix[i, j], matrixp[i, permutation[j]]);
}
}
}
}
}

2
src/UnitTests/TrigonometryTest.cs

@ -1,4 +1,4 @@
// <copyright file="TrigonometryTests.cs" company="Math.NET">
// <copyright file="TrigonometryTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics

1
src/UnitTests/UnitTests.csproj

@ -90,6 +90,7 @@
<Compile Include="ComplexTests\ComplexTest.cs" />
<Compile Include="ComplexTests\Complex32Test.TextHandling.cs" />
<Compile Include="ComplexTests\Complex32Test.cs" />
<Compile Include="PermutationTest.cs" />
<Compile Include="DistributionTests\CommonDistributionTests.cs" />
<Compile Include="DistributionTests\Continuous\BetaTests.cs" />
<Compile Include="DistributionTests\Continuous\ContinuousUniformTests.cs" />

Loading…
Cancel
Save