Search in sources :

Example 31 with ISymbol

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

the class Product 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.isPresent()) {
            arg1 = ast.arg1();
        }
    }
    if (arg1.isTimes()) {
        IAST prod = ast.setAtCopy(1, null);
        return ((IAST) arg1).mapThread(prod, 1);
    }
    IExpr temp = evaluateTableThrow(ast, Times(), Times(), engine);
    if (temp.isPresent()) {
        return temp;
    }
    if (arg1.isPower()) {
        IExpr powArg2 = arg1.getAt(2);
        boolean flag = true;
        // Prod( i^a, {i,from,to},... )
        for (int i = 2; i < ast.size(); i++) {
            IIterator iterator = Iterator.create((IAST) ast.get(i), engine);
            if (iterator.isValidVariable() && powArg2.isFree(iterator.getVariable())) {
                continue;
            }
            flag = false;
            break;
        }
        if (flag) {
            IAST prod = ast.copy();
            prod.set(1, arg1.getAt(1));
            return F.Power(prod, powArg2);
        }
    }
    IExpr argN = ast.get(ast.size() - 1);
    if (ast.size() >= 3 && argN.isList()) {
        IIterator iterator = Iterator.create((IAST) argN, engine);
        if (iterator.isValidVariable()) {
            if (iterator.getLowerLimit().isInteger() && iterator.getUpperLimit().isSymbol() && iterator.getStep().isOne()) {
                final ISymbol var = iterator.getVariable();
                final IInteger from = (IInteger) iterator.getLowerLimit();
                final ISymbol to = (ISymbol) iterator.getUpperLimit();
                if (arg1.isPower()) {
                    IExpr powArg1 = arg1.getAt(1);
                    IExpr powArg2 = arg1.getAt(2);
                    if (powArg1.isFree(var)) {
                        if (iterator.getLowerLimit().isOne()) {
                            if (powArg2.equals(var)) {
                                // Prod( a^i, ..., {i,from,to} )
                                if (ast.isAST2()) {
                                    return F.Power(powArg1, Times(C1D2, to, Plus(C1, to)));
                                }
                                IAST result = ast.clone();
                                result.remove(ast.size() - 1);
                                result.set(1, F.Power(powArg1, Times(C1D2, to, Plus(C1, to))));
                                return result;
                            }
                        }
                    }
                }
                if (arg1.isFree(var)) {
                    if (ast.isAST2()) {
                        if (from.isOne()) {
                            return F.Power(ast.arg1(), to);
                        }
                        if (from.isZero()) {
                            return F.Power(ast.arg1(), Plus(to, C1));
                        }
                    } else {
                        IAST result = ast.clone();
                        result.remove(ast.size() - 1);
                        if (from.isOne()) {
                            result.set(1, F.Power(ast.arg1(), to));
                            return result;
                        }
                        if (from.isZero()) {
                            result.set(1, F.Power(ast.arg1(), Plus(to, C1)));
                            return result;
                        }
                    }
                }
            }
        }
        IAST resultList = Times();
        temp = evaluateLast(ast.arg1(), iterator, resultList, C1);
        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;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IIterator(org.matheclipse.core.generic.interfaces.IIterator)

Example 32 with ISymbol

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

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> 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();
            if (expr instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    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(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(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) 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 33 with ISymbol

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

the class Expr2LP method expr2ObjectiveFunction.

private ISignedNumber expr2ObjectiveFunction(final IExpr expr, double[] coefficients) throws ArithmeticException, ClassCastException {
    if (expr instanceof IAST) {
        final IAST ast = (IAST) expr;
        if (ast.isPlus()) {
            double constantTerm = 0.0;
            for (int i = 1; i < ast.size(); i++) {
                IExpr temp = ast.get(i);
                ISignedNumber num = expr2ObjectiveFunction(temp, coefficients);
                if (num != null) {
                    constantTerm += num.doubleValue();
                }
            }
            return F.num(constantTerm);
        } else if (ast.isTimes()) {
            ISymbol variable = null;
            double value = 1.0;
            for (int i = 1; i < ast.size(); i++) {
                IExpr temp = ast.get(i);
                if (temp.isVariable()) {
                    if (variable != null) {
                        throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
                    }
                    variable = (ISymbol) temp;
                    continue;
                }
                ISignedNumber num = temp.evalSignedNumber();
                if (num != null) {
                    value *= num.doubleValue();
                    continue;
                }
                throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
            }
            if (variable != null) {
                for (int i = 0; i < coefficients.length; i++) {
                    if (variable.equals(fVariables.get(i))) {
                        coefficients[i] += value;
                        return null;
                    }
                }
                throw new WrongArgumentType(ast, "Conversion from expression to linear programming expression failed");
            }
            return F.num(value);
        }
    } else if (expr.isVariable()) {
        ISymbol variable = (ISymbol) expr;
        for (int i = 0; i < coefficients.length; i++) {
            if (variable.equals(fVariables.get(i))) {
                coefficients[i] += 1.0d;
                return null;
            }
        }
        throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
    }
    ISignedNumber num = expr.evalSignedNumber();
    if (num != null) {
        return num;
    }
    throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint)

Example 34 with ISymbol

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

the class JASConvert 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<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        if (ast.isSlot()) {
            try {
                return fPolyFactory.univariate(ast.toString(), 1L);
            } catch (IllegalArgumentException iae) {
            // fall through
            }
        } else {
            GenPolynomial<C> result = fPolyFactory.getZERO();
            GenPolynomial<C> 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() && ast.arg1().isSymbol()) {
                final ISymbol expr = (ISymbol) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.getSymbolName(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            } else if (ast.isPower() && ast.arg1().isSlot()) {
                final IAST expr = (IAST) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.toString(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        try {
            return fPolyFactory.univariate(((ISymbol) exprPoly).getSymbolName(), 1L);
        } catch (IllegalArgumentException iae) {
        // fall through
        }
    } 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);
    } else if (exprPoly instanceof INum && numeric2Rational) {
        IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
        return fraction2Poly(frac);
    } else if (exprPoly instanceof IComplexNum && numeric2Rational) {
        if (F.isZero(((IComplexNum) exprPoly).getImaginaryPart())) {
            // the imaginary part is zero
            IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
            return fraction2Poly(frac);
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 35 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3