use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class PredicateQ method isPossibeZeroFixedValues.
private static IExpr.COMPARE_TERNARY isPossibeZeroFixedValues(INumber number, IAST function, IAST variables, EvalEngine engine) {
IASTAppendable listOfRules = F.ListAlloc(variables.size());
for (int i = 1; i < variables.size(); i++) {
listOfRules.append(F.Rule(variables.get(i), number));
}
IExpr temp = function.replaceAll(listOfRules);
return isPossibleZeroExact(temp, engine);
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Programming method part.
/**
* Get the <code>Part[...]</code> of an expression. If the expression is no <code>IAST</code>
* return the expression.
*
* @param arg1 the expression from which parts should be extracted
* @param ast the <code>Part[...]</code> expression
* @param pos the index position from which the sub-expressions should be extracted
* @param engine the evaluation engine
* @return
*/
public static IExpr part(final IAST arg1, final IAST ast, int pos, EvalEngine engine) {
final IExpr arg2 = engine.evaluate(ast.get(pos));
int p1 = pos + 1;
int[] span = arg2.isSpan(arg1.size());
if (span != null) {
int start = span[0];
int last = span[1];
int step = span[2];
return spanPart(ast, pos, arg1, arg2, start, last, step, p1, engine);
} else if (arg2.equals(S.All)) {
return spanPart(ast, pos, arg1, arg2, 1, arg1.size() - 1, 1, p1, engine);
} else if (arg2.isReal()) {
final int indx = ast.get(pos).toIntDefault();
if (indx == Integer.MIN_VALUE) {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(ast.get(pos), arg1), engine);
}
IExpr result = getIndex(arg1, indx, engine);
if (result.isPresent()) {
if (p1 < ast.size()) {
if (result.isASTOrAssociation()) {
return part((IAST) result, ast, p1, engine);
} else {
// Part specification `1` is longer than depth of object.
return IOFunctions.printMessage(S.Part, "partd", F.list(result), engine);
}
}
return result;
}
return F.NIL;
} else if (arg1.isAssociation()) {
IAssociation assoc = (IAssociation) arg1;
if (arg2.isList()) {
IExpr temp = null;
final IAST list = (IAST) arg2;
// list.size());
final IAssociation result = F.assoc();
for (int i = 1; i < list.size(); i++) {
final IExpr listArg = list.get(i);
if (listArg.isReal()) {
final int indx = listArg.toIntDefault();
if (indx == Integer.MIN_VALUE) {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(listArg, arg1), engine);
}
IExpr ires = getIndexRule(arg1, indx, engine);
if (ires.isPresent()) {
if (p1 < ast.size()) {
if (ires.isASTOrAssociation()) {
temp = part((IAST) ires, ast, p1, engine);
if (temp.isPresent()) {
try {
result.appendRule(temp);
} catch (IndexOutOfBoundsException ioobex) {
return IOFunctions.printMessage(S.Part, "pkspec1", F.list(temp), engine);
}
} else {
// an error occurred
return F.NIL;
}
} else {
// Part specification `1` is longer than depth of object.
return IOFunctions.printMessage(S.Part, "partd", F.list(ires), engine);
}
} else {
try {
result.appendRule(ires);
} catch (IndexOutOfBoundsException ioobex) {
return IOFunctions.printMessage(S.Part, "pkspec1", F.list(ires), engine);
}
}
} else {
return F.NIL;
}
} else if (listArg.isAST(S.Key, 2)) {
result.appendRule(assoc.getRule(listArg.first()));
} else if (listArg.isString()) {
result.appendRule(assoc.getRule(listArg));
} else if (listArg.isNumber()) {
// The expression `1` cannot be used as a part specification.
return IOFunctions.printMessage(S.Part, "pkspec1", F.list(list), engine);
}
}
return result;
}
IExpr result = F.NIL;
if (arg2.isAST(S.Key, 2)) {
result = assoc.getValue(arg2.first());
} else if (arg2.isString()) {
result = assoc.getValue(arg2);
}
if (result.isPresent()) {
if (p1 < ast.size()) {
if (result.isASTOrAssociation()) {
return part((IAST) result, ast, p1, engine);
} else {
// Part specification `1` is longer than depth of object.
return IOFunctions.printMessage(S.Part, "partd", F.list(result), engine);
}
}
return result;
}
} else if (arg2.isList()) {
IExpr temp = null;
final IAST list = (IAST) arg2;
final IASTAppendable result = F.ast(arg1.head(), list.size());
for (int i = 1; i < list.size(); i++) {
final IExpr listArg = list.get(i);
if (listArg.isReal()) {
final int indx = listArg.toIntDefault();
if (indx == Integer.MIN_VALUE) {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(listArg, arg1), engine);
}
IExpr ires = getIndex(arg1, indx, engine);
if (ires.isPresent()) {
if (p1 < ast.size()) {
if (ires.isASTOrAssociation()) {
temp = part((IAST) ires, ast, p1, engine);
if (temp.isPresent()) {
result.append(temp);
} else {
// an error occurred
return F.NIL;
}
} else {
// Part specification `1` is longer than depth of object.
return IOFunctions.printMessage(S.Part, "partd", F.list(ires), engine);
}
} else {
result.append(ires);
}
} else {
return F.NIL;
}
} else if (listArg.isNumber() || listArg.isString()) {
// The expression `1` cannot be used as a part specification.
return IOFunctions.printMessage(S.Part, "pkspec1", F.list(list), engine);
}
}
return result;
}
// The expression `1` cannot be used as a part specification.
return IOFunctions.printMessage(S.Part, "pkspec1", F.list(arg2), engine);
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Programming method assignPart.
private static IExpr assignPart(final IExpr assignedExpr, final IAST part, int partPosition, IAST rhs, int rhsPos, EvalEngine engine) {
if (!assignedExpr.isASTOrAssociation() || 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];
IASTAppendable 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.copyAppendable();
}
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.copyAppendable();
}
result.set(i, temp);
}
}
} else {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(arg2, assignedAST), engine);
}
return result;
} else if (arg2.isReal()) {
final int indx = Validate.checkIntType(part, partPosition, Integer.MIN_VALUE);
IExpr ires = null;
ires = assignPartValue(assignedAST, indx, rhs);
if (partPositionPlus1 < part.size()) {
if (ires.isASTOrAssociation()) {
return assignPart(ires, part, partPositionPlus1, rhs, rhsPos++, engine);
} else {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(F.ZZ(partPosition), assignedAST), engine);
}
}
return ires;
} else if (arg2.isList()) {
IExpr temp = null;
final IAST list = (IAST) arg2;
final IASTAppendable 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.throwIntType(listArg, Integer.MIN_VALUE, engine);
ires = assignPartValue(assignedAST, indx, list);
if (ires == null) {
return F.NIL;
}
if (partPositionPlus1 < part.size()) {
if (ires.isASTOrAssociation()) {
temp = assignPart(ires, part, partPositionPlus1, rhs, rhsPos++, engine);
result.append(temp);
} else {
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(F.ZZ(partPosition), assignedAST), engine);
}
} else {
result.append(ires);
}
}
}
return result;
}
// Part `1` of `2` does not exist.
return IOFunctions.printMessage(S.Part, "partw", F.list(arg2, assignedAST), engine);
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class SparseArrayExpr method arrayRules.
/**
* Convert this sparse array to array rules list format.
*/
@Override
public IAST arrayRules() {
IASTAppendable result = F.ListAlloc(fData.size() + 1);
for (TrieNode<int[], IExpr> entry : fData.nodeSet()) {
int[] key = entry.getKey();
IExpr value = entry.getValue();
IAST lhs = F.ast(S.List, key);
result.append(F.Rule(lhs, value));
}
result.append(F.Rule(F.constantArray(F.$b(), fDimension.length), fDefaultValue));
return result;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class SparseArrayExpr method arrayRules.
/**
* Create array rules from the nested lists. From array rules a sparse array can be created.
*
* @param nestedListsOfValues
* @return
*/
public static IAST arrayRules(IAST nestedListsOfValues, IExpr defaultValue) {
int depth = SparseArrayExpr.depth(nestedListsOfValues, 1);
if (depth < 0) {
return F.NIL;
}
// default value rule is additionally appended at the end!
IASTAppendable result = F.ListAlloc(F.allocMin32(F.allocLevel1(nestedListsOfValues, x -> x.isList()) + 2));
IASTMutable positions = F.constantArray(F.C1, depth);
if (SparseArrayExpr.arrayRulesRecursive(nestedListsOfValues, depth + 1, depth, positions, defaultValue, result)) {
result.append(F.Rule(F.constantArray(F.$b(), depth), defaultValue));
return result;
}
return F.NIL;
}
Aggregations