예제 #1
0
def test__eval_product():
    from diofant.abc import i, n
    # issue 4809
    a = Function('a')
    assert product(2 * a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue 4810
    assert product(2**i, (i, 1, n)) == 2**(n / 2 + n**2 / 2)
예제 #2
0
def test_rational_products():
    assert simplify(product(1 + 1/n, (n, a, b))) == (1 + b)/a
    assert simplify(product(n + 1, (n, a, b))) == gamma(2 + b)/gamma(1 + a)
    assert simplify(product((n + 1)/(n - 1), (n, a, b))) == b*(1 + b)/(a*(a - 1))
    assert simplify(product(n/(n + 1)/(n + 2), (n, a, b))) == \
        a*gamma(a + 2)/(b + 1)/gamma(b + 3)
    assert simplify(product(n*(n + 1)/(n - 1)/(n - 2), (n, a, b))) == \
        b**2*(b - 1)*(1 + b)/(a - 1)**2/(a*(a - 2))
예제 #3
0
def test_rational_products():
    assert simplify(product(1 + 1/n, (n, a, b))) == (1 + b)/a
    assert simplify(product(n + 1, (n, a, b))) == gamma(2 + b)/gamma(1 + a)
    assert simplify(product((n + 1)/(n - 1), (n, a, b))) == b*(1 + b)/(a*(a - 1))
    assert simplify(product(n/(n + 1)/(n + 2), (n, a, b))) == \
        a*gamma(a + 2)/(b + 1)/gamma(b + 3)
    assert simplify(product(n*(n + 1)/(n - 1)/(n - 2), (n, a, b))) == \
        b**2*(b - 1)*(1 + b)/(a - 1)**2/(a*(a - 2))
예제 #4
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
예제 #5
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
예제 #6
0
def test_wallis_product():
    # Wallis product, given in two different forms to ensure that Product
    # can factor simple rational expressions
    A = product(4 * n**2 / (4 * n**2 - 1), (n, 1, b))
    B = product((2 * n) * (2 * n) / (2 * n - 1) / (2 * n + 1), (n, 1, b))
    half = Rational(1, 2)
    R = pi / 2 * factorial(b)**2 / factorial(b - half) / factorial(b + half)
    assert A.equals(R)
    assert B.equals(R)
예제 #7
0
def test_rational_products():
    assert product(1 + 1 / n, (n, a, b)).equals((1 + b) / a)
    assert product(n + 1, (n, a, b)).equals(gamma(2 + b) / gamma(1 + a))
    assert product((n + 1) / (n - 1),
                   (n, a, b)).equals(b * (1 + b) / (a * (a - 1)))
    assert product(n / (n + 1) / (n + 2),
                   (n, a, b)).equals(a * gamma(a + 2) / (b + 1) / gamma(b + 3))
    assert product(n * (n + 1) / (n - 1) / (n - 2), (n, a, b)).equals(
        b**2 * (b - 1) * (1 + b) / (a - 1)**2 / (a * (a - 2)))
예제 #8
0
def test__eval_product():
    # issue sympy/sympy#4809
    a = Function('a')
    assert product(2*a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue sympy/sympy#4810
    assert product(2**i, (i, 1, n)) == 2**(n/2 + n**2/2)

    assert (product((i - 1)*(i**6 + i - 1), (i, n, m)) ==
            rf(n - 1, m - n + 1)*product(i**6 + i - 1, (i, n, m)))

    assert (product(log(i)**2*cos(i)**3, (i, n, m)) ==
            Product(log(i)**2*cos(i)**3, (i, n, m)))
예제 #9
0
def test_multiple_products():
    assert product(x, (n, 1, k), (k, 1, m)) == x**(m**2 / 2 + m / 2)
    assert product(f(n), (n, 1, m),
                   (m, 1, k)) == Product(f(n), (n, 1, m), (m, 1, k)).doit()
    assert Product(f(n), (m, 1, k), (n, 1, k)).doit() == \
        Product(Product(f(n), (m, 1, k)), (n, 1, k)).doit() == \
        product(f(n), (m, 1, k), (n, 1, k)) == \
        product(product(f(n), (m, 1, k)), (n, 1, k)) == \
        Product(f(n)**k, (n, 1, k))
    assert Product(x, (x, 1, k),
                   (k, 1, n)).doit() == Product(factorial(k), (k, 1, n))

    assert Product(x**k, (n, 1, k), (k, 1, m)).variables == [n, k]
예제 #10
0
def test_multiple_products():
    assert product(x, (n, 1, k), (k, 1, m)) == x**(m**2/2 + m/2)
    assert product(f(n), (
        n, 1, m), (m, 1, k)) == Product(f(n), (n, 1, m), (m, 1, k)).doit()
    assert Product(f(n), (m, 1, k), (n, 1, k)).doit() == \
        Product(Product(f(n), (m, 1, k)), (n, 1, k)).doit() == \
        product(f(n), (m, 1, k), (n, 1, k)) == \
        product(product(f(n), (m, 1, k)), (n, 1, k)) == \
        Product(f(n)**k, (n, 1, k))
    assert Product(
        x, (x, 1, k), (k, 1, n)).doit() == Product(factorial(k), (k, 1, n))

    assert Product(x**k, (n, 1, k), (k, 1, m)).variables == [n, k]
예제 #11
0
def test__eval_product():
    # issue sympy/sympy#4809
    a = Function('a')
    assert product(2 * a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n))
    # issue sympy/sympy#4810
    assert product(2**i, (i, 1, n)) == 2**(n / 2 + n**2 / 2)

    assert (product(
        (i - 1) * (i**6 + i - 1),
        (i, n, m)) == rf(n - 1, m - n + 1) * product(i**6 + i - 1, (i, n, m)))

    assert (product(log(i)**2 * cos(i)**3,
                    (i, n, m)) == Product(log(i)**2 * cos(i)**3, (i, n, m)))
예제 #12
0
def test_simple_products():
    assert Product(nan, (x, 1, 3)) is nan
    assert product(nan, (x, 1, 3)) is nan
    assert Product(x, (n, a, a)).doit() == x
    assert Product(x, (x, a, a)).doit() == a
    assert Product(x, (y, 1, a)).doit() == x**a

    lo, hi = 1, 2
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == 2
    assert s2.doit() == 1

    lo, hi = x, x + 1
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    s3 = 1 / Product(n, (n, hi + 1, lo - 1))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == x * (x + 1)
    assert s2.doit() == 1
    assert s3.doit() == x * (x + 1)

    assert Product(Integral(2 * x, (x, 1, y)) + 2 * x,
                   (x, 1, 2)).doit() == (y**2 + 1) * (y**2 + 3)
    assert product(2, (n, a, b)) == 2**(b - a + 1)
    assert product(n, (n, 1, b)) == factorial(b)
    assert product(n**3, (n, 1, b)) == factorial(b)**3
    assert product(3**(2 + n),
                   (n, a, b)) == (3**(2 * (1 - a + b) + b / 2 + (b**2) / 2 +
                                      a / 2 - (a**2) / 2))
    assert product(cos(n), (n, 3, 5)) == cos(3) * cos(4) * cos(5)
    assert product(cos(n), (n, x, x + 2)) == cos(x) * cos(x + 1) * cos(x + 2)
    assert isinstance(product(cos(n), (n, x, x + Rational(1, 2))), Product)
    # If Product managed to evaluate this one, it most likely got it wrong!
    assert isinstance(Product(n**n, (n, 1, b)), Product)
예제 #13
0
def test_simple_products():
    assert Product(nan, (x, 1, 3)) is nan
    assert product(nan, (x, 1, 3)) is nan
    assert Product(x, (n, a, a)).doit() == x
    assert Product(x, (x, a, a)).doit() == a
    assert Product(x, (y, 1, a)).doit() == x**a

    lo, hi = 1, 2
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == 2
    assert s2.doit() == 1

    lo, hi = x, x + 1
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    s3 = 1 / Product(n, (n, hi + 1, lo - 1))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == x*(x + 1)
    assert s2.doit() == 1
    assert s3.doit() == x*(x + 1)

    assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \
        (y**2 + 1)*(y**2 + 3)
    assert product(2, (n, a, b)) == 2**(b - a + 1)
    assert product(n, (n, 1, b)) == factorial(b)
    assert product(n**3, (n, 1, b)) == factorial(b)**3
    assert product(3**(2 + n), (n, a, b)) \
        == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2)
    assert product(cos(n), (n, 3, 5)) == cos(3)*cos(4)*cos(5)
    assert product(cos(n), (n, x, x + 2)) == cos(x)*cos(x + 1)*cos(x + 2)
    assert isinstance(product(cos(n), (n, x, x + Rational(1, 2))), Product)
    # If Product managed to evaluate this one, it most likely got it wrong!
    assert isinstance(Product(n**n, (n, 1, b)), Product)
예제 #14
0
 def maxima_product(self, var, low, high):
     return product(self, (var, low, high))
예제 #15
0
def test_evalf_mul():
    # diofant should not try to expand this; it should be handled term-wise
    # in evalf through mpmath
    assert NS(product(1 + sqrt(n)*I, (n, 1, 500)), 1) == '5.e+567 + 2.e+568*I'
예제 #16
0
def test_evalf_mul():
    # diofant should not try to expand this; it should be handled term-wise
    # in evalf through mpmath
    assert NS(product(1 + sqrt(n)*I, (n, 1, 500)), 1) == '5.e+567 + 2.e+568*I'
예제 #17
0
def test_product_pow():
    # issue 4817
    assert product(2**f(k), (k, 1, n)) == 2**Sum(f(k), (k, 1, n))
    assert product(2**(2 * f(k)), (k, 1, n)) == 2**Sum(2 * f(k), (k, 1, n))
예제 #18
0
def test_Product_doit():
    assert Product(n * Integral(a**2), (n, 1, 3)).doit() == 2 * a**9 / 9
    assert Product(n*Integral(a**2), (n, 1, 3)).doit(deep=False) == \
        6*Integral(a**2)**3
    assert product(n * Integral(a**2), (n, 1, 3)) == 6 * Integral(a**2)**3
예제 #19
0
def test_rational_products():
    assert product(1 + 1/k, (k, 1, n)) == rf(2, n)/factorial(n)
예제 #20
0
def test_product_pow():
    # issue sympy/sympy#4817
    assert product(2**f(k), (k, 1, n)) == 2**Sum(f(k), (k, 1, n))
    assert product(2**(2*f(k)), (k, 1, n)) == 2**Sum(2*f(k), (k, 1, n))
예제 #21
0
def test_rational_products():
    assert product(1 + 1 / k, (k, 1, n)) == rf(2, n) / factorial(n)
예제 #22
0
파일: utils.py 프로젝트: probing-lab/amber
    def compute_moment(self, k):
        if self.distribution == 'finite':
            return sum([p * (b**k) for b, p in self.parameters])

        if self.distribution == 'uniform':
            l, u = self.parameters
            return (u**(k + 1) - l**(k + 1)) / ((k + 1) * (u - l))

        if self.distribution == 'gauss' or self.distribution == 'normal':
            mu, sigma_squared = self.parameters
            # For low moments avoid scipy.stats.moments as it does not support
            # parametric parameters. In the future get all moments directly,
            # using the following properties:
            # https://math.stackexchange.com/questions/1945448/methods-for-finding-raw-moments-of-the-normal-distribution
            if k == 0:
                return 1
            elif k == 1:
                return mu
            elif k == 2:
                return mu**2 + sigma_squared
            elif k == 3:
                return mu * (mu**2 + 3 * sigma_squared)
            elif k == 4:
                return mu**4 + 6 * mu**2 * sigma_squared + 3 * sigma_squared**2
            moment = norm(loc=mu, scale=sqrt(sigma_squared)).moment(k)
            return Rational(moment)

        if self.distribution == 'bernoulli':
            return sympify(self.parameters[0])

        if self.distribution == 'geometric':
            p = sympify(self.parameters[0])
            return p * polylog(-k, 1 - p)

        if self.distribution == 'exponential':
            lambd = sympify(self.parameters[0])
            return factorial(k) / (lambd**k)

        if self.distribution == 'beta':
            alpha, beta = self.parameters
            alpha = sympify(alpha)
            beta = sympify(beta)
            r = symbols('r')
            return product((alpha + r) / (alpha + beta + r), (r, 0, k - 1))

        if self.distribution == 'chi-squared':
            n = sympify(self.parameters[0])
            i = symbols('i')
            return product(n + 2 * i, (i, 0, k - 1))

        if self.distribution == 'rayleigh':
            s = sympify(self.parameters[0])
            return (2**(k / 2)) * (s**k) * gamma(1 + k / 2)

        if self.distribution == 'unknown':
            return sympify(f"{self.var_name}(0)^{k}")

        if self.distribution == 'laplace':
            mu, b = self.parameters
            mu = sympify(mu)
            b = sympify(b)
            x = Laplace("x", mu, b)
            return E(x**k)

        if self.distribution == 'binomial':
            n, p = self.parameters
            n = sympify(n)
            p = sympify(p)
            x = Binomial("x", n, p)
            return E(x**k)

        if self.distribution == 'hypergeometric':
            N, K, n = self.parameters
            N = sympify(N)
            K = sympify(K)
            n = sympify(n)
            x = Hypergeometric("x", N, K, n)
            return E(x**k)
예제 #23
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]

    pytest.raises(ValueError, lambda: Product(n))
    pytest.raises(ValueError, lambda: Product(n * k))
    pytest.raises(ValueError, lambda: Product(n, k))
    pytest.raises(ValueError, lambda: Product(n, k, 1))
    pytest.raises(ValueError, lambda: Product(n, k, 1, 10))
    pytest.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
예제 #24
0
def test_Product_doit():
    assert Product(n*Integral(a**2), (n, 1, 3)).doit() == 2 * a**9 / 9
    assert Product(n*Integral(a**2), (n, 1, 3)).doit(deep=False) == \
        6*Integral(a**2)**3
    assert product(n*Integral(a**2), (n, 1, 3)) == 6*Integral(a**2)**3
예제 #25
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]

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

    assert product(1, (n, 1, oo)) == 1  # issue sympy/sympy#8301
    assert product(2, (n, 1, oo)) == oo
    assert isinstance(product(-1, (n, 1, oo)), Product)

    assert product(Kd(n, m), (m, 1, 3)) == 0
    assert product(Kd(n, m), (m, 1, 1)) == Kd(n, 1)