Search in sources :

Example 11 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 12 with IBuiltInSymbol

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;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol)

Example 13 with IBuiltInSymbol

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();
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ComplexNum(org.matheclipse.core.expression.ComplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IExpr(org.matheclipse.core.interfaces.IExpr) INumericComplexConstant(org.matheclipse.core.eval.interfaces.INumericComplexConstant)

Example 14 with IBuiltInSymbol

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();
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) INumericComplex(org.matheclipse.core.eval.interfaces.INumericComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) ComplexNum(org.matheclipse.core.expression.ComplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 15 with IBuiltInSymbol

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;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) AbstractFunctionOptionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

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