Search in sources :

Example 21 with IASTMutable

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);
    }
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 22 with IASTMutable

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;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 23 with IASTMutable

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());
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IntegerNode(org.matheclipse.parser.client.ast.IntegerNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) Pattern2Node(org.matheclipse.parser.client.ast.Pattern2Node) FloatNode(org.matheclipse.parser.client.ast.FloatNode) Apint(org.apfloat.Apint) DoubleNode(org.matheclipse.parser.client.eval.DoubleNode) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) FractionNode(org.matheclipse.parser.client.ast.FractionNode) Apint(org.apfloat.Apint) Apfloat(org.apfloat.Apfloat) SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) PatternNode(org.matheclipse.parser.client.ast.PatternNode) IInteger(org.matheclipse.core.interfaces.IInteger) ASTNode(org.matheclipse.parser.client.ast.ASTNode) StringNode(org.matheclipse.parser.client.ast.StringNode) IExpr(org.matheclipse.core.interfaces.IExpr) Pattern3Node(org.matheclipse.parser.client.ast.Pattern3Node)

Example 24 with IASTMutable

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);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 25 with IASTMutable

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;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2