use of org.matheclipse.core.form.tex.TeXFormFactory in project symja_android_library by axkr.
the class SymjaMMAEvaluator method printForm.
/**
* Print the result in the default output form
*
* @param symjaMMACodeRunner
* @param result
* @return
*/
TryResult printForm(final SymjaMMACodeRunner symjaMMACodeRunner, final Object result) {
switch(fUsedForm) {
case SymjaMMAEvaluator.JAVAFORM:
return TryResult.createResult(((IExpr) result).internalJavaString(JAVA_FORM_PROPERTIES, -1, x -> null));
case SymjaMMAEvaluator.TRADITIONALFORM:
StringBuilder traditionalBuffer = new StringBuilder();
fOutputTraditionalFactory.reset(false);
if (fOutputTraditionalFactory.convert(traditionalBuffer, (IExpr) result)) {
return TryResult.createResult(traditionalBuffer.toString());
} else {
return TryResult.createResult("ERROR-IN-TRADITIONALFORM");
}
// return "";
case SymjaMMAEvaluator.INPUTFORM:
StringBuilder inputBuffer = new StringBuilder();
fInputFactory.reset(false);
if (fInputFactory.convert(inputBuffer, (IExpr) result)) {
return TryResult.createResult(inputBuffer.toString());
} else {
return TryResult.createResult("ERROR-IN-INPUTFORM");
}
case SymjaMMAEvaluator.TEXFORM:
final TeXFormFactory fTeXFactory = new TeXFormFactory();
final StringBuilder texBuilder = new StringBuilder();
texBuilder.append("$$");
if (fTeXFactory.convert(texBuilder, (IExpr) result, 0)) {
texBuilder.append("$$");
return TryResult.createResult(new LatexNotebookOutput(texBuilder.toString()));
} else {
return TryResult.createResult("ERROR-IN-TEXFORM");
}
}
return TryResult.createResult(result);
}
use of org.matheclipse.core.form.tex.TeXFormFactory in project symja_android_library by axkr.
the class TeXTests method testSmartFuzz.
public void testSmartFuzz() {
boolean quietMode = true;
EvalEngine engine = EvalEngine.get();
List<ASTNode> node = parseFileToList();
IExpr temp;
TeXFormFactory fTeXFactory = new TeXFormFactory();
OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
fInputFactory.setInputForm(true);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
IAST seedList = //
F.List(//
F.complex(-0.5, 0.5), //
F.complex(0.0, 0.5), //
F.complex(0.0, -1.0), //
F.complex(0.0, 1.0), //
F.num(-0.5), //
F.num(0.5), //
F.num(Math.PI * (-0.5)), //
F.num(Math.PI * 0.5), //
F.num(-Math.PI), //
F.num(Math.PI), //
F.num(-Math.E), //
F.num(Math.E), //
S.True, //
S.False, //
F.assoc(F.CEmptyList), //
F.assoc(F.List(F.Rule(F.x, F.y))), //
F.CEmptyList, //
F.List(F.Rule(F.C1, F.C0)), //
F.List(F.Rule(F.x, F.CN1)), //
F.C0, //
F.C1, //
F.CN1, //
F.C2, //
F.CN2, //
F.CN10, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.ComplexInfinity, //
F.x_, //
F.y_, //
F.C1DSqrt5, //
F.Slot1, //
F.stringx(""), //
F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
int counter = 0;
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 1; j < 10000; j++) {
int i = 0;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++));
if (temp.isAST() && temp.size() > 1) {
final StringBuilder buf = new StringBuilder();
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
IASTMutable mutant = ((IAST) temp).copy();
int randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
for (int k = 0; k < 1; k++) {
seedIndex = random.nextInt(1, seedList.size());
seed = seedList.get(seedIndex);
randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
}
engine.init();
engine.setQuietMode(quietMode);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
final String mutantStr = fInputFactory.toString(mutant);
try {
// System.out.print(".");
if (counter++ > 80) {
// System.out.println("");
counter = 0;
System.out.flush();
System.err.flush();
}
// eval.eval(mutantStr);
fTeXFactory.convert(buf, mutant, 0);
System.out.println(buf.toString());
} catch (FlowControlException mex) {
if (!quietMode) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
}
} catch (SyntaxError se) {
if (!quietMode) {
System.err.println(mutantStr);
se.printStackTrace();
System.err.println();
}
// fail();
} catch (MathException mex) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
fail();
} catch (RuntimeException rex) {
System.err.println(mutantStr);
rex.printStackTrace();
fail();
} catch (Error rex) {
System.err.println(mutantStr);
if (rex instanceof StackOverflowError) {
System.err.println("java.lang.StackOverflowError");
rex.printStackTrace();
} else {
rex.printStackTrace();
fail();
}
}
}
}
}
// return result;
}
use of org.matheclipse.core.form.tex.TeXFormFactory in project symja_android_library by axkr.
the class TeXUtilities method toTeX.
/**
* Converts the objectExpression into a TeX expression and writes the result to the given <code>
* Writer</code>
*
* @param objectExpression
* @param out
*/
public synchronized boolean toTeX(final IExpr objectExpression, final Writer out) {
final StringBuilder buf = new StringBuilder();
if (objectExpression != null) {
int exponentFigures = fEvalEngine.getSignificantFigures() - 1;
int significantFigures = fEvalEngine.getSignificantFigures() + 1;
try {
IExpr result = objectExpression;
if (objectExpression.isAST()) {
result = fEvalEngine.evalHoldPattern((IAST) objectExpression, true, true);
}
TeXFormFactory teXFactory = new TeXFormFactory(exponentFigures, significantFigures);
if (teXFactory.convert(buf, result, 0)) {
out.write(buf.toString());
return true;
} else {
out.write("ERROR-IN-TEXFORM");
}
} catch (final IOException ioe) {
// parsedExpression == null ==> fError occured
} catch (final RuntimeException rex) {
LOGGER.debug("TeXUtilities.toTeX() failed", rex);
}
return false;
}
return true;
}
Aggregations