Exemple #1
0
 def _eval_as_leading_term(self, x, logx=None, cdir=0):
     from sympy.series.order import Order
     n, z = [a.as_leading_term(x) for a in self.args]
     o = Order(z, x)
     if n == 0 and o.contains(1 / x):
         return o.getn() * log(x)
     else:
         return self.func(n, z)
Exemple #2
0
    def _eval_as_leading_term(self, x, logx=None, cdir=0):
        from sympy.series.order import Order
        from sympy.functions.elementary.exponential import log
        from sympy.functions.elementary.piecewise import Piecewise, piecewise_fold
        from .function import expand_mul

        old = self

        if old.has(Piecewise):
            old = piecewise_fold(old)

        # This expansion is the last part of expand_log. expand_log also calls
        # expand_mul with factor=True, which would be more expensive
        if any(isinstance(a, log) for a in self.args):
            logflags = dict(deep=True, log=True, mul=False, power_exp=False,
                power_base=False, multinomial=False, basic=False, force=False,
                factor=False)
            old = old.expand(**logflags)
        expr = expand_mul(old)

        if not expr.is_Add:
            return expr.as_leading_term(x, logx=logx, cdir=cdir)

        infinite = [t for t in expr.args if t.is_infinite]

        leading_terms = [t.as_leading_term(x, logx=logx, cdir=cdir) for t in expr.args]

        min, new_expr = Order(0), 0

        try:
            for term in leading_terms:
                order = Order(term, x)
                if not min or order not in min:
                    min = order
                    new_expr = term
                elif min in order:
                    new_expr += term

        except TypeError:
            return expr

        is_zero = new_expr.is_zero
        if is_zero is None:
            new_expr = new_expr.trigsimp().cancel()
            is_zero = new_expr.is_zero
        if is_zero is True:
            # simple leading term analysis gave us cancelled terms but we have to send
            # back a term, so compute the leading term (via series)
            n0 = min.getn()
            res = Order(1)
            incr = S.One
            while res.is_Order:
                res = old._eval_nseries(x, n=n0+incr, logx=None, cdir=cdir).cancel().powsimp().trigsimp()
                incr *= 2
            return res.as_leading_term(x, logx=logx, cdir=cdir)

        elif new_expr is S.NaN:
            return old.func._from_args(infinite)

        else:
            return new_expr