Search in sources :

Example 1 with LimitException

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

the class PlusOp method plus.

/**
 * Add an argument <code>arg</code> to this <code>Plus()</code> expression.
 *
 * @param arg
 * @return <code>F.Indeterminate</code> if the result is indeterminated, <code>F.NIL</code>
 *         otherwise.
 */
public IExpr plus(final IExpr arg) {
    // }
    if (arg.isIndeterminate()) {
        return S.Indeterminate;
    }
    try {
        if (numberValue.isPresent() && numberValue.isDirectedInfinity()) {
            if (numberValue.isComplexInfinity()) {
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                numberValue = F.CComplexInfinity;
                evaled = true;
                return F.NIL;
            } else if (numberValue.isInfinity()) {
                if (arg.isInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            } else if (numberValue.isNegativeInfinity()) {
                if (arg.isNegativeInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            }
        }
        if (arg.isNumber()) {
            if (arg.isZero()) {
                evaled = true;
                return F.NIL;
            }
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            if (numberValue.isNumber()) {
                numberValue = numberValue.plus(arg);
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isInfinity()) {
                if (arg.isNegativeInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                numberValue = F.CInfinity;
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isNegativeInfinity()) {
                numberValue = negativeInfinityPlus(arg);
                if (numberValue.isIndeterminate()) {
                    return S.Indeterminate;
                }
                evaled = true;
                return F.NIL;
            }
            return F.NIL;
        } else if (arg.isQuantity()) {
            // if (arg.isQuantity()) {
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            IQuantity q = (IQuantity) arg;
            IExpr temp = q.plus(numberValue, true);
            if (temp.isPresent()) {
                evaled = true;
                numberValue = temp;
            } else {
                if (addMerge(q, F.C1)) {
                    evaled = true;
                }
            }
            return F.NIL;
        // }
        } else if (arg.isAST()) {
            final IAST ast = (IAST) arg;
            final int headID = ((IAST) arg).headID();
            if (headID >= ID.DirectedInfinity) {
                switch(headID) {
                    case ID.DirectedInfinity:
                        if (arg.isDirectedInfinity()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                if (arg.isComplexInfinity()) {
                                    if (plusMap != null && plusMap.size() > 0) {
                                        evaled = true;
                                    }
                                } else {
                                    if (plusMap != null) {
                                        Iterator<Entry<IExpr, IExpr>> iterator = plusMap.entrySet().iterator();
                                        while (iterator.hasNext()) {
                                            Entry<IExpr, IExpr> entry = iterator.next();
                                            if (entry.getKey().isRealResult()) {
                                                iterator.remove();
                                                evaled = true;
                                            }
                                        }
                                    }
                                }
                                return F.NIL;
                            }
                            if (arg.isInfinity()) {
                                if (numberValue.isNegativeInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CInfinity;
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isNegativeInfinity()) {
                                numberValue = negativeInfinityPlus(numberValue);
                                if (numberValue.isIndeterminate()) {
                                    return S.Indeterminate;
                                }
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isComplexInfinity()) {
                                if (numberValue.isDirectedInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CComplexInfinity;
                                evaled = true;
                                return F.NIL;
                            }
                        }
                        break;
                    case ID.Times:
                        if (ast.size() > 1) {
                            if (ast.arg1().isNumber()) {
                                if (addMerge(ast.rest().oneIdentity1(), ast.arg1())) {
                                    evaled = true;
                                }
                                return F.NIL;
                            }
                            if (addMerge(ast, F.C1)) {
                                evaled = true;
                            }
                        }
                        return F.NIL;
                    case ID.Interval:
                        if (arg.isInterval()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            IExpr temp;
                            if (numberValue.isInterval()) {
                                temp = IntervalSym.plus((IAST) numberValue, (IAST) arg);
                            } else {
                                temp = IntervalSym.plus(numberValue, (IAST) arg);
                            }
                            if (temp.isPresent()) {
                                numberValue = temp;
                                evaled = true;
                            } else {
                                if (addMerge(arg, F.C1)) {
                                    evaled = true;
                                }
                            }
                            return F.NIL;
                        }
                        break;
                    // break;
                    case ID.SeriesData:
                        if (arg instanceof ASTSeriesData) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            numberValue = ((ASTSeriesData) arg).plus(numberValue);
                            evaled = true;
                            return F.NIL;
                        }
                        break;
                }
            }
        // } else if (!numberValue.isPresent() && arg.isRealResult()) {
        // numberValue = arg;
        // return F.NIL;
        }
        if (addMerge(arg, F.C1)) {
            evaled = true;
        }
    } catch (ValidateException | LimitException e) {
        LOGGER.debug("PlusOp.plus() failed", e);
        throw e;
    } catch (SymjaMathException sme) {
        LOGGER.debug("PlusOp.plus() failed", sme);
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IQuantity(org.matheclipse.core.tensor.qty.IQuantity) Entry(java.util.Map.Entry) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 2 with LimitException

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

the class AbstractMatrix1Matrix method numericEval.

@Override
public IExpr numericEval(final IAST ast, final EvalEngine engine) {
    RealMatrix matrix;
    boolean togetherMode = engine.isTogetherMode();
    try {
        engine.setTogetherMode(true);
        int[] dims = checkMatrixDimensions(ast.arg1());
        if (dims != null) {
            if (engine.isArbitraryMode()) {
                FieldMatrix<IExpr> fieldMatrix = Convert.list2Matrix(ast.arg1());
                if (fieldMatrix == null) {
                    return F.NIL;
                }
                Predicate<IExpr> zeroChecker = AbstractMatrix1Expr.optionZeroTest(ast, 2, engine);
                fieldMatrix = matrixEval(fieldMatrix, zeroChecker);
                return Convert.matrix2List(fieldMatrix);
            }
            matrix = ast.arg1().toRealMatrix();
            if (matrix != null) {
                matrix = realMatrixEval(matrix);
                if (matrix != null) {
                    return Convert.realMatrix2List(matrix);
                }
            }
        }
        return F.NIL;
    } catch (LimitException le) {
        throw le;
    } catch (final RuntimeException rex) {
        LOGGER.log(engine.getLogLevel(), ast.topHead(), rex);
        return F.NIL;
    } finally {
        engine.setTogetherMode(togetherMode);
    }
// return evaluate(ast, engine);
}
Also used : MathRuntimeException(org.hipparchus.exception.MathRuntimeException) RealMatrix(org.hipparchus.linear.RealMatrix) IExpr(org.matheclipse.core.interfaces.IExpr) LimitException(org.matheclipse.core.eval.exception.LimitException)

Example 3 with LimitException

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

the class AbstractArg12 method binaryOperator.

public IExpr binaryOperator(IAST ast, final IExpr o0, final IExpr o1) {
    IExpr result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    if (o0.isInexactNumber() && o1.isInexactNumber()) {
        try {
            EvalEngine engine = EvalEngine.get();
            INumber arg1 = ((INumber) o0).evaluatePrecision(engine);
            INumber arg2 = ((INumber) o1).evaluatePrecision(engine);
            if (arg1 instanceof ApcomplexNum) {
                if (arg2.isNumber()) {
                    result = e2ApcomplexArg((ApcomplexNum) arg1, arg2.apcomplexNumValue());
                }
            } else if (arg2 instanceof ApcomplexNum) {
                if (arg1.isNumber()) {
                    result = e2ApcomplexArg(arg1.apcomplexNumValue(), (ApcomplexNum) arg2);
                }
            } else if (arg1 instanceof ComplexNum) {
                if (arg2.isNumber()) {
                    result = e2DblComArg((ComplexNum) arg1, arg2.complexNumValue());
                }
            } else if (arg2 instanceof ComplexNum) {
                if (arg1.isNumber()) {
                    result = e2DblComArg(arg1.complexNumValue(), (ComplexNum) arg2);
                }
            }
            if (arg1 instanceof ApfloatNum) {
                if (arg2.isReal()) {
                    result = e2ApfloatArg((ApfloatNum) arg1, ((ISignedNumber) arg2).apfloatNumValue());
                }
            } else if (arg2 instanceof ApfloatNum) {
                if (arg1.isReal()) {
                    result = e2ApfloatArg(((ISignedNumber) arg1).apfloatNumValue(), (ApfloatNum) arg2);
                }
            } else if (arg1 instanceof Num) {
                if (arg2.isReal()) {
                    result = e2DblArg((Num) arg1, ((ISignedNumber) arg2).numValue());
                }
            } else if (arg2 instanceof Num) {
                if (arg1.isReal()) {
                    result = e2DblArg(((ISignedNumber) arg1).numValue(), (Num) arg2);
                }
            }
            if (result.isPresent()) {
                return result;
            }
        } catch (LimitException le) {
            throw le;
        } catch (RuntimeException rex) {
            LOGGER.debug("AbstractArg12.binaryOperator() failed", rex);
            return F.NIL;
        }
    }
    if (o0 instanceof IInteger) {
        if (o1 instanceof IInteger) {
            return e2IntArg((IInteger) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IFraction) {
        if (o1 instanceof IInteger) {
            return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
        }
        if (o1 instanceof IFraction) {
            return e2FraArg((IFraction) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IComplex) {
        if (o1 instanceof IInteger) {
            return eComIntArg((IComplex) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return eComFraArg((IComplex) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg((IComplex) o0, (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IAST) {
        if (o1 instanceof IInteger) {
            return eFunIntArg((IAST) o0, (IInteger) o1);
        }
        if (o1 instanceof IAST) {
            return e2FunArg((IAST) o0, (IAST) o1);
        }
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) ComplexNum(org.matheclipse.core.expression.ComplexNum) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) INum(org.matheclipse.core.interfaces.INum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) IComplex(org.matheclipse.core.interfaces.IComplex) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) LimitException(org.matheclipse.core.eval.exception.LimitException) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum)

Example 4 with LimitException

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

the class AbstractArg1 method evaluate.

@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
    final IExpr arg1 = ast.arg1();
    final IExpr result = e1ObjArg(arg1);
    if (result.isPresent()) {
        return result;
    }
    // argument dispatching
    if (arg1 instanceof IAST) {
        return e1FunArg((IAST) arg1);
    }
    final int hier = ast.arg1().hierarchy();
    if (hier <= IExpr.INTEGERID) {
        if (hier <= IExpr.DOUBLECOMPLEXID) {
            try {
                if (hier == IExpr.DOUBLEID) {
                    if (arg1 instanceof ApfloatNum) {
                        return e1ApfloatArg(((ApfloatNum) arg1).apfloatValue());
                    }
                    return e1DblArg(((Num) arg1).doubleValue());
                }
                if (arg1 instanceof ApcomplexNum) {
                    return e1ApcomplexArg(((ApcomplexNum) arg1).apcomplexValue());
                }
                return e1ComplexArg(((ComplexNum) arg1).complexValue());
            } catch (LimitException le) {
                throw le;
            } catch (RuntimeException rex) {
                // EvalEngine.get().printMessage(ast.topHead().toString() + ": " + rex.getMessage());
                return F.NIL;
            }
        } else {
            return e1IntArg((IInteger) arg1);
        }
    } else {
        if (hier <= IExpr.COMPLEXID) {
            if (hier == IExpr.FRACTIONID) {
                return e1FraArg((IFraction) ast.arg1());
            }
            return e1ComArg((IComplex) ast.arg1());
        } else {
            if (hier == IExpr.SYMBOLID) {
                return e1SymArg((ISymbol) ast.arg1());
            }
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum)

Example 5 with LimitException

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

the class Solve method solveIntegers.

public static IExpr solveIntegers(final IAST ast, IAST equationVariables, IAST userDefinedVariables, int maximumNumberOfResults, EvalEngine engine) {
    if (!userDefinedVariables.isEmpty()) {
        IAST equationsAndInequations = Validate.checkEquationsAndInequations(ast, 1);
        if (equationsAndInequations.isEmpty()) {
            return F.NIL;
        }
        try {
            // if (ToggleFeature.CHOCO_SOLVER) {
            // // try calling choco solver
            // if (equationsAndInequations.isFreeAST(x -> x.equals(S.Power))) {
            // try {
            // IAST resultList =
            // ChocoConvert.integerSolve(
            // equationsAndInequations,
            // equationVariables,
            // userDefinedVariables,
            // engine);
            // if (resultList.isPresent()) {
            // EvalAttributes.sort((IASTMutable) resultList);
            // return resultList;
            // }
            // } catch (RuntimeException rex) {
            // // try 2nd solver
            // }
            // }
            // }
            // call cream solver
            CreamConvert converter = new CreamConvert();
            IAST resultList = converter.integerSolve(equationsAndInequations, equationVariables, userDefinedVariables, maximumNumberOfResults, engine);
            if (resultList.isPresent()) {
                EvalAttributes.sort((IASTMutable) resultList);
                return resultList;
            }
        } catch (LimitException le) {
            LOGGER.debug("Solve.of() failed", le);
            throw le;
        } catch (RuntimeException rex) {
            LOGGER.log(engine.getLogLevel(), "Integers solution not found", rex);
            return F.NIL;
        }
    }
    return F.NIL;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) CreamConvert(org.matheclipse.core.convert.CreamConvert)

Aggregations

LimitException (org.matheclipse.core.eval.exception.LimitException)13 IExpr (org.matheclipse.core.interfaces.IExpr)10 IAST (org.matheclipse.core.interfaces.IAST)7 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 IInteger (org.matheclipse.core.interfaces.IInteger)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 Num (org.matheclipse.core.expression.Num)3 INum (org.matheclipse.core.interfaces.INum)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 RealMatrix (org.hipparchus.linear.RealMatrix)2 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)2 ValidateException (org.matheclipse.core.eval.exception.ValidateException)2 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)2 IRational (org.matheclipse.core.interfaces.IRational)2 Entry (java.util.Map.Entry)1 TreeSet (java.util.TreeSet)1