Search in sources :

Example 6 with IInteger

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

the class IntegerExponent method evaluate.

@Override
public IExpr evaluate(IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2, 3);
    IInteger base = F.C10;
    if (ast.isAST2()) {
        IExpr arg2 = ast.arg2();
        if (arg2.isInteger() && ((IInteger) arg2).compareInt(1) > 0) {
            base = (IInteger) arg2;
        } else {
            return F.NIL;
        }
    }
    IExpr arg1 = ast.arg1();
    if (arg1.isInteger()) {
        return ((IInteger) arg1).exponent(base);
    }
    return F.NIL;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with IInteger

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

the class Limit method powerLimit.

private static IExpr powerLimit(final IAST powerAST, LimitData data) {
    // IAST rule = data.getRule();
    IExpr arg1 = powerAST.arg1();
    IExpr arg2 = powerAST.arg1();
    if (powerAST.arg2().equals(data.getSymbol()) && data.getLimitValue().isZero()) {
        return F.C1;
    }
    if (arg1.isTimes() && arg2.isFree(data.getSymbol())) {
        IAST isFreeResult = ((IAST) arg1).partitionTimes(Predicates.isFree(data.getSymbol()), F.C1, F.C1, F.List);
        if (!isFreeResult.get(2).isOne()) {
            return F.Times(F.Power(isFreeResult.get(1), arg2), data.limit(F.Power(isFreeResult.get(2), arg2)));
        }
    }
    if (powerAST.arg2().isNumericFunction()) {
        // Limit[a_^exp_,sym->lim] -> Limit[a,sym->lim]^exp
        IExpr exp = powerAST.arg2();
        // IExpr temp = F.evalQuiet(F.Limit(arg1.arg1(), rule));?
        IExpr temp = evalLimitQuiet(powerAST.arg1(), data);
        if (temp.isNumericFunction()) {
            if (temp.isZero()) {
                if (exp.isPositive()) {
                    // 0 ^ (positve exponent)
                    return F.C0;
                }
                if (exp.isNegative()) {
                    // 0 ^ (negative exponent)
                    if (exp.isInteger()) {
                        IInteger n = (IInteger) exp;
                        if (n.isEven()) {
                            return F.CInfinity;
                        }
                        if (data.getDirection() == DIRECTION_FROM_SMALLER_VALUES) {
                            return F.CNInfinity;
                        } else {
                            data.setDirection(DIRECTION_FROM_LARGER_VALUES);
                            return F.CInfinity;
                        }
                    } else if (exp.isFraction()) {
                        if (data.getDirection() != DIRECTION_FROM_SMALLER_VALUES) {
                            data.setDirection(DIRECTION_FROM_LARGER_VALUES);
                            return F.CInfinity;
                        }
                    }
                }
                return F.NIL;
            }
            return F.Power(temp, exp);
        }
        if (exp.isInteger()) {
            IInteger n = (IInteger) exp;
            if (temp.isInfinity()) {
                if (n.isPositive()) {
                    return temp;
                } else if (n.isNegative()) {
                    return F.C0;
                }
                return F.NIL;
            } else if (temp.isNegativeInfinity()) {
                if (n.isPositive()) {
                    if (n.isEven()) {
                        return F.CInfinity;
                    } else {
                        return F.CNInfinity;
                    }
                } else if (n.isNegative()) {
                    return F.C0;
                }
                return F.NIL;
            } else if (temp.equals(F.Indeterminate) || temp.isAST(F.Limit)) {
                return F.NIL;
            }
            if (n.isPositive()) {
                return F.Power(temp, n);
            } else if (n.isNegative() && n.isEven()) {
                return F.Power(temp, n);
            }
        }
    }
    return F.NIL;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 8 with IInteger

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

the class FrobeniusSolve method evaluate.

/** {@inheritDoc} */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    if (ast.arg1().isList()) {
        IAST list = ast.getAST(1);
        try {
            IInteger[][] equations = new IInteger[1][list.size()];
            // format looks like: { { 12, 16, 20, 27, 123 } };
            for (int i = 1; i < list.size(); i++) {
                equations[0][i - 1] = (IInteger) list.get(i);
            }
            equations[0][list.size() - 1] = (IInteger) ast.arg2();
            // all solutions
            int numberOfSolutions = -1;
            if (ast.size() == 4) {
                numberOfSolutions = ((ISignedNumber) ast.arg3()).toInt();
            }
            FrobeniusSolver solver = new FrobeniusSolver(equations);
            IInteger[] solution;
            IAST result = F.List();
            if (numberOfSolutions < 0) {
                while ((solution = solver.take()) != null) {
                    result.append(Lists.asList(solution));
                }
            } else {
                while ((solution = solver.take()) != null) {
                    if (--numberOfSolutions < 0) {
                        break;
                    }
                    result.append(Lists.asList(solution));
                }
            }
            return result;
        } catch (RuntimeException e) {
            if (Config.SHOW_STACKTRACE) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : FrobeniusSolver(org.matheclipse.core.frobenius.FrobeniusSolver) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST)

Example 9 with IInteger

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

the class SolutionProviderAbstract method currentRemainders.

@Override
public IInteger[] currentRemainders() {
    IInteger[] remainders = new IInteger[coefficients.length];
    IInteger factor = currentCounter.subtract(F.C1);
    for (int i = 0; i < coefficients.length; ++i) {
        remainders[i] = currentRemainder[i].subtract(coefficients[i].multiply(factor));
    }
    return remainders;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger)

Example 10 with IInteger

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

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            if (expr instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    //
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IInteger (org.matheclipse.core.interfaces.IInteger)59 IAST (org.matheclipse.core.interfaces.IAST)34 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)18 IFraction (org.matheclipse.core.interfaces.IFraction)16 BigInteger (java.math.BigInteger)11 INum (org.matheclipse.core.interfaces.INum)10 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)6 ExpVector (edu.jas.poly.ExpVector)5 INumber (org.matheclipse.core.interfaces.INumber)4 ModLong (edu.jas.arith.ModLong)3 GenPolynomial (edu.jas.poly.GenPolynomial)3 Num (org.matheclipse.core.expression.Num)3 IRational (org.matheclipse.core.interfaces.IRational)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 TreeSet (java.util.TreeSet)2 BigFraction (org.hipparchus.fraction.BigFraction)2 KSubsetsList (org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)2