Search in sources :

Example 1 with Num

use of org.matheclipse.core.expression.Num in project symja_android_library by axkr.

the class EvalComplex method evalSymbol.

public static double[] evalSymbol(final ISymbol symbol) {
    if (symbol.hasLocalVariableStack()) {
        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.isSignedNumberConstant()) {
        // 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 2 with Num

use of org.matheclipse.core.expression.Num 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.size() - 1);
        }
    }
    // 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 3 with Num

use of org.matheclipse.core.expression.Num in project symja_android_library by axkr.

the class AbstractArg12 method binaryOperator.

public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
    IExpr result = F.NIL;
    if (o0 instanceof ApcomplexNum) {
        if (o1.isNumber()) {
            result = e2ApcomplexArg((ApcomplexNum) o0, ((INumber) o1).apcomplexNumValue(((ApcomplexNum) o0).precision()));
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ApcomplexNum) {
        if (o0.isNumber()) {
            result = e2ApcomplexArg(((INumber) o0).apcomplexNumValue(((ApcomplexNum) o1).precision()), (ApcomplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o0 instanceof ComplexNum) {
        if (o1.isNumber()) {
            result = e2DblComArg((ComplexNum) o0, ((INumber) o1).complexNumValue());
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ComplexNum) {
        if (o0.isNumber()) {
            result = e2DblComArg(((INumber) o0).complexNumValue(), (ComplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    if (o0 instanceof ApfloatNum) {
        if (o1.isSignedNumber()) {
            result = e2ApfloatArg((ApfloatNum) o0, ((ISignedNumber) o1).apfloatNumValue(((ApfloatNum) o0).precision()));
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ApfloatNum) {
        if (o0.isSignedNumber()) {
            result = e2ApfloatArg(((ISignedNumber) o0).apfloatNumValue(((ApfloatNum) o1).precision()), (ApfloatNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o0 instanceof Num) {
        if (o1.isSignedNumber()) {
            result = e2DblArg((Num) o0, ((ISignedNumber) o1).numValue());
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof Num) {
        if (o0.isSignedNumber()) {
            result = e2DblArg(((ISignedNumber) o0).numValue(), (Num) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    if (o0 instanceof IInteger) {
        if (o1 instanceof IInteger) {
            return e2IntArg((IInteger) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IFraction) {
        if (o1 instanceof IInteger) {
            return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
        }
        if (o1 instanceof IFraction) {
            return e2FraArg((IFraction) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IComplex) {
        if (o1 instanceof IInteger) {
            return eComIntArg((IComplex) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return eComFraArg((IComplex) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg((IComplex) o0, (IComplex) o1);
        }
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
    }
    if (o0 instanceof IAST) {
        if (o1 instanceof IInteger) {
            return eFunIntArg((IAST) o0, (IInteger) o1);
        }
        if (o1 instanceof IAST) {
            return e2FunArg((IAST) o0, (IAST) o1);
        }
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) ComplexNum(org.matheclipse.core.expression.ComplexNum) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) INum(org.matheclipse.core.interfaces.INum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum)

Example 4 with Num

use of org.matheclipse.core.expression.Num in project symja_android_library by axkr.

the class Iterator method create.

/**
	 * Iterator specification for functions like <code>Table()</code> or
	 * <code>Sum()</code> or <code>Product()</code>
	 * 
	 * @param list
	 *            a list representing an iterator specification
	 * @param engine
	 *            the evaluation engine
	 * @return the iterator
	 */
public static IIterator<IExpr> create(final IAST list, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    // fNumericMode = evalEngine.isNumericMode() ||
    // list.isMember(Predicates.isNumeric(), false);
    boolean localNumericMode = evalEngine.isNumericMode();
    try {
        if (list.hasNumericArgument()) {
            evalEngine.setNumericMode(true);
        }
        fNumericMode = evalEngine.isNumericMode();
        switch(list.size()) {
            case 2:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                step = F.C1;
                variable = null;
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = F.C1;
                if (list.arg1().isSymbol()) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
                    ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
                    ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
                    return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
                }
                break;
            case 5:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = evalEngine.evalWithoutNumericReset(list.arg4());
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        int iStep = ((IInteger) step).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) INum(org.matheclipse.core.interfaces.INum) Num(org.matheclipse.core.expression.Num) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with Num

use of org.matheclipse.core.expression.Num in project symja_android_library by axkr.

the class Iterator method create.

/**
	 * Iterator specification for functions like <code>Table()</code> or
	 * <code>Sum()</code> or <code>Product()</code>
	 * 
	 * @param list
	 *            a list representing an iterator specification
	 * @param symbol
	 *            the variable symbol
	 * @param engine
	 *            the evaluation engine
	 * @return the iterator
	 */
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    boolean localNumericMode = evalEngine.isNumericMode();
    try {
        if (list.hasNumericArgument()) {
            evalEngine.setNumericMode(true);
        }
        fNumericMode = evalEngine.isNumericMode();
        switch(list.size()) {
            case 2:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                step = F.C1;
                variable = symbol;
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(symbol, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                variable = symbol;
                if (lowerLimit instanceof Num && upperLimit instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(symbol, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = evalEngine.evalWithoutNumericReset(list.arg3());
                variable = symbol;
                if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        int iStep = ((IInteger) step).toInt();
                        return new IntIterator(symbol, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) INum(org.matheclipse.core.interfaces.INum) Num(org.matheclipse.core.expression.Num) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

Num (org.matheclipse.core.expression.Num)6 IExpr (org.matheclipse.core.interfaces.IExpr)6 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 ComplexNum (org.matheclipse.core.expression.ComplexNum)3 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)3 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 IRational (org.matheclipse.core.interfaces.IRational)2 INumeric (org.matheclipse.core.eval.interfaces.INumeric)1 INumericComplex (org.matheclipse.core.eval.interfaces.INumericComplex)1 INumericComplexConstant (org.matheclipse.core.eval.interfaces.INumericComplexConstant)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)1 IAST (org.matheclipse.core.interfaces.IAST)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)1 IFraction (org.matheclipse.core.interfaces.IFraction)1