Search in sources :

Example 26 with IBuiltInSymbol

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

use of org.matheclipse.core.interfaces.IBuiltInSymbol 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();
    return engine.evalAttributes(symbol, this).orElseGet(() -> 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 28 with IBuiltInSymbol

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

the class AbstractAST method isListOfEdges.

/**
 * {@inheritDoc}
 */
@Override
public GraphType isListOfEdges() {
    if (head().equals(S.List)) {
        boolean directed = true;
        for (int i = 1; i < size(); i++) {
            IExpr temp = get(i);
            if (temp.isAST2() && temp.head().isBuiltInSymbol()) {
                IBuiltInSymbol symbol = (IBuiltInSymbol) temp.head();
                if (symbol == S.DirectedEdge || symbol == S.Rule) {
                    continue;
                }
                if (!(symbol == S.UndirectedEdge || symbol == S.TwoWayRule)) {
                    // the row is no list of edges
                    return null;
                }
                directed = false;
            } else {
                return null;
            }
        }
        Builder builder = new DefaultGraphType.Builder();
        if (directed) {
            return builder.directed().build();
        }
        return builder.undirected().build();
    }
    return null;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) Builder(org.jgrapht.graph.DefaultGraphType.Builder) CacheBuilder(com.google.common.cache.CacheBuilder) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 29 with IBuiltInSymbol

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

the class KryoUtil method main.

public static void main(String[] args) throws Exception {
    // List<Class<?>> asList = Arrays.asList(B3.class.getDeclaredClasses());
    // for (int i = 0; i < asList.size(); i++) {
    // System.out.println(
    // "kryo.register(B3." + asList.get(i).getSimpleName() + ".class);");
    // }
    // List<Class<?>> asList = Arrays.asList(UtilityFunctionCtors.class.getDeclaredClasses());
    // for (int i = 0; i < asList.size(); i++) {
    // System.out.println("kryo.register(Class.forName(\"" + asList.get(i).toString().substring(6)
    // + "\"), new IASTSerializer());");
    // }
    F.initSymbols();
    Kryo kryo = initKryo();
    // IBuiltInSymbol object = S.Im;
    // IAST object = F.Sin(F.x);
    // IAST object =F.ternaryAST3(F.f, F.x, F.Dummy("$dummy"), F.stringx("string"));
    Integrate.CONST.await();
    Context rubiContext = Context.RUBI;
    Output rubiOutput = new Output(new FileOutputStream("rubi_context.bin"));
    kryo.writeClassAndObject(rubiOutput, rubiContext);
    rubiOutput.close();
    Input rubInput = new Input(new FileInputStream("rubi_context.bin"));
    rubiContext = (Context) kryo.readClassAndObject(rubInput);
    System.out.println(rubiContext.toString());
    rubInput.close();
    IBuiltInSymbol symbol = S.Integrate;
    RulesData rulesData = symbol.getRulesData();
    AbstractVisitor visitor = Share.createVisitor();
    rulesData.accept(visitor);
    Output output = new Output(new FileOutputStream("integrate.bin"));
    kryo.writeClassAndObject(output, rulesData);
    output.close();
    Input input = new Input(new FileInputStream("integrate.bin"));
    RulesData object2 = (RulesData) kryo.readClassAndObject(input);
    System.out.println(object2.definition());
    input.close();
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) AbstractVisitor(org.matheclipse.core.visit.AbstractVisitor) Input(com.esotericsoftware.kryo.io.Input) RulesData(org.matheclipse.core.patternmatching.RulesData) Output(com.esotericsoftware.kryo.io.Output) FileOutputStream(java.io.FileOutputStream) Kryo(com.esotericsoftware.kryo.Kryo) FileInputStream(java.io.FileInputStream)

Example 30 with IBuiltInSymbol

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

the class S method initFinalSymbol.

/**
 * Convert the symbolName to lowercase (if <code>Config.PARSER_USE_LOWERCASE_SYMBOLS</code> is
 * set) and insert a new Symbol in the <code>PREDEFINED_SYMBOLS_MAP</code>. The symbol is created
 * using the given upper case string to use it as associated class name in package
 * org.matheclipse.core.reflection.system.
 *
 * @param symbolName the predefined symbol name in upper-case form
 * @param ordinal
 * @return
 */
public static IBuiltInSymbol initFinalSymbol(final String symbolName, int ordinal) {
    String str;
    if (ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS) {
        str = (symbolName.length() == 1) ? symbolName : symbolName.toLowerCase();
    } else {
        str = symbolName;
    }
    final IBuiltInSymbol temp = new BuiltInSymbol(str, ordinal);
    BUILT_IN_SYMBOLS[ordinal] = temp;
    org.matheclipse.core.expression.Context.SYSTEM.put(str, temp);
    GLOBAL_IDS_MAP.put(temp, (short) ordinal);
    return temp;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol)

Aggregations

IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)34 IExpr (org.matheclipse.core.interfaces.IExpr)20 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)15 IAST (org.matheclipse.core.interfaces.IAST)13 ISymbol (org.matheclipse.core.interfaces.ISymbol)11 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)6 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 IFunctionEvaluator (org.matheclipse.core.eval.interfaces.IFunctionEvaluator)4 IOException (java.io.IOException)3 IGraphics3D (org.matheclipse.core.graphics.IGraphics3D)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 BufferedReader (java.io.BufferedReader)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