Search in sources :

Example 41 with IInteger

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

the class BigIntegerSym method multiply.

@Override
public IRational multiply(IRational parm1) {
    if (parm1.isZero()) {
        return F.C0;
    }
    if (parm1.isOne()) {
        return this;
    }
    if (parm1.isMinusOne()) {
        return this.negate();
    }
    if (parm1 instanceof IFraction) {
        return ((IFraction) parm1).multiply(this);
    }
    IInteger p1 = (IInteger) parm1;
    BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator());
    return valueOf(newnum);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger)

Example 42 with IInteger

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

the class BigFractionSym method multiply.

@Override
public IRational multiply(IRational parm1) {
    if (parm1.isOne()) {
        return this;
    }
    if (parm1.isZero()) {
        return parm1;
    }
    if (parm1.isMinusOne()) {
        return this.negate();
    }
    if (parm1 instanceof IFraction) {
        return mul((IFraction) parm1);
    }
    IInteger p1 = (IInteger) parm1;
    BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator());
    return valueOf(newnum, toBigDenominator());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger)

Example 43 with IInteger

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

the class Coefficient method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IExpr arg2 = ast.arg2();
    // list of variable expressions extracted from the second argument
    IAST listOfVariables = null;
    // array of corresponding exponents for the list of variables
    long[] exponents = null;
    if (arg2.isTimes()) {
        // Times(x, y^a,...)
        IAST arg2AST = (IAST) arg2;
        VariablesSet eVar = new VariablesSet(arg2AST);
        listOfVariables = eVar.getVarList();
        exponents = new long[listOfVariables.size() - 1];
        for (int i = 0; i < exponents.length; i++) {
            exponents[i] = 0L;
        }
        for (int i = 1; i < arg2AST.size(); i++) {
            long value = 1L;
            IExpr a1 = arg2AST.get(i);
            if (arg2AST.get(i).isPower() && arg2AST.get(i).getAt(2).isInteger()) {
                a1 = arg2AST.get(i).getAt(1);
                IInteger ii = (IInteger) arg2AST.get(i).getAt(2);
                try {
                    value = ii.toLong();
                } catch (ArithmeticException ae) {
                    return F.NIL;
                }
            }
            if (!setExponent(listOfVariables, a1, exponents, value)) {
                return F.NIL;
            }
        }
    } else {
        listOfVariables = F.List();
        listOfVariables.append(arg2);
        exponents = new long[1];
        exponents[0] = 1;
    }
    try {
        long n = 1;
        if (ast.isAST3()) {
            if (ast.arg3().isNegativeInfinity()) {
                return F.C0;
            }
            n = Validate.checkLongType(ast.arg3());
            for (int i = 0; i < exponents.length; i++) {
                exponents[i] *= n;
            }
        }
        ExpVectorLong expArr = new ExpVectorLong(exponents);
        IExpr expr = F.evalExpandAll(ast.arg1());
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables, listOfVariables.size() - 1);
        ExprPolynomial poly = ring.create(expr, true, true);
        return poly.coefficient(expArr);
    } catch (RuntimeException ae) {
        if (Config.DEBUG) {
            ae.printStackTrace();
        }
        return F.C0;
    }
}
Also used : ExpVectorLong(org.matheclipse.core.polynomials.ExpVectorLong) ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 44 with IInteger

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

the class Solve method integerVariable.

public static IntVariable integerVariable(Network net, TreeMap<ISymbol, IntVariable> map, IExpr expr) throws ArithmeticException {
    if (expr instanceof ISymbol) {
        return map.get(expr);
    }
    if (expr instanceof IInteger) {
        // throws ArithmeticException
        int value = ((IInteger) expr).toInt();
        return new IntVariable(net, value);
    }
    if (expr.isPlus()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.add(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isTimes()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.multiply(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isPower()) {
        IAST ast = (IAST) expr;
        if (ast.arg2().isInteger()) {
            int value = ((IInteger) ast.arg2()).toInt();
            if (value > 0) {
                IntVariable result = integerVariable(net, map, ast.arg1());
                for (int i = 1; i < value; i++) {
                    result = result.multiply(integerVariable(net, map, ast.arg1()));
                }
                return result;
            }
        }
    } else if (expr.isASTSizeGE(F.Max, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.max(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isASTSizeGE(F.Min, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.min(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isAbs()) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).abs();
    } else if (expr.isAST(F.Sign, 2)) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).sign();
    }
    throw new WrongArgumentType(expr, "for Solve(..., Integers)");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IntVariable(jp.ac.kobe_u.cs.cream.IntVariable)

Example 45 with IInteger

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

the class PowerMod method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    for (int i = 1; i < ast.size(); i++) {
        if (!(ast.get(i).isInteger())) {
            return F.NIL;
        }
    }
    IInteger arg1 = (IInteger) ast.get(1);
    IInteger arg2 = (IInteger) ast.get(2);
    IInteger arg3 = (IInteger) ast.get(3);
    try {
        if (arg2.isMinusOne()) {
            return arg1.modInverse(arg3);
        }
        return arg1.modPow(arg2, arg3);
    } catch (ArithmeticException ae) {
        engine.printMessage("PowerMod: " + ae.getMessage());
    }
    return F.NIL;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger)

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