diff --git a/src/Numerics/LinearAlgebra/Solvers/DelegateStopCriterion.cs b/src/Numerics/LinearAlgebra/Solvers/DelegateStopCriterion.cs new file mode 100644 index 00000000..60926f26 --- /dev/null +++ b/src/Numerics/LinearAlgebra/Solvers/DelegateStopCriterion.cs @@ -0,0 +1,95 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2014 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; + +namespace MathNet.Numerics.LinearAlgebra.Solvers +{ + /// + /// Stop criterion that delegates the status determination to a delegate. + /// + public class DelegateStopCriterion : IIterationStopCriterion + where T : struct, IEquatable, IFormattable + { + readonly Func, Vector, Vector, IterationStatus> _determine; + IterationStatus _status = IterationStatus.Continue; + + /// + /// Create a new instance of this criterion with a custom implementation. + /// + /// Custom implementation with the same signature and semantics as the DetermineStatus method. + public DelegateStopCriterion(Func, Vector, Vector, IterationStatus> determine) + { + _determine = determine; + } + + /// + /// Determines the status of the iterative calculation by delegating it to the provided delegate. + /// Result is set into Status field. + /// + /// The number of iterations that have passed so far. + /// The vector containing the current solution values. + /// The right hand side vector. + /// The vector containing the current residual vectors. + /// + /// The individual stop criteria may internally track the progress of the calculation based + /// on the invocation of this method. Therefore this method should only be called if the + /// calculation has moved forwards at least one step. + /// + public IterationStatus DetermineStatus(int iterationNumber, Vector solutionVector, Vector sourceVector, Vector residualVector) + { + return _status = _determine(iterationNumber, solutionVector, sourceVector, residualVector); + } + + /// + /// Gets the current calculation status. + /// + public IterationStatus Status + { + get { return _status; } + } + + /// + /// Resets the IIterationStopCriterion to the pre-calculation state. + /// + public void Reset() + { + _status = IterationStatus.Continue; + } + + /// + /// Clones this criterion and its settings. + /// + public IIterationStopCriterion Clone() + { + return new DelegateStopCriterion(_determine); + } + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 7889971d..1f75e256 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -98,6 +98,7 @@ +