use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class Parser method getPart.
/**
* Get a <i>part [[..]]</i> of an expression <code>{a,b,c}[[2]]</code> → <code>b</code>
*
*/
private ASTNode getPart() throws SyntaxError {
ASTNode temp = getFactor();
if (fToken != TT_PARTOPEN) {
return temp;
}
FunctionNode function = null;
do {
if (function == null) {
function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), temp);
} else {
function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), function);
}
fRecursionDepth++;
try {
do {
getNextToken();
if (fToken == TT_ARGUMENTS_CLOSE) {
if (fInputString.length() > fCurrentPosition && fInputString.charAt(fCurrentPosition) == ']') {
throwSyntaxError("Statement (i.e. index) expected in [[ ]].");
}
}
function.add(parseExpression());
} while (fToken == TT_COMMA);
if (fToken == TT_ARGUMENTS_CLOSE) {
// scanner-step begin: (instead of getNextToken() call):
if (fInputString.length() > fCurrentPosition) {
if (fInputString.charAt(fCurrentPosition) == ']') {
fCurrentPosition++;
fToken = TT_PARTCLOSE;
}
}
// scanner-step end
}
if (fToken != TT_PARTCLOSE) {
throwSyntaxError("']]' expected.");
}
} finally {
fRecursionDepth--;
}
getNextToken();
} while (fToken == TT_PARTOPEN);
return parseArguments(function);
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class DoubleEvaluator method optimizeFunction.
/**
* Optimize an already parsed in <code>functionNode</code> into an
* <code>ASTNode</code>.
*
* @param functionNode
* @return
*
*/
public ASTNode optimizeFunction(final FunctionNode functionNode) {
if (functionNode.size() > 0) {
boolean doubleOnly = true;
ASTNode node;
for (int i = 1; i < functionNode.size(); i++) {
node = functionNode.getNode(i);
if (node instanceof NumberNode) {
functionNode.set(i, new DoubleNode(((NumberNode) functionNode.getNode(i)).doubleValue()));
} else if (functionNode.getNode(i) instanceof FunctionNode) {
ASTNode optNode = optimizeFunction((FunctionNode) functionNode.getNode(i));
if (!(optNode instanceof DoubleNode)) {
doubleOnly = false;
}
functionNode.set(i, optNode);
} else if (node instanceof SymbolNode) {
Double dbl = SYMBOL_DOUBLE_MAP.get(node.toString());
if (dbl != null) {
functionNode.set(i, new DoubleNode(dbl.doubleValue()));
} else {
doubleOnly = false;
}
} else {
doubleOnly = false;
}
}
if (doubleOnly) {
try {
return new DoubleNode(evaluateFunction(functionNode));
} catch (Exception e) {
}
}
}
return functionNode;
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class Get method loadPackage.
/**
* Load a package from the given reader
*
* @param engine
* @param is
* @return the last evaluated expression result
*/
public static IExpr loadPackage(final EvalEngine engine, final Reader is) {
final BufferedReader r = new BufferedReader(is);
Context packageContext = null;
try {
final List<ASTNode> node = parseReader(r, engine);
IExpr temp;
int i = 0;
AST2Expr ast2Expr = AST2Expr.CONST;
if (engine.isRelaxedSyntax()) {
ast2Expr = AST2Expr.CONST_LC;
}
IExpr result = F.Null;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++), engine);
if (temp.isAST()) {
IAST ast = (IAST) temp;
IExpr head = temp.head();
if (head.equals(F.BeginPackage) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
packageContext = new Context(contextName);
ISymbol endSymbol = F.EndPackage;
for (int j = 2; j < ast.size(); j++) {
FileReader reader = new FileReader(ast.get(j).toString());
Get.loadPackage(engine, reader);
reader.close();
}
i = addContextToPath(new ContextPath(packageContext), node, i, engine, endSymbol);
continue;
} else if (head.equals(F.Begin) && ast.size() >= 2) {
String contextName = Validate.checkContextName(ast, 1);
ISymbol endSymbol = F.End;
i = addContextToPath(new ContextPath(contextName), node, i, engine, endSymbol);
continue;
}
}
result = engine.evaluate(temp);
}
return result;
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (packageContext != null) {
engine.getContextPath().add(packageContext);
}
try {
r.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return F.Null;
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class MathUtils method getFunctionVal.
public static double getFunctionVal(String f, String v, String x) {
// StringBuilder command = new StringBuilder();
// command.append("ReplaceAll(");
// command.append(f);
// command.append(",");
// command.append(v);
// command.append("-> (");
// command.append(x);
// command.append(")");
// command.append(")");
// String result = evaluate(command.toString(), "N");
//
// EvalDouble dEval = new EvalDouble(true);
// return dEval.evaluate(result);
// Variable var = new Variable(v);
// Expression fun,val;
// 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);
EvalDouble parser = new EvalDouble(true);
String var = v;
ASTNode fun, val;
parser.defineVariable(var);
try {
fun = parser.parse(f);
} catch (MathException e) {
// + e.getMessage(), e.context);
throw e;
}
try {
val = parser.parse(x);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
// var.setVal(val.getVal());
parser.defineVariable(var, parser.evaluateNode(val));
return parser.evaluateNode(fun);
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class MathUtils method getFunctionVal.
public static String getFunctionVal(String fun, String[] var, String resp, String[] vals) throws MathException {
// return evaluate(command.toString(), null);
try {
EvalDouble parParser = new EvalDouble(true);
double[] values = new double[vals.length];
for (int i = 0; i < vals.length; i++) {
values[i] = parParser.evaluate(vals[i]);
}
String respVar = null;
for (int i = 0; i < var.length; i++) {
if (var[i].equals(resp)) {
respVar = resp;
// parParser.add(respVar);
// respVar.setVal(values[i]);
parParser.defineVariable(respVar, values[i]);
} else {
String temp = var[i];
parParser.defineVariable(temp, values[i]);
}
}
if (respVar != null) {
try {
ASTNode f = parParser.parse(fun);
return parParser.evaluateNode(f) + "";
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
}
} catch (MathException e) {
// ParserContext(resp, 0, null));
throw e;
}
throw new MathException("MathUtils:getFunctionVal - cannot compute function values");
}
Aggregations