use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Programming method assignPart.
public static IExpr assignPart(final IExpr assignedExpr, final IAST part, int partPosition, IAST rhs, int rhsPos, EvalEngine engine) {
if (!assignedExpr.isAST() || partPosition >= part.size()) {
return assignedExpr;
}
IAST assignedAST = (IAST) assignedExpr;
final IExpr arg2 = part.get(partPosition);
int partPositionPlus1 = partPosition + 1;
int[] span = arg2.isSpan(assignedAST.size());
if (span != null) {
int start = span[0];
int last = span[1];
int step = span[2];
IAST result = F.NIL;
if (step < 0 && start >= last) {
int rhsIndx = 1;
for (int i = start; i >= last; i += step) {
IExpr temp = rhs.get(rhsIndx++);
if (!temp.isList()) {
temp = assignPart(assignedAST.get(i), part, partPositionPlus1, temp, engine);
} else {
temp = assignPart(assignedAST.get(i), part, partPositionPlus1, (IAST) temp, 1, engine);
}
if (temp.isPresent()) {
if (!result.isPresent()) {
result = assignedAST.clone();
}
result.set(i, temp);
}
}
} else if (step > 0 && (last != 1 || start <= last)) {
int rhsIndx = 1;
for (int i = start; i <= last; i += step) {
IExpr temp = rhs.get(rhsIndx++);
if (!temp.isList()) {
temp = assignPart(assignedAST.get(i), part, partPositionPlus1, temp, engine);
} else {
temp = assignPart(assignedAST.get(i), part, partPositionPlus1, (IAST) temp, 1, engine);
}
if (temp.isPresent()) {
if (!result.isPresent()) {
result = assignedAST.clone();
}
result.set(i, temp);
}
}
} else {
throw new WrongArgumentType(part, arg2, partPosition, "Wrong argument for Part[] function: " + arg2.toString() + " selects no part expression.");
}
return result;
} else if (arg2.isSignedNumber()) {
final int indx = Validate.checkIntType(part, partPosition, Integer.MIN_VALUE);
IExpr ires = null;
ires = assignPartValue(assignedAST, indx, rhs.getAST(rhsPos++));
if (partPositionPlus1 < part.size()) {
if (ires.isAST()) {
return assignPart((IAST) ires, part, partPositionPlus1, rhs, rhsPos++, engine);
} else {
throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function. Function or list expected.");
}
}
return ires;
} else if (arg2.isList()) {
IExpr temp = null;
final IAST list = (IAST) arg2;
final IAST result = F.ListAlloc(list.size());
for (int i = 1; i < list.size(); i++) {
final IExpr listArg = list.get(i);
if (listArg.isInteger()) {
IExpr ires = null;
final int indx = Validate.checkIntType(list, i, Integer.MIN_VALUE);
ires = assignPartValue(assignedAST, indx, list);
if (ires == null) {
return F.NIL;
}
if (partPositionPlus1 < part.size()) {
if (ires.isAST()) {
temp = assignPart(ires, part, partPositionPlus1, rhs, rhsPos++, engine);
result.append(temp);
} else {
throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function. Function or list expected.");
}
} else {
result.append(ires);
}
}
}
return result;
}
throw new WrongArgumentType(part, assignedAST, partPosition, "Wrong argument for Part[] function: " + arg2.toString() + " selects no part expression.");
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Replace method replaceExpr.
private static IExpr replaceExpr(final IAST ast, IExpr arg1, IExpr rules, final EvalEngine engine) {
if (rules.isListOfLists()) {
IAST result = F.List();
for (IExpr list : (IAST) rules) {
IAST subList = (IAST) list;
IExpr temp = F.NIL;
for (IExpr element : subList) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
Function<IExpr, IExpr> function = Functors.rules(rule);
temp = function.apply(arg1);
if (temp.isPresent()) {
break;
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
if (temp.isPresent()) {
result.append(temp);
} else {
result.append(arg1);
}
}
return result;
} else if (rules.isList()) {
for (IExpr element : (IAST) rules) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
Function<IExpr, IExpr> function = Functors.rules(rule);
IExpr temp = function.apply(arg1);
if (temp.isPresent()) {
return temp;
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
return arg1;
}
if (rules.isRuleAST()) {
return replaceRule(arg1, (IAST) rules);
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
engine.printMessage(wat.getMessage());
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Replace method replaceExprWithLevelSpecification.
private static IExpr replaceExprWithLevelSpecification(final IAST ast, IExpr arg1, IExpr rules, IExpr exprLevelSpecification, EvalEngine engine) {
// use replaceFunction#setRule() method to set the current rules which
// are initialized with null
ReplaceFunction replaceFunction = new ReplaceFunction(ast, null, engine);
VisitorLevelSpecification level = new VisitorLevelSpecification(replaceFunction, exprLevelSpecification, false);
if (rules.isListOfLists()) {
IAST result = F.List();
for (IExpr list : (IAST) rules) {
IExpr temp = F.NIL;
IAST subList = (IAST) list;
for (IExpr element : subList) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
replaceFunction.setRule(rule);
temp = arg1.accept(level);
if (temp.isPresent()) {
break;
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
result.append(temp.orElse(arg1));
}
return result;
}
replaceFunction.setRule(rules);
return arg1.accept(level).orElse(arg1);
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class ReplaceList method replaceExpr.
private static IAST replaceExpr(final IAST ast, IExpr arg1, IExpr rules, IAST result, int maxNumberOfResults, final EvalEngine engine) {
if (rules.isList()) {
for (IExpr element : (IAST) rules) {
if (element.isRuleAST()) {
IAST rule = (IAST) element;
Function<IExpr, IExpr> function = Functors.rules(rule);
IExpr temp = function.apply(arg1);
if (temp.isPresent()) {
if (maxNumberOfResults <= result.size()) {
return result;
}
result.append(temp);
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
throw wat;
}
}
return result;
}
if (rules.isRuleAST()) {
Function<IExpr, IExpr> function = Functors.rules((IAST) rules);
IExpr temp = function.apply(arg1);
if (temp.isPresent()) {
if (maxNumberOfResults <= result.size()) {
return result;
}
result.append(temp);
}
} else {
WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
engine.printMessage(wat.getMessage());
}
return result;
}
use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Resultant method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 4);
// TODO allow multinomials
IExpr arg3 = Validate.checkSymbolType(ast, 3);
ISymbol x = (ISymbol) arg3;
IExpr a = F.evalExpandAll(ast.arg1());
IExpr b = F.evalExpandAll(ast.arg2());
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(x));
try {
// check if a is a polynomial otherwise check ArithmeticException,
// ClassCastException
ring.create(a);
} catch (RuntimeException ex) {
throw new WrongArgumentType(ast, a, 1, "Polynomial expected!");
}
try {
// check if b is a polynomial otherwise check ArithmeticException,
// ClassCastException
ring.create(b);
return F.Together(resultant(a, b, x, engine));
} catch (RuntimeException ex) {
throw new WrongArgumentType(ast, b, 2, "Polynomial expected!");
}
}
Aggregations