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());
}
}
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());
}
}
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());
}
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());
}
}
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);
}
Aggregations