Search in sources :

Example 6 with ExpVector

use of edu.jas.poly.ExpVector in project symja_android_library by axkr.

the class JASConvert method complexPoly2Expr.

/**
	 * Convert a JAS complex polynomial to <code>IExpr</code>.
	 * 
	 * @param poly
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.C0;
    }
    IAST result = F.Plus();
    for (Monomial<Complex<BigRational>> monomial : poly) {
        Complex<BigRational> coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IAST monomTimes = F.Times();
        monomialToExpr(coeff, exp, monomTimes);
        result.append(monomTimes.getOneIdentity(F.C1));
    }
    return result.getOneIdentity(F.C0);
}
Also used : BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(edu.jas.poly.Complex)

Example 7 with ExpVector

use of edu.jas.poly.ExpVector in project symja_android_library by axkr.

the class JASIExpr method exprPoly2Expr.

/**
	 * Converts a <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a>
	 * polynomial to a MathEclipse AST with head <code>Plus</code>
	 * 
	 * @param poly
	 *            a JAS polynomial
	 * @param variable
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
public IExpr exprPoly2Expr(final GenPolynomial<IExpr> poly, IExpr variable) {
    if (poly.length() == 0) {
        return F.C0;
    }
    IAST result = F.Plus();
    for (Monomial<IExpr> monomial : poly) {
        IExpr coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IAST monomTimes = F.Times();
        monomialToExpr(coeff, exp, monomTimes);
        result.append(monomTimes.getOneIdentity(F.C1));
    }
    return result.getOneIdentity(F.C0);
}
Also used : ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 8 with ExpVector

use of edu.jas.poly.ExpVector 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 9 with ExpVector

use of edu.jas.poly.ExpVector in project symja_android_library by axkr.

the class JASModInteger method monomialToExpr.

public boolean monomialToExpr(IInteger coeff, ExpVector exp, IAST monomTimes) {
    long lExp;
    ExpVector leer = fPolyFactory.evzero;
    if (!coeff.isOne()) {
        monomTimes.append(coeff);
    }
    for (int i = 0; i < exp.length(); i++) {
        lExp = exp.getVal(i);
        if (lExp != 0) {
            int ix = leer.varIndex(i);
            if (ix >= 0) {
                if (lExp == 1L) {
                    monomTimes.append(fVariables.get(ix));
                } else {
                    monomTimes.append(F.Power(fVariables.get(ix), F.integer(lExp)));
                }
            } else {
                return false;
            }
        }
    }
    return true;
}
Also used : ExpVector(edu.jas.poly.ExpVector)

Example 10 with ExpVector

use of edu.jas.poly.ExpVector 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)

Aggregations

ExpVector (edu.jas.poly.ExpVector)22 IAST (org.matheclipse.core.interfaces.IAST)15 BigRational (edu.jas.arith.BigRational)6 IExpr (org.matheclipse.core.interfaces.IExpr)6 ModLong (edu.jas.arith.ModLong)5 IInteger (org.matheclipse.core.interfaces.IInteger)5 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 BigInteger (edu.jas.arith.BigInteger)2 ModLongRing (edu.jas.arith.ModLongRing)2 Complex (edu.jas.poly.Complex)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 Map (java.util.Map)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 JASModInteger (org.matheclipse.core.convert.JASModInteger)2 IFraction (org.matheclipse.core.interfaces.IFraction)2 AlgebraicNumber (edu.jas.poly.AlgebraicNumber)1 BigInteger (java.math.BigInteger)1 SortedMap (java.util.SortedMap)1 IComplex (org.matheclipse.core.interfaces.IComplex)1