Search in sources :

Example 71 with ISymbol

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

the class HornerScheme method collectTermN.

private void collectTermN(ISymbol sym, IExpr expr) {
    if (expr instanceof IAST) {
        IAST term = (IAST) expr;
        if (term.isASTSizeGE(F.Times, 2)) {
            for (int i = 1; i < term.size(); i++) {
                if (sym.equals(term.get(i))) {
                    IAST temp = F.ast(term, F.Times, false, i, i + 1);
                    addToMap(F.CD1, temp);
                    return;
                } else if (term.get(i).isAST(F.Power, 3)) {
                    IAST pow = (IAST) term.get(i);
                    if (pow.arg1().equals(sym) && pow.arg2().isSignedNumber()) {
                        IAST temp = F.ast(term, F.Times, false, i, i + 1);
                        addToMap((ISignedNumber) pow.arg2(), temp);
                        return;
                    }
                }
            }
        } else if (term.isAST(F.Power, 3)) {
            if (term.arg1().equals(sym) && term.arg2().isSignedNumber()) {
                addToMap((ISignedNumber) term.arg2(), F.CD1);
                return;
            }
        }
    } else if (expr instanceof ISymbol && expr.equals(sym)) {
        addToMap(F.CD1, F.CD1);
        return;
    }
    addToMap(F.CD0, expr);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST)

Example 72 with ISymbol

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

the class GenPolynomialMonomialIterator method create.

/**
	 * Create a <code>Polynomial</code> from the given <code>exprPoly</code>.
	 * 
	 * @param exprPoly
	 *            the polynomial expression
	 * @param coefficient
	 *            set to <code>true</code> if called by the
	 *            <code>Coefficient()</code> function
	 * @param checkNegativeExponents
	 *            if <code>true</code> don't allow negative exponents
	 * @return
	 */
public ExprPolynomial create(final IExpr exprPoly, boolean coefficient, boolean checkNegativeExponents) throws ArithmeticException, ClassCastException {
    int ix = evzero.indexVar(exprPoly, getVars());
    if (ix >= 0) {
        ExpVectorLong e = new ExpVectorLong(vars.size() - 1, ix, 1L);
        return getOne().multiply(e);
    }
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        ExprPolynomial result = getZero();
        ExprPolynomial p = getZero();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = create(expr, coefficient, checkNegativeExponents);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = create(expr, coefficient, checkNegativeExponents);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = create(expr, coefficient, checkNegativeExponents);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = create(expr, coefficient, checkNegativeExponents);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            ix = ExpVectorLong.indexVar(expr, getVars());
            if (ix >= 0) {
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                //
                }
                if (checkNegativeExponents && exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                ExpVectorLong e = new ExpVectorLong(vars.size() - 1, ix, exponent);
                return getOne().multiply(e);
            }
        // for (int i = 1; i < vars.size(); i++) {
        // if (vars.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());
        // }
        // ExpVectorLong e = new ExpVectorLong(vars.size() - 1, i - 1,
        // exponent);
        // return getOne().multiply(e);
        // }
        // }
        }
        if (coefficient) {
            return new ExprPolynomial(this, ast);
        }
        if (numericFunction) {
            if (ast.isNumericFunction()) {
                return new ExprPolynomial(this, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        if (coefficient) {
            return new ExprPolynomial(this, exprPoly);
        }
        if (numericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new ExprPolynomial(this, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new ExprPolynomial(this, exprPoly);
        }
    } else if (exprPoly.isNumber()) {
        return new ExprPolynomial(this, exprPoly);
    }
    if (exprPoly.isFree(Predicates.in(vars), true)) {
        return new ExprPolynomial(this, exprPoly);
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : 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) PrettyPrint(edu.jas.kern.PrettyPrint)

Example 73 with ISymbol

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

the class Solve method booleansSolve.

/**
	 * Solve boolean expressions recursivel.y
	 * 
	 * @param expr
	 * @param variables
	 * @param maximumNumberOfResults
	 * @param position
	 * @param resultList
	 */
protected static void booleansSolve(IExpr expr, IAST variables, int maximumNumberOfResults, int position, IAST resultList) {
    if (maximumNumberOfResults > 0 && maximumNumberOfResults < resultList.size()) {
        return;
    }
    if (variables.size() <= position) {
        if (EvalEngine.get().evalTrue(expr)) {
            IAST list = F.List();
            for (int i = 1; i < variables.size(); i++) {
                ISymbol sym = (ISymbol) variables.get(i);
                list.append(F.Rule(sym, sym.get()));
            }
            resultList.append(list);
        }
        return;
    }
    IExpr sym = variables.get(position);
    if (sym.isSymbol()) {
        try {
            ((ISymbol) sym).pushLocalVariable(F.False);
            booleansSolve(expr, variables, maximumNumberOfResults, position + 1, resultList);
        } finally {
            ((ISymbol) sym).popLocalVariable();
        }
        try {
            ((ISymbol) sym).pushLocalVariable(F.True);
            booleansSolve(expr, variables, maximumNumberOfResults, position + 1, resultList);
        } finally {
            ((ISymbol) sym).popLocalVariable();
        }
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 74 with ISymbol

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

the class Solve method integerVariable.

public static IntVariable integerVariable(Network net, TreeMap<ISymbol, IntVariable> map, IExpr expr) throws ArithmeticException {
    if (expr instanceof ISymbol) {
        return map.get(expr);
    }
    if (expr instanceof IInteger) {
        // throws ArithmeticException
        int value = ((IInteger) expr).toInt();
        return new IntVariable(net, value);
    }
    if (expr.isPlus()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.add(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isTimes()) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.multiply(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isPower()) {
        IAST ast = (IAST) expr;
        if (ast.arg2().isInteger()) {
            int value = ((IInteger) ast.arg2()).toInt();
            if (value > 0) {
                IntVariable result = integerVariable(net, map, ast.arg1());
                for (int i = 1; i < value; i++) {
                    result = result.multiply(integerVariable(net, map, ast.arg1()));
                }
                return result;
            }
        }
    } else if (expr.isASTSizeGE(F.Max, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.max(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isASTSizeGE(F.Min, 3)) {
        IAST ast = (IAST) expr;
        IntVariable result = integerVariable(net, map, ast.arg1());
        for (int i = 2; i < ast.size(); i++) {
            result = result.min(integerVariable(net, map, ast.get(i)));
        }
        return result;
    } else if (expr.isAbs()) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).abs();
    } else if (expr.isAST(F.Sign, 2)) {
        IAST ast = (IAST) expr;
        return integerVariable(net, map, ast.arg1()).sign();
    }
    throw new WrongArgumentType(expr, "for Solve(..., Integers)");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IntVariable(jp.ac.kobe_u.cs.cream.IntVariable)

Example 75 with ISymbol

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

the class Solve method rootsOfUnivariatePolynomial.

/**
	 * Evaluate the roots of a univariate polynomial with the Roots[] function.
	 * 
	 * @param exprAnalyzer
	 * @param fListOfVariables
	 * @return
	 */
private static IAST rootsOfUnivariatePolynomial(ExprAnalyzer exprAnalyzer, EvalEngine engine) {
    IExpr expr = exprAnalyzer.getNumerator();
    IExpr denom = exprAnalyzer.getDenominator();
    // try to solve the expr for a symbol in the symbol set
    for (ISymbol sym : exprAnalyzer.getSymbolSet()) {
        IExpr temp = F.NIL;
        if (expr.isNumericMode() && denom.isOne()) {
            temp = NRoots.roots(expr, F.List(sym), engine);
        }
        if (!temp.isPresent()) {
            temp = Roots.rootsOfVariable(expr, denom, F.List(sym), expr.isNumericMode(), engine);
        }
        if (temp.isPresent()) {
            IAST resultList = F.List();
            if (temp.isASTSizeGE(F.List, 2)) {
                IAST rootsList = (IAST) temp;
                for (IExpr root : rootsList) {
                    IAST rule = F.Rule(sym, root);
                    resultList.append(rule);
                }
                return resultList;
            }
            return F.NIL;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

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