Search in sources :

Example 46 with ISymbol

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

the class ModuleReplaceAll method visit.

@Override
public IExpr visit(IAST ast) {
    IdentityHashMap<ISymbol, ISymbol> variables = null;
    if (ast.isASTSizeGE(F.Block, 2) && ast.arg1().isList()) {
        IAST localVariablesList = (IAST) ast.arg1();
        int size = localVariablesList.size();
        for (int i = 1; i < size; i++) {
            IExpr temp = localVariablesList.get(i);
            if (temp.isSymbol()) {
                if (fModuleVariables.get((ISymbol) temp) != null) {
                    if (variables == null) {
                        variables = (IdentityHashMap<ISymbol, ISymbol>) fModuleVariables.clone();
                    }
                    variables.remove((ISymbol) temp);
                }
            } else {
                if (temp.isAST(F.Set, 3)) {
                    // lhs = rhs
                    final IAST setFun = (IAST) temp;
                    if (setFun.arg1().isSymbol()) {
                        if (fModuleVariables.get((ISymbol) setFun.arg1()) != null) {
                            if (variables == null) {
                                variables = (IdentityHashMap<ISymbol, ISymbol>) fModuleVariables.clone();
                            }
                            variables.remove((ISymbol) setFun.arg1());
                        }
                    }
                }
            }
        }
    }
    ModuleReplaceAll visitor = null;
    if (variables == null) {
        visitor = this;
    } else {
        visitor = new ModuleReplaceAll(variables);
    }
    IExpr temp;
    IAST result = F.NIL;
    int i = fOffset;
    while (i < ast.size()) {
        temp = ast.get(i).accept(visitor);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            result = ast.copy();
            result.set(i++, temp);
            break;
        }
        i++;
    }
    if (result.isPresent()) {
        while (i < ast.size()) {
            temp = ast.get(i).accept(visitor);
            if (temp.isPresent()) {
                result.set(i, temp);
            }
            i++;
        }
    }
    return result;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 47 with ISymbol

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

the class Sum method definiteSum.

/**
	 * Evaluate the definite sum: <code>Sum[arg1, {var, from, to}]</code>
	 * 
	 * @param arg1
	 *            the first argument of the <code>Sum[]</code> function.
	 * @param list
	 *            constructed as <code>{Symbol: var, Integer: from, Symbol: to}</code>
	 * @return
	 */
private IExpr definiteSum(final IExpr expr, final IIterator iterator, IAST list, EvalEngine engine) {
    final ISymbol var = iterator.getVariable();
    IExpr arg1 = expr;
    final IExpr from = iterator.getLowerLimit();
    final IExpr to = iterator.getUpperLimit();
    if (arg1.isFree(var, true)) {
        if (from.isOne()) {
            return F.Times(to, arg1);
        }
        if (from.isZero()) {
            return F.Times(Plus(to, C1), arg1);
        }
        if (!engine.evalTrue(F.Greater(C1, from)) && !engine.evalTrue(F.Greater(from, to))) {
            return F.Times(Plus(C1, F.Negate(from), to), arg1);
        }
    } else {
        if (arg1.isTimes()) {
            // Sum[ Times[a,b,c,...], {var, from, to} ]
            IAST filterCollector = F.Times();
            IAST restCollector = F.Times();
            ((IAST) arg1).filter(filterCollector, restCollector, new Predicate<IExpr>() {

                @Override
                public boolean test(IExpr input) {
                    return input.isFree(var, true);
                }
            });
            if (filterCollector.size() > 1) {
                IExpr temp = F.evalQuiet(F.Sum(restCollector.getOneIdentity(F.C1), list));
                if (temp.isFreeAST(F.Sum)) {
                    filterCollector.append(temp);
                    return filterCollector;
                }
            }
        }
        if (arg1.equals(var)) {
            if ((from.isVariable() && !from.equals(var)) || (to.isVariable() && !to.equals(var))) {
                // Sum(i, {i, from, to})
                return Times(C1D2, Plus(Subtract(to, from), C1), Plus(from, to));
            }
        }
        if (!engine.evalTrue(F.Greater(C0, from)) && !engine.evalTrue(F.Greater(from, to))) {
            IExpr temp = F.NIL;
            if (arg1.isPower()) {
                temp = sumPower((IAST) arg1, var, from, to);
            } else if (arg1.equals(var)) {
                temp = sumPowerFormula(from, to, F.C1);
            }
            if (temp.isPresent()) {
                return temp;
            }
        }
        if (arg1.isPower() && !engine.evalTrue(F.Greater(C1, from)) && !engine.evalTrue(F.Greater(from, to))) {
            IAST powAST = (IAST) arg1;
            if (powAST.equalsAt(1, var) && powAST.arg2().isFree(var) && to.isFree(var)) {
                if (from.isOne()) {
                    // i^(k),{i,1,n}) ==> HarmonicNumber(n,-k)
                    return F.HarmonicNumber(to, powAST.arg2().negate());
                }
                // i^k,{i,n,m} ==> HurwitzZeta(-k, n)-HurwitzZeta(-k,1+m)
                return F.Subtract(F.HurwitzZeta(F.Negate(powAST.arg2()), from), F.HurwitzZeta(F.Negate(powAST.arg2()), F.Plus(1, to)));
            }
        }
    }
    if (from.isPositive()) {
        IExpr temp1 = F.evalQuiet(F.Sum(arg1, F.List(var, C0, from.minus(F.C1))));
        if (!temp1.isComplexInfinity() && temp1.isFreeAST(F.Sum)) {
            IExpr temp2 = engine.evalQuietNull(F.Sum(arg1, F.List(var, C0, to)));
            if (temp2.isPresent() && !temp2.isComplexInfinity()) {
                return F.Subtract(temp2, temp1);
            }
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 48 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 49 with ISymbol

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

the class AbstractAST method internalJavaString.

/** {@inheritDoc} */
@Override
public final String internalJavaString(boolean symbolsAsFactoryMethod, int depth, boolean useOperators) {
    final String sep = ",";
    final IExpr temp = head();
    if (temp.equals(F.HoldForm) && size() == 2) {
        return arg1().internalFormString(symbolsAsFactoryMethod, depth);
    }
    if (temp.equals(F.Hold) && size() == 2) {
        return arg1().internalFormString(symbolsAsFactoryMethod, depth);
    }
    if (isInfinity()) {
        return "oo";
    }
    if (isNegativeInfinity()) {
        return "Noo";
    }
    if (isComplexInfinity()) {
        return "CComplexInfinity";
    }
    if (this.equals(F.Slot1)) {
        return "Slot1";
    }
    if (this.equals(F.Slot2)) {
        return "Slot2";
    }
    if (temp.equals(F.Rational) && size() == 3) {
        if (arg1().isInteger() && arg2().isInteger()) {
            return F.QQ((IInteger) arg1(), (IInteger) arg2()).internalJavaString(symbolsAsFactoryMethod, depth, useOperators);
        }
        return arg1().internalFormString(symbolsAsFactoryMethod, depth);
    }
    if (isPower()) {
        if (equalsAt(2, F.C1D2)) {
            if (arg1().isInteger()) {
                // square root of an integer number
                IInteger i = (IInteger) arg1();
                if (i.equals(F.C2)) {
                    return "CSqrt2";
                } else if (i.equals(F.C3)) {
                    return "CSqrt3";
                } else if (i.equals(F.C5)) {
                    return "CSqrt5";
                } else if (i.equals(F.C6)) {
                    return "CSqrt6";
                } else if (i.equals(F.C7)) {
                    return "CSqrt7";
                } else if (i.equals(F.C10)) {
                    return "CSqrt10";
                }
            }
            return "Sqrt(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
        }
        if (equalsAt(2, F.C2)) {
            return "Sqr(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
        }
        if (equalsAt(2, F.CN1D2) && arg1().isInteger()) {
            // negative square root of an integer number
            IInteger i = (IInteger) arg1();
            if (i.equals(F.C2)) {
                return "C1DSqrt2";
            } else if (i.equals(F.C3)) {
                return "C1DSqrt3";
            } else if (i.equals(F.C5)) {
                return "C1DSqrt5";
            } else if (i.equals(F.C6)) {
                return "C1DSqrt6";
            } else if (i.equals(F.C7)) {
                return "C1DSqrt7";
            } else if (i.equals(F.C10)) {
                return "C1DSqrt10";
            }
        }
        if (arg2().isInteger()) {
            try {
                long exp = ((IInteger) arg2()).toLong();
                // create Power(arg1, exp)
                return "Power(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + "," + Long.toString(exp) + ")";
            } catch (RuntimeException ex) {
            }
        }
    }
    StringBuilder text = new StringBuilder(size() * 10);
    if (temp.isSymbol()) {
        ISymbol sym = (ISymbol) temp;
        String name = null;
        if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
            name = sym.toString();
            if (name.length() > 0) {
                name = name.toLowerCase(Locale.ENGLISH);
            }
            name = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(name);
        }
        if (name == null && !Character.isUpperCase(sym.toString().charAt(0))) {
            text.append("$(");
            for (int i = 0; i < size(); i++) {
                text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
                if (i < size() - 1) {
                    text.append(sep);
                }
            }
            text.append(')');
            return text.toString();
        }
    } else if (temp.isPattern() || temp.isAST()) {
        text.append("$(");
        for (int i = 0; i < size(); i++) {
            text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
            if (i < size() - 1) {
                text.append(sep);
            }
        }
        text.append(')');
        return text.toString();
    }
    if (isTimes() && size() == 3 && arg1().isMinusOne() && !arg2().isTimes()) {
        return "Negate(" + arg2().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
    }
    if (useOperators && size() == 3) {
        if (isTimes()) {
            IExpr arg1 = arg1();
            IExpr arg2 = arg2();
            boolean isLowerPrecedence = arg1.isPlus();
            internalOperatorForm(arg1, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
            text.append('*');
            isLowerPrecedence = arg2.isPlus();
            internalOperatorForm(arg2, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
            return text.toString();
        } else if (isPlus()) {
            IExpr arg1 = arg1();
            IExpr arg2 = arg2();
            internalOperatorForm(arg1, false, symbolsAsFactoryMethod, depth, useOperators, text);
            text.append('+');
            internalOperatorForm(arg2, false, symbolsAsFactoryMethod, depth, useOperators, text);
            return text.toString();
        } else if (isPower()) {
            IExpr arg1 = arg1();
            IExpr arg2 = arg2();
            boolean isLowerPrecedence = arg1.isTimes() || arg1.isPlus();
            internalOperatorForm(arg1, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
            text.append('^');
            isLowerPrecedence = arg2.isTimes() || arg2.isPlus();
            internalOperatorForm(arg2, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
            return text.toString();
        }
    }
    text.append(temp.internalJavaString(false, 0, useOperators));
    text.append('(');
    if (isTimes() || isPlus()) {
        if (depth == 0 && isList()) {
            text.append('\n');
        }
        internalFormOrderless(this, text, sep, symbolsAsFactoryMethod, depth, useOperators);
        if (depth == 0 && isList()) {
            text.append('\n');
        }
    } else {
        if (depth == 0 && isList()) {
            text.append('\n');
        }
        for (int i = 1; i < size(); i++) {
            text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
            if (i < size() - 1) {
                text.append(sep);
                if (depth == 0 && isList()) {
                    text.append('\n');
                }
            }
        }
        if (depth == 0 && isList()) {
            text.append('\n');
        }
    }
    text.append(')');
    return text.toString();
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 50 with ISymbol

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

the class AbstractNonOrderlessArgMultiple method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    int size = ast.size();
    if (size == 3) {
        return binaryOperator(ast.arg1(), ast.arg2());
    }
    if (size > 3) {
        final ISymbol sym = ast.topHead();
        final IAST result = F.ast(sym);
        IExpr tres;
        IExpr temp = ast.arg1();
        boolean evaled = false;
        int i = 2;
        while (i < size) {
            tres = binaryOperator(temp, ast.get(i));
            if (!tres.isPresent()) {
                result.append(temp);
                if (i == size - 1) {
                    result.append(ast.get(i));
                } else {
                    temp = ast.get(i);
                }
                i++;
            } else {
                evaled = true;
                temp = tres;
                if (i == (size - 1)) {
                    result.append(temp);
                }
                i++;
            }
        }
        if (evaled) {
            if ((result.isAST1()) && ((sym.getAttributes() & ISymbol.ONEIDENTITY) == ISymbol.ONEIDENTITY)) {
                return result.arg1();
            }
            return result;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3