Search in sources :

Example 1 with ICoreFunctionEvaluator

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);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) ICoreFunctionEvaluator(org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator)

Example 2 with ICoreFunctionEvaluator

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);
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) ICoreFunctionEvaluator(org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) EvalEngine(org.matheclipse.core.eval.EvalEngine) PatternMatcherEvalEngine(org.matheclipse.core.patternmatching.PatternMatcherEvalEngine) IAST(org.matheclipse.core.interfaces.IAST) IAssociation(org.matheclipse.core.interfaces.IAssociation) ISymbol(org.matheclipse.core.interfaces.ISymbol) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) Parameter(java.lang.reflect.Parameter) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with ICoreFunctionEvaluator

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);
}
Also used : ICoreFunctionEvaluator(org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

ICoreFunctionEvaluator (org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator)3 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)1 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)1 ValidateException (org.matheclipse.core.eval.exception.ValidateException)1 IAssociation (org.matheclipse.core.interfaces.IAssociation)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)1 PatternMatcherEvalEngine (org.matheclipse.core.patternmatching.PatternMatcherEvalEngine)1