use of org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator in project symja_android_library by axkr.
the class EvalEngine method evalASTBuiltinFunction.
/**
* @param symbol
* @param ast
* @return <code>F.NIL</code> if no evaluation happened
*/
private IExpr evalASTBuiltinFunction(final ISymbol symbol, IAST ast) {
final int attributes = symbol.getAttributes();
if (fEvalLHSMode) {
if ((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL) {
// (i.e. Sin, Cos,...)
if (!(symbol.equals(S.Set) || symbol.equals(S.SetDelayed) || symbol.equals(S.UpSet) || symbol.equals(S.UpSetDelayed))) {
return F.NIL;
}
} else {
if ((ISymbol.NUMERICFUNCTION & attributes) != ISymbol.NUMERICFUNCTION) {
return F.NIL;
}
}
}
if (!symbol.equals(S.Integrate)) {
IExpr result;
if ((result = symbol.evalDownRule(this, ast)).isPresent()) {
return result;
}
}
if (symbol.isBuiltInSymbol()) {
final IEvaluator evaluator = ((IBuiltInSymbol) symbol).getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
if (ast.isEvalFlagOn(IAST.BUILT_IN_EVALED) && isSymbolicMode(attributes)) {
return F.NIL;
}
// evaluate a built-in function.
final IFunctionEvaluator functionEvaluator = (IFunctionEvaluator) evaluator;
OptionsResult opres = checkBuiltinArguments(ast, functionEvaluator);
if (opres == null) {
return F.NIL;
}
ast = opres.result;
try {
if (evaluator instanceof AbstractFunctionOptionEvaluator) {
AbstractFunctionOptionEvaluator optionsEvaluator = (AbstractFunctionOptionEvaluator) evaluator;
IExpr result = optionsEvaluator.evaluate(ast, opres.argSize, opres.options, this);
if (result.isPresent()) {
return result;
}
} else {
IExpr result = fNumericMode ? functionEvaluator.numericEval(ast, this) : functionEvaluator.evaluate(ast, this);
if (result.isPresent()) {
return result;
}
}
} catch (ValidateException ve) {
return IOFunctions.printMessage(ast.topHead(), ve, this);
} catch (FlowControlException e) {
throw e;
} catch (SymjaMathException ve) {
LOGGER.log(getLogLevel(), ast.topHead(), ve);
return F.NIL;
}
// cannot generally set the result as evaluated in built-in function. Especially problems in
// `togetherMode`
// if (isSymbolicMode(attributes) && !isTogetherMode()) {
// ast.addEvalFlags(IAST.BUILT_IN_EVALED);
// }
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator in project symja_android_library by axkr.
the class EvalEngine method checkBuiltinArguments.
/**
* Check the number of arguments if requested and transform the <code>ast</code> from an
* <i>operator form</i> to <i>normal form</i> if it is allowed.
*
* @param ast
* @param functionEvaluator
* @return
*/
public OptionsResult checkBuiltinArguments(IAST ast, final IFunctionEvaluator functionEvaluator) {
int[] expected;
OptionsResult opres = new OptionsResult(ast, ast.argSize(), null);
if ((expected = functionEvaluator.expectedArgSize(ast)) != null) {
if (expected.length == 2 && !ast.head().isBuiltInSymbol()) {
return null;
} else if (expected.length == 3 && expected[2] > 0) {
switch(expected[2]) {
case 1:
if (ast.isAST1()) {
opres.result = F.operatorForm1Append(ast);
if (!opres.result.isPresent()) {
return null;
}
}
break;
case 2:
if (ast.head().isAST1()) {
opres.result = F.operatorForm2Prepend(ast, expected, this);
if (!opres.result.isPresent()) {
return null;
}
}
break;
default:
}
}
ast = opres.result;
if (ast.argSize() < expected[0] || ast.argSize() > expected[1]) {
if (ast.isAST1() && expected.length > 2) {
// because an operator form is allowed do not print a message
return null;
}
if (ast.argSize() > expected[0]) {
if (functionEvaluator instanceof AbstractFunctionOptionEvaluator) {
AbstractFunctionOptionEvaluator optionEvaluator = (AbstractFunctionOptionEvaluator) functionEvaluator;
opres = getOptions(optionEvaluator, opres, ast, expected);
if (opres != null) {
return opres;
}
}
}
IOFunctions.printArgMessage(ast, expected, this);
return null;
}
}
if (functionEvaluator instanceof AbstractFunctionOptionEvaluator) {
AbstractFunctionOptionEvaluator optionEvaluator = (AbstractFunctionOptionEvaluator) functionEvaluator;
opres = getOptions(optionEvaluator, opres, ast, expected);
if (opres != null) {
return opres;
}
}
return opres;
}
Aggregations