use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class FrobeniusSolve method evaluate.
/**
* {@inheritDoc}
*/
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (ast.arg1().isList() && ast.arg2().isInteger()) {
IAST list = ast.getAST(1);
try {
int[] listInt = Validate.checkListOfInts(ast, list, true, false, engine);
if (listInt != null) {
for (int i = 0; i < listInt.length; i++) {
if (listInt[i] < 0 && ast.size() < 4) {
}
}
IInteger[] solution;
IASTAppendable result = F.ListAlloc(8);
FrobeniusSolver solver = getSolver(listInt, (IInteger) ast.arg2());
// all solutions
int numberOfSolutions = -1;
if (ast.size() == 4) {
numberOfSolutions = ast.arg3().toIntDefault(-1);
}
while ((solution = solver.take()) != null) {
if (result.size() >= Config.MAX_AST_SIZE) {
throw new ASTElementLimitExceeded(result.size());
}
result.append(F.List(solution));
}
return result;
}
} catch (LimitException le) {
throw le;
} catch (RuntimeException rex) {
LOGGER.debug("FrobeniusSolve.evaluate() failed", rex);
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class NDSolve method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (!ToggleFeature.DSOLVE) {
return F.NIL;
}
if (ast.arg3().isList()) {
final IAST tRangeList = (IAST) ast.arg3();
if (!(tRangeList.isAST2() || tRangeList.isAST3())) {
return F.NIL;
}
try {
final IAST listOfVariables = Validate.checkIsVariableOrVariableList(ast, 2, ast.topHead(), engine);
if (!listOfVariables.isPresent()) {
return F.NIL;
}
final int numberOfVariables = listOfVariables.argSize();
final ISymbol timeVar = (ISymbol) tRangeList.arg1();
IExpr tMinExpr = F.C0;
IExpr tMaxExpr = tRangeList.arg2();
if (tRangeList.isAST3()) {
tMinExpr = tRangeList.arg2();
tMaxExpr = tRangeList.arg3();
}
final double tMin = tMinExpr.evalDouble();
final double tMax = tMaxExpr.evalDouble();
final double tStep = 0.1;
IASTAppendable listOfEquations = Validate.checkEquations(ast, 1).copyAppendable();
IExpr[][] boundaryCondition = new IExpr[2][numberOfVariables];
int i = 1;
while (i < listOfEquations.size()) {
IExpr equation = listOfEquations.get(i);
if (equation.isFree(timeVar)) {
if (determineSingleBoundary(equation, listOfVariables, timeVar, boundaryCondition, engine)) {
listOfEquations.remove(i);
continue;
}
}
i++;
}
IExpr[] dotEquations = new IExpr[numberOfVariables];
i = 1;
while (i < listOfEquations.size()) {
IExpr equation = listOfEquations.get(i);
if (!equation.isFree(timeVar)) {
if (determineSingleDotEquation(equation, listOfVariables, timeVar, dotEquations, engine)) {
listOfEquations.remove(i);
continue;
}
}
i++;
}
if (listOfVariables.isList()) {
AbstractIntegrator abstractIntegrator = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
// AbstractIntegrator abstractIntegrator = new ClassicalRungeKuttaIntegrator(1.0);
double[] primaryState = new double[numberOfVariables];
for (int j = 0; j < numberOfVariables; j++) {
primaryState[j] = ((INum) engine.evalN(boundaryCondition[1][j])).doubleValue();
}
OrdinaryDifferentialEquation ode = new FirstODE(engine, dotEquations, listOfVariables, timeVar);
if (listOfVariables.size() > 1) {
IASTAppendable[] resultLists = new IASTAppendable[numberOfVariables];
for (int j = 0; j < primaryState.length; j++) {
resultLists[j] = F.ListAlloc();
}
for (double time = tMin; time < tMax; time += tStep) {
final ODEStateAndDerivative finalstate = abstractIntegrator.integrate(ode, new ODEState(time, primaryState), time + tStep);
primaryState = finalstate.getPrimaryState();
for (int j = 0; j < primaryState.length; j++) {
resultLists[j].append(F.list(F.num(time), F.num(primaryState[j])));
}
}
IASTAppendable primaryList = F.ListAlloc();
IASTAppendable secondaryList = F.ListAlloc();
for (int j = 1; j < listOfVariables.size(); j++) {
secondaryList.append(F.Rule(listOfVariables.get(j), engine.evaluate(F.Interpolation(resultLists[j - 1]))));
}
primaryList.append(secondaryList);
return primaryList;
}
}
} catch (LimitException le) {
throw le;
} catch (RuntimeException rex) {
LOGGER.log(engine.getLogLevel(), ast.topHead(), rex);
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class AbstractMatrix1Expr method numericEval.
@Override
public IExpr numericEval(final IAST ast, final EvalEngine engine) {
RealMatrix matrix;
IExpr arg1 = ast.arg1();
int[] dim = checkMatrixDimensions(arg1);
if (dim != null) {
try {
if (engine.isArbitraryMode()) {
FieldMatrix<IExpr> fieldMatrix = Convert.list2Matrix(arg1);
if (fieldMatrix != null) {
Predicate<IExpr> zeroChecker = optionZeroTest(ast, 2, engine);
return matrixEval(fieldMatrix, zeroChecker);
}
return F.NIL;
}
matrix = arg1.toRealMatrix();
if (matrix != null) {
return realMatrixEval(matrix);
} else {
FieldMatrix<IExpr> fieldMatrix = Convert.list2Matrix(arg1);
if (fieldMatrix != null) {
Predicate<IExpr> zeroChecker = optionZeroTest(ast, 2, engine);
return matrixEval(fieldMatrix, zeroChecker);
}
}
} catch (LimitException le) {
throw le;
} catch (final MathRuntimeException mre) {
// org.hipparchus.exception.MathIllegalArgumentException: inconsistent dimensions: 0 != 3
LOGGER.log(engine.getLogLevel(), ast.topHead(), mre);
} catch (final RuntimeException e) {
LOGGER.debug("AbstractMatrix1Expr.numericEval() failed", e);
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or <code>Sum()</code> or <code>
* Product()</code>
*
* @param list a list representing an iterator specification
* @param symbol the variable symbol
* @param engine the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
if (symbol != null && (!symbol.isVariable() || symbol.isProtected())) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(symbol), EvalEngine.get()));
}
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = symbol;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 3:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
variable = symbol;
if (upperLimit.isList()) {
if (variable != null) {
if (!variable.isVariable() || variable.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(variable), EvalEngine.get()));
}
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit);
} else if (lowerLimit.isReal() && upperLimit.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = evalEngine.evalWithoutNumericReset(list.arg3());
variable = symbol;
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
} else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
lowerLimit = null;
upperLimit = null;
step = null;
variable = null;
}
return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
} catch (LimitException le) {
throw le;
} catch (ArgumentTypeException atex) {
throw atex;
} catch (RuntimeException rex) {
throw new ClassCastException();
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or <code>Sum()</code> or <code>
* Product()</code>
*
* @param list a list representing an iterator specification
* @param position the position of the list in the argument sequence, for printing an error if
* list cannot be converted into an iterator form
* @param engine the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, int position, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
// fNumericMode = evalEngine.isNumericMode() ||
// list.isMember(Predicates.isNumeric(), false);
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
int iterationLimit = evalEngine.getIterationLimit();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = null;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
if (iUpperLimit > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
}
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
int iUpperLimit = ((IRational) upperLimit).floor().toInt();
if (iUpperLimit > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
}
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
if (!list.arg1().isVariable()) {
throw new ArgumentTypeException(IOFunctions.getMessage("vloc", F.list(list.arg1()), EvalEngine.get()));
}
break;
case 3:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (upperLimit.isList()) {
return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
}
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = F.C1;
if (list.arg1().isSymbol()) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit);
} else if (lowerLimit.isReal() && upperLimit.isReal()) {
ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
}
break;
case 5:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = evalEngine.evalWithoutNumericReset(list.arg4());
if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
} else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
// Argument `1` at position `2` does not have the correct form for an iterator.
String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
}
return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
} catch (LimitException le) {
throw le;
} catch (ArgumentTypeException atex) {
throw atex;
} catch (RuntimeException rex) {
// Argument `1` at position `2` does not have the correct form for an iterator.
String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
Aggregations