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;
}
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;
}
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;
}
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));
}
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;
}
Aggregations