Search in sources :

Example 16 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class LUDecompositionTest method testDecomposition.

public void testDecomposition(int nrows, int ncols) {
    MatrixNd M1 = new MatrixNd(nrows, ncols);
    M1.setRandom();
    Exception eActual = null;
    Exception eExpected = null;
    if (nrows != ncols) {
        eExpected = new ImproperSizeException("Matrix not square");
    }
    try {
        lu.factor(M1);
    } catch (Exception e) {
        eActual = e;
    }
    MatrixTest.checkExceptions(eActual, eExpected);
    if (eActual == null) {
        int n = nrows;
        MatrixNd L = new MatrixNd(n, n);
        MatrixNd U = new MatrixNd(n, n);
        MatrixNd PM = new MatrixNd(n, n);
        MatrixNd LU = new MatrixNd(n, n);
        int[] perm = new int[n];
        double[] row = new double[n];
        lu.get(L, U, perm);
        for (int i = 0; i < n; i++) {
            M1.getRow(perm[i], row);
            PM.setRow(i, row);
        }
        LU.mul(L, U);
        if (!LU.epsilonEquals(PM, EPSILON)) {
            throw new TestException("LU=\n" + LU.toString("%9.4f") + "expected:\n" + PM.toString("%9.4f"));
        }
        double condEst = lu.conditionEstimate(M1);
        // check vector solver
        VectorNd b = new VectorNd(n);
        for (int i = 0; i < n; i++) {
            b.set(i, RandomGenerator.get().nextDouble() - 0.5);
        }
        VectorNd x = new VectorNd(n);
        VectorNd Mx = new VectorNd(n);
        lu.solve(x, b);
        Mx.mul(M1, x);
        if (!Mx.epsilonEquals(b, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "Mx=" + Mx.toString("%9.4f") + "b=" + b.toString("%9.4f"));
        }
        // check matrix solver
        MatrixNd B = new MatrixNd(n, 3);
        B.setRandom();
        MatrixNd X = new MatrixNd(n, 3);
        MatrixNd MX = new MatrixNd(n, 3);
        lu.solve(X, B);
        MX.mul(M1, X);
        if (!MX.epsilonEquals(B, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "MX=" + MX.toString("%9.4f") + "B=" + B.toString("%9.4f"));
        }
        // check determinant
        if (n <= 3) {
            double det;
            if (n == 1) {
                det = M1.get(0, 0);
            } else if (n == 2) {
                det = M1.get(0, 0) * M1.get(1, 1) - M1.get(0, 1) * M1.get(1, 0);
            } else // n == 3
            {
                det = M1.get(0, 0) * M1.get(1, 1) * M1.get(2, 2) + M1.get(0, 1) * M1.get(1, 2) * M1.get(2, 0) + M1.get(0, 2) * M1.get(1, 0) * M1.get(2, 1) - M1.get(0, 2) * M1.get(1, 1) * M1.get(2, 0) - M1.get(0, 0) * M1.get(1, 2) * M1.get(2, 1) - M1.get(0, 1) * M1.get(1, 0) * M1.get(2, 2);
            }
            if (Math.abs(det - lu.determinant()) > Math.abs(det * condEst * EPSILON)) {
                throw new TestException("determinant failed: got " + lu.determinant() + " expected " + det + "\nM=\n" + M1.toString("%9.4f"));
            }
        }
        // check inverse
        MatrixNd MI = new MatrixNd(n, n);
        MatrixNd IMI = new MatrixNd(n, n);
        lu.inverse(MI);
        IMI.mul(M1, MI);
        MatrixNd I = new MatrixNd(n, n);
        I.setIdentity();
        if (!IMI.epsilonEquals(I, EPSILON * condEst)) {
            throw new TestException("failed inverse:\n" + MI.toString("%9.4f") + "M1=\n" + M1.toString("%9.4f"));
        }
    }
}
Also used : TestException(maspack.util.TestException) TestException(maspack.util.TestException)

Example 17 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class NumericListTest method checkInterpolation.

void checkInterpolation(NumericList list, double t, double v0, double v1) {
    VectorNd v = new VectorNd(myVsize);
    VectorNd vcheck = new VectorNd(myVsize);
    vcheck.set(0, v0);
    vcheck.set(1, v1);
    list.interpolate(v, t);
    if (!v.epsilonEquals(vcheck, 1e-9)) {
        throw new TestException("Interpolation at time " + t + "\n" + "Got " + v.toString("%8.3f") + ", expected " + vcheck.toString("%8.3f"));
    }
}
Also used : TestException(maspack.util.TestException) VectorNd(maspack.matrix.VectorNd)

Aggregations

TestException (maspack.util.TestException)17 NumberFormat (maspack.util.NumberFormat)4 Point3d (maspack.matrix.Point3d)3 Random (java.util.Random)2 Vector3d (maspack.matrix.Vector3d)2 ReaderTokenizer (maspack.util.ReaderTokenizer)2 StringReader (java.io.StringReader)1 InsideQuery (maspack.geometry.BVFeatureQuery.InsideQuery)1 Vertex3d (maspack.geometry.Vertex3d)1 MatrixNd (maspack.matrix.MatrixNd)1 SymmetricMatrix3d (maspack.matrix.SymmetricMatrix3d)1 VectorNd (maspack.matrix.VectorNd)1