Search in sources :

Example 11 with NumberFormat

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

the class VectoriTest method checkResult.

void checkResult(Vectori vr, Vectori vc, Exception eactual, Exception eexpected) {
    MatrixTest.checkExceptions(eactual, eexpected);
    if (!vr.equals(vc)) {
        VectorNi ME = new VectorNi(vr);
        VectorNi v1 = new VectorNi(vc);
        ME.sub(v1);
        ME.absolute();
        throw new TestException("Expected result:\n" + vc.toString(new NumberFormat("%d")) + "\n" + "Actual result:\n" + vr.toString(new NumberFormat("%d")) + "\n" + "max err: " + ME.maxElement());
    }
}
Also used : TestException(maspack.util.TestException) NumberFormat(maspack.util.NumberFormat)

Example 12 with NumberFormat

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

the class VectoriTest method doScanWrite.

void doScanWrite(Vectori vr, String fmt, boolean withBrackets) {
    StringWriter sw = new StringWriter();
    try {
        vr.write(new PrintWriter(sw), new NumberFormat(fmt), withBrackets);
        vr.scan(new ReaderTokenizer(new StringReader(sw.toString())));
    } catch (Exception e) {
        throw new TestException("scan/write error: " + e.getMessage());
    }
}
Also used : TestException(maspack.util.TestException) ReaderTokenizer(maspack.util.ReaderTokenizer) TestException(maspack.util.TestException) NumberFormat(maspack.util.NumberFormat)

Example 13 with NumberFormat

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

the class MatrixBase method write.

/**
 * Writes the contents of this matrix to a PrintWriter.
 *
 * @param pw
 * PrintWriter to write the matrix to
 * @param msg
 * Optional message - if not <code>null</code>, is printed on
 * a separate line preceeding the matrix information.
 * @param wfmt
 * specifies the matrix output format
 */
public void write(PrintWriter pw, String msg, WriteFormat wfmt) throws IOException {
    NumberFormat fmt = new NumberFormat("%g");
    if (msg != null) {
        pw.println(msg);
    }
    write(pw, fmt, wfmt, rowSize(), colSize());
}
Also used : NumberFormat(maspack.util.NumberFormat)

Example 14 with NumberFormat

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

the class LemkeSolverBase method printPredictedQv.

protected void printPredictedQv(String msg, double[] mv, double[] qv, int s, Variable driveVar, int numv) {
    Variable[] basicVars = getBasicVars();
    int maxNameLength = getMaxNameLength(basicVars, numv);
    NumberFormat ffmt = new NumberFormat("%24g");
    StringBuffer sbuf = new StringBuffer(256);
    double ratio = -qv[s] / mv[s];
    if (msg != null) {
        System.out.println(msg);
    }
    for (int i = 0; i < numv; i++) {
        sbuf.setLength(0);
        sbuf.append("  ");
        if (i == driveVar.idx) {
            sbuf.append("z0");
        } else if (i == s) {
            sbuf.append(driveVar.getName());
        } else {
            sbuf.append(basicVars[i].getName());
        }
        while (sbuf.length() < maxNameLength + 2) {
            sbuf.insert(2, ' ');
        }
        sbuf.append("   ");
        if (i == s) {
            sbuf.append(ffmt.format(ratio));
        } else {
            sbuf.append(ffmt.format(qv[i] + ratio * mv[i]));
        }
        System.out.println(sbuf.toString());
    }
}
Also used : NumberFormat(maspack.util.NumberFormat)

Example 15 with NumberFormat

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

the class IncompleteLUDecompositionTest method main.

public static void main(String[] args) {
    SparseMatrixNd A = new SparseMatrixNd(1, 1);
    Random random = new Random(51);
    try {
        A.scan(new ReaderTokenizer(new FileReader("/ubc/ece/home/hct/other/elliote/A.matrix")));
        System.out.println("read matrix " + A.rowSize() + " " + A.colSize());
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
    int n = A.rowSize();
    System.out.println("loaded matrix");
    // int n = 400;
    // A.setSize(n, n);
    // SVDecomposition svd;
    // do
    // {
    // A.setRandom(1, 2, n*2, random);
    // for(int i = 0; i < n; i++)
    // A.set(i, i, random.nextDouble() + 1);
    // 
    // svd = new SVDecomposition(A);
    // }
    // while(svd.determinant() < 1e-10);
    // A.mulTranspose(A);
    // System.out.println("created spd matrix");
    // waitforuser();
    IncompleteLUDecomposition ilud = new IncompleteLUDecomposition();
    ilud.factor(A, 0.00001);
    if (ilud.L.containsNaN() || ilud.U.containsNaN()) {
        System.out.println("LU contains NaN");
    }
    System.out.println("factored matrix");
    // waitforuser();
    SparseMatrixNd tmp = new SparseMatrixNd(n, n);
    tmp.mul(ilud.L, ilud.U);
    // System.out.println(new MatrixNd(tmp));
    tmp.sub(A);
    System.out.println("residual matrix one norm " + tmp.oneNorm());
    VectorNd x = new VectorNd(n);
    VectorNd b = new VectorNd(n);
    VectorNd xc = new VectorNd(n);
    try {
        System.out.println("writing solve matrix and incomplete cholesky decomposition");
        PrintWriter fwr;
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/LUA.matrix");
        // fwr.append("[ ");
        A.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/L.matrix");
        // fwr.append("[ ");
        ilud.L.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/U.matrix");
        // fwr.append("[ ");
        ilud.U.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        System.out.println("finished writing");
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
    // test backsolves
    System.out.println();
    CGSolver isolver = new CGSolver();
    DirectSolver dsolver = new UmfpackSolver();
    // dsolver.factor(A);
    b.setRandom(-1, 1, random);
    System.out.println("solving L * x = b");
    ilud.solveL(x, b);
    dsolver.analyzeAndFactor(ilud.L);
    dsolver.solve(xc, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println("xc " + xc);
    if (!x.epsilonEquals(xc, 1e-6)) {
        System.out.println("backsolve failed");
    }
    System.out.println();
    System.out.println("solving U * x = b");
    ilud.solveU(x, b);
    dsolver.analyzeAndFactor(ilud.U);
    dsolver.solve(xc, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println("xc " + xc);
    if (!x.epsilonEquals(xc, 1e-6)) {
        System.out.println("backsolve failed");
    }
    // test upcg solver
    System.out.println();
    System.out.println("solving A * x = b");
    double tol = 1e-2;
    int maxit = 1500;
    System.out.println("preconditioned solve untransformed");
    x.setZero();
    isolver.solve(x, A, b, tol, maxit, ilud);
    System.out.println("iterations " + isolver.getNumIterations());
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println();
    // System.out.println("preconditioned solve transformed");
    // x.setZero();
    // isolver.solveTransformed(x, A, b, tol, maxit, icd);
    // System.out.println("iterations " + isolver.getNumIterations());
    // System.out.println("b " + b);
    // System.out.println("x " + x);
    // 
    // System.out.println();
    System.out.println("unpreconditioned solve");
    x.setZero();
    isolver.solve(x, A, b, tol, maxit);
    System.out.println("iterations " + isolver.getNumIterations());
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println();
    System.out.println("direct solve");
    x.setZero();
    dsolver.analyzeAndFactor(A);
    dsolver.solve(x, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
}
Also used : IOException(java.io.IOException) Random(java.util.Random) SparseMatrixNd(maspack.matrix.SparseMatrixNd) ReaderTokenizer(maspack.util.ReaderTokenizer) VectorNd(maspack.matrix.VectorNd) FileReader(java.io.FileReader) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Aggregations

NumberFormat (maspack.util.NumberFormat)55 PrintWriter (java.io.PrintWriter)15 IOException (java.io.IOException)8 Point3d (maspack.matrix.Point3d)6 BufferedWriter (java.io.BufferedWriter)5 File (java.io.File)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 Point (artisynth.core.mechmodels.Point)4 FileWriter (java.io.FileWriter)4 ArrayList (java.util.ArrayList)4 SparseBlockMatrix (maspack.matrix.SparseBlockMatrix)4 ReaderTokenizer (maspack.util.ReaderTokenizer)4 TestException (maspack.util.TestException)4 Face (maspack.geometry.Face)3 VectorNd (maspack.matrix.VectorNd)3 FunctionTimer (maspack.util.FunctionTimer)3 InternalErrorException (maspack.util.InternalErrorException)3 ContactPoint (artisynth.core.mechmodels.ContactPoint)2 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)2 Font (java.awt.Font)2