Esempio n. 1
0
    def eval(cls, r, k):
        r, k = map(sympify, (r, k))

        if k.is_Number:
            if k is S.Zero:
                return S.One
            elif k.is_Integer:
                if k.is_negative:
                    return S.Zero
                else:
                    if r.is_Integer and r.is_nonnegative:
                        r, k = int(r), int(k)

                        if k > r:
                            return S.Zero
                        elif k > r // 2:
                            k = r - k

                        M, result = int(sqrt(r)), 1

                        for prime in sieve.primerange(2, r+1):
                            if prime > r - k:
                                result *= prime
                            elif prime > r // 2:
                                continue
                            elif prime > M:
                                if r % prime < k % prime:
                                    result *= prime
                            else:
                                R, K = r, k
                                exp = a = 0

                                while R > 0:
                                    a = int((R % prime) < (K % prime + a))
                                    R, K = R // prime, K // prime
                                    exp = a + exp

                                if exp > 0:
                                    result *= prime**exp

                        return C.Integer(result)
                    else:
                        result = r - k + 1

                        for i in xrange(2, k+1):
                            result *= r-k+i
                            result /= i

                        return result

        if k.is_integer:
            if k.is_negative:
                return S.Zero
        else:
            return C.gamma(r+1)/(C.gamma(r-k+1)*C.gamma(k+1))
Esempio n. 2
0
    def eval(cls, r, k):
        r, k = map(sympify, (r, k))

        if k.is_Number:
            if k is S.Zero:
                return S.One
            elif k.is_Integer:
                if k.is_negative:
                    return S.Zero
                else:
                    if r.is_Integer and r.is_nonnegative:
                        r, k = int(r), int(k)

                        if k > r:
                            return S.Zero
                        elif k > r // 2:
                            k = r - k

                        M, result = int(sqrt(r)), 1

                        for prime in sieve.primerange(2, r + 1):
                            if prime > r - k:
                                result *= prime
                            elif prime > r // 2:
                                continue
                            elif prime > M:
                                if r % prime < k % prime:
                                    result *= prime
                            else:
                                R, K = r, k
                                exp = a = 0

                                while R > 0:
                                    a = int((R % prime) < (K % prime + a))
                                    R, K = R // prime, K // prime
                                    exp = a + exp

                                if exp > 0:
                                    result *= prime**exp

                        return C.Integer(result)
                    else:
                        result = r - k + 1

                        for i in xrange(2, k + 1):
                            result *= r - k + i
                            result /= i

                        return result

        if k.is_integer:
            if k.is_negative:
                return S.Zero
        else:
            return C.gamma(r + 1) / (C.gamma(r - k + 1) * C.gamma(k + 1))
Esempio n. 3
0
 def eval(cls, n, m, x):
     if m.could_extract_minus_sign():
         # P^{-m}_n  --->  F * P^m_n
         return S.NegativeOne**(-m) * (C.factorial(m + n)/C.factorial(n - m)) * assoc_legendre(n, -m, x)
     if m == 0:
         # P^0_n  --->  L_n
         return legendre(n, x)
     if x == 0:
         return 2**m*sqrt(S.Pi) / (C.gamma((1 - m - n)/2)*C.gamma(1 - (m - n)/2))
     if n.is_Number and m.is_Number and n.is_integer and m.is_integer:
         if n.is_negative:
             raise ValueError("%s : 1st index must be nonnegative integer (got %r)" % (cls, n))
         if abs(m) > n:
             raise ValueError("%s : abs('2nd index') must be <= '1st index' (got %r, %r)" % (cls, n, m))
         return cls._eval_at_order(int(n), abs(int(m))).subs(_x, x)
Esempio n. 4
0
    def eval(cls, n):
        n = sympify(n)

        if n.is_Number:
            if n is S.Zero:
                return S.One
            elif n.is_Integer:
                if n.is_negative:
                    return S.Zero
                else:
                    n, result = n.p, 1

                    if n < 20:
                        for i in range(2, n+1):
                            result *= i
                    else:
                        N, bits = n, 0

                        while N != 0:
                            if N & 1 == 1:
                                bits += 1

                            N = N >> 1

                        result = cls._recursive(n)*2**(n-bits)

                    return C.Integer(result)

        if n.is_integer:
            if n.is_negative:
                return S.Zero
        else:
            return C.gamma(n+1)
Esempio n. 5
0
    def eval(cls, n):
        n = sympify(n)

        if n.is_Number:
            if n is S.Zero:
                return S.One
            elif n.is_Integer:
                if n.is_negative:
                    return S.Zero
                else:
                    n, result = n.p, 1

                    if n < 20:
                        for i in range(2, n + 1):
                            result *= i
                    else:
                        N, bits = n, 0

                        while N != 0:
                            if N & 1 == 1:
                                bits += 1

                            N = N >> 1

                        result = cls._recursive(n) * 2**(n - bits)

                    return C.Integer(result)

        if n.is_integer:
            if n.is_negative:
                return S.Zero
        else:
            return C.gamma(n + 1)
Esempio n. 6
0
    def eval(cls, n, a, x):
        # For negative n the polynomials vanish
        # See http://functions.wolfram.com/Polynomials/GegenbauerC3/03/01/03/0012/
        if n.is_negative:
            return S.Zero

        # Some special values for fixed a
        if a == S.Half:
            return legendre(n, x)
        elif a == S.One:
            return chebyshevu(n, x)
        elif a == S.NegativeOne:
            return S.Zero

        if not n.is_Number:
            # Handle this before the general sign extraction rule
            if x == S.NegativeOne:
                if (C.re(a) > S.Half) == True:
                    return S.ComplexInfinity
                else:
                    # No sec function available yet
                    # return (C.cos(S.Pi*(a+n)) * C.sec(S.Pi*a) * C.gamma(2*a+n) /
                    #            (C.gamma(2*a) * C.gamma(n+1)))
                    return None

            # Symbolic result C^a_n(x)
            # C^a_n(-x)  --->  (-1)**n * C^a_n(x)
            if x.could_extract_minus_sign():
                return S.NegativeOne ** n * gegenbauer(n, a, -x)
            # We can evaluate for some special values of x
            if x == S.Zero:
                return (
                    2 ** n * sqrt(S.Pi) * C.gamma(a + S.Half * n) / (C.gamma((1 - n) / 2) * C.gamma(n + 1) * C.gamma(a))
                )
            if x == S.One:
                return C.gamma(2 * a + n) / (C.gamma(2 * a) * C.gamma(n + 1))
            elif x == S.Infinity:
                if n.is_positive:
                    return C.RisingFactorial(a, n) * S.Infinity
        else:
            # n is a given fixed integer, evaluate into polynomial
            return gegenbauer_poly(n, a, x)
Esempio n. 7
0
 def eval(cls, n, x):
     if not n.is_Number:
         # Symbolic result L_n(x)
         # L_n(-x)  --->  (-1)**n * L_n(x)
         if x.could_extract_minus_sign():
             return S.NegativeOne**n * legendre(n,-x)
         # L_{-n}(x)  --->  L_{n-1}(x)
         if n.could_extract_minus_sign():
             return legendre(-n - S.One,x)
         # We can evaluate for some special values of x
         if x == S.Zero:
             return sqrt(S.Pi)/(C.gamma(S.Half - n/2)*C.gamma(S.One + n/2))
         elif x == S.One:
             return S.One
         elif x == S.Infinity:
             return S.Infinity
     else:
         # n is a given fixed integer, evaluate into polynomial
         if n.is_negative:
             raise ValueError("The index n must be nonnegative integer (got %r)" % n)
         else:
             return cls._eval_at_order(n, x)
Esempio n. 8
0
 def eval(cls, n, x):
     if not n.is_Number:
         # Symbolic result L_n(x)
         # L_n(-x)  --->  (-1)**n * L_n(x)
         if x.could_extract_minus_sign():
             return S.NegativeOne**n * legendre(n,-x)
         # L_{-n}(x)  --->  L_{n-1}(x)
         if n.could_extract_minus_sign():
             return legendre(-n - S.One,x)
         # We can evaluate for some special values of x
         if x == S.Zero:
             return sqrt(S.Pi)/(C.gamma(S.Half - n/2)*C.gamma(S.One + n/2))
         elif x == S.One:
             return S.One
         elif x == S.Infinity:
             return S.Infinity
     else:
         # n is a given fixed integer, evaluate into polynomial
         if n.is_negative:
             raise ValueError("The index n must be nonnegative integer (got %r)" % n)
         else:
             return cls._eval_at_order(n, x)
Esempio n. 9
0
 def eval(cls, n, x):
     if not n.is_Number:
         # Symbolic result H_n(x)
         # H_n(-x)  --->  (-1)**n * H_n(x)
         if x.could_extract_minus_sign():
             return S.NegativeOne**n * hermite(n,-x)
         # We can evaluate for some special values of x
         if x == S.Zero:
             return 2**n * sqrt(S.Pi) / C.gamma((S.One - n)/2)
         elif x == S.Infinity:
             return S.Infinity
     else:
         # n is a given fixed integer, evaluate into polynomial
         if n.is_negative:
             raise ValueError("The index n must be nonnegative integer (got %r)" % n)
         else:
             return cls._eval_at_order(n, x)
Esempio n. 10
0
 def eval(cls, n, x):
     if not n.is_Number:
         # Symbolic result H_n(x)
         # H_n(-x)  --->  (-1)**n * H_n(x)
         if x.could_extract_minus_sign():
             return S.NegativeOne**n * hermite(n, -x)
         # We can evaluate for some special values of x
         if x == S.Zero:
             return 2**n * sqrt(S.Pi) / C.gamma((S.One - n) / 2)
         elif x == S.Infinity:
             return S.Infinity
     else:
         # n is a given fixed integer, evaluate into polynomial
         if n.is_negative:
             raise ValueError(
                 "The index n must be nonnegative integer (got %r)" % n)
         else:
             return cls._eval_at_order(n, x)
Esempio n. 11
0
    def eval(cls, n, a, x):
        # For negative n the polynomials vanish
        # See http://functions.wolfram.com/Polynomials/GegenbauerC3/03/01/03/0012/
        if n.is_negative:
            return S.Zero

        # Some special values for fixed a
        if a == S.Half:
            return legendre(n, x)
        elif a == S.One:
            return chebyshevu(n, x)
        elif a == S.NegativeOne:
            return S.Zero

        if not n.is_Number:
            # Handle this before the general sign extraction rule
            if x == S.NegativeOne:
                if (C.re(a) > S.Half) is True:
                    return S.ComplexInfinity
                else:
                    # No sec function available yet
                    #return (C.cos(S.Pi*(a+n)) * C.sec(S.Pi*a) * C.gamma(2*a+n) /
                    #            (C.gamma(2*a) * C.gamma(n+1)))
                    return None

            # Symbolic result C^a_n(x)
            # C^a_n(-x)  --->  (-1)**n * C^a_n(x)
            if x.could_extract_minus_sign():
                return S.NegativeOne**n * gegenbauer(n, a, -x)
            # We can evaluate for some special values of x
            if x == S.Zero:
                return (2**n * sqrt(S.Pi) * C.gamma(a + S.Half * n) / (C.gamma(
                    (1 - n) / 2) * C.gamma(n + 1) * C.gamma(a)))
            if x == S.One:
                return C.gamma(2 * a + n) / (C.gamma(2 * a) * C.gamma(n + 1))
            elif x == S.Infinity:
                if n.is_positive:
                    return C.RisingFactorial(a, n) * S.Infinity
        else:
            # n is a given fixed integer, evaluate into polynomial
            return gegenbauer_poly(n, a, x)
Esempio n. 12
0
    def eval(cls, n, a, b, x):
        # Simplify to other polynomials
        # P^{a, a}_n(x)
        if a == b:
            if a == -S.Half:
                return C.RisingFactorial(S.Half, n) / C.factorial(n) * chebyshevt(n, x)
            elif a == S.Zero:
                return legendre(n, x)
            elif a == S.Half:
                return C.RisingFactorial(3 * S.Half, n) / C.factorial(n + 1) * chebyshevu(n, x)
            else:
                return C.RisingFactorial(a + 1, n) / C.RisingFactorial(2 * a + 1, n) * gegenbauer(n, a + S.Half, x)
        elif b == -a:
            # P^{a, -a}_n(x)
            return (
                C.gamma(n + a + 1) / C.gamma(n + 1) * (1 + x) ** (a / 2) / (1 - x) ** (a / 2) * assoc_legendre(n, -a, x)
            )
        elif a == -b:
            # P^{-b, b}_n(x)
            return (
                C.gamma(n - b + 1) / C.gamma(n + 1) * (1 - x) ** (b / 2) / (1 + x) ** (b / 2) * assoc_legendre(n, b, x)
            )

        if not n.is_Number:
            # Symbolic result P^{a,b}_n(x)
            # P^{a,b}_n(-x)  --->  (-1)**n * P^{b,a}_n(-x)
            if x.could_extract_minus_sign():
                return S.NegativeOne ** n * jacobi(n, b, a, -x)
            # We can evaluate for some special values of x
            if x == S.Zero:
                return (
                    2 ** (-n)
                    * C.gamma(a + n + 1)
                    / (C.gamma(a + 1) * C.factorial(n))
                    * C.hyper([-b - n, -n], [a + 1], -1)
                )
            if x == S.One:
                return C.RisingFactorial(a + 1, n) / C.factorial(n)
            elif x == S.Infinity:
                if n.is_positive:
                    # TODO: Make sure a+b+2*n \notin Z
                    return C.RisingFactorial(a + b + n + 1, n) * S.Infinity
        else:
            # n is a given fixed integer, evaluate into polynomial
            return jacobi_poly(n, a, b, x)
Esempio n. 13
0
    def eval(cls, n, a, b, x):
        # Simplify to other polynomials
        # P^{a, a}_n(x)
        if a == b:
            if a == -S.Half:
                return C.RisingFactorial(
                    S.Half, n) / C.factorial(n) * chebyshevt(n, x)
            elif a == S.Zero:
                return legendre(n, x)
            elif a == S.Half:
                return C.RisingFactorial(
                    3 * S.Half, n) / C.factorial(n + 1) * chebyshevu(n, x)
            else:
                return C.RisingFactorial(a + 1, n) / C.RisingFactorial(
                    2 * a + 1, n) * gegenbauer(n, a + S.Half, x)
        elif b == -a:
            # P^{a, -a}_n(x)
            return C.gamma(n + a + 1) / C.gamma(n + 1) * (1 + x)**(a / 2) / (
                1 - x)**(a / 2) * assoc_legendre(n, -a, x)
        elif a == -b:
            # P^{-b, b}_n(x)
            return C.gamma(n - b + 1) / C.gamma(n + 1) * (1 - x)**(b / 2) / (
                1 + x)**(b / 2) * assoc_legendre(n, b, x)

        if not n.is_Number:
            # Symbolic result P^{a,b}_n(x)
            # P^{a,b}_n(-x)  --->  (-1)**n * P^{b,a}_n(-x)
            if x.could_extract_minus_sign():
                return S.NegativeOne**n * jacobi(n, b, a, -x)
            # We can evaluate for some special values of x
            if x == S.Zero:
                return (2**(-n) * C.gamma(a + n + 1) /
                        (C.gamma(a + 1) * C.factorial(n)) *
                        C.hyper([-b - n, -n], [a + 1], -1))
            if x == S.One:
                return C.RisingFactorial(a + 1, n) / C.factorial(n)
            elif x == S.Infinity:
                if n.is_positive:
                    # TODO: Make sure a+b+2*n \notin Z
                    return C.RisingFactorial(a + b + n + 1, n) * S.Infinity
        else:
            # n is a given fixed integer, evaluate into polynomial
            return jacobi_poly(n, a, b, x)
Esempio n. 14
0
 def _eval_rewrite_as_polynomial(self, n, x):
     # TODO: Should make sure n is in N_0
     k = C.Dummy("k")
     kern = C.RisingFactorial(
         -n, k) / (C.gamma(k + alpha + 1) * C.factorial(k)) * x**k
     return C.gamma(n + alpha + 1) / C.factorial(n) * C.Sum(kern, (k, 0, n))
Esempio n. 15
0
 def _eval_rewrite_as_gamma(self, arg):
     return C.gamma(1 + arg)
Esempio n. 16
0
 def _eval_rewrite_as_polynomial(self, n, x):
     # TODO: Should make sure n is in N_0
     k = C.Dummy("k")
     kern = C.RisingFactorial(
         -n, k) / (C.gamma(k + alpha + 1) * C.factorial(k)) * x**k
     return C.gamma(n + alpha + 1) / C.factorial(n) * C.Sum(kern, (k, 0, n))
Esempio n. 17
0
 def _eval_rewrite_as_gamma(self, x, k):
     return C.gamma(x + k) / C.gamma(x)
Esempio n. 18
0
 def _eval_rewrite_as_gamma(self, r, k):
     return C.gamma(r+1) / (C.gamma(r-k+1)*C.gamma(k+1))
Esempio n. 19
0
 def _eval_rewrite_as_gamma(self, x, k):
     return (-1)**k * C.gamma(-x + k) / C.gamma(-x)
Esempio n. 20
0
 def _eval_rewrite_as_gamma(self, x, k):
     return C.gamma(x + k) / C.gamma(x)
Esempio n. 21
0
 def _eval_rewrite_as_gamma(self, arg):
     return C.gamma(1 + arg)
Esempio n. 22
0
 def _eval_rewrite_as_gamma(self, x, k):
     return (-1)**k * C.gamma(-x + k) / C.gamma(-x)
Esempio n. 23
0
 def _eval_rewrite_as_gamma(self, r, k):
     return C.gamma(r + 1) / (C.gamma(r - k + 1) * C.gamma(k + 1))