use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class CreateTensor method createTensorRecursive.
private void createTensorRecursive(IASTMutable list, int[] dims, int position, int[] index) {
final int size = dims[position];
if (dims.length - 1 == position) {
for (int i = 1; i <= size; i++) {
index[position] = i;
list.set(i, function.apply(index));
}
return;
}
final int size2 = dims[position + 1];
for (int i = 1; i <= size; i++) {
index[position] = i;
IASTMutable currentList = F.ast(S.List, size2);
list.set(i, currentList);
createTensorRecursive(currentList, dims, position + 1, index);
}
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class HMArrayList method mapReverse.
@Override
public IAST mapReverse(final Function<IExpr, IExpr> function) {
IASTMutable result = F.NIL;
int i = firstIndex + 1;
int j = lastIndex - 1;
while (i < lastIndex) {
IExpr temp = function.apply(array[i++]);
if (temp.isPresent()) {
// something was evaluated - return a new IAST:
result = copy();
result.set(j--, temp);
break;
}
j--;
}
if (result.isPresent()) {
while (i < lastIndex) {
IExpr temp = function.apply(array[i++]);
if (temp.isPresent()) {
result.set(j, temp);
}
j--;
}
return result;
}
return this;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AST2Expr method convertNode.
/**
* Converts a parsed ASTNode expression into a Symja IExpr expression
*
* @param node the parsed ASTNode
* @return the Symja expression
*/
private IExpr convertNode(ASTNode node) {
if (node == null) {
return null;
}
if (node instanceof FunctionNode) {
final FunctionNode functionNode = (FunctionNode) node;
int size = functionNode.size();
IASTMutable ast;
switch(size) {
case 1:
ast = F.headAST0(convertNode(functionNode.get(0)));
break;
case 2:
ast = F.unaryAST1(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)));
break;
case 3:
ast = F.binaryAST2(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)), convertNode(functionNode.get(2)));
break;
case 4:
ast = F.ternaryAST3(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)), convertNode(functionNode.get(2)), convertNode(functionNode.get(3)));
break;
default:
IASTAppendable appendableAST = F.ast(convertNode(functionNode.get(0)), functionNode.size());
for (int i = 1; i < functionNode.size(); i++) {
appendableAST.append(convertNode(functionNode.get(i)));
}
ast = appendableAST;
}
int functionID = ast.headID();
if (functionID > ID.UNKNOWN) {
IExpr temp = evaluateOnInput(functionID, ast, functionNode);
if (temp.isPresent()) {
return temp;
}
}
return ast;
}
if (node instanceof SymbolNode) {
String nodeStr = node.getString();
return convertSymbol(nodeStr);
}
// PatternNode
if (node instanceof Pattern3Node) {
final Pattern3Node p3n = (Pattern3Node) node;
SymbolNode sn = p3n.getSymbol();
return F.$ps((ISymbol) convertNode(sn), convertNode(p3n.getConstraint()), p3n.isDefault(), true);
}
if (node instanceof Pattern2Node) {
final Pattern2Node p2n = (Pattern2Node) node;
SymbolNode sn = p2n.getSymbol();
return F.$ps((ISymbol) convertNode(sn), convertNode(p2n.getConstraint()), p2n.isDefault(), false);
}
if (node instanceof PatternNode) {
final PatternNode pn = (PatternNode) node;
SymbolNode sn = pn.getSymbol();
if (sn == null) {
return F.$b(convertNode(pn.getConstraint()), pn.isDefault());
}
ASTNode defaultValue = pn.getDefaultValue();
if (defaultValue != null) {
return F.Optional(F.$p((ISymbol) convertNode(pn.getSymbol()), convertNode(pn.getConstraint())), convertNode(defaultValue));
}
return F.$p((ISymbol) convertNode(pn.getSymbol()), convertNode(pn.getConstraint()), pn.isDefault());
}
if (node instanceof IntegerNode) {
final IntegerNode integerNode = (IntegerNode) node;
final String iStr = integerNode.getString();
if (iStr != null) {
return F.ZZ(iStr, integerNode.getNumberFormat());
}
return F.ZZ(integerNode.getIntValue());
}
if (node instanceof FractionNode) {
FractionNode fr = (FractionNode) node;
IInteger numerator = (IInteger) convertNode(fr.getNumerator());
IInteger denominator = (IInteger) convertNode(fr.getDenominator());
if (denominator.isZero()) {
return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
}
if (denominator.isOne()) {
return fr.isSign() ? numerator.negate() : numerator;
}
// return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
return F.fraction(fr.isSign() ? numerator.negate() : numerator, denominator);
}
if (node instanceof StringNode) {
return F.$str(node.getString());
}
if (node instanceof FloatNode) {
String nStr = node.getString();
String floatStr = nStr;
int index = nStr.indexOf("*^");
int exponent = 1;
if (index > 0) {
floatStr = nStr.substring(0, index);
exponent = Integer.parseInt(nStr.substring(index + 2));
}
if (EvalEngine.isApfloat(fPrecision)) {
Apfloat apfloatValue = new Apfloat(floatStr, fPrecision);
if (exponent != 1) {
// value * 10 ^ exponent
return F.num(apfloatValue.multiply(ApfloatMath.pow(new Apint(10), new Apint(exponent))));
}
return F.num(apfloatValue);
}
double doubleValue = Double.parseDouble(floatStr);
if (exponent != 1) {
// value * 10 ^ exponent
return F.num(doubleValue * Math.pow(10, exponent));
}
return F.num(doubleValue);
}
if (node instanceof DoubleNode) {
return F.num(((DoubleNode) node).doubleValue());
}
return F.symbol(node.toString());
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class ListFunctions method rankedMin.
/**
* Sort the list of real numbers and return the n-th smallest element, or sort the values of an
* association of real numbers and return the n-th smallest element.
*
* @param listOrAssociation list or association of real elements
* @param n must be in the range (1..list.argSize())
* @param ast
* @param engine
* @return {@link F#NIL} if not all elements are real
*/
private static IExpr rankedMin(IAST listOrAssociation, int n, final IAST ast, EvalEngine engine) {
// TODO choose better algorithm: https://www.baeldung.com/cs/k-smallest-numbers-array
// check for non real elements (especially complex numbers)
int quantities = 0;
for (int i = 1; i < listOrAssociation.size(); i++) {
IExpr element = listOrAssociation.getValue(i);
if (element.isQuantity()) {
quantities++;
continue;
}
ISignedNumber r = element.evalReal();
if (//
r == null && !(element.isInfinity() || element.isNegativeInfinity())) {
for (int j = i; j < listOrAssociation.size(); j++) {
element = listOrAssociation.get(j);
if (element.isComplexNumeric() || element.isComplex()) {
// Input `1` is not a vector of reals or integers.
return IOFunctions.printMessage(ast.topHead(), "rvec", F.list(listOrAssociation), engine);
}
}
return F.NIL;
}
}
if (quantities > 0 && quantities != listOrAssociation.argSize()) {
return F.NIL;
}
IASTMutable orderedList = listOrAssociation.copyAST();
return EvalAttributes.copySortLess(orderedList).get(n);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class OutputFunctions method checkIsVariableOrVariableList.
/**
* Get an array with 2 elements returning the declared variables in the first entry and the
* corresponding types <code>Real, Integer,...</code> for the variable names in the second entry.
*
* @param ast the original definition <code>
* CompilePrint({variable/types}, function)</code>
* @param engine the evaluation engine
* @return <code>null</code> if the variable declaration isn't correct
*/
public static IAST[] checkIsVariableOrVariableList(IAST ast, EvalEngine engine) {
IASTMutable[] result = new IASTMutable[2];
IExpr arg1 = ast.arg1();
if (arg1.isList()) {
IAST list = (IAST) arg1;
result[0] = list.copy();
result[1] = F.constantArray(S.Real, list.argSize());
for (int i = 1; i < list.size(); i++) {
if (!checkVariable(list.get(i), i, result[0], result[1], engine)) {
// `1` is not a valid variable.
IOFunctions.printMessage(ast.topHead(), "ivar", F.list(list.get(i)), engine);
return null;
}
}
} else {
result[0] = F.unaryAST1(S.List, arg1);
result[1] = F.unaryAST1(S.List, S.Real);
if (!checkVariable(arg1, 1, result[0], result[1], engine)) {
// `1` is not a valid variable.
IOFunctions.printMessage(ast.topHead(), "ivar", F.list(arg1), engine);
return null;
}
}
return result;
}
Aggregations