diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs index a43e5121..48b04817 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/BiCgStabSolver.cs @@ -111,7 +111,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var solver = new BiCgStab(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB, monitor); + var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs index 01b7fd8b..2cd6b69d 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/CompositeSolverExample.cs @@ -112,7 +112,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var solver = new CompositeSolver(SolverSetup.LoadFromAssembly(Assembly.GetExecutingAssembly())); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB, monitor); + var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs index 330be972..ce2b7a02 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/GpBiCgSolver.cs @@ -109,7 +109,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var solver = new GpBiCg(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB, monitor); + var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs index 6b6d3672..99f8f2c1 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/MlkBiCgStabSolver.cs @@ -110,7 +110,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var solver = new MlkBiCgStab(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB, monitor); + var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); diff --git a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs index 05094df1..4ebc8502 100644 --- a/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs +++ b/src/Examples/LinearAlgebra/IterativeSolvers/TFQMRSolver.cs @@ -110,7 +110,7 @@ namespace Examples.LinearAlgebra.IterativeSolversExamples var solver = new TFQMR(); // 1. Solve the matrix equation - var resultX = solver.Solve(matrixA, vectorB, monitor); + var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index a5ffe88a..b7de0309 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -29,9 +29,12 @@ // using System; +using MathNet.Numerics.LinearAlgebra.Solvers; namespace MathNet.Numerics.LinearAlgebra.Double { + using Solvers.StopCriterium; + internal class GenericBuilder : IGenericBuilder { public double Zero @@ -63,11 +66,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double { return new SparseVector(size); } + + public IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations) + { + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; + } } } namespace MathNet.Numerics.LinearAlgebra.Single { + using Solvers.StopCriterium; + internal class GenericBuilder : IGenericBuilder { public float Zero @@ -99,11 +115,23 @@ namespace MathNet.Numerics.LinearAlgebra.Single { return new SparseVector(size); } + + public IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations) + { + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; + } } } namespace MathNet.Numerics.LinearAlgebra.Complex { + using Solvers.StopCriterium; #if NOSYSNUMERICS using Complex = Numerics.Complex; @@ -142,11 +170,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { return new SparseVector(size); } + + public IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations) + { + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; + } } } namespace MathNet.Numerics.LinearAlgebra.Complex32 { + using Solvers.StopCriterium; + internal class GenericBuilder : IGenericBuilder { public Numerics.Complex32 Zero @@ -178,6 +219,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { return new SparseVector(size); } + + public IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations) + { + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; + } } } @@ -232,6 +284,8 @@ namespace MathNet.Numerics.LinearAlgebra /// /// The size of the vector. Vector SparseVector(int size); + + IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000); } internal static class Builder where T : struct, IEquatable, IFormattable diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs index 3c86293f..da6f0829 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs @@ -98,7 +98,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -115,11 +115,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw Matrix.DimensionsDontMatch(input, result); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -277,67 +275,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient , A. - /// The solution , b. - /// The result , x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs index 58d72645..329588b3 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/CompositeSolver.cs @@ -77,7 +77,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -89,11 +89,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); } // Create a copy of the solution and result vectors so we can use them @@ -112,7 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers iterator.Reset(); // Start the solver - solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2); + solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2 ?? preconditioner); status = iterator.Status; } catch (Exception) @@ -151,67 +154,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers } } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs index 762d4ce3..d9f89ae6 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs @@ -161,7 +161,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -173,11 +173,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw Matrix.DimensionsDontMatch(matrix, input, result); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -376,67 +374,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs deleted file mode 100644 index c38d6ec8..00000000 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs +++ /dev/null @@ -1,73 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2010 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Complex.Solvers.StopCriterium; -using MathNet.Numerics.LinearAlgebra.Solvers; - -namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers -{ - -#if NOSYSNUMERICS - using Complex = Numerics.Complex; -#else - using Complex = System.Numerics.Complex; -#endif - - /// - /// An iterator that is used to check if an iterative calculation should continue or stop. - /// - public static class Iterator - { - - // TODO: Refactor - - /// - /// Creates the default stop criteria. - /// - public static IIterationStopCriterium[] CreateDefaultStopCriteria() - { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - 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 2827d730..b34e19e5 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs @@ -247,7 +247,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -259,11 +259,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw Matrix.DimensionsDontMatch(matrix, input, result); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -541,67 +539,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers // copy the temporary result to the real result vector xtemp.CopyTo(result); } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs index 32222bb9..53da4217 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs @@ -95,7 +95,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -107,11 +107,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers throw Matrix.DimensionsDontMatch(matrix, input, result); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -277,67 +275,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs index 3619e7fe..c09fb1f8 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs @@ -91,7 +91,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -108,11 +108,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -270,67 +268,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient , A. - /// The solution , b. - /// The result , x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs index 75fde196..bb7e97d3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/CompositeSolver.cs @@ -70,7 +70,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -82,11 +82,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); } // Create a copy of the solution and result vectors so we can use them @@ -105,7 +108,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers iterator.Reset(); // Start the solver - solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2); + solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2 ?? preconditioner); status = iterator.Status; } catch (Exception) @@ -144,67 +147,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers } } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs index e113020d..6257fb8d 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs @@ -154,7 +154,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -171,11 +171,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -374,67 +372,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs deleted file mode 100644 index c1539b89..00000000 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs +++ /dev/null @@ -1,68 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2010 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.StopCriterium; -using MathNet.Numerics.LinearAlgebra.Solvers; - -namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers -{ - using Numerics; - - /// - /// An iterator that is used to check if an iterative calculation should continue or stop. - /// - public static class Iterator - { - - // TODO: Refactor - - /// - /// Creates the default stop criteria. - /// - public static IIterationStopCriterium[] CreateDefaultStopCriteria() - { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - 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 a9686192..9e27dd7e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs @@ -240,7 +240,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -257,11 +257,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -539,67 +537,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers // copy the temporary result to the real result vector xtemp.CopyTo(result); } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs index 9f810c29..c8799b7f 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs @@ -87,7 +87,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -104,11 +104,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -274,67 +272,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = null) - { - if (matrix.RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) - { - throw Matrix.DimensionsDontMatch(input, matrix, result); - } - - if (iterator == null) - { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs index b263415d..ed19649d 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs @@ -91,7 +91,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -108,11 +108,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -270,67 +268,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient , A. - /// The solution , b. - /// The result , x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs index b26fad67..994f65d0 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/CompositeSolver.cs @@ -70,7 +70,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -82,11 +82,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); } // Create a copy of the solution and result vectors so we can use them @@ -105,7 +108,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers iterator.Reset(); // Start the solver - solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2); + solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2 ?? preconditioner); status = iterator.Status; } catch (Exception) @@ -144,67 +147,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers } } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs index 20b1fbf9..0a818da2 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs @@ -160,7 +160,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -177,11 +177,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -380,67 +378,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs deleted file mode 100644 index afbca83b..00000000 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs +++ /dev/null @@ -1,66 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Double.Solvers.StopCriterium; -using MathNet.Numerics.LinearAlgebra.Solvers; - -namespace MathNet.Numerics.LinearAlgebra.Double.Solvers -{ - /// - /// An iterator that is used to check if an iterative calculation should continue or stop. - /// - public static class Iterator - { - - // TODO: Refactor - - /// - /// Creates the default stop criteria. - /// - public static IIterationStopCriterium[] CreateDefaultStopCriteria() - { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - 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 72f0d315..7a264119 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs @@ -240,7 +240,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -257,11 +257,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -539,67 +537,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers // copy the temporary result to the real result vector xtemp.CopyTo(result); } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs index 21d9f875..a9890f57 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs @@ -87,7 +87,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -104,11 +104,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -274,67 +272,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Matrix.Factorization.cs b/src/Numerics/LinearAlgebra/Matrix.Factorization.cs deleted file mode 100644 index ba1fd360..00000000 --- a/src/Numerics/LinearAlgebra/Matrix.Factorization.cs +++ /dev/null @@ -1,78 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Factorization; - -namespace MathNet.Numerics.LinearAlgebra -{ - /// - /// Defines the base class for Matrix classes. - /// - public abstract partial class Matrix - { - /// - /// Computes the Cholesky decomposition for a matrix. - /// - /// The Cholesky decomposition object. - public abstract Cholesky Cholesky(); - - /// - /// Computes the LU decomposition for a matrix. - /// - /// The LU decomposition object. - public abstract LU LU(); - - /// - /// Computes the QR decomposition for a matrix. - /// - /// The type of QR factorization to perform. - /// The QR decomposition object. - public abstract QR QR(QRMethod method = QRMethod.Thin); - - /// - /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. - /// - /// The QR decomposition object. - public abstract GramSchmidt GramSchmidt(); - - /// - /// Computes the SVD decomposition for a matrix. - /// - /// Compute the singular U and VT vectors or not. - /// The SVD decomposition object. - public abstract Svd Svd(bool computeVectors); - - /// - /// Computes the EVD decomposition for a matrix. - /// - /// The EVD decomposition object. - public abstract Evd Evd(); - } -} diff --git a/src/Numerics/LinearAlgebra/Matrix.Solve.cs b/src/Numerics/LinearAlgebra/Matrix.Solve.cs new file mode 100644 index 00000000..c41994ab --- /dev/null +++ b/src/Numerics/LinearAlgebra/Matrix.Solve.cs @@ -0,0 +1,284 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2013 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using MathNet.Numerics.LinearAlgebra.Factorization; +using MathNet.Numerics.LinearAlgebra.Solvers; + +namespace MathNet.Numerics.LinearAlgebra +{ + /// + /// Defines the base class for Matrix classes. + /// + public abstract partial class Matrix + { + + // Factorizations + + /// + /// Computes the Cholesky decomposition for a matrix. + /// + /// The Cholesky decomposition object. + public abstract Cholesky Cholesky(); + + /// + /// Computes the LU decomposition for a matrix. + /// + /// The LU decomposition object. + public abstract LU LU(); + + /// + /// Computes the QR decomposition for a matrix. + /// + /// The type of QR factorization to perform. + /// The QR decomposition object. + public abstract QR QR(QRMethod method = QRMethod.Thin); + + /// + /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. + /// + /// The QR decomposition object. + public abstract GramSchmidt GramSchmidt(); + + /// + /// Computes the SVD decomposition for a matrix. + /// + /// Compute the singular U and VT vectors or not. + /// The SVD decomposition object. + public abstract Svd Svd(bool computeVectors); + + /// + /// Computes the EVD decomposition for a matrix. + /// + /// The EVD decomposition object. + public abstract Evd Evd(); + + + // Iterative Solvers: Try + + /// + /// Solves the matrix equation Ax = b, where A is the coefficient matrix (this matrix), b is the solution vector and x is the unknown vector. + /// + /// The solution vector b. + /// The result vector x. + /// The iterative solver to use. + public IterationStatus TrySolveIterative(Vector input, Vector result, IIterativeSolver solver, IPreconditioner preconditioner, Iterator iterator = null) + { + if (iterator == null) + { + iterator = new Iterator(Builder.IterativeSolverStopCriteria()); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); + } + + solver.Solve(this, input, result, iterator, preconditioner); + + return iterator.Status; + } + + /// + /// Solves the matrix equation AX = B, where A is the coefficient matrix (this matrix), B is the solution matrix and X is the unknown matrix. + /// + /// The solution matrix B. + /// The result matrix X + /// The iterative solver to use. + public IterationStatus TrySolveIterative(Matrix input, Matrix result, IIterativeSolver solver, IPreconditioner preconditioner, Iterator iterator = null) + { + if (RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount) + { + throw DimensionsDontMatch(this, input, result); + } + + if (iterator == null) + { + iterator = new Iterator(Builder.IterativeSolverStopCriteria()); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); + } + + for (var column = 0; column < input.ColumnCount; column++) + { + var solution = Builder.DenseVector(RowCount); + + solver.Solve(this, input.Column(column), solution, iterator, preconditioner); + + foreach (var element in solution.EnumerateNonZeroIndexed()) + { + result.At(element.Item1, column, element.Item2); + } + } + + return iterator.Status; + } + + public IterationStatus TrySolveIterative(Vector input, Vector result, IIterativeSolver solver, Iterator iterator) + { + var preconditioner = new UnitPreconditioner(); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Matrix input, Matrix result, IIterativeSolver solver, Iterator iterator) + { + var preconditioner = new UnitPreconditioner(); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Vector input, Vector result, IIterativeSolver solver) + { + var preconditioner = new UnitPreconditioner(); + var iterator = new Iterator(Builder.IterativeSolverStopCriteria()); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Matrix input, Matrix result, IIterativeSolver solver) + { + var preconditioner = new UnitPreconditioner(); + var iterator = new Iterator(Builder.IterativeSolverStopCriteria()); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Vector input, Vector result, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) + { + var iterator = new Iterator(stopCriteria.Length == 0 ? Builder.IterativeSolverStopCriteria() : stopCriteria); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Matrix input, Matrix result, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) + { + var iterator = new Iterator(stopCriteria.Length == 0 ? Builder.IterativeSolverStopCriteria() : stopCriteria); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Vector input, Vector result, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) + { + var preconditioner = new UnitPreconditioner(); + var iterator = new Iterator(stopCriteria.Length == 0 ? Builder.IterativeSolverStopCriteria() : stopCriteria); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + public IterationStatus TrySolveIterative(Matrix input, Matrix result, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) + { + var preconditioner = new UnitPreconditioner(); + var iterator = new Iterator(stopCriteria.Length == 0 ? Builder.IterativeSolverStopCriteria() : stopCriteria); + return TrySolveIterative(input, result, solver, preconditioner, iterator); + } + + + // Iterative Solvers: Simple + + public Vector SolveIterative(Vector input, IIterativeSolver solver, IPreconditioner preconditioner, Iterator iterator) + { + var result = Builder.DenseVector(RowCount); + TrySolveIterative(input, result, solver, preconditioner, iterator); + return result; + } + + public Matrix SolveIterative(Matrix input, IIterativeSolver solver, IPreconditioner preconditioner, Iterator iterator) + { + var result = Builder.DenseMatrix(input.RowCount, input.ColumnCount); + TrySolveIterative(input, result, solver, preconditioner, iterator); + return result; + } + + public Vector SolveIterative(Vector input, IIterativeSolver solver, Iterator iterator) + { + var result = Builder.DenseVector(RowCount); + TrySolveIterative(input, result, solver, iterator); + return result; + } + + public Matrix SolveIterative(Matrix input, IIterativeSolver solver, Iterator iterator) + { + var result = Builder.DenseMatrix(input.RowCount, input.ColumnCount); + TrySolveIterative(input, result, solver, iterator); + return result; + } + + /// + /// Solves the matrix equation Ax = b, where A is the coefficient matrix (this matrix), b is the solution vector and x is the unknown vector. + /// + /// The solution vector b. + /// The iterative solver to use. + /// The result vector x. + public Vector SolveIterative(Vector input, IIterativeSolver solver) + { + var result = Builder.DenseVector(RowCount); + TrySolveIterative(input, result, solver); + return result; + } + + /// + /// Solves the matrix equation AX = B, where A is the coefficient matrix (this matrix), B is the solution matrix and X is the unknown matrix. + /// + /// The solution matrix B. + /// The iterative solver to use. + /// The result matrix X. + public Matrix SolveIterative(Matrix input, IIterativeSolver solver) + { + var result = Builder.DenseMatrix(input.RowCount, input.ColumnCount); + TrySolveIterative(input, result, solver); + return result; + } + + public Vector SolveIterative(Vector input, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) + { + var result = Builder.DenseVector(RowCount); + TrySolveIterative(input, result, solver, preconditioner, stopCriteria); + return result; + } + + public Matrix SolveIterative(Matrix input, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) + { + var result = Builder.DenseMatrix(input.RowCount, input.ColumnCount); + TrySolveIterative(input, result, solver, preconditioner, stopCriteria); + return result; + } + + public Vector SolveIterative(Vector input, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) + { + var result = Builder.DenseVector(RowCount); + TrySolveIterative(input, result, solver, stopCriteria); + return result; + } + + public Matrix SolveIterative(Matrix input, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) + { + var result = Builder.DenseMatrix(input.RowCount, input.ColumnCount); + TrySolveIterative(input, result, solver, stopCriteria); + return result; + } + } +} diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs index 3eaec91f..c9618ab1 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs @@ -91,7 +91,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -108,11 +108,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -270,67 +268,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient , A. - /// The solution , b. - /// The result , x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient , A. - /// The solution , B. - /// The result , X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs index c7bee637..d4503b1d 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/CompositeSolver.cs @@ -70,7 +70,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -82,11 +82,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); + } + + if (preconditioner == null) + { + preconditioner = new UnitPreconditioner(); } // Create a copy of the solution and result vectors so we can use them @@ -105,7 +108,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers iterator.Reset(); // Start the solver - solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2); + solver.Item1.Solve(matrix, internalInput, internalResult, iterator, solver.Item2 ?? preconditioner); status = iterator.Status; } catch (Exception) @@ -144,67 +147,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers } } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs index 25bf38b5..09c78625 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs @@ -154,7 +154,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -171,11 +171,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -374,67 +372,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs deleted file mode 100644 index 8fafd536..00000000 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs +++ /dev/null @@ -1,66 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2010 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Single.Solvers.StopCriterium; -using MathNet.Numerics.LinearAlgebra.Solvers; - -namespace MathNet.Numerics.LinearAlgebra.Single.Solvers -{ - /// - /// An iterator that is used to check if an iterative calculation should continue or stop. - /// - public static class Iterator - { - - // TODO: Refactor - - /// - /// Creates the default stop criteria. - /// - public static IIterationStopCriterium[] CreateDefaultStopCriteria() - { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(), - 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 c6ae4702..e5b67495 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs @@ -243,7 +243,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -260,11 +260,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -542,67 +540,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers // copy the temporary result to the real result vector xtemp.CopyTo(result); } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs index 03f56cce..d7077524 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs @@ -87,7 +87,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, Iterator iterator = null, IPreconditioner preconditioner = null) + public void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner) { if (matrix.RowCount != matrix.ColumnCount) { @@ -104,11 +104,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers throw Matrix.DimensionsDontMatch(input, matrix); } - // Initialize the solver fields - // Set the convergence monitor if (iterator == null) { - iterator = new Iterator(Iterator.CreateDefaultStopCriteria()); + iterator = new Iterator(); } if (preconditioner == null) @@ -274,67 +272,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers iterationNumber++; } } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - public void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = 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()); - } - - if (preconditioner == null) - { - preconditioner = new UnitPreconditioner(); - } - - for (var column = 0; column < input.ColumnCount; column++) - { - var solution = Solve(matrix, input.Column(column), iterator, preconditioner); - foreach (var element in solution.EnumerateNonZeroIndexed()) - { - result.At(element.Item1, column, element.Item2); - } - } - } - - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - public Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = new DenseVector(matrix.RowCount); - Solve(matrix, vector, result, iterator, preconditioner); - return result; - } - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - public Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null) - { - var result = matrix.CreateMatrix(input.RowCount, input.ColumnCount); - Solve(matrix, input, result, iterator, preconditioner); - return result; - } } } diff --git a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs index 066b1fd4..76c53aed 100644 --- a/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs +++ b/src/Numerics/LinearAlgebra/Solvers/IIterativeSolver.cs @@ -38,15 +38,6 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// public interface IIterativeSolver where T : struct, IEquatable, IFormattable { - /// - /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the - /// solution vector and x is the unknown vector. - /// - /// The coefficient matrix, A. - /// The solution vector, b. - /// The result vector, x. - Vector Solve(Matrix matrix, Vector vector, Iterator iterator = null, IPreconditioner preconditioner = null); - /// /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the /// solution vector and x is the unknown vector. @@ -54,24 +45,6 @@ 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, Iterator iterator = null, IPreconditioner preconditioner = null); - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X. - Matrix Solve(Matrix matrix, Matrix input, Iterator iterator = null, IPreconditioner preconditioner = null); - - /// - /// Solves the matrix equation AX = B, where A is the coefficient matrix, B is the - /// solution matrix and X is the unknown matrix. - /// - /// The coefficient matrix, A. - /// The solution matrix, B. - /// The result matrix, X - void Solve(Matrix matrix, Matrix input, Matrix result, Iterator iterator = null, IPreconditioner preconditioner = null); + void Solve(Matrix matrix, Vector input, Vector result, Iterator iterator, IPreconditioner preconditioner); } } diff --git a/src/Numerics/LinearAlgebra/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Solvers/Iterator.cs index 470254bc..405a7b9a 100644 --- a/src/Numerics/LinearAlgebra/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Solvers/Iterator.cs @@ -51,6 +51,14 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers /// IterationStatus _status = IterationStatus.Continue; + /// + /// Initializes a new instance of the class with the default stop criteria. + /// + public Iterator() + { + _stopCriteria = new List>(Matrix.Builder.IterativeSolverStopCriteria()); + } + /// /// Initializes a new instance of the class with the specified stop criteria. /// diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index a2a08745..001240d9 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -122,7 +122,7 @@ - + @@ -243,7 +243,6 @@ - @@ -269,7 +268,6 @@ - @@ -311,7 +309,6 @@ - @@ -340,7 +337,6 @@ - diff --git a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs index 240ce66f..d79147c5 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(2); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(3); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -104,7 +104,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -149,7 +149,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,7 +265,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ }); var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -296,7 +296,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new ResidualStopCriterium(1e-10), }); var solver = new BiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 48f2e0ff..ab5884ab 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(2); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(3); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -104,7 +104,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -229,7 +229,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -267,7 +267,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ }); var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -298,7 +298,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new ResidualStopCriterium(1e-10), }); var solver = new GpBiCg(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 dfb7eff3..c96bc7f8 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(2); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(3); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -105,7 +105,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -228,7 +228,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,7 +266,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ }); var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -297,7 +297,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new ResidualStopCriterium(1e-10), }); var solver = new MlkBiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 a0e8682d..e67b1dad 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(2); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var input = new DenseVector(3); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -105,7 +105,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -228,7 +228,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,7 +266,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ }); var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -297,7 +297,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ new ResidualStopCriterium(1e-10), }); var solver = new TFQMR(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 3c35e76b..1a5f2380 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(2); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(3); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -104,7 +104,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -149,7 +149,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -269,9 +269,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat }); var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -311,9 +311,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new BiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs index 95134a7f..0343063f 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(2); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(3); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -104,7 +104,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -229,7 +229,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -267,9 +267,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat }); var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs index 033237a7..ce4a6829 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(2); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(3); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -105,7 +105,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -228,7 +228,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,9 +266,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat }); var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -308,9 +308,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new MlkBiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs index c2fa4c47..94b82325 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(2); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var input = new DenseVector(3); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -105,7 +105,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -228,7 +228,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,9 +266,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat }); var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -308,9 +308,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new TFQMR(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs index f25d97f7..6dc6ac79 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(2); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(3); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -102,7 +102,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,7 +147,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,7 +225,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -267,7 +267,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative }); var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -297,7 +297,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new ResidualStopCriterium(1e-10) }); var solver = new BiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 b8e72673..6d7e1fd3 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(2); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(3); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -102,7 +102,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -148,7 +148,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -269,7 +269,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative }); var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -299,7 +299,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new ResidualStopCriterium(1e-10) }); var solver = new GpBiCg(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 ebc95618..4c09c6f3 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(2); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(3); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -103,7 +103,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -148,7 +148,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -226,7 +226,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -268,7 +268,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative }); var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -298,7 +298,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new ResidualStopCriterium(1e-10) }); var solver = new MlkBiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 0606e466..6085d7de 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(2); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var input = new DenseVector(3); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -103,7 +103,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -148,7 +148,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -226,7 +226,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,7 +264,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative }); var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA*resultx; @@ -294,7 +294,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative new ResidualStopCriterium(1e-10) }); var solver = new TFQMR(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, 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 3c924e17..d45b3c79 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(2); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(3); var solver = new BiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -102,7 +102,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -147,7 +147,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -225,7 +225,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new BiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -264,9 +264,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new BiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -305,9 +305,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new BiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs index 2f26796d..b2466961 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(2); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(3); var solver = new GpBiCg(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -102,7 +102,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -148,7 +148,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new GpBiCg(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -266,9 +266,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new GpBiCg(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -307,9 +307,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new GpBiCg(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs index b5734135..cb568dee 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs @@ -64,7 +64,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(2); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -77,7 +77,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(3); var solver = new MlkBiCgStab(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -104,7 +104,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -149,7 +149,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new MlkBiCgStab(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -234,14 +234,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative Vector x; try { - x = solver.Solve(matrix, y, monitor); + x = matrix.SolveIterative(y, solver, monitor); } catch (Exception) { continue; } - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { continue; } @@ -283,9 +283,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new MlkBiCgStab(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -326,9 +326,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new MlkBiCgStab(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; diff --git a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs index 25f8c054..89a97d0f 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(2); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -76,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var input = new DenseVector(3); var solver = new TFQMR(); - Assert.Throws(() => solver.Solve(matrix, input)); + Assert.Throws(() => matrix.SolveIterative(input, solver)); } /// @@ -103,7 +103,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -148,7 +148,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -226,7 +226,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative var solver = new TFQMR(); // Solve equation Ax = y - var x = solver.Solve(matrix, y, monitor); + var x = matrix.SolveIterative(y, solver, monitor); // Now compare the results Assert.IsNotNull(x, "#02"); @@ -265,9 +265,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new TFQMR(); - var resultx = solver.Solve(matrixA, vectorb, monitor); + var resultx = matrixA.SolveIterative(vectorb, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue; @@ -306,9 +306,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative new ResidualStopCriterium((float) Math.Pow(1.0/10.0, iteration)) }); var solver = new TFQMR(); - var matrixX = solver.Solve(matrixA, matrixB, monitor); + var matrixX = matrixA.SolveIterative(matrixB, solver, monitor); - if (!(monitor.Status == IterationStatus.Converged)) + if (monitor.Status != IterationStatus.Converged) { // Solution was not found, try again downgrading convergence boundary continue;