Search in sources :

Example 1 with TeXFormFactory

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);
}
Also used : MagicCommandAutocompletePatterns(com.twosigma.beakerx.autocomplete.MagicCommandAutocompletePatterns) LatexNotebookOutput(com.twosigma.beakerx.symjamma.output.LatexNotebookOutput) SymjaMMAAutocomplete(com.twosigma.beakerx.symjamma.autocomplete.SymjaMMAAutocomplete) IOInit(org.matheclipse.io.IOInit) ImportPath(com.twosigma.beakerx.kernel.ImportPath) MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer) TryResult(com.twosigma.beakerx.TryResult) Prefix(org.matheclipse.core.interfaces.IExpr.SourceCodeProperties.Prefix) BeakerCellExecutor(com.twosigma.beakerx.jvm.threads.BeakerCellExecutor) MarkdownNotebookOutput(com.twosigma.beakerx.symjamma.output.MarkdownNotebookOutput) ExecutionOptions(com.twosigma.beakerx.kernel.ExecutionOptions) Locale(java.util.Locale) BeakerXClient(com.twosigma.beakerx.BeakerXClient) JobDescriptor(com.twosigma.beakerx.evaluator.JobDescriptor) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) EvalEngine(org.matheclipse.core.eval.EvalEngine) EnvVariablesFilter.envVariablesFilter(com.twosigma.beakerx.symjamma.evaluator.EnvVariablesFilter.envVariablesFilter) F(org.matheclipse.core.expression.F) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) File(java.io.File) Executors(java.util.concurrent.Executors) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) Classpath(com.twosigma.beakerx.kernel.Classpath) SourceCodeProperties(org.matheclipse.core.interfaces.IExpr.SourceCodeProperties) TempFolderFactory(com.twosigma.beakerx.evaluator.TempFolderFactory) BaseEvaluator(com.twosigma.beakerx.evaluator.BaseEvaluator) CellExecutor(com.twosigma.beakerx.jvm.threads.CellExecutor) TempFolderFactoryImpl(com.twosigma.beakerx.evaluator.TempFolderFactoryImpl) IExpr(org.matheclipse.core.interfaces.IExpr) ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) ParserConfig(org.matheclipse.parser.client.ParserConfig) EvaluatorParameters(com.twosigma.beakerx.kernel.EvaluatorParameters) PathToJar(com.twosigma.beakerx.kernel.PathToJar) AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) LatexNotebookOutput(com.twosigma.beakerx.symjamma.output.LatexNotebookOutput)

Example 2 with TeXFormFactory

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;
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) AST2Expr(org.matheclipse.core.convert.AST2Expr) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 3 with TeXFormFactory

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;
}
Also used : TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IOException(java.io.IOException)

Aggregations

TeXFormFactory (org.matheclipse.core.form.tex.TeXFormFactory)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)2 IAST (org.matheclipse.core.interfaces.IAST)2 BeakerXClient (com.twosigma.beakerx.BeakerXClient)1 TryResult (com.twosigma.beakerx.TryResult)1 AutocompleteResult (com.twosigma.beakerx.autocomplete.AutocompleteResult)1 MagicCommandAutocompletePatterns (com.twosigma.beakerx.autocomplete.MagicCommandAutocompletePatterns)1 BaseEvaluator (com.twosigma.beakerx.evaluator.BaseEvaluator)1 JobDescriptor (com.twosigma.beakerx.evaluator.JobDescriptor)1 TempFolderFactory (com.twosigma.beakerx.evaluator.TempFolderFactory)1 TempFolderFactoryImpl (com.twosigma.beakerx.evaluator.TempFolderFactoryImpl)1 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)1 BeakerCellExecutor (com.twosigma.beakerx.jvm.threads.BeakerCellExecutor)1 CellExecutor (com.twosigma.beakerx.jvm.threads.CellExecutor)1 Classpath (com.twosigma.beakerx.kernel.Classpath)1 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)1 ExecutionOptions (com.twosigma.beakerx.kernel.ExecutionOptions)1 ImportPath (com.twosigma.beakerx.kernel.ImportPath)1