Browse Source

LA: simplify IPreConditioner, misc iterative solver cleanup

optimization-1
Christoph Ruegg 13 years ago
parent
commit
db046f8e99
  1. 1
      src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs
  2. 1
      src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs
  3. 1
      src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs
  4. 1
      src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs
  5. 1
      src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs
  6. 1
      src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs
  7. 51
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Diagonal.cs
  8. 132
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs
  9. 54
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs
  10. 1
      src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs
  11. 50
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Diagonal.cs
  12. 131
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs
  13. 53
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs
  14. 1
      src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs
  15. 50
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Diagonal.cs
  16. 131
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs
  17. 53
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs
  18. 1
      src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs
  19. 50
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Diagonal.cs
  20. 131
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs
  21. 53
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs
  22. 2
      src/Numerics/LinearAlgebra/Solvers/ICalculationStatus.cs
  23. 1
      src/Numerics/LinearAlgebra/Solvers/IIterationStopCriterium.cs
  24. 3
      src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs
  25. 4
      src/Numerics/LinearAlgebra/Solvers/IIterativeSolverSetup.cs
  26. 26
      src/Numerics/LinearAlgebra/Solvers/IIterator.cs
  27. 7
      src/Numerics/LinearAlgebra/Solvers/IPreConditioner.cs
  28. 29
      src/Numerics/LinearAlgebra/Solvers/IterationCountStopCriterium.cs
  29. 76
      src/Numerics/LinearAlgebra/Solvers/Iterator.cs
  30. 48
      src/Numerics/LinearAlgebra/Solvers/UnitPreconditioner.cs
  31. 2
      src/Numerics/Numerics.csproj
  32. 1
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs
  33. 1
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs
  34. 1
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs
  35. 1
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs
  36. 56
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs
  37. 56
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Preconditioners/PreConditionerTest.cs
  38. 2
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs
  39. 1
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs
  40. 1
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs
  41. 1
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs
  42. 1
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs
  43. 56
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs
  44. 56
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Preconditioners/PreConditionerTest.cs
  45. 2
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs
  46. 1
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs
  47. 1
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs
  48. 1
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs
  49. 1
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs
  50. 56
      src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs
  51. 56
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Preconditioners/PreConditionerTest.cs
  52. 2
      src/UnitTests/LinearAlgebraTests/Double/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs
  53. 1
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs
  54. 1
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs
  55. 1
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs
  56. 1
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs
  57. 56
      src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs
  58. 56
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Preconditioners/PreConditionerTest.cs
  59. 2
      src/UnitTests/LinearAlgebraTests/Single/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs

1
src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace Examples.LinearAlgebra.IterativeSolversExamples
{

1
src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs

@ -35,7 +35,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace Examples.LinearAlgebra.IterativeSolversExamples
{

1
src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace Examples.LinearAlgebra.IterativeSolversExamples
{

1
src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace Examples.LinearAlgebra.IterativeSolversExamples
{

1
src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace Examples.LinearAlgebra.IterativeSolversExamples
{

1
src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs

@ -30,7 +30,6 @@
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
{

51
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Diagonal.cs

@ -39,6 +39,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
using Complex = Numerics.Complex;
#else
using Complex = System.Numerics.Complex;
#endif
/// <summary>
@ -50,7 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <summary>
/// The inverse of the matrix diagonal.
/// </summary>
private Complex[] _inverseDiagonals;
Complex[] _inverseDiagonals;
/// <summary>
/// Returns the decomposed matrix diagonal.
@ -61,7 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var result = new DiagonalMatrix(_inverseDiagonals.Length);
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
result.At(i, i, 1 / _inverseDiagonals[i]);
result.At(i, i, 1/_inverseDiagonals[i]);
}
return result;
@ -76,11 +77,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<Complex> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
@ -89,35 +85,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
_inverseDiagonals = new Complex[matrix.RowCount];
for (var i = 0; i < matrix.RowCount; i++)
{
_inverseDiagonals[i] = 1 / matrix.At(i, i);
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex> Approximate(Vector<Complex> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _inverseDiagonals.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
_inverseDiagonals[i] = 1/matrix.At(i, i);
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
@ -127,16 +96,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex> rhs, Vector<Complex> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -149,7 +108,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
lhs[i] = rhs[i] * _inverseDiagonals[i];
lhs[i] = rhs[i]*_inverseDiagonals[i];
}
}
}

132
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs

@ -40,6 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
using Complex = Numerics.Complex;
#else
using Complex = System.Numerics.Complex;
#endif
/// <summary>
@ -70,36 +71,36 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// The default drop tolerance.
/// </summary>
public const double DefaultDropTolerance = 0.0001;
/// <summary>
/// The decomposed upper triangular matrix.
/// </summary>
private SparseMatrix _upper;
SparseMatrix _upper;
/// <summary>
/// The decomposed lower triangular matrix.
/// </summary>
private SparseMatrix _lower;
SparseMatrix _lower;
/// <summary>
/// The array containing the pivot values.
/// </summary>
private int[] _pivots;
int[] _pivots;
/// <summary>
/// The fill level.
/// </summary>
private double _fillLevel = DefaultFillLevel;
double _fillLevel = DefaultFillLevel;
/// <summary>
/// The drop tolerance.
/// </summary>
private double _dropTolerance = DefaultDropTolerance;
double _dropTolerance = DefaultDropTolerance;
/// <summary>
/// The pivot tolerance.
/// </summary>
private double _pivotTolerance;
double _pivotTolerance;
/// <summary>
/// Initializes a new instance of the <see cref="Ilutp"/> class with the default settings.
@ -145,7 +146,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
_dropTolerance = dropTolerance;
_pivotTolerance = pivotTolerance;
}
/// <summary>
/// Gets or sets the amount of fill that is allowed in the matrix. The
/// value is a fraction of the number of non-zero entries in the original
@ -168,11 +169,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get
{
return _fillLevel;
}
get { return _fillLevel; }
set
{
if (value < 0)
@ -203,11 +200,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get
{
return _dropTolerance;
}
get { return _dropTolerance; }
set
{
if (value < 0)
@ -240,11 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get
{
return _pivotTolerance;
}
get { return _pivotTolerance; }
set
{
if (value < 0)
@ -255,7 +244,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
_pivotTolerance = value;
}
}
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
/// </summary>
@ -373,7 +362,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
// Create the upper triangular matrix and copy the values
_upper = new SparseMatrix(sparseMatrix.RowCount);
// Create the pivot array
_pivots = new int[sparseMatrix.RowCount];
for (var i = 0; i < _pivots.Length; i++)
@ -384,20 +373,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var workVector = new DenseVector(sparseMatrix.RowCount);
var rowVector = new DenseVector(sparseMatrix.ColumnCount);
var indexSorting = new int[sparseMatrix.RowCount];
// spaceLeft = lfilNnz * nnz(A)
var spaceLeft = (int)_fillLevel * sparseMatrix.NonZerosCount;
var spaceLeft = (int) _fillLevel*sparseMatrix.NonZerosCount;
// for i = 1, .. , n
for (var i = 0; i < sparseMatrix.RowCount; i++)
{
// w = a(i,*)
sparseMatrix.Row(i, workVector);
// pivot the row
PivotRow(workVector);
var vectorNorm = workVector.InfinityNorm();
// for j = 1, .. , i - 1)
for (var j = 0; j < i; j++)
{
@ -415,7 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
if (workVector[j] != 0.0)
{
// Calculate the multiplication factors that go into the L matrix
workVector[j] = workVector[j] / _upper[j, j];
workVector[j] = workVector[j]/_upper[j, j];
if (workVector[j].Magnitude < _dropTolerance)
{
workVector[j] = 0.0;
@ -447,19 +436,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
// {
// w(j) = 0
// }
if (workVector[j].Magnitude <= _dropTolerance * vectorNorm.Real)
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm.Real)
{
workVector[j] = 0.0;
}
}
// spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
var spaceRow = spaceLeft / (sparseMatrix.RowCount - i + 1);
var spaceRow = spaceLeft/(sparseMatrix.RowCount - i + 1);
// lfil = spaceRow / 2 // space for this row of L
var fillLevel = spaceRow / 2;
var fillLevel = spaceRow/2;
FindLargestItems(0, i - 1, indexSorting, workVector);
// l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
var lowerNonZeroCount = 0;
var count = 0;
@ -476,10 +465,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
}
FindLargestItems(i + 1, sparseMatrix.RowCount - 1, indexSorting, workVector);
// lfil = spaceRow - nnz(L(i,:)) // space for this row of U
fillLevel = spaceRow - lowerNonZeroCount;
// u(i,j) = w(j) for j = i + 1, .. , n // only the largest lfil - 1 elements
var upperNonZeroCount = 0;
count = 0;
@ -511,12 +500,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
// -1 values.
if ((i + 1) < (sparseMatrix.RowCount - 1))
{
if (workVector[i].Magnitude < _pivotTolerance * workVector[indexSorting[0]].Magnitude)
if (workVector[i].Magnitude < _pivotTolerance*workVector[indexSorting[0]].Magnitude)
{
// swap columns of u (which holds the values of A in the
// sections that haven't been partitioned yet.
SwapColumns(_upper, i, indexSorting[0]);
// Update P
var temp = _pivots[i];
_pivots[i] = _pivots[indexSorting[0]];
@ -538,10 +527,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// Pivot elements in the <paramref name="row"/> according to internal pivot array
/// </summary>
/// <param name="row">Row <see cref="Vector"/> to pivot in</param>
private void PivotRow(Vector<Complex> row)
void PivotRow(Vector<Complex> row)
{
var knownPivots = new Dictionary<int, int>();
// pivot the row
for (var i = 0; i < row.Count; i++)
{
@ -563,7 +552,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="knownPivots">Pivots already done</param>
/// <param name="currentItem">Current item to pivot</param>
/// <returns><c>true</c> if performed, otherwise <c>false</c></returns>
private bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
{
if (knownPivots.ContainsKey(_pivots[currentItem]))
{
@ -590,7 +579,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="matrix">Source <see cref="Matrix"/>.</param>
/// <param name="firstColumn">First column index to swap</param>
/// <param name="secondColumn">Second column index to swap</param>
private static void SwapColumns(Matrix<Complex> matrix, int firstColumn, int secondColumn)
static void SwapColumns(Matrix<Complex> matrix, int firstColumn, int secondColumn)
{
for (var i = 0; i < matrix.RowCount; i++)
{
@ -607,7 +596,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="upperBound">Sort till upper bound</param>
/// <param name="sortedIndices">Array with sorted vector indicies</param>
/// <param name="values">Source <see cref="Vector"/></param>
private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<Complex> values)
static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<Complex> values)
{
// Copy the indices for the values into the array
for (var i = 0; i < upperBound + 1 - lowerBound; i++)
@ -627,33 +616,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
IlutpElementSorter.SortDoubleIndicesDecreasing(0, upperBound - lowerBound, sortedIndices, values);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex> Approximate(Vector<Complex> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _upper.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -661,16 +623,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex> rhs, Vector<Complex> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -680,7 +632,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
// Solve equation here
// Pivot(vector, result);
// Solve L*Y = B(piv,:)
@ -692,7 +644,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var sum = Complex.Zero;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -706,16 +658,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var sum = Complex.Zero;
for (var j = _upper.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
// We have a column pivot so we only need to pivot the
// end result not the incoming right hand side vector
var temp = lhs.Clone();
Pivot(temp, lhs);
}
@ -724,7 +676,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// </summary>
/// <param name="vector">Source <see cref="Vector"/>.</param>
/// <param name="result">Result <see cref="Vector"/> after pivoting.</param>
private void Pivot(Vector<Complex> vector, Vector<Complex> result)
void Pivot(Vector<Complex> vector, Vector<Complex> result)
{
for (var i = 0; i < _pivots.Length; i++)
{

54
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs

@ -39,6 +39,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
using Complex = Numerics.Complex;
#else
using Complex = System.Numerics.Complex;
#endif
/// <summary>
@ -56,7 +57,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// The matrix holding the lower (L) and upper (U) matrices. The
/// decomposition matrices are combined to reduce storage.
/// </summary>
private SparseMatrix _decompositionLU;
SparseMatrix _decompositionLU;
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
@ -91,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
{
result[i, j] = 1.0;
}
else
else
{
result[i, j] = _decompositionLU[i, j];
}
@ -140,11 +141,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
{
if (_decompositionLU[i, k] != 0.0)
{
var t = _decompositionLU[i, k] / _decompositionLU[k, k];
var t = _decompositionLU[i, k]/_decompositionLU[k, k];
_decompositionLU[i, k] = t;
if (_decompositionLU[k, i] != 0.0)
{
_decompositionLU[i, i] = _decompositionLU[i, i] - (t * _decompositionLU[k, i]);
_decompositionLU[i, i] = _decompositionLU[i, i] - (t*_decompositionLU[k, i]);
}
for (var j = k + 1; j < _decompositionLU.RowCount; j++)
@ -156,7 +157,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
if (_decompositionLU[i, j] != 0.0)
{
_decompositionLU[i, j] = _decompositionLU[i, j] - (t * _decompositionLU[k, j]);
_decompositionLU[i, j] = _decompositionLU[i, j] - (t*_decompositionLU[k, j]);
}
}
}
@ -164,33 +165,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex> Approximate(Vector<Complex> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _decompositionLU.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -198,16 +172,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex> rhs, Vector<Complex> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -236,7 +200,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var sum = Complex.Zero;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -256,10 +220,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
var sum = Complex.Zero;
for (var j = _decompositionLU.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
}
}

1
src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs

@ -30,7 +30,6 @@
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
{

50
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Diagonal.cs

@ -45,7 +45,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <summary>
/// The inverse of the matrix diagonal.
/// </summary>
private Complex32[] _inverseDiagonals;
Complex32[] _inverseDiagonals;
/// <summary>
/// Returns the decomposed matrix diagonal.
@ -56,7 +56,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var result = new DiagonalMatrix(_inverseDiagonals.Length);
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
result[i, i] = 1 / _inverseDiagonals[i];
result[i, i] = 1/_inverseDiagonals[i];
}
return result;
@ -71,11 +71,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<Complex32> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
@ -84,37 +79,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
_inverseDiagonals = new Complex32[matrix.RowCount];
for (var i = 0; i < matrix.RowCount; i++)
{
_inverseDiagonals[i] = 1 / matrix[i, i];
_inverseDiagonals[i] = 1/matrix[i, i];
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex32> Approximate(Vector<Complex32> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _inverseDiagonals.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -122,16 +90,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex32> rhs, Vector<Complex32> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -144,7 +102,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
lhs[i] = rhs[i] * _inverseDiagonals[i];
lhs[i] = rhs[i]*_inverseDiagonals[i];
}
}
}

131
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs

@ -65,36 +65,36 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// The default drop tolerance.
/// </summary>
public const double DefaultDropTolerance = 0.0001;
/// <summary>
/// The decomposed upper triangular matrix.
/// </summary>
private SparseMatrix _upper;
SparseMatrix _upper;
/// <summary>
/// The decomposed lower triangular matrix.
/// </summary>
private SparseMatrix _lower;
SparseMatrix _lower;
/// <summary>
/// The array containing the pivot values.
/// </summary>
private int[] _pivots;
int[] _pivots;
/// <summary>
/// The fill level.
/// </summary>
private double _fillLevel = DefaultFillLevel;
double _fillLevel = DefaultFillLevel;
/// <summary>
/// The drop tolerance.
/// </summary>
private double _dropTolerance = DefaultDropTolerance;
double _dropTolerance = DefaultDropTolerance;
/// <summary>
/// The pivot tolerance.
/// </summary>
private double _pivotTolerance;
double _pivotTolerance;
/// <summary>
/// Initializes a new instance of the <see cref="Ilutp"/> class with the default settings.
@ -140,7 +140,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
_dropTolerance = dropTolerance;
_pivotTolerance = pivotTolerance;
}
/// <summary>
/// Gets or sets the amount of fill that is allowed in the matrix. The
/// value is a fraction of the number of non-zero entries in the original
@ -163,11 +163,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get
{
return _fillLevel;
}
get { return _fillLevel; }
set
{
if (value < 0)
@ -198,11 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get
{
return _dropTolerance;
}
get { return _dropTolerance; }
set
{
if (value < 0)
@ -235,11 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get
{
return _pivotTolerance;
}
get { return _pivotTolerance; }
set
{
if (value < 0)
@ -250,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
_pivotTolerance = value;
}
}
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
/// </summary>
@ -368,7 +356,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
// Create the upper triangular matrix and copy the values
_upper = new SparseMatrix(sparseMatrix.RowCount);
// Create the pivot array
_pivots = new int[sparseMatrix.RowCount];
for (var i = 0; i < _pivots.Length; i++)
@ -379,20 +367,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var workVector = new DenseVector(sparseMatrix.RowCount);
var rowVector = new DenseVector(sparseMatrix.ColumnCount);
var indexSorting = new int[sparseMatrix.RowCount];
// spaceLeft = lfilNnz * nnz(A)
var spaceLeft = (int)_fillLevel * sparseMatrix.NonZerosCount;
var spaceLeft = (int) _fillLevel*sparseMatrix.NonZerosCount;
// for i = 1, .. , n
for (var i = 0; i < sparseMatrix.RowCount; i++)
{
// w = a(i,*)
sparseMatrix.Row(i, workVector);
// pivot the row
PivotRow(workVector);
var vectorNorm = workVector.InfinityNorm();
// for j = 1, .. , i - 1)
for (var j = 0; j < i; j++)
{
@ -410,7 +398,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
if (workVector[j] != 0.0f)
{
// Calculate the multiplication factors that go into the L matrix
workVector[j] = workVector[j] / _upper[j, j];
workVector[j] = workVector[j]/_upper[j, j];
if (workVector[j].Magnitude < _dropTolerance)
{
workVector[j] = 0.0f;
@ -442,19 +430,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
// {
// w(j) = 0
// }
if (workVector[j].Magnitude <= _dropTolerance * vectorNorm.Real)
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm.Real)
{
workVector[j] = 0.0f;
}
}
// spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
var spaceRow = spaceLeft / (sparseMatrix.RowCount - i + 1);
var spaceRow = spaceLeft/(sparseMatrix.RowCount - i + 1);
// lfil = spaceRow / 2 // space for this row of L
var fillLevel = spaceRow / 2;
var fillLevel = spaceRow/2;
FindLargestItems(0, i - 1, indexSorting, workVector);
// l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
var lowerNonZeroCount = 0;
var count = 0;
@ -471,10 +459,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
}
FindLargestItems(i + 1, sparseMatrix.RowCount - 1, indexSorting, workVector);
// lfil = spaceRow - nnz(L(i,:)) // space for this row of U
fillLevel = spaceRow - lowerNonZeroCount;
// u(i,j) = w(j) for j = i + 1, .. , n // only the largest lfil - 1 elements
var upperNonZeroCount = 0;
count = 0;
@ -506,12 +494,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
// -1 values.
if ((i + 1) < (sparseMatrix.RowCount - 1))
{
if (workVector[i].Magnitude < _pivotTolerance * workVector[indexSorting[0]].Magnitude)
if (workVector[i].Magnitude < _pivotTolerance*workVector[indexSorting[0]].Magnitude)
{
// swap columns of u (which holds the values of A in the
// sections that haven't been partitioned yet.
SwapColumns(_upper, i, indexSorting[0]);
// Update P
var temp = _pivots[i];
_pivots[i] = _pivots[indexSorting[0]];
@ -533,10 +521,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// Pivot elements in the <paramref name="row"/> according to internal pivot array
/// </summary>
/// <param name="row">Row <see cref="Vector"/> to pivot in</param>
private void PivotRow(Vector<Complex32> row)
void PivotRow(Vector<Complex32> row)
{
var knownPivots = new Dictionary<int, int>();
// pivot the row
for (var i = 0; i < row.Count; i++)
{
@ -558,7 +546,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="knownPivots">Pivots already done</param>
/// <param name="currentItem">Current item to pivot</param>
/// <returns><c>true</c> if performed, otherwise <c>false</c></returns>
private bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
{
if (knownPivots.ContainsKey(_pivots[currentItem]))
{
@ -585,7 +573,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="matrix">Source <see cref="Matrix"/>.</param>
/// <param name="firstColumn">First column index to swap</param>
/// <param name="secondColumn">Second column index to swap</param>
private static void SwapColumns(Matrix<Complex32> matrix, int firstColumn, int secondColumn)
static void SwapColumns(Matrix<Complex32> matrix, int firstColumn, int secondColumn)
{
for (var i = 0; i < matrix.RowCount; i++)
{
@ -602,7 +590,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="upperBound">Sort till upper bound</param>
/// <param name="sortedIndices">Array with sorted vector indicies</param>
/// <param name="values">Source <see cref="Vector"/></param>
private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<Complex32> values)
static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<Complex32> values)
{
// Copy the indices for the values into the array
for (var i = 0; i < upperBound + 1 - lowerBound; i++)
@ -622,33 +610,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
IlutpElementSorter.SortDoubleIndicesDecreasing(0, upperBound - lowerBound, sortedIndices, values);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex32> Approximate(Vector<Complex32> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _upper.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -656,16 +617,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex32> rhs, Vector<Complex32> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -675,7 +626,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
// Solve equation here
// Pivot(vector, result);
// Solve L*Y = B(piv,:)
@ -687,7 +638,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var sum = Complex32.Zero;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -701,16 +652,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var sum = Complex32.Zero;
for (var j = _upper.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
// We have a column pivot so we only need to pivot the
// end result not the incoming right hand side vector
var temp = lhs.Clone();
Pivot(temp, lhs);
}
@ -719,7 +670,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// </summary>
/// <param name="vector">Source <see cref="Vector"/>.</param>
/// <param name="result">Result <see cref="Vector"/> after pivoting.</param>
private void Pivot(Vector<Complex32> vector, Vector<Complex32> result)
void Pivot(Vector<Complex32> vector, Vector<Complex32> result)
{
for (var i = 0; i < _pivots.Length; i++)
{

53
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs

@ -51,7 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// The matrix holding the lower (L) and upper (U) matrices. The
/// decomposition matrices are combined to reduce storage.
/// </summary>
private SparseMatrix _decompositionLU;
SparseMatrix _decompositionLU;
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
@ -86,7 +86,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
{
result[i, j] = 1.0f;
}
else
else
{
result[i, j] = _decompositionLU[i, j];
}
@ -135,11 +135,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
{
if (_decompositionLU[i, k] != 0.0f)
{
var t = _decompositionLU[i, k] / _decompositionLU[k, k];
var t = _decompositionLU[i, k]/_decompositionLU[k, k];
_decompositionLU[i, k] = t;
if (_decompositionLU[k, i] != 0.0f)
{
_decompositionLU[i, i] = _decompositionLU[i, i] - (t * _decompositionLU[k, i]);
_decompositionLU[i, i] = _decompositionLU[i, i] - (t*_decompositionLU[k, i]);
}
for (var j = k + 1; j < _decompositionLU.RowCount; j++)
@ -151,7 +151,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
if (_decompositionLU[i, j] != 0.0f)
{
_decompositionLU[i, j] = _decompositionLU[i, j] - (t * _decompositionLU[k, j]);
_decompositionLU[i, j] = _decompositionLU[i, j] - (t*_decompositionLU[k, j]);
}
}
}
@ -159,33 +159,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<Complex32> Approximate(Vector<Complex32> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _decompositionLU.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -193,16 +166,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<Complex32> rhs, Vector<Complex32> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -231,7 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var sum = Complex32.Zero;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -251,10 +214,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
var sum = Complex32.Zero;
for (var j = _decompositionLU.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
}
}

1
src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs

@ -30,7 +30,6 @@
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
{

50
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Diagonal.cs

@ -43,7 +43,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <summary>
/// The inverse of the matrix diagonal.
/// </summary>
private double[] _inverseDiagonals;
double[] _inverseDiagonals;
/// <summary>
/// Returns the decomposed matrix diagonal.
@ -54,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var result = new DiagonalMatrix(_inverseDiagonals.Length);
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
result[i, i] = 1 / _inverseDiagonals[i];
result[i, i] = 1/_inverseDiagonals[i];
}
return result;
@ -69,11 +69,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<double> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
@ -82,37 +77,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
_inverseDiagonals = new double[matrix.RowCount];
for (var i = 0; i < matrix.RowCount; i++)
{
_inverseDiagonals[i] = 1 / matrix[i, i];
_inverseDiagonals[i] = 1/matrix[i, i];
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<double> Approximate(Vector<double> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _inverseDiagonals.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -120,16 +88,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<double> rhs, Vector<double> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -142,7 +100,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
lhs[i] = rhs[i] * _inverseDiagonals[i];
lhs[i] = rhs[i]*_inverseDiagonals[i];
}
}
}

131
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs

@ -63,36 +63,36 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// The default drop tolerance.
/// </summary>
public const double DefaultDropTolerance = 0.0001;
/// <summary>
/// The decomposed upper triangular matrix.
/// </summary>
private SparseMatrix _upper;
SparseMatrix _upper;
/// <summary>
/// The decomposed lower triangular matrix.
/// </summary>
private SparseMatrix _lower;
SparseMatrix _lower;
/// <summary>
/// The array containing the pivot values.
/// </summary>
private int[] _pivots;
int[] _pivots;
/// <summary>
/// The fill level.
/// </summary>
private double _fillLevel = DefaultFillLevel;
double _fillLevel = DefaultFillLevel;
/// <summary>
/// The drop tolerance.
/// </summary>
private double _dropTolerance = DefaultDropTolerance;
double _dropTolerance = DefaultDropTolerance;
/// <summary>
/// The pivot tolerance.
/// </summary>
private double _pivotTolerance;
double _pivotTolerance;
/// <summary>
/// Initializes a new instance of the <see cref="Ilutp"/> class with the default settings.
@ -138,7 +138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
_dropTolerance = dropTolerance;
_pivotTolerance = pivotTolerance;
}
/// <summary>
/// Gets or sets the amount of fill that is allowed in the matrix. The
/// value is a fraction of the number of non-zero entries in the original
@ -161,11 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get
{
return _fillLevel;
}
get { return _fillLevel; }
set
{
if (value < 0)
@ -196,11 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get
{
return _dropTolerance;
}
get { return _dropTolerance; }
set
{
if (value < 0)
@ -233,11 +225,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get
{
return _pivotTolerance;
}
get { return _pivotTolerance; }
set
{
if (value < 0)
@ -248,7 +236,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
_pivotTolerance = value;
}
}
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
/// </summary>
@ -366,7 +354,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
// Create the upper triangular matrix and copy the values
_upper = new SparseMatrix(sparseMatrix.RowCount);
// Create the pivot array
_pivots = new int[sparseMatrix.RowCount];
for (var i = 0; i < _pivots.Length; i++)
@ -377,20 +365,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var workVector = new DenseVector(sparseMatrix.RowCount);
var rowVector = new DenseVector(sparseMatrix.ColumnCount);
var indexSorting = new int[sparseMatrix.RowCount];
// spaceLeft = lfilNnz * nnz(A)
var spaceLeft = (int)_fillLevel * sparseMatrix.NonZerosCount;
var spaceLeft = (int) _fillLevel*sparseMatrix.NonZerosCount;
// for i = 1, .. , n
for (var i = 0; i < sparseMatrix.RowCount; i++)
{
// w = a(i,*)
sparseMatrix.Row(i, workVector);
// pivot the row
PivotRow(workVector);
var vectorNorm = workVector.InfinityNorm();
// for j = 1, .. , i - 1)
for (var j = 0; j < i; j++)
{
@ -408,7 +396,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
if (workVector[j] != 0.0)
{
// Calculate the multiplication factors that go into the L matrix
workVector[j] = workVector[j] / _upper[j, j];
workVector[j] = workVector[j]/_upper[j, j];
if (Math.Abs(workVector[j]) < _dropTolerance)
{
workVector[j] = 0.0;
@ -440,19 +428,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
// {
// w(j) = 0
// }
if (Math.Abs(workVector[j]) <= _dropTolerance * vectorNorm)
if (Math.Abs(workVector[j]) <= _dropTolerance*vectorNorm)
{
workVector[j] = 0.0;
}
}
// spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
var spaceRow = spaceLeft / (sparseMatrix.RowCount - i + 1);
var spaceRow = spaceLeft/(sparseMatrix.RowCount - i + 1);
// lfil = spaceRow / 2 // space for this row of L
var fillLevel = spaceRow / 2;
var fillLevel = spaceRow/2;
FindLargestItems(0, i - 1, indexSorting, workVector);
// l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
var lowerNonZeroCount = 0;
var count = 0;
@ -469,10 +457,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
}
FindLargestItems(i + 1, sparseMatrix.RowCount - 1, indexSorting, workVector);
// lfil = spaceRow - nnz(L(i,:)) // space for this row of U
fillLevel = spaceRow - lowerNonZeroCount;
// u(i,j) = w(j) for j = i + 1, .. , n // only the largest lfil - 1 elements
var upperNonZeroCount = 0;
count = 0;
@ -504,12 +492,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
// -1 values.
if ((i + 1) < (sparseMatrix.RowCount - 1))
{
if (Math.Abs(workVector[i]) < _pivotTolerance * Math.Abs(workVector[indexSorting[0]]))
if (Math.Abs(workVector[i]) < _pivotTolerance*Math.Abs(workVector[indexSorting[0]]))
{
// swap columns of u (which holds the values of A in the
// sections that haven't been partitioned yet.
SwapColumns(_upper, i, indexSorting[0]);
// Update P
var temp = _pivots[i];
_pivots[i] = _pivots[indexSorting[0]];
@ -531,10 +519,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// Pivot elements in the <paramref name="row"/> according to internal pivot array
/// </summary>
/// <param name="row">Row <see cref="Vector"/> to pivot in</param>
private void PivotRow(Vector<double> row)
void PivotRow(Vector<double> row)
{
var knownPivots = new Dictionary<int, int>();
// pivot the row
for (var i = 0; i < row.Count; i++)
{
@ -556,7 +544,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="knownPivots">Pivots already done</param>
/// <param name="currentItem">Current item to pivot</param>
/// <returns><c>true</c> if performed, otherwise <c>false</c></returns>
private bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
{
if (knownPivots.ContainsKey(_pivots[currentItem]))
{
@ -583,7 +571,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="matrix">Source <see cref="Matrix"/>.</param>
/// <param name="firstColumn">First column index to swap</param>
/// <param name="secondColumn">Second column index to swap</param>
private static void SwapColumns(Matrix<double> matrix, int firstColumn, int secondColumn)
static void SwapColumns(Matrix<double> matrix, int firstColumn, int secondColumn)
{
for (var i = 0; i < matrix.RowCount; i++)
{
@ -600,7 +588,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="upperBound">Sort till upper bound</param>
/// <param name="sortedIndices">Array with sorted vector indicies</param>
/// <param name="values">Source <see cref="Vector"/></param>
private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<double> values)
static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<double> values)
{
// Copy the indices for the values into the array
for (var i = 0; i < upperBound + 1 - lowerBound; i++)
@ -620,33 +608,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
IlutpElementSorter.SortDoubleIndicesDecreasing(0, upperBound - lowerBound, sortedIndices, values);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<double> Approximate(Vector<double> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _upper.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -654,16 +615,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<double> rhs, Vector<double> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -673,7 +624,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
// Solve equation here
// Pivot(vector, result);
// Solve L*Y = B(piv,:)
@ -685,7 +636,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var sum = 0.0;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -699,16 +650,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var sum = 0.0;
for (var j = _upper.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
// We have a column pivot so we only need to pivot the
// end result not the incoming right hand side vector
var temp = lhs.Clone();
Pivot(temp, lhs);
}
@ -717,7 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// </summary>
/// <param name="vector">Source <see cref="Vector"/>.</param>
/// <param name="result">Result <see cref="Vector"/> after pivoting.</param>
private void Pivot(Vector<double> vector, Vector<double> result)
void Pivot(Vector<double> vector, Vector<double> result)
{
for (var i = 0; i < _pivots.Length; i++)
{

53
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// The matrix holding the lower (L) and upper (U) matrices. The
/// decomposition matrices are combined to reduce storage.
/// </summary>
private SparseMatrix _decompositionLU;
SparseMatrix _decompositionLU;
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
{
result[i, j] = 1.0;
}
else
else
{
result[i, j] = _decompositionLU[i, j];
}
@ -133,11 +133,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
{
if (_decompositionLU[i, k] != 0.0)
{
var t = _decompositionLU[i, k] / _decompositionLU[k, k];
var t = _decompositionLU[i, k]/_decompositionLU[k, k];
_decompositionLU[i, k] = t;
if (_decompositionLU[k, i] != 0.0)
{
_decompositionLU[i, i] = _decompositionLU[i, i] - (t * _decompositionLU[k, i]);
_decompositionLU[i, i] = _decompositionLU[i, i] - (t*_decompositionLU[k, i]);
}
for (var j = k + 1; j < _decompositionLU.RowCount; j++)
@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
if (_decompositionLU[i, j] != 0.0)
{
_decompositionLU[i, j] = _decompositionLU[i, j] - (t * _decompositionLU[k, j]);
_decompositionLU[i, j] = _decompositionLU[i, j] - (t*_decompositionLU[k, j]);
}
}
}
@ -157,33 +157,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<double> Approximate(Vector<double> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _decompositionLU.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -191,16 +164,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<double> rhs, Vector<double> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -229,7 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var sum = 0.0;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -249,10 +212,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
var sum = 0.0;
for (var j = _decompositionLU.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
}
}

1
src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs

@ -30,7 +30,6 @@
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
{

50
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Diagonal.cs

@ -43,7 +43,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <summary>
/// The inverse of the matrix diagonal.
/// </summary>
private float[] _inverseDiagonals;
float[] _inverseDiagonals;
/// <summary>
/// Returns the decomposed matrix diagonal.
@ -54,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var result = new DiagonalMatrix(_inverseDiagonals.Length);
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
result[i, i] = 1 / _inverseDiagonals[i];
result[i, i] = 1/_inverseDiagonals[i];
}
return result;
@ -69,11 +69,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<float> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
@ -82,37 +77,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
_inverseDiagonals = new float[matrix.RowCount];
for (var i = 0; i < matrix.RowCount; i++)
{
_inverseDiagonals[i] = 1 / matrix[i, i];
_inverseDiagonals[i] = 1/matrix[i, i];
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<float> Approximate(Vector<float> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _inverseDiagonals.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -120,16 +88,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<float> rhs, Vector<float> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_inverseDiagonals == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -142,7 +100,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
for (var i = 0; i < _inverseDiagonals.Length; i++)
{
lhs[i] = rhs[i] * _inverseDiagonals[i];
lhs[i] = rhs[i]*_inverseDiagonals[i];
}
}
}

131
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs

@ -63,36 +63,36 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// The default drop tolerance.
/// </summary>
public const double DefaultDropTolerance = 0.0001;
/// <summary>
/// The decomposed upper triangular matrix.
/// </summary>
private SparseMatrix _upper;
SparseMatrix _upper;
/// <summary>
/// The decomposed lower triangular matrix.
/// </summary>
private SparseMatrix _lower;
SparseMatrix _lower;
/// <summary>
/// The array containing the pivot values.
/// </summary>
private int[] _pivots;
int[] _pivots;
/// <summary>
/// The fill level.
/// </summary>
private double _fillLevel = DefaultFillLevel;
double _fillLevel = DefaultFillLevel;
/// <summary>
/// The drop tolerance.
/// </summary>
private double _dropTolerance = DefaultDropTolerance;
double _dropTolerance = DefaultDropTolerance;
/// <summary>
/// The pivot tolerance.
/// </summary>
private double _pivotTolerance;
double _pivotTolerance;
/// <summary>
/// Initializes a new instance of the <see cref="Ilutp"/> class with the default settings.
@ -138,7 +138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
_dropTolerance = dropTolerance;
_pivotTolerance = pivotTolerance;
}
/// <summary>
/// Gets or sets the amount of fill that is allowed in the matrix. The
/// value is a fraction of the number of non-zero entries in the original
@ -161,11 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get
{
return _fillLevel;
}
get { return _fillLevel; }
set
{
if (value < 0)
@ -196,11 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get
{
return _dropTolerance;
}
get { return _dropTolerance; }
set
{
if (value < 0)
@ -233,11 +225,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get
{
return _pivotTolerance;
}
get { return _pivotTolerance; }
set
{
if (value < 0)
@ -248,7 +236,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
_pivotTolerance = value;
}
}
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
/// </summary>
@ -366,7 +354,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
// Create the upper triangular matrix and copy the values
_upper = new SparseMatrix(sparseMatrix.RowCount);
// Create the pivot array
_pivots = new int[sparseMatrix.RowCount];
for (var i = 0; i < _pivots.Length; i++)
@ -377,20 +365,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var workVector = new DenseVector(sparseMatrix.RowCount);
var rowVector = new DenseVector(sparseMatrix.ColumnCount);
var indexSorting = new int[sparseMatrix.RowCount];
// spaceLeft = lfilNnz * nnz(A)
var spaceLeft = (int)_fillLevel * sparseMatrix.NonZerosCount;
var spaceLeft = (int) _fillLevel*sparseMatrix.NonZerosCount;
// for i = 1, .. , n
for (var i = 0; i < sparseMatrix.RowCount; i++)
{
// w = a(i,*)
sparseMatrix.Row(i, workVector);
// pivot the row
PivotRow(workVector);
var vectorNorm = workVector.InfinityNorm();
// for j = 1, .. , i - 1)
for (var j = 0; j < i; j++)
{
@ -408,7 +396,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
if (workVector[j] != 0.0)
{
// Calculate the multiplication factors that go into the L matrix
workVector[j] = workVector[j] / _upper[j, j];
workVector[j] = workVector[j]/_upper[j, j];
if (Math.Abs(workVector[j]) < _dropTolerance)
{
workVector[j] = 0.0f;
@ -440,19 +428,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
// {
// w(j) = 0
// }
if (Math.Abs(workVector[j]) <= _dropTolerance * vectorNorm)
if (Math.Abs(workVector[j]) <= _dropTolerance*vectorNorm)
{
workVector[j] = 0.0f;
}
}
// spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
var spaceRow = spaceLeft / (sparseMatrix.RowCount - i + 1);
var spaceRow = spaceLeft/(sparseMatrix.RowCount - i + 1);
// lfil = spaceRow / 2 // space for this row of L
var fillLevel = spaceRow / 2;
var fillLevel = spaceRow/2;
FindLargestItems(0, i - 1, indexSorting, workVector);
// l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
var lowerNonZeroCount = 0;
var count = 0;
@ -469,10 +457,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
}
FindLargestItems(i + 1, sparseMatrix.RowCount - 1, indexSorting, workVector);
// lfil = spaceRow - nnz(L(i,:)) // space for this row of U
fillLevel = spaceRow - lowerNonZeroCount;
// u(i,j) = w(j) for j = i + 1, .. , n // only the largest lfil - 1 elements
var upperNonZeroCount = 0;
count = 0;
@ -504,12 +492,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
// -1 values.
if ((i + 1) < (sparseMatrix.RowCount - 1))
{
if (Math.Abs(workVector[i]) < _pivotTolerance * Math.Abs(workVector[indexSorting[0]]))
if (Math.Abs(workVector[i]) < _pivotTolerance*Math.Abs(workVector[indexSorting[0]]))
{
// swap columns of u (which holds the values of A in the
// sections that haven't been partitioned yet.
SwapColumns(_upper, i, indexSorting[0]);
// Update P
var temp = _pivots[i];
_pivots[i] = _pivots[indexSorting[0]];
@ -531,10 +519,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// Pivot elements in the <paramref name="row"/> according to internal pivot array
/// </summary>
/// <param name="row">Row <see cref="Vector"/> to pivot in</param>
private void PivotRow(Vector<float> row)
void PivotRow(Vector<float> row)
{
var knownPivots = new Dictionary<int, int>();
// pivot the row
for (var i = 0; i < row.Count; i++)
{
@ -556,7 +544,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="knownPivots">Pivots already done</param>
/// <param name="currentItem">Current item to pivot</param>
/// <returns><c>true</c> if performed, otherwise <c>false</c></returns>
private bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
bool PivotMapFound(Dictionary<int, int> knownPivots, int currentItem)
{
if (knownPivots.ContainsKey(_pivots[currentItem]))
{
@ -583,7 +571,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="matrix">Source <see cref="Matrix"/>.</param>
/// <param name="firstColumn">First column index to swap</param>
/// <param name="secondColumn">Second column index to swap</param>
private static void SwapColumns(Matrix<float> matrix, int firstColumn, int secondColumn)
static void SwapColumns(Matrix<float> matrix, int firstColumn, int secondColumn)
{
for (var i = 0; i < matrix.RowCount; i++)
{
@ -600,7 +588,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="upperBound">Sort till upper bound</param>
/// <param name="sortedIndices">Array with sorted vector indicies</param>
/// <param name="values">Source <see cref="Vector"/></param>
private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<float> values)
static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector<float> values)
{
// Copy the indices for the values into the array
for (var i = 0; i < upperBound + 1 - lowerBound; i++)
@ -620,33 +608,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
IlutpElementSorter.SortDoubleIndicesDecreasing(0, upperBound - lowerBound, sortedIndices, values);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<float> Approximate(Vector<float> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _upper.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -654,16 +615,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<float> rhs, Vector<float> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_upper == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -673,7 +624,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
// Solve equation here
// Pivot(vector, result);
// Solve L*Y = B(piv,:)
@ -685,7 +636,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var sum = 0.0f;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -699,16 +650,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var sum = 0.0f;
for (var j = _upper.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
// We have a column pivot so we only need to pivot the
// end result not the incoming right hand side vector
var temp = lhs.Clone();
Pivot(temp, lhs);
}
@ -717,7 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// </summary>
/// <param name="vector">Source <see cref="Vector"/>.</param>
/// <param name="result">Result <see cref="Vector"/> after pivoting.</param>
private void Pivot(Vector<float> vector, Vector<float> result)
void Pivot(Vector<float> vector, Vector<float> result)
{
for (var i = 0; i < _pivots.Length; i++)
{

53
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// The matrix holding the lower (L) and upper (U) matrices. The
/// decomposition matrices are combined to reduce storage.
/// </summary>
private SparseMatrix _decompositionLU;
SparseMatrix _decompositionLU;
/// <summary>
/// Returns the upper triagonal matrix that was created during the LU decomposition.
@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
{
result[i, j] = 1.0f;
}
else
else
{
result[i, j] = _decompositionLU[i, j];
}
@ -133,11 +133,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
{
if (_decompositionLU[i, k] != 0.0)
{
var t = _decompositionLU[i, k] / _decompositionLU[k, k];
var t = _decompositionLU[i, k]/_decompositionLU[k, k];
_decompositionLU[i, k] = t;
if (_decompositionLU[k, i] != 0.0)
{
_decompositionLU[i, i] = _decompositionLU[i, i] - (t * _decompositionLU[k, i]);
_decompositionLU[i, i] = _decompositionLU[i, i] - (t*_decompositionLU[k, i]);
}
for (var j = k + 1; j < _decompositionLU.RowCount; j++)
@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
if (_decompositionLU[i, j] != 0.0)
{
_decompositionLU[i, j] = _decompositionLU[i, j] - (t * _decompositionLU[k, j]);
_decompositionLU[i, j] = _decompositionLU[i, j] - (t*_decompositionLU[k, j]);
}
}
}
@ -157,33 +157,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
}
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
public Vector<float> Approximate(Vector<float> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if (rhs.Count != _decompositionLU.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rhs");
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
@ -191,16 +164,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector<float> rhs, Vector<float> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
@ -229,7 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var sum = 0.0f;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = rhs[i] - sum;
@ -249,10 +212,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
var sum = 0.0f;
for (var j = _decompositionLU.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
sum += rowValues[j]*lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
lhs[i] = 1/rowValues[i]*(lhs[i] - sum);
}
}
}

2
src/Numerics/LinearAlgebra/Solvers/Status/ICalculationStatus.cs → src/Numerics/LinearAlgebra/Solvers/ICalculationStatus.cs

@ -28,7 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.LinearAlgebra.Solvers.Status
namespace MathNet.Numerics.LinearAlgebra.Solvers
{
/// <summary>
/// Defines the base interface for calculation status objects.

1
src/Numerics/LinearAlgebra/Solvers/IIterationStopCriterium.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
namespace MathNet.Numerics.LinearAlgebra.Solvers
{

3
src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
namespace MathNet.Numerics.LinearAlgebra.Solvers
{

4
src/Numerics/LinearAlgebra/Solvers/IIterativeSolverSetup.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -68,4 +68,4 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <value>Returns a value between 0 and 1 inclusive.</value>
double Reliability { get; }
}
}
}

26
src/Numerics/LinearAlgebra/Solvers/IIterator.cs

@ -29,8 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
namespace MathNet.Numerics.LinearAlgebra.Solvers
{
@ -39,28 +37,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// </summary>
public interface IIterator<T> where T : struct, IEquatable<T>, IFormattable
{
/// <summary>
/// Adds an <see cref="IIterationStopCriterium{T}"/> to the internal collection of stop-criteria. Only a
/// single stop criterium of each type can be stored.
/// </summary>
/// <param name="stopCriterium">The stop criterium to add.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="stopCriterium"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="stopCriterium"/> is of the same type as an already stored criterium.</exception>
void Add(IIterationStopCriterium<T> stopCriterium);
/// <summary>
/// Removes the <see cref="IIterationStopCriterium{T}"/> from the internal collection.
/// </summary>
/// <param name="stopCriterium">The stop criterium that must be removed.</param>
void Remove(IIterationStopCriterium<T> stopCriterium);
/// <summary>
/// Indicates if the specific stop criterium is stored by the <see cref="IIterator{T}"/>.
/// </summary>
/// <param name="stopCriterium">The stop criterium.</param>
/// <returns><c>true</c> if the <see cref="IIterator{T}"/> contains the stop criterium; otherwise <c>false</c>.</returns>
bool Contains(IIterationStopCriterium<T> stopCriterium);
/// <summary>
/// Indicates to the iterator that the iterative process has been cancelled.
/// </summary>
@ -97,7 +73,5 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// calculation.
/// </remarks>
void ResetToPrecalculationState();
IIterator<T> Clone();
}
}

7
src/Numerics/LinearAlgebra/Solvers/IPreConditioner.cs

@ -58,13 +58,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <param name="matrix">The matrix on which the preconditioner is based.</param>
void Initialize(Matrix<T> matrix);
/// <summary>
/// Approximates the solution to the matrix equation <b>Mx = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
Vector<T> Approximate(Vector<T> rhs);
/// <summary>
/// Approximates the solution to the matrix equation <b>Mx = b</b>.
/// </summary>

29
src/Numerics/LinearAlgebra/Solvers/IterationCountStopCriterium.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -32,7 +32,7 @@ using System;
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
namespace MathNet.Numerics.LinearAlgebra.Solvers
{
/// <summary>
/// Defines an <see cref="IIterationStopCriterium{T}"/> that monitors the numbers of iteration
@ -49,17 +49,17 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
/// <summary>
/// The default status.
/// </summary>
private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined();
static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined();
/// <summary>
/// The maximum number of iterations the calculation is allowed to perform.
/// </summary>
private int _maximumNumberOfIterations;
int _maximumNumberOfIterations;
/// <summary>
/// The status of the calculation
/// </summary>
private ICalculationStatus _status = DefaultStatus;
ICalculationStatus _status = DefaultStatus;
/// <summary>
/// Initializes a new instance of the <see cref="IterationCountStopCriterium{T}"/> class with the default maximum
@ -91,10 +91,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
public int MaximumNumberOfIterations
{
[DebuggerStepThrough]
get
{
return _maximumNumberOfIterations;
}
get { return _maximumNumberOfIterations; }
[DebuggerStepThrough]
set
@ -149,7 +146,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
/// <summary>
/// Set status to <see cref="CalculationFailure"/>
/// </summary>
private void SetStatusToFinished()
void SetStatusToFinished()
{
if (!(_status is CalculationStoppedWithoutConvergence))
{
@ -160,7 +157,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
/// <summary>
/// Set status to <see cref="CalculationRunning"/>
/// </summary>
private void SetStatusToRunning()
void SetStatusToRunning()
{
if (!(_status is CalculationRunning))
{
@ -174,10 +171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
public ICalculationStatus Status
{
[DebuggerStepThrough]
get
{
return _status;
}
get { return _status; }
}
/// <summary>
@ -196,10 +190,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium
public StopLevel StopLevel
{
[DebuggerStepThrough]
get
{
return StopLevel.StoppedWithoutConvergence;
}
get { return StopLevel.StoppedWithoutConvergence; }
}
/// <summary>

76
src/Numerics/LinearAlgebra/Solvers/Iterator.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -32,7 +32,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Solvers
@ -45,23 +44,23 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary>
/// The default status for the iterator.
/// </summary>
private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined();
static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined();
/// <summary>
/// The collection that holds all the stop criteria and the flag indicating if they should be added
/// to the child iterators.
/// </summary>
private readonly Dictionary<Type, IIterationStopCriterium<T>> _stopCriterias = new Dictionary<Type, IIterationStopCriterium<T>>();
readonly Dictionary<Type, IIterationStopCriterium<T>> _stopCriterias = new Dictionary<Type, IIterationStopCriterium<T>>();
/// <summary>
/// The status of the iterator.
/// </summary>
private ICalculationStatus _status = DefaultStatus;
ICalculationStatus _status = DefaultStatus;
/// <summary>
/// Indicates if the iteration was canceled.
/// </summary>
private bool _wasIterationCancelled;
bool _wasIterationCancelled;
/// <summary>
/// Initializes a new instance of the <see cref="Iterator{T}"/> class.
@ -97,25 +96,9 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// single stop criterium of each type can be stored.
/// </summary>
/// <param name="stopCriterium">The stop criterium to add.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="stopCriterium"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="stopCriterium"/> is of the same type as an already
/// stored criterium.
/// </exception>
public void Add(IIterationStopCriterium<T> stopCriterium)
{
if (stopCriterium == null)
{
throw new ArgumentNullException("stopCriterium");
}
if (_stopCriterias.ContainsKey(stopCriterium.GetType()))
{
throw new ArgumentException(Resources.StopCriteriumDuplicate);
}
// Store the stop criterium.
_stopCriterias.Add(stopCriterium.GetType(), stopCriterium);
_stopCriterias[stopCriterium.GetType()] = stopCriterium;
}
/// <summary>
@ -124,17 +107,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <param name="stopCriterium">The stop criterium that must be removed.</param>
public void Remove(IIterationStopCriterium<T> stopCriterium)
{
if (stopCriterium == null)
{
throw new ArgumentNullException("stopCriterium");
}
if (!_stopCriterias.ContainsKey(stopCriterium.GetType()))
{
return;
}
// Remove from the collection
_stopCriterias.Remove(stopCriterium.GetType());
}
@ -145,7 +117,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <returns><c>true</c> if the <see cref="IIterator{T}"/> contains the stop criterium; otherwise <c>false</c>.</returns>
public bool Contains(IIterationStopCriterium<T> stopCriterium)
{
return stopCriterium != null && _stopCriterias.ContainsKey(stopCriterium.GetType());
return _stopCriterias.ContainsKey(stopCriterium.GetType());
}
/// <summary>
@ -154,10 +126,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <remarks>Used for testing only.</remarks>
internal int NumberOfCriteria
{
get
{
return _stopCriterias.Count;
}
get { return _stopCriterias.Count; }
}
/// <summary>
@ -166,10 +135,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <remarks>Used for testing only.</remarks>
internal IEnumerable<IIterationStopCriterium<T>> StoredStopCriteria
{
get
{
return _stopCriterias.Select(criterium => criterium.Value);
}
get { return _stopCriterias.Select(criterium => criterium.Value); }
}
/// <summary>
@ -209,21 +175,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
throw new ArgumentOutOfRangeException("iterationNumber");
}
if (solutionVector == null)
{
throw new ArgumentNullException("solutionVector");
}
if (sourceVector == null)
{
throw new ArgumentNullException("sourceVector");
}
if (residualVector == null)
{
throw new ArgumentNullException("residualVector");
}
// While we're cancelled we don't call on the stop-criteria.
if (_wasIterationCancelled)
{
@ -260,12 +211,9 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary>
/// Gets the current calculation status.
/// </summary>
public ICalculationStatus Status
{
get
{
return _status;
}
public ICalculationStatus Status
{
get { return _status; }
}
/// <summary>

48
src/Numerics/LinearAlgebra/Solvers/UnitPreconditioner.cs

@ -44,23 +44,17 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// The coefficient matrix on which this preconditioner operates.
/// Is used to check dimensions on the different vectors that are processed.
/// </summary>
private int _size;
int _size;
/// <summary>
/// Initializes the preconditioner and loads the internal data structures.
/// </summary>
/// <param name="matrix">
/// The matrix upon which the preconditioner is based.
/// </param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<T> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
@ -74,8 +68,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="lhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// <para>
/// If <paramref name="rhs"/> and <paramref name="lhs"/> do not have the same size.
@ -89,16 +81,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// </exception>
public void Approximate(Vector<T> rhs, Vector<T> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _size))
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
@ -106,31 +88,5 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
rhs.CopyTo(lhs);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </exception>
public Vector<T> Approximate(Vector<T> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (rhs.Count != _size)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = Vector<T>.Builder.DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
}
}

2
src/Numerics/Numerics.csproj

@ -353,7 +353,7 @@
<Compile Include="LinearAlgebra\Solvers\Status\CalculationIndetermined.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\CalculationRunning.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\CalculationStoppedWithoutConvergence.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\ICalculationStatus.cs" />
<Compile Include="LinearAlgebra\Solvers\ICalculationStatus.cs" />
<Compile Include="LinearAlgebra\Double\Solvers\StopCriterium\DivergenceStopCriterium.cs" />
<Compile Include="LinearAlgebra\Double\Solvers\StopCriterium\FailureStopCriterium.cs" />
<Compile Include="LinearAlgebra\Double\Solvers\StopCriterium\ResidualStopCriterium.cs" />

1
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative

56
src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers
@ -80,19 +79,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers
Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria");
}
/// <summary>
/// Create with duplicates throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void CreateWithDuplicatesThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Iterator<Complex>(new IIterationStopCriterium<Complex>[]
{
new FailureStopCriterium(),
new FailureStopCriterium()
}));
}
/// <summary>
/// Can create with collection.
/// </summary>
@ -117,29 +103,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers
}
}
/// <summary>
/// Add with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void AddWithNullStopCriteriumThrowsArgumentNullException()
{
var iterator = new Iterator<Complex>();
Assert.Throws<ArgumentNullException>(() => iterator.Add(null));
}
/// <summary>
/// Add with existing stop criterium throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void AddWithExistingStopCriteriumThrowsArgumentException()
{
var iterator = new Iterator<Complex>();
iterator.Add(new FailureStopCriterium());
Assert.AreEqual(1, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentException>(() => iterator.Add(new FailureStopCriterium()));
}
/// <summary>
/// Can add criterium.
/// </summary>
@ -170,25 +133,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers
}
}
/// <summary>
/// Remove with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void RemoveWithNullStopCriteriumThrowsArgumentNullException()
{
var criteria = new List<IIterationStopCriterium<Complex>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<Complex>(),
new ResidualStopCriterium()
};
var iterator = new Iterator<Complex>(criteria);
Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentNullException>(() => iterator.Remove(null));
}
/// <summary>
/// Can remove with non-existing stop criterium.
/// </summary>

56
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Preconditioners/PreConditionerTest.cs

@ -96,25 +96,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Precondi
/// <param name="result">Result vector.</param>
protected abstract void CheckResult(IPreConditioner<Complex> preconditioner, SparseMatrix matrix, Vector<Complex> vector, Vector<Complex> result);
/// <summary>
/// Approximate with a unit matrix returning new vector.
/// </summary>
[Test]
public void ApproximateWithUnitMatrixReturningNewVector()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = preconditioner.Approximate(vector);
CheckResult(preconditioner, newMatrix, vector, result);
}
/// <summary>
/// Approximate returning old vector.
/// </summary>
@ -151,40 +132,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Precondi
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with <c>null</c> vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = new DenseVector(vector.Count + 10);
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(null, result));
}
/// <summary>
/// Approximate with <c>null</c> result vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullResultVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
Vector<Complex> result = null;
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with non initialized preconditioner throws <c>ArgumentException</c>.
/// </summary>
@ -194,7 +141,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Precondi
const int Size = 10;
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector));
var result = new DenseVector(vector.Count);
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
}
}

2
src/UnitTests/LinearAlgebraTests/Complex/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs

@ -30,8 +30,8 @@
using System;
using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.StopCriterium

1
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32.Solvers;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative

56
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers
@ -80,19 +79,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers
Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria");
}
/// <summary>
/// Create with duplicates throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void CreateWithDuplicatesThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Iterator<Complex32>(new IIterationStopCriterium<Complex32>[]
{
new FailureStopCriterium(),
new FailureStopCriterium()
}));
}
/// <summary>
/// Can create with collection.
/// </summary>
@ -117,29 +103,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers
}
}
/// <summary>
/// Add with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void AddWithNullStopCriteriumThrowsArgumentNullException()
{
var iterator = new Iterator<Complex32>();
Assert.Throws<ArgumentNullException>(() => iterator.Add(null));
}
/// <summary>
/// Add with existing stop criterium throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void AddWithExistingStopCriteriumThrowsArgumentException()
{
var iterator = new Iterator<Complex32>();
iterator.Add(new FailureStopCriterium());
Assert.AreEqual(1, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentException>(() => iterator.Add(new FailureStopCriterium()));
}
/// <summary>
/// Can add criterium.
/// </summary>
@ -170,25 +133,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers
}
}
/// <summary>
/// Remove with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void RemoveWithNullStopCriteriumThrowsArgumentNullException()
{
var criteria = new List<IIterationStopCriterium<Complex32>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<Complex32>(),
new ResidualStopCriterium()
};
var iterator = new Iterator<Complex32>(criteria);
Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentNullException>(() => iterator.Remove(null));
}
/// <summary>
/// Can remove with non-existing stop criterium.
/// </summary>

56
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Preconditioners/PreConditionerTest.cs

@ -89,25 +89,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Precon
/// <param name="result">Result vector.</param>
protected abstract void CheckResult(IPreConditioner<Complex32> preconditioner, SparseMatrix matrix, Vector<Complex32> vector, Vector<Complex32> result);
/// <summary>
/// Approximate with a unit matrix returning new vector.
/// </summary>
[Test]
public void ApproximateWithUnitMatrixReturningNewVector()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = preconditioner.Approximate(vector);
CheckResult(preconditioner, newMatrix, vector, result);
}
/// <summary>
/// Approximate returning old vector.
/// </summary>
@ -144,40 +125,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Precon
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with <c>null</c> vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = new DenseVector(vector.Count + 10);
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(null, result));
}
/// <summary>
/// Approximate with <c>null</c> result vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullResultVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
Vector<Complex32> result = null;
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with non initialized preconditioner throws <c>ArgumentException</c>.
/// </summary>
@ -187,7 +134,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Precon
const int Size = 10;
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector));
var result = new DenseVector(vector.Count);
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
}
}

2
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs

@ -30,8 +30,8 @@
using System;
using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.StopCriterium

1
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative

56
src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers
@ -78,19 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers
Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria");
}
/// <summary>
/// Create with duplicates throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void CreateWithDuplicatesThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Iterator<double>(new IIterationStopCriterium<double>[]
{
new FailureStopCriterium(),
new FailureStopCriterium()
}));
}
/// <summary>
/// Can create with collection.
/// </summary>
@ -115,29 +101,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers
}
}
/// <summary>
/// Add with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void AddWithNullStopCriteriumThrowsArgumentNullException()
{
var iterator = new Iterator<double>();
Assert.Throws<ArgumentNullException>(() => iterator.Add(null));
}
/// <summary>
/// Add with existing stop criterium throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void AddWithExistingStopCriteriumThrowsArgumentException()
{
var iterator = new Iterator<double>();
iterator.Add(new FailureStopCriterium());
Assert.AreEqual(1, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentException>(() => iterator.Add(new FailureStopCriterium()));
}
/// <summary>
/// Can add criterium.
/// </summary>
@ -168,25 +131,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers
}
}
/// <summary>
/// Remove with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void RemoveWithNullStopCriteriumThrowsArgumentNullException()
{
var criteria = new List<IIterationStopCriterium<double>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<double>(),
new ResidualStopCriterium()
};
var iterator = new Iterator<double>(criteria);
Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentNullException>(() => iterator.Remove(null));
}
/// <summary>
/// Can remove with non-existing stop criterium.
/// </summary>

56
src/UnitTests/LinearAlgebraTests/Double/Solvers/Preconditioners/PreConditionerTest.cs

@ -87,25 +87,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Precondit
/// <param name="result">Result vector.</param>
protected abstract void CheckResult(IPreConditioner<double> preconditioner, SparseMatrix matrix, Vector<double> vector, Vector<double> result);
/// <summary>
/// Approximate with a unit matrix returning new vector.
/// </summary>
[Test]
public void ApproximateWithUnitMatrixReturningNewVector()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = preconditioner.Approximate(vector);
CheckResult(preconditioner, newMatrix, vector, result);
}
/// <summary>
/// Approximate returning old vector.
/// </summary>
@ -142,40 +123,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Precondit
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with <c>null</c> vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = new DenseVector(vector.Count + 10);
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(null, result));
}
/// <summary>
/// Approximate with <c>null</c> result vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullResultVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
Vector<double> result = null;
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with non initialized preconditioner throws <c>ArgumentException</c>.
/// </summary>
@ -185,7 +132,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Precondit
const int Size = 10;
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector));
var result = new DenseVector(vector.Count);
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
}
}

2
src/UnitTests/LinearAlgebraTests/Double/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs

@ -30,8 +30,8 @@
using System;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.StopCriterium

1
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Single.Solvers;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Single.Solvers;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs

@ -35,7 +35,6 @@ using MathNet.Numerics.LinearAlgebra.Single.Solvers;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative

1
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Single.Solvers;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative

56
src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs

@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers
@ -78,19 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers
Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria");
}
/// <summary>
/// Create with duplicates throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void CreateWithDuplicatesThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Iterator<float>(new IIterationStopCriterium<float>[]
{
new FailureStopCriterium(),
new FailureStopCriterium()
}));
}
/// <summary>
/// Can create with collection.
/// </summary>
@ -115,29 +101,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers
}
}
/// <summary>
/// Add with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void AddWithNullStopCriteriumThrowsArgumentNullException()
{
var iterator = new Iterator<float>();
Assert.Throws<ArgumentNullException>(() => iterator.Add(null));
}
/// <summary>
/// Add with existing stop criterium throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void AddWithExistingStopCriteriumThrowsArgumentException()
{
var iterator = new Iterator<float>();
iterator.Add(new FailureStopCriterium());
Assert.AreEqual(1, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentException>(() => iterator.Add(new FailureStopCriterium()));
}
/// <summary>
/// Can add criterium.
/// </summary>
@ -168,25 +131,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers
}
}
/// <summary>
/// Remove with <c>null</c> stop criterium throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void RemoveWithNullStopCriteriumThrowsArgumentNullException()
{
var criteria = new List<IIterationStopCriterium<float>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<float>(),
new ResidualStopCriterium()
};
var iterator = new Iterator<float>(criteria);
Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count");
Assert.Throws<ArgumentNullException>(() => iterator.Remove(null));
}
/// <summary>
/// Can remove with non-existing stop criterium.
/// </summary>

56
src/UnitTests/LinearAlgebraTests/Single/Solvers/Preconditioners/PreConditionerTest.cs

@ -89,25 +89,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Precondit
/// <param name="result">Result vector.</param>
protected abstract void CheckResult(IPreConditioner<float> preconditioner, SparseMatrix matrix, Vector<float> vector, Vector<float> result);
/// <summary>
/// Approximate with a unit matrix returning new vector.
/// </summary>
[Test]
public void ApproximateWithUnitMatrixReturningNewVector()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = preconditioner.Approximate(vector);
CheckResult(preconditioner, newMatrix, vector, result);
}
/// <summary>
/// Approximate returning old vector.
/// </summary>
@ -144,40 +125,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Precondit
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with <c>null</c> vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
var result = new DenseVector(vector.Count + 10);
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(null, result));
}
/// <summary>
/// Approximate with <c>null</c> result vector throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ApproximateWithNullResultVectorThrowsArgumentNullException()
{
const int Size = 10;
var newMatrix = CreateUnitMatrix(Size);
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
preconditioner.Initialize(newMatrix);
Vector<float> result = null;
Assert.Throws<ArgumentNullException>(() => preconditioner.Approximate(vector, result));
}
/// <summary>
/// Approximate with non initialized preconditioner throws <c>ArgumentException</c>.
/// </summary>
@ -187,7 +134,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Precondit
const int Size = 10;
var vector = CreateStandardBcVector(Size);
var preconditioner = CreatePreconditioner();
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector));
var result = new DenseVector(vector.Count);
Assert.Throws<ArgumentException>(() => preconditioner.Approximate(vector, result));
}
}
}

2
src/UnitTests/LinearAlgebraTests/Single/Solvers/StopCriterium/IterationCountStopCriteriumTest.cs

@ -30,8 +30,8 @@
using System;
using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.LinearAlgebra.Solvers.StopCriterium;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.StopCriterium

Loading…
Cancel
Save