Search in sources :

Example 21 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Solve method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IAST variables = Validate.checkIsVariableOrVariableList(ast, 2, engine);
    if (variables == null) {
        return F.NIL;
    }
    IExpr domain = F.Complexes;
    if (ast.isAST3()) {
        domain = ast.arg3();
        if (domain.equals(F.Booleans)) {
            IAST resultList = F.List();
            booleansSolve(ast.arg1(), variables, 0, 1, resultList);
            return resultList;
        }
        if (domain.equals(F.Integers)) {
            IAST equationsAndInequations = Validate.checkEquationsAndInequations(ast, 1);
            try {
                IAST resultList = Solve.integerSolve(equationsAndInequations, variables);
                EvalAttributes.sort(resultList);
                return resultList;
            } catch (RuntimeException rex) {
                if (Config.SHOW_STACKTRACE) {
                    rex.printStackTrace();
                }
                engine.printMessage("Integer solution not found: " + rex.getMessage());
            }
            return F.NIL;
        }
        if (!domain.equals(F.Reals) && !domain.equals(F.Complexes)) {
            throw new WrongArgumentType(ast, ast.arg3(), 3, "Booleans or Integers expected!");
        }
    }
    IAST termsEqualZeroList = Validate.checkEquations(ast, 1);
    IAST temp = solveTimesEquationsRecursively(termsEqualZeroList, variables, engine);
    if (temp.isPresent()) {
        return temp;
    }
    temp = solveEquations(termsEqualZeroList, variables, 0, engine);
    if (temp.isPresent()) {
        // }
        return temp;
    }
    return F.NIL;
// return solvePlusEquationsSimplified(termsEqualZeroList,
// variables,engine);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 22 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Symbol method reassignSymbolValue.

/**
	 * {@inheritDoc}
	 */
@Override
public IExpr[] reassignSymbolValue(IAST ast, ISymbol functionSymbol, EvalEngine engine) {
    IExpr[] result = new IExpr[2];
    IExpr symbolValue;
    if (hasLocalVariableStack()) {
        symbolValue = get();
        result[0] = symbolValue;
        // IExpr calculatedResult = function.apply(symbolValue);
        ast.set(1, symbolValue);
        // F.binaryAST2(this, symbolValue, value));
        IExpr calculatedResult = engine.evaluate(ast);
        if (calculatedResult != null) {
            set(calculatedResult);
            result[1] = calculatedResult;
            return result;
        }
    } else {
        if (fRulesData != null) {
            PatternMatcherEquals pme = fRulesData.getEqualDownRules().get(this);
            if (pme != null) {
                symbolValue = pme.getRHS();
                if (symbolValue != null) {
                    result[0] = symbolValue;
                    // IExpr calculatedResult = function.apply(symbolValue);
                    ast.set(1, symbolValue);
                    IExpr calculatedResult = engine.evaluate(ast);
                    if (calculatedResult != null) {
                        pme.setRHS(calculatedResult);
                        result[1] = calculatedResult;
                        return result;
                    }
                }
            }
        }
    }
    throw new WrongArgumentType(this, functionSymbol.toString() + " - Symbol: " + toString() + " has no value! Reassignment with a new value is not possible");
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) PatternMatcherEquals(org.matheclipse.core.patternmatching.PatternMatcherEquals) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 23 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class QuarticSolver method convertTerm2Coefficients.

private static boolean convertTerm2Coefficients(final IExpr exprPoly, IExpr x, IExpr[] coefficients) {
    if (exprPoly.isFree(x, true)) {
        coefficients[0] = F.eval(F.Plus(coefficients[0], exprPoly));
        return true;
    }
    if (exprPoly instanceof IAST) {
        IAST ast = (IAST) exprPoly;
        if (ast.isTimes()) {
            int exponent = -1;
            IAST coeff = ast.clone();
            for (int i = 1; i < ast.size(); i++) {
                if (ast.get(i).isPower()) {
                    final IExpr temp = ast.get(i).getAt(1);
                    if (x.equals(temp)) {
                        try {
                            exponent = Validate.checkPowerExponent((IAST) ast.get(i));
                        } catch (WrongArgumentType e) {
                        }
                        if (exponent < 0 || exponent > 4) {
                            return false;
                        }
                        coeff.remove(i);
                        coefficients[exponent] = F.eval(F.Plus(coefficients[exponent], coeff));
                        return true;
                    }
                } else if (x.equals(ast.get(i))) {
                    coeff.remove(i);
                    coefficients[1] = F.eval(F.Plus(coefficients[1], coeff));
                    return true;
                }
            }
            return true;
        } else if (ast.isPower()) {
            final IExpr temp = ast.arg1();
            if (x.equals(temp)) {
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent((IAST) ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0 || exponent > 4) {
                    return false;
                }
                coefficients[exponent] = F.eval(F.Plus(coefficients[exponent], F.C1));
                return true;
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        if (x.equals(exprPoly)) {
            coefficients[1] = F.eval(F.Plus(coefficients[1], F.C1));
            return true;
        }
    }
    return false;
}
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)

Example 24 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType 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 25 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType 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)

Aggregations

WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)37 IAST (org.matheclipse.core.interfaces.IAST)30 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)7 IInteger (org.matheclipse.core.interfaces.IInteger)6 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)5 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExpVector (edu.jas.poly.ExpVector)3 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 IFraction (org.matheclipse.core.interfaces.IFraction)3 ModLong (edu.jas.arith.ModLong)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 ArrayList (java.util.ArrayList)2 Apint (org.apfloat.Apint)2 PrettyPrint (edu.jas.kern.PrettyPrint)1 Function (java.util.function.Function)1 IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)1 Apfloat (org.apfloat.Apfloat)1 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)1