Exemple #1
0
def test_lerchphi_expansion():
    assert myexpand(lerchphi(1, s, a), zeta(s, a))
    assert myexpand(lerchphi(z, s, 1), polylog(s, z) / z)

    # direct summation
    assert myexpand(lerchphi(z, -1, a), a / (1 - z) + z / (1 - z)**2)
    assert myexpand(lerchphi(z, -3, a), None)

    # polylog reduction
    assert myexpand(
        lerchphi(z, s, Rational(1, 2)),
        2**(s - 1) * (polylog(s, sqrt(z)) / sqrt(z) -
                      polylog(s,
                              polar_lift(-1) * sqrt(z)) / sqrt(z)))
    assert myexpand(lerchphi(z, s, 2), -1 / z + polylog(s, z) / z**2)
    assert myexpand(lerchphi(z, s, Rational(3, 2)), None)
    assert myexpand(lerchphi(z, s, Rational(7, 3)), None)
    assert myexpand(lerchphi(z, s, -Rational(1, 3)), None)
    assert myexpand(lerchphi(z, s, -Rational(5, 2)), None)

    # hurwitz zeta reduction
    assert myexpand(lerchphi(-1, s, a),
                    2**(-s) * zeta(s, a / 2) - 2**(-s) * zeta(s, (a + 1) / 2))
    assert myexpand(lerchphi(I, s, a), None)
    assert myexpand(lerchphi(-I, s, a), None)
    assert myexpand(lerchphi(exp(2 * I * pi / 5), s, a), None)
def test_rewriting():
    assert dirichlet_eta(x).rewrite(zeta) == (1 - 2**(1 - x))*zeta(x)
    assert zeta(x).rewrite(dirichlet_eta) == dirichlet_eta(x)/(1 - 2**(1 - x))
    assert tn(dirichlet_eta(x), dirichlet_eta(x).rewrite(zeta), x)
    assert tn(zeta(x), zeta(x).rewrite(dirichlet_eta), x)

    assert zeta(x, a).rewrite(lerchphi) == lerchphi(1, x, a)
    assert polylog(s, z).rewrite(lerchphi) == lerchphi(z, s, 1)*z

    assert lerchphi(1, x, a).rewrite(zeta) == zeta(x, a)
    assert z*lerchphi(z, s, 1).rewrite(polylog) == polylog(s, z)
def test_derivatives():
    assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x)
    assert zeta(x, a).diff(a) == -x*zeta(x + 1, a)
    assert lerchphi(
        z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a*lerchphi(z, s, a))/z
    assert lerchphi(z, s, a).diff(a) == -s*lerchphi(z, s + 1, a)
    assert polylog(s, z).diff(z) == polylog(s - 1, z)/z

    b = randcplx()
    c = randcplx()
    assert td(zeta(b, x), x)
    assert td(polylog(b, z), z)
    assert td(lerchphi(c, b, x), x)
    assert td(lerchphi(x, b, c), x)
def test_rewriting():
    assert dirichlet_eta(x).rewrite(zeta) == (1 - 2**(1 - x))*zeta(x)
    assert zeta(x).rewrite(dirichlet_eta) == dirichlet_eta(x)/(1 - 2**(1 - x))
    assert zeta(z, 2).rewrite(dirichlet_eta) == zeta(z, 2)
    assert zeta(z, 2).rewrite('tractable') == zeta(z, 2)
    assert tn(dirichlet_eta(x), dirichlet_eta(x).rewrite(zeta), x)
    assert tn(zeta(x), zeta(x).rewrite(dirichlet_eta), x)

    assert zeta(x, a).rewrite(lerchphi) == lerchphi(1, x, a)
    assert polylog(s, z).rewrite(lerchphi) == lerchphi(z, s, 1)*z

    assert lerchphi(1, x, a).rewrite(zeta) == zeta(x, a)
    assert z*lerchphi(z, s, 1).rewrite(polylog) == polylog(s, z)

    assert lerchphi(z, s, a).rewrite(zeta) == lerchphi(z, s, a)
Exemple #5
0
def test_lerchphi():
    from diofant import combsimp, exp_polar, polylog, log, lerchphi
    assert hyperexpand(hyper([1, a], [a + 1], z) / a) == lerchphi(z, 1, a)
    assert hyperexpand(hyper([1, a, a], [a + 1, a + 1], z) / a**2) == lerchphi(
        z, 2, a)
    assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \
        lerchphi(z, 3, a)
    assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \
        lerchphi(z, 10, a)
    assert combsimp(
        hyperexpand(meijerg([0, 1 - a], [], [0], [-a],
                            exp_polar(-I * pi) * z))) == lerchphi(z, 1, a)
    assert combsimp(
        hyperexpand(
            meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a],
                    exp_polar(-I * pi) * z))) == lerchphi(z, 2, a)
    assert combsimp(
        hyperexpand(
            meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a],
                    exp_polar(-I * pi) * z))) == lerchphi(z, 3, a)

    assert hyperexpand(z * hyper([1, 1], [2], z)) == -log(1 + -z)
    assert hyperexpand(z * hyper([1, 1, 1], [2, 2], z)) == polylog(2, z)
    assert hyperexpand(z * hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z)

    assert hyperexpand(hyper([1, a, 1 + Rational(1, 2)], [a + 1, Rational(1, 2)], z)) == \
        -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a)

    # Now numerical tests. These make sure reductions etc are carried out
    # correctly

    # a rational function (polylog at negative integer order)
    assert can_do([2, 2, 2], [1, 1])

    # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1
    # reduction of order for polylog
    assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10)

    # reduction of order for lerchphi
    # XXX lerchphi in mpmath is flaky
    assert can_do([1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b],
                  numerical=False)

    # test a bug
    from diofant import Abs
    assert hyperexpand(hyper([Rational(1, 2), Rational(1, 2), Rational(1, 2), 1],
                             [Rational(3, 2), Rational(3, 2), Rational(3, 2)], Rational(1, 4))) == \
        Abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, Rational(1, 2)))
Exemple #6
0
def test_derivatives():
    assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x)
    assert zeta(x, a).diff(a) == -x * zeta(x + 1, a)
    assert zeta(z).diff(z) == Derivative(zeta(z), z)
    assert lerchphi(
        z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a * lerchphi(z, s, a)) / z
    pytest.raises(ArgumentIndexError, lambda: lerchphi(z, s, a).fdiff(4))
    assert lerchphi(z, s, a).diff(a) == -s * lerchphi(z, s + 1, a)
    assert polylog(s, z).diff(z) == polylog(s - 1, z) / z
    pytest.raises(ArgumentIndexError, lambda: polylog(s, z).fdiff(3))

    b = randcplx()
    c = randcplx()
    assert td(zeta(b, x), x)
    assert td(polylog(b, z), z)
    assert td(lerchphi(c, b, x), x)
    assert td(lerchphi(x, b, c), x)
def test_derivatives():
    assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x)
    assert zeta(x, a).diff(a) == -x*zeta(x + 1, a)
    assert zeta(z).diff(z) == Derivative(zeta(z), z)
    assert lerchphi(
        z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a*lerchphi(z, s, a))/z
    pytest.raises(ArgumentIndexError, lambda: lerchphi(z, s, a).fdiff(4))
    assert lerchphi(z, s, a).diff(a) == -s*lerchphi(z, s + 1, a)
    assert polylog(s, z).diff(z) == polylog(s - 1, z)/z
    pytest.raises(ArgumentIndexError, lambda: polylog(s, z).fdiff(3))

    b = randcplx()
    c = randcplx()
    assert td(zeta(b, x), x)
    assert td(polylog(b, z), z)
    assert td(lerchphi(c, b, x), x)
    assert td(lerchphi(x, b, c), x)
Exemple #8
0
def test_polylog_eval():
    assert polylog(s, 0) == 0
    assert polylog(s, 1) == zeta(s)
    assert polylog(s, -1) == -dirichlet_eta(s)
    assert polylog(s, exp_polar(I * pi)) == polylog(s, -1)
    assert polylog(s, 2 * exp_polar(2 * I * pi)) == polylog(
        s, 2 * exp_polar(2 * I * pi), evaluate=False)
def test_lerchphi():
    assert hyperexpand(hyper([1, a], [a + 1], z)/a) == lerchphi(z, 1, a)
    assert hyperexpand(
        hyper([1, a, a], [a + 1, a + 1], z)/a**2) == lerchphi(z, 2, a)
    assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \
        lerchphi(z, 3, a)
    assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \
        lerchphi(z, 10, a)
    assert combsimp(hyperexpand(meijerg([0, 1 - a], [], [0],
                                        [-a], exp_polar(-I*pi)*z))) == lerchphi(z, 1, a)
    assert combsimp(hyperexpand(meijerg([0, 1 - a, 1 - a], [], [0],
                                        [-a, -a], exp_polar(-I*pi)*z))) == lerchphi(z, 2, a)
    assert combsimp(hyperexpand(meijerg([0, 1 - a, 1 - a, 1 - a], [], [0],
                                        [-a, -a, -a], exp_polar(-I*pi)*z))) == lerchphi(z, 3, a)

    assert hyperexpand(z*hyper([1, 1], [2], z)) == -log(1 + -z)
    assert hyperexpand(z*hyper([1, 1, 1], [2, 2], z)) == polylog(2, z)
    assert hyperexpand(z*hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z)

    assert hyperexpand(hyper([1, a, 1 + Rational(1, 2)], [a + 1, Rational(1, 2)], z)) == \
        -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a)

    # Now numerical tests. These make sure reductions etc are carried out
    # correctly

    # a rational function (polylog at negative integer order)
    assert can_do([2, 2, 2], [1, 1])

    # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1
    # reduction of order for polylog
    assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10)

    # reduction of order for lerchphi
    # XXX lerchphi in mpmath is flaky
    assert can_do(
        [1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b], numerical=False)

    # test a bug
    assert hyperexpand(hyper([Rational(1, 2), Rational(1, 2), Rational(1, 2), 1],
                             [Rational(3, 2), Rational(3, 2), Rational(3, 2)], Rational(1, 4))) == \
        abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, Rational(1, 2)))
def test_lerchphi_expansion():
    assert myexpand(lerchphi(1, s, a), zeta(s, a))
    assert myexpand(lerchphi(z, s, 1), polylog(s, z)/z)

    # direct summation
    assert myexpand(lerchphi(z, -1, a), a/(1 - z) + z/(1 - z)**2)
    assert myexpand(lerchphi(z, -3, a), None)

    # polylog reduction
    assert myexpand(lerchphi(z, s, Rational(1, 2)),
                    2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z)
                                - polylog(s, polar_lift(-1)*sqrt(z))/sqrt(z)))
    assert myexpand(lerchphi(z, s, 2), -1/z + polylog(s, z)/z**2)
    assert myexpand(lerchphi(z, s, Rational(3, 2)), None)
    assert myexpand(lerchphi(z, s, Rational(7, 3)), None)
    assert myexpand(lerchphi(z, s, -Rational(1, 3)), None)
    assert myexpand(lerchphi(z, s, -Rational(5, 2)), None)

    # hurwitz zeta reduction
    assert myexpand(lerchphi(-1, s, a),
                    2**(-s)*zeta(s, a/2) - 2**(-s)*zeta(s, (a + 1)/2))
    assert myexpand(lerchphi(I, s, a), None)
    assert myexpand(lerchphi(-I, s, a), None)
    assert myexpand(lerchphi(exp(2*I*pi/5), s, a), None)
Exemple #11
0
def test_polylog_expansion():
    assert myexpand(polylog(1, z), -log(1 - z))
    assert myexpand(polylog(0, z), z / (1 - z))
    assert myexpand(polylog(-1, z), z**2 / (1 - z)**2 + z / (1 - z))
    assert myexpand(polylog(-5, z), None)
Exemple #12
0
def test_Function():
    assert mathematica_code(f(x, y, z)) == 'f[x, y, z]'
    assert mathematica_code(sin(x)**cos(x)) == 'Sin[x]^Cos[x]'
    assert mathematica_code(sign(x)) == 'Sign[x]'

    assert mathematica_code(atanh(x),
                            user_functions={'atanh':
                                            'ArcTanh'}) == 'ArcTanh[x]'

    assert (mathematica_code(meijerg(
        ((1, 1), (3, 4)), ((1, ), ()),
        x)) == 'MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]')
    assert (mathematica_code(hyper(
        (1, 2, 3), (3, 4), x)) == 'HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]')

    assert mathematica_code(Min(x, y)) == 'Min[x, y]'
    assert mathematica_code(Max(x, y)) == 'Max[x, y]'
    assert mathematica_code(Max(x,
                                2)) == 'Max[2, x]'  # issue sympy/sympy#15344

    assert mathematica_code(binomial(x, y)) == 'Binomial[x, y]'

    assert mathematica_code(log(x)) == 'Log[x]'
    assert mathematica_code(tan(x)) == 'Tan[x]'
    assert mathematica_code(cot(x)) == 'Cot[x]'
    assert mathematica_code(asin(x)) == 'ArcSin[x]'
    assert mathematica_code(acos(x)) == 'ArcCos[x]'
    assert mathematica_code(atan(x)) == 'ArcTan[x]'
    assert mathematica_code(acot(x)) == 'ArcCot[x]'
    assert mathematica_code(sinh(x)) == 'Sinh[x]'
    assert mathematica_code(cosh(x)) == 'Cosh[x]'
    assert mathematica_code(tanh(x)) == 'Tanh[x]'
    assert mathematica_code(coth(x)) == 'Coth[x]'
    assert mathematica_code(asinh(x)) == 'ArcSinh[x]'
    assert mathematica_code(acosh(x)) == 'ArcCosh[x]'
    assert mathematica_code(atanh(x)) == 'ArcTanh[x]'
    assert mathematica_code(acoth(x)) == 'ArcCoth[x]'
    assert mathematica_code(sech(x)) == 'Sech[x]'
    assert mathematica_code(csch(x)) == 'Csch[x]'
    assert mathematica_code(erf(x)) == 'Erf[x]'
    assert mathematica_code(erfi(x)) == 'Erfi[x]'
    assert mathematica_code(erfc(x)) == 'Erfc[x]'
    assert mathematica_code(conjugate(x)) == 'Conjugate[x]'
    assert mathematica_code(re(x)) == 'Re[x]'
    assert mathematica_code(im(x)) == 'Im[x]'
    assert mathematica_code(polygamma(x, y)) == 'PolyGamma[x, y]'
    assert mathematica_code(factorial(x)) == 'Factorial[x]'
    assert mathematica_code(factorial2(x)) == 'Factorial2[x]'
    assert mathematica_code(rf(x, y)) == 'Pochhammer[x, y]'
    assert mathematica_code(gamma(x)) == 'Gamma[x]'
    assert mathematica_code(zeta(x)) == 'Zeta[x]'
    assert mathematica_code(Heaviside(x)) == 'UnitStep[x]'
    assert mathematica_code(fibonacci(x)) == 'Fibonacci[x]'
    assert mathematica_code(polylog(x, y)) == 'PolyLog[x, y]'
    assert mathematica_code(loggamma(x)) == 'LogGamma[x]'
    assert mathematica_code(uppergamma(x, y)) == 'Gamma[x, y]'

    class MyFunc1(Function):
        @classmethod
        def eval(cls, x):
            pass

    class MyFunc2(Function):
        @classmethod
        def eval(cls, x, y):
            pass

    pytest.raises(
        ValueError,
        lambda: mathematica_code(MyFunc1(x),
                                 user_functions={'MyFunc1': ['Myfunc1']}))
    assert mathematica_code(MyFunc1(x),
                            user_functions={'MyFunc1':
                                            'Myfunc1'}) == 'Myfunc1[x]'
    assert mathematica_code(
        MyFunc2(x, y),
        user_functions={'MyFunc2':
                        [(lambda *x: False, 'Myfunc2')]}) == 'MyFunc2[x, y]'
def test_polylog_expansion():
    assert myexpand(polylog(1, z), -log(1 - z))
    assert myexpand(polylog(0, z), z/(1 - z))
    assert myexpand(polylog(-1, z), z**2/(1 - z)**2 + z/(1 - z))
    assert myexpand(polylog(-5, z), None)
def test_polylog_eval():
    assert polylog(s, 0) == 0
    assert polylog(s, 1) == zeta(s)
    assert polylog(s, -1) == -dirichlet_eta(s)
    assert polylog(s, exp_polar(I*pi)) == polylog(s, -1)
    assert polylog(s, 2*exp_polar(2*I*pi)) == polylog(s, 2*exp_polar(2*I*pi), evaluate=False)
Exemple #15
0
    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)