public void Should_go_to_the_first_wish_and_return() { var warehouse = new Warehouse(1, 1, 1); var wishes = new DummyPickings { PickingList = new List <PickingPos> { new PickingPos(1, 1, 1, 1, 1, 1) } }; var solver = new CompositeSolver(warehouse, wishes); var solution = solver.Solve(); var shiftPointList = solution.ShiftPointList; var destination = new ShiftPoint(1, 1); var intermediatePoint = new ShiftPoint(1, 0); var wantedSolution = new List <ShiftPoint> { _initShiftPoint, intermediatePoint, destination, intermediatePoint, _initShiftPoint }; Check.That(shiftPointList).IsEqualTo(wantedSolution); }
protected override void Solve(out string answer) { answer = $"Solution not created yet..."; StringBuilder progress = new StringBuilder("Composites checked: "); CompositeSolver compositeSolver = new CompositeSolver(); List <long> oddComposites = new List <long>(); //Pseudo code foreach (long candidate in Enumerable64.Range(1, 34000)) { if (compositeSolver.IsOddComposite(candidate)) { oddComposites.Add(candidate); if (compositeSolver.IsSumOfPrimeAndTwiceSquare(candidate)) { progress.Append($"{candidate}, "); //UpdateProgress(progress.ToString()); } else { answer = $"The smallest odd composite that cannot be written as the sum of a prime and twice a square is: {candidate}."; return; } } } }
public void Should_no_go_in_empty_aisles() { const int aiseLenght = 2; var warehouse = new Warehouse(1, 6, aiseLenght); var clientWish1 = new PickingPos(1, 1, 1, 1, aiseLenght, 1); var clientWish2 = new PickingPos(2, 1, 6, 1, aiseLenght, 1); var wishes = new DummyPickings { PickingList = new List <PickingPos> { clientWish1, clientWish2 } }; var solver = new CompositeSolver(warehouse, wishes); var solution = solver.Solve(); var intermediatePoint = new ShiftPoint(1, 0); var intermediatePoint2 = new ShiftPoint(7, 0); var wantedSolution = new List <ShiftPoint> { _initShiftPoint, intermediatePoint, clientWish1.ConverToShiftPoint(), intermediatePoint, intermediatePoint2, clientWish2.ConverToShiftPoint(), intermediatePoint2, _initShiftPoint }; Check.That(solution.ShiftPointList).IsEqualTo(wantedSolution); }
public void CompositeSolver_SparseFloat() { Matrix <float> matrix = SparseMatrix.OfArray(new[, ] { { 1f, 0f, 0f }, { 0f, 1f, 0f }, { 0f, 0f, 1f } }); Vector <float> vector = new DenseVector(new[] { 1f, 2f, 3f }); IIterativeSolver <float> solver = new CompositeSolver(new List <IIterativeSolverSetup <float> > { new UserBiCgStabFloat() }); CompositeSolver(matrix, vector, solver); }
public void CompositeSolver_DenseFloat() { Matrix <float> matrix = DenseMatrix.OfArray(new[, ] { { 5.00f, 2.00f, -4.00f }, { 3.00f, -7.00f, 6.00f }, { 4.00f, 1.00f, 5.00f } }); Vector <float> vector = new DenseVector(new[] { -7.0f, 38.0f, 43.0f }); IIterativeSolver <float> solver = new CompositeSolver(new List <IIterativeSolverSetup <float> > { new UserBiCgStabFloat() }); CompositeSolver(matrix, vector, solver); }
public void Solve_OneCellEmpty_DoSolves() { var field = new Field(); field[0] = new[] { 0, 8, 9, 0, 0, 0, 0, 0, 0 }; field[1] = new[] { 0, 3, 5, 6, 0, 0, 8, 0, 0 }; field[2] = new[] { 6, 0, 0, 0, 0, 7, 9, 3, 0 }; field[3] = new[] { 0, 0, 2, 7, 6, 9, 4, 0, 3 }; field[4] = new[] { 0, 0, 0, 8, 0, 5, 0, 0, 0 }; field[5] = new[] { 5, 0, 7, 3, 1, 4, 6, 0, 0 }; field[6] = new[] { 0, 7, 6, 2, 0, 0, 0, 0, 9 }; field[7] = new[] { 0, 0, 3, 0, 0, 6, 7, 8, 0 }; field[8] = new[] { 0, 0, 0, 0, 0, 0, 2, 1, 0 }; Print(field); var solver = new CompositeSolver(new ISolverInstance[] { new CrossingSolver(), new OccupationSolver() }); var solvedField = solver.Solve(field); PrintZeroMetric(solvedField); Print(solvedField); solver.IsFieldModified.Should().BeTrue(); solvedField.RowWithDuplicatesIndex().Should().Be(-1); solvedField.ColumnWithDuplicatesIndex().Should().Be(-1); solvedField.SquareWithDuplicatesIndex().Should().Be(-1); for (int row = 0; row < Constraints.Size; row++) { solvedField[row].Distinct().Should().BeEquivalentTo(Enumerable.Range(1, 9)); } for (int col = 0; col < 9; col++) { var aColumn = new List <int>(9); for (int row = 0; row < 9; row++) { aColumn.Add(solvedField[row][col]); } aColumn.Should().BeEquivalentTo(Enumerable.Range(1, 9)); } }
/// <summary> /// The main method that runs the Composite solver. /// </summary> public void UseSolver() { // Create a sparse matrix. For now the size will be 10 x 10 elements Matrix matrix = CreateMatrix(10); // Create the right hand side vector. The size is the same as the matrix // and all values will be 2.0. Vector rightHandSideVector = new DenseVector(10, 2.0); // Create a new iterator. This checks for convergence of the results of the // iterative matrix solver. // In this case we'll create the default iterator IIterator iterator = Iterator.CreateDefault(); // Create the solver CompositeSolver solver = new CompositeSolver(iterator); // Now that all is set we can solve the matrix equation. Vector solutionVector = solver.Solve(matrix, rightHandSideVector); // Another way to get the values is by using the overloaded solve method // In this case the solution vector needs to be of the correct size. solver.Solve(matrix, rightHandSideVector, solutionVector); // Finally you can check the reason the solver finished the iterative process // by calling the SolutionStatus property on the iterator ICalculationStatus status = iterator.Status; if (status is CalculationCancelled) Console.WriteLine("The user cancelled the calculation."); if (status is CalculationIndetermined) Console.WriteLine("Oh oh, something went wrong. The iterative process was never started."); if (status is CalculationConverged) Console.WriteLine("Yippee, the iterative process converged."); if (status is CalculationDiverged) Console.WriteLine("I'm sorry the iterative process diverged."); if (status is CalculationFailure) Console.WriteLine("Oh dear, the iterative process failed."); if (status is CalculationStoppedWithoutConvergence) Console.WriteLine("Oh dear, the iterative process did not converge."); }
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 criteriums to monitor an iterative calculation. There are next available stop criteriums: // - DivergenceStopCriterium: monitors an iterative calculation for signs of divergence; // - FailureStopCriterium: monitors residuals for NaN's; // - IterationCountStopCriterium: monitors the numbers of iteration steps; // - ResidualStopCriterium: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation var iterationCountStopCriterium = new IterationCountStopCriterium(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged var residualStopCriterium = new ResidualStopCriterium(1e-10); // Create monitor with defined stop criteriums var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // 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: CompositeSolver.LoadSolverInformationFromAssembly(Assembly.GetExecutingAssembly()); // Create composite solver var solver = new CompositeSolver(monitor); // 1. Solve the matrix equation var resultX = solver.Solve(matrixA, vectorB); 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(solver.IterationResult); 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(); }
/// <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 criteriums to monitor an iterative calculation. There are next available stop criteriums: // - DivergenceStopCriterium: monitors an iterative calculation for signs of divergence; // - FailureStopCriterium: monitors residuals for NaN's; // - IterationCountStopCriterium: monitors the numbers of iteration steps; // - ResidualStopCriterium: monitors residuals if calculation is considered converged; // Stop calculation if 1000 iterations reached during calculation var iterationCountStopCriterium = new IterationCountStopCriterium(1000); // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged var residualStopCriterium = new ResidualStopCriterium(1e-10); // Create monitor with defined stop criteriums var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium }); // 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: CompositeSolver.LoadSolverInformationFromAssembly(Assembly.GetExecutingAssembly()); // Create composite solver var solver = new CompositeSolver(monitor); // 1. Solve the matrix equation var resultX = solver.Solve(matrixA, vectorB); 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(solver.IterationResult); 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(); }