Example usage for org.apache.commons.math3.linear ConjugateGradient solve

List of usage examples for org.apache.commons.math3.linear ConjugateGradient solve

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ConjugateGradient solve.

Prototype

@Override
public RealVector solve(final RealLinearOperator a, final RealVector b) throws NullArgumentException,
        NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException 

Source Link

Usage

From source file:edu.duke.cs.osprey.tupexp.IterativeCGTupleFitter.java

@Override
double[] doFit() {
    //return fit tuple coefficients

    ConjugateGradient cg = new ConjugateGradient(100000, 1e-6, false);//max_iter; delta; whether to check pos def
    //delta is target ratio of residual norm to true vals norm

    long startTime = System.currentTimeMillis();

    while (true) {
        double iterStartTime = System.currentTimeMillis();

        Atb = calcRHS();//from   w w w.  j  a v  a2  s. c  om
        RealVector ans = cg.solve(AtA, Atb);
        double[] newFitVals = calcFitVals(ans);

        System.out.println(
                "Conjugate gradient fitting time (ms): " + (System.currentTimeMillis() - iterStartTime));

        //boolean done = checkDone(curFitVals, newFitVals);
        double resid = calcResidual(newFitVals);
        System.out.println("Step residual: " + resid);

        if (resid > curResid) {//gotten worse...use previous vals
            System.out.println("Iterative conjugate gradient fitting time (ms): "
                    + (System.currentTimeMillis() - startTime));
            return curCoeffs.toArray();
        } else if (resid > curResid - 1e-4) {//basically converged
            System.out.println("Iterative conjugate gradient fitting time (ms): "
                    + (System.currentTimeMillis() - startTime));
            return ans.toArray();
        } else {//keep going
            curCoeffs = ans;
            curFitVals = newFitVals;
            curResid = resid;
        }
    }
}

From source file:org.eclipse.dataset.LinearAlgebra.java

/**
 * Calculation A x = v by conjugate gradient method with the stopping criterion being
 * that the estimated residual r = v - A x satisfies ||r|| < delta ||v||
 * @param a//w  w  w  . j av a 2 s . c  o m
 * @param v
 * @param maxIterations
 * @param delta parameter used by stopping criterion
 * @return solution of A^-1 v by conjugate gradient method
 */
public static Dataset calcConjugateGradient(Dataset a, Dataset v, int maxIterations, double delta) {
    ConjugateGradient cg = new ConjugateGradient(maxIterations, delta, false);
    return createDataset(cg.solve((RealLinearOperator) createRealMatrix(a), createRealVector(v)));
}