Search in sources :

Example 1 with ModLong

use of edu.jas.arith.ModLong in project symja_android_library by axkr.

the class MonomialList method monomialListModulus.

/**
	 * Get the monomial list of a univariate polynomial with coefficients
	 * reduced by a modulo value.
	 * 
	 * @param polynomial
	 * @param variable
	 * @param termOrder
	 *            the JAS term ordering
	 * @param option
	 *            the "Modulus" option
	 * @return the list of monomials of the univariate polynomial.
	 */
private static IAST monomialListModulus(IExpr polynomial, List<IExpr> variablesList, final TermOrder termOrder, IExpr option) throws JASConversionException {
    try {
        // found "Modulus" option => use ModIntegerRing
        ModLongRing modIntegerRing = JASModInteger.option2ModLongRing((ISignedNumber) option);
        JASModInteger jas = new JASModInteger(variablesList, modIntegerRing);
        GenPolynomial<ModLong> polyExpr = jas.expr2JAS(polynomial);
        IAST list = F.List();
        for (Monomial<ModLong> monomial : polyExpr) {
            ModLong coeff = monomial.coefficient();
            ExpVector exp = monomial.exponent();
            IAST monomTimes = F.Times();
            jas.monomialToExpr(F.integer(coeff.getVal()), exp, monomTimes);
            list.append(monomTimes);
        }
        return list;
    } catch (ArithmeticException ae) {
        // toInt() conversion failed
        if (Config.DEBUG) {
            ae.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ModLong(edu.jas.arith.ModLong) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) ModLongRing(edu.jas.arith.ModLongRing) JASModInteger(org.matheclipse.core.convert.JASModInteger)

Example 2 with ModLong

use of edu.jas.arith.ModLong in project symja_android_library by axkr.

the class JASModInteger method expr2Poly.

/**
	 * Convert the given expression into a
	 * <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial
	 * 
	 * @param exprPoly
	 * @param numeric2Rational
	 *            if <code>true</code>, <code>INum</code> double values are
	 *            converted to <code>BigRational</code> internally
	 * 
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
private GenPolynomial<ModLong> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<ModLong> result = fPolyFactory.getZERO();
        GenPolynomial<ModLong> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2Poly(expr, numeric2Rational);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2Poly(expr, numeric2Rational);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2Poly(expr, numeric2Rational);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2Poly(expr, numeric2Rational);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            for (int i = 0; i < fVariables.size(); i++) {
                if (fVariables.get(i).equals(expr)) {
                    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(), i, exponent);
                    return fPolyFactory.valueOf(e);
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        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);
            }
        }
    // class cast exception
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : ModLong(edu.jas.arith.ModLong) 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)

Example 3 with ModLong

use of edu.jas.arith.ModLong in project symja_android_library by axkr.

the class JASModInteger method expr2IExprPoly.

private GenPolynomial<ModLong> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<ModLong> result = fPolyFactory.getZERO();
        GenPolynomial<ModLong> 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();
            for (int i = 0; i < fVariables.size(); i++) {
                if (fVariables.get(i).equals(expr)) {
                    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(), i, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        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);
            }
        }
        return new GenPolynomial(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    } else if (exprPoly instanceof IFraction) {
        return fraction2Poly((IFraction) exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial(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) ModLong(edu.jas.arith.ModLong) 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)

Example 4 with ModLong

use of edu.jas.arith.ModLong in project symja_android_library by axkr.

the class JASModInteger method modLongPoly2Expr.

public IExpr modLongPoly2Expr(final GenPolynomial<ModLong> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.Plus(F.C0);
    }
    IAST result = F.Plus();
    for (Monomial<ModLong> monomial : poly) {
        ModLong coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IInteger coeffValue = F.integer(coeff.getVal());
        IAST monomTimes = F.Times();
        monomialToExpr(coeffValue, exp, monomTimes);
        result.append(monomTimes.getOneIdentity(F.C1));
    }
    return result.getOneIdentity(F.C0);
}
Also used : ModLong(edu.jas.arith.ModLong) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with ModLong

use of edu.jas.arith.ModLong in project symja_android_library by axkr.

the class Algebra method factorModulus.

private static IAST factorModulus(IExpr expr, List<IExpr> varList, boolean factorSquareFree, IExpr option) throws JASConversionException {
    try {
        // found "Modulus" option => use ModIntegerRing
        ModLongRing modIntegerRing = JASModInteger.option2ModLongRing((ISignedNumber) option);
        JASModInteger jas = new JASModInteger(varList, modIntegerRing);
        GenPolynomial<ModLong> poly = jas.expr2JAS(expr);
        return factorModulus(jas, modIntegerRing, poly, factorSquareFree);
    } catch (ArithmeticException ae) {
        // toInt() conversion failed
        if (Config.DEBUG) {
            ae.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ModLong(edu.jas.arith.ModLong) ModLongRing(edu.jas.arith.ModLongRing) JASModInteger(org.matheclipse.core.convert.JASModInteger)

Aggregations

ModLong (edu.jas.arith.ModLong)7 IAST (org.matheclipse.core.interfaces.IAST)6 ExpVector (edu.jas.poly.ExpVector)5 ModLongRing (edu.jas.arith.ModLongRing)3 JASModInteger (org.matheclipse.core.convert.JASModInteger)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 GenPolynomial (edu.jas.poly.GenPolynomial)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 SortedMap (java.util.SortedMap)1 IFraction (org.matheclipse.core.interfaces.IFraction)1