Search in sources :

Example 16 with IBuiltInSymbol

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

the class Derivative method evaluateDIfPossible.

private static IExpr evaluateDIfPossible(IAST head, IAST headDerivative, IAST fullDerivative, EvalEngine engine) {
    IASTAppendable newFunction = F.ast(headDerivative.arg1());
    IASTAppendable list = F.ListAlloc(headDerivative.size());
    IASTAppendable dExpr;
    for (int i = 1; i < head.size(); i++) {
        IExpr n = head.get(i);
        IExpr symbol = F.Slot(i);
        if (fullDerivative != null) {
            if (fullDerivative.size() != headDerivative.size()) {
                return F.NIL;
            }
            symbol = fullDerivative.get(i);
            if (!symbol.isVariable()) {
                return F.NIL;
            }
        }
        newFunction.append(symbol);
        if (n.isOne()) {
            list.append(symbol);
        } else {
            int ni = n.toIntDefault();
            if (ni < 0) {
                if (ni == Integer.MIN_VALUE) {
                    list.append(F.list(symbol, n));
                } else {
                    return F.NIL;
                }
            } else if (ni > 0) {
                int iterationLimit = engine.getIterationLimit();
                if (iterationLimit > 0 && iterationLimit < ni) {
                    // Iteration limit of `1` exceeded.
                    return IOFunctions.printMessage(S.Derivative, "itlim", F.list(F.ZZ(iterationLimit)), engine);
                }
                list.append(F.list(symbol, n));
            }
        }
    }
    boolean doEval = false;
    IExpr temp = newFunction;
    if (headDerivative.arg1().isBuiltInSymbol()) {
        IBuiltInSymbol builtin = (IBuiltInSymbol) headDerivative.arg1();
        if (builtin.isNumericFunctionAttribute()) {
            if (head.isAST1()) {
                int n = head.first().toIntDefault();
                if (n > 0) {
                    IExpr dResult = S.Derivative.evalDownRule(engine, (n == 1) ? headDerivative : headDerivative.setAtCopy(0, head.setAtCopy(1, F.C1)));
                    if (dResult.isPresent()) {
                        doEval = true;
                    }
                }
            } else {
                IExpr dResult = S.Derivative.evalDownRule(engine, headDerivative);
                if (dResult.isPresent()) {
                    doEval = true;
                }
            }
        }
    } else {
        temp = engine.evaluateNIL(newFunction);
        if (temp.isPresent()) {
            doEval = true;
        }
    }
    if (doEval) {
        dExpr = F.ast(S.D, list.size() + 1);
        dExpr.append(temp);
        // w.r.t these symbols
        dExpr.appendArgs(list);
        return F.Function(engine.evaluate(dExpr));
    }
    return F.NIL;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 17 with IBuiltInSymbol

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

the class F method $s.

/**
	 * <p>
	 * Get or create a global predefined symbol which is retrieved from the SYSTEM context map or created or retrieved
	 * from the SYSTEM context variables map.
	 * </p>
	 * <p>
	 * <b>Note:</b> user defined variables on the context path are defined with method <code>userSymbol()</code>
	 * </p>
	 * 
	 * @param symbolName
	 *            the name of the symbol
	 * @param setEval
	 *            if <code>true</code> determine and assign the built-in evaluator object to the symbol.
	 * @return
	 */
private static ISymbol $s(final String symbolName, boolean setEval) {
    String name = symbolName;
    if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
        if (symbolName.length() == 1) {
            name = symbolName;
        } else {
            name = symbolName.toLowerCase(Locale.ENGLISH);
        }
    }
    ISymbol symbol = Context.PREDEFINED_SYMBOLS_MAP.get(name);
    if (symbol != null) {
        return symbol;
    }
    if (Config.SERVER_MODE) {
        symbol = HIDDEN_SYMBOLS_MAP.get(name);
        // symbol = engine.getUserVariable(name);
        if (symbol != null) {
            return symbol;
        }
        if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
            if (SYMBOL_OBSERVER.createPredefinedSymbol(name)) {
                // second try, because the symbol may now be added to
                // fSymbolMap
                ISymbol secondTry = Context.PREDEFINED_SYMBOLS_MAP.get(name);
                if (secondTry != null) {
                    return secondTry;
                }
            }
        } else {
            if (Character.isUpperCase(name.charAt(0))) {
                if (SYMBOL_OBSERVER.createPredefinedSymbol(name)) {
                    // second try, because the symbol may now be added to
                    // fSymbolMap
                    ISymbol secondTry = Context.PREDEFINED_SYMBOLS_MAP.get(name);
                    if (secondTry != null) {
                        return secondTry;
                    }
                }
            }
        }
        symbol = new BuiltInSymbol(name);
        // engine.putUserVariable(name, symbol);
        HIDDEN_SYMBOLS_MAP.put(name, symbol);
        if (name.charAt(0) == '$') {
            SYMBOL_OBSERVER.createUserSymbol(symbol);
        }
    } else {
        symbol = HIDDEN_SYMBOLS_MAP.get(name);
        if (symbol != null) {
            return symbol;
        }
        symbol = new BuiltInSymbol(name);
        HIDDEN_SYMBOLS_MAP.put(name, symbol);
        if (symbol.isBuiltInSymbol()) {
            if (!setEval) {
                ((IBuiltInSymbol) symbol).setEvaluator(BuiltInSymbol.DUMMY_EVALUATOR);
            } else {
                ((IBuiltInSymbol) symbol).getEvaluator();
            }
        }
    }
    return symbol;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) ISymbol(org.matheclipse.core.interfaces.ISymbol) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol)

Example 18 with IBuiltInSymbol

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

the class F method initFinalSymbol.

/**
	 * Insert a new Symbol in the <code>SYSTEM</code> context.
	 * 
	 * @param symbolName
	 *            the predefined symbol name in upper-case form
	 * @param evaluator
	 *            defines the evaluation behaviour of the symbol
	 * @return
	 */
public static IBuiltInSymbol initFinalSymbol(final String symbolName, IEvaluator evaluator) {
    IBuiltInSymbol temp = new BuiltInSymbol(symbolName, evaluator);
    evaluator.setUp(temp);
    Context.SYSTEM.put(symbolName, temp);
    return temp;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol)

Example 19 with IBuiltInSymbol

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

the class F 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
	 * @return
	 */
public static IBuiltInSymbol initFinalSymbol(final String symbolName) {
    IBuiltInSymbol temp = new BuiltInSymbol(symbolName);
    Context.SYSTEM.put(symbolName, temp);
    return temp;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol)

Example 20 with IBuiltInSymbol

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

the class F method localBiFunction.

public static IBuiltInSymbol localBiFunction(final String symbolName, BiFunction<IExpr, IExpr, IExpr> function) {
    IBuiltInSymbol localBuittIn = new BuiltInSymbol(symbolName, java.lang.Integer.MAX_VALUE);
    localBuittIn.setEvaluator(new AbstractCoreFunctionEvaluator() {

        @Override
        public IExpr evaluate(IAST ast, EvalEngine engine) {
            return function.apply(ast.arg1(), ast.arg2());
        }
    });
    return localBuittIn;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) AbstractCoreFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractCoreFunctionEvaluator) EvalEngine(org.matheclipse.core.eval.EvalEngine) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) 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