Пример #1
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
    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)
Пример #2
0
def test_intractable():
    assert limit(1/gamma(x), x, oo) == 0
    assert limit(1/loggamma(x), x, oo) == 0
    assert limit(gamma(x)/loggamma(x), x, oo) == oo
    assert limit(exp(gamma(x))/gamma(x), x, oo) == oo
    assert limit(gamma(3 + 1/x), x, oo) == 2
    assert limit(gamma(Rational(1, 7) + 1/x), x, oo) == gamma(Rational(1, 7))
    assert limit(log(x**x)/log(gamma(x)), x, oo) == 1
    assert limit(log(gamma(gamma(x)))/exp(x), x, oo) == oo
    assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2)

    # issue sympy/sympy#10804
    assert limit(2*airyai(x)*root(x, 4) *
                 exp(2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi)
    assert limit(airybi(x)*root(x, 4) *
                 exp(-2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi)
    assert limit(airyai(1/x), x, oo) == (3**Rational(5, 6) *
                                         gamma(Rational(1, 3))/(6*pi))
    assert limit(airybi(1/x), x, oo) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert limit(airyai(2 + 1/x), x, oo) == airyai(2)
    assert limit(airybi(2 + 1/x), x, oo) == airybi(2)

    # issue sympy/sympy#10976
    assert limit(erf(m/x)/erf(1/x), x, oo) == m

    assert limit(Max(x**2, x, exp(x))/x, x, oo) == oo
Пример #3
0
def test_branch_bug():
    assert hyperexpand(hyper((-Rational(1, 3), Rational(1, 2)), (Rational(2, 3), Rational(3, 2)), -z)) == \
        -z**Rational(1, 3)*lowergamma(exp_polar(I*pi)/3, z)/5 \
        + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z))
    assert hyperexpand(meijerg([Rational(7, 6), 1], [], [Rational(2, 3)], [Rational(1, 6), 0], z)) == \
        2*z**Rational(2, 3)*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) -
                             2*lowergamma(Rational(2, 3), z)/z**Rational(2, 3))*gamma(Rational(2, 3))/gamma(Rational(5, 3))
Пример #4
0
def test_branch_bug():
    # TODO combsimp cannot prove that the factor is unity
    assert powdenest(integrate(erf(x**3), x, meijerg=True).diff(x),
                     polar=True) == 2*erf(x**3)*gamma(Rational(2, 3))/3/gamma(Rational(5, 3))
    assert integrate(erf(x**3), x, meijerg=True) == \
        2*x*erf(x**3)*gamma(Rational(2, 3))/(3*gamma(Rational(5, 3))) \
        - 2*gamma(Rational(2, 3))*lowergamma(Rational(2, 3), x**6)/(3*sqrt(pi)*gamma(Rational(5, 3)))
Пример #5
0
def test_branch_bug():
    assert hyperexpand(hyper((-Rational(1, 3), Rational(1, 2)), (Rational(2, 3), Rational(3, 2)), -z)) == \
        -cbrt(z)*lowergamma(exp_polar(I*pi)/3, z)/5 \
        + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z))
    assert hyperexpand(meijerg([Rational(7, 6), 1], [], [Rational(2, 3)], [Rational(1, 6), 0], z)) == \
        2*z**Rational(2, 3)*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) -
                             2*lowergamma(Rational(2, 3), z)/z**Rational(2, 3))*gamma(Rational(2, 3))/gamma(Rational(5, 3))
Пример #6
0
def test_erfc():
    assert erfc(nan) == nan

    assert erfc(oo) == 0
    assert erfc(-oo) == 2

    assert erfc(0) == 1

    assert erfc(I * oo) == -oo * I
    assert erfc(-I * oo) == oo * I

    assert erfc(-x) == Integer(2) - erfc(x)
    assert erfc(erfcinv(x)) == x
    assert erfc(erfinv(x)) == 1 - x

    assert erfc(I).is_extended_real is False
    assert erfc(w).is_extended_real is True
    assert erfc(z).is_extended_real is None

    assert conjugate(erfc(z)) == erfc(conjugate(z))

    assert erfc(x).as_leading_term(x) == 1
    assert erfc(1 / x).as_leading_term(x) == erfc(1 / x)

    assert erfc(z).rewrite('erf') == 1 - erf(z)
    assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z)
    assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite('hyper') == 1 - 2 * z * hyper(
        [Rational(1, 2)], [Rational(3, 2)], -z**2) / sqrt(pi)
    assert erfc(z).rewrite('meijerg') == 1 - z * meijerg(
        [Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2) / sqrt(pi)
    assert erfc(z).rewrite(
        'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z
    assert erfc(z).rewrite('expint') == 1 - sqrt(z**2) / z + z * expint(
        Rational(1, 2), z**2) / sqrt(pi)

    assert erfc(x).as_real_imag() == \
        ((erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
          erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
          I*(erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
             erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) *
          re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))
    assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag()
    assert erfc(w).as_real_imag() == (erfc(w), 0)
    assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag()
    assert erfc(I).as_real_imag() == (1, -erfi(1))

    pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2))

    assert erfc(x).taylor_term(3, x, *(-2 * x / sqrt(pi),
                                       0)) == 2 * x**3 / 3 / sqrt(pi)

    assert erfc(x).limit(x, oo) == 0

    assert erfc(x).diff(x) == -2 * exp(-x**2) / sqrt(pi)
Пример #7
0
def test_branch_bug():
    # TODO combsimp cannot prove that the factor is unity
    assert powdenest(integrate(erf(x**3), x, meijerg=True).diff(x),
                     polar=True) == 2*erf(x**3)*gamma(Rational(2, 3))/3/gamma(Rational(5, 3))
    assert integrate(erf(x**3), x, meijerg=True) == \
        2*x*erf(x**3)*gamma(Rational(2, 3))/(3*gamma(Rational(5, 3))) \
        - 2*gamma(Rational(2, 3))*lowergamma(Rational(2, 3), x**6)/(3*sqrt(pi)*gamma(Rational(5, 3)))
Пример #8
0
def test_sympyissue_4890():
    z = Symbol('z', positive=True)
    assert integrate(exp(-log(x)**2), x) == \
        sqrt(pi)*exp(Rational(1, 4))*erf(log(x)-Rational(1, 2))/2
    assert integrate(exp(log(x)**2), x) == \
        sqrt(pi)*exp(-Rational(1, 4))*erfi(log(x)+Rational(1, 2))/2
    assert integrate(exp(-z*log(x)**2), x) == \
        sqrt(pi)*exp(1/(4*z))*erf(sqrt(z)*log(x) - 1/(2*sqrt(z)))/(2*sqrt(z))
Пример #9
0
def test_sympyissue_4890():
    z = Symbol('z', positive=True)
    assert integrate(exp(-log(x)**2), x) == \
        sqrt(pi)*exp(Rational(1, 4))*erf(log(x)-Rational(1, 2))/2
    assert integrate(exp(log(x)**2), x) == \
        sqrt(pi)*exp(-Rational(1, 4))*erfi(log(x)+Rational(1, 2))/2
    assert integrate(exp(-z*log(x)**2), x) == \
        sqrt(pi)*exp(1/(4*z))*erf(sqrt(z)*log(x) - 1/(2*sqrt(z)))/(2*sqrt(z))
Пример #10
0
def test_erfi():
    assert erfi(nan) == nan

    assert erfi(+oo) == +oo
    assert erfi(-oo) == -oo

    assert erfi(0) == 0

    assert erfi(I * oo) == I
    assert erfi(-I * oo) == -I

    assert erfi(-x) == -erfi(x)

    assert erfi(I * erfinv(x)) == I * x
    assert erfi(I * erfcinv(x)) == I * (1 - x)
    assert erfi(I * erf2inv(0, x)) == I * x

    assert erfi(I).is_extended_real is False
    assert erfi(w).is_extended_real is True
    assert erfi(z).is_extended_real is None

    assert conjugate(erfi(z)) == erfi(conjugate(z))

    assert erfi(z).rewrite('erf') == -I * erf(I * z)
    assert erfi(z).rewrite('erfc') == I * erfc(I * z) - I
    assert erfi(z).rewrite('fresnels') == (1 - I) * (
        fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 + I) / sqrt(pi)))
    assert erfi(z).rewrite('fresnelc') == (1 - I) * (
        fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 + I) / sqrt(pi)))
    assert erfi(z).rewrite('hyper') == 2 * z * hyper(
        [Rational(1, 2)], [Rational(3, 2)], z**2) / sqrt(pi)
    assert erfi(z).rewrite('meijerg') == z * meijerg(
        [Rational(1, 2)], [], [0], [Rational(-1, 2)], -z**2) / sqrt(pi)
    assert erfi(z).rewrite('uppergamma') == (
        sqrt(-z**2) / z * (uppergamma(Rational(1, 2), -z**2) / sqrt(pi) - 1))
    assert erfi(z).rewrite('expint') == sqrt(-z**2) / z - z * expint(
        Rational(1, 2), -z**2) / sqrt(pi)

    assert erfi(x).as_real_imag() == \
        ((erfi(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
          erfi(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
          I*(erfi(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
             erfi(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) *
          re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))
    assert erfi(x).as_real_imag(deep=False) == erfi(x).as_real_imag()
    assert erfi(w).as_real_imag() == (erfi(w), 0)
    assert erfi(w).as_real_imag(deep=False) == erfi(w).as_real_imag()
    assert erfi(I).as_real_imag() == (0, erf(1))

    pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2))

    assert erfi(x).taylor_term(3, x, *(2 * x / sqrt(pi),
                                       0)) == 2 * x**3 / 3 / sqrt(pi)

    assert erfi(x).limit(x, oo) == oo
Пример #11
0
def test__erfs():
    assert _erfs(z).diff(z) == -2/sqrt(S.Pi) + 2*z*_erfs(z)

    assert _erfs(1/z).series(z) == \
        z/sqrt(pi) - z**3/(2*sqrt(pi)) + 3*z**5/(4*sqrt(pi)) + O(z**6)

    assert expand(erf(z).rewrite('tractable').diff(z).rewrite('intractable')) \
        == erf(z).diff(z)
    assert _erfs(z).rewrite("intractable") == (-erf(z) + 1)*exp(z**2)
Пример #12
0
def test_risch_option():
    # risch=True only allowed on indefinite integrals
    pytest.raises(ValueError,
                  lambda: integrate(1 / log(x), (x, 0, oo), risch=True))
    assert integrate(exp(-x**2), x,
                     risch=True) == NonElementaryIntegral(exp(-x**2), x)
    assert integrate(log(1 / x) * y, x, y,
                     risch=True) == y**2 * (x * log(1 / x) / 2 + x / 2)
    assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
Пример #13
0
def test__erfs():
    assert _erfs(z).diff(z) == -2/sqrt(pi) + 2*z*_erfs(z)
    pytest.raises(ArgumentIndexError, lambda: _erfs(x).fdiff(2))

    assert _erfs(1/z).series(z) == \
        z/sqrt(pi) - z**3/(2*sqrt(pi)) + 3*z**5/(4*sqrt(pi)) + O(z**6)

    assert expand(erf(z).rewrite('tractable').diff(z).rewrite('intractable')) \
        == erf(z).diff(z)
    assert _erfs(z).rewrite("intractable") == (-erf(z) + 1)*exp(z**2)
Пример #14
0
def test__erfs():
    assert _erfs(z).diff(z) == -2 / sqrt(pi) + 2 * z * _erfs(z)
    pytest.raises(ArgumentIndexError, lambda: _erfs(x).fdiff(2))

    assert _erfs(1/z).series(z) == \
        z/sqrt(pi) - z**3/(2*sqrt(pi)) + 3*z**5/(4*sqrt(pi)) + O(z**6)

    assert expand(erf(z).rewrite('tractable').diff(z).rewrite('intractable')) \
        == erf(z).diff(z)
    assert _erfs(z).rewrite('intractable') == (-erf(z) + 1) * exp(z**2)
Пример #15
0
def test_erfinv():
    assert erfinv(0) == 0
    assert erfinv(1) == S.Infinity
    assert erfinv(nan) == S.NaN

    assert erfinv(erf(w)) == w
    assert erfinv(erf(-w)) == -w

    assert erfinv(x).diff() == sqrt(pi)*exp(erfinv(x)**2)/2

    assert erfinv(z).rewrite('erfcinv') == erfcinv(1-z)
Пример #16
0
def test_risch_option():
    # risch=True only allowed on indefinite integrals
    pytest.raises(ValueError, lambda: integrate(1/log(x), (x, 0, oo), risch=True))
    assert integrate(exp(-x**2), x, risch=True) == NonElementaryIntegral(exp(-x**2), x)
    assert integrate(log(1/x)*y, x, y, risch=True) == y**2*(x*log(1/x)/2 + x/2)
    assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
    # TODO: How to test risch=False?

    # issue sympy/sympy#2708
    f = 1/(a + z + log(z))
    integral_f = NonElementaryIntegral(f, (z, 2, 3))
    assert Integral(f, (z, 2, 3)).doit() == integral_f
Пример #17
0
def test_erf_f_code():
    routine = make_routine("test", erf(x) - erf(-2 * x))
    code_gen = FCodeGen()
    source = get_string(code_gen.dump_f95, [routine])
    expected = (
        "REAL*8 function test(x)\n"
        "implicit none\n"
        "REAL*8, intent(in) :: x\n"
        "test = erf(x) + erf(2.0d0*x)\n"
        "end function\n"
    )
    assert source == expected, source
Пример #18
0
def test_erfc():
    assert erfc(nan) == nan

    assert erfc(oo) == 0
    assert erfc(-oo) == 2

    assert erfc(0) == 1

    assert erfc(I*oo) == -oo*I
    assert erfc(-I*oo) == oo*I

    assert erfc(-x) == Integer(2) - erfc(x)
    assert erfc(erfcinv(x)) == x
    assert erfc(erfinv(x)) == 1 - x

    assert erfc(I).is_extended_real is False
    assert erfc(w).is_extended_real is True
    assert erfc(z).is_extended_real is None

    assert conjugate(erfc(z)) == erfc(conjugate(z))

    assert erfc(x).as_leading_term(x) == 1
    assert erfc(1/x).as_leading_term(x) == erfc(1/x)

    assert erfc(z).rewrite('erf') == 1 - erf(z)
    assert erfc(z).rewrite('erfi') == 1 + I*erfi(I*z)
    assert erfc(z).rewrite('fresnels') == 1 - (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) -
                                                       I*fresnels(z*(1 - I)/sqrt(pi)))
    assert erfc(z).rewrite('fresnelc') == 1 - (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) -
                                                       I*fresnels(z*(1 - I)/sqrt(pi)))
    assert erfc(z).rewrite('hyper') == 1 - 2*z*hyper([Rational(1, 2)], [Rational(3, 2)], -z**2)/sqrt(pi)
    assert erfc(z).rewrite('meijerg') == 1 - z*meijerg([Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2)/sqrt(pi)
    assert erfc(z).rewrite('uppergamma') == 1 - sqrt(z**2)*erf(sqrt(z**2))/z
    assert erfc(z).rewrite('expint') == 1 - sqrt(z**2)/z + z*expint(Rational(1, 2), z**2)/sqrt(pi)

    assert erfc(x).as_real_imag() == \
        ((erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 +
          erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2,
          I*(erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) -
             erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) *
          re(x)*Abs(im(x))/(2*im(x)*Abs(re(x)))))
    assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag()
    assert erfc(w).as_real_imag() == (erfc(w), 0)
    assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag()
    assert erfc(I).as_real_imag() == (1, -erfi(1))

    pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2))

    assert erfc(x).taylor_term(3, x, *(-2*x/sqrt(pi), 0)) == 2*x**3/3/sqrt(pi)

    assert erfc(x).limit(x, oo) == 0

    assert erfc(x).diff(x) == -2*exp(-x**2)/sqrt(pi)
Пример #19
0
def test_erfi():
    assert erfi(nan) == nan

    assert erfi(+oo) == +oo
    assert erfi(-oo) == -oo

    assert erfi(0) == 0

    assert erfi(I*oo) == I
    assert erfi(-I*oo) == -I

    assert erfi(-x) == -erfi(x)

    assert erfi(I*erfinv(x)) == I*x
    assert erfi(I*erfcinv(x)) == I*(1 - x)
    assert erfi(I*erf2inv(0, x)) == I*x

    assert erfi(I).is_extended_real is False
    assert erfi(w).is_extended_real is True
    assert erfi(z).is_extended_real is None

    assert conjugate(erfi(z)) == erfi(conjugate(z))

    assert erfi(z).rewrite('erf') == -I*erf(I*z)
    assert erfi(z).rewrite('erfc') == I*erfc(I*z) - I
    assert erfi(z).rewrite('fresnels') == (1 - I)*(fresnelc(z*(1 + I)/sqrt(pi)) -
                                                   I*fresnels(z*(1 + I)/sqrt(pi)))
    assert erfi(z).rewrite('fresnelc') == (1 - I)*(fresnelc(z*(1 + I)/sqrt(pi)) -
                                                   I*fresnels(z*(1 + I)/sqrt(pi)))
    assert erfi(z).rewrite('hyper') == 2*z*hyper([Rational(1, 2)], [Rational(3, 2)], z**2)/sqrt(pi)
    assert erfi(z).rewrite('meijerg') == z*meijerg([Rational(1, 2)], [], [0], [Rational(-1, 2)], -z**2)/sqrt(pi)
    assert erfi(z).rewrite('uppergamma') == (sqrt(-z**2)/z*(uppergamma(Rational(1, 2),
                                                                       -z**2)/sqrt(pi) - 1))
    assert erfi(z).rewrite('expint') == sqrt(-z**2)/z - z*expint(Rational(1, 2), -z**2)/sqrt(pi)

    assert erfi(x).as_real_imag() == \
        ((erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 +
          erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2,
          I*(erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) -
             erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) *
          re(x)*Abs(im(x))/(2*im(x)*Abs(re(x)))))
    assert erfi(x).as_real_imag(deep=False) == erfi(x).as_real_imag()
    assert erfi(w).as_real_imag() == (erfi(w), 0)
    assert erfi(w).as_real_imag(deep=False) == erfi(w).as_real_imag()
    assert erfi(I).as_real_imag() == (0, erf(1))

    pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2))

    assert erfi(x).taylor_term(3, x, *(2*x/sqrt(pi), 0)) == 2*x**3/3/sqrt(pi)

    assert erfi(x).limit(x, oo) == oo
Пример #20
0
def test_erfinv():
    assert erfinv(0) == 0
    assert erfinv(-1) == -oo
    assert erfinv(+1) == +oo
    assert erfinv(nan) == nan

    assert erfinv(erf(w)) == w
    assert erfinv(erf(-w)) == -w

    assert erfinv(x).diff() == sqrt(pi) * exp(erfinv(x)**2) / 2

    assert erfinv(z).rewrite('erfcinv') == erfcinv(1 - z)

    pytest.raises(ArgumentIndexError, lambda: erfinv(x).fdiff(2))
Пример #21
0
def test_erfinv():
    assert erfinv(0) == 0
    assert erfinv(-1) == -oo
    assert erfinv(+1) == +oo
    assert erfinv(nan) == nan

    assert erfinv(erf(w)) == w
    assert erfinv(erf(-w)) == -w

    assert erfinv(x).diff() == sqrt(pi)*exp(erfinv(x)**2)/2

    assert erfinv(z).rewrite('erfcinv') == erfcinv(1-z)

    pytest.raises(ArgumentIndexError, lambda: erfinv(x).fdiff(2))
Пример #22
0
def test_recursive():
    a, b, c = symbols('a b c', positive=True)
    r = exp(-(x - a)**2)*exp(-(x - b)**2)
    e = integrate(r, (x, 0, oo), meijerg=True)
    assert simplify(e.expand()) == (
        sqrt(2)*sqrt(pi)*(
            (erf(sqrt(2)*(a + b)/2) + 1)*exp(-a**2/2 + a*b - b**2/2))/4)
    e = integrate(exp(-(x - a)**2)*exp(-(x - b)**2)*exp(c*x), (x, 0, oo), meijerg=True)
    assert simplify(e) == (
        sqrt(2)*sqrt(pi)*(erf(sqrt(2)*(2*a + 2*b + c)/4) + 1)*exp(-a**2 - b**2
                                                                  + (2*a + 2*b + c)**2/8)/4)
    assert simplify(integrate(exp(-(x - a - b - c)**2), (x, 0, oo), meijerg=True)) == \
        sqrt(pi)/2*(1 + erf(a + b + c))
    assert simplify(integrate(exp(-(x + a + b + c)**2), (x, 0, oo), meijerg=True)) == \
        sqrt(pi)/2*(1 - erf(a + b + c))
Пример #23
0
def test_recursive():
    a, b, c = symbols('a b c', positive=True)
    r = exp(-(x - a)**2)*exp(-(x - b)**2)
    e = integrate(r, (x, 0, oo), meijerg=True)
    assert simplify(e.expand()) == (
        sqrt(2)*sqrt(pi)*(
            (erf(sqrt(2)*(a + b)/2) + 1)*exp(-a**2/2 + a*b - b**2/2))/4)
    e = integrate(exp(-(x - a)**2)*exp(-(x - b)**2)*exp(c*x), (x, 0, oo), meijerg=True)
    assert simplify(e) == (
        sqrt(2)*sqrt(pi)*(erf(sqrt(2)*(2*a + 2*b + c)/4) + 1)*exp(-a**2 - b**2
                                                                  + (2*a + 2*b + c)**2/8)/4)
    assert simplify(integrate(exp(-(x - a - b - c)**2), (x, 0, oo), meijerg=True)) == \
        sqrt(pi)/2*(1 + erf(a + b + c))
    assert simplify(integrate(exp(-(x + a + b + c)**2), (x, 0, oo), meijerg=True)) == \
        sqrt(pi)/2*(1 - erf(a + b + c))
Пример #24
0
def test_erfc():
    assert erfc(nan) == nan

    assert erfc(oo) == 0
    assert erfc(-oo) == 2

    assert erfc(0) == 1

    assert erfc(I * oo) == -oo * I
    assert erfc(-I * oo) == oo * I

    assert erfc(-x) == Integer(2) - erfc(x)
    assert erfc(erfcinv(x)) == x

    assert erfc(I).is_extended_real is False
    assert erfc(w).is_extended_real is True
    assert erfc(z).is_extended_real is None

    assert conjugate(erfc(z)) == erfc(conjugate(z))

    assert erfc(x).as_leading_term(x) == S.One
    assert erfc(1 / x).as_leading_term(x) == erfc(1 / x)

    assert erfc(z).rewrite('erf') == 1 - erf(z)
    assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z)
    assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite(
        'hyper') == 1 - 2 * z * hyper([S.Half], [3 * S.Half], -z**2) / sqrt(pi)
    assert erfc(z).rewrite('meijerg') == 1 - z * meijerg(
        [S.Half], [], [0], [-S.Half], z**2) / sqrt(pi)
    assert erfc(z).rewrite(
        'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z
    assert erfc(z).rewrite('expint') == S.One - sqrt(z**2) / z + z * expint(
        S.Half, z**2) / sqrt(S.Pi)

    assert erfc(x).as_real_imag() == \
        ((erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 +
         erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2,
         I*(erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) -
         erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) *
         re(x)*Abs(im(x))/(2*im(x)*Abs(re(x)))))

    pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2))
Пример #25
0
def test_sympyissue_7383():
    x, z, R, a = symbols('x z R a')
    r = sqrt(x**2 + z**2)
    u = erf(a * r / sqrt(2)) / r
    Ec = u.diff(z, z).subs({x: sqrt(R * R - z * z)})
    assert integrate(Ec, (z, -R, R)).simplify() == \
        -2*sqrt(2)*R*a**3*exp(-R**2*a**2/2)/(3*sqrt(pi))
Пример #26
0
def test_heurisch_hacking():
    assert (heurisch(sqrt(1 + 7 * x**2), x,
                     hints=[]) == x * sqrt(1 + 7 * x**2) / 2 +
            sqrt(7) * asinh(sqrt(7) * x) / 14)
    assert (heurisch(sqrt(1 - 7 * x**2), x,
                     hints=[]) == x * sqrt(1 - 7 * x**2) / 2 +
            sqrt(7) * asin(sqrt(7) * x) / 14)

    assert heurisch(sqrt(y * x**2 - 1), x, hints=[]) is None

    assert (heurisch(1 / sqrt(1 + 7 * x**2), x,
                     hints=[]) == sqrt(7) * asinh(sqrt(7) * x) / 7)
    assert (heurisch(1 / sqrt(1 - 7 * x**2), x,
                     hints=[]) == sqrt(7) * asin(sqrt(7) * x) / 7)

    assert heurisch(exp(-7 * x**2), x,
                    hints=[]) == sqrt(7 * pi) * erf(sqrt(7) * x) / 14
    assert heurisch(exp(2 * x**2), x,
                    hints=[]) == sqrt(2) * sqrt(pi) * erfi(sqrt(2) * x) / 4

    assert (heurisch(exp(2 * x**2 - 3 * x), x,
                     hints=[]) == sqrt(2) * sqrt(pi) *
            erfi(sqrt(2) * x - 3 * sqrt(2) / 4) / (4 * E**Rational(9, 8)))

    assert heurisch(1 / sqrt(9 - 4 * x**2), x, hints=[]) == asin(2 * x / 3) / 2
    assert heurisch(1 / sqrt(9 + 4 * x**2), x,
                    hints=[]) == asinh(2 * x / 3) / 2

    assert heurisch(li(x), x, hints=[]) == x * li(x) - Ei(2 * log(x))
    assert heurisch(li(log(x)), x, hints=[]) is None

    assert (heurisch(sqrt(1 + x), x,
                     hints=[x, sqrt(1 + x)]) == 2 * x * sqrt(x + 1) / 3 +
            2 * sqrt(x + 1) / 3)
Пример #27
0
def test_sympyissue_7383():
    x, z, R, a = symbols('x z R a')
    r = sqrt(x**2 + z**2)
    u = erf(a*r/sqrt(2))/r
    Ec = u.diff(z, z).subs({x: sqrt(R*R - z*z)})
    assert integrate(Ec, (z, -R, R)).simplify() == \
        -2*sqrt(2)*R*a**3*exp(-R**2*a**2/2)/(3*sqrt(pi))
Пример #28
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
Пример #29
0
def test_NormalDistribution():
    nd = NormalDistribution(0, 1)
    x = Symbol('x')
    assert nd.cdf(x) == erf(sqrt(2)*x/2)/2 + Rational(1, 2)
    assert isinstance(nd.sample(), float) or nd.sample().is_Number
    assert nd.expectation(1, x) == 1
    assert nd.expectation(x, x) == 0
    assert nd.expectation(x**2, x) == 1
Пример #30
0
def test_NormalDistribution():
    nd = NormalDistribution(0, 1)
    x = Symbol('x')
    assert nd.cdf(x) == erf(sqrt(2)*x/2)/2 + Rational(1, 2)
    assert isinstance(nd.sample(), float) or nd.sample().is_Number
    assert nd.expectation(1, x) == 1
    assert nd.expectation(x, x) == 0
    assert nd.expectation(x**2, x) == 1
Пример #31
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
Пример #32
0
def test_expint():
    assert mytn(expint(x, y), expint(x, y).rewrite(uppergamma),
                y**(x - 1)*uppergamma(1 - x, y), x)
    assert mytd(
        expint(x, y), -y**(x - 1)*meijerg([], [1, 1], [0, 0, 1 - x], [], y), x)
    assert mytd(expint(x, y), -expint(x - 1, y), y)
    assert mytn(expint(1, x), expint(1, x).rewrite(Ei),
                -Ei(x*polar_lift(-1)) + I*pi, x)

    assert expint(-4, x) == exp(-x)/x + 4*exp(-x)/x**2 + 12*exp(-x)/x**3 \
        + 24*exp(-x)/x**4 + 24*exp(-x)/x**5
    assert expint(-Rational(3, 2), x) == \
        exp(-x)/x + 3*exp(-x)/(2*x**2) - 3*sqrt(pi)*erf(sqrt(x))/(4*x**Rational(5, 2)) \
        + 3*sqrt(pi)/(4*x**Rational(5, 2))

    assert tn_branch(expint, 1)
    assert tn_branch(expint, 2)
    assert tn_branch(expint, 3)
    assert tn_branch(expint, 1.7)
    assert tn_branch(expint, pi)

    assert expint(y, x*exp_polar(2*I*pi)) == \
        x**(y - 1)*(exp(2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(y, x*exp_polar(-2*I*pi)) == \
        x**(y - 1)*(exp(-2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(2, x*exp_polar(2*I*pi)) == 2*I*pi*x + expint(2, x)
    assert expint(2, x*exp_polar(-2*I*pi)) == -2*I*pi*x + expint(2, x)
    assert (expint(n, x*exp_polar(2*I*pi)) ==
            expint(n, x*exp_polar(2*I*pi), evaluate=False))

    assert expint(1, x).rewrite(Ei).rewrite(expint) == expint(1, x)
    assert (expint(2, x, evaluate=False).rewrite(Shi) ==
            expint(2, x, evaluate=False))
    assert mytn(E1(x), E1(x).rewrite(Shi), Shi(x) - Chi(x), x)
    assert mytn(E1(polar_lift(I)*x), E1(polar_lift(I)*x).rewrite(Si),
                -Ci(x) + I*Si(x) - I*pi/2, x)

    assert mytn(expint(2, x), expint(2, x).rewrite(Ei).rewrite(expint),
                -x*E1(x) + exp(-x), x)
    assert mytn(expint(3, x), expint(3, x).rewrite(Ei).rewrite(expint),
                x**2*E1(x)/2 + (1 - x)*exp(-x)/2, x)

    assert expint(Rational(3, 2), z).nseries(z, n=10) == \
        2 + 2*z - z**2/3 + z**3/15 - z**4/84 + z**5/540 - \
        2*sqrt(pi)*sqrt(z) + O(z**6)

    assert E1(z).series(z) == -EulerGamma - log(z) + z - \
        z**2/4 + z**3/18 - z**4/96 + z**5/600 + O(z**6)

    assert expint(4, z).series(z) == Rational(1, 3) - z/2 + z**2/2 + \
        z**3*(log(z)/6 - Rational(11, 36) + EulerGamma/6) - z**4/24 + \
        z**5/240 + O(z**6)
    assert (expint(x, x).series(x, x0=1, n=2) ==
            expint(1, 1) + (x - 1)*(-meijerg(((), (1, 1)),
                                             ((0, 0, 0), ()), 1) - 1/E) +
            O((x - 1)**2, (x, 1)))

    pytest.raises(ArgumentIndexError, lambda: expint(x, y).fdiff(3))
Пример #33
0
def test_simplify_other():
    assert simplify(sin(x)**2 + cos(x)**2) == 1
    assert simplify(gamma(x + 1)/gamma(x)) == x
    assert simplify(sin(x)**2 + cos(x)**2 + factorial(x)/gamma(x)) == 1 + x
    assert simplify(
        Eq(sin(x)**2 + cos(x)**2, factorial(x)/gamma(x))) == Eq(x, 1)
    nc = symbols('nc', commutative=False)
    assert simplify(x + x*nc) == x*(1 + nc)
    # issue sympy/sympy#6123
    # f = exp(-I*(k*sqrt(t) + x/(2*sqrt(t)))**2)
    # ans = integrate(f, (k, -oo, oo), conds='none')
    ans = (I*(-pi*x*exp(-3*I*pi/4 + I*x**2/(4*t))*erf(x*exp(-3*I*pi/4) /
                                                      (2*sqrt(t)))/(2*sqrt(t)) + pi*x*exp(-3*I*pi/4 + I*x**2/(4*t)) /
              (2*sqrt(t)))*exp(-I*x**2/(4*t))/(sqrt(pi)*x) - I*sqrt(pi) *
           (-erf(x*exp(I*pi/4)/(2*sqrt(t))) + 1)*exp(I*pi/4)/(2*sqrt(t)))
    assert simplify(ans) == -(-1)**Rational(3, 4)*sqrt(pi)/sqrt(t)
    # issue sympy/sympy#6370
    assert simplify(2**(2 + x)/4) == 2**x
Пример #34
0
def test_expint():
    assert mytn(expint(x, y),
                expint(x, y).rewrite(uppergamma),
                y**(x - 1) * uppergamma(1 - x, y), x)
    assert mytd(expint(x, y),
                -y**(x - 1) * meijerg([], [1, 1], [0, 0, 1 - x], [], y), x)
    assert mytd(expint(x, y), -expint(x - 1, y), y)
    assert mytn(expint(1, x),
                expint(1, x).rewrite(Ei), -Ei(x * polar_lift(-1)) + I * pi, x)

    assert expint(-4, x) == exp(-x)/x + 4*exp(-x)/x**2 + 12*exp(-x)/x**3 \
        + 24*exp(-x)/x**4 + 24*exp(-x)/x**5
    assert expint(-Rational(3, 2), x) == \
        exp(-x)/x + 3*exp(-x)/(2*x**2) - 3*sqrt(pi)*erf(sqrt(x))/(4*x**Rational(5, 2)) \
        + 3*sqrt(pi)/(4*x**Rational(5, 2))

    assert tn_branch(expint, 1)
    assert tn_branch(expint, 2)
    assert tn_branch(expint, 3)
    assert tn_branch(expint, 1.7)
    assert tn_branch(expint, pi)

    assert expint(y, x*exp_polar(2*I*pi)) == \
        x**(y - 1)*(exp(2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(y, x*exp_polar(-2*I*pi)) == \
        x**(y - 1)*(exp(-2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(2,
                  x * exp_polar(2 * I * pi)) == 2 * I * pi * x + expint(2, x)
    assert expint(2, x *
                  exp_polar(-2 * I * pi)) == -2 * I * pi * x + expint(2, x)
    assert expint(1, x).rewrite(Ei).rewrite(expint) == expint(1, x)

    assert mytn(E1(x), E1(x).rewrite(Shi), Shi(x) - Chi(x), x)
    assert mytn(E1(polar_lift(I) * x),
                E1(polar_lift(I) * x).rewrite(Si),
                -Ci(x) + I * Si(x) - I * pi / 2, x)

    assert mytn(expint(2, x),
                expint(2, x).rewrite(Ei).rewrite(expint), -x * E1(x) + exp(-x),
                x)
    assert mytn(expint(3, x),
                expint(3, x).rewrite(Ei).rewrite(expint),
                x**2 * E1(x) / 2 + (1 - x) * exp(-x) / 2, x)

    assert expint(Rational(3, 2), z).nseries(z, n=10) == \
        2 + 2*z - z**2/3 + z**3/15 - z**4/84 + z**5/540 - \
        2*sqrt(pi)*sqrt(z) + O(z**6)

    assert E1(z).series(z) == -EulerGamma - log(z) + z - \
        z**2/4 + z**3/18 - z**4/96 + z**5/600 + O(z**6)

    assert expint(4, z).series(z) == Rational(1, 3) - z/2 + z**2/2 + \
        z**3*(log(z)/6 - Rational(11, 36) + EulerGamma/6) - z**4/24 + \
        z**5/240 + O(z**6)
Пример #35
0
def test_sympyissue_3940():
    a, b, c, d = symbols('a:d', positive=True, finite=True)
    assert integrate(exp(-x**2 + I*c*x), x) == \
        -sqrt(pi)*exp(-c**2/4)*erf(I*c/2 - x)/2
    assert integrate(exp(a*x**2 + b*x + c), x) == \
        sqrt(pi)*exp(c)*exp(-b**2/(4*a))*erfi(sqrt(a)*x + b/(2*sqrt(a)))/(2*sqrt(a))

    assert expand_mul(integrate(exp(-x**2)*exp(I*k*x), (x, -oo, oo))) == \
        sqrt(pi)*exp(-k**2/4)
    a, d = symbols('a d', positive=True)
    assert expand_mul(integrate(exp(-a*x**2 + 2*d*x), (x, -oo, oo))) == \
        sqrt(pi)*exp(d**2/a)/sqrt(a)
Пример #36
0
def test_sympyissue_3940():
    a, b, c, d = symbols('a:d', positive=True, finite=True)
    assert integrate(exp(-x**2 + I*c*x), x) == \
        -sqrt(pi)*exp(-c**2/4)*erf(I*c/2 - x)/2
    assert integrate(exp(a*x**2 + b*x + c), x) == \
        sqrt(pi)*exp(c)*exp(-b**2/(4*a))*erfi(sqrt(a)*x + b/(2*sqrt(a)))/(2*sqrt(a))

    assert expand_mul(integrate(exp(-x**2)*exp(I*k*x), (x, -oo, oo))) == \
        sqrt(pi)*exp(-k**2/4)
    a, d = symbols('a d', positive=True)
    assert expand_mul(integrate(exp(-a*x**2 + 2*d*x), (x, -oo, oo))) == \
        sqrt(pi)*exp(d**2/a)/sqrt(a)
Пример #37
0
def test_ratsimp():
    f, g = 1/x + 1/y, (x + y)/(x*y)

    assert f != g and ratsimp(f) == g

    f, g = 1/(1 + 1/x), 1 - 1/(x + 1)

    assert f != g and ratsimp(f) == g

    f, g = x/(x + y) + y/(x + y), 1

    assert f != g and ratsimp(f) == g

    f, g = -x - y - y**2/(x + y) + x**2/(x + y), -2*y

    assert f != g and ratsimp(f) == g

    f = (a*c*x*y + a*c*z - b*d*x*y - b*d*z - b*t*x*y - b*t*x - b*t*z +
         e*x)/(x*y + z)
    G = [a*c - b*d - b*t + (-b*t*x + e*x)/(x*y + z),
         a*c - b*d - b*t - ( b*t*x - e*x)/(x*y + z)]

    assert f != g and ratsimp(f) in G

    A = sqrt(pi)

    B = log(erf(x) - 1)
    C = log(erf(x) + 1)

    D = 8 - 8*erf(x)

    f = A*B/D - A*C/D + A*C*erf(x)/D - A*B*erf(x)/D + 2*A/D

    assert ratsimp(f) == A*B/8 - A*C/8 - A/(4*erf(x) - 4)
Пример #38
0
def test_ratsimp():
    f, g = 1/x + 1/y, (x + y)/(x*y)

    assert f != g and ratsimp(f) == g

    f, g = 1/(1 + 1/x), 1 - 1/(x + 1)

    assert f != g and ratsimp(f) == g

    f, g = x/(x + y) + y/(x + y), 1

    assert f != g and ratsimp(f) == g

    f, g = -x - y - y**2/(x + y) + x**2/(x + y), -2*y

    assert f != g and ratsimp(f) == g

    f = (a*c*x*y + a*c*z - b*d*x*y - b*d*z - b*t*x*y - b*t*x - b*t*z +
         e*x)/(x*y + z)
    G = [a*c - b*d - b*t + (-b*t*x + e*x)/(x*y + z),
         a*c - b*d - b*t - ( b*t*x - e*x)/(x*y + z)]

    assert f != g and ratsimp(f) in G

    A = sqrt(pi)

    B = log(erf(x) - 1)
    C = log(erf(x) + 1)

    D = 8 - 8*erf(x)

    f = A*B/D - A*C/D + A*C*erf(x)/D - A*B*erf(x)/D + 2*A/D

    assert ratsimp(f) == A*B/8 - A*C/8 - A/(4*erf(x) - 4)
Пример #39
0
def test_unpolarify():
    p = exp_polar(7*I) + 1
    u = exp(7*I) + 1

    assert unpolarify(1) == 1
    assert unpolarify(p) == u
    assert unpolarify(p**2) == u**2
    assert unpolarify(p**x) == p**x
    assert unpolarify(p*x) == u*x
    assert unpolarify(p + x) == u + x
    assert unpolarify(sqrt(sin(p))) == sqrt(sin(u))

    # Test reduction to principal branch 2*pi.
    t = principal_branch(x, 2*pi)
    assert unpolarify(t) == x
    assert unpolarify(sqrt(t)) == sqrt(t)

    # Test exponents_only.
    assert unpolarify(p**p, exponents_only=True) == p**u
    assert unpolarify(uppergamma(x, p**p)) == uppergamma(x, p**u)

    # Test functions.
    assert unpolarify(sin(p)) == sin(u)
    assert unpolarify(tanh(p)) == tanh(u)
    assert unpolarify(gamma(p)) == gamma(u)
    assert unpolarify(erf(p)) == erf(u)
    assert unpolarify(uppergamma(x, p)) == uppergamma(x, p)

    assert unpolarify(uppergamma(sin(p), sin(p + exp_polar(0)))) == \
        uppergamma(sin(u), sin(u + 1))
    assert unpolarify(uppergamma(polar_lift(0), 2*exp_polar(0))) == \
        uppergamma(0, 2)

    assert unpolarify(Eq(p, 0)) == Eq(u, 0)
    assert unpolarify(Ne(p, 0)) == Ne(u, 0)
    assert unpolarify(polar_lift(x) > 0) == (x > 0)

    # Test bools
    assert unpolarify(True) is True
Пример #40
0
def test_unpolarify():
    p = exp_polar(7*I) + 1
    u = exp(7*I) + 1

    assert unpolarify(1) == 1
    assert unpolarify(p) == u
    assert unpolarify(p**2) == u**2
    assert unpolarify(p**x) == p**x
    assert unpolarify(p*x) == u*x
    assert unpolarify(p + x) == u + x
    assert unpolarify(sqrt(sin(p))) == sqrt(sin(u))

    # Test reduction to principal branch 2*pi.
    t = principal_branch(x, 2*pi)
    assert unpolarify(t) == x
    assert unpolarify(sqrt(t)) == sqrt(t)

    # Test exponents_only.
    assert unpolarify(p**p, exponents_only=True) == p**u
    assert unpolarify(uppergamma(x, p**p)) == uppergamma(x, p**u)

    # Test functions.
    assert unpolarify(sin(p)) == sin(u)
    assert unpolarify(tanh(p)) == tanh(u)
    assert unpolarify(gamma(p)) == gamma(u)
    assert unpolarify(erf(p)) == erf(u)
    assert unpolarify(uppergamma(x, p)) == uppergamma(x, p)

    assert unpolarify(uppergamma(sin(p), sin(p + exp_polar(0)))) == \
        uppergamma(sin(u), sin(u + 1))
    assert unpolarify(uppergamma(polar_lift(0), 2*exp_polar(0))) == \
        uppergamma(0, 2)

    assert unpolarify(Eq(p, 0)) == Eq(u, 0)
    assert unpolarify(Ne(p, 0)) == Ne(u, 0)
    assert unpolarify(polar_lift(x) > 0) == (x > 0)

    # Test bools
    assert unpolarify(True) is True
Пример #41
0
def test_erfi():
    assert erfi(nan) == nan

    assert erfi(oo) == S.Infinity
    assert erfi(-oo) == S.NegativeInfinity

    assert erfi(0) == S.Zero

    assert erfi(I * oo) == I
    assert erfi(-I * oo) == -I

    assert erfi(-x) == -erfi(x)

    assert erfi(I * erfinv(x)) == I * x
    assert erfi(I * erfcinv(x)) == I * (1 - x)
    assert erfi(I * erf2inv(0, x)) == I * x

    assert erfi(I).is_extended_real is False
    assert erfi(w).is_extended_real is True
    assert erfi(z).is_extended_real is None

    assert conjugate(erfi(z)) == erfi(conjugate(z))

    assert erfi(z).rewrite('erf') == -I * erf(I * z)
    assert erfi(z).rewrite('erfc') == I * erfc(I * z) - I
    assert erfi(z).rewrite('fresnels') == (1 - I) * (
        fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 + I) / sqrt(pi)))
    assert erfi(z).rewrite('fresnelc') == (1 - I) * (
        fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 + I) / sqrt(pi)))
    assert erfi(z).rewrite('hyper') == 2 * z * hyper([S.Half], [3 * S.Half], z
                                                     **2) / sqrt(pi)
    assert erfi(z).rewrite('meijerg') == z * meijerg(
        [S.Half], [], [0], [-S.Half], -z**2) / sqrt(pi)
    assert erfi(z).rewrite('uppergamma') == (
        sqrt(-z**2) / z * (uppergamma(S.Half, -z**2) / sqrt(S.Pi) - S.One))
    assert erfi(z).rewrite(
        'expint') == sqrt(-z**2) / z - z * expint(S.Half, -z**2) / sqrt(S.Pi)

    assert erfi(x).as_real_imag() == \
        ((erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 +
         erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2,
         I*(erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) -
         erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) *
         re(x)*Abs(im(x))/(2*im(x)*Abs(re(x)))))

    pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2))
Пример #42
0
def test_inverse_laplace_transform():
    from diofant import sinh, cosh, besselj, besseli, simplify, factor_terms
    ILT = inverse_laplace_transform
    a, b, c, = symbols('a b c', positive=True, finite=True)
    t = symbols('t')

    def simp_hyp(expr):
        return factor_terms(expand_mul(expr)).rewrite(sin)

    # just test inverses of all of the above
    assert ILT(1 / s, s, t) == Heaviside(t)
    assert ILT(1 / s**2, s, t) == t * Heaviside(t)
    assert ILT(1 / s**5, s, t) == t**4 * Heaviside(t) / 24
    assert ILT(exp(-a * s) / s, s, t) == Heaviside(t - a)
    assert ILT(exp(-a * s) / (s + b), s,
               t) == exp(b * (a - t)) * Heaviside(-a + t)
    assert ILT(a / (s**2 + a**2), s, t) == sin(a * t) * Heaviside(t)
    assert ILT(s / (s**2 + a**2), s, t) == cos(a * t) * Heaviside(t)
    # TODO is there a way around simp_hyp?
    assert simp_hyp(ILT(a / (s**2 - a**2), s, t)) == sinh(a * t) * Heaviside(t)
    assert simp_hyp(ILT(s / (s**2 - a**2), s, t)) == cosh(a * t) * Heaviside(t)
    assert ILT(a / ((s + b)**2 + a**2), s,
               t) == exp(-b * t) * sin(a * t) * Heaviside(t)
    assert ILT((s + b) / ((s + b)**2 + a**2), s,
               t) == exp(-b * t) * cos(a * t) * Heaviside(t)
    # TODO sinh/cosh shifted come out a mess. also delayed trig is a mess
    # TODO should this simplify further?
    assert ILT(exp(-a*s)/s**b, s, t) == \
        (t - a)**(b - 1)*Heaviside(t - a)/gamma(b)

    assert ILT(exp(-a*s)/sqrt(1 + s**2), s, t) == \
        Heaviside(t - a)*besselj(0, a - t)  # note: besselj(0, x) is even

    # XXX ILT turns these branch factor into trig functions ...
    assert simplify(ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2),
                    s, t).rewrite(exp)) == \
        Heaviside(t)*besseli(b, a*t)
    assert ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2),
               s, t).rewrite(exp) == \
        Heaviside(t)*besselj(b, a*t)

    assert ILT(1 / (s * sqrt(s + 1)), s, t) == Heaviside(t) * erf(sqrt(t))
    # TODO can we make erf(t) work?

    assert ILT(1 / (s**2 * (s**2 + 1)), s, t) == (t - sin(t)) * Heaviside(t)

    assert ILT( (s * eye(2) - Matrix([[1, 0], [0, 2]])).inv(), s, t) ==\
        Matrix([[exp(t)*Heaviside(t), 0], [0, exp(2*t)*Heaviside(t)]])
Пример #43
0
def test_single_normal():
    mu = Symbol('mu', real=True)
    sigma = Symbol('sigma', real=True, positive=True)
    X = Normal('x', 0, 1)
    Y = X*sigma + mu

    assert simplify(E(Y)) == mu
    assert simplify(variance(Y)) == sigma**2
    pdf = density(Y)
    x = Symbol('x')
    assert (pdf(x) ==
            sqrt(2)*exp(-(x - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma))

    assert P(X**2 < 1) == erf(sqrt(2)/2)

    assert E(X, Eq(X, mu)) == mu
Пример #44
0
def test_single_normal():
    mu = Symbol('mu', real=True)
    sigma = Symbol('sigma', real=True, positive=True)
    X = Normal('x', 0, 1)
    Y = X*sigma + mu

    assert simplify(E(Y)) == mu
    assert simplify(variance(Y)) == sigma**2
    pdf = density(Y)
    x = Symbol('x')
    assert (pdf(x) ==
            sqrt(2)*exp(-(x - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma))

    assert P(X**2 < 1) == erf(sqrt(2)/2)

    assert E(X, Eq(X, mu)) == mu
Пример #45
0
def test_heurisch_hacking():
    assert (heurisch(sqrt(1 + 7*x**2), x, hints=[]) ==
            x*sqrt(1 + 7*x**2)/2 + sqrt(7)*asinh(sqrt(7)*x)/14)
    assert (heurisch(sqrt(1 - 7*x**2), x, hints=[]) ==
            x*sqrt(1 - 7*x**2)/2 + sqrt(7)*asin(sqrt(7)*x)/14)

    assert (heurisch(1/sqrt(1 + 7*x**2), x, hints=[]) ==
            sqrt(7)*asinh(sqrt(7)*x)/7)
    assert (heurisch(1/sqrt(1 - 7*x**2), x, hints=[]) ==
            sqrt(7)*asin(sqrt(7)*x)/7)

    assert (heurisch(exp(-7*x**2), x, hints=[]) == sqrt(7*pi)*erf(sqrt(7)*x)/14)

    assert heurisch(1/sqrt(9 - 4*x**2), x, hints=[]) == asin(2*x/3)/2
    assert heurisch(1/sqrt(9 + 4*x**2), x, hints=[]) == asinh(2*x/3)/2

    assert heurisch(li(x), x, hints=[]) == x*li(x) - Ei(2*log(x))
Пример #46
0
def test_inverse_laplace_transform():
    ILT = inverse_laplace_transform
    a, b, c, = symbols('a b c', positive=True, finite=True)

    def simp_hyp(expr):
        return factor_terms(expand_mul(expr)).rewrite(sin)

    # just test inverses of all of the above
    assert ILT(1/s, s, t) == Heaviside(t)
    assert ILT(1/s**2, s, t) == t*Heaviside(t)
    assert ILT(1/s**5, s, t) == t**4*Heaviside(t)/24
    assert ILT(exp(-a*s)/s, s, t) == Heaviside(t - a)
    assert ILT(exp(-a*s)/(s + b), s, t) == exp(b*(a - t))*Heaviside(-a + t)
    assert ILT(a/(s**2 + a**2), s, t) == sin(a*t)*Heaviside(t)
    assert ILT(s/(s**2 + a**2), s, t) == cos(a*t)*Heaviside(t)
    # TODO is there a way around simp_hyp?
    assert simp_hyp(ILT(a/(s**2 - a**2), s, t)) == sinh(a*t)*Heaviside(t)
    assert simp_hyp(ILT(s/(s**2 - a**2), s, t)) == cosh(a*t)*Heaviside(t)
    assert ILT(a/((s + b)**2 + a**2), s, t) == exp(-b*t)*sin(a*t)*Heaviside(t)
    assert ILT(
        (s + b)/((s + b)**2 + a**2), s, t) == exp(-b*t)*cos(a*t)*Heaviside(t)
    # TODO sinh/cosh shifted come out a mess. also delayed trig is a mess
    # TODO should this simplify further?
    assert ILT(exp(-a*s)/s**b, s, t) == \
        (t - a)**(b - 1)*Heaviside(t - a)/gamma(b)

    assert ILT(exp(-a*s)/sqrt(1 + s**2), s, t) == \
        Heaviside(t - a)*besselj(0, a - t)  # note: besselj(0, x) is even

    # XXX ILT turns these branch factor into trig functions ...
    assert simplify(ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2),
                        s, t).rewrite(exp)) == \
        Heaviside(t)*besseli(b, a*t)
    assert ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2),
               s, t).rewrite(exp) == \
        Heaviside(t)*besselj(b, a*t)

    assert ILT(1/(s*sqrt(s + 1)), s, t) == Heaviside(t)*erf(sqrt(t))
    # TODO can we make erf(t) work?

    assert ILT(1/(s**2*(s**2 + 1)), s, t) == (t - sin(t))*Heaviside(t)

    assert ILT( (s * eye(2) - Matrix([[1, 0], [0, 2]])).inv(), s, t) ==\
        Matrix([[exp(t)*Heaviside(t), 0], [0, exp(2*t)*Heaviside(t)]])
Пример #47
0
def test_lowergamma():
    assert lowergamma(x, y).diff(y) == y**(x - 1)*exp(-y)
    assert td(lowergamma(randcplx(), y), y)
    assert td(lowergamma(x, randcplx()), x)
    assert lowergamma(x, y).diff(x) == \
        gamma(x)*polygamma(0, x) - uppergamma(x, y)*log(y) \
        - meijerg([], [1, 1], [0, 0, x], [], y)
    pytest.raises(ArgumentIndexError, lambda: lowergamma(x, y).fdiff(3))

    assert lowergamma(Rational(1, 2), x) == sqrt(pi)*erf(sqrt(x))
    assert not lowergamma(Rational(1, 2) - 3, x).has(lowergamma)
    assert not lowergamma(Rational(1, 2) + 3, x).has(lowergamma)
    assert lowergamma(Rational(1, 2), x, evaluate=False).has(lowergamma)
    assert tn(lowergamma(Rational(1, 2) + 3, x, evaluate=False),
              lowergamma(Rational(1, 2) + 3, x), x)
    assert tn(lowergamma(Rational(1, 2) - 3, x, evaluate=False),
              lowergamma(Rational(1, 2) - 3, x), x)
    assert lowergamma(0, 1) == zoo

    assert tn_branch(-3, lowergamma)
    assert tn_branch(-4, lowergamma)
    assert tn_branch(Rational(1, 3), lowergamma)
    assert tn_branch(pi, lowergamma)
    assert lowergamma(3, exp_polar(4*pi*I)*x) == lowergamma(3, x)
    assert lowergamma(y, exp_polar(5*pi*I)*x) == \
        exp(4*I*pi*y)*lowergamma(y, x*exp_polar(pi*I))
    assert lowergamma(-2, exp_polar(5*pi*I)*x) == \
        lowergamma(-2, x*exp_polar(I*pi)) + 2*pi*I

    assert conjugate(lowergamma(x, y)) == lowergamma(conjugate(x), conjugate(y))
    assert conjugate(lowergamma(x, 0)) == conjugate(lowergamma(x, 0))
    assert conjugate(lowergamma(x, -oo)) == conjugate(lowergamma(x, -oo))

    assert lowergamma(
        x, y).rewrite(expint) == -y**x*expint(-x + 1, y) + gamma(x)
    k = Symbol('k', integer=True)
    assert lowergamma(
        k, y).rewrite(expint) == -y**k*expint(-k + 1, y) + gamma(k)
    k = Symbol('k', integer=True, positive=False)
    assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y)
    assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y)

    assert (x*lowergamma(x, 1)/gamma(x + 1)).limit(x, 0) == 1
Пример #48
0
def test_heurisch_hacking():
    assert heurisch(sqrt(1 + 7*x**2), x, hints=[]) == \
        x*sqrt(1 + 7*x**2)/2 + sqrt(7)*asinh(sqrt(7)*x)/14
    assert heurisch(sqrt(1 - 7*x**2), x, hints=[]) == \
        x*sqrt(1 - 7*x**2)/2 + sqrt(7)*asin(sqrt(7)*x)/14

    assert heurisch(1/sqrt(1 + 7*x**2), x, hints=[]) == \
        sqrt(7)*asinh(sqrt(7)*x)/7
    assert heurisch(1/sqrt(1 - 7*x**2), x, hints=[]) == \
        sqrt(7)*asin(sqrt(7)*x)/7

    assert heurisch(exp(-7*x**2), x, hints=[]) == \
        sqrt(7*pi)*erf(sqrt(7)*x)/14

    assert heurisch(1/sqrt(9 - 4*x**2), x, hints=[]) == \
        asin(2*x/3)/2

    assert heurisch(1/sqrt(9 + 4*x**2), x, hints=[]) == \
        asinh(2*x/3)/2
Пример #49
0
def test_lowergamma():
    from diofant import meijerg, exp_polar, I, expint
    assert lowergamma(x, y).diff(y) == y**(x - 1) * exp(-y)
    assert td(lowergamma(randcplx(), y), y)
    assert td(lowergamma(x, randcplx()), x)
    assert lowergamma(x, y).diff(x) == \
        gamma(x)*polygamma(0, x) - uppergamma(x, y)*log(y) \
        - meijerg([], [1, 1], [0, 0, x], [], y)

    assert lowergamma(S.Half, x) == sqrt(pi) * erf(sqrt(x))
    assert not lowergamma(S.Half - 3, x).has(lowergamma)
    assert not lowergamma(S.Half + 3, x).has(lowergamma)
    assert lowergamma(S.Half, x, evaluate=False).has(lowergamma)
    assert tn(lowergamma(S.Half + 3, x, evaluate=False),
              lowergamma(S.Half + 3, x), x)
    assert tn(lowergamma(S.Half - 3, x, evaluate=False),
              lowergamma(S.Half - 3, x), x)

    assert tn_branch(-3, lowergamma)
    assert tn_branch(-4, lowergamma)
    assert tn_branch(Rational(1, 3), lowergamma)
    assert tn_branch(pi, lowergamma)
    assert lowergamma(3, exp_polar(4 * pi * I) * x) == lowergamma(3, x)
    assert lowergamma(y, exp_polar(5*pi*I)*x) == \
        exp(4*I*pi*y)*lowergamma(y, x*exp_polar(pi*I))
    assert lowergamma(-2, exp_polar(5*pi*I)*x) == \
        lowergamma(-2, x*exp_polar(I*pi)) + 2*pi*I

    assert conjugate(lowergamma(x, y)) == lowergamma(conjugate(x),
                                                     conjugate(y))
    assert conjugate(lowergamma(x, 0)) == conjugate(lowergamma(x, 0))
    assert conjugate(lowergamma(x, -oo)) == conjugate(lowergamma(x, -oo))

    assert lowergamma(
        x, y).rewrite(expint) == -y**x * expint(-x + 1, y) + gamma(x)
    k = Symbol('k', integer=True)
    assert lowergamma(
        k, y).rewrite(expint) == -y**k * expint(-k + 1, y) + gamma(k)
    k = Symbol('k', integer=True, positive=False)
    assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y)
    assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y)
Пример #50
0
def test_erf_evalf():
    assert abs( erf(Float(2.0)) - 0.995322265 ) < 1E-8  # XXX
Пример #51
0
def test_risch_option():
    # risch=True only allowed on indefinite integrals
    pytest.raises(ValueError, lambda: integrate(1/log(x), (x, 0, oo), risch=True))
    assert integrate(exp(-x**2), x, risch=True) == NonElementaryIntegral(exp(-x**2), x)
    assert integrate(log(1/x)*y, x, y, risch=True) == y**2*(x*log(1/x)/2 + x/2)
    assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
Пример #52
0
def test_erf_series():
    assert erf(x).series(x, 0, 7) == 2*x/sqrt(pi) - \
        2*x**3/3/sqrt(pi) + x**5/5/sqrt(pi) + O(x**7)
Пример #53
0
def test_sympyissue_13575():
    assert limit(acos(erfi(x)), x, 1) == pi/2 + I*log(sqrt(erf(I)**2 + 1) +
                                                      erf(I))
Пример #54
0
def test_erf():
    assert erf(nan) == nan

    assert erf(oo) == 1
    assert erf(-oo) == -1

    assert erf(0) == 0

    assert erf(I*oo) == oo*I
    assert erf(-I*oo) == -oo*I

    assert erf(-2) == -erf(2)
    assert erf(-x*y) == -erf(x*y)
    assert erf(-x - y) == -erf(x + y)

    assert erf(erfinv(x)) == x
    assert erf(erfcinv(x)) == 1 - x
    assert erf(erf2inv(0, x)) == x
    assert erf(erf2inv(0, erf(erfcinv(1 - erf(erfinv(x)))))) == x

    assert erf(I).is_extended_real is False
    assert erf(w).is_extended_real is True
    assert erf(z).is_extended_real is None

    assert conjugate(erf(z)) == erf(conjugate(z))

    assert erf(x).as_leading_term(x) == 2*x/sqrt(pi)
    assert erf(1/x).as_leading_term(x) == erf(1/x)

    assert erf(z).rewrite('uppergamma') == sqrt(z**2)*erf(sqrt(z**2))/z
    assert erf(z).rewrite('erfc') == 1 - erfc(z)
    assert erf(z).rewrite('erfi') == -I*erfi(I*z)
    assert erf(z).rewrite('fresnels') == (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) -
                                                  I*fresnels(z*(1 - I)/sqrt(pi)))
    assert erf(z).rewrite('fresnelc') == (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) -
                                                  I*fresnels(z*(1 - I)/sqrt(pi)))
    assert erf(z).rewrite('hyper') == 2*z*hyper([Rational(1, 2)], [Rational(3, 2)], -z**2)/sqrt(pi)
    assert erf(z).rewrite('meijerg') == z*meijerg([Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2)/sqrt(pi)
    assert erf(z).rewrite('expint') == sqrt(z**2)/z - z*expint(Rational(1, 2), z**2)/sqrt(pi)

    assert limit(exp(x)*exp(x**2)*(erf(x + 1/exp(x)) - erf(x)), x, oo) == \
        2/sqrt(pi)
    assert limit((1 - erf(z))*exp(z**2)*z, z, oo) == 1/sqrt(pi)
    assert limit((1 - erf(x))*exp(x**2)*sqrt(pi)*x, x, oo) == 1
    assert limit(((1 - erf(x))*exp(x**2)*sqrt(pi)*x - 1)*2*x**2, x, oo) == -1

    l = Limit((1 - erf(y/x))*exp(y**2/x**2), x, 0)
    assert l.doit() == l  # cover _erfs._eval_aseries

    assert erf(x).as_real_imag() == \
        ((erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 +
          erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2,
          I*(erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) -
             erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) *
          re(x)*Abs(im(x))/(2*im(x)*Abs(re(x)))))
    assert erf(x).as_real_imag() == erf(x).as_real_imag(deep=False)
    assert erf(w).as_real_imag() == (erf(w), 0)
    assert erf(w).as_real_imag() == erf(w).as_real_imag(deep=False)
    assert erf(I).as_real_imag() == (0, erfi(1))

    pytest.raises(ArgumentIndexError, lambda: erf(x).fdiff(2))

    assert erf(x).taylor_term(3, x, *(2*x/sqrt(pi), 0)) == -2*x**3/3/sqrt(pi)
Пример #55
0
def test_pmint_erf():
    f = exp(-x**2)*erf(x)/(erf(x)**3 - erf(x)**2 - erf(x) + 1)
    g = sqrt(pi)*log(erf(x) - 1)/8 - sqrt(pi)*log(erf(x) + 1)/8 - sqrt(pi)/(4*erf(x) - 4)

    assert ratsimp(heurisch(f, x)) == g
Пример #56
0
def test_sympyissue_10976():
    assert gruntz(erf(m/x)/erf(1/x), x) == m
Пример #57
0
def test_fresnel():
    assert fresnels(0) == 0
    assert fresnels(+oo) == Rational(+1, 2)
    assert fresnels(-oo) == Rational(-1, 2)

    assert fresnels(z) == fresnels(z)
    assert fresnels(-z) == -fresnels(z)
    assert fresnels(I*z) == -I*fresnels(z)
    assert fresnels(-I*z) == I*fresnels(z)

    assert conjugate(fresnels(z)) == fresnels(conjugate(z))

    assert fresnels(z).diff(z) == sin(pi*z**2/2)

    assert fresnels(z).rewrite(erf) == (1 + I)/4 * (
        erf((1 + I)/2*sqrt(pi)*z) - I*erf((1 - I)/2*sqrt(pi)*z))

    assert fresnels(z).rewrite(hyper) == \
        pi*z**3/6 * hyper([Rational(3, 4)], [Rational(3, 2), Rational(7, 4)], -pi**2*z**4/16)

    assert fresnels(z).series(z, n=15) == \
        pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15)

    assert fresnels(y/z).limit(z, 0) == fresnels(oo*sign(y))

    assert fresnels(x).taylor_term(-1, z) == 0
    assert fresnels(x).taylor_term(1, z, *(pi*z**3/6,)) == -pi**3*z**7/336
    assert fresnels(x).taylor_term(1, z) == -pi**3*z**7/336

    assert fresnels(w).is_extended_real is True
    assert fresnels(z).is_extended_real is None

    assert fresnels(z).as_real_imag() == \
        ((fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 +
          fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2,
          I*(fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) -
             fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) *
          re(z)*Abs(im(z))/(2*im(z)*Abs(re(z)))))
    assert fresnels(z).as_real_imag(deep=False) == fresnels(z).as_real_imag()
    assert fresnels(w).as_real_imag() == (fresnels(w), 0)
    assert fresnels(w).as_real_imag(deep=False) == fresnels(w).as_real_imag()
    assert (fresnels(I, evaluate=False).as_real_imag() ==
            (0, -erf(sqrt(pi)/2 + I*sqrt(pi)/2)/4 +
             I*(-erf(sqrt(pi)/2 + I*sqrt(pi)/2) + erf(sqrt(pi)/2 -
                I*sqrt(pi)/2))/4 - erf(sqrt(pi)/2 - I*sqrt(pi)/2)/4))

    assert fresnels(2 + 3*I).as_real_imag() == (
        fresnels(2 + 3*I)/2 + fresnels(2 - 3*I)/2,
        I*(fresnels(2 - 3*I) - fresnels(2 + 3*I))/2
    )

    assert expand_func(integrate(fresnels(z), z)) == \
        z*fresnels(z) + cos(pi*z**2/2)/pi

    assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(9, 4) * \
        meijerg(((), (1,)), ((Rational(3, 4),),
                             (Rational(1, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(3, 4)*(z**2)**Rational(3, 4))

    assert fresnelc(0) == 0
    assert fresnelc(+oo) == Rational(+1, 2)
    assert fresnelc(-oo) == Rational(-1, 2)

    assert fresnelc(z) == fresnelc(z)
    assert fresnelc(-z) == -fresnelc(z)
    assert fresnelc(I*z) == I*fresnelc(z)
    assert fresnelc(-I*z) == -I*fresnelc(z)

    assert conjugate(fresnelc(z)) == fresnelc(conjugate(z))

    assert fresnelc(z).diff(z) == cos(pi*z**2/2)
    pytest.raises(ArgumentIndexError, lambda: fresnels(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: fresnelc(z).fdiff(2))

    assert fresnelc(z).rewrite(erf) == (1 - I)/4 * (
        erf((1 + I)/2*sqrt(pi)*z) + I*erf((1 - I)/2*sqrt(pi)*z))

    assert fresnelc(z).rewrite(hyper) == \
        z * hyper([Rational(1, 4)], [Rational(1, 2), Rational(5, 4)], -pi**2*z**4/16)

    assert fresnelc(x).taylor_term(-1, z) == 0
    assert fresnelc(x).taylor_term(1, z, *(z,)) == -pi**2*z**5/40
    assert fresnelc(x).taylor_term(1, z) == -pi**2*z**5/40

    assert fresnelc(z).series(z, n=15) == \
        z - pi**2*z**5/40 + pi**4*z**9/3456 - pi**6*z**13/599040 + O(z**15)

    assert fresnelc(y/z).limit(z, 0) == fresnelc(oo*sign(y))

    # issue sympy/sympy#6510
    assert fresnels(z).series(z, oo) == \
        (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + \
        (3/(pi**3*z**5) - 1/(pi*z) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + Rational(1, 2)
    assert fresnelc(z).series(z, oo) == \
        (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + \
        (-3/(pi**3*z**5) + 1/(pi*z) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + Rational(1, 2)
    assert fresnels(1/z).series(z) == \
        (-z**3/pi**2 + O(z**6))*sin(pi/(2*z**2)) + (-z/pi + 3*z**5/pi**3 +
                                                    O(z**6))*cos(pi/(2*z**2)) + Rational(1, 2)
    assert fresnelc(1/z).series(z) == \
        (-z**3/pi**2 + O(z**6))*cos(pi/(2*z**2)) + (z/pi - 3*z**5/pi**3 +
                                                    O(z**6))*sin(pi/(2*z**2)) + Rational(1, 2)

    assert fresnelc(w).is_extended_real is True

    assert fresnelc(z).as_real_imag() == \
        ((fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 +
          fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2,
          I*(fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) -
             fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) *
          re(z)*Abs(im(z))/(2*im(z)*Abs(re(z)))))

    assert fresnelc(2 + 3*I).as_real_imag() == (
        fresnelc(2 - 3*I)/2 + fresnelc(2 + 3*I)/2,
        I*(fresnelc(2 - 3*I) - fresnelc(2 + 3*I))/2
    )

    assert expand_func(integrate(fresnelc(z), z)) == \
        z*fresnelc(z) - sin(pi*z**2/2)/pi

    assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(3, 4) * \
        meijerg(((), (1,)), ((Rational(1, 4),),
                             (Rational(3, 4), 0)), -pi**2*z**4/16)/(2*root(-z, 4)*root(z**2, 4))

    verify_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z)
    verify_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(hyper), z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z)

    verify_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z)
    verify_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z)
Пример #58
0
def test_erf2():
    assert erf2(0, 0) == 0
    assert erf2(x, x) == 0
    assert erf2(nan, 0) == nan

    assert erf2(-oo,  y) == erf(y) + 1
    assert erf2( oo,  y) == erf(y) - 1
    assert erf2(  x, oo) == 1 - erf(x)
    assert erf2(  x, -oo) == -1 - erf(x)
    assert erf2(x, erf2inv(x, y)) == y

    assert erf2(-x, -y) == -erf2(x, y)
    assert erf2(-x,  y) == erf(y) + erf(x)
    assert erf2( x, -y) == -erf(y) - erf(x)
    assert erf2(x, y).rewrite('fresnels') == erf(y).rewrite(fresnels)-erf(x).rewrite(fresnels)
    assert erf2(x, y).rewrite('fresnelc') == erf(y).rewrite(fresnelc)-erf(x).rewrite(fresnelc)
    assert erf2(x, y).rewrite('hyper') == erf(y).rewrite(hyper)-erf(x).rewrite(hyper)
    assert erf2(x, y).rewrite('meijerg') == erf(y).rewrite(meijerg)-erf(x).rewrite(meijerg)
    assert erf2(x, y).rewrite('uppergamma') == erf(y).rewrite(uppergamma) - erf(x).rewrite(uppergamma)
    assert erf2(x, y).rewrite('expint') == erf(y).rewrite(expint)-erf(x).rewrite(expint)

    assert erf2(I, w).is_extended_real is False
    assert erf2(2*w, w).is_extended_real is True
    assert erf2(z, w).is_extended_real is None
    assert erf2(w, z).is_extended_real is None

    assert conjugate(erf2(x, y)) == erf2(conjugate(x), conjugate(y))

    assert erf2(x, y).rewrite('erf') == erf(y) - erf(x)
    assert erf2(x, y).rewrite('erfc') == erfc(x) - erfc(y)
    assert erf2(x, y).rewrite('erfi') == I*(erfi(I*x) - erfi(I*y))

    pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: erf2(x, y).fdiff(3))

    assert erf2(x, y).diff(x) == -2*exp(-x**2)/sqrt(pi)
    assert erf2(x, y).diff(y) == +2*exp(-y**2)/sqrt(pi)
Пример #59
0
def test_heurisch_special():
    assert heurisch(erf(x), x) == x*erf(x) + exp(-x**2)/sqrt(pi)
    assert heurisch(exp(-x**2)*erf(x), x) == sqrt(pi)*erf(x)**2 / 4
Пример #60
0
def test_meijerint():
    s, t, mu = symbols('s t mu', extended_real=True)
    assert integrate(meijerg([], [], [0], [], s*t)
                     * meijerg([], [], [mu/2], [-mu/2], t**2/4),
                     (t, 0, oo)).is_Piecewise
    s = symbols('s', positive=True)
    assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo)) == \
        gamma(s + 1)
    assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo),
                     meijerg=True) == gamma(s + 1)
    assert isinstance(integrate(x**s*meijerg([[], []], [[0], []], x),
                                (x, 0, oo), meijerg=False),
                      Integral)

    assert meijerint_indefinite(exp(x), x) == exp(x)

    # TODO what simplifications should be done automatically?
    # This tests "extra case" for antecedents_1.
    a, b = symbols('a b', positive=True)
    assert simplify(meijerint_definite(x**a, x, 0, b)[0]) == \
        b**(a + 1)/(a + 1)

    # This tests various conditions and expansions:
    meijerint_definite((x + 1)**3*exp(-x), x, 0, oo) == (16, True)

    # Again, how about simplifications?
    sigma, mu = symbols('sigma mu', positive=True)
    i, c = meijerint_definite(exp(-((x - mu)/(2*sigma))**2), x, 0, oo)
    assert simplify(i) == sqrt(pi)*sigma*(erf(mu/(2*sigma)) + 1)
    assert c

    i, _ = meijerint_definite(exp(-mu*x)*exp(sigma*x), x, 0, oo)
    # TODO it would be nice to test the condition
    assert simplify(i) == 1/(mu - sigma)

    # Test substitutions to change limits
    assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True)
    # Note: causes a NaN in _check_antecedents
    assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1
    assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == \
        1 - exp(-exp(I*arg(x))*abs(x))

    # Test -oo to oo
    assert meijerint_definite(exp(-x**2), x, -oo, oo) == (sqrt(pi), True)
    assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True)
    assert meijerint_definite(exp(-(2*x - 3)**2), x, -oo, oo) == \
        (sqrt(pi)/2, True)
    assert meijerint_definite(exp(-abs(2*x - 3)), x, -oo, oo) == (1, True)
    assert meijerint_definite(exp(-((x - mu)/sigma)**2/2)/sqrt(2*pi*sigma**2),
                              x, -oo, oo) == (1, True)

    # Test one of the extra conditions for 2 g-functinos
    assert meijerint_definite(exp(-x)*sin(x), x, 0, oo) == (Rational(1, 2), True)

    # Test a bug
    def res(n):
        return (1/(1 + x**2)).diff(x, n).subs({x: 1})*(-1)**n
    for n in range(6):
        assert integrate(exp(-x)*sin(x)*x**n, (x, 0, oo), meijerg=True) == \
            res(n)

    # This used to test trigexpand... now it is done by linear substitution
    assert simplify(integrate(exp(-x)*sin(x + a), (x, 0, oo), meijerg=True)
                    ) == sqrt(2)*sin(a + pi/4)/2

    # Test the condition 14 from prudnikov.
    # (This is besselj*besselj in disguise, to stop the product from being
    #  recognised in the tables.)
    a, b, s = symbols('a b s')
    assert meijerint_definite(meijerg([], [], [a/2], [-a/2], x/4)
                              * meijerg([], [], [b/2], [-b/2], x/4)*x**(s - 1), x, 0, oo) == \
        (4*2**(2*s - 2)*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
         / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
            * gamma(a/2 + b/2 - s + 1)),
            And(0 < -2*re(4*s) + 8, 0 < re(a/2 + b/2 + s), re(2*s) < 1))

    # test a bug
    assert integrate(sin(x**a)*sin(x**b), (x, 0, oo), meijerg=True) == \
        Integral(sin(x**a)*sin(x**b), (x, 0, oo))

    # test better hyperexpand
    assert integrate(exp(-x**2)*log(x), (x, 0, oo), meijerg=True) == \
        (sqrt(pi)*polygamma(0, Rational(1, 2))/4).expand()

    # Test hyperexpand bug.
    n = symbols('n', integer=True)
    assert simplify(integrate(exp(-x)*x**n, x, meijerg=True)) == \
        lowergamma(n + 1, x)

    # Test a bug with argument 1/x
    alpha = symbols('alpha', positive=True)
    assert meijerint_definite((2 - x)**alpha*sin(alpha/x), x, 0, 2) == \
        (sqrt(pi)*alpha*gamma(alpha + 1)*meijerg(((), (alpha/2 + Rational(1, 2),
                                                       alpha/2 + 1)), ((0, 0, Rational(1, 2)), (-Rational(1, 2),)), alpha**2/16)/4, True)

    # test a bug related to 3016
    a, s = symbols('a s', positive=True)
    assert simplify(integrate(x**s*exp(-a*x**2), (x, -oo, oo))) == \
        a**(-s/2 - Rational(1, 2))*((-1)**s + 1)*gamma(s/2 + Rational(1, 2))/2