diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs index d2701c99..0474e696 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs @@ -88,7 +88,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public BiCgStab() @@ -115,18 +115,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public BiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(Iterator iterator) : this(null, iterator) { } @@ -135,7 +135,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -149,19 +149,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -177,10 +177,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -460,19 +460,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs index 0ab0abd8..798ee066 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs @@ -44,6 +44,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers using Complex = Numerics.Complex; #else using Complex = System.Numerics.Complex; + #endif /// @@ -64,6 +65,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers public sealed class CompositeSolver : IIterativeSolver { #region Internal class - DoubleComparer + /// /// An IComparer used to compare double precision floating points. /// @@ -88,19 +90,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers { return x.CompareTo(y, 1); } - } + } + #endregion /// /// The default status used if the solver is not running. /// - private static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); + static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); /// /// The default status used if the solver is running. /// - private static readonly ICalculationStatus RunningStatus = new CalculationRunning(); - + static readonly ICalculationStatus RunningStatus = new CalculationRunning(); + #if PORTABLE private static readonly Dictionary>> SolverSetups = new Dictionary>>(); #else @@ -108,7 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The collection of iterative solver setups. Stored based on the /// ratio between the relative speed and relative accuracy. /// - private static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); + static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); #endif #region Solver information loading methods @@ -168,7 +171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Now load the assembly with an AssemblyName var assemblyName = new AssemblyName(assemblyFileName); var assembly = Assembly.Load(assemblyName.FullName); - + // Can't get this because we checked that the file exists. // FileNotFoundException --> Can't get this because we checked that the file exists. @@ -273,7 +276,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers { interfaceTypes.Clear(); interfaceTypes.AddRange(type.GetInterfaces()); - if (!interfaceTypes.Any(match => typeof(IIterativeSolverSetup).IsAssignableFrom(match))) + if (!interfaceTypes.Any(match => typeof (IIterativeSolverSetup).IsAssignableFrom(match))) { continue; } @@ -284,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers { // If something goes wrong we just ignore it and move on with the next type. // There should probably be a log somewhere indicating that something went wrong? - setup = (IIterativeSolverSetup)Activator.CreateInstance(type); + setup = (IIterativeSolverSetup) Activator.CreateInstance(type); } catch (ArgumentException) { @@ -324,7 +327,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } // Ok we want the solver, so store the object - var ratio = setup.SolutionSpeed / setup.Reliability; + var ratio = setup.SolutionSpeed/setup.Reliability; if (!SolverSetups.ContainsKey(ratio)) { SolverSetups.Add(ratio, new List>()); @@ -333,35 +336,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers var list = SolverSetups[ratio]; list.Add(setup); } - } + } #endregion /// /// The collection of solvers that will be used to /// - private readonly List> _solvers = new List>(); + readonly List> _solvers = new List>(); /// /// The status of the calculation. /// - private ICalculationStatus _status = NonRunningStatus; + ICalculationStatus _status = NonRunningStatus; /// /// The iterator that is used to control the iteration process. /// - private IIterator _iterator; + Iterator _iterator; /// /// A flag indicating if the solver has been stopped or not. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// The solver that is currently running. Reference is used to be able to stop the /// solver if the user cancels the solve process. /// - private IIterativeSolver _currentSolver; + IIterativeSolver _currentSolver; /// /// Initializes a new instance of the class with the default iterator. @@ -374,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class with the specified iterator. /// /// The iterator that will be used to control the iteration process. - public CompositeSolver(IIterator iterator) + public CompositeSolver(Iterator iterator) { _iterator = iterator; } @@ -383,7 +386,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Sets the IIterator that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -393,9 +396,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// public ICalculationStatus IterationResult { - get - { - return _status; + get + { + return _status; } } @@ -409,7 +412,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers { _hasBeenStopped = true; if (_currentSolver != null) - { + { _currentSolver.StopSolve(); } } @@ -501,7 +504,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers try { // Reset the iterator and pass it to the solver - _iterator.ResetToPrecalculationState(); + _iterator.Reset(); solver.SetIterator(_iterator); // Start the solver @@ -519,7 +522,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } // There was no fatal breakdown so check the status - if (_iterator.Status is CalculationConverged) + if (_iterator.HasConverged) { // We're done internalResult.CopyTo(result); @@ -529,7 +532,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.Status is CalculationStoppedWithoutConvergence) + if (_iterator.HasStoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -558,7 +561,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// Load solvers /// - private void LoadSolvers() + void LoadSolvers() { if (SolverSetups.Count == 0) { diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs index 4bd314b6..fabf3990 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs @@ -86,7 +86,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates the number of BiCGStab steps should be taken @@ -109,7 +109,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public GpBiCg() @@ -125,18 +125,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(Iterator iterator) : this(null, iterator) { } @@ -145,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -159,19 +159,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -225,10 +225,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -543,19 +543,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs index ca2dff4f..dbc74e20 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs @@ -51,16 +51,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// Creates a default iterator with all the objects. /// - /// A new object. - public static IIterator CreateDefault() + /// A new object. + public static Iterator CreateDefault() { - var iterator = new Iterator(); - iterator.Add(new FailureStopCriterium()); - iterator.Add(new DivergenceStopCriterium()); - iterator.Add(new IterationCountStopCriterium()); - iterator.Add(new ResidualStopCriterium()); - - return iterator; + return new Iterator( + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(), + new ResidualStopCriterium()); } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs index 8cd508b5..cd8acfec 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs @@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. @@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public MlkBiCgStab() @@ -129,18 +129,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(Iterator iterator) : this(null, iterator) { } @@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -163,19 +163,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -223,10 +223,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -726,19 +726,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs index 302f2b1b..f5e467c1 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs @@ -77,7 +77,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. @@ -88,7 +88,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public TFQMR() @@ -104,18 +104,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(Iterator iterator) : this(null, iterator) { } @@ -124,7 +124,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -138,19 +138,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -166,10 +166,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -315,7 +315,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (sigma.Real.AlmostEqual(0, 1) && sigma.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -388,7 +388,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (rho.Real.AlmostEqual(0, 1) && rho.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -450,19 +450,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs index ba8d17b2..dcaff85d 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs @@ -81,7 +81,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. @@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public BiCgStab() @@ -108,18 +108,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public BiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(Iterator iterator) : this(null, iterator) { } @@ -128,7 +128,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -142,19 +142,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -170,10 +170,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -453,19 +453,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs index 9cceea81..07145609 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs @@ -57,6 +57,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers public sealed class CompositeSolver : IIterativeSolver { #region Internal class - DoubleComparer + /// /// An IComparer used to compare double precision floating points. /// @@ -81,19 +82,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers { return x.CompareTo(y, 1); } - } + } + #endregion /// /// The default status used if the solver is not running. /// - private static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); + static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); /// /// The default status used if the solver is running. /// - private static readonly ICalculationStatus RunningStatus = new CalculationRunning(); - + static readonly ICalculationStatus RunningStatus = new CalculationRunning(); + #if PORTABLE private static readonly Dictionary>> SolverSetups = new Dictionary>>(); #else @@ -101,7 +103,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The collection of iterative solver setups. Stored based on the /// ratio between the relative speed and relative accuracy. /// - private static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); + static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); #endif #region Solver information loading methods @@ -161,7 +163,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Now load the assembly with an AssemblyName var assemblyName = new AssemblyName(assemblyFileName); var assembly = Assembly.Load(assemblyName.FullName); - + // Can't get this because we checked that the file exists. // FileNotFoundException --> Can't get this because we checked that the file exists. @@ -266,7 +268,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers { interfaceTypes.Clear(); interfaceTypes.AddRange(type.GetInterfaces()); - if (!interfaceTypes.Any(match => typeof(IIterativeSolverSetup).IsAssignableFrom(match))) + if (!interfaceTypes.Any(match => typeof (IIterativeSolverSetup).IsAssignableFrom(match))) { continue; } @@ -277,7 +279,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers { // If something goes wrong we just ignore it and move on with the next type. // There should probably be a log somewhere indicating that something went wrong? - setup = (IIterativeSolverSetup)Activator.CreateInstance(type); + setup = (IIterativeSolverSetup) Activator.CreateInstance(type); } catch (ArgumentException) { @@ -317,7 +319,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } // Ok we want the solver, so store the object - var ratio = setup.SolutionSpeed / setup.Reliability; + var ratio = setup.SolutionSpeed/setup.Reliability; if (!SolverSetups.ContainsKey(ratio)) { SolverSetups.Add(ratio, new List>()); @@ -326,35 +328,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers var list = SolverSetups[ratio]; list.Add(setup); } - } + } #endregion /// /// The collection of solvers that will be used to /// - private readonly List> _solvers = new List>(); + readonly List> _solvers = new List>(); /// /// The status of the calculation. /// - private ICalculationStatus _status = NonRunningStatus; + ICalculationStatus _status = NonRunningStatus; /// /// The iterator that is used to control the iteration process. /// - private IIterator _iterator; + Iterator _iterator; /// /// A flag indicating if the solver has been stopped or not. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// The solver that is currently running. Reference is used to be able to stop the /// solver if the user cancels the solve process. /// - private IIterativeSolver _currentSolver; + IIterativeSolver _currentSolver; /// /// Initializes a new instance of the class with the default iterator. @@ -367,7 +369,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class with the specified iterator. /// /// The iterator that will be used to control the iteration process. - public CompositeSolver(IIterator iterator) + public CompositeSolver(Iterator iterator) { _iterator = iterator; } @@ -376,7 +378,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Sets the IIterator that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -386,9 +388,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// public ICalculationStatus IterationResult { - get - { - return _status; + get + { + return _status; } } @@ -402,7 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers { _hasBeenStopped = true; if (_currentSolver != null) - { + { _currentSolver.StopSolve(); } } @@ -494,7 +496,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers try { // Reset the iterator and pass it to the solver - _iterator.ResetToPrecalculationState(); + _iterator.Reset(); solver.SetIterator(_iterator); // Start the solver @@ -512,7 +514,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } // There was no fatal breakdown so check the status - if (_iterator.Status is CalculationConverged) + if (_iterator.HasConverged) { // We're done internalResult.CopyTo(result); @@ -522,7 +524,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.Status is CalculationStoppedWithoutConvergence) + if (_iterator.HasStoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -551,7 +553,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// Load solvers /// - private void LoadSolvers() + void LoadSolvers() { if (SolverSetups.Count == 0) { diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs index dacaebd8..fdbd57b7 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs @@ -79,7 +79,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates the number of BiCGStab steps should be taken @@ -102,7 +102,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public GpBiCg() @@ -118,18 +118,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(Iterator iterator) : this(null, iterator) { } @@ -138,7 +138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -152,19 +152,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -218,10 +218,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -541,19 +541,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs index a1bd9630..661704b0 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs @@ -46,16 +46,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// Creates a default iterator with all the objects. /// - /// A new object. - public static IIterator CreateDefault() + /// A new object. + public static Iterator CreateDefault() { - var iterator = new Iterator(); - iterator.Add(new FailureStopCriterium()); - iterator.Add(new DivergenceStopCriterium()); - iterator.Add(new IterationCountStopCriterium()); - iterator.Add(new ResidualStopCriterium()); - - return iterator; + return new Iterator( + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(), + new ResidualStopCriterium()); } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs index aeb4832d..9c5b4c3c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs @@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. @@ -105,7 +105,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public MlkBiCgStab() @@ -121,18 +121,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(Iterator iterator) : this(null, iterator) { } @@ -141,7 +141,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -155,19 +155,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -215,10 +215,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -723,19 +723,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs index d09398f5..2f206bd0 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs @@ -69,7 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. @@ -80,7 +80,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public TFQMR() @@ -96,18 +96,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(Iterator iterator) : this(null, iterator) { } @@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -130,19 +130,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -158,10 +158,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -312,7 +312,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (sigma.Real.AlmostEqual(0, 1) && sigma.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -385,7 +385,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (rho.Real.AlmostEqual(0, 1) && rho.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -447,19 +447,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs index c683f00d..9f0b1c5b 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs @@ -70,29 +70,29 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to , in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; - + bool _hasBeenStopped; + /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public BiCgStab() : this(null, null) @@ -107,18 +107,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public BiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(Iterator iterator) : this(null, iterator) { } @@ -127,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -141,19 +141,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -169,10 +169,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -182,9 +182,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// public ICalculationStatus IterationResult { - get - { - return (_iterator != null) ? _iterator.Status : DefaultStatus; + get + { + return (_iterator != null) ? _iterator.Status : DefaultStatus; } } @@ -274,9 +274,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { _preconditioner = new UnitPreconditioner(); } - + _preconditioner.Initialize(matrix); - + // Compute r_0 = b - Ax_0 for some initial guess x_0 // In this case we take x_0 = vector // This is basically a SAXPY so it could be made a lot faster @@ -285,7 +285,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Choose r~ (for example, r~ = r_0) var tempResiduals = residuals.Clone(); - + // create seven temporary vectors needed to hold temporary // coefficients. All vectors are mangled in each iteration. // These are defined here to prevent stressing the garbage collector @@ -321,7 +321,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (iterationNumber != 0) { // beta_(i-1) = (rho_(i-1)/rho_(i-2))(alpha_(i-1)/omega(i-1)) - var beta = (currentRho / oldRho) * (alpha / omega); + var beta = (currentRho/oldRho)*(alpha/omega); // p_i = r_(i-1) + beta_(i-1)(p_(i-1) - omega_(i-1) * nu_(i-1)) nu.Multiply(-omega, temp); @@ -340,12 +340,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // SOLVE Mp~ = p_i // M = preconditioner _preconditioner.Approximate(vecP, vecPdash); - + // nu_i = Ap~ matrix.Multiply(vecPdash, nu); // alpha_i = rho_(i-1)/ (r~^T nu_i) = rho / dotproduct(r~ and nu_i) - alpha = currentRho * 1 / tempResiduals.DotProduct(nu); + alpha = currentRho*1/tempResiduals.DotProduct(nu); // s = r_(i-1) - alpha_i nu_i nu.Multiply(-alpha, temp); @@ -389,7 +389,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers matrix.Multiply(vecSdash, temp); // omega_i = temp^T s / temp^T temp - omega = temp.DotProduct(vecS) / temp.DotProduct(temp); + omega = temp.DotProduct(vecS)/temp.DotProduct(temp); // x_i = x_(i-1) + alpha_i p^ + omega_i s^ temp.Multiply(-omega, residuals); @@ -403,7 +403,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers vecPdash.Multiply(alpha, temp); result.Add(temp, temp2); temp2.CopyTo(result); - + // for continuation it is necessary that omega_i != 0.0 // If omega is only 1 ULP from zero then we fail. if (omega.AlmostEqual(0, 1)) @@ -433,11 +433,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Residual values in . /// Instance of the x. /// Instance of the b. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); - + // Do not use residual = residual.Negate() because it creates another object residual.Multiply(-1, residual); @@ -453,21 +453,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs index c97f0a76..525e476a 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs @@ -57,6 +57,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers public sealed class CompositeSolver : IIterativeSolver { #region Internal class - DoubleComparer + /// /// An IComparer used to compare double precision floating points. /// @@ -78,19 +79,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { return x.CompareTo(y, 1); } - } + } + #endregion /// /// The default status used if the solver is not running. /// - private static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); + static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); /// /// The default status used if the solver is running. /// - private static readonly ICalculationStatus RunningStatus = new CalculationRunning(); - + static readonly ICalculationStatus RunningStatus = new CalculationRunning(); + #if PORTABLE private static readonly Dictionary>> SolverSetups = new Dictionary>>(); #else @@ -98,7 +100,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The collection of iterative solver setups. Stored based on the /// ratio between the relative speed and relative accuracy. /// - private static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); + static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); #endif #region Solver information loading methods @@ -158,7 +160,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Now load the assembly with an AssemblyName var assemblyName = new AssemblyName(assemblyFileName); var assembly = Assembly.Load(assemblyName.FullName); - + // Can't get this because we checked that the file exists. // FileNotFoundException --> Can't get this because we checked that the file exists. @@ -263,7 +265,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { interfaceTypes.Clear(); interfaceTypes.AddRange(type.GetInterfaces()); - if (!interfaceTypes.Any(match => typeof(IIterativeSolverSetup).IsAssignableFrom(match))) + if (!interfaceTypes.Any(match => typeof (IIterativeSolverSetup).IsAssignableFrom(match))) { continue; } @@ -274,7 +276,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { // If something goes wrong we just ignore it and move on with the next type. // There should probably be a log somewhere indicating that something went wrong? - setup = (IIterativeSolverSetup)Activator.CreateInstance(type); + setup = (IIterativeSolverSetup) Activator.CreateInstance(type); } catch (ArgumentException) { @@ -314,7 +316,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } // Ok we want the solver, so store the object - var ratio = setup.SolutionSpeed / setup.Reliability; + var ratio = setup.SolutionSpeed/setup.Reliability; if (!SolverSetups.ContainsKey(ratio)) { SolverSetups.Add(ratio, new List>()); @@ -323,35 +325,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers var list = SolverSetups[ratio]; list.Add(setup); } - } + } #endregion /// /// The collection of solvers that will be used to /// - private readonly List> _solvers = new List>(); + readonly List> _solvers = new List>(); /// /// The status of the calculation. /// - private ICalculationStatus _status = NonRunningStatus; + ICalculationStatus _status = NonRunningStatus; /// /// The iterator that is used to control the iteration process. /// - private IIterator _iterator; + Iterator _iterator; /// /// A flag indicating if the solver has been stopped or not. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// The solver that is currently running. Reference is used to be able to stop the /// solver if the user cancels the solve process. /// - private IIterativeSolver _currentSolver; + IIterativeSolver _currentSolver; /// /// Initializes a new instance of the class with the default iterator. @@ -364,16 +366,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Initializes a new instance of the class with the specified iterator. /// /// The iterator that will be used to control the iteration process. - public CompositeSolver(IIterator iterator) + public CompositeSolver(Iterator iterator) { _iterator = iterator; } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -383,9 +385,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// public ICalculationStatus IterationResult { - get - { - return _status; + get + { + return _status; } } @@ -399,7 +401,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { _hasBeenStopped = true; if (_currentSolver != null) - { + { _currentSolver.StopSolve(); } } @@ -491,7 +493,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers try { // Reset the iterator and pass it to the solver - _iterator.ResetToPrecalculationState(); + _iterator.Reset(); solver.SetIterator(_iterator); // Start the solver @@ -509,7 +511,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } // There was no fatal breakdown so check the status - if (_iterator.Status is CalculationConverged) + if (_iterator.HasConverged) { // We're done internalResult.CopyTo(result); @@ -519,7 +521,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.Status is CalculationStoppedWithoutConvergence) + if (_iterator.HasStoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -548,7 +550,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// Load solvers /// - private void LoadSolvers() + void LoadSolvers() { if (SolverSetups.Count == 0) { diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs index b28e12db..9409d225 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs @@ -68,41 +68,41 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to null, in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// Indicates the number of BiCGStab steps should be taken /// before switching. /// - private int _numberOfBiCgStabSteps = 1; + int _numberOfBiCgStabSteps = 1; /// /// Indicates the number of GPBiCG steps should be taken /// before switching. /// - private int _numberOfGpbiCgSteps = 4; + int _numberOfGpbiCgSteps = 4; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public GpBiCg() : this(null, null) @@ -117,18 +117,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(Iterator iterator) : this(null, iterator) { } @@ -137,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -151,19 +151,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -187,7 +187,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw new ArgumentOutOfRangeException("value"); } - _numberOfBiCgStabSteps = value; + _numberOfBiCgStabSteps = value; } } @@ -223,10 +223,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -236,7 +236,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// public ICalculationStatus IterationResult { - get + get { return (_iterator != null) ? _iterator.Status : DefaultStatus; } @@ -329,7 +329,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { _preconditioner = new UnitPreconditioner(); } - + _preconditioner.Initialize(matrix); // x_0 is initial guess @@ -384,7 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers matrix.Multiply(temp, s); // alpha_k = (r*_0 * r_k) / (r*_0 * s_k) - var alpha = rdash.DotProduct(residuals) / rdash.DotProduct(s); + var alpha = rdash.DotProduct(residuals)/rdash.DotProduct(s); // y_k = t_(k-1) - r_k - alpha_k * w_(k-1) + alpha_k s_k s.Subtract(w, temp); @@ -400,14 +400,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // t_k = r_k - alpha_k s_k s.Multiply(-alpha, temp2); residuals.Add(temp2, t); - + // Solve M d_k = t_k _preconditioner.Approximate(t, temp); // c_k = A d_k matrix.Multiply(temp, c); var cdot = c.DotProduct(c); - + // cDot can only be zero if c is a zero vector // We'll set cDot to 1 if it is zero to prevent NaN's // Note that the calculation should continue fine because @@ -427,7 +427,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (((_numberOfBiCgStabSteps == 0) && (iterationNumber == 0)) || ShouldRunBiCgStabSteps(iterationNumber)) { // sigma_k = (c_k * t_k) / (c_k * c_k) - sigma = ctdot / cdot; + sigma = ctdot/cdot; // eta_k = 0 eta = 0; @@ -448,13 +448,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers var ytdot = y.DotProduct(t); var cydot = c.DotProduct(y); - var denom = (cdot * ydot) - (cydot * cydot); + var denom = (cdot*ydot) - (cydot*cydot); // sigma_k = ((y_k * y_k)(c_k * t_k) - (y_k * t_k)(c_k * y_k)) / ((c_k * c_k)(y_k * y_k) - (y_k * c_k)(c_k * y_k)) - sigma = ((ydot * ctdot) - (ytdot * cydot)) / denom; + sigma = ((ydot*ctdot) - (ytdot*cydot))/denom; // eta_k = ((c_k * c_k)(y_k * t_k) - (y_k * c_k)(c_k * t_k)) / ((c_k * c_k)(y_k * y_k) - (y_k * c_k)(c_k * y_k)) - eta = ((cdot * ytdot) - (cydot * ctdot)) / denom; + eta = ((cdot*ytdot) - (cydot*ctdot))/denom; } // u_k = sigma_k s_k + eta_k (t_(k-1) - r_k + beta_(k-1) u_(k-1)) @@ -482,7 +482,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers p.Multiply(alpha, temp2); xtemp.Add(temp2, temp3); temp3.CopyTo(xtemp); - + xtemp.Add(z, temp3); temp3.CopyTo(xtemp); @@ -500,7 +500,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // beta_k = alpha_k / sigma_k * (r*_0 * r_(k+1)) / (r*_0 * r_k) // But first we check if there is a possible NaN. If so just reset beta to zero. - beta = (!sigma.AlmostEqual(0, 1)) ? alpha / sigma * rdash.DotProduct(residuals) / rdash.DotProduct(t0) : 0; + beta = (!sigma.AlmostEqual(0, 1)) ? alpha/sigma*rdash.DotProduct(residuals)/rdash.DotProduct(t0) : 0; // w_k = c_k + beta_k s_k s.Multiply(beta, temp2); @@ -529,7 +529,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Residual values in . /// Instance of the x. /// Instance of the b. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); @@ -547,33 +547,31 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } - + /// /// Decide if to do steps with BiCgStab /// /// Number of iteration /// true if yes, otherwise false - private bool ShouldRunBiCgStabSteps(int iterationNumber) + bool ShouldRunBiCgStabSteps(int iterationNumber) { // Run the first steps as BiCGStab // The number of steps past a whole iteration set - var difference = iterationNumber % (_numberOfBiCgStabSteps + _numberOfGpbiCgSteps); + var difference = iterationNumber%(_numberOfBiCgStabSteps + _numberOfGpbiCgSteps); // Do steps with BiCGStab if: // - The difference is zero or more (i.e. we have done zero or more complete cycles) diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs index 05fc6260..b2bd473f 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs @@ -44,16 +44,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// Creates a default iterator with all the objects. /// - /// A new object. - public static IIterator CreateDefault() + /// A new object. + public static Iterator CreateDefault() { - var iterator = new Iterator(); - iterator.Add(new FailureStopCriterium()); - iterator.Add(new DivergenceStopCriterium()); - iterator.Add(new IterationCountStopCriterium()); - iterator.Add(new ResidualStopCriterium()); - - return iterator; + return new Iterator( + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(), + new ResidualStopCriterium()); } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs index b31a58a7..c703af30 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs @@ -67,45 +67,45 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// The default number of starting vectors. /// - private const int DefaultNumberOfStartingVectors = 50; - + const int DefaultNumberOfStartingVectors = 50; + /// /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to , in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// - private IList> _startingVectors; + IList> _startingVectors; /// /// The number of starting vectors used by the algorithm /// - private int _numberOfStartingVectors = DefaultNumberOfStartingVectors; + int _numberOfStartingVectors = DefaultNumberOfStartingVectors; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public MlkBiCgStab() : this(null, null) @@ -120,18 +120,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(Iterator iterator) : this(null, iterator) { } @@ -140,7 +140,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -154,19 +154,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -217,10 +217,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -349,7 +349,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers { _preconditioner = new UnitPreconditioner(); } - + _preconditioner.Initialize(matrix); // Choose an initial guess x_0 @@ -403,7 +403,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers var zw = new DenseVector(residuals.Count); var d = CreateVectorArray(_startingVectors.Count, residuals.Count); - + // g_0 = r_0 var g = CreateVectorArray(_startingVectors.Count, residuals.Count); residuals.CopyTo(g[k - 1]); @@ -428,7 +428,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } // alpha_(jk+1) = q^T_1 r_((j-1)k+k) / c_((j-1)k+k) - var alpha = _startingVectors[0].DotProduct(residuals) / c[k - 1]; + var alpha = _startingVectors[0].DotProduct(residuals)/c[k - 1]; // u_(jk+1) = r_((j-1)k+k) - alpha_(jk+1) w_((j-1)k+k) w[k - 1].Multiply(-alpha, temp); @@ -450,7 +450,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers rho = 1.0; } - rho = -u.DotProduct(temp) / rho; + rho = -u.DotProduct(temp)/rho; // r_(jk+1) = rho_(j+1) A u~_(jk+1) + u_(jk+1) u.CopyTo(residuals); @@ -468,7 +468,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers gtemp.Multiply(alpha, gtemp); xtemp.Add(gtemp, temp2); temp2.CopyTo(xtemp); - + // Check convergence and stop if we are converged. if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) { @@ -503,7 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers for (var s = i; s < k - 1; s++) { // beta^(jk+i)_((j-1)k+s) = -q^t_(s+1) z_d / c_((j-1)k+s) - beta = -_startingVectors[s + 1].DotProduct(zd) / c[s]; + beta = -_startingVectors[s + 1].DotProduct(zd)/c[s]; // z_d = z_d + beta^(jk+i)_((j-1)k+s) d_((j-1)k+s) d[s].Multiply(beta, temp); @@ -522,7 +522,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } } - beta = rho * c[k - 1]; + beta = rho*c[k - 1]; if (beta.AlmostEqual(0, 1)) { throw new Exception("Iterative solver experience a numerical break down"); @@ -531,7 +531,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // beta^(jk+i)_((j-1)k+k) = -(q^T_1 (r_(jk+1) + rho_(j+1) z_w)) / (rho_(j+1) c_((j-1)k+k)) zw.Multiply(rho, temp2); residuals.Add(temp2, temp); - beta = -_startingVectors[0].DotProduct(temp) / beta; + beta = -_startingVectors[0].DotProduct(temp)/beta; // z_g = z_g + beta^(jk+i)_((j-1)k+k) g_((j-1)k+k) g[k - 1].Multiply(beta, temp); @@ -551,7 +551,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers for (var s = 0; s < i - 1; s++) { // beta^(jk+i)_(jk+s) = -q^T_s+1 z_d / c_(jk+s) - beta = -_startingVectors[s + 1].DotProduct(zd) / c[s]; + beta = -_startingVectors[s + 1].DotProduct(zd)/c[s]; // z_d = z_d + beta^(jk+i)_(jk+s) * d_(jk+s) d[s].Multiply(beta, temp); @@ -581,7 +581,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } // alpha_(jk+i+1) = q^T_(i+1) u_(jk+i) / c_(jk+i) - alpha = _startingVectors[i + 1].DotProduct(u) / c[i]; + alpha = _startingVectors[i + 1].DotProduct(u)/c[i]; // u_(jk+i+1) = u_(jk+i) - alpha_(jk+i+1) d_(jk+i) d[i].Multiply(-alpha, temp); @@ -592,7 +592,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner.Approximate(g[i], gtemp); // x_(jk+i+1) = x_(jk+i) + rho_(j+1) alpha_(jk+i+1) g~_(jk+i) - gtemp.Multiply(rho * alpha, temp); + gtemp.Multiply(rho*alpha, temp); xtemp.Add(temp, temp2); temp2.CopyTo(xtemp); @@ -600,7 +600,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers matrix.Multiply(gtemp, w[i]); // r_(jk+i+1) = r_(jk+i) - rho_(j+1) alpha_(jk+i+1) w_(jk+i) - w[i].Multiply(-rho * alpha, temp); + w[i].Multiply(-rho*alpha, temp); residuals.Add(temp, temp2); temp2.CopyTo(residuals); @@ -627,7 +627,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Maximum number /// Number of variables /// Number of starting vectors to create - private static int NumberOfStartingVectorsToCreate(int maximumNumberOfStartingVectors, int numberOfVariables) + static int NumberOfStartingVectorsToCreate(int maximumNumberOfStartingVectors, int numberOfVariables) { // Create no more starting vectors than the size of the problem - 1 return Math.Min(maximumNumberOfStartingVectors, (numberOfVariables - 1)); @@ -644,7 +644,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// the is smaller than /// the . /// - private static IList> CreateStartingVectors(int maximumNumberOfStartingVectors, int numberOfVariables) + static IList> CreateStartingVectors(int maximumNumberOfStartingVectors, int numberOfVariables) { // Create no more starting vectors than the size of the problem - 1 // Get random values and then orthogonalize them with @@ -659,7 +659,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers for (var i = 0; i < matrix.ColumnCount; i++) { var samples = distribution.Samples().Take(matrix.RowCount).ToArray(); - + // Set the column matrix.SetColumn(i, samples); } @@ -673,9 +673,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers for (var i = 0; i < orthogonalMatrix.ColumnCount; i++) { result.Add(orthogonalMatrix.Column(i)); - + // Normalize the result vector - result[i].Multiply(1 / result[i].L2Norm(), result[i]); + result[i].Multiply(1/result[i].L2Norm(), result[i]); } return result; @@ -687,7 +687,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Number of vectors /// Size of each vector /// Array of random vectors - private static Vector[] CreateVectorArray(int arraySize, int vectorSize) + static Vector[] CreateVectorArray(int arraySize, int vectorSize) { var result = new Vector[arraySize]; for (var i = 0; i < result.Length; i++) @@ -705,7 +705,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Residual data. /// x data. /// b data. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); @@ -723,21 +723,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs index cf65e5a4..df93300d 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs @@ -29,7 +29,6 @@ // using System; -using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners; using MathNet.Numerics.LinearAlgebra.Solvers; using MathNet.Numerics.LinearAlgebra.Solvers.Status; using MathNet.Numerics.Properties; @@ -59,29 +58,29 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to , in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public TFQMR() : this(null, null) @@ -96,18 +95,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(Iterator iterator) : this(null, iterator) { } @@ -116,7 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -130,19 +129,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -158,10 +157,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -171,9 +170,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// public ICalculationStatus IterationResult { - get - { - return (_iterator != null) ? _iterator.Status : DefaultStatus; + get + { + return (_iterator != null) ? _iterator.Status : DefaultStatus; } } @@ -296,7 +295,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Calculate the initial values for v // M temp = yEven _preconditioner.Approximate(yeven, temp); - + // v = A temp matrix.Multiply(temp, v); @@ -315,12 +314,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (sigma.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } // alpha = rho / sigma - alpha = rho / sigma; + alpha = rho/sigma; // yOdd = yEven - alpha * v v.Multiply(-alpha, temp1); @@ -345,18 +344,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers temp2.CopyTo(pseudoResiduals); // d = yOdd + theta * theta * eta / alpha * d - d.Multiply(theta * theta * eta / alpha, temp); + d.Multiply(theta*theta*eta/alpha, temp); yinternal.Add(temp, d); // theta = ||pseudoResiduals||_2 / tau - theta = pseudoResiduals.L2Norm() / tau; - var c = 1 / Math.Sqrt(1 + (theta * theta)); + theta = pseudoResiduals.L2Norm()/tau; + var c = 1/Math.Sqrt(1 + (theta*theta)); // tau = tau * theta * c - tau *= theta * c; + tau *= theta*c; // eta = c^2 * alpha - eta = c * c * alpha; + eta = c*c*alpha; // x = x + eta * d d.Multiply(eta, temp1); @@ -388,12 +387,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (rho.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } var rhoNew = pseudoResiduals.DotProduct(r); - var beta = rhoNew / rho; + var beta = rhoNew/rho; // Update rho for the next loop rho = rhoNew; @@ -430,7 +429,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Residual values in . /// Instance of the x. /// Instance of the b. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); @@ -448,21 +447,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// @@ -470,9 +467,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// Number to check /// true if even, otherwise false - private static bool IsEven(int number) + static bool IsEven(int number) { - return number % 2 == 0; + return number%2 == 0; } /// diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs index 88b3324c..ea9cc357 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs @@ -70,29 +70,29 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to , in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; - + bool _hasBeenStopped; + /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public BiCgStab() : this(null, null) @@ -107,18 +107,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public BiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(Iterator iterator) : this(null, iterator) { } @@ -127,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -141,19 +141,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public BiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -169,10 +169,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -182,9 +182,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// public ICalculationStatus IterationResult { - get - { - return (_iterator != null) ? _iterator.Status : DefaultStatus; + get + { + return (_iterator != null) ? _iterator.Status : DefaultStatus; } } @@ -274,9 +274,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { _preconditioner = new UnitPreconditioner(); } - + _preconditioner.Initialize(matrix); - + // Compute r_0 = b - Ax_0 for some initial guess x_0 // In this case we take x_0 = vector // This is basically a SAXPY so it could be made a lot faster @@ -321,7 +321,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (iterationNumber != 0) { // beta_(i-1) = (rho_(i-1)/rho_(i-2))(alpha_(i-1)/omega(i-1)) - var beta = (currentRho / oldRho) * (alpha / omega); + var beta = (currentRho/oldRho)*(alpha/omega); // p_i = r_(i-1) + beta_(i-1)(p_(i-1) - omega_(i-1) * nu_(i-1)) nu.Multiply(-omega, temp); @@ -340,12 +340,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // SOLVE Mp~ = p_i // M = preconditioner _preconditioner.Approximate(vecP, vecPdash); - + // nu_i = Ap~ matrix.Multiply(vecPdash, nu); // alpha_i = rho_(i-1)/ (r~^T nu_i) = rho / dotproduct(r~ and nu_i) - alpha = currentRho * 1 / tempResiduals.DotProduct(nu); + alpha = currentRho*1/tempResiduals.DotProduct(nu); // s = r_(i-1) - alpha_i nu_i nu.Multiply(-alpha, temp); @@ -389,7 +389,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers matrix.Multiply(vecSdash, temp); // omega_i = temp^T s / temp^T temp - omega = temp.DotProduct(vecS) / temp.DotProduct(temp); + omega = temp.DotProduct(vecS)/temp.DotProduct(temp); // x_i = x_(i-1) + alpha_i p^ + omega_i s^ temp.Multiply(-omega, residuals); @@ -433,11 +433,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Residual values in . /// Instance of the x. /// Instance of the b. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); - + // Do not use residual = residual.Negate() because it creates another object residual.Multiply(-1, residual); @@ -453,21 +453,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs index cc8d9c33..3d63e4df 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs @@ -57,6 +57,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers public sealed class CompositeSolver : IIterativeSolver { #region Internal class - DoubleComparer + /// /// An IComparer used to compare double precision floating points. /// @@ -81,19 +82,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { return x.CompareTo(y, 1); } - } + } + #endregion /// /// The default status used if the solver is not running. /// - private static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); + static readonly ICalculationStatus NonRunningStatus = new CalculationIndetermined(); /// /// The default status used if the solver is running. /// - private static readonly ICalculationStatus RunningStatus = new CalculationRunning(); - + static readonly ICalculationStatus RunningStatus = new CalculationRunning(); + #if PORTABLE private static readonly Dictionary>> SolverSetups = new Dictionary>>(); #else @@ -101,7 +103,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The collection of iterative solver setups. Stored based on the /// ratio between the relative speed and relative accuracy. /// - private static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); + static readonly SortedList>> SolverSetups = new SortedList>>(new DoubleComparer()); #endif #region Solver information loading methods @@ -161,7 +163,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Now load the assembly with an AssemblyName var assemblyName = new AssemblyName(assemblyFileName); var assembly = Assembly.Load(assemblyName.FullName); - + // Can't get this because we checked that the file exists. // FileNotFoundException --> Can't get this because we checked that the file exists. @@ -266,7 +268,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { interfaceTypes.Clear(); interfaceTypes.AddRange(type.GetInterfaces()); - if (!interfaceTypes.Any(match => typeof(IIterativeSolverSetup).IsAssignableFrom(match))) + if (!interfaceTypes.Any(match => typeof (IIterativeSolverSetup).IsAssignableFrom(match))) { continue; } @@ -277,7 +279,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { // If something goes wrong we just ignore it and move on with the next type. // There should probably be a log somewhere indicating that something went wrong? - setup = (IIterativeSolverSetup)Activator.CreateInstance(type); + setup = (IIterativeSolverSetup) Activator.CreateInstance(type); } catch (ArgumentException) { @@ -317,7 +319,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } // Ok we want the solver, so store the object - var ratio = setup.SolutionSpeed / setup.Reliability; + var ratio = setup.SolutionSpeed/setup.Reliability; if (!SolverSetups.ContainsKey(ratio)) { SolverSetups.Add(ratio, new List>()); @@ -326,35 +328,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers var list = SolverSetups[ratio]; list.Add(setup); } - } + } #endregion /// /// The collection of solvers that will be used to /// - private readonly List> _solvers = new List>(); + readonly List> _solvers = new List>(); /// /// The status of the calculation. /// - private ICalculationStatus _status = NonRunningStatus; + ICalculationStatus _status = NonRunningStatus; /// /// The iterator that is used to control the iteration process. /// - private IIterator _iterator; + Iterator _iterator; /// /// A flag indicating if the solver has been stopped or not. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// The solver that is currently running. Reference is used to be able to stop the /// solver if the user cancels the solve process. /// - private IIterativeSolver _currentSolver; + IIterativeSolver _currentSolver; /// /// Initializes a new instance of the class with the default iterator. @@ -367,16 +369,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class with the specified iterator. /// /// The iterator that will be used to control the iteration process. - public CompositeSolver(IIterator iterator) + public CompositeSolver(Iterator iterator) { _iterator = iterator; } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -386,9 +388,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// public ICalculationStatus IterationResult { - get - { - return _status; + get + { + return _status; } } @@ -402,7 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { _hasBeenStopped = true; if (_currentSolver != null) - { + { _currentSolver.StopSolve(); } } @@ -494,7 +496,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers try { // Reset the iterator and pass it to the solver - _iterator.ResetToPrecalculationState(); + _iterator.Reset(); solver.SetIterator(_iterator); // Start the solver @@ -512,7 +514,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } // There was no fatal breakdown so check the status - if (_iterator.Status is CalculationConverged) + if (_iterator.HasConverged) { // We're done internalResult.CopyTo(result); @@ -522,7 +524,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.Status is CalculationStoppedWithoutConvergence) + if (_iterator.HasStoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -551,7 +553,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// Load solvers /// - private void LoadSolvers() + void LoadSolvers() { if (SolverSetups.Count == 0) { diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs index 9caa11ad..f81a7253 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs @@ -79,7 +79,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates the number of BiCGStab steps should be taken @@ -102,7 +102,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public GpBiCg() @@ -118,18 +118,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(Iterator iterator) : this(null, iterator) { } @@ -138,7 +138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -152,19 +152,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -218,10 +218,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -541,19 +541,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs index e4352a6e..50ee9a51 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs @@ -44,16 +44,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// Creates a default iterator with all the objects. /// - /// A new object. - public static IIterator CreateDefault() + /// A new object. + public static Iterator CreateDefault() { - var iterator = new Iterator(); - iterator.Add(new FailureStopCriterium()); - iterator.Add(new DivergenceStopCriterium()); - iterator.Add(new IterationCountStopCriterium()); - iterator.Add(new ResidualStopCriterium()); - - return iterator; + return new Iterator( + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(), + new ResidualStopCriterium()); } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs index 558977a6..d61b6ba7 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs @@ -66,45 +66,45 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// The default number of starting vectors. /// - private const int DefaultNumberOfStartingVectors = 50; - + const int DefaultNumberOfStartingVectors = 50; + /// /// The status used if there is no status, i.e. the solver hasn't run yet and there is no /// iterator. /// - private static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); + static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); /// /// The preconditioner that will be used. Can be set to , in which case the default /// pre-conditioner will be used. /// - private IPreConditioner _preconditioner; + IPreConditioner _preconditioner; /// /// The iterative process controller. /// - private IIterator _iterator; + Iterator _iterator; /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// - private IList> _startingVectors; + IList> _startingVectors; /// /// The number of starting vectors used by the algorithm /// - private int _numberOfStartingVectors = DefaultNumberOfStartingVectors; + int _numberOfStartingVectors = DefaultNumberOfStartingVectors; /// /// Indicates if the user has stopped the solver. /// - private bool _hasBeenStopped; + bool _hasBeenStopped; /// /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public MlkBiCgStab() : this(null, null) @@ -119,18 +119,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(Iterator iterator) : this(null, iterator) { } @@ -139,7 +139,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -153,19 +153,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -216,10 +216,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -348,7 +348,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers { _preconditioner = new UnitPreconditioner(); } - + _preconditioner.Initialize(matrix); // Choose an initial guess x_0 @@ -402,7 +402,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers var zw = new DenseVector(residuals.Count); var d = CreateVectorArray(_startingVectors.Count, residuals.Count); - + // g_0 = r_0 var g = CreateVectorArray(_startingVectors.Count, residuals.Count); residuals.CopyTo(g[k - 1]); @@ -427,7 +427,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } // alpha_(jk+1) = q^T_1 r_((j-1)k+k) / c_((j-1)k+k) - var alpha = _startingVectors[0].DotProduct(residuals) / c[k - 1]; + var alpha = _startingVectors[0].DotProduct(residuals)/c[k - 1]; // u_(jk+1) = r_((j-1)k+k) - alpha_(jk+1) w_((j-1)k+k) w[k - 1].Multiply(-alpha, temp); @@ -449,7 +449,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers rho = 1.0f; } - rho = -u.DotProduct(temp) / rho; + rho = -u.DotProduct(temp)/rho; // r_(jk+1) = rho_(j+1) A u~_(jk+1) + u_(jk+1) u.CopyTo(residuals); @@ -502,7 +502,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers for (var s = i; s < k - 1; s++) { // beta^(jk+i)_((j-1)k+s) = -q^t_(s+1) z_d / c_((j-1)k+s) - beta = -_startingVectors[s + 1].DotProduct(zd) / c[s]; + beta = -_startingVectors[s + 1].DotProduct(zd)/c[s]; // z_d = z_d + beta^(jk+i)_((j-1)k+s) d_((j-1)k+s) d[s].Multiply(beta, temp); @@ -521,7 +521,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } } - beta = rho * c[k - 1]; + beta = rho*c[k - 1]; if (beta.AlmostEqual(0, 1)) { throw new Exception("Iterative solver experience a numerical break down"); @@ -530,7 +530,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // beta^(jk+i)_((j-1)k+k) = -(q^T_1 (r_(jk+1) + rho_(j+1) z_w)) / (rho_(j+1) c_((j-1)k+k)) zw.Multiply(rho, temp2); residuals.Add(temp2, temp); - beta = -_startingVectors[0].DotProduct(temp) / beta; + beta = -_startingVectors[0].DotProduct(temp)/beta; // z_g = z_g + beta^(jk+i)_((j-1)k+k) g_((j-1)k+k) g[k - 1].Multiply(beta, temp); @@ -550,7 +550,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers for (var s = 0; s < i - 1; s++) { // beta^(jk+i)_(jk+s) = -q^T_s+1 z_d / c_(jk+s) - beta = -_startingVectors[s + 1].DotProduct(zd) / c[s]; + beta = -_startingVectors[s + 1].DotProduct(zd)/c[s]; // z_d = z_d + beta^(jk+i)_(jk+s) * d_(jk+s) d[s].Multiply(beta, temp); @@ -580,7 +580,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } // alpha_(jk+i+1) = q^T_(i+1) u_(jk+i) / c_(jk+i) - alpha = _startingVectors[i + 1].DotProduct(u) / c[i]; + alpha = _startingVectors[i + 1].DotProduct(u)/c[i]; // u_(jk+i+1) = u_(jk+i) - alpha_(jk+i+1) d_(jk+i) d[i].Multiply(-alpha, temp); @@ -591,7 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner.Approximate(g[i], gtemp); // x_(jk+i+1) = x_(jk+i) + rho_(j+1) alpha_(jk+i+1) g~_(jk+i) - gtemp.Multiply(rho * alpha, temp); + gtemp.Multiply(rho*alpha, temp); xtemp.Add(temp, temp2); temp2.CopyTo(xtemp); @@ -599,7 +599,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers matrix.Multiply(gtemp, w[i]); // r_(jk+i+1) = r_(jk+i) - rho_(j+1) alpha_(jk+i+1) w_(jk+i) - w[i].Multiply(-rho * alpha, temp); + w[i].Multiply(-rho*alpha, temp); residuals.Add(temp, temp2); temp2.CopyTo(residuals); @@ -626,7 +626,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Maximum number /// Number of variables /// Number of starting vectors to create - private static int NumberOfStartingVectorsToCreate(int maximumNumberOfStartingVectors, int numberOfVariables) + static int NumberOfStartingVectorsToCreate(int maximumNumberOfStartingVectors, int numberOfVariables) { // Create no more starting vectors than the size of the problem - 1 return Math.Min(maximumNumberOfStartingVectors, (numberOfVariables - 1)); @@ -643,7 +643,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// the is smaller than /// the . /// - private static IList> CreateStartingVectors(int maximumNumberOfStartingVectors, int numberOfVariables) + static IList> CreateStartingVectors(int maximumNumberOfStartingVectors, int numberOfVariables) { // Create no more starting vectors than the size of the problem - 1 // Get random values and then orthogonalize them with @@ -660,9 +660,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers var samples = new float[matrix.RowCount]; for (var j = 0; j < matrix.RowCount; j++) { - samples[j] = (float)distribution.Sample(); + samples[j] = (float) distribution.Sample(); } - + // Set the column matrix.SetColumn(i, samples); } @@ -676,9 +676,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers for (var i = 0; i < orthogonalMatrix.ColumnCount; i++) { result.Add(orthogonalMatrix.Column(i)); - + // Normalize the result vector - result[i].Multiply(1 / result[i].L2Norm(), result[i]); + result[i].Multiply(1/result[i].L2Norm(), result[i]); } return result; @@ -690,7 +690,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Number of vectors /// Size of each vector /// Array of random vectors - private static Vector[] CreateVectorArray(int arraySize, int vectorSize) + static Vector[] CreateVectorArray(int arraySize, int vectorSize) { var result = new Vector[arraySize]; for (var i = 0; i < result.Length; i++) @@ -708,7 +708,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Residual data. /// x data. /// b data. - private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) + static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b) { // -Ax = residual matrix.Multiply(x, residual); @@ -726,21 +726,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - private bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs index 908781d0..aa54e494 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs @@ -69,7 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// The iterative process controller. /// - IIterator _iterator; + Iterator _iterator; /// /// Indicates if the user has stopped the solver. @@ -80,7 +80,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// public TFQMR() @@ -96,18 +96,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use a default preconditioner. /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(Iterator iterator) : this(null, iterator) { } @@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Initializes a new instance of the class. /// /// - /// When using this constructor the solver will use the with + /// When using this constructor the solver will use the with /// the standard settings. /// /// The that will be used to precondition the matrix equation. @@ -130,19 +130,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The main advantages of using a user defined are: + /// The main advantages of using a user defined are: /// /// It is possible to set the desired convergence limits. /// /// It is possible to check the reason for which the solver finished - /// the iterative procedure by calling the property. + /// the iterative procedure by calling the property. /// /// /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, IIterator iterator) + /// The that will be used to monitor the iterative process. + public TFQMR(IPreConditioner preconditioner, Iterator iterator) { _iterator = iterator; _preconditioner = preconditioner; @@ -158,10 +158,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - public void SetIterator(IIterator iterator) + public void SetIterator(Iterator iterator) { _iterator = iterator; } @@ -312,7 +312,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (sigma.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -385,7 +385,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (rho.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.IterationCancelled(); + _iterator.Cancel(); break; } @@ -447,19 +447,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// true if continue, otherwise false bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) { + // We stop if either: + // - the user has stopped the calculation + // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) + if (_hasBeenStopped) { - _iterator.IterationCancelled(); + _iterator.Cancel(); return true; } - _iterator.DetermineStatus(iterationNumber, result, source, residuals); - var status = _iterator.Status; - - // We stop if either: - // - the user has stopped the calculation - // - the calculation needs to be stopped from a numerical point of view (divergence, convergence etc.) - return (!status.TerminatesCalculation) && (!_hasBeenStopped); + return !_iterator.DetermineStatus(iterationNumber, result, source, residuals).TerminatesCalculation; } /// diff --git a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs index 379c3007..147f53f0 100644 --- a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs +++ b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs @@ -47,10 +47,10 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers void StopSolve(); /// - /// Sets the that will be used to track the iterative process. + /// Sets the that will be used to track the iterative process. /// /// The iterator. - void SetIterator(IIterator iterator); + void SetIterator(Iterator iterator); /// /// Gets the status of the iteration once the calculation is finished. diff --git a/src/Numerics/LinearAlgebra/Solvers/IIterator.cs b/src/Numerics/LinearAlgebra/Solvers/IIterator.cs deleted file mode 100644 index 571b66fc..00000000 --- a/src/Numerics/LinearAlgebra/Solvers/IIterator.cs +++ /dev/null @@ -1,77 +0,0 @@ -// -// 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-2013 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 -{ - /// - /// Defines the base interface for iterators that help control an iterative calculation. - /// - public interface IIterator where T : struct, IEquatable, IFormattable - { - /// - /// Indicates to the iterator that the iterative process has been cancelled. - /// - /// Does not reset the stop-criteria. - void IterationCancelled(); - - /// - /// Determines the status of the iterative calculation based on the stop criteria stored - /// by the current . Status is set to Status field of current object. - /// - /// 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 iterators 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. - /// - void DetermineStatus(int iterationNumber, Vector solutionVector, Vector sourceVector, Vector residualVector); - - /// - /// Gets the current calculation status. - /// - /// is not a legal value. Status should be set in implementation.. - ICalculationStatus Status { get; } - - /// - /// Resets the to the pre-calculation state. - /// - /// - /// Note to implementers: Invoking this method should not clear the user defined - /// property values, only the state that is used to track the progress of the - /// calculation. - /// - void ResetToPrecalculationState(); - } -} diff --git a/src/Numerics/LinearAlgebra/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Solvers/Iterator.cs index bc8bb718..726109b2 100644 --- a/src/Numerics/LinearAlgebra/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Solvers/Iterator.cs @@ -39,34 +39,29 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// /// An iterator that is used to check if an iterative calculation should continue or stop. /// - public sealed class Iterator : IIterator where T : struct, IEquatable, IFormattable + public sealed class Iterator where T : struct, IEquatable, IFormattable { - /// - /// The default status for the iterator. - /// - static readonly ICalculationStatus DefaultStatus = new CalculationIndetermined(); - /// /// The collection that holds all the stop criteria and the flag indicating if they should be added /// to the child iterators. /// - readonly List> _stopCriterias = new List>(); + readonly List> _stopCriteria; /// /// The status of the iterator. /// - ICalculationStatus _status = DefaultStatus; + ICalculationStatus _status = new CalculationIndetermined(); /// - /// Indicates if the iteration was canceled. - /// - bool _wasIterationCancelled; - - /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class with the specified stop criteria. /// - public Iterator() : this(null) + /// + /// The specified stop criteria. Only one stop criterium of each type can be passed in. None + /// of the stop criteria will be passed on to child iterators. + /// + public Iterator(params IIterationStopCriterium[] stopCriteria) { + _stopCriteria = new List>(stopCriteria); } /// @@ -76,83 +71,38 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// The specified stop criteria. Only one stop criterium of each type can be passed in. None /// of the stop criteria will be passed on to child iterators. /// - /// Thrown if contains multiple stop criteria of the same type. public Iterator(IEnumerable> stopCriteria) { - // Add the stop criteria - if (stopCriteria == null) - { - return; - } - - foreach (var stopCriterium in stopCriteria.Where(stopCriterium => stopCriterium != null)) - { - Add(stopCriterium); - } - } - - /// - /// Adds an to the internal collection of stop-criteria. Only a - /// single stop criterium of each type can be stored. - /// - /// The stop criterium to add. - public void Add(IIterationStopCriterium stopCriterium) - { - _stopCriterias.Add(stopCriterium); + _stopCriteria = new List>(stopCriteria); } /// - /// Removes the from the internal collection. - /// - /// The stop criterium that must be removed. - public void Remove(IIterationStopCriterium stopCriterium) - { - _stopCriterias.Remove(stopCriterium); - } - - /// - /// Indicates if the specific stop criterium is stored by the . - /// - /// The stop criterium. - /// true if the contains the stop criterium; otherwise false. - public bool Contains(IIterationStopCriterium stopCriterium) - { - return _stopCriterias.Contains(stopCriterium); - } - - /// - /// Gets the number of stored stop criteria. + /// Gets the current calculation status. /// - /// Used for testing only. - internal int NumberOfCriteria + public ICalculationStatus Status { - get { return _stopCriterias.Count; } + get { return _status; } } /// - /// Gets an IEnumerator that enumerates over all the stored stop criteria. + /// True if the calculation has converged to the desired convergence levels. /// - /// Used for testing only. - internal IEnumerable> StoredStopCriteria + public bool HasConverged { - get { return _stopCriterias; } + get { return _status is CalculationConverged; } } /// - /// Indicates to the iterator that the iterative process has been cancelled. + /// True if the calculation has been stopped due to reaching the stopping limits but that convergence was not achieved. /// - /// - /// Does not reset the stop-criteria. - /// - public void IterationCancelled() + public bool HasStoppedWithoutConvergence { - _wasIterationCancelled = true; - _status = new CalculationCancelled(); + get { return _status is CalculationStoppedWithoutConvergence; } } /// /// Determines the status of the iterative calculation based on the stop criteria stored - /// by the current . Result is set into Status field. + /// by the current . Result is set into Status field. /// /// The number of iterations that have passed so far. /// The vector containing the current solution values. @@ -163,9 +113,9 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// on the invocation of this method. Therefore this method should only be called if the /// calculation has moved forwards at least one step. /// - public void DetermineStatus(int iterationNumber, Vector solutionVector, Vector sourceVector, Vector residualVector) + public ICalculationStatus DetermineStatus(int iterationNumber, Vector solutionVector, Vector sourceVector, Vector residualVector) { - if (_stopCriterias.Count == 0) + if (_stopCriteria.Count == 0) { throw new ArgumentException(Resources.StopCriteriumMissing); } @@ -176,12 +126,12 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers } // While we're cancelled we don't call on the stop-criteria. - if (_wasIterationCancelled) + if (_status is CalculationCancelled) { - return; + return _status; } - foreach (var stopCriterium in _stopCriterias) + foreach (var stopCriterium in _stopCriteria) { var status = stopCriterium.DetermineStatus(iterationNumber, solutionVector, sourceVector, residualVector); @@ -196,7 +146,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers } _status = status; - return; + return _status; } // Got all the way through @@ -205,29 +155,31 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers { _status = new CalculationRunning(); } + + return _status; } /// - /// Gets the current calculation status. + /// Indicates to the iterator that the iterative process has been cancelled. /// - public ICalculationStatus Status + /// + /// Does not reset the stop-criteria. + /// + public void Cancel() { - get { return _status; } + _status = new CalculationCancelled(); } /// - /// Resets the to the pre-calculation state. + /// Resets the to the pre-calculation state. /// - public void ResetToPrecalculationState() + public void Reset() { - // Indicate that we're no longer cancelled. - _wasIterationCancelled = false; - // Reset the status. - _status = DefaultStatus; + _status = new CalculationIndetermined(); // Reset the stop-criteria - foreach (var stopCriterium in _stopCriterias) + foreach (var stopCriterium in _stopCriteria) { stopCriterium.ResetToPrecalculationState(); } @@ -237,10 +189,9 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// Creates a deep clone of the current iterator. /// /// The deep clone of the current iterator. - public IIterator Clone() + public Iterator Clone() { - var stopCriteria = _stopCriterias.Select(stopCriterium => stopCriterium.Clone()).ToList(); - return new Iterator(stopCriteria); + return new Iterator(_stopCriteria.Select(sc => sc.Clone())); } } } diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 3ba13983..741235e7 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -218,7 +218,6 @@ - diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs index 8bb366ad..284a4452 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative @@ -115,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -160,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -238,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs index 3fae428e..e4e91654 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative @@ -115,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -240,7 +239,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs index c025c9f9..ac29c757 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative @@ -116,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -239,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs index fe39f223..0a6da4e0 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterative @@ -116,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -239,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs index b0f8ead1..576064b6 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/IteratorTest.cs @@ -46,135 +46,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers [TestFixture] public class IteratorTest { - /// - /// Can create with null collection. - /// - [Test] - public void CreateWithNullCollection() - { - var iterator = new Iterator(null); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with empty collection. - /// - [Test] - public void CreateWithEmptyCollection() - { - var iterator = new Iterator(new IIterationStopCriterium[] { }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection with nulls. - /// - [Test] - public void CreateWithCollectionWithNulls() - { - var iterator = new Iterator(new IIterationStopCriterium[] { null, null }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection. - /// - [Test] - public void CreateWithCollection() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.IsNotNull(iterator, "Should have an iterator"); - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can add criterium. - /// - [Test] - public void Add() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(); - Assert.AreEqual(0, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Add(criterium); - Assert.IsTrue(iterator.Contains(criterium), "Missing criterium"); - } - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can remove with non-existing stop criterium. - /// - [Test] - public void RemoveWithNonExistingStopCriterium() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - iterator.Remove(new ResidualStopCriterium()); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - } - - /// - /// Can remove. - /// - [Test] - public void Remove() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Remove(criterium); - Assert.IsFalse(iterator.Contains(criterium), "Did not remove the criterium"); - } - } - /// /// Determine status without stop criteria throws ArgumentException. /// @@ -332,43 +203,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers DenseVector.Create(3, i => 4)); Assert.IsInstanceOf(typeof (CalculationRunning), iterator.Status, "Incorrect status"); - iterator.ResetToPrecalculationState(); + iterator.Reset(); Assert.IsInstanceOf(typeof (CalculationIndetermined), iterator.Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[0].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[1].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[2].Status, "Incorrect status"); } - - /// - /// Can clone. - /// - [Test] - public void Clone() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - - var iterator = new Iterator(criteria); - - var clonedIterator = iterator.Clone(); - Assert.IsInstanceOf(typeof(Iterator), clonedIterator, "Incorrect type"); - - var clone = clonedIterator as Iterator; - Assert.IsNotNull(clone); - - // ReSharper disable PossibleNullReferenceException - Assert.AreEqual(iterator.NumberOfCriteria, clone.NumberOfCriteria, "Incorrect criterium count"); - - // ReSharper restore PossibleNullReferenceException - foreach (var criterium in clone.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => c.GetType() == criterium.GetType()), "Criterium missing"); - } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs index 9c0a887a..c8f97fc6 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative @@ -115,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -160,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -238,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -266,13 +265,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new BiCgStab(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -314,7 +313,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new BiCgStab(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs index 3046f243..2167cc8b 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative @@ -115,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -240,7 +239,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -270,7 +269,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs index 2327c755..c2e6ffbb 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative @@ -116,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -239,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -263,13 +262,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new MlkBiCgStab(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -311,7 +310,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new MlkBiCgStab(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs index abb6a3a1..9a2f41bd 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Complex32; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterative @@ -116,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -161,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -239,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -263,13 +262,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new TFQMR(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -311,7 +310,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new TFQMR(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs index 27f229a5..9dfaecfd 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/IteratorTest.cs @@ -46,135 +46,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers [TestFixture] public class IteratorTest { - /// - /// Can create with null collection. - /// - [Test] - public void CreateWithNullCollection() - { - var iterator = new Iterator(null); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with empty collection. - /// - [Test] - public void CreateWithEmptyCollection() - { - var iterator = new Iterator(new IIterationStopCriterium[] { }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection with nulls. - /// - [Test] - public void CreateWithCollectionWithNulls() - { - var iterator = new Iterator(new IIterationStopCriterium[] { null, null }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection. - /// - [Test] - public void CreateWithCollection() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.IsNotNull(iterator, "Should have an iterator"); - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can add criterium. - /// - [Test] - public void Add() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(); - Assert.AreEqual(0, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Add(criterium); - Assert.IsTrue(iterator.Contains(criterium), "Missing criterium"); - } - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can remove with non-existing stop criterium. - /// - [Test] - public void RemoveWithNonExistingStopCriterium() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - iterator.Remove(new ResidualStopCriterium()); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - } - - /// - /// Can remove. - /// - [Test] - public void Remove() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Remove(criterium); - Assert.IsFalse(iterator.Contains(criterium), "Did not remove the criterium"); - } - } - /// /// Determine status without stop criteria throws ArgumentException. /// @@ -332,43 +203,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers DenseVector.Create(3, i => 4)); Assert.IsInstanceOf(typeof (CalculationRunning), iterator.Status, "Incorrect status"); - iterator.ResetToPrecalculationState(); + iterator.Reset(); Assert.IsInstanceOf(typeof (CalculationIndetermined), iterator.Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[0].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[1].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[2].Status, "Incorrect status"); } - - /// - /// Can clone. - /// - [Test] - public void Clone() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - - var iterator = new Iterator(criteria); - - var clonedIterator = iterator.Clone(); - Assert.IsInstanceOf(typeof(Iterator), clonedIterator, "Incorrect type"); - - var clone = clonedIterator as Iterator; - Assert.IsNotNull(clone); - - // ReSharper disable PossibleNullReferenceException - Assert.AreEqual(iterator.NumberOfCriteria, clone.NumberOfCriteria, "Incorrect criterium count"); - - // ReSharper restore PossibleNullReferenceException - foreach (var criterium in clone.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => c.GetType() == criterium.GetType()), "Criterium missing"); - } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs index 5d942ade..78535989 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs @@ -33,7 +33,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.Status; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative @@ -113,7 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -158,7 +157,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -236,7 +235,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs index 9aa77417..ea31e7f2 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs @@ -33,7 +33,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.Status; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative @@ -113,7 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -159,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -238,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs index ae195d8c..81f03525 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs @@ -33,7 +33,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.Status; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative @@ -114,7 +113,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -159,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -237,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs index b048425a..16f0e32d 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs @@ -33,7 +33,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.Status; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative @@ -114,7 +113,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -159,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -237,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs index 7bc00517..8b8fbc8e 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/IteratorTest.cs @@ -44,135 +44,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers [TestFixture] public class IteratorTest { - /// - /// Can create with null collection. - /// - [Test] - public void CreateWithNullCollection() - { - var iterator = new Iterator(null); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with empty collection. - /// - [Test] - public void CreateWithEmptyCollection() - { - var iterator = new Iterator(new IIterationStopCriterium[] { }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection with nulls. - /// - [Test] - public void CreateWithCollectionWithNulls() - { - var iterator = new Iterator(new IIterationStopCriterium[] { null, null }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection. - /// - [Test] - public void CreateWithCollection() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.IsNotNull(iterator, "Should have an iterator"); - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can add criterium. - /// - [Test] - public void Add() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(); - Assert.AreEqual(0, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Add(criterium); - Assert.IsTrue(iterator.Contains(criterium), "Missing criterium"); - } - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can remove with non-existing stop criterium. - /// - [Test] - public void RemoveWithNonExistingStopCriterium() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - iterator.Remove(new ResidualStopCriterium()); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - } - - /// - /// Can remove. - /// - [Test] - public void Remove() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Remove(criterium); - Assert.IsFalse(iterator.Contains(criterium), "Did not remove the criterium"); - } - } - /// /// Determine status without stop criteria throws ArgumentException. /// @@ -330,43 +201,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers DenseVector.Create(3, i => 4)); Assert.IsInstanceOf(typeof (CalculationRunning), iterator.Status, "Incorrect status"); - iterator.ResetToPrecalculationState(); + iterator.Reset(); Assert.IsInstanceOf(typeof (CalculationIndetermined), iterator.Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[0].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[1].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[2].Status, "Incorrect status"); } - - /// - /// Can clone. - /// - [Test] - public void Clone() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - - var iterator = new Iterator(criteria); - - var clonedIterator = iterator.Clone(); - Assert.IsInstanceOf(typeof(Iterator), clonedIterator, "Incorrect type"); - - var clone = clonedIterator as Iterator; - Assert.IsNotNull(clone); - - // ReSharper disable PossibleNullReferenceException - Assert.AreEqual(iterator.NumberOfCriteria, clone.NumberOfCriteria, "Incorrect criterium count"); - - // ReSharper restore PossibleNullReferenceException - foreach (var criterium in clone.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => c.GetType().Equals(criterium.GetType())), "Criterium missing"); - } - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs index 6b2a3ade..a5c5b1b5 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Single; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative @@ -113,7 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -158,7 +157,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -236,7 +235,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -262,12 +261,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new BiCgStab(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -308,7 +307,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new BiCgStab(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs index 2dbb2e1c..fc38bdfa 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Single; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative @@ -113,7 +112,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -159,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -238,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -264,12 +263,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -310,7 +309,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs index e980630c..20b26e86 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs @@ -34,7 +34,6 @@ using MathNet.Numerics.LinearAlgebra.Single; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative @@ -115,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -160,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -242,7 +241,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative continue; } - if (!(monitor.Status is CalculationConverged)) + if (!(monitor.HasConverged)) { continue; } @@ -281,12 +280,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new MlkBiCgStab(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -329,7 +328,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new MlkBiCgStab(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs index 87869135..0e3c517c 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.LinearAlgebra.Single; 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 NUnit.Framework; namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative @@ -114,7 +113,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -159,7 +158,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -237,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var z = matrix.Multiply(x); // Check that the solution converged - Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); + Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) @@ -263,12 +262,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), - new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), + new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new TFQMR(monitor); var resultx = solver.Solve(matrixA, vectorb); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -309,7 +308,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new TFQMR(monitor); var matrixX = solver.Solve(matrixA, matrixB); - if (!(monitor.Status is CalculationConverged)) + if (!monitor.HasConverged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs index bf3f8e44..81948de5 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/IteratorTest.cs @@ -44,135 +44,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers [TestFixture] public class IteratorTest { - /// - /// Can create with null collection. - /// - [Test] - public void CreateWithNullCollection() - { - var iterator = new Iterator(null); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with empty collection. - /// - [Test] - public void CreateWithEmptyCollection() - { - var iterator = new Iterator(new IIterationStopCriterium[] { }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection with nulls. - /// - [Test] - public void CreateWithCollectionWithNulls() - { - var iterator = new Iterator(new IIterationStopCriterium[] { null, null }); - Assert.IsNotNull(iterator, "Should have an iterator"); - Assert.AreEqual(0, iterator.NumberOfCriteria, "There shouldn't be any criteria"); - } - - /// - /// Can create with collection. - /// - [Test] - public void CreateWithCollection() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.IsNotNull(iterator, "Should have an iterator"); - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can add criterium. - /// - [Test] - public void Add() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(); - Assert.AreEqual(0, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Add(criterium); - Assert.IsTrue(iterator.Contains(criterium), "Missing criterium"); - } - - // Check that we have all the criteria - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - foreach (var criterium in iterator.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => ReferenceEquals(c, criterium)), "Criterium missing"); - } - } - - /// - /// Can remove with non-existing stop criterium. - /// - [Test] - public void RemoveWithNonExistingStopCriterium() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - iterator.Remove(new ResidualStopCriterium()); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - } - - /// - /// Can remove. - /// - [Test] - public void Remove() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - var iterator = new Iterator(criteria); - Assert.AreEqual(criteria.Count, iterator.NumberOfCriteria, "Incorrect criterium count"); - - foreach (var criterium in criteria) - { - iterator.Remove(criterium); - Assert.IsFalse(iterator.Contains(criterium), "Did not remove the criterium"); - } - } - /// /// Determine status without stop criteria throws ArgumentException. /// @@ -330,43 +201,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers DenseVector.Create(3, i => 4)); Assert.IsInstanceOf(typeof (CalculationRunning), iterator.Status, "Incorrect status"); - iterator.ResetToPrecalculationState(); + iterator.Reset(); Assert.IsInstanceOf(typeof (CalculationIndetermined), iterator.Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[0].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[1].Status, "Incorrect status"); Assert.IsInstanceOf(typeof (CalculationIndetermined), criteria[2].Status, "Incorrect status"); } - - /// - /// Can clone. - /// - [Test] - public void Clone() - { - var criteria = new List> - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - new ResidualStopCriterium() - }; - - var iterator = new Iterator(criteria); - - var clonedIterator = iterator.Clone(); - Assert.IsInstanceOf(typeof(Iterator), clonedIterator, "Incorrect type"); - - var clone = clonedIterator as Iterator; - Assert.IsNotNull(clone); - - // ReSharper disable PossibleNullReferenceException - Assert.AreEqual(iterator.NumberOfCriteria, clone.NumberOfCriteria, "Incorrect criterium count"); - - // ReSharper restore PossibleNullReferenceException - foreach (var criterium in clone.StoredStopCriteria) - { - Assert.IsTrue(criteria.Exists(c => c.GetType() == criterium.GetType()), "Criterium missing"); - } - } } }