Exemple #1
0
    def _eval_expand_func(self, **hints):
        n, z = self.args

        if n.is_Integer and n.is_nonnegative:
            if z.is_Add:
                coeff = z.args[0]
                if coeff.is_Integer:
                    e = -(n + 1)
                    if coeff > 0:
                        tail = Add(*[
                            C.Pow(z - i, e) for i in xrange(1,
                                                            int(coeff) + 1)
                        ])
                    else:
                        tail = -Add(
                            *[C.Pow(z + i, e) for i in xrange(0, int(-coeff))])
                    return polygamma(
                        n, z - coeff) + (-1)**n * C.factorial(n) * tail

            elif z.is_Mul:
                coeff, z = z.as_two_terms()
                if coeff.is_Integer and coeff.is_positive:
                    tail = [
                        polygamma(n, z + C.Rational(i, coeff))
                        for i in xrange(0, int(coeff))
                    ]
                    if n == 0:
                        return Add(*tail) / coeff + log(coeff)
                    else:
                        return Add(*tail) / coeff**(n + 1)
                z *= coeff

        return polygamma(n, z)
Exemple #2
0
    def _print_Mul(self, product):
        a = []  # items in the numerator
        b = []  # items that are in the denominator (if any)

        if self.order not in ('old', 'none'):
            args = product.as_ordered_factors()
        else:
            args = product.args

        # Gather terms for numerator/denominator
        for item in args:
            if item.is_commutative and item.is_Pow and item.exp.is_Rational and item.exp.is_negative:
                b.append(C.Pow(item.base, -item.exp))
            elif item.is_Rational and item is not S.Infinity:
                if item.p != 1:
                    a.append(C.Rational(item.p))
                if item.q != 1:
                    b.append(C.Rational(item.q))
            else:
                a.append(item)

        # Convert to pretty forms. Add parens to Add instances if there
        # is more than one term in the numer/denom
        for i in xrange(0, len(a)):
            if a[i].is_Add and len(a) > 1:
                a[i] = prettyForm(*self._print(a[i]).parens())
            else:
                a[i] = self._print(a[i])

        for i in xrange(0, len(b)):
            if b[i].is_Add and len(b) > 1:
                b[i] = prettyForm(*self._print(b[i]).parens())
            else:
                b[i] = self._print(b[i])

        # Construct a pretty form
        if len(b) == 0:
            return prettyForm.__mul__(*a)
        else:
            if len(a) == 0:
                a.append(self._print(S.One))
            return prettyForm.__mul__(*a) / prettyForm.__mul__(*b)
Exemple #3
0
    def _eval_expand_func(self, deep=True, **hints):
        if deep:
            arg = self.args[0].expand(deep, **hints)
        else:
            arg = self.args[0]

        if arg.is_Add:
            coeff, tail = arg.as_coeff_add()
            if coeff and coeff.q != 1:
                tail = (C.Rational(1, coeff.q), ) + tail
                coeff = floor(coeff)
            tail = arg._new_rawargs(*tail, **dict(reeval=False))
            return gamma(tail) * C.RisingFactorial(tail, coeff)

        return self.func(*self.args)
Exemple #4
0
def cbrt(arg):
    """This function computes the principial cube root of `arg`, so
    it's just a shortcut for `arg**Rational(1, 3)`.

    Examples
    ========

    >>> from sympy import cbrt, Symbol
    >>> x = Symbol('x')

    >>> cbrt(x)
    x**(1/3)

    >>> cbrt(x)**3
    x

    Note that cbrt(x**3) does not simplify to x.

    >>> cbrt(x**3)
    (x**3)**(1/3)

    This is because the two are not equal to each other in general.
    For example, consider `x == -1`:

    >>> from sympy import Eq
    >>> Eq(cbrt(x**3), x).subs(x, -1)
    False

    This is because cbrt computes the principal cube root, this
    identity does hold if `x` is positive:

    >>> y = Symbol('y', positive=True)
    >>> cbrt(y**3)
    y

    See Also
    ========

    sympy.polys.rootoftools.RootOf, root, real_root

    References
    ==========

    * http://en.wikipedia.org/wiki/Cube_root
    * http://en.wikipedia.org/wiki/Principal_value

    """
    return C.Pow(arg, C.Rational(1, 3))
    def _eval_expand_func(self, *args):
        arg = self.args[0].expand()

        if arg.is_Add:
            for i, coeff in enumerate(arg.args[:]):
                if arg.args[i].is_Number:
                    terms = C.Add(*(arg.args[:i] + arg.args[i + 1:]))

                    if coeff.is_Rational:
                        if coeff.q != 1:
                            terms += C.Rational(1, coeff.q)
                            coeff = C.Integer(int(coeff))
                    else:
                        continue

                    return gamma(terms) * C.RisingFactorial(terms, coeff)

        return self.func(*self.args)