Search in sources :

Example 96 with IAST

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

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

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

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

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

the class Convert method realVector2List.

/**
	 * Convert a RealVector to a IAST list.
	 * 
	 * @param vector
	 * @return <code>F.NIL</code> if no conversion was possible
	 */
public static IAST realVector2List(final RealVector vector) {
    if (vector == null) {
        return F.NIL;
    }
    final int rowSize = vector.getDimension();
    final IAST out = F.ast(F.List);
    for (int i = 0; i < rowSize; i++) {
        out.append(F.num(vector.getEntry(i)));
    }
    out.addEvalFlags(IAST.IS_VECTOR);
    return out;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)413 IExpr (org.matheclipse.core.interfaces.IExpr)248 ISymbol (org.matheclipse.core.interfaces.ISymbol)76 IInteger (org.matheclipse.core.interfaces.IInteger)34 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)30 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)22 ExpVector (edu.jas.poly.ExpVector)15 ArrayList (java.util.ArrayList)14 BigRational (edu.jas.arith.BigRational)13 JASIExpr (org.matheclipse.core.convert.JASIExpr)13 VariablesSet (org.matheclipse.core.convert.VariablesSet)13 INum (org.matheclipse.core.interfaces.INum)13 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)12 GenPolynomial (edu.jas.poly.GenPolynomial)11 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)11 IFraction (org.matheclipse.core.interfaces.IFraction)11 INumber (org.matheclipse.core.interfaces.INumber)11 IComplex (org.matheclipse.core.interfaces.IComplex)10 ModLong (edu.jas.arith.ModLong)9 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)9