示例#1
0
def test_hankel_transform():
    r = Symbol("r")
    k = Symbol("k")
    nu = Symbol("nu")
    m = Symbol("m")

    assert hankel_transform(1/r, r, k, 0) == 1/k
    assert inverse_hankel_transform(1/k, k, r, 0) == 1/r

    assert hankel_transform(
        1/r**m, r, k, 0) == 2**(-m + 1)*k**(m - 2)*gamma(-m/2 + 1)/gamma(m/2)
    assert inverse_hankel_transform(
        2**(-m + 1)*k**(m - 2)*gamma(-m/2 + 1)/gamma(m/2), k, r, 0) == r**(-m)

    assert hankel_transform(1/r**m, r, k, nu) == (
        2*2**(-m)*k**(m - 2)*gamma(-m/2 + nu/2 + 1)/gamma(m/2 + nu/2))
    assert inverse_hankel_transform(2**(-m + 1)*k**(
        m - 2)*gamma(-m/2 + nu/2 + 1)/gamma(m/2 + nu/2), k, r, nu) == r**(-m)

    assert hankel_transform(r**nu*exp(-a*r), r, k, nu) == \
        2**(nu + 1)*a*k**(-nu - 3)*(a**2/k**2 + 1)**(-nu -
                                                     Rational(3, 2))*gamma(nu + Rational(3, 2))/sqrt(pi)
    assert inverse_hankel_transform(
        2**(nu + 1)*a*k**(-nu - 3)*(a**2/k**2 + 1)**(-nu - Rational(3, 2))*gamma(
            nu + Rational(3, 2))/sqrt(pi), k, r, nu) == r**nu*exp(-a*r)
示例#2
0
def test_polarify():
    z = Symbol('z', polar=True)
    f = Function('f')
    ES = {}

    assert polarify(-1) == (polar_lift(-1), ES)
    assert polarify(1 + I) == (polar_lift(1 + I), ES)

    assert polarify(exp(x), subs=False) == exp(x)
    assert polarify(1 + x, subs=False) == 1 + x
    assert polarify(f(I) + x, subs=False) == f(polar_lift(I)) + x

    assert polarify(x, lift=True) == polar_lift(x)
    assert polarify(z, lift=True) == z
    assert polarify(f(x), lift=True) == f(polar_lift(x))
    assert polarify(1 + x, lift=True) == polar_lift(1 + x)
    assert polarify(1 + f(x), lift=True) == polar_lift(1 + f(polar_lift(x)))

    newex, subs = polarify(f(x) + z)
    assert newex.subs(subs) == f(x) + z

    mu = Symbol("mu")
    sigma = Symbol("sigma", positive=True)

    # Make sure polarify(lift=True) doesn't try to lift the integration
    # variable
    assert polarify(
        Integral(sqrt(2)*x*exp(-(-mu + x)**2/(2*sigma**2))/(2*sqrt(pi)*sigma),
                 (x, -oo, oo)), lift=True) == Integral(sqrt(2)*(sigma*exp_polar(0))**exp_polar(I*pi) *
                                                       exp((sigma*exp_polar(0))**(2*exp_polar(I*pi))*exp_polar(I*pi)*polar_lift(-mu + x) **
                                                           (2*exp_polar(0))/2)*exp_polar(0)*polar_lift(x)/(2*sqrt(pi)), (x, -oo, oo))
示例#3
0
def test_DifferentialExtension_all_attrs():
    # Test 'unimportant' attributes
    DE = DifferentialExtension(exp(x)*log(x), x, dummy=False, handle_first='exp')
    assert DE.f == exp(x)*log(x)
    assert DE.newf == t0*t1
    assert DE.x == x
    assert DE.cases == ['base', 'exp', 'primitive']
    assert DE.case == 'primitive'

    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1/x, t1) == DE.D[DE.level]
    pytest.raises(ValueError, lambda: DE.increment_level())
    DE.decrement_level()
    assert DE.level == -2
    assert DE.t == t0 == DE.T[DE.level]
    assert DE.d == Poly(t0, t0) == DE.D[DE.level]
    assert DE.case == 'exp'
    DE.decrement_level()
    assert DE.level == -3
    assert DE.t == x == DE.T[DE.level] == DE.x
    assert DE.d == Poly(1, x) == DE.D[DE.level]
    assert DE.case == 'base'
    pytest.raises(ValueError, lambda: DE.decrement_level())
    DE.increment_level()
    DE.increment_level()
    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1/x, t1) == DE.D[DE.level]
    assert DE.case == 'primitive'
示例#4
0
def test_unevaluated():
    X = Normal('x', 0, 1)
    assert E(X, evaluate=False) == (
        Integral(sqrt(2)*x*exp(-x**2/2)/(2*sqrt(pi)), (x, -oo, oo)))

    assert E(X + 1, evaluate=False) == (
        Integral(sqrt(2)*x*exp(-x**2/2)/(2*sqrt(pi)), (x, -oo, oo)) + 1)
示例#5
0
def test_transform():
    a = Integral(x**2 + 1, (x, -1, 2))
    fx = x
    fy = 3*y + 1
    assert a.doit() == a.transform(fx, fy).doit()
    assert a.transform(fx, fy).transform(fy, fx) == a
    fx = 3*x + 1
    fy = y
    assert a.transform(fx, fy).transform(fy, fx) == a
    a = Integral(sin(1/x), (x, 0, 1))
    assert a.transform(x, 1/y) == Integral(sin(y)/y**2, (y, 1, oo))
    assert a.transform(x, 1/y).transform(y, 1/x) == a
    a = Integral(exp(-x**2), (x, -oo, oo))
    assert a.transform(x, 2*y) == Integral(2*exp(-4*y**2), (y, -oo, oo))
    # < 3 arg limit handled properly
    assert Integral(x, x).transform(x, a*y).doit() == \
        Integral(y*a**2, y).doit()
    assert Integral(x, (x, 0, -3)).transform(x, 1/y).doit() == \
        Integral(-1/x**3, (x, -oo, Rational(-1, 3))).doit()
    assert Integral(x, (x, 0, 3)).transform(x, 1/y) == \
        Integral(y**(-3), (y, Rational(1, 3), oo))
    # issue sympy/sympy#8400
    i = Integral(x + y, (x, 1, 2), (y, 1, 2))
    assert i.transform(x, (x + 2*y, x)).doit() == \
        i.transform(x, (x + 2*z, x)).doit() == 3
示例#6
0
def test_sympyissue_4199():
    ypos = Symbol('y', positive=True)
    # TODO: Remove conds='none' below, let the assumption take care of it.
    assert (integrate(exp(-I*2*pi*ypos*x)*x, (x, -oo, oo), conds='none') ==
            Integral(exp(-I*2*pi*ypos*x)*x, (x, -oo, oo)))
    assert (integrate(exp(-I*2*pi*ypos*x)*x, (x, 0, oo), conds='none') ==
            Integral(exp(-2*I*pi*x*ypos)*x, (x, 0, oo)))
示例#7
0
def test_integrate_linearterm_pow():
    # check integrate((a*x+b)^c, x)  --  issue sympy/sympy#3499
    y = Symbol('y', positive=True)
    # TODO: Remove conds='none' below, let the assumption take care of it.
    assert integrate(x**y, x, conds='none') == x**(y + 1)/(y + 1)
    assert integrate((exp(y)*x + 1/y)**(1 + sin(y)), x, conds='none') == \
        exp(-y)*(exp(y)*x + 1/y)**(2 + sin(y)) / (2 + sin(y))
示例#8
0
def test_equal():
    b = Symbol("b")
    a = Symbol("a")
    e1 = a + b
    e2 = 2*a*b
    e3 = a**3*b**2
    e4 = a*b + b*a
    assert not e1 == e2
    assert not e1 == e2
    assert e1 != e2
    assert e2 == e4
    assert e2 != e3
    assert not e2 == e3

    x = Symbol("x")
    e1 = exp(x + 1/x)
    y = Symbol("x")
    e2 = exp(y + 1/y)
    assert e1 == e2
    assert not e1 != e2
    y = Symbol("y")
    e2 = exp(y + 1/y)
    assert not e1 == e2
    assert e1 != e2

    e5 = 3 + 2*x - x - x
    assert e5 == 3
    assert 3 == e5
    assert e5 != 4
    assert 4 != e5
    assert e5 != 3 + x
    assert 3 + x != e5
示例#9
0
def test_rewrite():
    assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - Rational(1, 2), z)
    assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - Rational(1, 2), z)
    assert besseli(n, z).rewrite(besselj) == \
        exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z)
    assert besselj(n, z).rewrite(besseli) == \
        exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z)
    assert besselj(2, z).rewrite(bessely) == besselj(2, z)
    assert bessely(2, z).rewrite(besselj) == bessely(2, z)
    assert bessely(2, z).rewrite(besseli) == bessely(2, z)
    assert besselk(2, z).rewrite(besseli) == besselk(2, z)
    assert besselk(2, z).rewrite(besselj) == besselk(2, z)
    assert besselk(2, z).rewrite(bessely) == besselk(2, z)

    nu = randcplx()

    assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z)
    assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z)

    assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z)
    assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z)

    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z)
    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z)

    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z)
示例#10
0
def test_lognormal():
    mean = Symbol('mu', real=True)
    std = Symbol('sigma', positive=True, real=True)
    X = LogNormal('x', mean, std)
    # The diofant integrator can't do this too well
    # assert E(X) == exp(mean+std**2/2)
    # assert variance(X) == (exp(std**2)-1) * exp(2*mean + std**2)

    # Right now, only density function and sampling works
    # Test sampling: Only e^mean in sample std of 0
    for i in range(3):
        X = LogNormal('x', i, 0)
        assert sample(X) == N(exp(i))
    # The diofant integrator can't do this too well
    # assert E(X) ==

    mu = Symbol("mu", extended_real=True)
    sigma = Symbol("sigma", positive=True)

    X = LogNormal('x', mu, sigma)
    assert density(X)(x) == (sqrt(2)*exp(-(-mu + log(x))**2
                                         / (2*sigma**2))/(2*x*sqrt(pi)*sigma))

    X = LogNormal('x', 0, 1)  # Mean 0, standard deviation 1
    assert density(X)(x) == sqrt(2)*exp(-log(x)**2/2)/(2*x*sqrt(pi))
示例#11
0
def test_branching():
    assert besselj(polar_lift(k), x) == besselj(k, x)
    assert besseli(polar_lift(k), x) == besseli(k, x)

    n = Symbol('n', integer=True)
    assert besselj(n, exp_polar(2*pi*I)*x) == besselj(n, x)
    assert besselj(n, polar_lift(x)) == besselj(n, x)
    assert besseli(n, exp_polar(2*pi*I)*x) == besseli(n, x)
    assert besseli(n, polar_lift(x)) == besseli(n, x)

    def tn(func, s):
        c = uniform(1, 5)
        expr = func(s, c*exp_polar(I*pi)) - func(s, c*exp_polar(-I*pi))
        eps = 1e-15
        expr2 = func(s + eps, -c + eps*I) - func(s + eps, -c - eps*I)
        return abs(expr - expr2).evalf(strict=False) < 1e-10

    nu = Symbol('nu')
    assert besselj(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besselj(nu, x)
    assert besseli(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besseli(nu, x)
    assert tn(besselj, 2)
    assert tn(besselj, pi)
    assert tn(besselj, I)
    assert tn(besseli, 2)
    assert tn(besseli, pi)
    assert tn(besseli, I)
示例#12
0
def test_cosine_transform():
    f = Function("f")

    # Test unevaluated form
    assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w)
    assert inverse_cosine_transform(
        f(w), w, t) == InverseCosineTransform(f(w), w, t)

    assert cosine_transform(1/sqrt(t), t, w) == 1/sqrt(w)
    assert inverse_cosine_transform(1/sqrt(w), w, t) == 1/sqrt(t)

    assert cosine_transform(1/(
        a**2 + t**2), t, w) == sqrt(2)*sqrt(pi)*exp(-a*w)/(2*a)

    assert cosine_transform(t**(
        -a), t, w) == 2**(-a + Rational(1, 2))*w**(a - 1)*gamma((-a + 1)/2)/gamma(a/2)
    assert inverse_cosine_transform(2**(-a +
                                        Rational(1, 2))*w**(a - 1)*gamma(-a/2 + Rational(1, 2))/gamma(a/2), w, t) == t**(-a)

    assert cosine_transform(
        exp(-a*t), t, w) == sqrt(2)*a/(sqrt(pi)*(a**2 + w**2))
    assert inverse_cosine_transform(
        sqrt(2)*a/(sqrt(pi)*(a**2 + w**2)), w, t) == exp(-a*t)

    assert cosine_transform(exp(-a*sqrt(t))*cos(a*sqrt(
        t)), t, w) == a*exp(-a**2/(2*w))/(2*w**Rational(3, 2))

    assert cosine_transform(1/(a + t), t, w) == sqrt(2)*(
        (-2*Si(a*w) + pi)*sin(a*w)/2 - cos(a*w)*Ci(a*w))/sqrt(pi)
    assert inverse_cosine_transform(sqrt(2)*meijerg(((Rational(1, 2), 0), ()), (
        (Rational(1, 2), 0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*pi), w, t) == 1/(a + t)

    assert cosine_transform(1/sqrt(a**2 + t**2), t, w) == sqrt(2)*meijerg(
        ((Rational(1, 2),), ()), ((0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*sqrt(pi))
    assert inverse_cosine_transform(sqrt(2)*meijerg(((Rational(1, 2),), ()), ((0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*sqrt(pi)), w, t) == 1/(a*sqrt(1 + t**2/a**2))
示例#13
0
def test_sympyissue_5261():
    x = symbols('x', extended_real=True)
    e = I*x
    assert exp(e).subs({exp(x): y}) == y**I
    assert (2**e).subs({2**x: y}) == y**I
    eq = (-2)**e
    assert eq.subs({(-2)**x: y}) == eq
示例#14
0
def test_add():
    assert (a**2 - b - c).subs({a**2 - b: d}) in [d - c, a**2 - b - c]
    assert (a**2 - c).subs({a**2 - c: d}) == d
    assert (a**2 - b - c).subs({a**2 - c: d}) in [d - b, a**2 - b - c]
    assert (a**2 - x - c).subs({a**2 - c: d}) in [d - x, a**2 - x - c]
    assert (a**2 - b - sqrt(a)).subs({a**2 - sqrt(a): c}) == c - b
    assert (a + b + exp(a + b)).subs({a + b: c}) == c + exp(c)
    assert (c + b + exp(c + b)).subs({c + b: a}) == a + exp(a)
    assert (a + b + c + d).subs({b + c: x}) == a + d + x
    assert (a + b + c + d).subs({-b - c: x}) == a + d - x
    assert ((x + 1)*y).subs({x + 1: t}) == t*y
    assert ((-x - 1)*y).subs({x + 1: t}) == -t*y
    assert ((x - 1)*y).subs({x + 1: t}) == y*(t - 2)
    assert ((-x + 1)*y).subs({x + 1: t}) == y*(-t + 2)

    # this should work everytime:
    e = a**2 - b - c
    assert e.subs({Add(*e.args[:2]): d}) == d + e.args[2]
    assert e.subs({a**2 - c: d}) == d - b

    # the fallback should recognize when a change has
    # been made; while .1 == Rational(1, 10) they are not the same
    # and the change should be made
    assert (0.1 + a).subs({0.1: Rational(1, 10)}) == Rational(1, 10) + a

    e = (-x*(-y + 1) - y*(y - 1))
    ans = (-x*x - y*(-x)).expand()
    assert e.subs({-y + 1: x}) == ans
示例#15
0
def test_exponential():
    x = Symbol('x', extended_real=True)
    assert limit((1 + x/n)**n, n, oo) == exp(x)
    assert limit((1 + x/(2*n))**n, n, oo) == exp(x/2)
    assert limit((1 + x/(2*n + 1))**n, n, oo) == exp(x/2)
    assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)
    assert limit(1 + (1 + 1/x)**x, x, oo) == 1 + E
示例#16
0
def test_ode_solutions():
    # only a few examples here, the rest will be tested in the actual dsolve tests
    assert constant_renumber(constantsimp(C1*exp(2*x) + exp(x)*(C2 + C3), [C1, C2, C3]), 'C', 1, 3) == \
        constant_renumber((C1*exp(x) + C2*exp(2*x)), 'C', 1, 2)
    assert constant_renumber(
        constantsimp(Eq(f(x), I*C1*sinh(x/3) + C2*cosh(x/3)), [C1, C2]),
        'C', 1, 2) == constant_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2)
    assert constant_renumber(constantsimp(Eq(f(x), acos((-C1)/cos(x))), [C1]), 'C', 1, 1) == \
        Eq(f(x), acos(C1/cos(x)))
    assert constant_renumber(
        constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), [C1]),
        'C', 1, 1) == Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0)
    assert constant_renumber(constantsimp(Eq(log(x*sqrt(2)*sqrt(1/x)*sqrt(f(x))
                                                 / C1) + x**2/(2*f(x)**2), 0), [C1]), 'C', 1, 1) == \
        Eq(log(C1*sqrt(x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0)
    assert constant_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) -
                                             cos(f(x)/x)*exp(-f(x)/x)/2, 0), [C1]), 'C', 1, 1) == \
        Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x) *
           exp(-f(x)/x)/2, 0)
    u2 = Symbol('u2')
    _a = Symbol('_a')
    assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2),
                                                       (u2, _a, x/f(x))) + log(f(x)/C1), 0), [C1]), 'C', 1, 1) == \
        Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) +
           log(C1*f(x)), 0)
    assert [constantsimp(i, [C1]) for i in [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x + x**2))]] == \
        [Eq(f(x), sqrt(x*(C1 + x))), Eq(f(x), -sqrt(x*(C1 + x)))]
示例#17
0
def test_dict_ambigous():   # see issue sympy/sympy#3566
    y = Symbol('y')
    z = Symbol('z')

    f = x*exp(x)
    g = z*exp(z)

    df = {x: y, exp(x): y}
    dg = {z: y, exp(z): y}

    assert f.subs(df) == y**2
    assert g.subs(dg) == y**2

    # and this is how order can affect the result
    assert f.subs({x: y}).subs({exp(x): y}) == y*exp(y)
    assert f.subs({exp(x): y}).subs({x: y}) == y**2

    # length of args and count_ops are the same so
    # default_sort_key resolves ordering...if one
    # doesn't want this result then an unordered
    # sequence should not be used.
    e = 1 + x*y
    assert e.subs({x: y, y: 2}) == 5
    # here, there are no obviously clashing keys or values
    # but the results depend on the order
    assert exp(x/2 + y).subs({exp(y + 1): 2, x: 2}) == exp(y + 1)
示例#18
0
def test_real_imag():
    x, y, z = symbols('x, y, z')
    X, Y, Z = symbols('X, Y, Z', commutative=False)
    a = Symbol('a', extended_real=True)
    assert (2*a*x).as_real_imag() == (2*a*re(x), 2*a*im(x))

    # issue sympy/sympy#5395:
    assert (x*x.conjugate()).as_real_imag() == (Abs(x)**2, 0)
    assert im(x*x.conjugate()) == 0
    assert im(x*y.conjugate()*z*y) == im(x*z)*Abs(y)**2
    assert im(x*y.conjugate()*x*y) == im(x**2)*Abs(y)**2
    assert im(Z*y.conjugate()*X*y) == im(Z*X)*Abs(y)**2
    assert im(X*X.conjugate()) == im(X*X.conjugate(), evaluate=False)
    assert (sin(x)*sin(x).conjugate()).as_real_imag() == \
        (Abs(sin(x))**2, 0)

    # issue sympy/sympy#6573:
    assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2*re(x)*im(x))

    # issue sympy/sympy#6428:
    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)
    assert (i*r*x).as_real_imag() == (I*i*r*im(x), -I*i*r*re(x))
    assert (i*r*x*(y + 2)).as_real_imag() == (
        I*i*r*(re(y) + 2)*im(x) + I*i*r*re(x)*im(y),
        -I*i*r*(re(y) + 2)*re(x) + I*i*r*im(x)*im(y))

    # issue sympy/sympy#7106:
    assert ((1 + I)/(1 - I)).as_real_imag() == (0, 1)
    assert ((1 + 2*I)*(1 + 3*I)).as_real_imag() == (-5, 5)

    assert exp(x).as_real_imag(deep=False) == (re(exp(x)), im(exp(x)))
    assert (2**x).as_real_imag(deep=False) == (re(2**x), im(2**x))
示例#19
0
def test_RootSum_diff():
    f = x**3 + x + 3

    g = Lambda(r, exp(r*x))
    h = Lambda(r, r*exp(r*x))

    assert RootSum(f, g).diff(x) == RootSum(f, h)
示例#20
0
def test_evalf_ramanujan():
    assert NS(exp(pi*sqrt(163)) - 640320**3 - 744, 10) == '-7.499274028e-13'
    # A related identity
    A = 262537412640768744*exp(-pi*sqrt(163))
    B = 196884*exp(-2*pi*sqrt(163))
    C = 103378831900730205293632*exp(-3*pi*sqrt(163))
    assert NS(1 - A - B + C, 10) == '1.613679005e-59'
示例#21
0
def test_sympyissue_5673():
    e = LambertW(-1)
    assert e.is_comparable is False
    assert e.is_positive is not True
    e2 = 1 - 1/(1 - exp(-1000))
    assert e2.is_positive is not True
    e3 = -2 + exp(exp(LambertW(log(2)))*LambertW(log(2)))
    assert e3.is_nonzero is not True
示例#22
0
def test_pmint_WrightOmega():
    def omega(x):
        return LambertW(exp(x))

    f = (1 + omega(x) * (2 + cos(omega(x)) * (x + omega(x))))/(1 + omega(x))/(x + omega(x))
    g = log(x + LambertW(exp(x))) + sin(LambertW(exp(x)))

    assert heurisch(f, x) == g
示例#23
0
def test_fisher_z():
    d1 = Symbol("d1", positive=True)
    d2 = Symbol("d2", positive=True)

    X = FisherZ("x", d1, d2)
    assert density(X)(x) == (2*d1**(d1/2)*d2**(d2/2) *
                             (d1*exp(2*x) + d2)**(-d1/2 - d2/2) *
                             exp(d1*x)/beta(d1/2, d2/2))
示例#24
0
def test_unevaluated_fail():
    X = Normal('x', 0, 1)
    assert P(X > 0, evaluate=False) == (
        Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)), (x, 0, oo)))

    assert P(X > 0, X**2 < 1, evaluate=False) == (
        Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi) *
                                       Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)),
                                                (x, -1, 1))), (x, 0, 1)))
示例#25
0
def test_RootSum_subs():
    f = x**3 + x + 3
    g = Lambda(r, exp(r*x))

    F = y**3 + y + 3
    G = Lambda(r, exp(r*y))

    assert RootSum(f, g).subs({y: 1}) == RootSum(f, g)
    assert RootSum(f, g).subs({x: y}) == RootSum(F, G)
示例#26
0
def test_exp_log():
    x = Symbol("x", extended_real=True)
    assert log(exp(x)) == x
    assert exp(log(x)) == x
    assert log(x).inverse() == exp

    y = Symbol("y", polar=True)
    assert log(exp_polar(z)) == z
    assert exp(log(y)) == y
示例#27
0
def test_basic1():
    assert limit(x, x, oo) == oo
    assert limit(x, x, -oo) == -oo
    assert limit(-x, x, oo) == -oo
    assert limit(x**2, x, -oo) == oo
    assert limit(-x**2, x, oo) == -oo
    assert limit(x*log(x), x, 0, dir="+") == 0
    assert limit(1/x, x, oo) == 0
    assert limit(exp(x), x, oo) == oo
    assert limit(-exp(x), x, oo) == -oo
    assert limit(exp(x)/x, x, oo) == oo
    assert limit(1/x - exp(-x), x, oo) == 0
    assert limit(x + 1/x, x, oo) == oo
    assert limit(x - x**2, x, oo) == -oo
    assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
    assert limit((1 + x)**oo, x, 0) == oo
    assert limit((1 + x)**oo, x, 0, dir='-') == 0
    assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo
    assert limit(y/x/log(x), x, 0) == -oo*sign(y)
    assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
    limit(Sum(1/x, (x, 1, y)) - log(y), y, oo)
    limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo)
    assert limit(gamma(1/x + 3), x, oo) == 2
    assert limit(nan, x, -oo) == nan
    assert limit(O(2)*x, x, nan) == nan
    assert limit(sin(O(x)), x, 0) == 0
    assert limit(1/(x - 1), x, 1, dir="+") == oo
    assert limit(1/(x - 1), x, 1, dir="-") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="-") == oo
    assert limit(1/sin(x), x, pi, dir="+") == -oo
    assert limit(1/sin(x), x, pi, dir="-") == oo
    assert limit(1/cos(x), x, pi/2, dir="+") == -oo
    assert limit(1/cos(x), x, pi/2, dir="-") == oo
    assert limit(1/tan(x**3), x, cbrt(2*pi), dir="+") == oo
    assert limit(1/tan(x**3), x, cbrt(2*pi), dir="-") == -oo
    assert limit(1/cot(x)**3, x, 3*pi/2, dir="+") == -oo
    assert limit(1/cot(x)**3, x, 3*pi/2, dir="-") == oo

    # approaching 0
    # from dir="+"
    assert limit(1 + 1/x, x, 0) == oo
    # from dir='-'
    # Add
    assert limit(1 + 1/x, x, 0, dir='-') == -oo
    # Pow
    assert limit(x**(-2), x, 0, dir='-') == oo
    assert limit(x**(-3), x, 0, dir='-') == -oo
    assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
    assert limit(x**2, x, 0, dir='-') == 0
    assert limit(sqrt(x), x, 0, dir='-') == 0
    assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi))
    assert limit((1 + cos(x))**oo, x, 0) == oo

    assert limit(x**2, x, 0, dir='real') == 0
    assert limit(exp(x), x, 0, dir='real') == 1
    pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
示例#28
0
def test_expbug4():
    x = Symbol("x", extended_real=True)
    assert (log(
        sin(2*x)/x)*(1 + x)).series(x, 0, 2) == log(2) + x*log(2) + O(x**2)
    assert exp(
        log(sin(2*x)/x)*(1 + x)).series(x, 0, 2) == 2 + 2*x*log(2) + O(x**2)

    assert exp(log(2) + O(x)).nseries(x, 1) == 2 + O(x)
    assert ((2 + O(x))**(1 + x)).nseries(x, 1) == 2 + O(x)
示例#29
0
def test_deltaintegrate():
    assert deltaintegrate(x, x) is None
    assert deltaintegrate(x + DiracDelta(x), x) is None
    assert deltaintegrate(DiracDelta(x, 0), x) == Heaviside(x)
    for n in range(10):
        assert deltaintegrate(DiracDelta(x, n + 1), x) == DiracDelta(x, n)
    assert deltaintegrate(DiracDelta(x), x) == Heaviside(x)
    assert deltaintegrate(DiracDelta(-x), x) == Heaviside(x)
    assert deltaintegrate(DiracDelta(x - y), x) == Heaviside(x - y)
    assert deltaintegrate(DiracDelta(y - x), x) == Heaviside(x - y)

    assert deltaintegrate(x*DiracDelta(x), x) == 0
    assert deltaintegrate((x - y)*DiracDelta(x - y), x) == 0

    assert deltaintegrate(DiracDelta(x)**2, x) == DiracDelta(0)*Heaviside(x)
    assert deltaintegrate(y*DiracDelta(x)**2, x) == \
        y*DiracDelta(0)*Heaviside(x)
    assert deltaintegrate(DiracDelta(x, 1)**2, x) is None
    assert deltaintegrate(y*DiracDelta(x, 1)**2, x) is None

    assert deltaintegrate(DiracDelta(x) * f(x), x) == f(0) * Heaviside(x)
    assert deltaintegrate(DiracDelta(-x) * f(x), x) == f(0) * Heaviside(x)
    assert deltaintegrate(DiracDelta(x - 1) * f(x), x) == f(1) * Heaviside(x - 1)
    assert deltaintegrate(DiracDelta(1 - x) * f(x), x) == f(1) * Heaviside(x - 1)
    assert deltaintegrate(DiracDelta(x**2 + x - 2), x) == \
        Heaviside(x - 1)/3 + Heaviside(x + 2)/3

    p = cos(x)*(DiracDelta(x) + DiracDelta(x**2 - 1))*sin(x)*(x - pi)
    assert deltaintegrate(p, x) - (-pi*(cos(1)*Heaviside(-1 + x)*sin(1)/2 -
                                        cos(1)*Heaviside(1 + x)*sin(1)/2) +
                                   cos(1)*Heaviside(1 + x)*sin(1)/2 +
                                   cos(1)*Heaviside(-1 + x)*sin(1)/2) == 0

    p = x_2*DiracDelta(x - x_2)*DiracDelta(x_2 - x_1)
    assert deltaintegrate(p, x_2) == x*DiracDelta(x - x_1)*Heaviside(x_2 - x)

    p = x*y**2*z*DiracDelta(y - x)*DiracDelta(y - z)*DiracDelta(x - z)
    assert deltaintegrate(p, y) == x**3*z*DiracDelta(x - z)**2*Heaviside(y - x)
    assert deltaintegrate((x + 1)*DiracDelta(2*x), x) == Rational(1, 2) * Heaviside(x)
    assert deltaintegrate((x + 1)*DiracDelta(2*x/3 + Rational(4, 9)), x) == \
        Rational(1, 2) * Heaviside(x + Rational(2, 3))

    a, b, c = symbols('a b c', commutative=False)
    assert deltaintegrate(DiracDelta(x - y)*f(x - b)*f(x - a), x) == \
        f(y - b)*f(y - a)*Heaviside(x - y)

    p = f(x - a)*DiracDelta(x - y)*f(x - c)*f(x - b)
    assert deltaintegrate(p, x) == f(y - a)*f(y - c)*f(y - b)*Heaviside(x - y)

    p = DiracDelta(x - z)*f(x - b)*f(x - a)*DiracDelta(x - y)
    assert deltaintegrate(p, x) == DiracDelta(y - z)*f(y - b)*f(y - a) * \
        Heaviside(x - y)

    # for test coverage
    assert deltaintegrate(DiracDelta(exp(x) - 1), x) is None
    assert deltaintegrate((x - 1)*DiracDelta(exp(x) - 1), x) is None
示例#30
0
def test_functions_subs():
    f, g = symbols('f g', cls=Function)
    l = Lambda((x, y), sin(x) + y)
    assert (g(y, x) + cos(x)).subs({g: l}) == sin(y) + x + cos(x)
    assert (f(x)**2).subs({f: sin}) == sin(x)**2
    assert (f(x, y)).subs({f: log}) == log(x, y)
    assert (f(x, y)).subs({f: sin}) == f(x, y)
    assert (sin(x) + atan2(x, y)).subs({atan2: f, sin: g}) == \
        f(x, y) + g(x)
    assert (g(f(x + y, x))).subs({f: l, g: Lambda(x, exp(x))}) == exp(x + sin(x + y))
示例#31
0
def test_ignore_order_terms():
    eq = exp(x).series(x, 0, 3) + sin(y + x**3) - 1
    assert cse(eq) == ([], [sin(x**3 + y) + x + x**2 / 2 + O(x**3)])
示例#32
0
 def exponential(x, rate):
     return rate*exp(-rate*x)
示例#33
0
def test_sinh_rewrite():
    assert sinh(x).rewrite(exp) == (exp(x) - exp(-x))/2 \
        == sinh(x).rewrite('tractable')
    assert sinh(x).rewrite(cosh) == -I*cosh(x + I*pi/2)
    assert sinh(x).rewrite(tanh) == 2*tanh(x/2)/(1 - tanh(x/2)**2)
    assert sinh(x).rewrite(coth) == 2*coth(x/2)/(coth(x/2)**2 - 1)
示例#34
0
def test_minpoly_compose():
    # issue sympy/sympy#6868
    eq = (-1 / (800 * sqrt(
        Rational(-1, 240) + 1 /
        (18000 *
         (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3)) +
        2 *
        (Rational(-1, 17280000) + sqrt(15) * I / 28800000)**Rational(1, 3))))
    mp = minimal_polynomial(eq + 3, x)
    assert mp == 8000 * x**2 - 48000 * x + 71999

    # issue sympy/sympy#5888
    assert minimal_polynomial(exp(I * pi / 8), x) == x**8 + 1

    mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x)
    assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
        770912*x**4 - 268432*x**2 + 28561
    mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x)
    assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
            232*x - 239
    mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x)
    assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127

    mp = minimal_polynomial(cos(pi / 9), x)
    assert mp == 8 * x**3 - 6 * x - 1

    mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x)
    assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
        770912*x**4 - 268432*x**2 + 28561
    mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x)
    assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
            232*x - 239
    mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x)
    assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127

    mp = minimal_polynomial(exp(2 * I * pi / 7), x)
    assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1
    mp = minimal_polynomial(exp(2 * I * pi / 15), x)
    assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1
    mp = minimal_polynomial(cos(2 * pi / 7), x)
    assert mp == 8 * x**3 + 4 * x**2 - 4 * x - 1
    mp = minimal_polynomial(sin(2 * pi / 7), x)
    ex = (5 * cos(2 * pi / 7) - 7) / (9 * cos(pi / 7) - 5 * cos(3 * pi / 7))
    mp = minimal_polynomial(ex, x)
    assert mp == x**3 + 2 * x**2 - x - 1
    assert minimal_polynomial(-1 / (2 * cos(pi / 7)),
                              x) == x**3 + 2 * x**2 - x - 1
    assert minimal_polynomial(sin(2*pi/15), x) == \
            256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1
    assert minimal_polynomial(sin(5 * pi / 14),
                              x) == 8 * x**3 - 4 * x**2 - 4 * x + 1
    assert minimal_polynomial(cos(
        pi / 15), x) == 16 * x**4 + 8 * x**3 - 16 * x**2 - 8 * x + 1

    ex = RootOf(x**3 + x * 4 + 1, 0)
    mp = minimal_polynomial(ex, x)
    assert mp == x**3 + 4 * x + 1
    mp = minimal_polynomial(ex + 1, x)
    assert mp == x**3 - 3 * x**2 + 7 * x - 4
    assert minimal_polynomial(exp(I * pi / 3), x) == x**2 - x + 1
    assert minimal_polynomial(exp(I * pi / 4), x) == x**4 + 1
    assert minimal_polynomial(exp(I * pi / 6), x) == x**4 - x**2 + 1
    assert minimal_polynomial(exp(I * pi / 9), x) == x**6 - x**3 + 1
    assert minimal_polynomial(exp(I * pi / 10),
                              x) == x**8 - x**6 + x**4 - x**2 + 1
    assert minimal_polynomial(sin(pi / 9),
                              x) == 64 * x**6 - 96 * x**4 + 36 * x**2 - 3
    assert minimal_polynomial(sin(pi/11), x) == 1024*x**10 - 2816*x**8 + \
            2816*x**6 - 1232*x**4 + 220*x**2 - 11

    ex = 2**Rational(1, 3) * exp(Rational(2, 3) * I * pi)
    assert minimal_polynomial(ex, x) == x**3 - 2

    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(cos(pi * sqrt(2)), x))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(sin(pi * sqrt(2)), x))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(exp(I * pi * sqrt(2)), x))

    # issue sympy/sympy#5934
    ex = 1 / (-36000 - 7200 * sqrt(5) +
              (12 * sqrt(10) * sqrt(sqrt(5) + 5) +
               24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2) + 1
    pytest.raises(ZeroDivisionError, lambda: minimal_polynomial(ex, x))

    ex = sqrt(1 + 2**Rational(1, 3)) + sqrt(1 + 2**Rational(1, 4)) + sqrt(2)
    mp = minimal_polynomial(ex, x)
    assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576

    mp = minimal_polynomial(sin(pi / 27), x)
    assert mp == (262144 * x**18 - 1179648 * x**16 + 2211840 * x**14 -
                  2236416 * x**12 + 1317888 * x**10 - 456192 * x**8 +
                  88704 * x**6 - 8640 * x**4 + 324 * x**2 - 3)
示例#35
0
def test_sympyissue_6122():
    assert integrate(exp(-I*x**2), (x, -oo, oo), meijerg=True) == \
        -I*sqrt(pi)*exp(I*pi/4)
示例#36
0
def test_expand():
    from diofant import besselsimp, Symbol, exp, exp_polar, I

    assert expand_func(besselj(Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(bessely(Rational(1, 2), z).rewrite(yn)) == \
        -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))

    # XXX: teach sin/cos to work around arguments like
    # x*exp_polar(I*pi*n/2).  Then change besselsimp -> expand_func
    assert besselsimp(besselj(Rational(1, 2),
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(-1, 2),
                              z)) == sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besselj(-Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(bessely(Rational(1, 2), z)) == \
        -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z))
    assert besselsimp(bessely(Rational(-1, 2),
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(bessely(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(bessely(Rational(-5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besseli(Rational(1, 2),
                              z)) == sqrt(2) * sinh(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besseli(Rational(-1, 2), z)) == \
        sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besseli(Rational(-5, 2), z)) == \
        sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besselk(Rational(1, 2), z)) == \
        besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z))
    assert besselsimp(besselk(Rational(5, 2), z)) == \
        besselsimp(besselk(Rational(-5, 2), z)) == \
        sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2))

    def check(eq, ans):
        return tn(eq, ans) and eq == ans

    rn = randcplx(a=1, b=0, d=0, c=2)

    for besselx in [besselj, bessely, besseli, besselk]:
        ri = Rational(2 * randint(-11, 10) + 1,
                      2)  # half integer in [-21/2, 21/2]
        assert tn(besselsimp(besselx(ri, z)), besselx(ri, z))

    assert check(expand_func(besseli(rn, x)),
                 besseli(rn - 2, x) - 2 * (rn - 1) * besseli(rn - 1, x) / x)
    assert check(expand_func(besseli(-rn, x)),
                 besseli(-rn + 2, x) + 2 * (-rn + 1) * besseli(-rn + 1, x) / x)

    assert check(expand_func(besselj(rn, x)),
                 -besselj(rn - 2, x) + 2 * (rn - 1) * besselj(rn - 1, x) / x)
    assert check(
        expand_func(besselj(-rn, x)),
        -besselj(-rn + 2, x) + 2 * (-rn + 1) * besselj(-rn + 1, x) / x)

    assert check(expand_func(besselk(rn, x)),
                 besselk(rn - 2, x) + 2 * (rn - 1) * besselk(rn - 1, x) / x)
    assert check(expand_func(besselk(-rn, x)),
                 besselk(-rn + 2, x) - 2 * (-rn + 1) * besselk(-rn + 1, x) / x)

    assert check(expand_func(bessely(rn, x)),
                 -bessely(rn - 2, x) + 2 * (rn - 1) * bessely(rn - 1, x) / x)
    assert check(
        expand_func(bessely(-rn, x)),
        -bessely(-rn + 2, x) + 2 * (-rn + 1) * bessely(-rn + 1, x) / x)

    n = Symbol('n', integer=True, positive=True)

    assert expand_func(besseli(n + 2, z)) == \
        besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z
    assert expand_func(besselj(n + 2, z)) == \
        -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z
    assert expand_func(besselk(n + 2, z)) == \
        besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z
    assert expand_func(bessely(n + 2, z)) == \
        -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z

    assert expand_func(besseli(n + Rational(1, 2), z).rewrite(jn)) == \
        (sqrt(2)*sqrt(z)*exp(-I*pi*(n + Rational(1, 2))/2) *
         exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi))
    assert expand_func(besselj(n + Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi)

    r = Symbol('r', extended_real=True)
    p = Symbol('p', positive=True)
    i = Symbol('i', integer=True)

    for besselx in [besselj, bessely, besseli, besselk]:
        assert besselx(i, p).is_extended_real
        assert besselx(i, x).is_extended_real is None
        assert besselx(x, z).is_extended_real is None

    for besselx in [besselj, besseli]:
        assert besselx(i, r).is_extended_real
    for besselx in [bessely, besselk]:
        assert besselx(i, r).is_extended_real is None
示例#37
0
def test_sympyissue_8368():
    assert meijerint_indefinite(cosh(x)*exp(-x*t), x) == (
        (-t - 1)*exp(x) + (-t + 1)*exp(-x))*exp(-t*x)/2/(t**2 - 1)
示例#38
0
def test_limit():
    assert gruntz(x, x) == oo
    assert gruntz(-x, x) == -oo
    assert gruntz(-x, x) == -oo
    assert gruntz((-x)**2, x) == oo
    assert gruntz(-x**2, x) == -oo
    assert gruntz((1 / x) * log(1 / x), x) == 0  # Gruntz: p15, 2.11
    assert gruntz(1 / x, x) == 0
    assert gruntz(exp(x), x) == oo
    assert gruntz(-exp(x), x) == -oo
    assert gruntz(exp(x) / x, x) == oo
    assert gruntz(1 / x - exp(-x), x) == 0
    assert gruntz(x + 1 / x, x) == oo

    assert gruntz((1 / x)**(1 / x), x) == 1  # Gruntz: p15, 2.11
    assert gruntz((exp(1 / x) - 1) * x, x) == 1
    assert gruntz(1 + 1 / x, x) == 1
    assert gruntz(-exp(1 / x), x) == -1
    assert gruntz(x + exp(-x), x) == oo
    assert gruntz(x + exp(-x**2), x) == oo
    assert gruntz(x + exp(-exp(x)), x) == oo
    assert gruntz(13 + 1 / x - exp(-x), x) == 13

    a = Symbol('a')
    assert gruntz(x - log(1 + exp(x)), x) == 0
    assert gruntz(x - log(a + exp(x)), x) == 0
    assert gruntz(exp(x) / (1 + exp(x)), x) == 1
    assert gruntz(exp(x) / (a + exp(x)), x) == 1

    assert gruntz((3**x + 5**x)**(1 / x), x) == 5  # issue sympy/sympy#3463

    assert gruntz(Ei(x + exp(-x)) * exp(-x) * x, x) == 1

    assert gruntz(1 / li(x), x) == 0
    assert gruntz(1 / Li(x), x) == 0

    # issue diofant/diofant#56
    assert gruntz((log(E + 1 / x) - 1)**(1 - sqrt(E + 1 / x)), x) == oo

    # issue sympy/sympy#9471
    assert gruntz((((27**(log(x, 3)))) / x**3), x) == 1
    assert gruntz((((27**(log(x, 3) + 1))) / x**3), x) == 27

    # issue sympy/sympy#9449
    y = Symbol('y')
    assert gruntz(x * (abs(1 / x + y) - abs(y - 1 / x)) / 2, x) == sign(y)

    # issue sympy/sympy#8481
    assert gruntz(m**x * exp(-m) / factorial(x), x) == 0

    # issue sympy/sympy#4187
    assert gruntz(exp(1 / x) * log(1 / x) - Ei(1 / x), x) == -EulerGamma
    assert gruntz(exp(x) * log(x) - Ei(x), x) == oo

    # issue sympy/sympy#10382
    assert gruntz(fibonacci(x + 1) / fibonacci(x), x) == GoldenRatio

    assert gruntz(zeta(x), x) == 1
    assert gruntz(zeta(m) * zeta(x), x) == zeta(m)
示例#39
0
def test_exp_log_series():
    assert gruntz(x / log(log(x * exp(x))), x) == oo
示例#40
0
def test_rewrite():
    assert rewrite(Integer(1), x, m) == (1, None)

    e = exp(x)
    assert rewrite(e, x, m) == (1 / m, -x)
    e = exp(x**2)
    assert rewrite(e, x, m) == (1 / m, -x**2)
    e = exp(x + 1 / x)
    assert rewrite(e, x, m) == (1 / m, -x - 1 / x)
    e = 1 / exp(-x + exp(-x)) - exp(x)
    assert rewrite(e, x, m) == (Add(-1 / m, 1 / (m * exp(m)),
                                    evaluate=False), -x)

    e = exp(x) * log(log(exp(x)))
    assert mrv(e, x) == {exp(x)}
    assert rewrite(e, x, m) == (1 / m * log(x), -x)

    e = exp(-x + 1 / x**2) - exp(x + 1 / x)
    assert rewrite(e, x, m) == (Add(m,
                                    Mul(-1,
                                        exp(1 / x + x**(-2)) / m,
                                        evaluate=False),
                                    evaluate=False), -x + 1 / x**2)
示例#41
0
def test_mrv_leadterm():
    assert mrv_leadterm(Integer(1), x) == (1, 0)

    assert mrv_leadterm(-exp(1 / x), x) == (-1, 0)
    assert mrv_leadterm(1 / exp(-x + exp(-x)) - exp(x), x) == (-1, 0)
    assert mrv_leadterm((exp(1 / x - exp(-x)) - exp(1 / x)) * exp(x),
                        x) == (-exp(1 / x), 0)

    # Gruntz: p51, 3.25
    assert mrv_leadterm((log(exp(x) + x) - x)/log(exp(x) + log(x))*exp(x), x) == \
        (1, 0)

    # Gruntz: p56, 3.27
    assert mrv(exp(-x + exp(-x) * exp(-x * log(x))), x) == {exp(-x * log(x))}
    assert mrv_leadterm(exp(-x + exp(-x) * exp(-x * log(x))),
                        x) == (exp(-x), 0)
示例#42
0
def test_gruntz_evaluation():
    # Gruntz' thesis pp. 122 to 123
    # 8.1
    assert gruntz(exp(x) * (exp(1 / x - exp(-x)) - exp(1 / x)), x) == -1
    # 8.2
    assert gruntz(
        exp(x) *
        (exp(1 / x + exp(-x) + exp(-x**2)) - exp(1 / x - exp(-exp(x)))),
        x) == 1
    # 8.3
    assert gruntz(exp(exp(x - exp(-x)) / (1 - 1 / x)) - exp(exp(x)), x) == oo
    # 8.4
    assert gruntz(
        exp(exp(exp(x) / (1 - 1 / x))) -
        exp(exp(exp(x) / (1 - 1 / x - log(x)**(-log(x))))), x) == -oo
    # 8.5
    assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(exp(x))), x) == oo
    # 8.6
    assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(x))))), x) == oo
    # 8.7
    assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(exp(x)))))),
                  x) == 1
    # 8.8
    assert gruntz(exp(exp(x)) / exp(exp(x - exp(-exp(exp(x))))), x) == 1
    # 8.9
    assert gruntz(
        log(x)**2 * exp(
            sqrt(log(x)) *
            (log(log(x)))**2 * exp(sqrt(log(log(x))) *
                                   (log(log(log(x))))**3)) / sqrt(x), x) == 0
    # 8.10
    assert gruntz((x * log(x) * (log(x * exp(x) - x**2))**2) /
                  (log(log(x**2 + 2 * exp(exp(3 * x**3 * log(x)))))),
                  x) == Rational(1, 3)
    # 8.11
    assert gruntz(
        (exp(x * exp(-x) / (exp(-x) + exp(-2 * x**2 / (x + 1)))) - exp(x)) / x,
        x) == -exp(2)
    # 8.12
    assert gruntz((3**x + 5**x)**(1 / x), x) == 5
    # 8.13
    assert gruntz(x / log(x**(log(x**(log(2) / log(x))))), x) == oo
    # 8.14
    assert gruntz(
        exp(exp(2 * log(x**5 + x) * log(log(x)))) /
        exp(exp(10 * log(x) * log(log(x)))), x) == oo
    # 8.15
    assert gruntz(
        exp(
            exp(
                Rational(5, 2) * x**(-Rational(5, 7)) +
                Rational(21, 8) * x**Rational(6, 11) + 2 * x**(-8) +
                Rational(54, 17) * x**Rational(49, 45)))**8 /
        log(log(-log(Rational(4, 3) * x**(-Rational(5, 14)))))**Rational(7, 6),
        x) == oo
    # 8.16
    assert gruntz(
        (exp(4 * x * exp(-x) /
             (1 / exp(x) + 1 / exp(2 * x**2 / (x + 1)))) - exp(x)) / exp(x)**4,
        x) == 1
    # 8.17
    assert gruntz(exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1))))/exp(x), x) \
        == 1
    # 8.18
    assert gruntz(
        (exp(exp(-x / (1 + exp(-x)))) * exp(-x / (1 + exp(-x /
                                                          (1 + exp(-x))))) *
         exp(exp(-x + exp(-x / (1 + exp(-x)))))) /
        (exp(-x / (1 + exp(-x))))**2 - exp(x) + x, x) == 2
    # 8.19
    assert gruntz(
        log(x) * (log(log(x) + log(log(x))) - log(log(x))) /
        (log(log(x) + log(log(log(x))))), x) == 1
    # 8.20
    assert gruntz(
        exp((log(log(x + exp(log(x) * log(log(x)))))) /
            (log(log(log(exp(x) + x + log(x)))))), x) == E
    # Another
    assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(x)), x) == oo
示例#43
0
def test_mrv():
    assert mrv(x, x) == {x}
    assert mrv(x + 1 / x, x) == {x}
    assert mrv(x**2, x) == {x}
    assert mrv(log(x), x) == {x}
    assert mrv(exp(x), x) == {exp(x)}
    assert mrv(exp(-x), x) == {exp(-x)}
    assert mrv(exp(x**2), x) == {exp(x**2)}
    assert mrv(-exp(1 / x), x) == {x}
    assert mrv(exp(x + 1 / x), x) == {exp(x + 1 / x)}

    assert mrv(exp(x + exp(-exp(x))), x) == {exp(-exp(x))}
    assert mrv(exp(x + exp(-x)), x) == {exp(x + exp(-x)), exp(-x)}
    assert mrv(exp(1 / x + exp(-x)), x) == {exp(-x)}

    assert mrv(exp(x + exp(-x**2)), x) == {exp(-x**2)}

    assert mrv(exp(-x + 1 / x**2) - exp(x + 1 / x),
               x) == {exp(x + 1 / x), exp(1 / x**2 - x)}

    assert mrv(exp(x**2) + x * exp(x) + exp(x * log(log(x))) / x,
               x) == {exp(x**2)}
    assert mrv(exp(x) * (exp(1 / x + exp(-x)) - exp(1 / x)),
               x) == {exp(x), exp(-x)}
    assert mrv(log(x**2 + 2 * exp(exp(3 * x**3 * log(x)))),
               x) == {exp(exp(3 * x**3 * log(x)))}
    assert mrv(log(x - log(x)) / log(x), x) == {x}
    assert mrv((exp(1 / x - exp(-x)) - exp(1 / x)) * exp(x),
               x) == {exp(x), exp(-x)}
    assert mrv(1 / exp(-x + exp(-x)) - exp(x),
               x) == {exp(x), exp(-x), exp(x - exp(-x))}
    assert mrv(log(log(x * exp(x * exp(x)) + 1)), x) == {exp(x * exp(x))}
    assert mrv(exp(exp(log(log(x) + 1 / x))), x) == {x}

    assert mrv((log(log(x) + log(log(x))) - log(log(x))) /
               log(log(x) + log(log(log(x)))) * log(x), x) == {x}
    assert mrv(log(log(x*exp(x*exp(x)) + 1)) - exp(exp(log(log(x) + 1/x))), x) == \
        {exp(x*exp(x))}

    # Gruntz: p47, 3.21
    h = exp(-x / (1 + exp(-x)))
    e = Mul(exp(h),
            exp(-x / (1 + h)),
            exp(exp(-x + h)),
            Pow(h, -2, evaluate=False),
            evaluate=False) - exp(x) + x
    assert mrv(e, x) == {exp(-x + h), exp(-x / (1 + h)), h, exp(x), exp(-x)}
示例#44
0
def test_compare():
    assert compare(Integer(2), x, x) < 0
    assert compare(x, exp(x), x) < 0
    assert compare(exp(x), exp(x**2), x) < 0
    assert compare(exp(x**2), exp(exp(x)), x) < 0
    assert compare(Integer(1), exp(exp(x)), x) < 0

    assert compare(x, Integer(2), x) > 0
    assert compare(exp(x), x, x) > 0
    assert compare(exp(x**2), exp(x), x) > 0
    assert compare(exp(exp(x)), exp(x**2), x) > 0
    assert compare(exp(exp(x)), Integer(1), x) > 0

    assert compare(Integer(2), Integer(3), x) == 0
    assert compare(Integer(3), Integer(-5), x) == 0
    assert compare(Integer(2), Integer(-5), x) == 0

    assert compare(x, x**2, x) == 0
    assert compare(x**2, x**3, x) == 0
    assert compare(x**3, 1 / x, x) == 0
    assert compare(1 / x, x**m, x) == 0
    assert compare(x**m, -x, x) == 0

    assert compare(exp(x), exp(-x), x) == 0
    assert compare(exp(-x), exp(2 * x), x) == 0
    assert compare(exp(2 * x), exp(x)**2, x) == 0
    assert compare(exp(x)**2, exp(x + exp(-x)), x) == 0
    assert compare(exp(x), exp(x + exp(-x)), x) == 0

    assert compare(exp(x**2), 1 / exp(x**2), x) == 0

    assert compare(exp(x), x**5, x) > 0
    assert compare(exp(x**2), exp(x)**2, x) > 0
    assert compare(exp(x), exp(x + exp(-x)), x) == 0
    assert compare(exp(x + exp(-x)), exp(x), x) == 0
    assert compare(exp(x + exp(-x)), exp(-x), x) == 0
    assert compare(exp(-x), x, x) > 0
    assert compare(x, exp(-x), x) < 0
    assert compare(exp(x + 1 / x), x, x) > 0
    assert compare(exp(-exp(x)), exp(x), x) > 0
    assert compare(exp(exp(-exp(x)) + x), exp(-exp(x)), x) < 0

    assert compare(exp(exp(x)), exp(x + exp(-exp(x))), x) > 0
示例#45
0
def test_ccode_constants_mathh():
    assert ccode(exp(1)) == 'M_E'
    assert ccode(pi) == 'M_PI'
    assert ccode(oo) == 'HUGE_VAL'
    assert ccode(-oo) == '-HUGE_VAL'
示例#46
0
def test_sympyissue_6682():
    assert gruntz(exp(2 * Ei(-1 / x)) * x**2, x) == exp(2 * EulerGamma)
示例#47
0
def test_meijer():
    pytest.raises(TypeError, lambda: meijerg(1, z))
    pytest.raises(TypeError, lambda: meijerg(((1, ), (2, )), (3, ), (4, ), z))
    pytest.raises(TypeError, lambda: meijerg((1, 2, 3), (4, 5), z))

    assert meijerg(((1, 2), (3,)), ((4,), (5,)), z) == \
        meijerg(Tuple(1, 2), Tuple(3), Tuple(4), Tuple(5), z)

    g = meijerg((1, 2), (3, 4, 5), (6, 7, 8, 9), (10, 11, 12, 13, 14), z)
    assert g.an == Tuple(1, 2)
    assert g.ap == Tuple(1, 2, 3, 4, 5)
    assert g.aother == Tuple(3, 4, 5)
    assert g.bm == Tuple(6, 7, 8, 9)
    assert g.bq == Tuple(6, 7, 8, 9, 10, 11, 12, 13, 14)
    assert g.bother == Tuple(10, 11, 12, 13, 14)
    assert g.argument == z
    assert g.nu == 75
    assert g.delta == -1
    assert g.is_commutative is True

    assert meijerg([1, 2], [3], [4], [5], z).delta == Rational(1, 2)

    # just a few checks to make sure that all arguments go where they should
    assert tn(meijerg(Tuple(), Tuple(), Tuple(0), Tuple(), -z), exp(z), z)
    assert tn(
        sqrt(pi) *
        meijerg(Tuple(), Tuple(), Tuple(0), Tuple(Rational(1, 2)), z**2 / 4),
        cos(z), z)
    assert tn(meijerg(Tuple(1, 1), Tuple(), Tuple(1), Tuple(0), z), log(1 + z),
              z)

    # test exceptions
    pytest.raises(ValueError, lambda: meijerg(((3, 1), (2, )), ((oo, ),
                                                                (2, 0)), x))
    pytest.raises(ValueError, lambda: meijerg(((3, 1), (2, )), ((1, ),
                                                                (2, 0)), x))

    # differentiation
    g = meijerg((randcplx(), ), (randcplx() + 2 * I, ), Tuple(),
                (randcplx(), randcplx()), z)
    assert td(g, z)

    g = meijerg(Tuple(), (randcplx(), ), Tuple(), (randcplx(), randcplx()), z)
    assert td(g, z)

    g = meijerg(Tuple(), Tuple(), Tuple(randcplx()),
                Tuple(randcplx(), randcplx()), z)
    assert td(g, z)

    a1, a2, b1, b2, c1, c2, d1, d2 = symbols('a1:3, b1:3, c1:3, d1:3')
    assert meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z).diff(z) == \
        (meijerg((a1 - 1, a2), (b1, b2), (c1, c2), (d1, d2), z)
         + (a1 - 1)*meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z))/z

    assert meijerg([z, z], [], [], [], z).diff(z) == \
        Derivative(meijerg([z, z], [], [], [], z), z)

    # meijerg is unbranched wrt parameters
    from diofant import polar_lift as pl
    assert meijerg([pl(a1)], [pl(a2)], [pl(b1)], [pl(b2)], pl(z)) == \
        meijerg([a1], [a2], [b1], [b2], pl(z))

    # integrand
    from diofant.abc import a, b, c, d, s
    assert meijerg([a], [b], [c], [d], z).integrand(s) == \
        z**s*gamma(c - s)*gamma(-a + s + 1)/(gamma(b - s)*gamma(-d + s + 1))
示例#48
0
def test_diff_wrt_not_allowed():
    pytest.raises(ValueError, lambda: diff(sin(x**2), x**2))
    pytest.raises(ValueError, lambda: diff(exp(x * y), x * y))
    pytest.raises(ValueError, lambda: diff(1 + x, 1 + x))
示例#49
0
def test_Abs():
    pytest.raises(TypeError, lambda: Abs(Interval(2, 3)))  # issue sympy/sympy#8717

    x, y = symbols('x,y')
    assert sign(sign(x)) == sign(x)
    assert isinstance(sign(x*y), sign)
    assert Abs(0) == 0
    assert Abs(1) == 1
    assert Abs(-1) == 1
    assert abs(I) == 1
    assert abs(-I) == 1
    assert abs(nan) == nan
    assert abs(zoo) == oo
    assert abs(+I * pi) == pi
    assert abs(-I * pi) == pi
    assert abs(+I * x) == abs(x)
    assert abs(-I * x) == abs(x)
    assert abs(-2*x) == 2*abs(x)
    assert abs(-2.0*x) == 2.0*abs(x)
    assert abs(2*pi*x*y) == 2*pi*abs(x*y)
    assert abs(conjugate(x)) == abs(x)
    assert conjugate(abs(x)) == abs(x)

    a = cos(1)**2 + sin(1)**2 - 1
    assert abs(a*x).series(x).simplify() == 0

    a = Symbol('a', positive=True)
    assert abs(2*pi*x*a) == 2*pi*a*abs(x)
    assert abs(2*pi*I*x*a) == 2*pi*a*abs(x)

    x = Symbol('x', extended_real=True)
    n = Symbol('n', integer=True)
    assert abs((-1)**n) == 1
    assert x**(2*n) == abs(x)**(2*n)
    assert abs(x).diff(x) == sign(x)
    assert abs(-x).fdiff() == sign(x)
    assert abs(x) == Abs(x)  # Python built-in
    assert abs(x)**3 == x**2*abs(x)
    assert abs(x)**4 == x**4
    assert (abs(x)**(3*n)).args == (abs(x), 3*n)  # leave symbolic odd unchanged
    assert (1/abs(x)).args == (abs(x), -1)
    assert 1/abs(x)**3 == 1/(x**2*abs(x))
    assert abs(x)**-3 == abs(x)/x**4
    assert abs(x**3) == x**2*abs(x)
    assert abs(x**pi) == Abs(x**pi, evaluate=False)

    x = Symbol('x', imaginary=True)
    assert abs(x).diff(x) == -sign(x)

    pytest.raises(ArgumentIndexError, lambda: abs(z).fdiff(2))

    eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3))
    # if there is a fast way to know when you can and when you cannot prove an
    # expression like this is zero then the equality to zero is ok
    assert abs(eq) == 0
    q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6)
    p = cbrt(expand(q**3))
    d = p - q
    assert abs(d) == 0

    assert abs(4*exp(pi*I/4)) == 4
    assert abs(3**(2 + I)) == 9
    assert abs((-3)**(1 - I)) == 3*exp(pi)

    assert abs(+oo) is oo
    assert abs(-oo) is oo
    assert abs(oo + I) is oo
    assert abs(oo + I*oo) is oo

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    x = Symbol('x')
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert abs(sign(z)) == Abs(sign(z), evaluate=False)
示例#50
0
def test_csch_rewrite():
    assert csch(x).rewrite(exp) == 1 / (exp(x)/2 - exp(-x)/2) \
        == csch(x).rewrite('tractable')
    assert csch(x).rewrite(cosh) == I/cosh(x + I*pi/2)
    assert csch(x).rewrite(tanh) == (1 - tanh(x/2)**2)/(2*tanh(x/2))
    assert csch(x).rewrite(coth) == (coth(x/2)**2 - 1)/(2*coth(x/2))
示例#51
0
def test_sympyissue_6348():
    assert integrate(exp(I*x)/(1 + x**2), (x, -oo, oo)).simplify().rewrite(exp) \
        == pi*exp(-1)
示例#52
0
def test_rewrite_Sum():
    assert Product(1 - 1/(4*k**2), (k, 1, oo)).rewrite(Sum) == \
        exp(Sum(log(1 - 1/(4*k**2)), (k, 1, oo)))
示例#53
0
def test_sech_rewrite():
    assert sech(x).rewrite(exp) == 1 / (exp(x)/2 + exp(-x)/2) \
        == sech(x).rewrite('tractable')
    assert sech(x).rewrite(sinh) == I/sinh(x + I*pi/2)
    assert sech(x).rewrite(tanh) == (1 - tanh(x/2)**2)/(1 + tanh(x/2)**2)
    assert sech(x).rewrite(coth) == (coth(x/2)**2 - 1)/(coth(x/2)**2 + 1)
示例#54
0
def test_ode_solutions():
    # only a few examples here, the rest will be tested in the actual dsolve tests
    assert constant_renumber(constantsimp(C1*exp(2*x) + exp(x)*(C2 + C3), [C1, C2, C3]), 'C', 1, 3) == \
        constant_renumber((C1*exp(x) + C2*exp(2*x)), 'C', 1, 2)
    assert constant_renumber(
        constantsimp(Eq(f(x), I*C1*sinh(x/3) + C2*cosh(x/3)), [C1, C2]),
        'C', 1, 2) == constant_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2)
    assert constant_renumber(constantsimp(Eq(f(x), acos((-C1)/cos(x))), [C1]), 'C', 1, 1) == \
        Eq(f(x), acos(C1/cos(x)))
    assert constant_renumber(
        constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), [C1]),
        'C', 1, 1) == Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0)
    assert constant_renumber(constantsimp(Eq(log(x*sqrt(2)*sqrt(1/x)*sqrt(f(x))
                                                 / C1) + x**2/(2*f(x)**2), 0), [C1]), 'C', 1, 1) == \
        Eq(log(C1*sqrt(x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0)
    assert constant_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) -
                                             cos(f(x)/x)*exp(-f(x)/x)/2, 0), [C1]), 'C', 1, 1) == \
        Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x) *
           exp(-f(x)/x)/2, 0)
    u2 = Symbol('u2')
    _a = Symbol('_a')
    assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2),
                                                       (u2, _a, x/f(x))) + log(f(x)/C1), 0), [C1]), 'C', 1, 1) == \
        Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) +
           log(C1*f(x)), 0)
    assert [constantsimp(i, [C1]) for i in [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x + x**2))]] == \
        [Eq(f(x), sqrt(x*(C1 + x))), Eq(f(x), -sqrt(x*(C1 + x)))]

    # issue sympy/sympy5770
    k = Symbol('k', extended_real=True)
    t = Symbol('t')
    w = Function('w')
    sol = dsolve(w(t).diff((t, 6)) - k**6*w(t), w(t))
    assert len([s for s in sol.free_symbols if s.name.startswith('C')]) == 6
    assert constantsimp((C1*cos(x) + C2*cos(x))*exp(x), {C1, C2}) == \
        C1*cos(x)*exp(x)
    assert constantsimp(C1*cos(x) + C2*cos(x) + C3*sin(x), {C1, C2, C3}) == \
        C1*cos(x) + C3*sin(x)
    assert constantsimp(exp(C1 + x), {C1}) == C1*exp(x)
    assert constantsimp(x + C1 + y, {C1, y}) == C1 + x
    assert constantsimp(x + C1 + Integral(x, (x, 1, 2)), {C1}) == C1 + x
示例#55
0
def test_expint():
    """ Test various exponential integrals. """
    assert simplify(integrate(exp(-z*x)/x**y,
                              (x, 1, oo),
                              meijerg=True,
                              conds='none').rewrite(expint)) == expint(y, z)

    assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(1, z)
    assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(2, z).rewrite(Ei).rewrite(expint)
    assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(3, z).rewrite(Ei).rewrite(expint).expand()

    t = Symbol('t', positive=True)
    assert integrate(-cos(x)/x, (x, t, oo), meijerg=True).expand() == Ci(t)
    assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \
        Si(t) - pi/2
    assert integrate(sin(x)/x, (x, 0, z), meijerg=True) == Si(z)
    assert integrate(sinh(x)/x, (x, 0, z), meijerg=True) == Shi(z)
    assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \
        I*pi - expint(1, x)
    assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \
        == expint(1, x) - exp(-x)/x - I*pi

    u = Symbol('u', polar=True)
    assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Ci(u)
    assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Chi(u)

    assert (integrate(expint(1, x), x,
                      meijerg=True).rewrite(expint).expand() ==
            x*expint(1, x) - exp(-x))
    assert (integrate(expint(2, x), x,
                      meijerg=True).rewrite(expint).expand() ==
            -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2)
    assert (simplify(unpolarify(integrate(expint(y, x), x,
                                          meijerg=True).rewrite(expint))) ==
            -expint(y + 1, x))

    assert integrate(Si(x), x, meijerg=True) == x*Si(x) + cos(x)
    assert integrate(Ci(u), u, meijerg=True).expand() == u*Ci(u) - sin(u)
    assert integrate(Shi(x), x, meijerg=True) == x*Shi(x) - cosh(x)
    assert integrate(Chi(u), u, meijerg=True).expand() == u*Chi(u) - sinh(u)

    assert integrate(Si(x)*exp(-x), (x, 0, oo), meijerg=True) == pi/4
    assert integrate(expint(1, x)*sin(x), (x, 0, oo), meijerg=True) == log(2)/2
示例#56
0
def test_postprocess():
    eq = (x + 1 + exp((x + 1) / (y + 1)) + cos(y + 1))
    assert cse([eq, Eq(x, z + 1), z - 2, (z + 1)*(x + 1)],
               postprocess=cse_main.cse_separate) == \
        [[(x1, y + 1), (x2, z + 1), (x, x2), (x0, x + 1)],
         [x0 + exp(x0/x1) + cos(x1), z - 2, x0*x2]]
示例#57
0
def test_coth_rewrite():
    assert coth(x).rewrite(exp) == (exp(x) + exp(-x))/(exp(x) - exp(-x)) \
        == coth(x).rewrite('tractable')
    assert coth(x).rewrite(sinh) == -I*sinh(I*pi/2 - x)/sinh(x)
    assert coth(x).rewrite(cosh) == -I*cosh(x)/cosh(I*pi/2 - x)
    assert coth(x).rewrite(tanh) == 1/tanh(x)
示例#58
0
def test_cosh_rewrite():
    assert cosh(x).rewrite(exp) == (exp(x) + exp(-x))/2 \
        == cosh(x).rewrite('tractable')
    assert cosh(x).rewrite(sinh) == -I*sinh(x + I*pi/2)
    assert cosh(x).rewrite(tanh) == (1 + tanh(x/2)**2)/(1 - tanh(x/2)**2)
    assert cosh(x).rewrite(coth) == (coth(x/2)**2 + 1)/(coth(x/2)**2 - 1)
示例#59
0
def test_gruntz_eval_special():
    # Gruntz, p. 126
    assert gruntz(
        exp(x) * (sin(1 / x + exp(-x)) - sin(1 / x + exp(-x**2))), x) == 1
    assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2),
                  x) == -2 / sqrt(pi)
    assert gruntz(
        exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))),
        x) == 1
    assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x) == oo
    assert gruntz(exp(exp(digamma(digamma(x)))) / x, x) == exp(-Rational(1, 2))
    assert gruntz(exp(exp(digamma(log(x)))) / x, x) == exp(-Rational(1, 2))
    assert gruntz(digamma(digamma(digamma(x))), x) == oo
    assert gruntz(loggamma(loggamma(x)), x) == oo
    assert gruntz(
        ((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x *
        log(x), x) == -Rational(1, 2)
    assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x) \
        == Rational(1, 2)
    assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x) == 1
示例#60
0
def test_gruntz_eval_special_slow():
    assert gruntz(
        gamma(x + 1) / sqrt(2 * pi) - exp(-x) *
        (x**(x + Rational(1, 2)) + x**(x - Rational(1, 2)) / 12), x) == oo
    assert gruntz(exp(exp(exp(digamma(digamma(digamma(x)))))) / x, x) == 0
    assert gruntz(exp(gamma(x - exp(-x)) * exp(1 / x)) - exp(gamma(x)),
                  x) == oo
    assert gruntz((Ei(x - exp(-exp(x))) - Ei(x)) * exp(-x) * exp(exp(x)) * x,
                  x) == -1
    assert gruntz(exp((log(2) + 1) * x) * (zeta(x + exp(-x)) - zeta(x)),
                  x) == -log(2)