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