Search in sources :

Example 1 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 2 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 3 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 4 with IBuiltInSymbol

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

the class AbstractAST method opposite.

/**
 * {@inheritDoc}
 */
@Override
public IExpr opposite() {
    final int lhsOrdinal = (head() instanceof IBuiltInSymbol) ? ((IBuiltInSymbol) head()).ordinal() : -1;
    if (lhsOrdinal > 0) {
        if (isTimes()) {
            IExpr arg1 = arg1();
            if (arg1.isNumber()) {
                if (arg1.isMinusOne()) {
                    if (size() == 3) {
                        return arg2();
                    }
                    return rest();
                }
                return setAtCopy(1, ((INumber) arg1).negate());
            }
            IASTAppendable timesAST = copyAppendable();
            timesAST.append(1, F.CN1);
            return timesAST;
        }
        if (isNegativeInfinity()) {
            return F.CInfinity;
        }
        if (isInfinity()) {
            return F.CNInfinity;
        }
        if (isPlus()) {
            return map(x -> x.negate(), 1);
        }
    }
    return F.Times(F.CN1, this);
// return F.eval(F.Times(F.CN1, this));
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with IBuiltInSymbol

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

the class EvalEngine method evalASTArg1.

/**
 * Evaluate an AST with only one argument (i.e. <code>head[arg1]</code>). The evaluation steps are
 * controlled by the header attributes.
 *
 * @param ast
 * @return
 */
private IExpr evalASTArg1(final IAST ast) {
    // special case ast.isAST1()
    // head == ast[0] --- arg1 == ast[1]
    IExpr result = ast.head().evaluateHead(ast, this);
    if (result.isPresent()) {
        return result;
    }
    final ISymbol symbol = ast.topHead();
    final int attributes = symbol.getAttributes();
    if ((attributes & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
        if ((result = F.flattenSequence(ast)).isPresent()) {
            return result;
        }
    }
    if ((result = evalArgs(ast, attributes)).isPresent()) {
        return result;
    }
    final IExpr arg1 = ast.arg1();
    if (ISymbol.hasFlatAttribute(attributes)) {
        if (arg1.head().equals(symbol)) {
            // associative
            return arg1;
        }
        if (arg1.isUnevaluated() && arg1.first().head().equals(symbol) && arg1.first().isAST()) {
            IAST unevaluated = (IAST) arg1.first();
            return unevaluated.map(symbol, x -> F.Unevaluated(x));
        }
    }
    if ((ISymbol.LISTABLE & attributes) == ISymbol.LISTABLE) {
        if (symbol.isBuiltInSymbol()) {
            if (arg1.isRealVector() && ((IAST) arg1).size() > 1) {
                final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
                if (module instanceof DoubleUnaryOperator) {
                    DoubleUnaryOperator oper = (DoubleUnaryOperator) module;
                    return ASTRealVector.map((IAST) arg1, oper);
                }
            } else if (arg1.isRealMatrix()) {
                final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
                if (module instanceof DoubleUnaryOperator) {
                    DoubleUnaryOperator oper = (DoubleUnaryOperator) module;
                    return ASTRealMatrix.map((IAST) arg1, oper);
                }
            }
            if (arg1.isList()) {
                // thread over the list
                return EvalAttributes.threadList(ast, S.List, ast.head(), ((IAST) arg1).argSize());
            } else if (arg1.isAssociation()) {
                // thread over the association
                return ((IAssociation) arg1).mapThread(ast, 1);
            } else if (arg1.isSparseArray()) {
                return ((ISparseArray) arg1).mapThread(ast, 1);
            } else if (arg1.isConditionalExpression()) {
                IExpr temp = ast.extractConditionalExpression(true);
                if (temp.isPresent()) {
                    return temp;
                }
            }
        }
    }
    if ((ISymbol.NUMERICFUNCTION & attributes) == ISymbol.NUMERICFUNCTION) {
        if (ast.arg1().isIndeterminate()) {
            return S.Indeterminate;
        }
    }
    if (!(arg1 instanceof IPatternObject) && arg1.isPresent()) {
        ISymbol lhsSymbol = arg1.isSymbol() ? (ISymbol) arg1 : arg1.topHead();
        return lhsSymbol.evalUpRules(ast, this);
    }
    return F.NIL;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISparseArray(org.matheclipse.core.interfaces.ISparseArray) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Aggregations

IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)33 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