Search in sources :

Example 11 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class MathUtils method arcLength.

public static double arcLength(String f, String v, String a, String b) throws MathException {
    // Expression fun;
    // Variable var = new Variable(v);
    // Parser parser = new Parser(Parser.STANDARD_FUNCTIONS |
    // Parser.OPTIONAL_PARENS
    // | Parser.OPTIONAL_STARS | Parser.OPTIONAL_SPACES
    // | Parser.BRACES | Parser.BRACKETS| Parser.BOOLEANS);
    // parser.add(var);
    // setUpParser(parser);
    ASTNode fun;
    String var = v;
    EvalDouble parser = new EvalDouble(true);
    parser.defineVariable(var);
    try {
        fun = parser.parse(f);
    } catch (MathException e) {
        throw e;
    }
    String integrand = "sqrt(1+(" + parser.derivative(fun, var) + ")^2)";
    return integrate(integrand, v, a, b);
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 12 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class Import method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    if (!(ast.arg2() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 2, ast.size() - 1);
    }
    IStringX arg1 = (IStringX) ast.arg1();
    IStringX arg2 = (IStringX) ast.arg2();
    FileReader reader = null;
    try {
        reader = new FileReader(arg1.toString());
        if (arg2.contentEquals("Table")) {
            AST2Expr ast2Expr = AST2Expr.CONST;
            if (engine.isRelaxedSyntax()) {
                ast2Expr = AST2Expr.CONST_LC;
            }
            final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
            CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
            Iterable<CSVRecord> records = csvFormat.parse(reader);
            IAST rowList = F.List();
            for (CSVRecord record : records) {
                IAST columnList = F.List();
                for (String string : record) {
                    final ASTNode node = parser.parse(string);
                    IExpr temp = ast2Expr.convert(node, engine);
                    columnList.append(temp);
                }
                rowList.append(columnList);
            }
            return rowList;
        }
    } catch (IOException ioe) {
        engine.printMessage("Import: file " + arg1.toString() + " not found!");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
    return F.NIL;
}
Also used : IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 13 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class RulePreprocessor method generateFunctionStrings.

/**
	 * Generate Java files (*.java) from Symja rule files (*.m)
	 * 
	 * @param sourceLocation
	 *            source directory for rule (*.m) files
	 * @param targetLocation
	 *            target directory for the generated Java files
	 * @param ignoreTimestamp
	 *            if <code>false</code> only change the target file (*.java), if
	 *            the source file (*.m) has a newer time stamp than the target
	 *            file.
	 */
public static void generateFunctionStrings(final File sourceLocation, File targetLocation, boolean ignoreTimestamp) {
    if (sourceLocation.exists()) {
        // Get the list of the files contained in the package
        final String[] files = sourceLocation.list();
        if (files != null) {
            StringBuffer buffer;
            EvalEngine engine = new EvalEngine(true);
            for (int i = 0; i < files.length; i++) {
                File sourceFile = new File(sourceLocation, files[i]);
                // we are only interested in .m files
                if (files[i].endsWith(".m")) {
                    ASTNode node = parseFileToList(sourceFile);
                    if (node != null) {
                        buffer = new StringBuffer(100000);
                        PrintWriter out;
                        try {
                            String className = files[i].substring(0, files[i].length() - 2);
                            String symbolName = className.substring(0, className.length() - 5);
                            File targetFile = new File(targetLocation, className + ".java");
                            if (targetFile.exists()) {
                                if (!ignoreTimestamp && (sourceFile.lastModified() <= targetFile.lastModified())) {
                                    // existing ones
                                    continue;
                                }
                            }
                            System.out.println(className);
                            out = new PrintWriter(targetFile.getCanonicalPath());
                            out.print(HEADER);
                            out.print(className);
                            out.print(" {\n");
                            convert(node, "", buffer, out, symbolName, engine);
                            out.println(FOOTER1);
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 14 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class ParserTestCase method testParser7.

public void testParser7() {
    try {
        Parser p = new Parser();
        ASTNode obj = p.parse("a+%%%+%3*:=4!");
        fail("A SyntaxError exception should occur here");
    } catch (Exception e) {
        assertEquals("Syntax error in line: 1 - Operator: := is no prefix operator.\n" + "a+%%%+%3*:=4!\n" + "          ^", e.getMessage());
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) Parser(org.matheclipse.parser.client.Parser)

Example 15 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class ParserTestCase method testParser3.

public void testParser3() {
    try {
        Parser p = new Parser();
        ASTNode obj = p.parse("f[y,z](a+b+c)");
        assertEquals(obj.toString(), "Times(f(y, z), Plus(a, b, c))");
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals("", e.getMessage());
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) Parser(org.matheclipse.parser.client.Parser)

Aggregations

ASTNode (org.matheclipse.parser.client.ast.ASTNode)56 Parser (org.matheclipse.parser.client.Parser)32 IExpr (org.matheclipse.core.interfaces.IExpr)10 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)8 MathException (org.matheclipse.parser.client.math.MathException)6 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)5 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)5 IAST (org.matheclipse.core.interfaces.IAST)4 IOException (java.io.IOException)3 AST2Expr (org.matheclipse.core.convert.AST2Expr)3 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 FileReader (java.io.FileReader)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1