Search in sources :

Example 1 with GVector

use of javax.vecmath.GVector in project bb4-common by bb4.

the class ConjugateGradientSolver method solve.

/**
 * Find a solution or return the initial guess if something goes wrong.
 * @param initialGuess the initial guess for the solution x, x0
 * @return solution vector
 */
public GVector solve(GVector initialGuess) {
    GVector x = new GVector(initialGuess);
    GVector tempv = new GVector(initialGuess);
    tempv.mul(matrix, initialGuess);
    GVector bb = new GVector(b);
    bb.sub(tempv);
    GVector r = new GVector(bb);
    GVector p = new GVector(r);
    GVector xnew = new GVector(p);
    GVector rnew = new GVector(p);
    GVector pnew = new GVector(p);
    GVector matrixMultp = new GVector(p);
    GMatrix matrixInverse = new GMatrix(matrix);
    matrixInverse.invert();
    double error, norm;
    int iteration = 0;
    do {
        matrixMultp.mul(matrix, p);
        double lambda = (r.dot(p) / p.dot(matrixMultp));
        xnew.scaleAdd(lambda, p, x);
        rnew.scaleAdd(-lambda, matrixMultp, r);
        double alpha = -(rnew.dot(matrixMultp) / p.dot(matrixMultp));
        pnew.scaleAdd(alpha, p, rnew);
        p.set(pnew);
        r.set(rnew);
        // System.out.println("the residual = "+r.toString());
        x.set(xnew);
        // error = Math.abs(r.dot(r)); // wrong way to compute norm
        rnew.mul(r, matrixInverse);
        norm = rnew.dot(r);
        error = norm * norm;
        // System.out.println("xi = "+x.toString());
        iteration++;
    // System.out.println("The error for iteration " + iteration + " is : " + error );
    } while (error > eps && iteration < maxIterations);
    if (error > eps || Double.isNaN(error) || Double.isInfinite(error)) {
        // something went wrong
        throw new IllegalStateException("Unable to converge on a solution. Error = " + error);
    // return initialGuess;
    }
    return xnew;
}
Also used : GVector(javax.vecmath.GVector) GMatrix(javax.vecmath.GMatrix)

Example 2 with GVector

use of javax.vecmath.GVector in project bb4-common by bb4.

the class ConjugateGradientSolver method solve.

public GVector solve() {
    double[] zeros = new double[b.getSize()];
    GVector initialGuess = new GVector(zeros);
    return solve(initialGuess);
}
Also used : GVector(javax.vecmath.GVector)

Example 3 with GVector

use of javax.vecmath.GVector in project bb4-common by bb4.

the class ConjugateGradientSolverTest method solveSimple4by4WithLowMaxIt.

@Test(expected = IllegalStateException.class)
public void solveSimple4by4WithLowMaxIt() {
    GVector b = new GVector(new double[] { 1, 1, 1, 1 });
    solver = new ConjugateGradientSolver(MATRIX_4x4, b);
    solver.setEpsilon(0.00000000001);
    solver.setMaxIterations(3);
    GVector solution = solver.solve();
}
Also used : GVector(javax.vecmath.GVector) Test(org.junit.Test)

Example 4 with GVector

use of javax.vecmath.GVector in project bb4-common by bb4.

the class ConjugateGradientSolverTest method solve3by4System.

/* didn't work. typo?
    @Test
    public void solveSimple3by3System() {

        GMatrix A = new GMatrix(3, 3, new double[] {
                5, 4, -1,
                0, 10, -3,
                0, 0, 1
        });
        GVector b = new GVector(new double[] {0, 11, 3});

        solver = new ConjugateGradientSolver(A, b);

        GVector solution = solver.solve();

        System.out.println("Solution = "+ solution);
        GVector expectedSolution = new GVector(new double[] {-1, 2, 3});

        assertTrue("Unexpected solution:" + solution,
                LinearUtil.appxVectorsEqual(expectedSolution, solution, 0.0000001));
    }*/
// Conjugant gradient can only be applied to square matrices
@Test(expected = MismatchedSizeException.class)
public void solve3by4System() {
    GMatrix A = new GMatrix(3, 4, new double[] { 2, 2, -1, 1, 1, 1, 1, 1, 2, -2, 2, 3 });
    GVector b = new GVector(new double[] { 11, 4, 7 });
    solver = new ConjugateGradientSolver(A, b);
    solver.solve();
}
Also used : GVector(javax.vecmath.GVector) GMatrix(javax.vecmath.GMatrix) Test(org.junit.Test)

Example 5 with GVector

use of javax.vecmath.GVector in project jwt by emweb.

the class WebGLUtils method add.

public static GVector add(GVector v1, GVector v2) {
    if (v1.getSize() != v2.getSize()) {
        assert (v1.getSize() == 3 || v2.getSize() == 3);
        assert (v1.getSize() == 4 || v2.getSize() == 4);
        v1 = vec4ToVec3(v1);
        v2 = vec4ToVec3(v2);
    }
    GVector result = new GVector(v1.getSize());
    result.add(v1, v2);
    return result;
}
Also used : GVector(javax.vecmath.GVector)

Aggregations

GVector (javax.vecmath.GVector)13 GMatrix (javax.vecmath.GMatrix)4 Test (org.junit.Test)4