package com.imsl.test.example.math; import com.imsl.math.*; /** *
* Solves a positive definite linear system using the conjugate gradient * method.
* * The solution to a positive definite linear system is found. The coefficient * matrix is stored as a full matrix. The system is \(Ax = b\) where * $$ * A=\begin{bmatrix} * 1.0 & -3.0 & 2.0 \\ * -3.0 & 10.0 & -5.0 \\ * 2.0 & -5.0 & 6.0 * \end{bmatrix} * $$ ** and \( b = (27.0, -78.0, 64.0)^T\)
* * @see Code * @see Output * */ 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}; @Override 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)); } }