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 F method localFunction.
public static IBuiltInSymbol localFunction(String symbolName, IEvaluator evaluator) {
IBuiltInSymbol localBuiltIn = new BuiltInDummy(symbolName);
localBuiltIn.setEvaluator(evaluator);
return localBuiltIn;
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class EvalComplex method evalSymbol.
public static double[] evalSymbol(final ISymbol symbol) {
IExpr expr = symbol.assignedValue();
if (expr != null) {
// final IExpr expr = symbol.get();
if (expr instanceof ISignedNumber) {
final double[] result = new double[2];
result[0] = ((ISignedNumber) expr).doubleValue();
result[1] = 0.0;
return result;
}
if (expr instanceof ComplexNum) {
final double[] result = new double[2];
result[0] = ((ComplexNum) expr).getRealPart();
result[1] = ((ComplexNum) expr).getImaginaryPart();
return result;
}
}
if (symbol.isRealConstant()) {
// fast evaluation path
final double[] result = new double[2];
result[0] = ((ISignedNumberConstant) ((IBuiltInSymbol) symbol).getEvaluator()).evalReal();
result[1] = 0.0;
return result;
}
if (symbol.isBuiltInSymbol()) {
final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
if (module instanceof INumericComplexConstant) {
// fast evaluation path
return ((INumericComplexConstant) module).evalComplex();
}
}
// slow evaluation path
final IExpr result = F.evaln(symbol);
if (result instanceof ComplexNum) {
final double[] res = new double[2];
res[0] = ((ComplexNum) result).getRealPart();
res[1] = ((ComplexNum) result).getImaginaryPart();
return res;
}
if (result instanceof Num) {
final double[] res = new double[2];
res[0] = ((Num) result).doubleValue();
res[1] = 0.0;
return res;
}
throw new UnsupportedOperationException();
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class EvalComplex method evalAST.
public static double[] evalAST(final DoubleStack stack, final int top, final IAST ast) {
final int newTop = top;
final ISymbol symbol = (ISymbol) ast.get(0);
if (symbol.isBuiltInSymbol()) {
final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
if (module instanceof INumericComplex) {
// fast evaluation path
stack.ensureCapacity(top + ast.size() * 2);
for (int i = 1; i < ast.size(); i++) {
final double[] result = eval(stack, newTop, ast.get(i));
stack.push(result[0]);
stack.push(result[1]);
}
return ((INumericComplex) module).evalComplex(stack, ast.argSize());
}
}
// slow evaluation path
final IExpr result = F.evaln(ast);
if (result instanceof ComplexNum) {
final double[] res = new double[2];
res[0] = ((ComplexNum) result).getRealPart();
res[1] = ((ComplexNum) result).getImaginaryPart();
return res;
}
if (result instanceof Num) {
final double[] res = new double[2];
res[0] = ((Num) result).doubleValue();
res[1] = 0.0;
return res;
}
throw new UnsupportedOperationException();
}
use of org.matheclipse.core.interfaces.IBuiltInSymbol in project symja_android_library by axkr.
the class EvalEngine method evalASTBuiltinFunction.
/**
* @param symbol
* @param ast
* @return <code>F.NIL</code> if no evaluation happened
*/
private IExpr evalASTBuiltinFunction(final ISymbol symbol, IAST ast) {
final int attributes = symbol.getAttributes();
if (fEvalLHSMode) {
if ((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL) {
// (i.e. Sin, Cos,...)
if (!(symbol.equals(S.Set) || symbol.equals(S.SetDelayed) || symbol.equals(S.UpSet) || symbol.equals(S.UpSetDelayed))) {
return F.NIL;
}
} else {
if ((ISymbol.NUMERICFUNCTION & attributes) != ISymbol.NUMERICFUNCTION) {
return F.NIL;
}
}
}
if (!symbol.equals(S.Integrate)) {
IExpr result;
if ((result = symbol.evalDownRule(this, ast)).isPresent()) {
return result;
}
}
if (symbol.isBuiltInSymbol()) {
final IEvaluator evaluator = ((IBuiltInSymbol) symbol).getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
if (ast.isEvalFlagOn(IAST.BUILT_IN_EVALED) && isSymbolicMode(attributes)) {
return F.NIL;
}
// evaluate a built-in function.
final IFunctionEvaluator functionEvaluator = (IFunctionEvaluator) evaluator;
OptionsResult opres = checkBuiltinArguments(ast, functionEvaluator);
if (opres == null) {
return F.NIL;
}
ast = opres.result;
try {
if (evaluator instanceof AbstractFunctionOptionEvaluator) {
AbstractFunctionOptionEvaluator optionsEvaluator = (AbstractFunctionOptionEvaluator) evaluator;
IExpr result = optionsEvaluator.evaluate(ast, opres.argSize, opres.options, this);
if (result.isPresent()) {
return result;
}
} else {
IExpr result = fNumericMode ? functionEvaluator.numericEval(ast, this) : functionEvaluator.evaluate(ast, this);
if (result.isPresent()) {
return result;
}
}
} catch (ValidateException ve) {
ve.printStackTrace();
return IOFunctions.printMessage(ast.topHead(), ve, this);
} catch (FlowControlException e) {
throw e;
} catch (SymjaMathException ve) {
LOGGER.log(getLogLevel(), ast.topHead(), ve);
return F.NIL;
}
// cannot generally set the result as evaluated in built-in function. Especially problems in
// `togetherMode`
// if (isSymbolicMode(attributes) && !isTogetherMode()) {
// ast.addEvalFlags(IAST.BUILT_IN_EVALED);
// }
}
}
return F.NIL;
}
Aggregations