Search in sources :

Example 11 with TestException

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

the class FemModel3d method testSimpleSurfaceMesh.

private void testSimpleSurfaceMesh() {
    FemMeshComp sfm = getSurfaceMeshComp();
    if (sfm != null) {
        for (Vertex3d vtx : sfm.getMesh().getVertices()) {
            FemNode3d node = getSurfaceNode(vtx);
            if (node == null) {
                throw new TestException("no node found for vertex " + vtx.getIndex());
            }
            Vertex3d chk = getSurfaceVertex(node);
            if (chk != vtx) {
                throw new TestException("no vertex found for node " + node.getNumber());
            }
        }
        System.out.println("SURFACE OK");
    }
}
Also used : Vertex3d(maspack.geometry.Vertex3d) TestException(maspack.util.TestException)

Example 12 with TestException

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

the class SVDecomposition3dTest method testDecomposition.

public void testDecomposition(Matrix3d M1, double[] svals) {
    svd.factor(M1);
    int nrows = 3;
    int ncols = 3;
    int mind = 3;
    Matrix3d U = svd.getU();
    Matrix3d V = svd.getV();
    Vector3d sig = new Vector3d();
    svd.getS(sig);
    if (!isOrthogonal(U)) {
        throw new TestException("U not orthogonal:\n" + U.toString("%9.4f"));
    }
    if (!isOrthogonal(V)) {
        throw new TestException("V not orthogonal:\n" + V.toString("%9.4f"));
    }
    // verify product
    double cond = svd.condition();
    Matrix3d US = new Matrix3d();
    US.set(U);
    US.mulCols(sig);
    Matrix3d MP = new Matrix3d();
    MP.mulTransposeRight(US, V);
    if (!MP.epsilonEquals(M1, EPSILON * svd.norm())) {
        Matrix3d ER = new Matrix3d();
        ER.sub(MP, M1);
        throw new TestException("U S V' = \n" + MP.toString("%9.4f") + "expecting:\n" + M1.toString("%9.4f") + "error=" + ER.infinityNorm());
    }
    if (svals != null) {
        boolean[] taken = new boolean[mind];
        for (int i = 0; i < mind; i++) {
            int j;
            for (j = 0; j < mind; j++) {
                if (!taken[j] && Math.abs(sig.get(j) - svals[i]) > svals[i] * EPSILON) {
                    taken[j] = true;
                    break;
                }
            }
            if (j == mind) {
                throw new TestException("singular values " + svals[i] + " not computed");
            }
        }
    }
    // check vector solver
    if (nrows == ncols) {
        Vector3d b = new Vector3d();
        for (int i = 0; i < nrows; i++) {
            b.set(i, RandomGenerator.get().nextDouble() - 0.5);
        }
        Vector3d x = new Vector3d();
        Vector3d Mx = new Vector3d();
        svd.solve(x, b);
        Mx.mul(M1, x);
        if (!Mx.epsilonEquals(b, EPSILON * cond)) {
            throw new TestException("solution failed:\n" + "Mx=" + Mx.toString("%9.4f") + "b=" + b.toString("%9.4f") + "x=" + x.toString("%9.4f"));
        }
    }
    // // check matrix solver
    // if (nrows == ncols)
    // {
    // MatrixNd B = new MatrixNd(nrows,3);
    // B.setRandom();
    // MatrixNd X = new MatrixNd(ncols,3);
    // MatrixNd MX = new MatrixNd(nrows,3);
    // svd.solve (X, B);
    // MX.mul (M1, X);
    // if (!MX.epsilonEquals (B, EPSILON*cond))
    // { throw new TestException (
    // "solution failed:\n" +
    // "MX=" + MX.toString("%9.4f") +
    // "B=" + B.toString("%9.4f"));
    // }
    // }
    // check determinant
    {
        double 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);
        double sigsum = 0;
        for (int i = 0; i < nrows; i++) {
            sigsum += Math.abs(sig.get(i));
        }
        if (Math.abs(det - svd.determinant()) > Math.abs(sigsum * EPSILON)) {
            throw new TestException("determinant failed: got " + svd.determinant() + " expected " + det + "\nM=\n" + M1.toString("%9.4f"));
        }
    }
    // check inverse
    if (nrows == ncols) {
        int n = nrows;
        Matrix3d MI = new Matrix3d();
        Matrix3d IMI = new Matrix3d();
        svd.inverse(MI);
        IMI.mul(M1, MI);
        Matrix3d I = new Matrix3d();
        I.setIdentity();
        if (!IMI.epsilonEquals(I, EPSILON * cond)) {
            throw new TestException("failed inverse:\n" + MI.toString("%9.4f") + "M1=\n" + M1.toString("%9.4f"));
        }
    }
}
Also used : TestException(maspack.util.TestException)

Example 13 with TestException

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

the class QRDecompositionTest method testDecomposition.

public void testDecomposition(MatrixNd M1, boolean usePivoting) {
    int nrows = M1.rowSize();
    int ncols = M1.colSize();
    Exception eActual = null;
    if (usePivoting) {
        qr.factorWithPivoting(M1);
    } else {
        qr.factor(M1);
    }
    int n = ncols;
    int m = nrows;
    MatrixNd QR = new MatrixNd(0, 0);
    MatrixNd MP = new MatrixNd(0, 0);
    MatrixNd QTQ = new MatrixNd(0, 0);
    int[] cperm = new int[n];
    // check the different possible values of Q and R
    int mind = Math.min(nrows, ncols);
    for (int p = mind - 1; p <= m + 1; p++) {
        MatrixNd R = new MatrixNd(p, n);
        MatrixNd Q = new MatrixNd(m, p);
        qr.get(Q, R, cperm);
        QR.mul(Q, R);
        MP.set(M1);
        MP.permuteColumns(cperm);
        if (!QR.epsilonEquals(MP, EPSILON)) {
            throw new TestException("QR=\n" + QR.toString("%9.4f") + "expected:\n" + MP.toString("%9.4f"));
        }
        QTQ.mulTransposeLeft(Q, Q);
        // Q = Q - I
        for (int i = 0; i < QTQ.rowSize(); i++) {
            QTQ.set(i, i, QTQ.get(i, i) - 1);
        }
        if (QTQ.frobeniusNorm() > EPSILON) {
            throw new TestException("Q not orthogonal:\n" + Q.toString("%12.9f"));
        }
        if (p == mind) {
            testPreMulQ(qr, M1);
            testPreMulQTranspose(qr, M1);
            testPostMulQ(qr, M1);
            testPostMulQTranspose(qr, M1);
        }
    }
    if (nrows < ncols) {
        return;
    }
    double condEst = qr.conditionEstimate();
    // used for computing desired minimum norm solution
    MatrixNd MTM = new MatrixNd(n, n);
    MTM.mulTransposeLeft(M1, M1);
    MTM.invert(MTM);
    // check vector solver
    VectorNd b = new VectorNd(m);
    for (int i = 0; i < m; i++) {
        b.set(i, RandomGenerator.get().nextDouble() - 0.5);
    }
    VectorNd x = new VectorNd(n);
    VectorNd Mx = new VectorNd(m);
    eActual = null;
    try {
        qr.solve(x, b);
    } catch (Exception e) {
        eActual = e;
    }
    if (m < n) {
        MatrixTest.checkExceptions(eActual, new ImproperSizeException("M has fewer rows than columns"));
    } else {
        MatrixTest.checkExceptions(eActual, null);
        if (m == n) {
            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"));
            }
        } else {
            VectorNd xcheck = new VectorNd(n);
            M1.mulTranspose(xcheck, b);
            MTM.mul(xcheck, xcheck);
            if (!xcheck.epsilonEquals(x, EPSILON * condEst)) {
                System.out.println("n=" + n);
                System.out.println("condEst=" + condEst);
                System.out.println("tol=" + EPSILON * condEst);
                System.out.println("M1=\n" + M1.toString("%10.4f"));
                throw new TestException("solution failed:\n" + "x=" + x.toString("%g") + "\nexpected=" + xcheck.toString("%g"));
            }
        }
    }
    // check matrix solver
    MatrixNd B = new MatrixNd(m, 3);
    B.setRandom();
    MatrixNd X = new MatrixNd(n, 3);
    MatrixNd MX = new MatrixNd(n, 3);
    eActual = null;
    try {
        qr.solve(X, B);
    } catch (Exception e) {
        eActual = e;
    }
    if (m < n) {
        MatrixTest.checkExceptions(eActual, new ImproperSizeException("M has fewer rows than columns"));
    } else {
        MatrixTest.checkExceptions(eActual, null);
        qr.solve(X, B);
        MX.mul(M1, X);
        if (m == n) {
            if (!MX.epsilonEquals(B, EPSILON * condEst)) {
                throw new TestException("solution failed:\n" + "MX=" + MX.toString("%9.4f") + "B=" + B.toString("%9.4f"));
            }
        } else {
            MatrixNd Xcheck = new MatrixNd(n, 3);
            Xcheck.mulTransposeLeft(M1, B);
            Xcheck.mul(MTM, Xcheck);
            if (!Xcheck.epsilonEquals(X, EPSILON * condEst)) {
                throw new TestException("solution failed:\n" + "X=" + X.toString("%9.4f") + "expected=" + Xcheck.toString("%9.4f"));
            }
        }
    }
    // check R solve
    b.setSize(n);
    B.setSize(n, 3);
    MatrixNd R = new MatrixNd(n, n);
    VectorNd Rx = new VectorNd(n);
    qr.get(null, R);
    qr.solveR(x, b);
    x.permute(cperm);
    Rx.mul(R, x);
    if (!Rx.epsilonEquals(b, EPSILON * condEst)) {
        throw new TestException("solveR failed:\n" + "Rx=" + Rx.toString("%9.4f") + "\n" + "b=" + b.toString("%9.4f"));
    }
    // check R matrix solve
    MatrixNd RX = new MatrixNd(n, 3);
    qr.solveR(X, B);
    X.permuteRows(cperm);
    RX.mul(R, X);
    if (!RX.epsilonEquals(B, EPSILON * condEst)) {
        throw new TestException("solveR failed:\n" + "RX=\n" + RX.toString("%9.4f") + "B=\n" + B.toString("%9.4f"));
    }
    // if (!usePivoting)
    {
        // check left R solve
        VectorNd xR = new VectorNd(n);
        VectorNd bperm = new VectorNd(n);
        bperm.set(b);
        bperm.permute(cperm);
        qr.get(null, R);
        qr.leftSolveR(x, b);
        xR.mulTranspose(R, x);
        if (!xR.epsilonEquals(bperm, EPSILON * condEst)) {
            throw new TestException("leftSolveR failed:\n" + "xR=" + xR.toString("%9.4f") + "\n" + "bperm=" + bperm.toString("%9.4f"));
        }
        // check left R matrix solve
        MatrixNd XR = new MatrixNd(3, n);
        MatrixNd BT = new MatrixNd(3, n);
        MatrixNd BP = new MatrixNd(3, n);
        BT.transpose(B);
        BP.set(BT);
        BP.permuteColumns(cperm);
        qr.leftSolveR(X, BT);
        XR.mul(X, R);
        if (!XR.epsilonEquals(BP, EPSILON * condEst)) {
            throw new TestException("leftSolveR failed:\n" + "XR=\n" + XR.toString("%9.4f") + "BP=\n" + BP.toString("%9.4f"));
        }
    }
    // check determinant
    if (m == n && 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 - qr.determinant()) > Math.abs(det * condEst * EPSILON)) {
            throw new TestException("determinant failed: got " + qr.determinant() + " expected " + det + "\nM=\n" + M1.toString("%9.4f"));
        }
    }
    // check inverse
    MatrixNd MI = new MatrixNd(n, n);
    MatrixNd IMI = new MatrixNd(n, n);
    eActual = null;
    try {
        qr.inverse(MI);
    } catch (Exception e) {
        eActual = e;
    }
    if (m != n) {
        MatrixTest.checkExceptions(eActual, new ImproperSizeException("Original matrix not square"));
    } else {
        MatrixTest.checkExceptions(eActual, null);
        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 14 with TestException

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

the class VectoriTest method testSetAndGet.

void testSetAndGet(Vectori vr) {
    Random randGen = RandomGenerator.get();
    int size = vr.size();
    int[] setBuf = new int[size];
    int[] getBuf = new int[size];
    for (int i = 0; i < size; i++) {
        int value = randGen.nextInt();
        vr.set(i, value);
        if (vr.get(i) != value) {
            throw new TestException(elementFailMessage("get/set", i));
        }
        setBuf[i] = value;
    }
    vr.get(getBuf);
    for (int i = 0; i < size; i++) {
        if (getBuf[i] != setBuf[i]) {
            throw new TestException(elementFailMessage("set", i));
        }
    }
    vx.set(vr);
    vr.set(vx);
    vr.get(getBuf);
    for (int i = 0; i < size; i++) {
        if (getBuf[i] != setBuf[i]) {
            throw new TestException("set(Vectori) failed for i=" + i + ": get=" + getBuf[i] + ", set=" + setBuf[i]);
        }
    }
}
Also used : Random(java.util.Random) TestException(maspack.util.TestException)

Example 15 with TestException

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

the class WavefrontReaderTest method test.

void test(String str, WavefrontReader check) throws IOException {
    WavefrontReader wfr = new WavefrontReader(new StringReader(str));
    wfr.parse();
    if (!wfr.equals(check)) {
        System.out.println(str);
        System.out.println("Expected:\n" + check.toString());
        System.out.println("Got:\n" + wfr.toString());
        throw new TestException("check not equal to result");
    }
}
Also used : TestException(maspack.util.TestException) StringReader(java.io.StringReader)

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