use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.
the class ExprEvaluatorTests method generateASTs.
private static void generateASTs(IBuiltInSymbol sym, int start, int end, IAST seedList, ThreadLocalRandom random, int[] counter, IFunctionEvaluator evaluator, EvalEngine engine, boolean nestedChaos, boolean headerExpr) {
boolean quietMode = true;
ExprEvaluator eval;
System.out.flush();
for (int j = start; j <= end; j++) {
eval = new ExprEvaluator(engine, true, (short) 20);
engine.init();
engine.setQuietMode(quietMode);
IASTAppendable ast;
if (headerExpr) {
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
ast = F.ast(F.unaryAST1(sym, seed));
} else {
ast = F.ast(sym);
}
SlowComputationThread thread = null;
try {
for (int k = 0; k < j; k++) {
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
if (nestedChaos && seed.isAST() && seed.size() > 1) {
int seedIndex2 = random.nextInt(1, seed.size());
seedIndex = random.nextInt(1, seedList.size());
IExpr seed2 = seedList.get(seedIndex);
if (seed != seed2) {
if (seed.isAssociation()) {
seed = ((IAssociation) seed).setAtCopy(seedIndex2, F.Rule(F.ZZ(seedIndex2), seed2));
} else {
seed = ((IAST) seed).setAtCopy(seedIndex2, seed2);
}
}
}
ast.append(seed);
}
if (counter[0]++ > 80) {
// System.out.println("");
counter[0] = 0;
System.out.flush();
System.err.flush();
}
// System.out.println(">> " + ast.toString());
// System.out.print(".");
thread = new SlowComputationThread(">> " + ast.toString(), engine);
thread.start();
// engine.evaluate(ast);
if (evaluator != null) {
evaluator.evaluate(ast, engine);
} else {
eval.eval(ast);
}
} catch (FlowControlException mex) {
if (!quietMode) {
System.err.println(ast.toString());
mex.printStackTrace();
System.err.println();
}
} catch (SyntaxError se) {
System.err.println(ast.toString());
se.printStackTrace();
System.err.println();
// fail();
} catch (ValidateException ve) {
System.err.println(ve.getMessage());
System.err.println(ast.toString());
System.err.println();
// fail();
} catch (MathException mex) {
System.err.println(ast.toString());
mex.printStackTrace();
System.err.println();
fail();
} catch (RuntimeException rex) {
System.err.println(ast.toString());
rex.printStackTrace();
fail();
} catch (Error rex) {
System.err.println(ast.toString());
if (rex instanceof StackOverflowError) {
System.err.println("java.lang.StackOverflowError");
rex.printStackTrace();
fail();
} else {
System.err.println(ast.toString());
rex.printStackTrace();
fail();
}
} finally {
if (thread != null) {
thread.terminate();
thread.interrupt();
}
}
}
}
use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.
the class Convert method list2ComplexMatrix.
/**
* Returns a <code>FieldMatrix<Complex></code> if possible.
*
* @param expr
* @return <code>null</code> if the conversion isn't possible.
* @throws ClassCastException
* @throws IndexOutOfBoundsException
*/
public static FieldMatrix<Complex> list2ComplexMatrix(IExpr expr) throws ClassCastException, IndexOutOfBoundsException {
if (expr == null) {
return null;
}
int[] dim = expr.isMatrix();
if (dim == null || dim[0] == 0 || dim[1] == 0) {
return null;
}
if (expr.isSparseArray()) {
// TODO optimize for sparse arrays
// ISparseArray array = (ISparseArray) expr;
expr = ((ISparseArray) expr).normal(false);
}
if (expr.isList()) {
try {
IAST list = (IAST) expr;
IAST currInRow = (IAST) list.arg1();
if (currInRow.isAST0()) {
// special case 0-Matrix
Complex[][] array = new Complex[0][0];
return new Array2DRowFieldMatrix<Complex>(array, false);
}
final int rowSize = expr.argSize();
final int colSize = currInRow.argSize();
final Complex[][] elements = new Complex[rowSize][colSize];
for (int i = 1; i < rowSize + 1; i++) {
currInRow = (IAST) list.get(i);
if (currInRow.isVector() < 0 || colSize != currInRow.argSize()) {
return null;
}
for (int j = 1; j < colSize + 1; j++) {
elements[i - 1][j - 1] = currInRow.get(j).evalComplex();
}
}
return new Array2DRowFieldMatrix<Complex>(elements, false);
} catch (ValidateException vex) {
// pass
}
}
return null;
}
use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.
the class Convert method list2ComplexVector.
public static FieldVector<Complex> list2ComplexVector(IExpr expr) throws ClassCastException {
if (expr == null) {
return null;
}
int dim = expr.isVector();
if (dim <= 0) {
return null;
}
if (expr.isSparseArray()) {
// ISparseArray array = (ISparseArray) expr;
// return array.toFieldVector(false);
expr = ((ISparseArray) expr).normal(false);
}
if (expr.isList()) {
try {
final int rowSize = expr.argSize();
IAST list = (IAST) expr;
final Complex[] elements = new Complex[rowSize];
for (int i = 0; i < rowSize; i++) {
elements[i] = list.get(i + 1).evalComplex();
}
return new ArrayFieldVector<Complex>(elements, false);
} catch (ValidateException vex) {
// pass
}
}
return null;
}
use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.
the class AbstractAST method evaluate.
/**
* {@inheritDoc}
*/
@Override
public IExpr evaluate(EvalEngine engine) {
LOGGER.debug("Evaluate {}", this);
final IExpr head = head();
final int argSize = argSize();
if (head instanceof ISymbol) {
ISymbol headSymbol = (ISymbol) head;
Class<?> clazz = headSymbol.getContext().getJavaClass();
if (clazz != null) {
String staticMethodName = headSymbol.getSymbolName();
// try {
// Method method = clazz.getMethod(staticMethodName);
// if (Modifier.isStatic(method.getModifiers())) {
// Parameter[] parameters = method.getParameters();
// if (parameters.length == argSize()) {
// Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
// if (params != null) {
// Object result;
// try {
// result = method.invoke(null, params);
// if (result instanceof String) {
// return F.stringx((String) result);
// }
// return Object2Expr.convert(result);
// } catch (IllegalAccessException
// | IllegalArgumentException
// | InvocationTargetException e) {
// // fall through?
// }
// }
// }
// }
//
// } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) {
// // fall through?
// }
Method[] methods = clazz.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
if (staticMethodName.equals(methods[i].getName())) {
Parameter[] parameters = methods[i].getParameters();
if (parameters.length == argSize()) {
Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
if (params != null) {
Object result;
try {
result = methods[i].invoke(null, params);
if (result instanceof String) {
return F.stringx((String) result);
}
return Object2Expr.convert(result, false, true);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// fall through?
}
}
}
}
}
}
}
if (head instanceof IBuiltInSymbol) {
final IEvaluator evaluator = ((IBuiltInSymbol) head).getEvaluator();
if (evaluator instanceof ICoreFunctionEvaluator) {
try {
ICoreFunctionEvaluator functionEvaluator = (ICoreFunctionEvaluator) evaluator;
EvalEngine.OptionsResult opres = engine.checkBuiltinArguments(this, functionEvaluator);
if (opres == null) {
return F.NIL;
}
IAST ast = opres.result;
IBuiltInSymbol header = ((IBuiltInSymbol) head);
if ((header.getAttributes() & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
IExpr temp;
if ((temp = F.flattenSequence(this)).isPresent()) {
return temp;
}
}
if (isBooleanFunction()) {
IExpr temp = extractConditionalExpression(false);
if (temp.isPresent()) {
return temp;
}
}
IExpr evaluateTemp = evalEvaluate(engine);
if (evaluateTemp.isPresent()) {
return evaluateTemp;
}
return functionEvaluator.evaluate(ast, engine);
} catch (ValidateException ve) {
return IOFunctions.printMessage(topHead(), ve, engine);
} catch (FlowControlException e) {
throw e;
} catch (SymjaMathException ve) {
LOGGER.log(engine.getLogLevel(), topHead(), ve);
return F.NIL;
}
}
}
}
if (head.isAssociation() && argSize == 1) {
return ((IAssociation) head).getValue(arg1());
}
final ISymbol symbol = topHead();
IExpr temp = engine.evalAttributes(symbol, this);
if (temp.isPresent()) {
return temp;
}
return engine.evalRules(symbol, this);
}
use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.
the class Product method evaluateProduct.
private IExpr evaluateProduct(final IAST preevaledProduct, IExpr arg1, EvalEngine engine) {
if (preevaledProduct.size() > 2) {
IAST list;
if (preevaledProduct.last().isList()) {
list = (IAST) preevaledProduct.last();
} else {
list = F.list(preevaledProduct.last());
}
if (list.isAST1()) {
// indefinite product case
IExpr variable = list.arg1();
if (preevaledProduct.arg1().isFree(variable) && variable.isVariable()) {
return indefiniteProduct(preevaledProduct, variable);
}
}
}
IExpr temp = F.NIL;
if (preevaledProduct.size() == 3) {
IExpr result = matcher1().apply(preevaledProduct);
if (result.isPresent()) {
return result;
}
}
try {
temp = evaluateTableThrow(preevaledProduct, Times(), Times(), engine);
if (temp.isPresent()) {
return temp;
}
} catch (final ValidateException ve) {
return IOFunctions.printMessage(S.Product, ve, engine);
}
if (arg1.isPower()) {
IExpr exponent = arg1.exponent();
boolean flag = true;
// Prod( i^a, {i,from,to},... )
for (int i = 2; i < preevaledProduct.size(); i++) {
IIterator<IExpr> iterator;
if (preevaledProduct.get(i).isList()) {
iterator = Iterator.create((IAST) preevaledProduct.get(i), i, engine);
} else {
iterator = Iterator.create(F.list(preevaledProduct.get(i)), i, engine);
}
if (iterator.isValidVariable() && exponent.isFree(iterator.getVariable())) {
continue;
}
flag = false;
break;
}
if (flag) {
IASTMutable prod = preevaledProduct.copy();
prod.set(1, arg1.base());
return F.Power(prod, exponent);
}
}
IExpr argN = preevaledProduct.last();
if (preevaledProduct.size() >= 3 && argN.isList()) {
try {
if (arg1.isZero()) {
// Product(0, {k, n, m})
return F.C0;
}
IIterator<IExpr> iterator = Iterator.create((IAST) argN, preevaledProduct.argSize(), engine);
if (iterator.isValidVariable() && iterator.getUpperLimit().isInfinity()) {
if (arg1.isOne()) {
// Product(1, {k, a, Infinity})
return F.C1;
}
if (arg1.isPositiveResult() && arg1.isIntegerResult()) {
// Product(n, {k, a, Infinity}) ;n is positive integer
return F.CInfinity;
}
}
if (iterator.isValidVariable() && !iterator.isNumericFunction()) {
if (iterator.getUpperLimit().isSymbol() && iterator.getStep().isOne()) {
final ISymbol var = iterator.getVariable();
final IExpr from = iterator.getLowerLimit();
final ISymbol to = (ISymbol) iterator.getUpperLimit();
if (arg1.isPower()) {
IExpr base = arg1.base();
if (base.isFree(var)) {
if (iterator.getLowerLimit().isOne()) {
IExpr exponent = arg1.exponent();
if (exponent.equals(var)) {
// Prod( a^i, ..., {i,from,to} )
if (preevaledProduct.isAST2()) {
return F.Power(base, Times(C1D2, to, Plus(C1, to)));
}
IASTAppendable result = preevaledProduct.removeAtClone(preevaledProduct.argSize());
// result.remove(ast.argSize());
result.set(1, F.Power(base, Times(C1D2, to, Plus(C1, to))));
return result;
}
}
}
}
if (arg1.isFree(var)) {
if (preevaledProduct.isAST2()) {
if (from.isOne()) {
return F.Power(preevaledProduct.arg1(), to);
}
if (from.isZero()) {
return F.Power(preevaledProduct.arg1(), Plus(to, C1));
}
if (from.isSymbol()) {
// 2^(1-from+to)
return F.Power(arg1, F.Plus(F.C1, from.negate(), to));
}
} else {
IASTAppendable result = preevaledProduct.removeAtClone(preevaledProduct.argSize());
// result.remove(ast.argSize());
if (from.isOne()) {
result.set(1, F.Power(preevaledProduct.arg1(), to));
return result;
}
if (from.isZero()) {
result.set(1, F.Power(preevaledProduct.arg1(), Plus(to, C1)));
return result;
}
if (from.isSymbol()) {
// 2^(1-from+to)
result.set(1, F.Power(arg1, F.Plus(F.C1, from.negate(), to)));
return result;
}
}
}
}
}
temp = F.NIL;
IAST resultList = Times();
temp = evaluateLast(preevaledProduct.arg1(), iterator, resultList, F.C1);
if (!temp.isPresent() || temp.equals(resultList)) {
return F.NIL;
}
} catch (final ValidateException ve) {
return IOFunctions.printMessage(S.Product, ve, engine);
} catch (RecursionLimitExceeded rle) {
// Recursion depth of `1` exceeded during evaluation of `2`.
int recursionLimit = engine.getRecursionLimit();
IOFunctions.printMessage(S.Product, "reclim2", F.list(recursionLimit < 0 ? F.CInfinity : F.ZZ(recursionLimit), preevaledProduct), engine);
return F.NIL;
}
if (preevaledProduct.isAST2()) {
return temp;
} else {
IASTAppendable result = preevaledProduct.removeAtClone(preevaledProduct.argSize());
result.set(1, temp);
return result;
}
}
return F.NIL;
}
Aggregations