public void CanSolveForRandomVector(int order) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreEqual(vectorb[i].Real, matrixBReconstruct[i].Real, 1e-5); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, 1e-5); } }
public void CanSolveForRandomMatrix(int order) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium<double>[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0e-7); } } }
public void CanSolveForRandomMatrix(int order) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium <double>[] { new IterationCountStopCriterium <double>(1000), new ResidualStopCriterium(1e-10) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0e-7); } } }
public void CanSolveForRandomVector([Values(5)] int order) { // Due to datatype "float" it can happen that solution will not converge for specific random matrix // That's why we will do 3 tries and downgrade stop criterium each time for (var iteration = 6; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreEqual(vectorb[i], matrixBReconstruct[i], (float)Math.Pow(1.0 / 10.0, iteration - 3)); } return; } }
public void SolveLongMatrixThrowsArgumentException() { var matrix = new SparseMatrix(3, 2); Vector input = new DenseVector(3); var solver = new GpBiCg(); Assert.Throws<ArgumentException>(() => solver.Solve(matrix, input)); }
public void SolveWideMatrix() { var matrix = new SparseMatrix(2, 3); Vector <Complex32> input = new DenseVector(2); var solver = new GpBiCg(); solver.Solve(matrix, input); }
public void SolveLongMatrix() { var matrix = new SparseMatrix(3, 2); Vector <float> input = new DenseVector(3); var solver = new GpBiCg(); solver.Solve(matrix, input); }
public void SolveLongMatrixThrowsArgumentException() { var matrix = new SparseMatrix(3, 2); Vector input = new DenseVector(3); var solver = new GpBiCg(); Assert.Throws <ArgumentException>(() => solver.Solve(matrix, input)); }
public void CanSolveForRandomMatrix(int order) { // Due to datatype "float" it can happen that solution will not converge for specific random matrix // That's why we will do 3 tries and downgrade stop criterium each time for (var iteration = 6; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium <float>[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], (float)Math.Pow(1.0 / 10.0, iteration - 3)); } } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void CanSolveForRandomMatrix(int order) { // Due to datatype "float" it can happen that solution will not converge for specific random matrix // That's why we will do 3 tries and downgrade stop criterium each time for (var iteration = 6; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium<float>[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float)Math.Pow(1.0/10.0, iteration)) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], (float)Math.Pow(1.0 / 10.0, iteration - 3)); } } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void CanSolveForRandomMatrix(int order) { for (var iteration = 5; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium<Complex32>[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreApproximatelyEqual(matrixB[i, j].Real, matrixBReconstruct[i, j].Real, (float)Math.Pow(1.0 / 10.0, iteration - 3)); Assert.AreApproximatelyEqual(matrixB[i, j].Imaginary, matrixBReconstruct[i, j].Imaginary, (float)Math.Pow(1.0 / 10.0, iteration - 3)); } } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void CanSolveForRandomMatrix(int order) { for (var iteration = 5; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order); var monitor = new Iterator(new IIterationStopCriterium <Complex32>[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)) }); var solver = new GpBiCg(monitor); var matrixX = solver.Solve(matrixA, matrixB); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } // The solution X row dimension is equal to the column dimension of A Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount); // The solution X has the same number of columns as B Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount); var matrixBReconstruct = matrixA * matrixX; // Check the reconstruction. for (var i = 0; i < matrixB.RowCount; i++) { for (var j = 0; j < matrixB.ColumnCount; j++) { Assert.AreApproximatelyEqual(matrixB[i, j].Real, matrixBReconstruct[i, j].Real, (float)Math.Pow(1.0 / 10.0, iteration - 3)); Assert.AreApproximatelyEqual(matrixB[i, j].Imaginary, matrixBReconstruct[i, j].Imaginary, (float)Math.Pow(1.0 / 10.0, iteration - 3)); } } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void SolveScaledUnitMatrixAndBackMultiply() { // Create the identity matrix Matrix matrix = SparseMatrix.Identity(100); // Scale it with a funny number matrix.Multiply((float)Math.PI, matrix); // Create the y vector Vector y = new DenseVector(matrix.RowCount, 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
public void CanSolveForRandomVector(int order) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium<double>[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var bReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreApproximatelyEqual(vectorb[i], bReconstruct[i], 1e-7); } }
public void SolveUnitMatrixAndBackMultiply() { // Create the identity matrix var matrix = SparseMatrix.Identity(100); // Create the y vector var y = DenseVector.Create(matrix.RowCount, i => 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator <Complex32>(new IIterationStopCriterium <Complex32>[] { new IterationCountStopCriterium <Complex32>(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
public void CanSolveForRandomVector(int order) { for (var iteration = 5; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreEqual(vectorb[i].Real, matrixBReconstruct[i].Real, (float)Math.Pow(1.0 / 10.0, iteration - 3)); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, (float)Math.Pow(1.0 / 10.0, iteration - 3)); } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void CanSolveForRandomVector([Values(4)] int order) { for (var iteration = 5; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium((float)Math.Pow(1.0 / 10.0, iteration)), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreEqual(vectorb[i].Real, matrixBReconstruct[i].Real, (float)Math.Pow(1.0 / 10.0, iteration - 3)); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, (float)Math.Pow(1.0 / 10.0, iteration - 3)); } return; } Assert.Fail("Solution was not found in 3 tries"); }
public void SolvePoissonMatrixAndBackMultiply() { // Create the matrix var matrix = new SparseMatrix(100); // Assemble the matrix. We assume we're solving the Poisson equation // on a rectangular 10 x 10 grid const int GridSize = 10; // The pattern is: // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0 for (var i = 0; i < matrix.RowCount; i++) { // Insert the first set of -1's if (i > (GridSize - 1)) { matrix[i, i - GridSize] = -1; } // Insert the second set of -1's if (i > 0) { matrix[i, i - 1] = -1; } // Insert the centerline values matrix[i, i] = 4; // Insert the first trailing set of -1's if (i < matrix.RowCount - 1) { matrix[i, i + 1] = -1; } // Insert the second trailing set of -1's if (i < matrix.RowCount - GridSize) { matrix[i, i + GridSize] = -1; } } // Create the y vector Vector<double> y = new DenseVector(matrix.RowCount, 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator(new IIterationStopCriterium<double>[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue(System.Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
public void SolveWideMatrix() { var matrix = new SparseMatrix(2, 3); Vector<float> input = new DenseVector(2); var solver = new GpBiCg(); solver.Solve(matrix, input); }
public void SolvePoissonMatrixAndBackMultiply() { // Create the matrix var matrix = new SparseMatrix(25); // Assemble the matrix. We assume we're solving the Poisson equation // on a rectangular 5 x 5 grid const int GridSize = 5; // The pattern is: // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0 for (var i = 0; i < matrix.RowCount; i++) { // Insert the first set of -1's if (i > (GridSize - 1)) { matrix[i, i - GridSize] = -1; } // Insert the second set of -1's if (i > 0) { matrix[i, i - 1] = -1; } // Insert the centerline values matrix[i, i] = 4; // Insert the first trailing set of -1's if (i < matrix.RowCount - 1) { matrix[i, i + 1] = -1; } // Insert the second trailing set of -1's if (i < matrix.RowCount - GridSize) { matrix[i, i + GridSize] = -1; } } // Create the y vector Vector y = new DenseVector(matrix.RowCount, 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
public void SolveScaledUnitMatrixAndBackMultiply() { // Create the identity matrix var matrix = SparseMatrix.Identity(100); // Scale it with a funny number matrix.Multiply(Math.PI, matrix); // Create the y vector var y = DenseVector.Create(matrix.RowCount, i => 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator<Complex>(new IIterationStopCriterium<Complex>[] { new IterationCountStopCriterium<Complex>(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.HasConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
public void SolveLongMatrix() { var matrix = new SparseMatrix(3, 2); Vector<double> input = new DenseVector(3); var solver = new GpBiCg(); solver.Solve(matrix, input); }
public void CanSolveForRandomVector(int order) { // Due to datatype "float" it can happen that solution will not converge for specific random matrix // That's why we will do 3 tries and downgrade stop criterium each time for (var iteration = 6; iteration > 3; iteration--) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium<float>[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium((float)Math.Pow(1.0/10.0, iteration)), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); if (!(monitor.Status is CalculationConverged)) { // Solution was not found, try again downgrading convergence boundary continue; } Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var bReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreApproximatelyEqual(vectorb[i], bReconstruct[i], (float)Math.Pow(1.0 / 10.0, iteration - 3)); } return; } Assert.Fail("Solution was not found in 3 tries"); }
/// <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 }); // Create Generalized Product Bi-Conjugate Gradient solver var solver = new GpBiCg(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 void SolveUnitMatrixAndBackMultiply() { // Create the identity matrix Matrix<double> matrix = SparseMatrix.Identity(100); // Create the y vector Vector<double> y = new DenseVector(matrix.RowCount, 1); // Create an iteration monitor which will keep track of iterative convergence var monitor = new Iterator(new IIterationStopCriterium<double>[] { new IterationCountStopCriterium(MaximumIterations), new ResidualStopCriterium(ConvergenceBoundary), new DivergenceStopCriterium(), new FailureStopCriterium() }); var solver = new GpBiCg(monitor); // Solve equation Ax = y var x = solver.Solve(matrix, y); // Now compare the results Assert.IsNotNull(x, "#02"); Assert.AreEqual(y.Count, x.Count, "#03"); // Back multiply the vector var z = matrix.Multiply(x); // Check that the solution converged Assert.IsTrue(monitor.Status is CalculationConverged, "#04"); // Now compare the vectors for (var i = 0; i < y.Count; i++) { Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i); } }
/// <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<double>(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<double>(new IIterationStopCriterium<double>[] { iterationCountStopCriterium, residualStopCriterium }); // Create Generalized Product Bi-Conjugate Gradient solver var solver = new GpBiCg(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 void CanSolveForRandomVector([Values(4, 8, 10)] int order) { var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order); var vectorb = MatrixLoader.GenerateRandomDenseVector(order); var monitor = new Iterator(new IIterationStopCriterium[] { new IterationCountStopCriterium(1000), new ResidualStopCriterium(1e-10), }); var solver = new GpBiCg(monitor); var resultx = solver.Solve(matrixA, vectorb); Assert.AreEqual(matrixA.ColumnCount, resultx.Count); var matrixBReconstruct = matrixA * resultx; // Check the reconstruction. for (var i = 0; i < order; i++) { Assert.AreEqual(vectorb[i].Real, matrixBReconstruct[i].Real, 1e-5); Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, 1e-5); } }