From c095cdb536f553d3fcbebb3b21b49861fd4d75ab Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 16 Sep 2013 19:36:08 +0200 Subject: [PATCH] LA: iterative solver no longer stateful on iterator --- .../IterativeSolvers/BiCgStabSolver.cs | 6 +- .../CompositeSolverExample.cs | 6 +- .../IterativeSolvers/GpBiCgSolver.cs | 6 +- .../IterativeSolvers/MlkBiCgStabSolver.cs | 6 +- .../IterativeSolvers/TFQMRSolver.cs | 6 +- .../LinearAlgebra/Complex/Solvers/BiCgStab.cs | 129 +++-------------- .../Complex/Solvers/CompositeSolver.cs | 72 +++------- .../LinearAlgebra/Complex/Solvers/GpBiCg.cs | 123 +++------------- .../LinearAlgebra/Complex/Solvers/Iterator.cs | 19 ++- .../Complex/Solvers/MlkBiCgStab.cs | 128 +++-------------- .../LinearAlgebra/Complex/Solvers/TFQMR.cs | 129 +++-------------- .../Complex32/Solvers/BiCgStab.cs | 129 +++-------------- .../Complex32/Solvers/CompositeSolver.cs | 74 +++------- .../LinearAlgebra/Complex32/Solvers/GpBiCg.cs | 120 ++-------------- .../Complex32/Solvers/Iterator.cs | 19 ++- .../Complex32/Solvers/MlkBiCgStab.cs | 128 +++-------------- .../LinearAlgebra/Complex32/Solvers/TFQMR.cs | 129 +++-------------- .../LinearAlgebra/Double/Solvers/BiCgStab.cs | 133 +++--------------- .../Double/Solvers/CompositeSolver.cs | 61 +++----- .../LinearAlgebra/Double/Solvers/GpBiCg.cs | 127 +++-------------- .../LinearAlgebra/Double/Solvers/Iterator.cs | 19 ++- .../Double/Solvers/MlkBiCgStab.cs | 132 +++-------------- .../LinearAlgebra/Double/Solvers/TFQMR.cs | 133 +++--------------- .../LinearAlgebra/Single/Solvers/BiCgStab.cs | 133 +++--------------- .../Single/Solvers/CompositeSolver.cs | 72 +++------- .../LinearAlgebra/Single/Solvers/GpBiCg.cs | 123 +++------------- .../LinearAlgebra/Single/Solvers/Iterator.cs | 19 ++- .../Single/Solvers/MlkBiCgStab.cs | 132 +++-------------- .../LinearAlgebra/Single/Solvers/TFQMR.cs | 129 +++-------------- .../LinearAlgebra/Solvers/IIterativeSolver.cs | 19 +-- .../Complex/Solvers/Iterative/BiCgStabTest.cs | 20 +-- .../Complex/Solvers/Iterative/GpBiCgTest.cs | 20 +-- .../Solvers/Iterative/MlkBiCgStabTest.cs | 20 +-- .../Complex/Solvers/Iterative/TFQMRTest.cs | 20 +-- .../Solvers/Iterative/BiCgStabTest.cs | 20 +-- .../Complex32/Solvers/Iterative/GpBiCgTest.cs | 16 +-- .../Solvers/Iterative/MlkBiCgStabTest.cs | 20 +-- .../Complex32/Solvers/Iterative/TFQMRTest.cs | 20 +-- .../Double/Solvers/Iterative/BiCgStabTest.cs | 20 +-- .../Double/Solvers/Iterative/GpBiCgTest.cs | 20 +-- .../Solvers/Iterative/MlkBiCgStabTest.cs | 20 +-- .../Double/Solvers/Iterative/TFQMRTest.cs | 20 +-- .../Single/Solvers/Iterative/BiCgStabTest.cs | 20 +-- .../Single/Solvers/Iterative/GpBiCgTest.cs | 20 +-- .../Solvers/Iterative/MlkBiCgStabTest.cs | 20 +-- .../Single/Solvers/Iterative/TFQMRTest.cs | 20 +-- 46 files changed, 636 insertions(+), 2141 deletions(-) diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs index b3923cd9..a43e5121 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs @@ -108,10 +108,10 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // Create Bi-Conjugate Gradient Stabilized solver - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB); + var resultX = solver.Solve(matrixA, vectorB, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); @@ -126,7 +126,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); - Console.WriteLine(solver.IterationResult); + Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs index 70467456..aaf68536 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs @@ -112,10 +112,10 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples CompositeSolver.LoadSolverInformationFromAssembly(Assembly.GetExecutingAssembly()); // Create composite solver - var solver = new CompositeSolver(monitor); + var solver = new CompositeSolver(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB); + var resultX = solver.Solve(matrixA, vectorB, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); @@ -130,7 +130,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); - Console.WriteLine(solver.IterationResult); + Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs index c2ecd8b3..330be972 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs @@ -106,10 +106,10 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // Create Generalized Product Bi-Conjugate Gradient solver - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB); + var resultX = solver.Solve(matrixA, vectorB, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); @@ -124,7 +124,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); - Console.WriteLine(solver.IterationResult); + Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs index 96547e12..6b6d3672 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs @@ -107,10 +107,10 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // Create Multiple-Lanczos Bi-Conjugate Gradient Stabilized solver - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB); + var resultX = solver.Solve(matrixA, vectorB, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); @@ -125,7 +125,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); - Console.WriteLine(solver.IterationResult); + Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs index 08b1d1d2..05094df1 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs @@ -107,10 +107,10 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // Create Transpose Free Quasi-Minimal Residual solver - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB); + var resultX = solver.Solve(matrixA, vectorB, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); @@ -125,7 +125,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); - Console.WriteLine(solver.IterationResult); + Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs index fb22c8c2..95dda13f 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs @@ -78,11 +78,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -96,44 +91,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// the standard settings and a default preconditioner. /// public BiCgStab() - : this(null, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// When using this constructor the solver will use a default preconditioner. - /// - /// - /// 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 that will be used to monitor the iterative process. - public BiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// - /// The that will be used to precondition the matrix equation. - public BiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) { } @@ -153,10 +110,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, Iterator iterator) + public BiCgStab(IPreConditioner preconditioner) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -169,23 +124,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -204,15 +142,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -223,7 +156,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -263,9 +196,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -302,7 +235,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers Complex omega = 0; var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1) var oldRho = currentRho; @@ -361,7 +294,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers temp2.CopyTo(temp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, temp, input, vecS)) + if (!ShouldContinue(iterator, iterationNumber, temp, input, vecS)) { temp.CopyTo(result); @@ -369,7 +302,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers CalculateTrueResidual(matrix, residuals, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // We're all good now. return; @@ -410,7 +343,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw new Exception("Iterative solver experience a numerical break down"); } - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -451,7 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -459,11 +392,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -474,20 +407,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient , A. /// The solution , B. /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -498,31 +421,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient , A. /// The solution , B. /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs index 199fe524..b04a03fb 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs @@ -339,11 +339,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// IterationStatus _status = IterationStatus.Indetermined; - /// - /// The iterator that is used to control the iteration process. - /// - Iterator _iterator; - /// /// A flag indicating if the solver has been stopped or not. /// @@ -358,37 +353,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// Initializes a new instance of the class with the default iterator. /// - public CompositeSolver() : this(null) - { - } - - /// - /// Initializes a new instance of the class with the specified iterator. - /// - /// The iterator that will be used to control the iteration process. - public CompositeSolver(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Sets the IIterator that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult + public CompositeSolver() { - get - { - return _status; - } } /// @@ -414,10 +380,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -428,7 +394,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -448,9 +414,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } // Load the solvers into our own internal data structure @@ -473,11 +439,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers try { // Reset the iterator and pass it to the solver - _iterator.Reset(); - solver.SetIterator(_iterator); + iterator.Reset(); // Start the solver - solver.Solve(matrix, internalInput, internalResult); + solver.Solve(matrix, internalInput, internalResult, iterator); + _status = iterator.Status; } catch (Exception) { @@ -491,7 +457,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } // There was no fatal breakdown so check the status - if (_iterator.HasConverged) + if (_status == IterationStatus.Converged) { // We're done internalResult.CopyTo(result); @@ -501,7 +467,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.HasStoppedWithoutConvergence) + if (_status == IterationStatus.StoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -522,9 +488,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Clean up // No longer need the current solver _currentSolver = null; - - // Set the final status - _status = _iterator.Status; } /// @@ -554,10 +517,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -568,16 +531,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { throw Matrix.DimensionsDontMatch(matrix, input, result); } + if (iterator == null) + { + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + } + for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs index 5830d0aa..6ef79844 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs @@ -76,11 +76,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates the number of BiCGStab steps should be taken /// before switching. @@ -106,7 +101,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// the standard settings and a default preconditioner. /// public GpBiCg() - : this(null, null) { } @@ -115,9 +109,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -128,45 +119,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public GpBiCg(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -217,23 +172,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -253,15 +191,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -272,7 +205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -307,9 +240,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -356,7 +289,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // for (k = 0, 1, .... ) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1)) p.Subtract(u, temp); @@ -497,7 +430,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers _preconditioner.Approximate(xtemp, result); // Now check for convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -534,7 +467,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -542,11 +475,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -574,20 +507,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -598,31 +521,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs index dbc74e20..c38d6ec8 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs @@ -49,16 +49,25 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // TODO: Refactor /// - /// Creates a default iterator with all the objects. + /// Creates the default stop criteria. /// - /// A new object. - public static Iterator CreateDefault() + public static IIterationStopCriterium[] CreateDefaultStopCriteria() { - return new Iterator( + return new IIterationStopCriterium[] + { new FailureStopCriterium(), new DivergenceStopCriterium(), new IterationCountStopCriterium(), - new ResidualStopCriterium()); + new ResidualStopCriterium() + }; + } + + /// + /// Creates a default iterator with all the default stop criteria. + /// + public static Iterator CreateDefault() + { + return new Iterator(CreateDefaultStopCriteria()); } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs index 72715d7e..66abbb9a 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs @@ -82,11 +82,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// @@ -110,7 +105,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// the standard settings and a default preconditioner. /// public MlkBiCgStab() - : this(null, null) { } @@ -119,9 +113,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -132,45 +123,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public MlkBiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) - { - _iterator = iterator; _preconditioner = preconditioner; } @@ -215,15 +170,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - /// /// Gets or sets a series of orthonormal vectors which will be used as basis for the /// Krylov sub-space. @@ -247,15 +193,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } } - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - [DebuggerStepThrough] - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -274,15 +211,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -293,7 +225,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -328,9 +260,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -400,7 +332,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // FOR (j = 0, 1, 2 ....) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // SOLVE M g~_((j-1)k+k) = g_((j-1)k+k) _preconditioner.Approximate(g[k - 1], gtemp); @@ -458,13 +390,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers temp2.CopyTo(xtemp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Calculate the true residual CalculateTrueResidual(matrix, residuals, xtemp, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // We're all good now. // Exit from the while loop. @@ -593,7 +525,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers temp2.CopyTo(residuals); // We can check the residuals here if they're close - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -717,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -725,11 +657,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -740,20 +672,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -764,31 +686,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs index 9750802a..c4bd3eb5 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs @@ -67,11 +67,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -85,7 +80,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// the standard settings and a default preconditioner. /// public TFQMR() - : this(null, null) { } @@ -94,9 +88,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -107,45 +98,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public TFQMR(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -158,23 +113,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -193,15 +131,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -212,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -247,9 +180,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -298,7 +231,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // Start the iteration var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + while (ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // First part of the step, the even bit if (IsEven(iterationNumber)) @@ -308,7 +241,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (sigma.Real.AlmostEqual(0, 1) && sigma.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -357,7 +290,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers temp2.CopyTo(x); // Check convergence and see if we can bail - if (!ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // Calculate the real values _preconditioner.Approximate(x, result); @@ -368,7 +301,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers CalculateTrueResidual(matrix, temp, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, temp)) + if (!ShouldContinue(iterator, iterationNumber, result, input, temp)) { // We're all good now. return; @@ -381,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (rho.Real.AlmostEqual(0, 1) && rho.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -441,7 +374,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -449,11 +382,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -474,20 +407,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -498,31 +421,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs index 44a9cc7f..6409319e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs @@ -71,11 +71,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -89,44 +84,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// the standard settings and a default preconditioner. /// public BiCgStab() - : this(null, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// When using this constructor the solver will use a default preconditioner. - /// - /// - /// 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 that will be used to monitor the iterative process. - public BiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// - /// The that will be used to precondition the matrix equation. - public BiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) { } @@ -146,10 +103,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, Iterator iterator) + public BiCgStab(IPreConditioner preconditioner) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -162,23 +117,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -197,15 +135,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -216,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -256,9 +189,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -295,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers Numerics.Complex32 omega = 0; var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1) var oldRho = currentRho; @@ -354,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers temp2.CopyTo(temp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, temp, input, vecS)) + if (!ShouldContinue(iterator, iterationNumber, temp, input, vecS)) { temp.CopyTo(result); @@ -362,7 +295,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers CalculateTrueResidual(matrix, residuals, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // We're all good now. return; @@ -403,7 +336,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw new Exception("Iterative solver experience a numerical break down"); } - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -444,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -452,11 +385,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -467,20 +400,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient , A. /// The solution , B. /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -491,31 +414,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient , A. /// The solution , B. /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs index fcf5ee9b..3552c548 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs @@ -86,7 +86,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers #endregion #if PORTABLE - private static readonly Dictionary>> SolverSetups = new Dictionary>>(); + private static readonly Dictionary>> SolverSetups = new Dictionary>>(); #else /// /// The collection of iterative solver setups. Stored based on the @@ -331,11 +331,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// IterationStatus _status = IterationStatus.Indetermined; - /// - /// The iterator that is used to control the iteration process. - /// - Iterator _iterator; - /// /// A flag indicating if the solver has been stopped or not. /// @@ -350,37 +345,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// Initializes a new instance of the class with the default iterator. /// - public CompositeSolver() : this(null) - { - } - - /// - /// Initializes a new instance of the class with the specified iterator. - /// - /// The iterator that will be used to control the iteration process. - public CompositeSolver(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Sets the IIterator that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult + public CompositeSolver() { - get - { - return _status; - } } /// @@ -406,10 +372,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -420,7 +386,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -440,9 +406,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } // Load the solvers into our own internal data structure @@ -465,11 +431,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers try { // Reset the iterator and pass it to the solver - _iterator.Reset(); - solver.SetIterator(_iterator); + iterator.Reset(); // Start the solver - solver.Solve(matrix, internalInput, internalResult); + solver.Solve(matrix, internalInput, internalResult, iterator); + _status = iterator.Status; } catch (Exception) { @@ -483,7 +449,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } // There was no fatal breakdown so check the status - if (_iterator.HasConverged) + if (_status == IterationStatus.Converged) { // We're done internalResult.CopyTo(result); @@ -493,7 +459,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.HasStoppedWithoutConvergence) + if (_status == IterationStatus.StoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -514,9 +480,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Clean up // No longer need the current solver _currentSolver = null; - - // Set the final status - _status = _iterator.Status; } /// @@ -546,10 +509,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -560,16 +523,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { throw Matrix.DimensionsDontMatch(matrix, input, result); } + if (iterator == null) + { + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + } + for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs index eef09eb2..8172911d 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs @@ -69,11 +69,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates the number of BiCGStab steps should be taken /// before switching. @@ -99,7 +94,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// the standard settings and a default preconditioner. /// public GpBiCg() - : this(null, null) { } @@ -108,9 +102,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -121,45 +112,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public GpBiCg(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -210,23 +165,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -246,15 +184,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -265,7 +198,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -305,9 +238,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -354,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // for (k = 0, 1, .... ) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1)) p.Subtract(u, temp); @@ -495,7 +428,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers _preconditioner.Approximate(xtemp, result); // Now check for convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -532,7 +465,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -540,11 +473,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -572,20 +505,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -596,23 +519,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { throw Matrix.DimensionsDontMatch(matrix, input, result); @@ -620,7 +528,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs index 661704b0..c1539b89 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs @@ -44,16 +44,25 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // TODO: Refactor /// - /// Creates a default iterator with all the objects. + /// Creates the default stop criteria. /// - /// A new object. - public static Iterator CreateDefault() + public static IIterationStopCriterium[] CreateDefaultStopCriteria() { - return new Iterator( + return new IIterationStopCriterium[] + { new FailureStopCriterium(), new DivergenceStopCriterium(), new IterationCountStopCriterium(), - new ResidualStopCriterium()); + new ResidualStopCriterium() + }; + } + + /// + /// Creates a default iterator with all the default stop criteria. + /// + public static Iterator CreateDefault() + { + return new Iterator(CreateDefaultStopCriteria()); } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs index 18c0ef03..3546ac7a 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs @@ -74,11 +74,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// @@ -102,7 +97,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// the standard settings and a default preconditioner. /// public MlkBiCgStab() - : this(null, null) { } @@ -111,9 +105,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -124,45 +115,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public MlkBiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) - { - _iterator = iterator; _preconditioner = preconditioner; } @@ -207,15 +162,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - /// /// Gets or sets a series of orthonormal vectors which will be used as basis for the /// Krylov sub-space. @@ -239,15 +185,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } } - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - [DebuggerStepThrough] - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -266,15 +203,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -285,7 +217,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -325,9 +257,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -397,7 +329,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // FOR (j = 0, 1, 2 ....) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // SOLVE M g~_((j-1)k+k) = g_((j-1)k+k) _preconditioner.Approximate(g[k - 1], gtemp); @@ -455,13 +387,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers temp2.CopyTo(xtemp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Calculate the true residual CalculateTrueResidual(matrix, residuals, xtemp, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // We're all good now. // Exit from the while loop. @@ -590,7 +522,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers temp2.CopyTo(residuals); // We can check the residuals here if they're close - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -714,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -722,11 +654,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -737,20 +669,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -761,31 +683,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs index b4cd55a0..1ff8e0c5 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs @@ -59,11 +59,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -77,7 +72,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// the standard settings and a default preconditioner. /// public TFQMR() - : this(null, null) { } @@ -86,9 +80,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -99,45 +90,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public TFQMR(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -150,23 +105,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -185,15 +123,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -204,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -244,9 +177,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -295,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // Start the iteration var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + while (ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // First part of the step, the even bit if (IsEven(iterationNumber)) @@ -305,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (sigma.Real.AlmostEqual(0, 1) && sigma.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -354,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers temp2.CopyTo(x); // Check convergence and see if we can bail - if (!ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // Calculate the real values _preconditioner.Approximate(x, result); @@ -365,7 +298,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers CalculateTrueResidual(matrix, temp, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, temp)) + if (!ShouldContinue(iterator, iterationNumber, result, input, temp)) { // We're all good now. return; @@ -378,7 +311,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (rho.Real.AlmostEqual(0, 1) && rho.Imaginary.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -438,7 +371,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -446,11 +379,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -471,20 +404,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -495,31 +418,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(input, matrix, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(input, matrix, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs index bade9a6b..303129bf 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs @@ -71,11 +71,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -88,44 +83,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public BiCgStab() : this(null, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// When using this constructor the solver will use a default preconditioner. - /// - /// - /// 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 that will be used to monitor the iterative process. - public BiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// - /// The that will be used to precondition the matrix equation. - public BiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) + public BiCgStab() { } @@ -145,10 +103,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, Iterator iterator) + public BiCgStab(IPreConditioner preconditioner) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -161,26 +117,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -199,15 +135,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -218,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -258,9 +189,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -297,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers double omega = 0; var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1) var oldRho = currentRho; @@ -356,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers temp2.CopyTo(temp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, temp, input, vecS)) + if (!ShouldContinue(iterator, iterationNumber, temp, input, vecS)) { temp.CopyTo(result); @@ -364,7 +295,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers CalculateTrueResidual(matrix, residuals, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // We're all good now. return; @@ -405,7 +336,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw new Exception("Iterative solver experience a numerical break down"); } - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -446,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -454,11 +385,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -469,20 +400,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient , A. /// The solution , B. /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -493,31 +414,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient , A. /// The solution , B. /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs index dde00b9e..47712d33 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs @@ -328,11 +328,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// IterationStatus _status = IterationStatus.Indetermined; - /// - /// The iterator that is used to control the iteration process. - /// - Iterator _iterator; - /// /// A flag indicating if the solver has been stopped or not. /// @@ -347,28 +342,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// Initializes a new instance of the class with the default iterator. /// - public CompositeSolver() : this(null) + public CompositeSolver() { } - /// - /// Initializes a new instance of the class with the specified iterator. - /// - /// The iterator that will be used to control the iteration process. - public CompositeSolver(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - /// /// Gets the status of the iteration once the calculation is finished. /// @@ -403,10 +380,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -417,7 +394,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -437,9 +414,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } // Load the solvers into our own internal data structure @@ -462,11 +439,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers try { // Reset the iterator and pass it to the solver - _iterator.Reset(); - solver.SetIterator(_iterator); + iterator.Reset(); // Start the solver - solver.Solve(matrix, internalInput, internalResult); + solver.Solve(matrix, internalInput, internalResult, iterator); + _status = iterator.Status; } catch (Exception) { @@ -480,7 +457,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } // There was no fatal breakdown so check the status - if (_iterator.HasConverged) + if (_status == IterationStatus.Converged) { // We're done internalResult.CopyTo(result); @@ -490,7 +467,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.HasStoppedWithoutConvergence) + if (_status == IterationStatus.StoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -511,9 +488,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Clean up // No longer need the current solver _currentSolver = null; - - // Set the final status - _status = _iterator.Status; } /// @@ -543,10 +517,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -557,16 +531,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { throw Matrix.DimensionsDontMatch(matrix, input, result); } + if (iterator == null) + { + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + } + for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs index b4a830cf..667d532b 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs @@ -69,11 +69,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates the number of BiCGStab steps should be taken /// before switching. @@ -98,7 +93,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public GpBiCg() : this(null, null) + public GpBiCg() { } @@ -107,9 +102,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -120,45 +112,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public GpBiCg(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -215,26 +171,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -254,15 +190,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -273,7 +204,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -313,9 +244,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -362,7 +293,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // for (k = 0, 1, .... ) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1)) p.Subtract(u, temp); @@ -503,7 +434,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner.Approximate(xtemp, result); // Now check for convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -540,7 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -548,11 +479,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -580,20 +511,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -604,31 +525,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs index b2bd473f..afbca83b 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs @@ -42,16 +42,25 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // TODO: Refactor /// - /// Creates a default iterator with all the objects. + /// Creates the default stop criteria. /// - /// A new object. - public static Iterator CreateDefault() + public static IIterationStopCriterium[] CreateDefaultStopCriteria() { - return new Iterator( + return new IIterationStopCriterium[] + { new FailureStopCriterium(), new DivergenceStopCriterium(), new IterationCountStopCriterium(), - new ResidualStopCriterium()); + new ResidualStopCriterium() + }; + } + + /// + /// Creates a default iterator with all the default stop criteria. + /// + public static Iterator CreateDefault() + { + return new Iterator(CreateDefaultStopCriteria()); } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs index 9161e7e8..42b9502b 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs @@ -74,11 +74,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// @@ -101,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public MlkBiCgStab() : this(null, null) + public MlkBiCgStab() { } @@ -110,9 +105,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -123,45 +115,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public MlkBiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -209,15 +165,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - /// /// Gets or sets a series of orthonormal vectors which will be used as basis for the /// Krylov sub-space. @@ -244,18 +191,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } } - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - [DebuggerStepThrough] - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -274,15 +209,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -293,7 +223,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -333,9 +263,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -405,7 +335,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // FOR (j = 0, 1, 2 ....) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // SOLVE M g~_((j-1)k+k) = g_((j-1)k+k) _preconditioner.Approximate(g[k - 1], gtemp); @@ -463,13 +393,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers temp2.CopyTo(xtemp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Calculate the true residual CalculateTrueResidual(matrix, residuals, xtemp, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // We're all good now. // Exit from the while loop. @@ -598,7 +528,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers temp2.CopyTo(residuals); // We can check the residuals here if they're close - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -716,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -724,11 +654,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -739,20 +669,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -763,31 +683,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs index b4f4023d..a6f2f372 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs @@ -59,11 +59,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -76,7 +71,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public TFQMR() : this(null, null) + public TFQMR() { } @@ -85,9 +80,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -98,45 +90,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public TFQMR(IPreConditioner preconditioner) - : this(preconditioner, null) { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, Iterator iterator) - { - _iterator = iterator; _preconditioner = preconditioner; } @@ -149,26 +105,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -187,15 +123,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -206,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -246,9 +177,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -297,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // Start the iteration var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + while (ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // First part of the step, the even bit if (IsEven(iterationNumber)) @@ -307,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (sigma.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -356,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers temp2.CopyTo(x); // Check convergence and see if we can bail - if (!ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // Calculate the real values _preconditioner.Approximate(x, result); @@ -367,7 +298,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers CalculateTrueResidual(matrix, temp, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, temp)) + if (!ShouldContinue(iterator, iterationNumber, result, input, temp)) { // We're all good now. return; @@ -380,7 +311,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (rho.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -440,7 +371,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -448,11 +379,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -473,20 +404,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -497,31 +418,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs index bb0e7189..04e5bce3 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs @@ -71,11 +71,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -88,44 +83,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public BiCgStab() : this(null, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// When using this constructor the solver will use a default preconditioner. - /// - /// - /// 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 that will be used to monitor the iterative process. - public BiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// - /// The that will be used to precondition the matrix equation. - public BiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) + public BiCgStab() { } @@ -145,10 +103,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// The that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public BiCgStab(IPreConditioner preconditioner, Iterator iterator) + public BiCgStab(IPreConditioner preconditioner) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -161,26 +117,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -199,15 +135,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -218,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient , A. /// The solution , b. /// The result , x. - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -258,9 +189,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -297,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers float omega = 0; var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1) var oldRho = currentRho; @@ -356,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers temp2.CopyTo(temp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, temp, input, vecS)) + if (!ShouldContinue(iterator, iterationNumber, temp, input, vecS)) { temp.CopyTo(result); @@ -364,7 +295,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers CalculateTrueResidual(matrix, residuals, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // We're all good now. return; @@ -405,7 +336,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw new Exception("Iterative solver experience a numerical break down"); } - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -446,7 +377,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -454,11 +385,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -469,20 +400,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient , A. /// The solution , B. /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -493,31 +414,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient , A. /// The solution , B. /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs index bd1d751e..82823bfd 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs @@ -331,11 +331,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// IterationStatus _status = IterationStatus.Indetermined; - /// - /// The iterator that is used to control the iteration process. - /// - Iterator _iterator; - /// /// A flag indicating if the solver has been stopped or not. /// @@ -350,37 +345,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// Initializes a new instance of the class with the default iterator. /// - public CompositeSolver() : this(null) - { - } - - /// - /// Initializes a new instance of the class with the specified iterator. - /// - /// The iterator that will be used to control the iteration process. - public CompositeSolver(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult + public CompositeSolver() { - get - { - return _status; - } } /// @@ -406,10 +372,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -420,7 +386,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -440,9 +406,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } // Load the solvers into our own internal data structure @@ -465,11 +431,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers try { // Reset the iterator and pass it to the solver - _iterator.Reset(); - solver.SetIterator(_iterator); + iterator.Reset(); // Start the solver - solver.Solve(matrix, internalInput, internalResult); + solver.Solve(matrix, internalInput, internalResult, iterator); + _status = iterator.Status; } catch (Exception) { @@ -483,7 +449,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } // There was no fatal breakdown so check the status - if (_iterator.HasConverged) + if (_status == IterationStatus.Converged) { // We're done internalResult.CopyTo(result); @@ -493,7 +459,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // We're not done // Either: // - calculation finished without convergence - if (_iterator.HasStoppedWithoutConvergence) + if (_status == IterationStatus.StoppedWithoutConvergence) { // Copy the internal result to the result vector and // continue with the calculation. @@ -514,9 +480,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Clean up // No longer need the current solver _currentSolver = null; - - // Set the final status - _status = _iterator.Status; } /// @@ -546,10 +509,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -560,16 +523,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { throw Matrix.DimensionsDontMatch(matrix, input, result); } + if (iterator == null) + { + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + } + for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs index 999f1a00..5eae6e5f 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs @@ -69,11 +69,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates the number of BiCGStab steps should be taken /// before switching. @@ -99,7 +94,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// the standard settings and a default preconditioner. /// public GpBiCg() - : this(null, null) { } @@ -108,9 +102,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -121,45 +112,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public GpBiCg(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public GpBiCg(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public GpBiCg(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -210,23 +165,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -246,15 +184,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -265,7 +198,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -305,9 +238,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -354,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // for (k = 0, 1, .... ) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1)) p.Subtract(u, temp); @@ -495,7 +428,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner.Approximate(xtemp, result); // Now check for convergence - if (!ShouldContinue(iterationNumber, result, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -532,7 +465,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -540,11 +473,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -572,20 +505,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -596,31 +519,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs index 50ee9a51..8fafd536 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs @@ -42,16 +42,25 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // TODO: Refactor /// - /// Creates a default iterator with all the objects. + /// Creates the default stop criteria. /// - /// A new object. - public static Iterator CreateDefault() + public static IIterationStopCriterium[] CreateDefaultStopCriteria() { - return new Iterator( + return new IIterationStopCriterium[] + { new FailureStopCriterium(), new DivergenceStopCriterium(), new IterationCountStopCriterium(), - new ResidualStopCriterium()); + new ResidualStopCriterium() + }; + } + + /// + /// Creates a default iterator with all the default stop criteria. + /// + public static Iterator CreateDefault() + { + return new Iterator(CreateDefaultStopCriteria()); } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs b/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs index b54f9cd7..b0494ff0 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs @@ -73,11 +73,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// The collection of starting vectors which are used as the basis for the Krylov sub-space. /// @@ -100,7 +95,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// When using this constructor the solver will use the with /// the standard settings and a default preconditioner. /// - public MlkBiCgStab() : this(null, null) + public MlkBiCgStab() { } @@ -109,9 +104,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -122,45 +114,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public MlkBiCgStab(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public MlkBiCgStab(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -208,15 +164,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - /// /// Gets or sets a series of orthonormal vectors which will be used as basis for the /// Krylov sub-space. @@ -243,18 +190,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } } - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - [DebuggerStepThrough] - get - { - return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; - } - } - /// /// Stops the solve process. /// @@ -273,15 +208,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -292,7 +222,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -332,9 +262,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -404,7 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // FOR (j = 0, 1, 2 ....) var iterationNumber = 0; - while (ShouldContinue(iterationNumber, xtemp, input, residuals)) + while (ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // SOLVE M g~_((j-1)k+k) = g_((j-1)k+k) _preconditioner.Approximate(g[k - 1], gtemp); @@ -462,13 +392,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers temp2.CopyTo(xtemp); // Check convergence and stop if we are converged. - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Calculate the true residual CalculateTrueResidual(matrix, residuals, xtemp, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // We're all good now. // Exit from the while loop. @@ -597,7 +527,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers temp2.CopyTo(residuals); // We can check the residuals here if they're close - if (!ShouldContinue(iterationNumber, xtemp, input, residuals)) + if (!ShouldContinue(iterator, iterationNumber, xtemp, input, residuals)) { // Recalculate the residuals and go round again. This is done to ensure that // we have the proper residuals. @@ -719,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -727,11 +657,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -742,20 +672,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -766,31 +686,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs index 4b45737f..6408e04a 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs @@ -59,11 +59,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// IPreConditioner _preconditioner; - /// - /// The iterative process controller. - /// - Iterator _iterator; - /// /// Indicates if the user has stopped the solver. /// @@ -77,7 +72,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// the standard settings and a default preconditioner. /// public TFQMR() - : this(null, null) { } @@ -86,9 +80,6 @@ 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: /// /// It is possible to set the desired convergence limits. @@ -99,45 +90,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// /// /// - /// The that will be used to monitor the iterative process. - public TFQMR(Iterator iterator) - : this(null, iterator) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// When using this constructor the solver will use the with - /// the standard settings. - /// /// The that will be used to precondition the matrix equation. public TFQMR(IPreConditioner preconditioner) - : this(preconditioner, null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// 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 that will be used to precondition the matrix equation. - /// The that will be used to monitor the iterative process. - public TFQMR(IPreConditioner preconditioner, Iterator iterator) { - _iterator = iterator; _preconditioner = preconditioner; } @@ -150,23 +105,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers _preconditioner = preconditioner; } - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - public void SetIterator(Iterator iterator) - { - _iterator = iterator; - } - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - public IterationStatus IterationResult - { - get { return (_iterator != null) ? _iterator.Status : IterationStatus.Indetermined; } - } - /// /// Stops the solve process. /// @@ -185,15 +123,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector) + public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null) { - if (vector == null) - { - throw new ArgumentNullException(); - } - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result); + Solve(matrix, vector, result, iterator); return result; } @@ -204,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - public void Solve(Matrix matrix, Vector input, Vector result) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null) { // If we were stopped before, we are no longer // We're doing this at the start of the method to ensure @@ -244,9 +177,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Initialize the solver fields // Set the convergence monitor - if (_iterator == null) + if (iterator == null) { - _iterator = Iterator.CreateDefault(); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } if (_preconditioner == null) @@ -295,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // Start the iteration var iterationNumber = 0; - while (ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + while (ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // First part of the step, the even bit if (IsEven(iterationNumber)) @@ -305,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (sigma.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -354,7 +287,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers temp2.CopyTo(x); // Check convergence and see if we can bail - if (!ShouldContinue(iterationNumber, result, input, pseudoResiduals)) + if (!ShouldContinue(iterator, iterationNumber, result, input, pseudoResiduals)) { // Calculate the real values _preconditioner.Approximate(x, result); @@ -365,7 +298,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers CalculateTrueResidual(matrix, temp, result, input); // Now recheck the convergence - if (!ShouldContinue(iterationNumber, result, input, temp)) + if (!ShouldContinue(iterator, iterationNumber, result, input, temp)) { // We're all good now. return; @@ -378,7 +311,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (rho.AlmostEqual(0, 1)) { // FAIL HERE - _iterator.Cancel(); + iterator.Cancel(); break; } @@ -438,7 +371,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// Source . /// Residual . /// true if continue, otherwise false - bool ShouldContinue(int iterationNumber, Vector result, Vector source, Vector residuals) + bool ShouldContinue(Iterator iterator, int iterationNumber, Vector result, Vector source, Vector residuals) { // We stop if either: // - the user has stopped the calculation @@ -446,11 +379,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers if (_hasBeenStopped) { - _iterator.Cancel(); + iterator.Cancel(); return true; } - var status = _iterator.DetermineStatus(iterationNumber, result, source, residuals); + var status = iterator.DetermineStatus(iterationNumber, result, source, residuals); return status == IterationStatus.Running || status == IterationStatus.Indetermined; } @@ -471,20 +404,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input) + public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result); + Solve(matrix, input, result, iterator); return result; } @@ -495,31 +418,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result) + public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null) { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (input == null) - { - throw new ArgumentNullException("input"); - } - - if (result == null) + if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) { - throw new ArgumentNullException("result"); + throw Matrix.DimensionsDontMatch(matrix, input, result); } - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + if (iterator == null) { - throw Matrix.DimensionsDontMatch(matrix, input, result); + iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); } for (var column = 0; column < input.ColumnCount; column++) { - var solution = Solve(matrix, input.Column(column)); + var solution = Solve(matrix, input.Column(column), iterator); foreach (var element in solution.EnumerateNonZeroIndexed()) { result.At(element.Item1, column, element.Item2); diff --git a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs index 4dab99a8..5bfcb97a 100644 --- a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs +++ b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs @@ -46,17 +46,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// void StopSolve(); - /// - /// Sets the that will be used to track the iterative process. - /// - /// The iterator. - void SetIterator(Iterator iterator); - - /// - /// Gets the status of the iteration once the calculation is finished. - /// - IterationStatus IterationResult { get; } - /// /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the /// solution vector and x is the unknown vector. @@ -64,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// The coefficient matrix, A. /// The solution vector, b. /// The result vector, x. - Vector Solve(Matrix matrix, Vector vector); + Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null); /// /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the @@ -73,7 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// The coefficient matrix, A. /// The solution vector, b /// The result vector, x - void Solve(Matrix matrix, Vector input, Vector result); + void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator = null); /// /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the @@ -82,7 +71,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X. - Matrix Solve(Matrix matrix, Matrix input); + Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null); /// /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the @@ -91,6 +80,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// The coefficient matrix, A. /// The solution matrix, B. /// The result matrix, X - void Solve(Matrix matrix, Matrix input, Matrix result); + void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null); } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs index 284a4452..2e6c31f5 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs @@ -101,10 +101,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -146,10 +146,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -224,10 +224,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -263,9 +263,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -295,8 +295,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new BiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new BiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs index e4e91654..9b35de71 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs @@ -101,10 +101,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -226,10 +226,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,9 +265,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -297,8 +297,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new GpBiCg(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new GpBiCg(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs index ac29c757..fe4ddb44 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs @@ -102,10 +102,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,10 +225,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,9 +264,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -296,8 +296,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new MlkBiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new MlkBiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs index 0a6da4e0..59f8594f 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs @@ -102,10 +102,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,10 +225,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,9 +264,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -296,8 +296,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new TFQMR(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new TFQMR(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs index c8f97fc6..7e4e9d89 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs @@ -101,10 +101,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -146,10 +146,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -224,10 +224,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -267,9 +267,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -310,8 +310,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new BiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new BiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs index 2167cc8b..763c7fff 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs @@ -101,10 +101,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -226,10 +226,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,9 +265,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)), }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs index c2e6ffbb..f35930fc 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs @@ -102,10 +102,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,10 +225,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,9 +264,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -307,8 +307,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new MlkBiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new MlkBiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs index 9a2f41bd..4316a337 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs @@ -102,10 +102,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,10 +147,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,10 +225,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,9 +264,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -307,8 +307,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new IterationCountStopCriterium(1000), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new TFQMR(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new TFQMR(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs index 78535989..629dc77c 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs @@ -99,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -144,10 +144,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -222,10 +222,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,9 +265,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -296,8 +296,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10) }); - var solver = new BiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new BiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs index ea31e7f2..757077d7 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs @@ -99,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -145,10 +145,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -224,10 +224,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -267,9 +267,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -298,8 +298,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10) }); - var solver = new GpBiCg(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new GpBiCg(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs index 81f03525..6461b4af 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs @@ -100,10 +100,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -145,10 +145,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -223,10 +223,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,9 +266,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -297,8 +297,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10) }); - var solver = new MlkBiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new MlkBiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs index 16f0e32d..430f958f 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs @@ -100,10 +100,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -145,10 +145,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -223,10 +223,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -262,9 +262,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb); + var resultx = solver.Solve(matrixA, vectorb, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -293,8 +293,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10) }); - var solver = new TFQMR(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new TFQMR(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs index a5c5b1b5..db7ba283 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs @@ -99,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -144,10 +144,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -222,10 +222,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new BiCgStab(monitor); + var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -263,8 +263,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new BiCgStab(monitor); - var resultx = solver.Solve(matrixA, vectorb); + var solver = new BiCgStab(); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -304,8 +304,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new BiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new BiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs index fc38bdfa..b38c926e 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs @@ -99,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -145,10 +145,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -224,10 +224,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new FailureStopCriterium() }); - var solver = new GpBiCg(monitor); + var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,8 +265,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new GpBiCg(monitor); - var resultx = solver.Solve(matrixA, vectorb); + var solver = new GpBiCg(); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -306,8 +306,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new GpBiCg(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new GpBiCg(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs index 20b26e86..5691c91c 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs @@ -101,10 +101,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -146,10 +146,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -228,13 +228,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new MlkBiCgStab(monitor); + var solver = new MlkBiCgStab(); // Solve equation Ax = y Vector x; try { - x = solver.Solve(matrix, y); + x = solver.Solve(matrix, y, monitor); } catch (Exception) { @@ -282,8 +282,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new MlkBiCgStab(monitor); - var resultx = solver.Solve(matrixA, vectorb); + var solver = new MlkBiCgStab(); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -325,8 +325,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new MlkBiCgStab(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new MlkBiCgStab(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) { diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs index 0e3c517c..bca6ab49 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs @@ -100,10 +100,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -145,10 +145,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -223,10 +223,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new DivergenceStopCriterium(), new FailureStopCriterium() }); - var solver = new TFQMR(monitor); + var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y); + var x = solver.Solve(matrix, y, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,8 +264,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new TFQMR(monitor); - var resultx = solver.Solve(matrixA, vectorb); + var solver = new TFQMR(); + var resultx = solver.Solve(matrixA, vectorb, monitor); if (!monitor.HasConverged) { @@ -305,8 +305,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); - var solver = new TFQMR(monitor); - var matrixX = solver.Solve(matrixA, matrixB); + var solver = new TFQMR(); + var matrixX = solver.Solve(matrixA, matrixB, monitor); if (!monitor.HasConverged) {