Search in sources :

Example 86 with IASTAppendable

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

Example 87 with IASTAppendable

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

Example 88 with IASTAppendable

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

Example 89 with IASTAppendable

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

Example 90 with IASTAppendable

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;
}
Also used : Arrays(java.util.Arrays) LinearAlgebra(org.matheclipse.core.builtin.LinearAlgebra) FieldElement(org.hipparchus.FieldElement) OpenMapRealMatrix(org.hipparchus.linear.OpenMapRealMatrix) FieldVector(org.hipparchus.linear.FieldVector) ObjectOutput(java.io.ObjectOutput) MathRuntimeException(org.hipparchus.exception.MathRuntimeException) OpenMapRealVector(org.hipparchus.linear.OpenMapRealVector) NullArgumentException(org.hipparchus.exception.NullArgumentException) DataExpr(org.matheclipse.core.expression.DataExpr) IOFunctions(org.matheclipse.core.builtin.IOFunctions) Function(java.util.function.Function) RealMatrix(org.hipparchus.linear.RealMatrix) Trie(org.matheclipse.parser.trie.Trie) ISparseArray(org.matheclipse.core.interfaces.ISparseArray) Tensors(org.matheclipse.core.generic.Tensors) EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Externalizable(java.io.Externalizable) IPatternMap(org.matheclipse.core.patternmatching.IPatternMap) Config(org.matheclipse.core.basic.Config) IOException(java.io.IOException) Field(org.hipparchus.Field) MathUtils(org.hipparchus.util.MathUtils) S(org.matheclipse.core.expression.S) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) PatternMatcherAndEvaluator(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator) Logger(org.apache.logging.log4j.Logger) IntList(it.unimi.dsi.fastutil.ints.IntList) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) AbstractFieldMatrix(org.hipparchus.linear.AbstractFieldMatrix) FieldMatrix(org.hipparchus.linear.FieldMatrix) IExpr(org.matheclipse.core.interfaces.IExpr) ObjectInput(java.io.ObjectInput) LocalizedCoreFormats(org.hipparchus.exception.LocalizedCoreFormats) RealVector(org.hipparchus.linear.RealVector) LogManager(org.apache.logging.log4j.LogManager) TrieNode(org.matheclipse.parser.trie.TrieNode) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Aggregations

IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)363 IExpr (org.matheclipse.core.interfaces.IExpr)219 IAST (org.matheclipse.core.interfaces.IAST)130 ISymbol (org.matheclipse.core.interfaces.ISymbol)36 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)30 IInteger (org.matheclipse.core.interfaces.IInteger)29 Map (java.util.Map)28 EvalEngine (org.matheclipse.core.eval.EvalEngine)20 PrettyPrint (edu.jas.kern.PrettyPrint)13 SortedMap (java.util.SortedMap)13 ArrayList (java.util.ArrayList)12 F (org.matheclipse.core.expression.F)12 BigRational (edu.jas.arith.BigRational)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 ExpVector (edu.jas.poly.ExpVector)9 HashMap (java.util.HashMap)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IStringX (org.matheclipse.core.interfaces.IStringX)8 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8