use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class F method ListAlloc.
/**
* Create a new <code>List</code> with the capacity <code>collection.size() + capacity</code> and
* append the elements of the collection.
*
* @param collection
* @param capacity
* @return
*/
public static IASTAppendable ListAlloc(Collection<? extends IExpr> collection, int capacity) {
IASTAppendable result = ast(List, collection.size() + capacity);
result.appendAll(collection);
return result;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class F method operatorForm2Prepend.
/**
* The operator form <code>op(f)[expr]</code> is transformed to <code>op(f, expr)</code>. The
* operator form <code>op(f)[expr1, expr2]</code> is transformed to <code>op(f, expr1, expr2)
* </code>.
*
* @param ast1 an <code>IAST</code> with condition <code>
* ast1Arg.head().isAST1() && ast1Arg.isAST1()</code>
* @return
*/
public static IAST operatorForm2Prepend(final IAST ast1, int[] expected, EvalEngine engine) {
if (ast1.head().isAST1() && ast1.argSize() > 0) {
if (ast1.argSize() + 1 < expected[0] || ast1.argSize() + 1 > expected[1]) {
return IOFunctions.printArgMessage(ast1, expected, engine);
}
IExpr headArg1 = ast1.head().first();
switch(ast1.size()) {
case 2:
return new AST2(ast1.topHead(), headArg1, ast1.arg1());
case 3:
return new AST3(ast1.topHead(), headArg1, ast1.arg1(), ast1.arg2());
default:
IASTAppendable result = ast(ast1.topHead(), ast1.size() + 1);
result.append(headArg1);
result.appendArgs(ast1);
return result;
}
}
return NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class F method flattenSequence.
/**
* Iterate over the arguments of <code>list</code> and flatten the arguments of <code>
* Sequence(...)
* </code> expressions. (i.e. <code>{Sequence(a,b,...)}</code> is rewritten as <code>{a,b,...}
* </code>). If some of the elements is the symbol <code>Nothing</code> it's automatically removed
* from the arguments.
*
* @param list an AST which may contain <code>Sequence(...)</code> expressions or <code>Nothing
* </code> symbols.
* @return <code>F.NIL</code> if no sequence is flattened
*/
public static IAST flattenSequence(final IAST list) {
if (list.isEvalFlagOn(IAST.SEQUENCE_FLATTENED)) {
return NIL;
}
final boolean isList = list.isList();
final int indx = list.indexOf(x -> x.isSequence() || (isList && x == Nothing));
if (indx > 0) {
final int extraSize = list.get(indx).size();
final IASTAppendable seqResult = F.ast(list.head(), list.size() + extraSize + 1);
seqResult.appendArgs(list, indx);
list.forEach(indx, list.size(), x -> {
if (x.isSequence()) {
seqResult.appendArgs((IAST) x);
} else if (isList && x == Nothing) {
} else {
seqResult.append(x);
}
});
return seqResult;
}
list.addEvalFlags(IAST.SEQUENCE_FLATTENED);
return NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class F method matrix.
/**
* Generate a <code>n x m</code> matrix. The indices start in Java convention with <code>0</code>.
*
* @param biFunction
* @param n the number of rows of the matrix.
* @param m the number of elements in one row
* @return
*/
public static IAST matrix(BiIntFunction<? extends IExpr> biFunction, int n, int m) {
if (n > Config.MAX_MATRIX_DIMENSION_SIZE || m > Config.MAX_MATRIX_DIMENSION_SIZE) {
ASTElementLimitExceeded.throwIt(((long) n) * ((long) m));
}
IASTAppendable matrix = ListAlloc(n);
for (int i = 0; i < n; i++) {
IASTAppendable row = ListAlloc(m);
for (int j = 0; j < m; j++) {
row.append(biFunction.apply(i, j));
}
matrix.append(row);
}
// because the rows can contain sub lists the IAST.IS_MATRIX flag cannot be set directly.
// isMatrix() must be
// used!
matrix.isMatrix(true);
return matrix;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class IntervalSym method mutableProcessorConditions.
/**
* Replaces the most common code. Determines the result depending on the fulfillment of
* conditions.
*
* @param ast
* @param processors conditions to be met
* @return IAST result, append value or F.NIL;
*/
private static IAST mutableProcessorConditions(final IAST ast, IExprProcessor... processors) {
if (processors != null && processors.length > 0) {
IAST interval = normalize(ast);
if (interval.isPresent()) {
try {
IASTAppendable result = F.IntervalAlloc(interval.size());
for (int i = 1; i < interval.size(); i++) {
IAST list = (IAST) interval.get(i);
IExpr min = list.arg1();
IExpr max = list.arg2();
boolean processed = false;
for (IExprProcessor processor : processors) {
processed = processor.apply(min, max, result, i);
if (processed) {
break;
}
}
if (!processed) {
return F.NIL;
}
}
return result;
} catch (RuntimeException rex) {
//
}
}
}
return F.NIL;
}
Aggregations