Search in sources :

Example 11 with IEvaluator

use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.

the class ExpressionJSONConvert method exportGraphics3DJSON.

private static JsonNode exportGraphics3DJSON(IExpr data3D) {
    if (data3D.isList()) {
        IAST list = (IAST) data3D;
        ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
        for (int i = 1; i < list.size(); i++) {
            IExpr arg = list.getAST(i);
            if (arg.isAST()) {
                IAST ast = (IAST) arg;
                if (ast.head().isBuiltInSymbol()) {
                    StringBuilder buf = new StringBuilder();
                    IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
                    IEvaluator evaluator = symbol.getEvaluator();
                    if (evaluator instanceof IGraphics3D) {
                    // JsonNode n = ((IGraphics3D) evaluator).graphics3D(buf, (IAST) ast);
                    // temp.add(n);
                    }
                }
            }
        }
        return temp;
    }
    ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
    temp.add(temp.toString());
    return temp;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IAST(org.matheclipse.core.interfaces.IAST) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IExpr(org.matheclipse.core.interfaces.IExpr) IGraphics3D(org.matheclipse.core.graphics.IGraphics3D)

Example 12 with IEvaluator

use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.

the class DoubleStackEvaluator method evalAST.

public static double evalAST(double[] stack, final int top, final IAST ast) {
    if (ast.head().isBuiltInSymbol()) {
        final IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
        final IEvaluator module = symbol.getEvaluator();
        if (module instanceof INumeric) {
            int newTop = top;
            // fast evaluation path
            if (top + ast.size() >= stack.length) {
                stack = new double[ast.size() + 50];
            }
            for (int i = 1; i < ast.size(); i++) {
                ++newTop;
                stack[newTop] = DoubleStackEvaluator.eval(stack, newTop, ast.get(i));
            }
            return ((INumeric) module).evalReal(stack, newTop, ast.argSize());
        }
    }
    // slow evaluation path
    final IExpr result = F.evaln(ast);
    if (result.isReal()) {
        return ((ISignedNumber) result).doubleValue();
    }
    if (result instanceof IComplexNum && F.isZero(((IComplexNum) result).imDoubleValue())) {
        return ((IComplexNum) result).reDoubleValue();
    }
    throw new UnsupportedOperationException("EvalDouble#evalAST(): " + ast);
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) INumeric(org.matheclipse.core.eval.interfaces.INumeric) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 13 with IEvaluator

use of org.matheclipse.core.interfaces.IEvaluator 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 14 with IEvaluator

use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.

the class FunctionIDGenerator method printGithubSymjaFunctionLineNumber.

public static void printGithubSymjaFunctionLineNumber() {
    try {
        ParserConfig.EXPLICIT_TIMES_OPERATOR = true;
        F.initSymbols();
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < AST2Expr.UPPERCASE_SYMBOL_STRINGS.length; i++) {
            list.add(AST2Expr.UPPERCASE_SYMBOL_STRINGS[i]);
        }
        for (int i = 0; i < AST2Expr.SYMBOL_STRINGS.length; i++) {
            list.add(AST2Expr.SYMBOL_STRINGS[i]);
        }
        for (int i = 0; i < AST2Expr.FUNCTION_STRINGS.length; i++) {
            list.add(AST2Expr.FUNCTION_STRINGS[i]);
        }
        for (int i = 0; i < AST2Expr.DOLLAR_STRINGS.length; i++) {
            list.add(AST2Expr.DOLLAR_STRINGS[i]);
        }
        Collections.sort(list);
        int counter = 0;
        System.out.println("/**\n" + "   * Generated by class: <code>org.matheclipse.core.preprocessor.FunctionIDGenerator\n" + "   * </code>\n" + "   */\n" + "public final static int[] LINE_NUMBER_OF_JAVA_CLASS = new int[] { //");
        for (int i = 0; i < list.size(); i++) {
            counter++;
            if (counter > 10) {
                System.out.println(" //");
                counter = 0;
            }
            ISymbol sym = F.symbol(list.get(i));
            if (sym instanceof IBuiltInSymbol) {
                IBuiltInSymbol builtin = (IBuiltInSymbol) sym;
                IEvaluator eval = builtin.getEvaluator();
                if (eval != null && eval != BuiltInSymbol.DUMMY_EVALUATOR) {
                    Class<? extends IEvaluator> clazz = eval.getClass();
                    if (// 
                    !clazz.isAnonymousClass() && !clazz.isSynthetic() && !clazz.isInterface() && !clazz.isPrimitive()) {
                        // 
                        String fileName = buildFileNameL(clazz);
                        File sourceLocation = new File(fileName);
                        int lineCounter = lineNumberOfClass(sourceLocation, list.get(i));
                        System.out.print(lineCounter);
                    } else {
                        System.out.print("0");
                    }
                } else {
                    System.out.print("0");
                }
            } else {
                System.out.print("0");
            }
            if (i < list.size() - 1) {
                System.out.print(",");
            }
        }
        System.out.print("};");
    } catch (RuntimeException ex) {
        System.out.println();
        ex.printStackTrace();
    }
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISymbol(org.matheclipse.core.interfaces.ISymbol) ArrayList(java.util.ArrayList) File(java.io.File)

Example 15 with IEvaluator

use of org.matheclipse.core.interfaces.IEvaluator in project symja_android_library by axkr.

the class GraphicsFunctions method exportGraphics3DRecursive.

public static boolean exportGraphics3DRecursive(StringBuilder buf, IAST data3D) {
    if (data3D.isList()) {
        boolean first = true;
        IAST rgbColor = F.NIL;
        IExpr opacity = F.NIL;
        IAST list = data3D;
        for (int i = 1; i < list.size(); i++) {
            IExpr arg = list.get(i);
            if (arg.isAST()) {
                IAST ast = (IAST) arg;
                if (ast.isList()) {
                    StringBuilder primitivesBuffer = new StringBuilder();
                    if (exportGraphics3DRecursive(primitivesBuffer, ast)) {
                        if (!first) {
                            buf.append(",");
                        }
                        first = false;
                        buf.append(primitivesBuffer);
                    }
                } else if (ast.isRGBColor()) {
                    rgbColor = ast;
                } else if (ast.isAST(S.Opacity, 2)) {
                    opacity = ast.arg1();
                } else if (ast.head().isBuiltInSymbol()) {
                    IBuiltInSymbol symbol = (IBuiltInSymbol) ast.head();
                    IEvaluator evaluator = symbol.getEvaluator();
                    if (evaluator instanceof IGraphics3D) {
                        StringBuilder primitivesBuffer = new StringBuilder();
                        if (((IGraphics3D) evaluator).graphics3D(primitivesBuffer, ast, rgbColor, opacity)) {
                            if (!first) {
                                buf.append(",");
                            }
                            first = false;
                            buf.append(primitivesBuffer);
                        }
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IGraphics3D(org.matheclipse.core.graphics.IGraphics3D)

Aggregations

IEvaluator (org.matheclipse.core.interfaces.IEvaluator)16 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)15 IExpr (org.matheclipse.core.interfaces.IExpr)12 IAST (org.matheclipse.core.interfaces.IAST)10 ISymbol (org.matheclipse.core.interfaces.ISymbol)6 EvalEngine (org.matheclipse.core.eval.EvalEngine)4 IFunctionEvaluator (org.matheclipse.core.eval.interfaces.IFunctionEvaluator)3 IGraphics3D (org.matheclipse.core.graphics.IGraphics3D)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ArrayList (java.util.ArrayList)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 Num (org.matheclipse.core.expression.Num)2 ByteArrayExpr (org.matheclipse.core.expression.data.ByteArrayExpr)2 IDistribution (org.matheclipse.core.interfaces.IDistribution)2 RomanArabicConverter (com.baeldung.algorithms.romannumerals.RomanArabicConverter)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Suppliers (com.google.common.base.Suppliers)1