Search in sources :

Example 6 with VariablesSet

use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.

the class Algebra method cancelGCD.

/**
	 * Calculate the 3 elements result array
	 * 
	 * <pre>
	 * [ 
	 *   commonFactor, 
	 *   numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)), 
	 *   denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)) 
	 * ]
	 * </pre>
	 * 
	 * for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial</code>.
	 * 
	 * 
	 * @param numeratorPolynomial
	 *            a <code>BigRational</code> polynomial which could be converted to JAS polynomial
	 * @param denominatorPolynomial
	 *            a <code>BigRational</code> polynomial which could be converted to JAS polynomial
	 * @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd equals 1
	 * @throws JASConversionException
	 */
public static IExpr[] cancelGCD(IExpr numeratorPolynomial, IExpr denominatorPolynomial) throws JASConversionException {
    try {
        if (denominatorPolynomial.isInteger() && numeratorPolynomial.isPlus()) {
            IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numeratorPolynomial, (IInteger) denominatorPolynomial);
            if (result != null) {
                return result;
            }
        }
        VariablesSet eVar = new VariablesSet(numeratorPolynomial);
        eVar.addVarList(denominatorPolynomial);
        if (eVar.size() == 0) {
            return null;
        }
        IAST vars = eVar.getVarList();
        ExprPolynomialRing ring = new ExprPolynomialRing(vars);
        ExprPolynomial pol1 = ring.create(numeratorPolynomial);
        ExprPolynomial pol2 = ring.create(denominatorPolynomial);
        ASTRange r = new ASTRange(eVar.getVarList(), 1);
        JASIExpr jas = new JASIExpr(r, true);
        GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
        GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
        GreatestCommonDivisor<IExpr> engine;
        engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
        GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
        IExpr[] result = new IExpr[3];
        if (gcd.isONE()) {
            result[0] = jas.exprPoly2Expr(gcd);
            result[1] = jas.exprPoly2Expr(p1);
            result[2] = jas.exprPoly2Expr(p2);
        } else {
            result[0] = F.C1;
            result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
            result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
        }
        return result;
    } catch (RuntimeException e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ASTRange(org.matheclipse.core.expression.ASTRange) JASIExpr(org.matheclipse.core.convert.JASIExpr) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) VariablesSet(org.matheclipse.core.convert.VariablesSet) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 7 with VariablesSet

use of org.matheclipse.core.convert.VariablesSet 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 8 with VariablesSet

use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.

the class RootIntervals method croots.

/**
	 * Complex numeric roots intervals.
	 * 
	 * @param ast
	 * @return
	 */
public static IAST croots(final IExpr arg, boolean numeric) {
    try {
        VariablesSet eVar = new VariablesSet(arg);
        if (!eVar.isSize(1)) {
            // only possible for univariate polynomials
            return F.NIL;
        }
        IExpr expr = F.evalExpandAll(arg);
        ASTRange r = new ASTRange(eVar.getVarList(), 1);
        List<IExpr> varList = r;
        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1));
        ComplexRootsAbstract<BigRational> cr = new ComplexRootsSturm<BigRational>(cfac);
        JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
        GenPolynomial<Complex<BigRational>> poly = jas.numericExpr2JAS(expr);
        Squarefree<Complex<BigRational>> engine = SquarefreeFactory.<Complex<BigRational>>getImplementation(cfac);
        poly = engine.squarefreePart(poly);
        List<Rectangle<BigRational>> roots = cr.complexRoots(poly);
        BigRational len = new BigRational(1, 100000L);
        IAST resultList = F.List();
        if (numeric) {
            for (Rectangle<BigRational> root : roots) {
                Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
                resultList.append(JASConvert.jas2Numeric(refine.getCenter(), Config.DEFAULT_ROOTS_CHOP_DELTA));
            }
        } else {
            IAST rectangleList;
            for (Rectangle<BigRational> root : roots) {
                rectangleList = F.List();
                Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
                rectangleList.append(JASConvert.jas2Complex(refine.getNW()));
                rectangleList.append(JASConvert.jas2Complex(refine.getSW()));
                rectangleList.append(JASConvert.jas2Complex(refine.getSE()));
                rectangleList.append(JASConvert.jas2Complex(refine.getNE()));
                resultList.append(rectangleList);
            // System.out.println("refine = " + refine);
            }
        }
        return resultList;
    } catch (InvalidBoundaryException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
    } catch (JASConversionException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) InvalidBoundaryException(edu.jas.root.InvalidBoundaryException) BigRational(edu.jas.arith.BigRational) ComplexRootsSturm(edu.jas.root.ComplexRootsSturm) Rectangle(edu.jas.root.Rectangle) VariablesSet(org.matheclipse.core.convert.VariablesSet) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) Complex(edu.jas.poly.Complex) ComplexRing(edu.jas.poly.ComplexRing) JASConvert(org.matheclipse.core.convert.JASConvert) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 9 with VariablesSet

use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.

the class Roots method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    IExpr arg1 = ast.arg1();
    if (arg1.isAST(F.Equal, 3)) {
        IAST eq = (IAST) arg1;
        if (eq.arg2().isZero()) {
            arg1 = eq.arg1();
        } else {
            arg1 = engine.evaluate(F.Subtract(eq.arg1(), eq.arg2()));
        }
    } else {
        throw new WrongArgumentType(ast, ast.arg1(), 1, "Equal() expression expected!");
    }
    VariablesSet eVar = null;
    if (ast.arg2().isList()) {
        eVar = new VariablesSet(ast.arg2());
    } else {
        eVar = new VariablesSet();
        eVar.add(ast.arg2());
    }
    if (!eVar.isSize(1)) {
        // factorization only possible for univariate polynomials
        throw new WrongArgumentType(ast, ast.arg2(), 2, "Only one variable expected");
    }
    IAST variables = eVar.getVarList();
    IExpr variable = variables.arg1();
    IAST list = roots(arg1, false, variables, engine);
    if (list.isPresent()) {
        IAST or = F.Or();
        for (int i = 1; i < list.size(); i++) {
            or.append(F.Equal(variable, list.get(i)));
        }
        return or;
    }
    return F.NIL;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet)

Example 10 with VariablesSet

use of org.matheclipse.core.convert.VariablesSet in project symja_android_library by axkr.

the class Sum method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    IExpr arg1 = ast.arg1();
    if (arg1.isAST()) {
        arg1 = F.expand(arg1, false, false);
    }
    if (arg1.isPlus()) {
        IAST sum = ast.setAtCopy(1, null);
        return ((IAST) arg1).mapThread(sum, 1);
    }
    IExpr temp;
    temp = evaluateTableThrow(ast, Plus(), Plus(), engine);
    if (temp.isPresent()) {
        return temp;
    }
    VariablesSet variablesSet = determineIteratorExprVariables(ast);
    IAST varList = variablesSet.getVarList();
    IExpr argN = ast.get(ast.size() - 1);
    IIterator<IExpr> iterator = null;
    if (argN.isList()) {
        argN = evalBlockWithoutReap(argN, varList);
        iterator = Iterator.create((IAST) argN, engine);
    // if (iterator.isSetIterator() || iterator.isNumericFunction()) {
    // IAST resultList = Plus();
    // temp = evaluateLast(ast.arg1(), iterator, resultList, C0);
    // if (temp.isPresent() && !temp.equals(resultList)) {
    // if (ast.isAST2()) {
    // return temp;
    // } else {
    // IAST result = ast.clone();
    // result.remove(ast.size() - 1);
    // result.set(1, temp);
    // return result;
    // }
    // }
    // }
    }
    // arg1 = evalBlockExpandWithoutReap(ast.arg1(), varList);
    if (arg1.isTimes()) {
        if (variablesSet.size() > 0) {
            temp = collectConstantFactors(ast, (IAST) arg1, variablesSet);
            if (temp.isPresent()) {
                return temp;
            }
        }
    }
    if (iterator != null) {
        if (iterator.isValidVariable() && iterator.isNumericFunction()) {
            IAST resultList = Plus();
            temp = evaluateLast(ast.arg1(), iterator, resultList, C0);
            if (!temp.isPresent() || temp.equals(resultList)) {
                return F.NIL;
            }
            if (ast.isAST2()) {
                return temp;
            } else {
                IAST result = ast.clone();
                result.remove(ast.size() - 1);
                result.set(1, temp);
                return result;
            }
        }
        if (iterator.isValidVariable() && !iterator.isNumericFunction()) {
            if (iterator.getStep().isOne()) {
                if (iterator.getUpperLimit().isDirectedInfinity()) {
                    temp = definiteSumInfinity(arg1, iterator, (IAST) argN, engine);
                } else {
                    temp = definiteSum(arg1, iterator, (IAST) argN, engine);
                }
                if (temp.isPresent()) {
                    if (ast.isAST2()) {
                        return temp;
                    }
                    IAST result = ast.clone();
                    result.remove(ast.size() - 1);
                    result.set(1, temp);
                    return result;
                }
            }
        }
    } else if (argN.isSymbol()) {
        temp = indefiniteSum(arg1, (ISymbol) argN);
        if (temp.isPresent()) {
            if (ast.isAST2()) {
                return temp;
            } else {
                IAST result = ast.clone();
                result.remove(ast.size() - 1);
                result.set(1, temp);
                return result;
            }
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet)

Aggregations

VariablesSet (org.matheclipse.core.convert.VariablesSet)13 IAST (org.matheclipse.core.interfaces.IAST)13 IExpr (org.matheclipse.core.interfaces.IExpr)11 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 JASIExpr (org.matheclipse.core.convert.JASIExpr)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 TermOrder (edu.jas.poly.TermOrder)2 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)2 LinearConstraintSet (org.hipparchus.optim.linear.LinearConstraintSet)2 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)2 NonNegativeConstraint (org.hipparchus.optim.linear.NonNegativeConstraint)2 Options (org.matheclipse.core.eval.util.Options)2 ASTRange (org.matheclipse.core.expression.ASTRange)2 IStringX (org.matheclipse.core.interfaces.IStringX)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 HornerScheme (org.matheclipse.core.polynomials.HornerScheme)2 BigRational (edu.jas.arith.BigRational)1 Complex (edu.jas.poly.Complex)1