Conjugate Gradient Example 1:
The solution to a positive definite linear system is found. The coefficient matrix is stored as a full matrix.
using System;
using Imsl.Math;
public class ConjugateGradientEx1 : ConjugateGradient.IFunction
{
private double[,] a = {
{1.0, - 3.0, 2.0},
{- 3.0, 10.0, - 5.0},
{2.0, - 5.0, 6.0} };
public void Amultp(double[] p, double[] z)
{
double[] w = Matrix.Multiply(a, p);
Array.Copy(w, 0, z, 0, z.Length);
}
public static void Main(String[] args)
{
int n = 3;
double[] b = {27.0, -78.0, 64.0};
double[] solution = null;
ConjugateGradientEx1 atp = new ConjugateGradientEx1();
// Construct Cg object
ConjugateGradient cg = new ConjugateGradient(n, atp);
// Solve Ax=b
solution = cg.Solve(b);
new PrintMatrix("Solution").Print(solution);
}
}
Output
Solution
0
0 1.00000000000116
1 -3.99999999999972
2 6.99999999999984
Link to C# source.