static int Main(string[] args) { if (args.Length < 1 || args.Length > 2) { Console.WriteLine("Use: LinearEquationSolver.exe <matrix.csv> [<number-of-iterations>]"); return(2); } Table table = Table.Load(args[0], new ReadSettings(Delimiter.Comma, false, false, FSharpOption <int> .None, ReadSettings.Default.ColumnTypes)); if (table.Count <= 1) { throw new Exception("Expecting 2 or more columns"); } var n = args.Length >= 2 ? int.Parse(args[1], CultureInfo.InvariantCulture) : 1; var columns = table.Select(column => column.Rows.AsReal as IEnumerable <double>); var A = Matrix <double> .Build.DenseOfColumns(columns.Where((_, i) => i < table.Count - 1)); var b = Vector <double> .Build.DenseOfEnumerable(columns.Last()); var iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); var residualStopCriterion = new ResidualStopCriterion <double>(1e-10); var monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); var solver = new CompositeSolver(SolverSetup <double> .LoadFromAssembly(System.Reflection.Assembly.GetExecutingAssembly())); Vector <double> x = null; for (var i = 0; i < n; i++) { //x = A.Solve(b); x = A.SolveIterative(b, solver, monitor); } Table result = Table.OfColumns(new[] { Column.Create("x", x.ToArray(), FSharpOption <int> .None) }); Table.Save(result, "result.csv"); return(0); }
/// <summary> /// Run example /// </summary> public void Run() { // Format matrix output to console var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone(); formatProvider.TextInfo.ListSeparator = " "; // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients var matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); Console.WriteLine(@"Matrix 'A' with coefficients"); Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // Create vector "b" with the constant terms. var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); Console.WriteLine(@"Vector 'b' with the constant terms"); Console.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation var iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged var residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria var monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Load all suitable solvers from current assembly. Below in this example, there is user-defined solver // "class UserBiCgStab : IIterativeSolverSetup<double>" which uses regular BiCgStab solver. But user may create any other solver // and solver setup classes which implement IIterativeSolverSetup<T> and pass assembly to next function: var solver = new CompositeSolver(SolverSetup <double> .LoadFromAssembly(typeof(CompositeSolver).Assembly)); // 1. Solve the matrix equation var resultX = matrixA.SolveIterative(vectorB, solver, monitor); Console.WriteLine(@"1. Solve the matrix equation"); Console.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; Console.WriteLine(@"2. Solver status of the iterations"); Console.WriteLine(monitor.Status); Console.WriteLine(); // 3. Solution result vector of the matrix equation Console.WriteLine(@"3. Solution result vector of the matrix equation"); Console.WriteLine(resultX.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" var reconstructVecorB = matrixA * resultX; Console.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); Console.WriteLine(); }
public override void ExecuteExample() { // <seealso cref="http://en.wikipedia.org/wiki/Biconjugate_gradient_stabilized_method">Biconjugate gradient stabilized method</seealso> MathDisplay.WriteLine("<b>Biconjugate gradient stabilised iterative solver</b>"); // Format matrix output to console var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone(); formatProvider.TextInfo.ListSeparator = " "; // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients var matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); MathDisplay.WriteLine(@"Matrix 'A' with coefficients"); MathDisplay.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create vector "b" with the constant terms. var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); MathDisplay.WriteLine(@"Vector 'b' with the constant terms"); MathDisplay.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation var iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged var residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria var monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Create Bi-Conjugate Gradient Stabilized solver var solverBiCgStab = new BiCgStab(); // 1. Solve the matrix equation var resultX = matrixA.SolveIterative(vectorB, solverBiCgStab, monitor); MathDisplay.WriteLine(@"1. Solve the matrix equation"); MathDisplay.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; MathDisplay.WriteLine(@"2. Solver status of the iterations"); MathDisplay.WriteLine(monitor.Status.ToString()); MathDisplay.WriteLine(); // 3. Solution result vector of the matrix equation MathDisplay.WriteLine(@"3. Solution result vector of the matrix equation"); MathDisplay.WriteLine(resultX.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" var reconstructVecorB = matrixA * resultX; MathDisplay.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); MathDisplay.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Generalized product biconjugate gradient solver</b>"); // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); MathDisplay.WriteLine(@"Matrix 'A' with coefficients"); MathDisplay.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create vector "b" with the constant terms. vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); MathDisplay.WriteLine(@"Vector 'b' with the constant terms"); MathDisplay.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Create Generalized Product Bi-Conjugate Gradient solver var solverGpBiCg = new GpBiCg(); // 1. Solve the matrix equation resultX = matrixA.SolveIterative(vectorB, solverGpBiCg, monitor); MathDisplay.WriteLine(@"1. Solve the matrix equation"); MathDisplay.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; MathDisplay.WriteLine(@"2. Solver status of the iterations"); MathDisplay.WriteLine(monitor.Status.ToString()); MathDisplay.WriteLine(); // 3. Solution result vector of the matrix equation MathDisplay.WriteLine(@"3. Solution result vector of the matrix equation"); MathDisplay.WriteLine(resultX.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" reconstructVecorB = matrixA * resultX; MathDisplay.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); MathDisplay.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Composite linear equation solver</b>"); // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); MathDisplay.WriteLine(@"Matrix 'A' with coefficients"); MathDisplay.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create vector "b" with the constant terms. vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); MathDisplay.WriteLine(@"Vector 'b' with the constant terms"); MathDisplay.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Load all suitable solvers from current assembly. Below (see UserBiCgStab) there is a custom user-defined solver // "class UserBiCgStab : IIterativeSolverSetup<double>" which derives from the regular BiCgStab solver. However users can // create any other solver and solver setup classes that implement IIterativeSolverSetup<T> and load the assembly that // contains them using the following function: var solverComp = new CompositeSolver(SolverSetup <double> .LoadFromAssembly(Assembly.GetExecutingAssembly())); // 1. Solve the linear system resultX = matrixA.SolveIterative(vectorB, solverComp, monitor); MathDisplay.WriteLine(@"1. Solve the matrix equation"); MathDisplay.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; MathDisplay.WriteLine(@"2. Solver status of the iterations"); MathDisplay.WriteLine(monitor.Status.ToString()); MathDisplay.WriteLine(); // 3. Solution result vector of the matrix equation MathDisplay.WriteLine(@"3. Solution result vector of the matrix equation"); MathDisplay.WriteLine(resultX.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" reconstructVecorB = matrixA * resultX; MathDisplay.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); MathDisplay.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Multiple-Lanczos biconjugate gradient stabilised iterative solver</b>"); // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); MathDisplay.WriteLine(@"Matrix 'A' with coefficients"); MathDisplay.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create vector "b" with the constant terms. vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); MathDisplay.WriteLine(@"Vector 'b' with the constant terms"); MathDisplay.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Create Multiple-Lanczos Bi-Conjugate Gradient Stabilized solver var solverLanczos = new MlkBiCgStab(); // 1. Solve the matrix equation resultX = matrixA.SolveIterative(vectorB, solverLanczos, monitor); MathDisplay.WriteLine(@"1. Solve the matrix equation"); MathDisplay.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; MathDisplay.WriteLine(@"2. Solver status of the iterations"); MathDisplay.WriteLine(monitor.Status.ToString()); MathDisplay.WriteLine(); // 3. Solution result vector of the matrix equation MathDisplay.WriteLine(@"3. Solution result vector of the matrix equation"); MathDisplay.WriteLine(resultX.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" reconstructVecorB = matrixA * resultX; MathDisplay.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); MathDisplay.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Transpose freee quasi-minimal residual iterative solver</b>"); // Solve next system of linear equations (Ax=b): // 5*x + 2*y - 4*z = -7 // 3*x - 7*y + 6*z = 38 // 4*x + 1*y + 5*z = 43 // Create matrix "A" with coefficients matrixA = DenseMatrix.OfArray(new[, ] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } }); MathDisplay.WriteLine(@"Matrix 'A' with coefficients"); MathDisplay.WriteLine(matrixA.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create vector "b" with the constant terms. vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 }); MathDisplay.WriteLine(@"Vector 'b' with the constant terms"); MathDisplay.WriteLine(vectorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // Create stop criteria to monitor an iterative calculation. There are next available stop criteria: // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence; // - FailureStopCriterion: monitors residuals for NaN's; // - IterationCountStopCriterion: monitors the numbers of iteration steps; // - ResidualStopCriterion: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged residualStopCriterion = new ResidualStopCriterion <double>(1e-10); // Create monitor with defined stop criteria monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion); // Create Transpose Free Quasi-Minimal Residual solver var solverTFQMR = new TFQMR(); // 1. Solve the matrix equation resultX = matrixA.SolveIterative(vectorB, solverTFQMR, monitor); MathDisplay.WriteLine(@"1. Solve the matrix equation"); MathDisplay.WriteLine(); // 2. Check solver status of the iterations. // Solver has property IterationResult which contains the status of the iteration once the calculation is finished. // Possible values are: // - CalculationCancelled: calculation was cancelled by the user; // - CalculationConverged: calculation has converged to the desired convergence levels; // - CalculationDiverged: calculation diverged; // - CalculationFailure: calculation has failed for some reason; // - CalculationIndetermined: calculation is indetermined, not started or stopped; // - CalculationRunning: calculation is running and no results are yet known; // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved; MathDisplay.WriteLine(@"2. Solver status of the iterations"); MathDisplay.WriteLine(monitor.Status.ToString()); MathDisplay.WriteLine(); // 3. Solution result vector of the matrix equation MathDisplay.WriteLine(@"3. Solution result vector of the matrix equation"); MathDisplay.WriteLine(resultX.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); // 4. Verify result. Multiply coefficient matrix "A" by result vector "x" reconstructVecorB = matrixA * resultX; MathDisplay.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'"); MathDisplay.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider)); MathDisplay.WriteLine(); }