Beispiel #1
0
    def _eval_as_leading_term(self, x):
        arg = self.args[0].as_leading_term(x)

        if C.Order(1, x).contains(arg):
            return 1 / arg
        else:
            return self.func(arg)
Beispiel #2
0
    def extract_leading_order(self, symbols, point=None):
        """
        Returns the leading term and its order.

        Examples
        ========

        >>> from sympy.abc import x
        >>> (x + 1 + 1/x**5).extract_leading_order(x)
        ((x**(-5), O(x**(-5))),)
        >>> (1 + x).extract_leading_order(x)
        ((1, O(1)),)
        >>> (x + x**2).extract_leading_order(x)
        ((x, O(x)),)

        """
        lst = []
        symbols = list(symbols if is_sequence(symbols) else [symbols])
        if not point:
            point = [0]*len(symbols)
        seq = [(f, C.Order(f, *zip(symbols, point))) for f in self.args]
        for ef, of in seq:
            for e, o in lst:
                if o.contains(of) and o != of:
                    of = None
                    break
            if of is None:
                continue
            new_lst = [(ef, of)]
            for e, o in lst:
                if of.contains(o) and o != of:
                    continue
                new_lst.append((e, o))
            lst = new_lst
        return tuple(lst)
Beispiel #3
0
    def _eval_as_leading_term(self, x):
        arg = self.args[0].as_leading_term(x)

        if x in arg.free_symbols and C.Order(1, x).contains(arg):
            return 1 / arg
        else:
            return self.func(arg)
Beispiel #4
0
 def _eval_as_leading_term(self, x):
     arg = self.args[0]
     if arg.is_Add:
         return C.Mul(*[exp(f).as_leading_term(x) for f in arg.args])
     arg = self.args[0].as_leading_term(x)
     if C.Order(1,x).contains(arg):
         return S.One
     return exp(arg)
Beispiel #5
0
 def _taylor(self, x, x0, n):
     l = []
     g = None
     for i in xrange(n):
         g = self.taylor_term(i, self.args[0], g)
         g = g.nseries(x, x0, n)
         l.append(g)
     return C.Add(*l) + C.Order(x**n, x)
Beispiel #6
0
 def _eval_nseries(self, x, x0, n):
     from sympy import powsimp
     arg = self.args[0]
     k, l = Wild("k"), Wild("l")
     r = arg.match(k*x**l)
     if r is not None:
         k, l = r[k], r[l]
         if l != 0 and not l.has(x) and not k.has(x):
             r = log(k) + l*log(x)
             return r
     order = C.Order(x**n, x)
     arg = self.args[0]
     x = order.symbols[0]
     ln = C.log
     use_lt = not C.Order(1,x).contains(arg)
     if not use_lt:
         arg0 = arg.limit(x, 0)
         use_lt = (arg0 is S.Zero)
     if use_lt: # singularity, #example: self = log(sin(x))
         # arg = (arg / lt) * lt
         lt = arg.as_leading_term(x) # arg = sin(x); lt = x
         a = powsimp((arg/lt).expand(), deep=True, combine='exp') # a = sin(x)/x
         # the idea is to recursively call ln(a).series(), but one needs to
         # make sure that ln(sin(x)/x) doesn't get "simplified" to
         # -log(x)+ln(sin(x)) and an infinite recursion occurs, see also the
         # issue 252.
         obj = ln(lt) + ln(a)._eval_nseries(x, x0, n)
     else:
         # arg -> arg0 + (arg - arg0) -> arg0 * (1 + (arg/arg0 - 1))
         z = (arg/arg0 - 1)
         x = order.symbols[0]
         ln = C.log
         o = C.Order(z, x)
         if o is S.Zero:
             return ln(1+z)+ ln(arg0)
         if o.expr.is_number:
             e = ln(order.expr*x)/ln(x)
         else:
             e = ln(order.expr)/ln(o.expr)
         n = e.limit(x,0) + 1
         if n.is_unbounded:
             # requested accuracy gives infinite series,
             # order is probably nonpolynomial e.g. O(exp(-1/x), x).
             return ln(1+z)+ ln(arg0)
         try:
             n = int(n)
         except TypeError:
             #well, the n is something more complicated (like 1+log(2))
             n = int(n.evalf()) + 1
         assert n>=0,`n`
         l = []
         g = None
         for i in xrange(n+2):
             g = ln.taylor_term(i, z, g)
             g = g.nseries(x, x0, n)
             l.append(g)
         obj = C.Add(*l) + ln(arg0)
     obj2 = expand_log(powsimp(obj, deep=True, combine='exp'))
     if obj2 != obj:
         r = obj2.nseries(x, x0, n)
     else:
         r = obj
     if r == self:
         return self
     return r + order