use of org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator in project symja_android_library by axkr.
the class EvalEngine method evalAST.
/**
* Evaluate an AST. The evaluation steps are controlled by the header attributes.
*
* @param ast
* @return <code>F.NIL</code> if no evaluation happened
*/
public IExpr evalAST(IAST ast) {
final IExpr head = ast.head();
if (ast.head().isCoreFunctionSymbol()) {
// evaluate a core function (without no rule definitions)
final ICoreFunctionEvaluator coreFunction = (ICoreFunctionEvaluator) ((IBuiltInSymbol) head).getEvaluator();
return fNumericMode ? coreFunction.numericEval(ast, this) : coreFunction.evaluate(ast, this);
}
final ISymbol symbol = ast.topHead();
IExpr result = evalAttributes(symbol, ast);
if (result.isPresent()) {
return result;
}
// System.out.println(ast.toString());
return evalRules(symbol, ast);
}
use of org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator in project symja_android_library by axkr.
the class AbstractAST method evaluate.
/**
* {@inheritDoc}
*/
@Override
public IExpr evaluate(EvalEngine engine) {
LOGGER.debug("Evaluate {}", this);
final IExpr head = head();
final int argSize = argSize();
if (head instanceof ISymbol) {
ISymbol headSymbol = (ISymbol) head;
Class<?> clazz = headSymbol.getContext().getJavaClass();
if (clazz != null) {
String staticMethodName = headSymbol.getSymbolName();
// try {
// Method method = clazz.getMethod(staticMethodName);
// if (Modifier.isStatic(method.getModifiers())) {
// Parameter[] parameters = method.getParameters();
// if (parameters.length == argSize()) {
// Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
// if (params != null) {
// Object result;
// try {
// result = method.invoke(null, params);
// if (result instanceof String) {
// return F.stringx((String) result);
// }
// return Object2Expr.convert(result);
// } catch (IllegalAccessException
// | IllegalArgumentException
// | InvocationTargetException e) {
// // fall through?
// }
// }
// }
// }
//
// } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) {
// // fall through?
// }
Method[] methods = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
if (staticMethodName.equals(methods[i].getName())) {
Parameter[] parameters = methods[i].getParameters();
if (parameters.length == argSize()) {
Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
if (params != null) {
Object result;
try {
result = methods[i].invoke(null, params);
if (result instanceof String) {
return F.stringx((String) result);
}
return Object2Expr.convert(result, false, true);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// fall through?
}
}
}
}
}
}
}
if (head instanceof IBuiltInSymbol) {
final IEvaluator evaluator = ((IBuiltInSymbol) head).getEvaluator();
if (evaluator instanceof ICoreFunctionEvaluator) {
try {
ICoreFunctionEvaluator functionEvaluator = (ICoreFunctionEvaluator) evaluator;
EvalEngine.OptionsResult opres = engine.checkBuiltinArguments(this, functionEvaluator);
if (opres == null) {
return F.NIL;
}
IAST ast = opres.result;
IBuiltInSymbol header = ((IBuiltInSymbol) head);
if ((header.getAttributes() & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
IExpr temp;
if ((temp = F.flattenSequence(this)).isPresent()) {
return temp;
}
}
if (isBooleanFunction()) {
IExpr temp = extractConditionalExpression(false);
if (temp.isPresent()) {
return temp;
}
}
IExpr evaluateTemp = evalEvaluate(engine);
if (evaluateTemp.isPresent()) {
return evaluateTemp;
}
return functionEvaluator.evaluate(ast, engine);
} catch (ValidateException ve) {
return IOFunctions.printMessage(topHead(), ve, engine);
} catch (FlowControlException e) {
throw e;
} catch (SymjaMathException ve) {
LOGGER.log(engine.getLogLevel(), topHead(), ve);
return F.NIL;
}
}
}
}
if (head.isAssociation() && argSize == 1) {
return ((IAssociation) head).getValue(arg1());
}
final ISymbol symbol = topHead();
IExpr temp = engine.evalAttributes(symbol, this);
if (temp.isPresent()) {
return temp;
}
return engine.evalRules(symbol, this);
}
use of org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator in project symja_android_library by axkr.
the class BuiltInSymbol method of.
/**
* {@inheritDoc}
*/
@Override
public IExpr of(EvalEngine engine, IExpr... args) {
if (fEvaluator instanceof ICoreFunctionEvaluator) {
// evaluate a core function (without no rule definitions)
final ICoreFunctionEvaluator coreFunction = (ICoreFunctionEvaluator) getEvaluator();
IAST ast = F.ast(args, this);
return coreFunction.evaluate(ast, engine);
}
return super.of(engine, args);
}
Aggregations