Search in sources :

Example 21 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Assumptions method addGreater.

private static boolean addGreater(IAST greaterAST, Assumptions assumptions) {
    if (greaterAST.isAST3()) {
        // arg1 > arg2 > arg3
        IExpr arg1 = greaterAST.arg1();
        IExpr arg2 = greaterAST.arg2();
        IExpr arg3 = greaterAST.arg3();
        if (arg1.isSignedNumber() && arg3.isSignedNumber() && !arg2.isNumber()) {
            if (((ISignedNumber) arg1).isGreaterThan(((ISignedNumber) arg3))) {
                ISignedNumber num1 = (ISignedNumber) arg1;
                ISignedNumber num3 = (ISignedNumber) arg3;
                IExpr key = arg2;
                SignedNumberRelations gla = assumptions.valueMap.get(key);
                if (gla == null) {
                    gla = new SignedNumberRelations();
                }
                gla.addLess(num1);
                gla.addGreater(num3);
                assumptions.valueMap.put(key, gla);
                return true;
            }
        }
        return false;
    }
    // arg1 > arg2
    if (greaterAST.arg2().isSignedNumber()) {
        SignedNumberRelations gla = assumptions.valueMap.get(greaterAST.arg1());
        if (gla == null) {
            gla = new SignedNumberRelations();
            gla.addGreater((ISignedNumber) greaterAST.arg2());
        } else {
            gla.addGreater((ISignedNumber) greaterAST.arg2());
        }
        assumptions.valueMap.put(greaterAST.arg1(), gla);
        return true;
    }
    if (greaterAST.arg1().isSignedNumber()) {
        ISignedNumber num = (ISignedNumber) greaterAST.arg1();
        IExpr key = greaterAST.arg2();
        SignedNumberRelations gla = assumptions.valueMap.get(key);
        if (gla == null) {
            gla = new SignedNumberRelations();
            gla.addLess(num);
        } else {
            gla.addLess(num);
        }
        assumptions.valueMap.put(key, gla);
        return true;
    }
    return false;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 22 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Assumptions method addLess.

private static boolean addLess(IAST lessAST, Assumptions assumptions) {
    if (lessAST.isAST3()) {
        // arg1 < arg2 < arg3;
        IExpr arg1 = lessAST.arg1();
        IExpr arg2 = lessAST.arg2();
        IExpr arg3 = lessAST.arg3();
        if (arg1.isSignedNumber() && arg3.isSignedNumber() && !arg2.isNumber()) {
            if (((ISignedNumber) arg1).isLessThan(((ISignedNumber) arg3))) {
                ISignedNumber num1 = (ISignedNumber) arg1;
                ISignedNumber num3 = (ISignedNumber) arg3;
                IExpr key = arg2;
                SignedNumberRelations gla = assumptions.valueMap.get(key);
                if (gla == null) {
                    gla = new SignedNumberRelations();
                }
                gla.addGreater(num1);
                gla.addLess(num3);
                assumptions.valueMap.put(key, gla);
                return true;
            }
        }
        return false;
    }
    // arg1 < arg2
    if (lessAST.arg2().isSignedNumber()) {
        SignedNumberRelations gla = assumptions.valueMap.get(lessAST.arg1());
        if (gla == null) {
            gla = new SignedNumberRelations();
        }
        gla.addLess((ISignedNumber) lessAST.arg2());
        assumptions.valueMap.put(lessAST.arg1(), gla);
        return true;
    }
    if (lessAST.arg1().isSignedNumber()) {
        ISignedNumber num = (ISignedNumber) lessAST.arg1();
        IExpr key = lessAST.arg2();
        SignedNumberRelations gla = assumptions.valueMap.get(key);
        if (gla == null) {
            gla = new SignedNumberRelations();
        }
        gla.addGreater(num);
        assumptions.valueMap.put(key, gla);
        return true;
    }
    return false;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 23 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Assumptions method addGreaterEqual.

private static boolean addGreaterEqual(IAST greaterEqualAST, Assumptions assumptions) {
    if (greaterEqualAST.isAST3()) {
        // arg1 >= arg2 >= arg3
        IExpr arg1 = greaterEqualAST.arg1();
        IExpr arg2 = greaterEqualAST.arg2();
        IExpr arg3 = greaterEqualAST.arg3();
        if (arg1.isSignedNumber() && arg3.isSignedNumber() && !arg2.isNumber()) {
            if (!((ISignedNumber) arg1).isLessThan(((ISignedNumber) arg3))) {
                ISignedNumber num1 = (ISignedNumber) arg1;
                ISignedNumber num3 = (ISignedNumber) arg3;
                IExpr key = arg2;
                SignedNumberRelations gla = assumptions.valueMap.get(key);
                if (gla == null) {
                    gla = new SignedNumberRelations();
                }
                gla.addLessEqual(num1);
                gla.addGreaterEqual(num3);
                assumptions.valueMap.put(key, gla);
                return true;
            }
        }
        return false;
    }
    // arg1 >= arg2
    if (greaterEqualAST.arg2().isSignedNumber()) {
        SignedNumberRelations gla = assumptions.valueMap.get(greaterEqualAST.arg1());
        if (gla == null) {
            gla = new SignedNumberRelations();
        }
        gla.addGreaterEqual((ISignedNumber) greaterEqualAST.arg2());
        assumptions.valueMap.put(greaterEqualAST.arg1(), gla);
        return true;
    }
    if (greaterEqualAST.arg1().isSignedNumber()) {
        ISignedNumber num = (ISignedNumber) greaterEqualAST.arg1();
        IExpr key = greaterEqualAST.arg2();
        SignedNumberRelations gla = assumptions.valueMap.get(key);
        if (gla == null) {
            gla = new SignedNumberRelations();
        }
        gla.addLessEqual(num);
        assumptions.valueMap.put(key, gla);
        return true;
    }
    return false;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 24 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class PatternMatcher method matchAST.

protected boolean matchAST(final IAST lhsPatternAST, final IExpr lhsEvalExpr, StackMatcher stackMatcher) {
    // "+lhsEvalExpr.toString());
    if (lhsPatternAST.isAST(F.PatternTest, 3)) {
        if (matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher)) {
            return fPatternMap.isPatternTest(lhsPatternAST.arg1(), lhsPatternAST.arg2());
        }
        return false;
    }
    if (lhsPatternAST.isAST(F.Except, 2, 3)) {
        if (lhsPatternAST.isAST2()) {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, stackMatcher);
        } else {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher);
        }
    }
    if (lhsEvalExpr instanceof IAST) {
        if (!lhsPatternAST.isPatternExpr() && lhsPatternAST.equals(lhsEvalExpr)) {
            return stackMatcher.matchRest();
        }
        final IAST lhsEvalAST = (IAST) lhsEvalExpr;
        final ISymbol sym = lhsPatternAST.topHead();
        if (lhsPatternAST.size() <= lhsEvalAST.size()) {
            if ((lhsPatternAST.isFlatAST()) && sym.equals(lhsEvalAST.topHead()) && !(lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() == lhsEvalAST.size())) {
                if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                    return false;
                }
                return matchFlatAndFlatOrderlessAST(sym, lhsPatternAST, lhsEvalAST, stackMatcher);
            }
            if (lhsPatternAST.size() < lhsEvalAST.size()) {
                if (lhsPatternAST.isEvalFlagOn(IAST.CONTAINS_PATTERN_SEQUENCE)) {
                    if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                        return false;
                    }
                    int lastPosition = lhsPatternAST.size() - 1;
                    if (lhsPatternAST.get(lastPosition).isAST(F.PatternTest, 3)) {
                        IAST patternTest = (IAST) lhsPatternAST.get(lastPosition);
                        if (patternTest.arg1().isPatternSequence()) {
                            // TODO only the special case, where the last
                            // element is
                            // a pattern sequence, is handled here
                            IAST seq = F.Sequence();
                            seq.appendAll(lhsEvalAST, lastPosition, lhsEvalAST.size());
                            if (((IPatternSequence) patternTest.arg1()).matchPatternSequence(seq, fPatternMap)) {
                                if (matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher)) {
                                    return fPatternMap.isPatternTest(patternTest.arg1(), patternTest.arg2());
                                }
                                return false;
                            }
                        }
                    }
                    if (lhsPatternAST.get(lastPosition).isPatternSequence()) {
                        // TODO only the special case, where the last
                        // element is
                        // a pattern sequence, is handled here
                        IAST seq = F.Sequence();
                        seq.appendAll(lhsEvalAST.range(), lastPosition, lhsEvalAST.size());
                        if (((IPatternSequence) lhsPatternAST.get(lastPosition)).matchPatternSequence(seq, fPatternMap)) {
                            return matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher);
                        }
                    }
                }
                return false;
            }
        }
        if (lhsPatternAST.size() != lhsEvalAST.size()) {
            return false;
        }
        IExpr e1 = lhsPatternAST.head();
        IExpr e2 = lhsEvalAST.head();
        if (e1.isSymbol() && e2.isSymbol()) {
            if (!e1.equals(e2)) {
                return false;
            }
        } else {
            // TODO create correct stack-matcher for the following call:
            if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                return false;
            }
        }
        if (lhsPatternAST.isOrderlessAST()) {
            // only "pure Orderless" and "FlatOrderless with same size()"
            // will be handled here:
            OrderlessStepVisitor visitor = new OrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap, (sym.hasOneIdentityAttribute()) || // matching
            (lhsPatternAST.size() == lhsEvalAST.size() && !sym.hasFlatAttribute()));
            MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
            return !iter.execute();
        }
        return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
    }
    if (lhsPatternAST.isAST(F.Rational, 3) && lhsEvalExpr.isRational()) {
        IRational numer = ((IRational) lhsEvalExpr).getNumerator();
        IRational denom = ((IRational) lhsEvalExpr).getDenominator();
        if (matchExpr(lhsPatternAST.arg1(), numer) && matchExpr(lhsPatternAST.arg2(), denom)) {
            return true;
        }
    } else if (lhsPatternAST.isAST(F.Complex, 3) && lhsEvalExpr.isNumber()) {
        ISignedNumber re = ((INumber) lhsEvalExpr).re();
        ISignedNumber im = ((INumber) lhsEvalExpr).im();
        if (matchExpr(lhsPatternAST.arg1(), re) && matchExpr(lhsPatternAST.arg2(), im)) {
            return true;
        }
    }
    return false;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MultisetPartitionsIterator(org.matheclipse.combinatoric.MultisetPartitionsIterator) IRational(org.matheclipse.core.interfaces.IRational) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 25 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class HornerScheme method generate.

public IAST generate(boolean numericMode, IAST poly, ISymbol sym) {
    if (numericMode) {
        for (int i = 1; i < poly.size(); i++) {
            collectTermN(sym, poly.get(i));
        }
        IAST result = F.Plus();
        IAST startResult = result;
        IAST temp;
        ISignedNumber start = F.CD0;
        for (Iterator<ISignedNumber> iter = map.keySet().iterator(); iter.hasNext(); ) {
            ISignedNumber exponent = iter.next();
            IExpr coefficient = getCoefficient(exponent);
            if (exponent.isLessThan(F.CD1)) {
                if (exponent.compareTo(F.CD0) == 0) {
                    result.append(coefficient);
                } else {
                    result.append(F.Times(coefficient, F.Power(sym, exponent)));
                }
            } else {
                temp = F.Times();
                ISignedNumber currentExponent = exponent.subtractFrom(start);
                if (currentExponent.equals(F.CD1)) {
                    temp.append(sym);
                } else {
                    temp.append(F.Power(sym, currentExponent));
                }
                result.append(temp);
                result = F.Plus();
                temp.append(result);
                result.append(coefficient);
                start = exponent;
            }
        }
        return startResult;
    } else {
        for (int i = 1; i < poly.size(); i++) {
            collectTerm(sym, poly.get(i));
        }
        IAST result = F.Plus();
        IAST startResult = result;
        IAST temp;
        ISignedNumber start = F.C0;
        for (Iterator<ISignedNumber> iter = map.keySet().iterator(); iter.hasNext(); ) {
            ISignedNumber exponent = iter.next();
            IExpr coefficient = getCoefficient(exponent);
            if (exponent.isLessThan(F.C1)) {
                if (exponent.compareTo(F.C0) == 0) {
                    result.append(coefficient);
                } else {
                    result.append(F.Times(coefficient, F.Power(sym, exponent)));
                }
            } else {
                temp = F.Times();
                ISignedNumber currentExponent = exponent.subtractFrom(start);
                if (currentExponent.equals(F.C1)) {
                    temp.append(sym);
                } else {
                    temp.append(F.Power(sym, currentExponent));
                }
                result.append(temp);
                result = F.Plus();
                temp.append(result);
                result.append(coefficient);
                start = exponent;
            }
        }
        return startResult;
    }
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)49 IExpr (org.matheclipse.core.interfaces.IExpr)31 IAST (org.matheclipse.core.interfaces.IAST)22 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)7 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 Num (org.matheclipse.core.expression.Num)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 IntegerDistribution (org.hipparchus.distribution.IntegerDistribution)2 RealDistribution (org.hipparchus.distribution.RealDistribution)2 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)2 WrappedException (org.matheclipse.core.eval.exception.WrappedException)2 Options (org.matheclipse.core.eval.util.Options)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 INum (org.matheclipse.core.interfaces.INum)2 IRational (org.matheclipse.core.interfaces.IRational)2 ArrayList (java.util.ArrayList)1