Search in sources :

Example 1 with F

use of org.matheclipse.core.expression.F 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)

Example 2 with F

use of org.matheclipse.core.expression.F in project symja_android_library by axkr.

the class Predicates method toFreeQ.

/**
 * Convert the pattern into a pattern-matching predicate used in {@link F#FreeQ(IExpr, IExpr)}.
 * FreeQ does test for subsequences (MemberQ does not test for subsequences).
 *
 * @param pattern
 * @return
 * @see IExpr#isFree(Predicate, boolean)
 */
public static Predicate<IExpr> toFreeQ(IExpr pattern) {
    if (pattern.isSymbol() || pattern.isNumber() || pattern.isString()) {
        return x -> x.equals(pattern);
    }
    final IPatternMatcher matcher;
    if (pattern.isOrderlessAST() && pattern.isFreeOfPatterns()) {
        // append a BlankNullSequence[] to match the parts of an Orderless expression
        IPatternSequence blankNullRest = F.$ps(null, true);
        IASTAppendable newPattern = ((IAST) pattern).copyAppendable();
        newPattern.append(blankNullRest);
        matcher = new PatternMatcher(newPattern);
    } else {
        matcher = new PatternMatcher(pattern);
    }
    return matcher;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) Predicate(java.util.function.Predicate) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISymbol(org.matheclipse.core.interfaces.ISymbol) Serializable(java.io.Serializable) BiPredicate(java.util.function.BiPredicate) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr) IPattern(org.matheclipse.core.interfaces.IPattern) Comparator(java.util.Comparator) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 3 with F

use of org.matheclipse.core.expression.F in project symja_android_library by axkr.

the class Eliminate method extractVariableRecursive.

/**
 * Extract a value for the given <code>variabe</code>.
 *
 * @param exprWithVariable expression which contains the given <code>variabe</code>.
 * @param exprWithoutVariable expression which doesn't contain the given <code>variabe</code>.
 * @param variable the variable which should be eliminated.
 * @param multipleValues if <code>true</code> multiple results are returned as list of values
 * @return <code>F.NIL</code> if we can't find an equation for the given variable <code>x</code>.
 */
private static IExpr extractVariableRecursive(IExpr exprWithVariable, IExpr exprWithoutVariable, Predicate<IExpr> predicate, IExpr variable, boolean multipleValues, EvalEngine engine) {
    if (exprWithVariable.equals(variable)) {
        return exprWithoutVariable;
    }
    if (exprWithVariable.isAST()) {
        IAST ast = (IAST) exprWithVariable;
        if (ast.isAST1()) {
            IASTAppendable inverseFunction = InverseFunction.getUnaryInverseFunction(ast, true);
            if (inverseFunction.isPresent()) {
                if (exprWithVariable.isAbs()) {
                    if (exprWithoutVariable.isNonNegativeResult()) {
                        // example: Abs(x-1) == 1
                        inverseFunction.append(exprWithoutVariable);
                        return extractVariableRecursive(ast.arg1(), inverseFunction, predicate, variable, multipleValues, engine);
                    }
                    return S.True;
                } else {
                    // example: Sin(f(x)) == y -> f(x) == ArcSin(y)
                    inverseFunction.append(exprWithoutVariable);
                    return extractVariableRecursive(ast.arg1(), inverseFunction, predicate, variable, multipleValues, engine);
                }
            }
        } else {
            int size = ast.size();
            if (size > 2) {
                if (exprWithoutVariable.isZero() && ast.isPlus()) {
                    IAST elimZeroPlus = F.binaryAST2(elimzeroplus, ast, variable);
                    IExpr result = zeroPlusMatcher().apply(elimZeroPlus);
                    if (result.isPresent()) {
                        return resultWithIfunMessage(result, variable, exprWithoutVariable, multipleValues, engine);
                    }
                }
                IAST elimInverse = F.binaryAST2(eliminv, ast, variable);
                IExpr result = inverseMatcher().apply(elimInverse);
                if (result.isPresent()) {
                    return resultWithIfunMessage(result, variable, exprWithoutVariable, multipleValues, engine);
                }
            }
            if (ast.isPlus()) {
                // a + b + c....
                if (// 
                exprWithoutVariable.isNumericFunction() && ast.isPolynomial(variable) && ast.isNumericFunction(variable)) {
                    IAST temp = RootsFunctions.rootsOfVariable(F.Subtract.of(ast, exprWithoutVariable), F.C1, F.list(variable), engine.isNumericMode(), engine);
                    if (temp.isList() && temp.size() > 1) {
                        if (!multipleValues || temp.size() == 2) {
                            return temp.first();
                        }
                        return temp;
                    }
                }
                IAST[] plusFilter = ast.filter(x -> x.isFree(predicate, true));
                IAST plusWithoutVariable = plusFilter[0];
                IAST plusWithVariable = plusFilter[1];
                if (plusWithoutVariable.isAST0()) {
                    IExpr factor = engine.evaluateNIL(F.Factor(ast));
                    if (factor.isPresent() && factor.isTimes()) {
                        // a * b * c....
                        IAST times = (IAST) factor;
                        IAST[] timesFilter = times.filter(x -> x.isFree(predicate, true));
                        IAST timesWithoutVariable = timesFilter[0];
                        IAST timesWithVariable = timesFilter[1];
                        if (timesWithoutVariable.isAST0()) {
                            return F.NIL;
                        }
                        IExpr rhsWithoutVariable = engine.evaluate(F.Divide(exprWithoutVariable, timesWithoutVariable));
                        return extractVariableRecursive(timesWithVariable.oneIdentity1(), rhsWithoutVariable, predicate, variable, multipleValues, engine);
                    }
                    return F.NIL;
                }
                IExpr rhsWithoutVariable = engine.evaluate(F.Subtract(exprWithoutVariable, plusWithoutVariable));
                return extractVariableRecursive(plusWithVariable.oneIdentity0(), rhsWithoutVariable, predicate, variable, multipleValues, engine);
            } else if (ast.isTimes()) {
                // a * b * c....
                IAST[] timesFilter = ast.filter(x -> x.isFree(predicate, true));
                IAST timesWithoutVariable = timesFilter[0];
                IAST timesWithVariable = timesFilter[1];
                if (timesWithoutVariable.isAST0()) {
                    IExpr[] numerDenom = Algebra.getNumeratorDenominator(ast, EvalEngine.get());
                    if (!numerDenom[1].isOne()) {
                        IExpr[] numerLinear = numerDenom[0].linear(variable);
                        if (numerLinear != null) {
                            IExpr[] denomLinear = numerDenom[1].linear(variable);
                            if (denomLinear != null) {
                                IExpr temp = EvalEngine.get().evaluate(numerLinear[1].subtract(denomLinear[1].times(exprWithoutVariable)));
                                if (!temp.isZero()) {
                                    return numerLinear[0].negate().plus(denomLinear[0].times(exprWithoutVariable)).times(temp.power(-1L));
                                }
                            }
                        }
                    }
                    // no change for given expression
                    return F.NIL;
                }
                IExpr value = F.Divide(exprWithoutVariable, timesWithoutVariable);
                return extractVariableRecursive(timesWithVariable.oneIdentity1(), value, predicate, variable, multipleValues, engine);
            } else if (ast.isPower()) {
                IExpr base = ast.base();
                IExpr exponent = ast.exponent();
                if (exponent.isFree(predicate, true)) {
                    // f(x) ^ a
                    printIfunMessage(engine);
                    IExpr value = engine.evaluate(F.Power(exprWithoutVariable, F.Divide(F.C1, exponent)));
                    return extractVariableRecursive(base, value, predicate, variable, multipleValues, engine);
                } else if (base.isFree(predicate, true)) {
                    if (base.isE()) {
                        if (exponent.isRealResult()) {
                            // E ^ f(x) /; Element(f(x), Reals)
                            return extractVariableRecursive(exponent, F.Log(exprWithoutVariable), predicate, variable, multipleValues, engine);
                        }
                        // E ^ f(x) /; Element(f(x), Complexes)
                        IExpr c1 = F.C(1);
                        final IExpr exprwovar = exprWithoutVariable;
                        IExpr temp = // [$ ConditionalExpression(2*I*Pi*c1 + Log(exprwovar), Element(c1, Integers)) $]
                        F.ConditionalExpression(F.Plus(F.Times(F.C2, F.CI, S.Pi, c1), F.Log(exprwovar)), // $$;
                        F.Element(c1, S.Integers));
                        return extractVariableRecursive(exponent, temp, predicate, variable, multipleValues, engine);
                    }
                    // a ^ f(x)
                    IExpr value = F.Divide(F.Log(exprWithoutVariable), F.Log(base));
                    return extractVariableRecursive(exponent, value, predicate, variable, multipleValues, engine);
                }
            }
        }
    }
    return F.NIL;
}
Also used : IStringX(org.matheclipse.core.interfaces.IStringX) Matcher(org.matheclipse.core.patternmatching.Matcher) EliminateRules(org.matheclipse.core.reflection.system.rulesets.EliminateRules) IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IOFunctions(org.matheclipse.core.builtin.IOFunctions) Supplier(java.util.function.Supplier) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) IComplex(org.matheclipse.core.interfaces.IComplex) ArrayList(java.util.ArrayList) BooleanFunctions(org.matheclipse.core.builtin.BooleanFunctions) AbstractFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator) IPattern(org.matheclipse.core.interfaces.IPattern) Suppliers(com.google.common.base.Suppliers) Algebra(org.matheclipse.core.builtin.Algebra) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Predicate(java.util.function.Predicate) Predicates(org.matheclipse.core.generic.Predicates) INum(org.matheclipse.core.interfaces.INum) ISymbol(org.matheclipse.core.interfaces.ISymbol) S(org.matheclipse.core.expression.S) Logger(org.apache.logging.log4j.Logger) Validate(org.matheclipse.core.eval.exception.Validate) IExpr(org.matheclipse.core.interfaces.IExpr) RootsFunctions(org.matheclipse.core.builtin.RootsFunctions) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) AbstractVisitorBoolean(org.matheclipse.core.visit.AbstractVisitorBoolean) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)3 F (org.matheclipse.core.expression.F)3 IAST (org.matheclipse.core.interfaces.IAST)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 Predicate (java.util.function.Predicate)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 IOFunctions (org.matheclipse.core.builtin.IOFunctions)2 S (org.matheclipse.core.expression.S)2 Suppliers (com.google.common.base.Suppliers)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1