Ejemplo n.º 1
0
def test_special_products():
    # Wallis product
    assert product((4*k)**2 / (4*k**2 - 1), (k, 1, n)) == \
        4**n*factorial(n)**2/rf(Rational(1, 2), n)/rf(Rational(3, 2), n)

    # Euler's product formula for sin
    assert product(1 + a/k**2, (k, 1, n)) == \
        rf(1 - sqrt(-a), n)*rf(1 + sqrt(-a), n)/factorial(n)**2
Ejemplo n.º 2
0
def test_special_products():
    # Wallis product
    assert product((4*k)**2 / (4*k**2-1), (k, 1, n)) == \
        4**n*factorial(n)**2/rf(Rational(1, 2), n)/rf(Rational(3, 2), n)

    # Euler's product formula for sin
    assert product(1 + a/k**2, (k, 1, n)) == \
        rf(1 - sqrt(-a), n)*rf(1 + sqrt(-a), n)/factorial(n)**2
Ejemplo n.º 3
0
def test_rf_ff_eval_hiprec():
    maple = Float('6.9109401292234329956525265438452')
    us = ff(18, S(2)/3).evalf(32)
    assert abs(us - maple)/us < 1e-31

    maple = Float('6.8261540131125511557924466355367')
    us = rf(18, S(2)/3).evalf(32)
    assert abs(us - maple)/us < 1e-31

    maple = Float('34.007346127440197150854651814225')
    us = rf(Float('4.4', 32), Float('2.2', 32));
    assert abs(us - maple)/us < 1e-31
Ejemplo n.º 4
0
def test_rf_ff_eval_hiprec():
    maple = Float('6.9109401292234329956525265438452')
    us = ff(18, Rational(2, 3)).evalf(32)
    assert abs(us - maple) / us < 1e-31

    maple = Float('6.8261540131125511557924466355367')
    us = rf(18, Rational(2, 3)).evalf(32)
    assert abs(us - maple) / us < 1e-31

    maple = Float('34.007346127440197150854651814225')
    us = rf(Float('4.4', 32), Float('2.2', 32))
    assert abs(us - maple) / us < 1e-31
Ejemplo n.º 5
0
def test_rsolve_hyper():
    assert rsolve_hyper([-1, -1, 1], 0, n) in [
        C0 * (S.Half - S.Half * sqrt(5)) ** n + C1 * (S.Half + S.Half * sqrt(5)) ** n,
        C1 * (S.Half - S.Half * sqrt(5)) ** n + C0 * (S.Half + S.Half * sqrt(5)) ** n,
    ]

    assert rsolve_hyper([n ** 2 - 2, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(2), n) + C1 * rf(-sqrt(2), n),
        C1 * rf(sqrt(2), n) + C0 * rf(-sqrt(2), n),
    ]

    assert rsolve_hyper([n ** 2 - k, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(k), n) + C1 * rf(-sqrt(k), n),
        C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n),
    ]

    assert rsolve_hyper([2 * n * (n + 1), -n ** 2 - 3 * n + 2, n - 1], 0, n) == C1 * factorial(n) + C0 * 2 ** n

    assert rsolve_hyper([n + 2, -(2 * n + 3) * (17 * n ** 2 + 51 * n + 39), n + 1], 0, n) == 0

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n ** 2 / 2 - n / 2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n ** 2 / 2 + n / 2

    assert rsolve_hyper([-1, 1], 3 * (n + n ** 2), n).expand() == C0 + n ** 3 - n
Ejemplo n.º 6
0
def test_rsolve_hyper():
    assert rsolve_hyper(
        [-1, -1, 1], 0,
        n) == C0 * (S.Half + S.Half * sqrt(5))**n + C1 * (S.Half -
                                                          S.Half * sqrt(5))**n

    assert rsolve_hyper([n**2 - 2, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(2), n) + C1 * rf(-sqrt(2), n),
        C1 * rf(sqrt(2), n) + C0 * rf(-sqrt(2), n)
    ]

    assert rsolve_hyper([n**2 - k, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(k), n) + C1 * rf(-sqrt(k), n),
        C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n)
    ]

    assert rsolve_hyper([2 * n * (n + 1), -n**2 - 3 * n + 2, n - 1], 0,
                        n) == C0 * factorial(n) + C1 * 2**n

    assert rsolve_hyper(
        [n + 2, -(2 * n + 3) * (17 * n**2 + 51 * n + 39), n + 1], 0, n) == 0

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == 0

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2 / 2 - n / 2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2 / 2 + n / 2

    assert rsolve_hyper([-1, 1], 3 * (n + n**2), n).expand() == C0 + n**3 - n
Ejemplo n.º 7
0
def test_issue_9699():
    n, k = symbols('n k', real=True)
    x, y = symbols('x, y')
    assert combsimp((n + 1)*factorial(n)) == factorial(n + 1)
    assert combsimp((x + 1)*factorial(x)/gamma(y)) == gamma(x + 2)/gamma(y)
    assert combsimp(factorial(n)/n) == factorial(n - 1)
    assert combsimp(rf(x + n, k)*binomial(n, k)) == binomial(n, k)*gamma(k + n + x)/gamma(n + x)
Ejemplo n.º 8
0
def test_simple_products():
    assert product(2, (k, a, n)) == 2**(n - a + 1)
    assert product(k, (k, 1, n)) == factorial(n)
    assert product(k**3, (k, 1, n)) == factorial(n)**3

    assert product(k + 1, (k, 0, n - 1)) == factorial(n)
    assert product(k + 1, (k, a, n - 1)) == rf(1 + a, n - a)

    assert product(cos(k), (k, 0, 5)) == cos(1)*cos(2)*cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 1, Rational(5, 2))) != cos(1)*cos(2)

    assert isinstance(product(k**k, (k, 1, n)), Product)

    assert Product(x**k, (k, 1, n)).variables == [k]

    raises(ValueError, lambda: Product(n))
    raises(ValueError, lambda: Product(n, k))
    raises(ValueError, lambda: Product(n, k, 1))
    raises(ValueError, lambda: Product(n, k, 1, 10))
    raises(ValueError, lambda: Product(n, (k, 1)))

    assert product(1, (n, 1, oo)) == 1  # issue 8301
    assert product(2, (n, 1, oo)) == oo
    assert product(-1, (n, 1, oo)).func is Product
Ejemplo n.º 9
0
def test_simple_products():
    assert product(2, (k, a, n)) == 2**(n - a + 1)
    assert product(k, (k, 1, n)) == factorial(n)
    assert product(k**3, (k, 1, n)) == factorial(n)**3

    assert product(k + 1, (k, 0, n - 1)) == factorial(n)
    assert product(k + 1, (k, a, n - 1)) == rf(1 + a, n - a)

    assert product(cos(k), (k, 0, 5)) == cos(1)*cos(2)*cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 1, Rational(5, 2))) != cos(1)*cos(2)

    assert isinstance(product(k**k, (k, 1, n)), Product)

    assert Product(x**k, (k, 1, n)).variables == [k]

    raises(ValueError, lambda: Product(n))
    raises(ValueError, lambda: Product(n, k))
    raises(ValueError, lambda: Product(n, k, 1))
    raises(ValueError, lambda: Product(n, k, 1, 10))
    raises(ValueError, lambda: Product(n, (k, 1)))

    assert product(1, (n, 1, oo)) == 1  # issue 8301
    assert product(2, (n, 1, oo)) is oo
    assert product(-1, (n, 1, oo)).func is Product
Ejemplo n.º 10
0
def test_rf_lambdify_mpmath():
    from sympy import lambdify
    x, y = symbols('x,y')
    f = lambdify((x,y), rf(x, y), 'mpmath')
    maple = Float('34.007346127440197')
    us = f(4.4, 2.2)
    assert abs(us - maple)/us < 1e-15
Ejemplo n.º 11
0
 def pdf(self, *syms):
     n, theta = self.n, self.theta
     term_1 = factorial(n)/rf(theta, n)
     term_2 = Mul.fromiter([theta**syms[j]/((j+1)**syms[j]*factorial(syms[j]))
                         for j in range(n)])
     cond = Eq(sum([(k+1)*syms[k] for k in range(n)]), n)
     return Piecewise((term_1 * term_2, cond), (0, True))
Ejemplo n.º 12
0
def test_rf_lambdify_mpmath():
    from sympy import lambdify
    x, y = symbols('x,y')
    f = lambdify((x, y), rf(x, y), 'mpmath')
    maple = Float('34.007346127440197')
    us = f(4.4, 2.2)
    assert abs(us - maple) / us < 1e-15
Ejemplo n.º 13
0
def test_ff_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert ff(nan, y) == nan
    assert ff(x, nan) == nan

    assert ff(x, y) == ff(x, y)

    assert ff(oo, 0) == 1
    assert ff(-oo, 0) == 1

    assert ff(oo, 6) == oo
    assert ff(-oo, 7) == -oo

    assert ff(oo, -6) == oo
    assert ff(-oo, -7) == oo

    assert ff(x, 0) == 1
    assert ff(x, 1) == x
    assert ff(x, 2) == x * (x - 1)
    assert ff(x, 3) == x * (x - 1) * (x - 2)
    assert ff(x, 5) == x * (x - 1) * (x - 2) * (x - 3) * (x - 4)

    assert ff(x, -1) == 1 / (x + 1)
    assert ff(x, -2) == 1 / ((x + 1) * (x + 2))
    assert ff(x, -3) == 1 / ((x + 1) * (x + 2) * (x + 3))

    assert ff(100, 100) == factorial(100)

    assert ff(2 * x**2 - 5 * x,
              2) == (2 * x**2 - 5 * x) * (2 * x**2 - 5 * x - 1)
    assert isinstance(ff(2 * x**2 - 5 * x, 2), Mul)
    assert ff(x**2 + 3 * x,
              -2) == 1 / ((x**2 + 3 * x + 1) * (x**2 + 3 * x + 2))

    assert ff(Poly(2 * x**2 - 5 * x, x),
              2) == Poly(4 * x**4 - 28 * x**3 + 59 * x**2 - 35 * x, x)
    assert isinstance(ff(Poly(2 * x**2 - 5 * x, x), 2), Poly)
    raises(ValueError, lambda: ff(Poly(2 * x**2 - 5 * x, x, y), 2))
    assert ff(Poly(x**2 + 3 * x, x),
              -2) == 1 / (x**4 + 12 * x**3 + 49 * x**2 + 78 * x + 40)
    raises(ValueError, lambda: ff(Poly(x**2 + 3 * x, x, y), -2))

    assert ff(x, m).is_integer is None
    assert ff(n, k).is_integer is None
    assert ff(n, m).is_integer is True
    assert ff(n, k + pi).is_integer is False
    assert ff(n, m + pi).is_integer is False
    assert ff(pi, m).is_integer is False

    assert isinstance(ff(x, x), ff)
    assert ff(n, n) == factorial(n)

    assert ff(x, k).rewrite(rf) == rf(x - k + 1, k)
    assert ff(x, k).rewrite(gamma) == (-1)**k * gamma(k - x) / gamma(-x)
    assert ff(n, k).rewrite(factorial) == factorial(n) / factorial(n - k)
    assert ff(x, k).rewrite(binomial) == factorial(k) * binomial(x, k)
Ejemplo n.º 14
0
def test_rf_lambdify_mpmath():
    from sympy import lambdify

    x, y = symbols("x,y")
    f = lambdify((x, y), rf(x, y), "mpmath")
    maple = Float("34.007346127440197")
    us = f(4.4, 2.2)
    assert abs(us - maple) / us < 1e-15
Ejemplo n.º 15
0
def test_ff_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert ff(nan, y) == nan
    assert ff(x, nan) == nan

    assert ff(x, y) == ff(x, y)

    assert ff(oo, 0) == 1
    assert ff(-oo, 0) == 1

    assert ff(oo, 6) == oo
    assert ff(-oo, 7) == -oo

    assert ff(oo, -6) == oo
    assert ff(-oo, -7) == oo

    assert ff(x, 0) == 1
    assert ff(x, 1) == x
    assert ff(x, 2) == x*(x - 1)
    assert ff(x, 3) == x*(x - 1)*(x - 2)
    assert ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4)

    assert ff(x, -1) == 1/(x + 1)
    assert ff(x, -2) == 1/((x + 1)*(x + 2))
    assert ff(x, -3) == 1/((x + 1)*(x + 2)*(x + 3))

    assert ff(100, 100) == factorial(100)

    assert ff(2*x**2 - 5*x, 2) == (2*x**2  - 5*x)*(2*x**2 - 5*x - 1)
    assert isinstance(ff(2*x**2 - 5*x, 2), Mul)
    assert ff(x**2 + 3*x, -2) == 1/((x**2 + 3*x + 1)*(x**2 + 3*x + 2))

    assert ff(Poly(2*x**2 - 5*x, x), 2) == Poly(4*x**4 - 28*x**3 + 59*x**2 - 35*x, x)
    assert isinstance(ff(Poly(2*x**2 - 5*x, x), 2), Poly)
    raises(ValueError, lambda: ff(Poly(2*x**2 - 5*x, x, y), 2))
    assert ff(Poly(x**2 + 3*x, x), -2) == 1/(x**4 + 12*x**3 + 49*x**2 + 78*x + 40)
    raises(ValueError, lambda: ff(Poly(x**2 + 3*x, x, y), -2))


    assert ff(x, m).is_integer is None
    assert ff(n, k).is_integer is None
    assert ff(n, m).is_integer is True
    assert ff(n, k + pi).is_integer is False
    assert ff(n, m + pi).is_integer is False
    assert ff(pi, m).is_integer is False

    assert isinstance(ff(x, x), ff)
    assert ff(n, n) == factorial(n)

    assert ff(x, k).rewrite(rf) == rf(x - k + 1, k)
    assert ff(x, k).rewrite(gamma) == (-1)**k*gamma(k - x) / gamma(-x)
    assert ff(n, k).rewrite(factorial) == factorial(n) / factorial(n - k)
    assert ff(x, k).rewrite(binomial) == factorial(k) * binomial(x, k)
Ejemplo n.º 16
0
def test_issue_9699():
    n, k = symbols('n k', real=True)
    x, y = symbols('x, y')
    assert combsimp((n + 1) * factorial(n)) == factorial(n + 1)
    assert combsimp(
        (x + 1) * factorial(x) / gamma(y)) == gamma(x + 2) / gamma(y)
    assert combsimp(factorial(n) / n) == factorial(n - 1)
    assert combsimp(
        rf(x + n, k) *
        binomial(n, k)) == binomial(n, k) * gamma(k + n + x) / gamma(n + x)
Ejemplo n.º 17
0
def test_simple_products():
    assert product(2, (k, a, n)) == 2**(n-a+1)
    assert product(k, (k, 1, n)) == factorial(n)
    assert product(k**3, (k, 1, n)) == factorial(n)**3

    assert product(k+1, (k, 0, n-1)) == factorial(n)
    assert product(k+1, (k, a, n-1)) == rf(1+a, n-a)

    assert product(cos(k), (k, 0, 5)) == cos(1)*cos(2)*cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(k), (k, 1, Rational(5, 2))) == cos(1)*cos(2)

    assert isinstance(product(k**k, (k, 1, n)), Product)
Ejemplo n.º 18
0
def test_rsolve_hyper():
    assert rsolve_hyper([-1, -1, 1], 0, n) in [
        C0 * (S.Half - S.Half * sqrt(5))**n + C1 *
        (S.Half + S.Half * sqrt(5))**n,
        C1 * (S.Half - S.Half * sqrt(5))**n + C0 *
        (S.Half + S.Half * sqrt(5))**n,
    ]

    assert rsolve_hyper([n**2 - 2, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(2), n) + C1 * rf(-sqrt(2), n),
        C1 * rf(sqrt(2), n) + C0 * rf(-sqrt(2), n),
    ]

    assert rsolve_hyper([n**2 - k, -2 * n - 1, 1], 0, n) in [
        C0 * rf(sqrt(k), n) + C1 * rf(-sqrt(k), n),
        C1 * rf(sqrt(k), n) + C0 * rf(-sqrt(k), n),
    ]

    assert (rsolve_hyper([2 * n * (n + 1), -(n**2) - 3 * n + 2, n - 1], 0,
                         n) == C1 * factorial(n) + C0 * 2**n)

    assert (rsolve_hyper(
        [n + 2, -(2 * n + 3) * (17 * n**2 + 51 * n + 39), n + 1], 0,
        n) == None)

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == None

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2 / 2 - n / 2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2 / 2 + n / 2

    assert rsolve_hyper([-1, 1], 3 * (n + n**2), n).expand() == C0 + n**3 - n

    assert rsolve_hyper([-a, 1], 0, n).expand() == C0 * a**n

    assert rsolve_hyper(
        [-a, 0, 1], 0,
        n).expand() == (-1)**n * C1 * a**(n / 2) + C0 * a**(n / 2)

    assert (rsolve_hyper([1, 1, 1], 0, n).expand() == C0 *
            (Rational(-1, 2) - sqrt(3) * I / 2)**n + C1 *
            (Rational(-1, 2) + sqrt(3) * I / 2)**n)

    assert rsolve_hyper([1, -2 * n / a - 2 / a, 1], 0, n) is None
Ejemplo n.º 19
0
 def pdf(self, *syms):
     n, theta = self.n, self.theta
     condi = isinstance(self.n, Integer)
     if not (isinstance(syms[0], IndexedBase) or condi):
         raise ValueError("Please use IndexedBase object for syms as "
                             "the dimension is symbolic")
     term_1 = factorial(n)/rf(theta, n)
     if condi:
         term_2 = Mul.fromiter([theta**syms[j]/((j+1)**syms[j]*factorial(syms[j]))
                                 for j in range(n)])
         cond = Eq(sum([(k + 1)*syms[k] for k in range(n)]), n)
         return Piecewise((term_1 * term_2, cond), (0, True))
     syms = syms[0]
     j, k = symbols('j, k', positive=True, integer=True)
     term_2 = Product(theta**syms[j]/((j+1)**syms[j]*factorial(syms[j])),
                         (j, 0, n - 1))
     cond = Eq(Sum((k + 1)*syms[k], (k, 0, n - 1)), n)
     return Piecewise((term_1 * term_2, cond), (0, True))
Ejemplo n.º 20
0
def test_rsolve_hyper():
    assert rsolve_hyper([-1, -1, 1], 0, n) in [
        C0*(S.Half - S.Half*sqrt(5))**n + C1*(S.Half + S.Half*sqrt(5))**n,
        C1*(S.Half - S.Half*sqrt(5))**n + C0*(S.Half + S.Half*sqrt(5))**n,
    ]

    assert rsolve_hyper([n**2 - 2, -2*n - 1, 1], 0, n) in [
        C0*rf(sqrt(2), n) + C1*rf(-sqrt(2), n),
        C1*rf(sqrt(2), n) + C0*rf(-sqrt(2), n),
    ]

    assert rsolve_hyper([n**2 - k, -2*n - 1, 1], 0, n) in [
        C0*rf(sqrt(k), n) + C1*rf(-sqrt(k), n),
        C1*rf(sqrt(k), n) + C0*rf(-sqrt(k), n),
    ]

    assert rsolve_hyper(
        [2*n*(n + 1), -n**2 - 3*n + 2, n - 1], 0, n) == C1*factorial(n) + C0*2**n

    assert rsolve_hyper(
        [n + 2, -(2*n + 3)*(17*n**2 + 51*n + 39), n + 1], 0, n) == None

    assert rsolve_hyper([-n - 1, -1, 1], 0, n) == None

    assert rsolve_hyper([-1, 1], n, n).expand() == C0 + n**2/2 - n/2

    assert rsolve_hyper([-1, 1], 1 + n, n).expand() == C0 + n**2/2 + n/2

    assert rsolve_hyper([-1, 1], 3*(n + n**2), n).expand() == C0 + n**3 - n

    assert rsolve_hyper([-a, 1],0,n).expand() == C0*a**n

    assert rsolve_hyper([-a, 0, 1], 0, n).expand() == (-1)**n*C1*a**(n/2) + C0*a**(n/2)

    assert rsolve_hyper([1, 1, 1], 0, n).expand() == \
        C0*(-S(1)/2 - sqrt(3)*I/2)**n + C1*(-S(1)/2 + sqrt(3)*I/2)**n

    assert rsolve_hyper([1, -2*n/a - 2/a, 1], 0, n) == None
Ejemplo n.º 21
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x*(x + 1)
    assert rf(x, 3) == x*(x + 1)*(x + 2)
    assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)

    assert rf(x, -1) == 1/(x - 1)
    assert rf(x, -2) == 1/((x - 1)*(x - 2))
    assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))

    assert rf(1, 100) == factorial(100)

    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)
    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False
Ejemplo n.º 22
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)
Ejemplo n.º 23
0
def test_ff_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert ff(nan, y) is nan
    assert ff(x, nan) is nan

    assert unchanged(ff, x, y)

    assert ff(oo, 0) == 1
    assert ff(-oo, 0) == 1

    assert ff(oo, 6) is oo
    assert ff(-oo, 7) is -oo
    assert ff(-oo, 6) is oo

    assert ff(oo, -6) is oo
    assert ff(-oo, -7) is oo

    assert ff(x, 0) == 1
    assert ff(x, 1) == x
    assert ff(x, 2) == x * (x - 1)
    assert ff(x, 3) == x * (x - 1) * (x - 2)
    assert ff(x, 5) == x * (x - 1) * (x - 2) * (x - 3) * (x - 4)

    assert ff(x, -1) == 1 / (x + 1)
    assert ff(x, -2) == 1 / ((x + 1) * (x + 2))
    assert ff(x, -3) == 1 / ((x + 1) * (x + 2) * (x + 3))

    assert ff(100, 100) == factorial(100)

    assert ff(2 * x**2 - 5 * x,
              2) == (2 * x**2 - 5 * x) * (2 * x**2 - 5 * x - 1)
    assert isinstance(ff(2 * x**2 - 5 * x, 2), Mul)
    assert ff(x**2 + 3 * x,
              -2) == 1 / ((x**2 + 3 * x + 1) * (x**2 + 3 * x + 2))

    assert ff(Poly(2 * x**2 - 5 * x, x),
              2) == Poly(4 * x**4 - 28 * x**3 + 59 * x**2 - 35 * x, x)
    assert isinstance(ff(Poly(2 * x**2 - 5 * x, x), 2), Poly)
    raises(ValueError, lambda: ff(Poly(2 * x**2 - 5 * x, x, y), 2))
    assert ff(Poly(x**2 + 3 * x, x),
              -2) == 1 / (x**4 + 12 * x**3 + 49 * x**2 + 78 * x + 40)
    raises(ValueError, lambda: ff(Poly(x**2 + 3 * x, x, y), -2))

    assert ff(x, m).is_integer is None
    assert ff(n, k).is_integer is None
    assert ff(n, m).is_integer is True
    assert ff(n, k + pi).is_integer is False
    assert ff(n, m + pi).is_integer is False
    assert ff(pi, m).is_integer is False

    assert isinstance(ff(x, x), ff)
    assert ff(n, n) == factorial(n)

    assert ff(x, k).rewrite(rf) == rf(x - k + 1, k)
    assert ff(x, k).rewrite(gamma) == (-1)**k * gamma(k - x) / gamma(-x)
    assert ff(n, k).rewrite(factorial) == factorial(n) / factorial(n - k)
    assert ff(x, k).rewrite(binomial) == factorial(k) * binomial(x, k)
    assert ff(x, y).rewrite(factorial) == ff(x, y)
    assert ff(x, y).rewrite(binomial) == ff(x, y)

    import random
    from mpmath import ff as mpmath_ff
    for i in range(100):
        x = -500 + 500 * random.random()
        k = -500 + 500 * random.random()
        assert (abs(mpmath_ff(x, k) - ff(x, k)) < 10**(-15))
def test_ff_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert ff(nan, y) is nan
    assert ff(x, nan) is nan

    assert unchanged(ff, x, y)

    assert ff(oo, 0) == 1
    assert ff(-oo, 0) == 1

    assert ff(oo, 6) is oo
    assert ff(-oo, 7) is -oo
    assert ff(-oo, 6) is oo

    assert ff(oo, -6) is oo
    assert ff(-oo, -7) is oo

    assert ff(x, 0) == 1
    assert ff(x, 1) == x
    assert ff(x, 2) == x*(x - 1)
    assert ff(x, 3) == x*(x - 1)*(x - 2)
    assert ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4)

    assert ff(x, -1) == 1/(x + 1)
    assert ff(x, -2) == 1/((x + 1)*(x + 2))
    assert ff(x, -3) == 1/((x + 1)*(x + 2)*(x + 3))

    assert ff(100, 100) == factorial(100)

    assert ff(2*x**2 - 5*x, 2) == (2*x**2  - 5*x)*(2*x**2 - 5*x - 1)
    assert isinstance(ff(2*x**2 - 5*x, 2), Mul)
    assert ff(x**2 + 3*x, -2) == 1/((x**2 + 3*x + 1)*(x**2 + 3*x + 2))

    assert ff(Poly(2*x**2 - 5*x, x), 2) == Poly(4*x**4 - 28*x**3 + 59*x**2 - 35*x, x)
    assert isinstance(ff(Poly(2*x**2 - 5*x, x), 2), Poly)
    raises(ValueError, lambda: ff(Poly(2*x**2 - 5*x, x, y), 2))
    assert ff(Poly(x**2 + 3*x, x), -2) == 1/(x**4 + 12*x**3 + 49*x**2 + 78*x + 40)
    raises(ValueError, lambda: ff(Poly(x**2 + 3*x, x, y), -2))


    assert ff(x, m).is_integer is None
    assert ff(n, k).is_integer is None
    assert ff(n, m).is_integer is True
    assert ff(n, k + pi).is_integer is False
    assert ff(n, m + pi).is_integer is False
    assert ff(pi, m).is_integer is False

    assert isinstance(ff(x, x), ff)
    assert ff(n, n) == factorial(n)

    def check(x, k, o, n):
        a, b = Dummy(), Dummy()
        r = lambda x, k: o(a, b).rewrite(n).subs({a:x,b:k})
        for i in range(-5,5):
          for j in range(-5,5):
              assert o(i, j) == r(i, j), (o, n)
    check(x, k, ff, rf)
    check(x, k, ff, gamma)
    check(n, k, ff, factorial)
    check(x, k, ff, binomial)
    check(x, y, ff, factorial)
    check(x, y, ff, binomial)

    assert ff(x, k).rewrite(rf) == rf(x - k + 1, k)
    assert ff(x, k).rewrite(gamma) == Piecewise(
        (gamma(x + 1)/gamma(-k + x + 1), x >= 0),
        ((-1)**k*gamma(k - x)/gamma(-x), True))
    assert ff(5, k).rewrite(gamma) == 120/gamma(6 - k)
    assert ff(n, k).rewrite(factorial) == Piecewise(
        (factorial(n)/factorial(-k + n), n >= 0),
        ((-1)**k*factorial(k - n - 1)/factorial(-n - 1), True))
    assert ff(5, k).rewrite(factorial) == 120/factorial(5 - k)
    assert ff(x, k).rewrite(binomial) == factorial(k) * binomial(x, k)
    assert ff(x, y).rewrite(factorial) == ff(x, y)
    assert ff(x, y).rewrite(binomial) == ff(x, y)

    import random
    from mpmath import ff as mpmath_ff
    for i in range(100):
        x = -500 + 500 * random.random()
        k = -500 + 500 * random.random()
        a = mpmath_ff(x, k)
        b = ff(x, k)
        assert (abs(a - b) < abs(a) * 10**(-15))
Ejemplo n.º 25
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x**2 + 3 * x, 2) == x**4 + 8 * x**3 + 19 * x**2 + 12 * x
    assert rf(x**3 + x, -2) == 1 / (x**6 - 9 * x**5 + 35 * x**4 - 75 * x**3 +
                                    94 * x**2 - 66 * x + 20)

    n = Symbol('n', integer=True)
    k = Symbol('k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)
    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False
Ejemplo n.º 26
0
def test_rational_products():
    assert product(1 + 1 / k, (k, 1, n)) == rf(2, n) / factorial(n)
Ejemplo n.º 27
0
n = Symbol("n", positive=True, integer=True)
k = Symbol("k", integer=True)
# binomialpoly = implemented_function(Function('(x+y)^n'), lambda n: (x+y)**n)
# binomialpoly_n = lambdify(n, binomialpoly(n))
binomialpoly = (x + y) ** n
binomialpolyexpand = Sum(binomial(n, k) * x ** k * y ** (n - k), (k, 0, n))

binomialpoly.subs(n, 1) == binomialpolyexpand.subs(n, 1).doit()  # True
binomialpoly.subs(n, 2).expand() == binomialpolyexpand.subs(n, 2).doit()  # True
binomialpoly.subs(n, 3).expand() == binomialpolyexpand.subs(n, 3).doit()  # True
binomialpoly.subs(n, 4).expand() == binomialpolyexpand.subs(n, 4).doit()  # True

# rising factorial
a, q = symbols("a q")
# a_n = Product(a+k,(k,0,n-1))
a_n = rf(a, n)
aq_n = Product(Rat(1) - a * q ** k, (k, 0, n - 1))
# e.g. aq_n.subs(n,3).doit()

# Hermite polynomials

HermiteF = factorial(n) * (Rat(-1)) ** k * (Rat(2) * x) ** (n - Rat(2) * k) / (factorial(n - Rat(2) * k) * factorial(k))
HermiteP = Sum(HermiteF, (k, 0, floor(n / 2)))
hermite_poly(1, x) == HermiteP.subs(n, 1).doit()  # True
hermite_poly(2, x) == HermiteP.subs(n, 2).doit()  # True
hermite_poly(3, x) == HermiteP.subs(n, 3).doit()  # True
hermite_poly(4, x) == HermiteP.subs(n, 4).doit()  # True

(HermiteF.subs(n, n + 1) / HermiteF).simplify()  # 2*x*(n + 1)/(-2*k + n + 1)
(HermiteF.subs(k, k + 1) / HermiteF).simplify()  # -(2*k - n)*(2*k - n + 1)/(4*x**2*(k + 1))
Ejemplo n.º 28
0
def test_rf_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert rf(nan, y) == nan
    assert rf(x, nan) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x**2 + 3 * x, 2) == (x**2 + 3 * x) * (x**2 + 3 * x + 1)
    assert isinstance(rf(x**2 + 3 * x, 2), Mul)
    assert rf(x**3 + x, -2) == 1 / ((x**3 + x - 1) * (x**3 + x - 2))

    assert rf(Poly(x**2 + 3 * x, x),
              2) == Poly(x**4 + 8 * x**3 + 19 * x**2 + 12 * x, x)
    assert isinstance(rf(Poly(x**2 + 3 * x, x), 2), Poly)
    raises(ValueError, lambda: rf(Poly(x**2 + 3 * x, x, y), 2))
    assert rf(Poly(x**3 + x, x),
              -2) == 1 / (x**6 - 9 * x**5 + 35 * x**4 - 75 * x**3 + 94 * x**2 -
                          66 * x + 20)
    raises(ValueError, lambda: rf(Poly(x**3 + x, x, y), -2))

    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False

    assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
    assert rf(x, k).rewrite(binomial) == factorial(k) * binomial(x + k - 1, k)
    assert rf(n, k).rewrite(factorial) == \
        factorial(n + k - 1) / factorial(n - 1)
Ejemplo n.º 29
0
def test_rf_eval_apply():
    x, y = symbols("x,y")
    n, k = symbols("n k", integer=True)
    m = Symbol("m", integer=True, nonnegative=True)

    assert rf(nan, y) == nan
    assert rf(x, nan) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x ** 2 + 3 * x, 2) == x ** 4 + 8 * x ** 3 + 19 * x ** 2 + 12 * x
    assert rf(x ** 3 + x, -2) == 1 / (x ** 6 - 9 * x ** 5 + 35 * x ** 4 - 75 * x ** 3 + 94 * x ** 2 - 66 * x + 20)

    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False

    assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
    assert rf(x, k).rewrite(binomial) == factorial(k) * binomial(x + k - 1, k)
    assert rf(n, k).rewrite(factorial) == factorial(n + k - 1) / factorial(n - 1)
Ejemplo n.º 30
0
def test_rational_products():
    assert product(1 + 1/k, (k, 1, n)) == rf(2, n)/factorial(n)
Ejemplo n.º 31
0
def test_F1():
    assert rf(x, 3) == x * (1 + x) * (2 + x)
Ejemplo n.º 32
0
def test_rf_eval_apply():
    x, y = symbols('x,y')

    assert rf(nan, y) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x*(x + 1)
    assert rf(x, 3) == x*(x + 1)*(x + 2)
    assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)

    assert rf(x, -1) == 1/(x - 1)
    assert rf(x, -2) == 1/((x - 1)*(x - 2))
    assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))

    assert rf(1, 100) == factorial(100)
Ejemplo n.º 33
0
    def to_sequence(self):
        """
        Finds the recurrence relation in power series expansion
        of the function.

        Examples
        ========

        >>> from sympy.holonomic.holonomic import HolonomicFunction, DifferentialOperators
        >>> from sympy.polys.domains import ZZ, QQ
        >>> from sympy import symbols
        >>> x = symbols('x')
        >>> R, Dx = DifferentialOperators(QQ.old_poly_ring(x),'Dx')

        >>> HolonomicFunction(Dx - 1, x, 0, [1]).to_sequence()
        HolonomicSequence((-1) + (n + 1)Sn, n), u(0) = 1

        See Also
        ========

        HolonomicFunction.series

        References
        ==========

        hal.inria.fr/inria-00070025/document
        """

        dict1 = {}
        n = symbols('n', integer=True)
        dom = self.annihilator.parent.base.dom
        R, _ = RecurrenceOperators(dom.old_poly_ring(n), 'Sn')

        for i, j in enumerate(self.annihilator.listofpoly):
            listofdmp = j.all_coeffs()
            degree = len(listofdmp) - 1
            for k in range(degree + 1):
                coeff = listofdmp[degree - k]
                if coeff == 0:
                    continue
                if i - k in dict1:
                    dict1[i - k] += (coeff * rf(n - k + 1, i))
                else:
                    dict1[i - k] = (coeff * rf(n - k + 1, i))

        sol = []
        lower = min(dict1.keys())
        upper = max(dict1.keys())

        for j in range(lower, upper + 1):
            if j in dict1.keys():
                sol.append(dict1[j].subs(n, n - lower))
            else:
                sol.append(S(0))
        # recurrence relation
        sol = RecurrenceOperator(sol, R)

        if not self._have_init_cond:
            return HolonomicSequence(sol)
        if self.x0 != 0:
            return HolonomicSequence(sol)
        # computing the initial conditions for recurrence
        order = sol.order
        all_roots = roots(sol.listofpoly[-1].rep, filter='Z')
        all_roots = all_roots.keys()

        if all_roots:
            max_root = max(all_roots)
            if max_root >= 0:
                order += max_root + 1

        y0 = _extend_y0(self, order)
        u0 = []
        # u(n) = y^n(0)/factorial(n)
        for i, j in enumerate(y0):
            u0.append(j / factorial(i))

        return HolonomicSequence(sol, u0)
Ejemplo n.º 34
0
def test_rf_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert rf(nan, y) == nan
    assert rf(x, nan) == nan

    assert rf(x, y) == rf(x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) == oo
    assert rf(-oo, 7) == -oo

    assert rf(oo, -6) == oo
    assert rf(-oo, -7) == oo

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x*(x + 1)
    assert rf(x, 3) == x*(x + 1)*(x + 2)
    assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)

    assert rf(x, -1) == 1/(x - 1)
    assert rf(x, -2) == 1/((x - 1)*(x - 2))
    assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x**2 + 3*x, 2) == (x**2 + 3*x)*(x**2 + 3*x + 1)
    assert isinstance(rf(x**2 + 3*x, 2), Mul)
    assert rf(x**3 + x, -2) == 1/((x**3 + x - 1)*(x**3 + x - 2))

    assert rf(Poly(x**2 + 3*x, x), 2) == Poly(x**4 + 8*x**3 + 19*x**2 + 12*x, x)
    assert isinstance(rf(Poly(x**2 + 3*x, x), 2), Poly)
    raises(ValueError, lambda: rf(Poly(x**2 + 3*x, x, y), 2))
    assert rf(Poly(x**3 + x, x), -2) == 1/(x**6 - 9*x**5 + 35*x**4 - 75*x**3 + 94*x**2 - 66*x + 20)
    raises(ValueError, lambda: rf(Poly(x**3 + x, x, y), -2))

    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False

    assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
    assert rf(x, k).rewrite(binomial) == factorial(k)*binomial(x + k - 1, k)
    assert rf(n, k).rewrite(factorial) == \
        factorial(n + k - 1) / factorial(n - 1)
Ejemplo n.º 35
0
def ExLax(X,Y,z,k,w_0,m_max):
  """
  Takes the series of Opt. Lett. 28(10), 2003 to compute the non-paraxial
  corrections to Ex up to order m.
  """

  # -- Symbol/initial function definition.
  x_sym, y_sym,z_sym,k_sym, w_0_sym = sympy.symbols('x_sym y_sym z_sym k_sym w_0_sym')
  z_r_sym     = k_sym*w_0_sym**2/2
  w_z_sym     = w_0_sym*sympy.sqrt(1+(z_sym/z_r_sym)**2)
  R_sym       = z_sym/(z_sym**2+z_r_sym**2)
  phi_0       = w_0_sym/w_z_sym*sympy.exp(-(x_sym**2+y_sym**2)/w_z_sym**2)    \
                *sympy.exp(-1j*k_sym*z_sym)                                   \
                *sympy.exp(1j*sympy.atan(z_sym/z_r_sym))
#                *sympy.exp(-1j*k_sym*(x_sym**2+y_sym**2)*R_sym/2)             \

  # -- Symbolic expressions for summation phi/psi = sum_m phi^(2m)/psi^(2m+1)
  phi         = phi_0
  psi         = 1j/k*sympy.diff(phi_0,x_sym)
  weginer_phi = sympy.S.Zero
  weginer_psi = sympy.S.Zero
  phi_aj      = [sympy.S.Zero]
  phi_sj      = [sympy.S.Zero]
  psi_aj      = [sympy.S.Zero]
  psi_sj      = [sympy.S.Zero]

  # -- Symbolic expressions for specific values of m, to be used in the loop.
  phi_2m      = phi
  psi_2mp1    = psi

  # -- We compute the derivatives analytically.
  derivatives = []
  for i in range(1,2*m_max+1):
    derivatives.append(sympy.diff(phi_0,z_sym,i))

  # -- We evaluate the higher-order terms.
  for m in range(1,m_max+1):
    phi_2m       = sympy.S.Zero

    for p in range(1,m+1):

      # -- We evaluate the product between the z factor and the derivative,
      # -- and add it to the symbolic expression.
      polynomial = z_sym**p*derivatives[m+p-2]
      phi_2m    += ExpansionCoefficient(m,p)*polynomial

    phi_2m   *= (1j/(2*k))**m
    psi_2mp1  = 1j/k*(sympy.diff(phi_2m,x_sym)+sympy.diff(psi_2mp1,z_sym))
    phi      += phi_2m
    psi      += psi_2mp1

    phi_aj.append()

    # -- Weginer transformation.
    numerator = sympy.S.Zero
    denominator = sympy.S.Zero

    for j in range(m+1):
      s_j = sympy.S.Zero
      for i in range(j+1):
        s_j += 
      numerator += (-1)**j*sympy.binomial(m,j)*sympy.rf(1+j,m-1)

  # -- We evaluate the magnetic field.
  Bx_sym = sympy.diff(psi, y_sym)/(1j*k)
  By_sym = (sympy.diff(phi, z_sym)-sympy.diff(psi,x_sym))/(1j*k)
  Bz_sym = -sympy.diff(phi, y_sym)/(1j*k)

  # -- We lambdify the expressions and evaluate them.
  Ex = sympy.lambdify((x_sym,y_sym,z_sym,k_sym,w_0_sym), phi)
  Ez = sympy.lambdify((x_sym,y_sym,z_sym,k_sym,w_0_sym), psi)
  Bx = sympy.lambdify((x_sym,y_sym,z_sym,k_sym,w_0_sym), Bx_sym)
  By = sympy.lambdify((x_sym,y_sym,z_sym,k_sym,w_0_sym), By_sym)
  Bz = sympy.lambdify((x_sym,y_sym,z_sym,k_sym,w_0_sym), Bz_sym)

  return Ex(X,Y,z,k,w_0), np.zeros_like(Ex(X,Y,z,k,w_0)), Ez(X,Y,z,k,w_0), Bx(X,Y,z,k,w_0), By(X,Y,z,k,w_0), Bz(X,Y,z,k,w_0)
Ejemplo n.º 36
0
def test_rf_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert rf(nan, y) is nan
    assert rf(x, nan) is nan

    assert unchanged(rf, x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) is oo
    assert rf(-oo, 7) is -oo
    assert rf(-oo, 6) is oo

    assert rf(oo, -6) is oo
    assert rf(-oo, -7) is oo

    assert rf(-1, pi) == 0
    assert rf(-5, 1 + I) == 0

    assert unchanged(rf, -3, k)
    assert unchanged(rf, x, Symbol('k', integer=False))
    assert rf(-3, Symbol('k', integer=False)) == 0
    assert rf(Symbol('x', negative=True, integer=True),
              Symbol('k', integer=False)) == 0

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x * (x + 1)
    assert rf(x, 3) == x * (x + 1) * (x + 2)
    assert rf(x, 5) == x * (x + 1) * (x + 2) * (x + 3) * (x + 4)

    assert rf(x, -1) == 1 / (x - 1)
    assert rf(x, -2) == 1 / ((x - 1) * (x - 2))
    assert rf(x, -3) == 1 / ((x - 1) * (x - 2) * (x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x**2 + 3 * x, 2) == (x**2 + 3 * x) * (x**2 + 3 * x + 1)
    assert isinstance(rf(x**2 + 3 * x, 2), Mul)
    assert rf(x**3 + x, -2) == 1 / ((x**3 + x - 1) * (x**3 + x - 2))

    assert rf(Poly(x**2 + 3 * x, x),
              2) == Poly(x**4 + 8 * x**3 + 19 * x**2 + 12 * x, x)
    assert isinstance(rf(Poly(x**2 + 3 * x, x), 2), Poly)
    raises(ValueError, lambda: rf(Poly(x**2 + 3 * x, x, y), 2))
    assert rf(Poly(x**3 + x, x),
              -2) == 1 / (x**6 - 9 * x**5 + 35 * x**4 - 75 * x**3 + 94 * x**2 -
                          66 * x + 20)
    raises(ValueError, lambda: rf(Poly(x**3 + x, x, y), -2))

    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False

    assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
    assert rf(x, k).rewrite(binomial) == factorial(k) * binomial(x + k - 1, k)
    assert rf(n, k).rewrite(factorial) == \
        factorial(n + k - 1) / factorial(n - 1)
    assert rf(x, y).rewrite(factorial) == rf(x, y)
    assert rf(x, y).rewrite(binomial) == rf(x, y)

    import random
    from mpmath import rf as mpmath_rf
    for i in range(100):
        x = -500 + 500 * random.random()
        k = -500 + 500 * random.random()
        assert (abs(mpmath_rf(x, k) - rf(x, k)) < 10**(-15))
def test_rf_eval_apply():
    x, y = symbols('x,y')
    n, k = symbols('n k', integer=True)
    m = Symbol('m', integer=True, nonnegative=True)

    assert rf(nan, y) is nan
    assert rf(x, nan) is nan

    assert unchanged(rf, x, y)

    assert rf(oo, 0) == 1
    assert rf(-oo, 0) == 1

    assert rf(oo, 6) is oo
    assert rf(-oo, 7) is -oo
    assert rf(-oo, 6) is oo

    assert rf(oo, -6) is oo
    assert rf(-oo, -7) is oo

    assert rf(-1, pi) == 0
    assert rf(-5, 1 + I) == 0

    assert unchanged(rf, -3, k)
    assert unchanged(rf, x, Symbol('k', integer=False))
    assert rf(-3, Symbol('k', integer=False)) == 0
    assert rf(Symbol('x', negative=True, integer=True), Symbol('k', integer=False)) == 0

    assert rf(x, 0) == 1
    assert rf(x, 1) == x
    assert rf(x, 2) == x*(x + 1)
    assert rf(x, 3) == x*(x + 1)*(x + 2)
    assert rf(x, 5) == x*(x + 1)*(x + 2)*(x + 3)*(x + 4)

    assert rf(x, -1) == 1/(x - 1)
    assert rf(x, -2) == 1/((x - 1)*(x - 2))
    assert rf(x, -3) == 1/((x - 1)*(x - 2)*(x - 3))

    assert rf(1, 100) == factorial(100)

    assert rf(x**2 + 3*x, 2) == (x**2 + 3*x)*(x**2 + 3*x + 1)
    assert isinstance(rf(x**2 + 3*x, 2), Mul)
    assert rf(x**3 + x, -2) == 1/((x**3 + x - 1)*(x**3 + x - 2))

    assert rf(Poly(x**2 + 3*x, x), 2) == Poly(x**4 + 8*x**3 + 19*x**2 + 12*x, x)
    assert isinstance(rf(Poly(x**2 + 3*x, x), 2), Poly)
    raises(ValueError, lambda: rf(Poly(x**2 + 3*x, x, y), 2))
    assert rf(Poly(x**3 + x, x), -2) == 1/(x**6 - 9*x**5 + 35*x**4 - 75*x**3 + 94*x**2 - 66*x + 20)
    raises(ValueError, lambda: rf(Poly(x**3 + x, x, y), -2))

    assert rf(x, m).is_integer is None
    assert rf(n, k).is_integer is None
    assert rf(n, m).is_integer is True
    assert rf(n, k + pi).is_integer is False
    assert rf(n, m + pi).is_integer is False
    assert rf(pi, m).is_integer is False

    def check(x, k, o, n):
        a, b = Dummy(), Dummy()
        r = lambda x, k: o(a, b).rewrite(n).subs({a:x,b:k})
        for i in range(-5,5):
          for j in range(-5,5):
              assert o(i, j) == r(i, j), (o, n, i, j)
    check(x, k, rf, ff)
    check(x, k, rf, binomial)
    check(n, k, rf, factorial)
    check(x, y, rf, factorial)
    check(x, y, rf, binomial)

    assert rf(x, k).rewrite(ff) == ff(x + k - 1, k)
    assert rf(x, k).rewrite(gamma) == Piecewise(
        (gamma(k + x)/gamma(x), x > 0),
        ((-1)**k*gamma(1 - x)/gamma(-k - x + 1), True))
    assert rf(5, k).rewrite(gamma) == gamma(k + 5)/24
    assert rf(x, k).rewrite(binomial) == factorial(k)*binomial(x + k - 1, k)
    assert rf(n, k).rewrite(factorial) == Piecewise(
        (factorial(k + n - 1)/factorial(n - 1), n > 0),
        ((-1)**k*factorial(-n)/factorial(-k - n), True))
    assert rf(5, k).rewrite(factorial) == factorial(k + 4)/24
    assert rf(x, y).rewrite(factorial) == rf(x, y)
    assert rf(x, y).rewrite(binomial) == rf(x, y)

    import random
    from mpmath import rf as mpmath_rf
    for i in range(100):
        x = -500 + 500 * random.random()
        k = -500 + 500 * random.random()
        assert (abs(mpmath_rf(x, k) - rf(x, k)) < 10**(-15))
Ejemplo n.º 38
0
def test_gammasimp():
    R = Rational

    # was part of test_combsimp_gamma() in test_combsimp.py
    assert gammasimp(gamma(x)) == gamma(x)
    assert gammasimp(gamma(x + 1)/x) == gamma(x)
    assert gammasimp(gamma(x)/(x - 1)) == gamma(x - 1)
    assert gammasimp(x*gamma(x)) == gamma(x + 1)
    assert gammasimp((x + 1)*gamma(x + 1)) == gamma(x + 2)
    assert gammasimp(gamma(x + y)*(x + y)) == gamma(x + y + 1)
    assert gammasimp(x/gamma(x + 1)) == 1/gamma(x)
    assert gammasimp((x + 1)**2/gamma(x + 2)) == (x + 1)/gamma(x + 1)
    assert gammasimp(x*gamma(x) + gamma(x + 3)/(x + 2)) == \
        (x + 2)*gamma(x + 1)

    assert gammasimp(gamma(2*x)*x) == gamma(2*x + 1)/2
    assert gammasimp(gamma(2*x)/(x - S(1)/2)) == 2*gamma(2*x - 1)

    assert gammasimp(gamma(x)*gamma(1 - x)) == pi/sin(pi*x)
    assert gammasimp(gamma(x)*gamma(-x)) == -pi/(x*sin(pi*x))
    assert gammasimp(1/gamma(x + 3)/gamma(1 - x)) == \
        sin(pi*x)/(pi*x*(x + 1)*(x + 2))

    assert gammasimp(factorial(n + 2)) == gamma(n + 3)
    assert gammasimp(binomial(n, k)) == \
        gamma(n + 1)/(gamma(k + 1)*gamma(-k + n + 1))

    assert powsimp(gammasimp(
        gamma(x)*gamma(x + S(1)/2)*gamma(y)/gamma(x + y))) == \
        2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y)
    assert gammasimp(1/gamma(x)/gamma(x - S(1)/3)/gamma(x + S(1)/3)) == \
        3**(3*x - S(3)/2)/(2*pi*gamma(3*x - 1))
    assert simplify(
        gamma(S(1)/2 + x/2)*gamma(1 + x/2)/gamma(1 + x)/sqrt(pi)*2**x) == 1
    assert gammasimp(gamma(S(-1)/4)*gamma(S(-3)/4)) == 16*sqrt(2)*pi/3

    assert powsimp(gammasimp(gamma(2*x)/gamma(x))) == \
        2**(2*x - 1)*gamma(x + S(1)/2)/sqrt(pi)

    # issue 6792
    e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
    assert gammasimp(e) == -k
    assert gammasimp(1/e) == -1/k
    e = (gamma(x) + gamma(x + 1))/gamma(x)
    assert gammasimp(e) == x + 1
    assert gammasimp(1/e) == 1/(x + 1)
    e = (gamma(x) + gamma(x + 2))*(gamma(x - 1) + gamma(x))/gamma(x)
    assert gammasimp(e) == (x**2 + x + 1)*gamma(x + 1)/(x - 1)
    e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2
    assert gammasimp(e**2) == k**2
    assert gammasimp(e**2/gamma(k + 1)) == k/gamma(k)
    a = R(1, 2) + R(1, 3)
    b = a + R(1, 3)
    assert gammasimp(gamma(2*k)/gamma(k)*gamma(k + a)*gamma(k + b))
    3*2**(2*k + 1)*3**(-3*k - 2)*sqrt(pi)*gamma(3*k + R(3, 2))/2

    # issue 9699
    assert gammasimp((x + 1)*factorial(x)/gamma(y)) == gamma(x + 2)/gamma(y)
    assert gammasimp(rf(x + n, k)*binomial(n, k)) == gamma(n + 1)*gamma(k + n
        + x)/(gamma(k + 1)*gamma(n + x)*gamma(-k + n + 1))

    A, B = symbols('A B', commutative=False)
    assert gammasimp(e*B*A) == gammasimp(e)*B*A

    # check iteration
    assert gammasimp(gamma(2*k)/gamma(k)*gamma(-k - R(1, 2))) == (
        -2**(2*k + 1)*sqrt(pi)/(2*((2*k + 1)*cos(pi*k))))
    assert gammasimp(
        gamma(k)*gamma(k + R(1, 3))*gamma(k + R(2, 3))/gamma(3*k/2)) == (
        3*2**(3*k + 1)*3**(-3*k - S.Half)*sqrt(pi)*gamma(3*k/2 + S.Half)/2)

    # issue 6153
    assert gammasimp(gamma(S(1)/4)/gamma(S(5)/4)) == 4

    # was part of test_combsimp() in test_combsimp.py
    assert gammasimp(binomial(n + 2, k + S(1)/2)) == gamma(n + 3)/ \
        (gamma(k + S(3)/2)*gamma(-k + n + S(5)/2))
    assert gammasimp(binomial(n + 2, k + 2.0)) == \
        gamma(n + 3)/(gamma(k + 3.0)*gamma(-k + n + 1))

    # issue 11548
    assert gammasimp(binomial(0, x)) == sin(pi*x)/(pi*x)

    e = gamma(n + S(1)/3)*gamma(n + S(2)/3)
    assert gammasimp(e) == e
    assert gammasimp(gamma(4*n + S(1)/2)/gamma(2*n - S(3)/4)) == \
        2**(4*n - S(5)/2)*(8*n - 3)*gamma(2*n + S(3)/4)/sqrt(pi)

    i, m = symbols('i m', integer = True)
    e = gamma(exp(i))
    assert gammasimp(e) == e
    e = gamma(m + 3)
    assert gammasimp(e) == e
    e = gamma(m + 1)/(gamma(i + 1)*gamma(-i + m + 1))
    assert gammasimp(e) == e

    p = symbols("p", integer=True, positive=True)
    assert gammasimp(gamma(-p+4)) == gamma(-p+4)
Ejemplo n.º 39
0
def test_F1():
    assert rf(x, 3) == x*(1 + x)*(2 + x)