本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Single.Solvers.Iterator类的典型用法代码示例。如果您正苦于以下问题:C# Iterator类的具体用法?C# Iterator怎么用?C# Iterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Iterator类属于MathNet.Numerics.LinearAlgebra.Single.Solvers命名空间,在下文中一共展示了Iterator类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DetermineStatus
public void DetermineStatus()
{
var criteria = new List<IIterationStopCriterium<float>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<float>(1)
};
var iterator = new Iterator<float>(criteria);
// First step, nothing should happen.
iterator.DetermineStatus(
0,
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4));
Assert.AreEqual(IterationStatus.Continue, iterator.Status, "Incorrect status");
// Second step, should run out of iterations.
iterator.DetermineStatus(
1,
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4));
Assert.AreEqual(IterationStatus.StoppedWithoutConvergence, iterator.Status, "Incorrect status");
}
示例2: CreateDefault
/// <summary>
/// Creates a default iterator with all the <see cref="IIterationStopCriterium"/> objects.
/// </summary>
/// <returns>A new <see cref="IIterator"/> object.</returns>
public static IIterator CreateDefault()
{
var iterator = new Iterator();
iterator.Add(new FailureStopCriterium());
iterator.Add(new DivergenceStopCriterium());
iterator.Add(new IterationCountStopCriterium());
iterator.Add(new ResidualStopCriterium());
return iterator;
}
示例3: CanSolveForRandomMatrix
public void CanSolveForRandomMatrix(int order)
{
// Due to datatype "float" it can happen that solution will not converge for specific random matrix
// That's why we will do 4 tries and downgrade stop criterium each time
for (var iteration = 6; iteration > 3; iteration--)
{
var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order);
var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order);
var monitor = new Iterator<float>(new IIterationStopCriterium<float>[]
{
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration))
});
var solver = new TFQMR(monitor);
var matrixX = solver.Solve(matrixA, matrixB);
if (!monitor.HasConverged)
{
// Solution was not found, try again downgrading convergence boundary
continue;
}
// The solution X row dimension is equal to the column dimension of A
Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);
// The solution X has the same number of columns as B
Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);
var matrixBReconstruct = matrixA*matrixX;
// Check the reconstruction.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], (float) Math.Pow(1.0/10.0, iteration - 4));
}
}
return;
}
}
示例4: SolveUnitMatrixAndBackMultiply
public void SolveUnitMatrixAndBackMultiply()
{
// Create the identity matrix
var matrix = SparseMatrix.Identity(100);
// Create the y vector
var y = DenseVector.Create(matrix.RowCount, i => 1);
// Create an iteration monitor which will keep track of iterative convergence
var monitor = new Iterator<float>(
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium(ConvergenceBoundary),
new DivergenceStopCriterium(),
new FailureStopCriterium());
var solver = new TFQMR();
// Solve equation Ax = y
var x = matrix.SolveIterative(y, solver, monitor);
// Now compare the results
Assert.IsNotNull(x, "#02");
Assert.AreEqual(y.Count, x.Count, "#03");
// Back multiply the vector
var z = matrix.Multiply(x);
// Check that the solution converged
Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04");
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
}
}
示例5: SolvePoissonMatrixAndBackMultiply
public void SolvePoissonMatrixAndBackMultiply()
{
// Create the matrix
var matrix = new SparseMatrix(25);
// Assemble the matrix. We assume we're solving the Poisson equation
// on a rectangular 5 x 5 grid
const int GridSize = 5;
// The pattern is:
// 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0
for (var i = 0; i < matrix.RowCount; i++)
{
// Insert the first set of -1's
if (i > (GridSize - 1))
{
matrix[i, i - GridSize] = -1;
}
// Insert the second set of -1's
if (i > 0)
{
matrix[i, i - 1] = -1;
}
// Insert the centerline values
matrix[i, i] = 4;
// Insert the first trailing set of -1's
if (i < matrix.RowCount - 1)
{
matrix[i, i + 1] = -1;
}
// Insert the second trailing set of -1's
if (i < matrix.RowCount - GridSize)
{
matrix[i, i + GridSize] = -1;
}
}
// Create the y vector
var y = DenseVector.Create(matrix.RowCount, i => 1);
// Create an iteration monitor which will keep track of iterative convergence
var monitor = new Iterator<float>(
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium(ConvergenceBoundary),
new DivergenceStopCriterium(),
new FailureStopCriterium());
var solver = new TFQMR();
// Solve equation Ax = y
var x = matrix.SolveIterative(y, solver, monitor);
// Now compare the results
Assert.IsNotNull(x, "#02");
Assert.AreEqual(y.Count, x.Count, "#03");
// Back multiply the vector
var z = matrix.Multiply(x);
// Check that the solution converged
Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04");
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
}
}
示例6: CanSolveForRandomVector
public void CanSolveForRandomVector(int order)
{
// Due to datatype "float" it can happen that solution will not converge for specific random matrix
// That's why we will do 3 tries and downgrade stop criterium each time
for (var iteration = 6; iteration > 3; iteration--)
{
var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order);
var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
var monitor = new Iterator<float>(
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)));
var solver = new TFQMR();
var resultx = matrixA.SolveIterative(vectorb, solver, monitor);
if (monitor.Status != IterationStatus.Converged)
{
// Solution was not found, try again downgrading convergence boundary
continue;
}
Assert.AreEqual(matrixA.ColumnCount, resultx.Count);
var matrixBReconstruct = matrixA*resultx;
// Check the reconstruction.
for (var i = 0; i < order; i++)
{
Assert.AreEqual(vectorb[i], matrixBReconstruct[i], (float) Math.Pow(1.0/10.0, iteration - 3));
}
return;
}
}
示例7: SolvePoissonMatrixAndBackMultiply
public void SolvePoissonMatrixAndBackMultiply()
{
// Create the matrix
var matrix = new SparseMatrix(25);
// Assemble the matrix. We assume we're solving the Poisson equation
// on a rectangular 5 x 5 grid
const int GridSize = 5;
// The pattern is:
// 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0
for (var i = 0; i < matrix.RowCount; i++)
{
// Insert the first set of -1's
if (i > (GridSize - 1))
{
matrix[i, i - GridSize] = -1;
}
// Insert the second set of -1's
if (i > 0)
{
matrix[i, i - 1] = -1;
}
// Insert the centerline values
matrix[i, i] = 4;
// Insert the first trailing set of -1's
if (i < matrix.RowCount - 1)
{
matrix[i, i + 1] = -1;
}
// Insert the second trailing set of -1's
if (i < matrix.RowCount - GridSize)
{
matrix[i, i + GridSize] = -1;
}
}
// Create the y vector
var y = DenseVector.Create(matrix.RowCount, i => 1);
// Due to datatype "float" it can happen that solution will not converge for specific random starting vectors
// That's why we will do 3 tries
for (var iteration = 0; iteration <= 3; iteration++)
{
// Create an iteration monitor which will keep track of iterative convergence
var monitor = new Iterator<float>(
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium<float>(ConvergenceBoundary),
new DivergenceStopCriterium<float>(),
new FailureStopCriterium<float>());
var solver = new MlkBiCgStab();
// Solve equation Ax = y
Vector<float> x;
try
{
x = matrix.SolveIterative(y, solver, monitor);
}
catch (Exception)
{
continue;
}
if (monitor.Status != IterationStatus.Converged)
{
continue;
}
// Now compare the results
Assert.IsNotNull(x, "#02");
Assert.AreEqual(y.Count, x.Count, "#03");
// Back multiply the vector
var z = matrix.Multiply(x);
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
return;
}
}
示例8: SolveScaledUnitMatrixAndBackMultiply
public void SolveScaledUnitMatrixAndBackMultiply()
{
// Create the identity matrix
var matrix = SparseMatrix.CreateIdentity(100);
// Scale it with a funny number
matrix.Multiply((float)Math.PI, matrix);
// Create the y vector
var y = Vector<float>.Build.Dense(matrix.RowCount, 1);
// Create an iteration monitor which will keep track of iterative convergence
var monitor = new Iterator<float>(
new IterationCountStopCriterion<float>(MaximumIterations),
new ResidualStopCriterion<float>(ConvergenceBoundary),
new DivergenceStopCriterion<float>(),
new FailureStopCriterion<float>());
var solver = new MlkBiCgStab();
// Solve equation Ax = y
var x = matrix.SolveIterative(y, solver, monitor);
// Now compare the results
Assert.IsNotNull(x, "#02");
Assert.AreEqual(y.Count, x.Count, "#03");
// Back multiply the vector
var z = matrix.Multiply(x);
// Check that the solution converged
Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04");
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
示例9: SolveUnitMatrixAndBackMultiply
public void SolveUnitMatrixAndBackMultiply()
{
// Create the identity matrix
var matrix = SparseMatrix.CreateIdentity(100);
// Create the y vector
var y = Vector<float>.Build.Dense(matrix.RowCount, 1);
// Create an iteration monitor which will keep track of iterative convergence
var monitor = new Iterator<float>(
new IterationCountStopCriterium<float>(MaximumIterations),
new ResidualStopCriterium<float>(ConvergenceBoundary),
new DivergenceStopCriterium<float>(),
new FailureStopCriterium<float>());
var solver = new TFQMR();
// Solve equation Ax = y
var x = matrix.SolveIterative(y, solver, monitor);
// Now compare the results
Assert.IsNotNull(x, "#02");
Assert.AreEqual(y.Count, x.Count, "#03");
// Back multiply the vector
var z = matrix.Multiply(x);
// Check that the solution converged
Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04");
// Now compare the vectors
Assert.LessOrEqual(Distance.Chebyshev(y, z), 2*ConvergenceBoundary);
}
示例10: DetermineStatusWithNegativeIterationNumberThrowsArgumentOutOfRangeException
public void DetermineStatusWithNegativeIterationNumberThrowsArgumentOutOfRangeException()
{
var criteria = new List<IIterationStopCriterium<float>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<float>(),
new ResidualStopCriterium()
};
var iterator = new Iterator<float>(criteria);
Assert.Throws<ArgumentOutOfRangeException>(() => iterator.DetermineStatus(
-1,
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 5),
DenseVector.Create(3, i => 6)));
}
示例11: ResetToPrecalculationState
public void ResetToPrecalculationState()
{
var criteria = new List<IIterationStopCriterium<float>>
{
new FailureStopCriterium(),
new DivergenceStopCriterium(),
new IterationCountStopCriterium<float>(1)
};
var iterator = new Iterator<float>(criteria);
// First step, nothing should happen.
iterator.DetermineStatus(
0,
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 4));
Assert.AreEqual(IterationStatus.Continue, iterator.Status, "Incorrect status");
iterator.Reset();
Assert.AreEqual(IterationStatus.Continue, iterator.Status, "Incorrect status");
Assert.AreEqual(IterationStatus.Continue, criteria[0].Status, "Incorrect status");
Assert.AreEqual(IterationStatus.Continue, criteria[1].Status, "Incorrect status");
Assert.AreEqual(IterationStatus.Continue, criteria[2].Status, "Incorrect status");
}
示例12: DetermineStatusWithoutStopCriteriaDoesNotThrow
public void DetermineStatusWithoutStopCriteriaDoesNotThrow()
{
var iterator = new Iterator<float>();
Assert.DoesNotThrow(() => iterator.DetermineStatus(
0,
DenseVector.Create(3, i => 4),
DenseVector.Create(3, i => 5),
DenseVector.Create(3, i => 6)));
}