Conjugate Gradient Example 1:

The solution to a positive definite linear system is found. The coefficient matrix is stored as a full matrix.
import com.imsl.math.*;

public class ConjugateGradientEx1 implements ConjugateGradient.Function{
    
    static private  double[][] a = {
            { 1.0, -3.0, 2.0},
            {-3.0, 10.0, -5.0},
            {2.0, -5.0, 6.0}};
    static private double[] b = {27.0, -78.0, 64.0};
    
    
    public void amultp(double[] p, double[] z) {
              
        double w[] = Matrix.multiply(a, p);
        System.arraycopy(w, 0, z, 0, z.length);
    }
    
    public static void main(String args[]) throws Exception {
        int n = 3;
        
        ConjugateGradientEx1 atp = new ConjugateGradientEx1();
        
        // Construct Cg object
        ConjugateGradient cg = new ConjugateGradient(n, atp);
        
        // Solve Ax=b
        new PrintMatrix("Solution").print(cg.solve(b));
    }
}

Output

Solution
   0   
0   1  
1  -4  
2   7  

Link to Java source.