def test_gamma(): assert gamma(nan) == nan assert gamma(oo) == oo assert gamma(-100) == zoo assert gamma(0) == zoo assert gamma(1) == 1 assert gamma(2) == 1 assert gamma(3) == 2 assert gamma(102) == factorial(101) assert gamma(Rational(1, 2)) == sqrt(pi) assert gamma(Rational(3, 2)) == Rational(1, 2)*sqrt(pi) assert gamma(Rational(5, 2)) == Rational(3, 4)*sqrt(pi) assert gamma(Rational(7, 2)) == Rational(15, 8)*sqrt(pi) assert gamma(Rational(-1, 2)) == -2*sqrt(pi) assert gamma(Rational(-3, 2)) == Rational(4, 3)*sqrt(pi) assert gamma(Rational(-5, 2)) == -Rational(8, 15)*sqrt(pi) assert gamma(Rational(-15, 2)) == Rational(256, 2027025)*sqrt(pi) assert gamma(Rational( -11, 8)).expand(func=True) == Rational(64, 33)*gamma(Rational(5, 8)) assert gamma(Rational( -10, 3)).expand(func=True) == Rational(81, 280)*gamma(Rational(2, 3)) assert gamma(Rational( 14, 3)).expand(func=True) == Rational(880, 81)*gamma(Rational(2, 3)) assert gamma(Rational( 17, 7)).expand(func=True) == Rational(30, 49)*gamma(Rational(3, 7)) assert gamma(Rational( 19, 8)).expand(func=True) == Rational(33, 64)*gamma(Rational(3, 8)) assert gamma(x).diff(x) == gamma(x)*polygamma(0, x) pytest.raises(ArgumentIndexError, lambda: gamma(x).fdiff(2)) assert gamma(x - 1).expand(func=True) == gamma(x)/(x - 1) assert gamma(x + 2).expand(func=True, mul=False) == x*(x + 1)*gamma(x) assert conjugate(gamma(x)) == gamma(conjugate(x)) assert expand_func(gamma(x + Rational(3, 2))) == \ (x + Rational(1, 2))*gamma(x + Rational(1, 2)) assert expand_func(gamma(x - Rational(1, 2))) == \ gamma(Rational(1, 2) + x)/(x - Rational(1, 2)) # Test a bug: assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4)) assert gamma(3*exp_polar(I*pi)/4).is_nonnegative is False assert gamma(3*exp_polar(I*pi)/4).is_nonpositive is True # Issue sympy/sympy#8526 k = Symbol('k', integer=True, nonnegative=True) assert isinstance(gamma(k), gamma) assert gamma(-k) == zoo
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic( n, 3).rewrite(polygamma) == polygamma(2, n + 1) / 2 - polygamma(2, 1) / 2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m * ( polygamma(m - 1, 1) - polygamma(m - 1, n + 1)) / factorial(m - 1) assert expand_func( harmonic(n + 4) ) == harmonic(n) + 1 / (n + 4) + 1 / (n + 3) + 1 / (n + 2) + 1 / (n + 1) assert expand_func(harmonic( n - 4)) == harmonic(n) - 1 / (n - 1) - 1 / (n - 2) - 1 / (n - 3) - 1 / n assert harmonic(n, m).rewrite("tractable") == harmonic( n, m).rewrite(polygamma).rewrite(gamma).rewrite("tractable") assert expand_func(harmonic(n, 2)).func is harmonic
def test_RootOf_expand_func2(): r0 = RootOf(x**3 + I*x + 2, 0) assert expand_func(r0) == RootOf(x**6 + 4*x**3 + x**2 + 4, 1) r1 = RootOf(x**3 + I*x + 2, 1) assert expand_func(r1) == RootOf(x**6 + 4*x**3 + x**2 + 4, 3) r2 = RootOf(x**4 + sqrt(2)*x**3 - I*x + 1, 0) assert expand_func(r2) == RootOf(x**16 - 4*x**14 + 8*x**12 - 6*x**10 + 10*x**8 + 5*x**4 + 2*x**2 + 1, 1) r3 = RootOf(x**3 - I*sqrt(2)*x + 5, 1) assert expand_func(r3) == RootOf(x**6 + 10*x**3 + 2*x**2 + 25, 2)
def test_airybi(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybi(z), airybi) assert airybi(0) == 3**Rational(5, 6) / (3 * gamma(Rational(2, 3))) assert airybi(oo) == oo assert airybi(-oo) == 0 assert diff(airybi(z), z) == airybiprime(z) assert series(airybi(z), z, 0, 3) == (cbrt(3) * gamma(Rational(1, 3)) / (2 * pi) + 3**Rational(2, 3) * z * gamma(Rational(2, 3)) / (2 * pi) + O(z**3)) l = Limit( airybi(I / x) / (exp(Rational(2, 3) * (I / x)**Rational(3, 2)) * sqrt(pi * sqrt(I / x))), x, 0) assert l.doit() == l assert airybi(z).rewrite(hyper) == (root(3, 6) * z * hyper( (), (Rational(4, 3), ), z**3 / 9) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * hyper( (), (Rational(2, 3), ), z**3 / 9) / (3 * gamma(Rational(2, 3)))) assert isinstance(airybi(z).rewrite(besselj), airybi) assert (airybi(t).rewrite(besselj) == sqrt(3) * sqrt(-t) * (besselj(-1 / 3, 2 * (-t)**Rational(3, 2) / 3) - besselj(Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airybi(z).rewrite(besseli) == ( sqrt(3) * (z * besseli(Rational(1, 3), 2 * z**Rational(3, 2) / 3) / cbrt(z**Rational(3, 2)) + cbrt(z**Rational(3, 2)) * besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3)) / 3) assert airybi(p).rewrite(besseli) == ( sqrt(3) * sqrt(p) * (besseli(-Rational(1, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(1, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert airybi(p).rewrite(besselj) == airybi(p) assert expand_func(airybi( 2 * cbrt(3 * z**5))) == (sqrt(3) * (1 - cbrt(z**5) / z**Rational(5, 3)) * airyai(2 * cbrt(3) * z**Rational(5, 3)) / 2 + (1 + cbrt(z**5) / z**Rational(5, 3)) * airybi(2 * cbrt(3) * z**Rational(5, 3)) / 2) assert expand_func(airybi(x * y)) == airybi(x * y) assert expand_func(airybi(log(x))) == airybi(log(x)) assert expand_func(airybi(2 * root(3 * z**5, 5))) == airybi( 2 * root(3 * z**5, 5)) assert airybi(x).taylor_term(-1, x) == 0
def test_RootOf_expand_func2(): r0 = RootOf(x**3 + I * x + 2, 0) assert expand_func(r0) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 1) r1 = RootOf(x**3 + I * x + 2, 1) assert expand_func(r1) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 3) r2 = RootOf(x**4 + sqrt(2) * x**3 - I * x + 1, 0) assert expand_func(r2) == RootOf( x**16 - 4 * x**14 + 8 * x**12 - 6 * x**10 + 10 * x**8 + 5 * x**4 + 2 * x**2 + 1, 1) r3 = RootOf(x**3 - I * sqrt(2) * x + 5, 1) assert expand_func(r3) == RootOf(x**6 + 10 * x**3 + 2 * x**2 + 25, 2)
def test_beta(): x, y = Symbol('x'), Symbol('y') assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x)*gamma(y)/gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta(x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y)*(digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y)*(digamma(y) - digamma(x + y))
def test_airyai(): z = Symbol('z', extended_real=False) r = Symbol('r', extended_real=True) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3))) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert series(airyai(z), z, 0, 3) == ( 3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0) assert l.doit() == l # cover _airyais._eval_aseries assert airyai(z).rewrite(hyper) == ( -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) + cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyai(z).rewrite(besseli) == ( -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyai(p).rewrite(besseli) == ( sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) - besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert expand_func(airyai(2*cbrt(3*z**5))) == ( -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 + (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyai(x*y)) == airyai(x*y) assert expand_func(airyai(log(x))) == airyai(log(x)) assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5)) assert (airyai(r).as_real_imag() == airyai(r).as_real_imag(deep=False) == (airyai(r), 0)) assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False) assert (airyai(x).as_real_imag() == (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - airyai(re(x) + I*re(x)*abs(im(x))/Abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert airyai(x).taylor_term(-1, x) == 0
def test_airyai(): z = Symbol('z', extended_real=False) r = Symbol('r', extended_real=True) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3))) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert airyai(z).series(z, 0, 3) == ( 3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0) assert l.doit() == l # cover _airyais._eval_aseries assert airyai(z).rewrite(hyper) == ( -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) + cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyai(z).rewrite(besseli) == ( -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyai(p).rewrite(besseli) == ( sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) - besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert expand_func(airyai(2*cbrt(3*z**5))) == ( -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 + (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyai(x*y)) == airyai(x*y) assert expand_func(airyai(log(x))) == airyai(log(x)) assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5)) assert (airyai(r).as_real_imag() == airyai(r).as_real_imag(deep=False) == (airyai(r), 0)) assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False) assert (airyai(x).as_real_imag() == (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert airyai(x).taylor_term(-1, x) == 0
def test_RootOf_expand_func(): r0 = RootOf(x**3 + x + 1, 0) assert expand_func(r0) == r0 r0 = RootOf(x**3 + I * x + 2, 0, extension=True) assert expand_func(r0) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 1) r1 = RootOf(x**3 + I * x + 2, 1, extension=True) assert expand_func(r1) == RootOf(x**6 + 4 * x**3 + x**2 + 4, 3) e = RootOf(x**4 + sqrt(2) * x**3 - I * x + 1, 0, extension=True) assert expand_func(e) == RootOf( x**16 - 4 * x**14 + 8 * x**12 - 6 * x**10 + 10 * x**8 + 5 * x**4 + 2 * x**2 + 1, 1)
def test_beta(): assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x) * gamma(y) / gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta( x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y) * (digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y) * (digamma(y) - digamma(x + y)) pytest.raises(ArgumentIndexError, lambda: beta(x, y).fdiff(3)) assert conjugate(beta(x, y)) == beta(conjugate(x), conjugate(y))
def test_beta(): assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x)*gamma(y)/gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta(x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y)*(digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y)*(digamma(y) - digamma(x + y)) pytest.raises(ArgumentIndexError, lambda: beta(x, y).fdiff(3)) assert conjugate(beta(x, y)) == beta(conjugate(x), conjugate(y))
def test_airybiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == root(3, 6) / gamma(Rational(1, 3)) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z * airybi(z) assert series(airybiprime(z), z, 0, 3) == (root(3, 6) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * z**2 / (6 * gamma(Rational(2, 3))) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**Rational(5, 6) * z**2 * hyper((), (Rational(5, 3), ), z**3 / 9) / (6 * gamma(Rational(2, 3))) + root(3, 6) * hyper( (), (Rational(1, 3), ), z**3 / 9) / gamma(Rational(1, 3))) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert (airybiprime(t).rewrite(besselj) == -sqrt(3) * t * (besselj(-Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3) + besselj(Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3) * (z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) / (z**Rational(3, 2))**Rational(2, 3) + (z**Rational(3, 2))**Rational(2, 3) * besseli(-Rational(2, 3), 2 * z**Rational(3, 2) / 3)) / 3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3) * p * (besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert airybiprime(p).rewrite(besselj) == airybiprime(p) assert expand_func(airybiprime( 2 * cbrt(3 * z**5))) == (sqrt(3) * (z**Rational(5, 3) / cbrt(z**5) - 1) * airyaiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2 + (z**Rational(5, 3) / cbrt(z**5) + 1) * airybiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2) assert expand_func(airybiprime(x * y)) == airybiprime(x * y) assert expand_func(airybiprime(log(x))) == airybiprime(log(x)) assert expand_func(airybiprime(2 * root(3 * z**5, 5))) == airybiprime( 2 * root(3 * z**5, 5)) assert airybiprime(-2).evalf(50) == Float( '0.27879516692116952268509756941098324140300059345163131', dps=50)
def test_airyaiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyaiprime(z), airyaiprime) assert airyaiprime(0) == -3**Rational(2, 3) / (3 * gamma(Rational(1, 3))) assert airyaiprime(oo) == 0 assert diff(airyaiprime(z), z) == z * airyai(z) assert series(airyaiprime(z), z, 0, 3) == (-3**Rational(2, 3) / (3 * gamma(Rational(1, 3))) + cbrt(3) * z**2 / (6 * gamma(Rational(2, 3))) + O(z**3)) assert airyaiprime(z).rewrite(hyper) == ( cbrt(3) * z**2 * hyper((), (Rational(5, 3), ), z**3 / 9) / (6 * gamma(Rational(2, 3))) - 3**Rational(2, 3) * hyper( (), (Rational(1, 3), ), z**3 / 9) / (3 * gamma(Rational(1, 3)))) assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime) assert (airyaiprime(t).rewrite(besselj) == t * (besselj(-Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3) - besselj(Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airyaiprime(z).rewrite(besseli) == ( z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) / (3 * (z**Rational(3, 2))**Rational(2, 3)) - (z**Rational(3, 2))**Rational(2, 3) * besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3) / 3) assert airyaiprime(p).rewrite(besseli) == ( p * (-besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert airyaiprime(p).rewrite(besselj) == airyaiprime(p) assert expand_func(airyaiprime( 2 * cbrt(3 * z**5))) == (sqrt(3) * (z**Rational(5, 3) / cbrt(z**5) - 1) * airybiprime(2 * cbrt(3) * z**Rational(5, 3)) / 6 + (z**Rational(5, 3) / cbrt(z**5) + 1) * airyaiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2) assert expand_func(airyaiprime(x * y)) == airyaiprime(x * y) assert expand_func(airyaiprime(log(x))) == airyaiprime(log(x)) assert expand_func(airyaiprime(2 * root(3 * z**5, 5))) == airyaiprime( 2 * root(3 * z**5, 5)) assert airyaiprime(-2).evalf(50) == Float( '0.61825902074169104140626429133247528291577794512414753', dps=50)
def test_binomial(): n = Symbol('n', integer=True) nz = Symbol('nz', integer=True, nonzero=True) k = Symbol('k', integer=True) kp = Symbol('kp', integer=True, positive=True) u = Symbol('u', negative=True) p = Symbol('p', positive=True) z = Symbol('z', zero=True) assert binomial(0, 0) == 1 assert binomial(1, 1) == 1 assert binomial(10, 10) == 1 assert binomial(n, z) == 1 assert binomial(1, 2) == 0 assert binomial(1, -1) == 0 assert binomial(-1, 1) == -1 assert binomial(-1, -1) == 1 assert binomial(Rational(1, 2), Rational(1, 2)) == 1 assert binomial(-10, 1) == -10 assert binomial(-10, 7) == -11440 assert binomial(n, -1).func == binomial assert binomial(kp, -1) == 0 assert binomial(nz, 0) == 1 assert expand_func(binomial(n, 1)) == n assert expand_func(binomial(n, 2)) == n*(n - 1)/2 assert expand_func(binomial(n, n - 2)) == n*(n - 1)/2 assert expand_func(binomial(n, n - 1)) == n assert binomial(n, 3).func == binomial assert binomial(n, 3).expand(func=True) == n**3/6 - n**2/2 + n/3 assert expand_func(binomial(n, 3)) == n*(n - 2)*(n - 1)/6 assert expand_func(binomial(1, 2, evaluate=False)) == 0 assert expand_func(binomial(n, 0, evaluate=False)) == 1 assert isinstance(expand_func(binomial(n, -1, evaluate=False)), binomial) assert isinstance(expand_func(binomial(n, k)), binomial) assert binomial(n, n) == 1 assert binomial(n, n + 1).func == binomial # e.g. (-1, 0) == 1 assert binomial(kp, kp + 1) == 0 assert binomial(n, u).func == binomial assert binomial(kp, u) == 0 assert binomial(n, p).func == binomial assert binomial(n, k).func == binomial assert binomial(n, n + p).func == binomial assert binomial(kp, kp + p) == 0 assert expand_func(binomial(n, n - 3)) == n*(n - 2)*(n - 1)/6 assert binomial(n, k).is_integer assert binomial(p, k).is_integer is None
def test_binomial(): n = Symbol('n', integer=True) nz = Symbol('nz', integer=True, nonzero=True) k = Symbol('k', integer=True) kp = Symbol('kp', integer=True, positive=True) u = Symbol('u', negative=True) p = Symbol('p', positive=True) z = Symbol('z', zero=True) assert binomial(0, 0) == 1 assert binomial(1, 1) == 1 assert binomial(10, 10) == 1 assert binomial(n, z) == 1 assert binomial(1, 2) == 0 assert binomial(1, -1) == 0 assert binomial(-1, 1) == -1 assert binomial(-1, -1) == 1 assert binomial(Rational(1, 2), Rational(1, 2)) == 1 assert binomial(-10, 1) == -10 assert binomial(-10, 7) == -11440 assert binomial(n, -1).func == binomial assert binomial(kp, -1) == 0 assert binomial(nz, 0) == 1 assert expand_func(binomial(n, 1)) == n assert expand_func(binomial(n, 2)) == n * (n - 1) / 2 assert expand_func(binomial(n, n - 2)) == n * (n - 1) / 2 assert expand_func(binomial(n, n - 1)) == n assert binomial(n, 3).func == binomial assert binomial(n, 3).expand(func=True) == n**3 / 6 - n**2 / 2 + n / 3 assert expand_func(binomial(n, 3)) == n * (n - 2) * (n - 1) / 6 assert expand_func(binomial(1, 2, evaluate=False)) == 0 assert expand_func(binomial(n, 0, evaluate=False)) == 1 assert isinstance(expand_func(binomial(n, -1, evaluate=False)), binomial) assert isinstance(expand_func(binomial(n, k)), binomial) assert binomial(n, n) == 1 assert binomial(n, n + 1).func == binomial # e.g. (-1, 0) == 1 assert binomial(kp, kp + 1) == 0 assert binomial(n, u).func == binomial assert binomial(kp, u) == 0 assert binomial(n, p).func == binomial assert binomial(n, k).func == binomial assert binomial(n, n + p).func == binomial assert binomial(kp, kp + p) == 0 assert expand_func(binomial(n, n - 3)) == n * (n - 2) * (n - 1) / 6 assert binomial(n, k).is_integer assert binomial(p, k).is_integer is None
def test_harmonic_rewrite_polygamma(): n = Symbol('n') m = Symbol('m') assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic( n, 3).rewrite(polygamma) == polygamma(2, n + 1) / 2 - polygamma(2, 1) / 2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m * ( polygamma(m - 1, 1) - polygamma(m - 1, n + 1)) / factorial(m - 1) assert expand_func( harmonic(n + 4) ) == harmonic(n) + 1 / (n + 4) + 1 / (n + 3) + 1 / (n + 2) + 1 / (n + 1) assert expand_func(harmonic( n - 4)) == harmonic(n) - 1 / (n - 1) - 1 / (n - 2) - 1 / (n - 3) - 1 / n assert harmonic(n, m).rewrite('tractable') == harmonic( n, m).rewrite(polygamma).rewrite(gamma).rewrite('tractable') assert isinstance(expand_func(harmonic(n, 2)), harmonic) assert expand_func(harmonic(n + Rational(1, 2))) == expand_func( harmonic(n + Rational(1, 2))) assert expand_func(harmonic(Rational(-1, 2))) == harmonic(Rational(-1, 2)) assert expand_func(harmonic(x)) == harmonic(x)
def test_airybi(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybi(z), airybi) assert airybi(0) == 3**Rational(5, 6)/(3*gamma(Rational(2, 3))) assert airybi(oo) == oo assert airybi(-oo) == 0 assert diff(airybi(z), z) == airybiprime(z) assert series(airybi(z), z, 0, 3) == ( cbrt(3)*gamma(Rational(1, 3))/(2*pi) + 3**Rational(2, 3)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airybi(I/x)/(exp(Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))), x, 0) assert l.doit() == l assert airybi(z).rewrite(hyper) == ( root(3, 6)*z*hyper((), (Rational(4, 3),), z**3/9)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airybi(z).rewrite(besselj), airybi) assert (airybi(t).rewrite(besselj) == sqrt(3)*sqrt(-t)*(besselj(-1/3, 2*(-t)**Rational(3, 2)/3) - besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airybi(z).rewrite(besseli) == ( sqrt(3)*(z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/cbrt(z**Rational(3, 2)) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3))/3) assert airybi(p).rewrite(besseli) == ( sqrt(3)*sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert airybi(p).rewrite(besselj) == airybi(p) assert expand_func(airybi(2*cbrt(3*z**5))) == ( sqrt(3)*(1 - cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2 + (1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airybi(x*y)) == airybi(x*y) assert expand_func(airybi(log(x))) == airybi(log(x)) assert expand_func(airybi(2*root(3*z**5, 5))) == airybi(2*root(3*z**5, 5)) assert airybi(x).taylor_term(-1, x) == 0
def test_beta(): a, b = symbols('alpha beta', positive=True) B = Beta('x', a, b) assert pspace(B).domain.set == Interval(0, 1) dens = density(B) x = Symbol('x') assert dens(x) == x**(a - 1)*(1 - x)**(b - 1) / beta(a, b) # This is too slow # assert E(B) == a / (a + b) # assert variance(B) == (a*b) / ((a+b)**2 * (a+b+1)) # Full symbolic solution is too much, test with numeric version a, b = Integer(1), Integer(2) B = Beta('x', a, b) assert expand_func(E(B)) == a/(a + b) assert expand_func(variance(B)) == (a*b)/(a + b)**2/(a + b + 1)
def test_beta(): a, b = symbols('alpha beta', positive=True) B = Beta('x', a, b) assert pspace(B).domain.set == Interval(0, 1) dens = density(B) x = Symbol('x') assert dens(x) == x**(a - 1)*(1 - x)**(b - 1) / beta(a, b) # This is too slow # assert E(B) == a / (a + b) # assert variance(B) == (a*b) / ((a+b)**2 * (a+b+1)) # Full symbolic solution is too much, test with numeric version a, b = Integer(1), Integer(2) B = Beta('x', a, b) assert expand_func(E(B)) == a/(a + b) assert expand_func(variance(B)) == (a*b)/(a + b)**2/(a + b + 1)
def myexpand(func, target): expanded = expand_func(func) if target is not None: return expanded == target if expanded == func: # it didn't expand return False # check to see that the expanded and original evaluate to the same value subs = {} for a in func.free_symbols: subs[a] = randcplx() return abs(func.subs(subs).evalf() - expanded.replace(exp_polar, exp).subs(subs).evalf()) < 1e-10
def test_jn(): assert mjn(0, z) == sin(z)/z assert mjn(1, z) == sin(z)/z**2 - cos(z)/z assert mjn(2, z) == (3/z**3 - 1/z)*sin(z) - (3/z**2) * cos(z) assert mjn(3, z) == (15/z**4 - 6/z**2)*sin(z) + (1/z - 15/z**3)*cos(z) assert mjn(4, z) == (1/z + 105/z**5 - 45/z**3)*sin(z) + \ (-105/z**4 + 10/z**2)*cos(z) assert mjn(5, z) == (945/z**6 - 420/z**4 + 15/z**2)*sin(z) + \ (-1/z - 945/z**5 + 105/z**3)*cos(z) assert mjn(6, z) == (-1/z + 10395/z**7 - 4725/z**5 + 210/z**3)*sin(z) + \ (-10395/z**6 + 1260/z**4 - 21/z**2)*cos(z) assert expand_func(jn(n, z)) == jn(n, z)
def test_jn(): assert mjn(0, z) == sin(z)/z assert mjn(1, z) == sin(z)/z**2 - cos(z)/z assert mjn(2, z) == (3/z**3 - 1/z)*sin(z) - (3/z**2) * cos(z) assert mjn(3, z) == (15/z**4 - 6/z**2)*sin(z) + (1/z - 15/z**3)*cos(z) assert mjn(4, z) == (1/z + 105/z**5 - 45/z**3)*sin(z) + \ (-105/z**4 + 10/z**2)*cos(z) assert mjn(5, z) == (945/z**6 - 420/z**4 + 15/z**2)*sin(z) + \ (-1/z - 945/z**5 + 105/z**3)*cos(z) assert mjn(6, z) == (-1/z + 10395/z**7 - 4725/z**5 + 210/z**3)*sin(z) + \ (-10395/z**6 + 1260/z**4 - 21/z**2)*cos(z) assert expand_func(jn(n, z)) == jn(n, z)
def myexpand(func, target): expanded = expand_func(func) if target is not None: return expanded == target if expanded == func: # it didn't expand return False # check to see that the expanded and original evaluate to the same value subs = {} for a in func.free_symbols: subs[a] = randcplx() return abs(func.subs(subs).n() - expanded.replace(exp_polar, exp).subs(subs).n()) < 1e-10
def test_airybiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == root(3, 6)/gamma(Rational(1, 3)) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z*airybi(z) assert series(airybiprime(z), z, 0, 3) == ( root(3, 6)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*z**2/(6*gamma(Rational(2, 3))) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**Rational(5, 6)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) + root(3, 6)*hyper((), (Rational(1, 3),), z**3/9)/gamma(Rational(1, 3))) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert (airybiprime(t).rewrite(besselj) == -sqrt(3)*t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3)*(z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(z**Rational(3, 2))**Rational(2, 3) + (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(2, 3), 2*z**Rational(3, 2)/3))/3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3)*p*(besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3) assert airybiprime(p).rewrite(besselj) == airybiprime(p) assert expand_func(airybiprime(2*cbrt(3*z**5))) == ( sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2 + (z**Rational(5, 3)/cbrt(z**5) + 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airybiprime(x*y)) == airybiprime(x*y) assert expand_func(airybiprime(log(x))) == airybiprime(log(x)) assert expand_func(airybiprime(2*root(3*z**5, 5))) == airybiprime(2*root(3*z**5, 5)) assert airybiprime(-2).evalf(50) == Float('0.27879516692116952268509756941098324140300059345163131', dps=50)
def test_airyaiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyaiprime(z), airyaiprime) assert airyaiprime(0) == -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) assert airyaiprime(oo) == 0 assert diff(airyaiprime(z), z) == z*airyai(z) assert series(airyaiprime(z), z, 0, 3) == ( -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) + cbrt(3)*z**2/(6*gamma(Rational(2, 3))) + O(z**3)) assert airyaiprime(z).rewrite(hyper) == ( cbrt(3)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) - 3**Rational(2, 3)*hyper((), (Rational(1, 3),), z**3/9)/(3*gamma(Rational(1, 3)))) assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime) assert (airyaiprime(t).rewrite(besselj) == t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) - besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyaiprime(z).rewrite(besseli) == ( z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(3*(z**Rational(3, 2))**Rational(2, 3)) - (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyaiprime(p).rewrite(besseli) == ( p*(-besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3) assert airyaiprime(p).rewrite(besselj) == airyaiprime(p) assert expand_func(airyaiprime(2*cbrt(3*z**5))) == ( sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/6 + (z**Rational(5, 3)/cbrt(z**5) + 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyaiprime(x*y)) == airyaiprime(x*y) assert expand_func(airyaiprime(log(x))) == airyaiprime(log(x)) assert expand_func(airyaiprime(2*root(3*z**5, 5))) == airyaiprime(2*root(3*z**5, 5)) assert airyaiprime(-2).evalf(50) == Float('0.61825902074169104140626429133247528291577794512414753', dps=50)
def test_airyai(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == 3**Rational(1, 3) / (3 * gamma(Rational(2, 3))) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert series(airyai(z), z, 0, 3) == (3**Rational(5, 6) * gamma(Rational(1, 3)) / (6 * pi) - 3**Rational(1, 6) * z * gamma(Rational(2, 3)) / (2 * pi) + O(z**3)) assert airyai(z).rewrite(hyper) == (-3**Rational(2, 3) * z * hyper( (), (Rational(4, 3), ), z**Integer(3) / 9) / (3 * gamma(Rational(1, 3))) + 3**Rational(1, 3) * hyper( (), (Rational(2, 3), ), z**Integer(3) / 9) / (3 * gamma(Rational(2, 3)))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t) * (besselj(-Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3) + besselj(Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airyai(z).rewrite(besseli) == ( -z * besseli(Rational(1, 3), 2 * z**Rational(3, 2) / 3) / (3 * (z**Rational(3, 2))**Rational(1, 3)) + (z**Rational(3, 2))**Rational(1, 3) * besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3) / 3) assert airyai(p).rewrite(besseli) == ( sqrt(p) * (besseli(-Rational(1, 3), 2 * p**Rational(3, 2) / 3) - besseli(Rational(1, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert expand_func(airyai(2 * (3 * z**5)**Rational(1, 3))) == ( -sqrt(3) * (-1 + (z**5)**Rational(1, 3) / z**Rational(5, 3)) * airybi(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 6 + (1 + (z**5)**Rational(1, 3) / z**Rational(5, 3)) * airyai(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 2)
def test_expand_func(): # evaluation at 1 of Gauss' hypergeometric function: a1, b1, c1 = randcplx(), randcplx(), randcplx() + 5 assert expand_func(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(-a - b + c)/(gamma(-a + c)*gamma(-b + c)) assert abs(expand_func(hyper([a1, b1], [c1], 1)) - hyper([a1, b1], [c1], 1)).evalf(strict=False) < 1e-10 # hyperexpand wrapper for hyper: assert expand_func(hyper([], [], z)) == exp(z) assert expand_func(hyper([1, 2, 3], [], z)) == hyper([1, 2, 3], [], z) assert expand_func(meijerg([[1, 1], []], [[1], [0]], z)) == log(z + 1) assert expand_func(meijerg([[1, 1], []], [[], []], z)) == \ meijerg([[1, 1], []], [[], []], z)
def test_expand_func(): # evaluation at 1 of Gauss' hypergeometric function: a1, b1, c1 = randcplx(), randcplx(), randcplx() + 5 assert expand_func(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(-a - b + c)/(gamma(-a + c)*gamma(-b + c)) assert abs(expand_func(hyper([a1, b1], [c1], 1)) - hyper([a1, b1], [c1], 1)).evalf(strict=False) < 1e-10 # hyperexpand wrapper for hyper: assert expand_func(hyper([], [], z)) == exp(z) assert expand_func(hyper([1, 2, 3], [], z)) == hyper([1, 2, 3], [], z) assert expand_func(meijerg([[1, 1], []], [[1], [0]], z)) == log(z + 1) assert expand_func(meijerg([[1, 1], []], [[], []], z)) == \ meijerg([[1, 1], []], [[], []], z)
def test_expand_func(): # evaluation at 1 of Gauss' hypergeometric function: from diofant.abc import a, b, c from diofant import gamma, expand_func a1, b1, c1 = randcplx(), randcplx(), randcplx() + 5 assert expand_func(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(-a - b + c)/(gamma(-a + c)*gamma(-b + c)) assert abs( expand_func(hyper([a1, b1], [c1], 1)).n() - hyper([a1, b1], [c1], 1).n()) < 1e-10 # hyperexpand wrapper for hyper: assert expand_func(hyper([], [], z)) == exp(z) assert expand_func(hyper([1, 2, 3], [], z)) == hyper([1, 2, 3], [], z) assert expand_func(meijerg([[1, 1], []], [[1], [0]], z)) == log(z + 1) assert expand_func(meijerg([[1, 1], []], [[], []], z)) == \ meijerg([[1, 1], []], [[], []], z)
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n, 3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma).rewrite(gamma).rewrite("tractable") assert isinstance(expand_func(harmonic(n, 2)), harmonic) assert expand_func(harmonic(n + Rational(1, 2))) == expand_func(harmonic(n + Rational(1, 2))) assert expand_func(harmonic(Rational(-1, 2))) == harmonic(Rational(-1, 2)) assert expand_func(harmonic(x)) == harmonic(x)
def test_sympyissue_4992(): # Note: psi in _check_antecedents becomes NaN. a = Symbol('a', positive=True) assert simplify(expand_func(integrate(exp(-x)*log(x)*x**a, (x, 0, oo)))) == \ (a*polygamma(0, a) + 1)*gamma(a)
def test_RootOf_expand_func1(): r0 = RootOf(x**3 + x + 1, 0) assert expand_func(r0) == r0 r1 = RootOf(x**3 - sqrt(2) * x + I, 1) assert expand_func(r1) == RootOf( x**12 - 4 * x**8 + 2 * x**6 + 4 * x**4 + 4 * x**2 + 1, 7)
def mjn(n, z): return expand_func(jn(n, z))
def myn(n, z): return expand_func(yn(n, z))
def test_yn(): assert myn(0, z) == -cos(z)/z assert myn(1, z) == -cos(z)/z**2 - sin(z)/z assert myn(2, z) == -((3/z**3 - 1/z)*cos(z) + (3/z**2)*sin(z)) assert expand_func(yn(n, z)) == yn(n, z)
def test_fresnel(): assert expand_func(integrate(sin(pi * x**2 / 2), x)) == fresnels(x) assert expand_func(integrate(cos(pi * x**2 / 2), x)) == fresnelc(x)
def test_sympyissue_3686( ): # remove this when fresnel itegrals are implemented assert expand_func(integrate(sin(x**2), x)) == \ sqrt(2)*sqrt(pi)*fresnels(sqrt(2)*x/sqrt(pi))/2
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)
def test_harmonic_rational(): ne = Integer(6) no = Integer(5) pe = Integer(8) po = Integer(9) qe = Integer(10) qo = Integer(13) Heee = harmonic(ne + pe/qe) Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8))) + Rational(13944145, 4720968)) Heeo = harmonic(ne + pe/qo) Aeeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(2422020029, 702257080)) Heoe = harmonic(ne + po/qe) Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4) + Rational(11818877030, 4286604231) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8))) Heoo = harmonic(ne + po/qo) Aeoo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(11669332571, 3628714320)) Hoee = harmonic(no + pe/qe) Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8))) + Rational(779405, 277704)) Hoeo = harmonic(no + pe/qo) Aoeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(53857323, 16331560)) Hooe = harmonic(no + po/qe) Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4) + Rational(486853480, 186374097) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8))) Hooo = harmonic(no + po/qo) Aooo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(383693479, 125128080)) H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo] A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo] for h, a in zip(H, A): e = expand_func(h).doit() assert cancel(e/a) == 1 assert h.evalf() == a.evalf()
def test_yn(): assert myn(0, z) == -cos(z) / z assert myn(1, z) == -cos(z) / z**2 - sin(z) / z assert myn(2, z) == -((3 / z**3 - 1 / z) * cos(z) + (3 / z**2) * sin(z)) assert expand_func(yn(n, z)) == yn(n, z)
def myn(n, z): return expand_func(yn(n, z))
def mjn(n, z): return expand_func(jn(n, z))
def test_rsolve(): f = y(n + 2) - y(n + 1) - y(n) h = sqrt(5)*(Rational(1, 2) + sqrt(5)/2)**n \ - sqrt(5)*(Rational(1, 2) - sqrt(5)/2)**n assert rsolve(f, y(n)) in [ C0*(Rational(1, 2) - sqrt(5)/2)**n + C1*(Rational(1, 2) + sqrt(5)/2)**n, C1*(Rational(1, 2) - sqrt(5)/2)**n + C0*(Rational(1, 2) + sqrt(5)/2)**n, ] assert rsolve(f, y(n), [0, 5]) == h assert rsolve(f, y(n), {0: 0, 1: 5}) == h assert rsolve(f, y(n), {y(0): 0, y(1): 5}) == h assert rsolve(y(n) - y(n - 1) - y(n - 2), y(n), [0, 5]) == h assert rsolve(Eq(y(n), y(n - 1) + y(n - 2)), y(n), [0, 5]) == h assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = (n - 1)*y(n + 2) - (n**2 + 3*n - 2)*y(n + 1) + 2*n*(n + 1)*y(n) g = C1*factorial(n) + C0*2**n h = -3*factorial(n) + 3*2**n assert rsolve(f, y(n)) == g assert rsolve(f, y(n), []) == g assert rsolve(f, y(n), {}) == g assert rsolve(f, y(n), [0, 3]) == h assert rsolve(f, y(n), {0: 0, 1: 3}) == h assert rsolve(f, y(n), {y(0): 0, y(1): 3}) == h assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = y(n) - y(n - 1) - 2 assert rsolve(f, y(n), {y(0): 0}) == 2*n assert rsolve(f, y(n), {y(0): 1}) == 2*n + 1 assert rsolve(f, y(n), {y(0): 0, y(1): 1}) is None assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = 3*y(n - 1) - y(n) - 1 assert rsolve(f, y(n), {y(0): 0}) == -3**n/2 + Rational(1, 2) assert rsolve(f, y(n), {y(0): 1}) == 3**n/2 + Rational(1, 2) assert rsolve(f, y(n), {y(0): 2}) == 3*3**n/2 + Rational(1, 2) assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = y(n) - 1/n*y(n - 1) assert rsolve(f, y(n)) == C0/factorial(n) assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = y(n) - 1/n*y(n - 1) - 1 assert rsolve(f, y(n)) is None f = 2*y(n - 1) + (1 - n)*y(n)/n assert rsolve(f, y(n), {y(1): 1}) == 2**(n - 1)*n assert rsolve(f, y(n), {y(1): 2}) == 2**(n - 1)*n*2 assert rsolve(f, y(n), {y(1): 3}) == 2**(n - 1)*n*3 assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 f = (n - 1)*(n - 2)*y(n + 2) - (n + 1)*(n + 2)*y(n) assert rsolve(f, y(n), {y(3): 6, y(4): 24}) == n*(n - 1)*(n - 2) assert rsolve( f, y(n), {y(3): 6, y(4): -24}) == -n*(n - 1)*(n - 2)*(-1)**(n) assert f.subs({y: Lambda(k, rsolve(f, y(n)).subs({n: k}))}).simplify() == 0 assert rsolve(Eq(y(n + 1), a*y(n)), y(n), {y(1): a}).simplify() == a**n assert rsolve(y(n) - a*y(n-2), y(n), {y(1): sqrt(a)*(a + b), y(2): a*(a - b)}).simplify() == \ a**(n/2)*(-(-1)**n*b + a) f = (-16*n**2 + 32*n - 12)*y(n - 1) + (4*n**2 - 12*n + 9)*y(n) assert expand_func(rsolve(f, y(n), {y(1): binomial(2*n + 1, 3)}).rewrite(gamma)).simplify() == \ 2**(2*n)*n*(2*n - 1)*(4*n**2 - 1)/12 assert (rsolve(y(n) + a*(y(n + 1) + y(n - 1))/2, y(n)) - (C0*(-sqrt(-1 + a**(-2)) - 1/a)**n + C1*(sqrt(-1 + a**(-2)) - 1/a)**n)).simplify() == 0
def test_sympyissue_4992(): # Note: psi in _check_antecedents becomes NaN. a = Symbol('a', positive=True) assert simplify(expand_func(integrate(exp(-x)*log(x)*x**a, (x, 0, oo)))) == \ (a*polygamma(0, a) + 1)*gamma(a)
def test_expand(): 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)) assert expand_func(besselj(I, z)) == besselj(I, z) # Test simplify helper assert simplify(besselj(Rational(1, 2), z)) == sqrt(2) * sin(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
def test_expand(): 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)) assert expand_func(besselj(I, z)) == besselj(I, z) # Test simplify helper assert simplify(besselj(Rational(1, 2), z)) == sqrt(2)*sin(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
def test_loggamma(): pytest.raises(TypeError, lambda: loggamma(2, 3)) pytest.raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2)) assert loggamma(-1) == oo assert loggamma(-2) == oo assert loggamma(0) == oo assert loggamma(1) == 0 assert loggamma(2) == 0 assert loggamma(3) == log(2) assert loggamma(4) == log(6) n = Symbol('n', integer=True, positive=True) assert loggamma(n) == log(gamma(n)) assert loggamma(-n) == oo assert loggamma(n/2) == log(2**(-n + 1)*sqrt(pi)*gamma(n)/gamma(n/2 + Rational(1, 2))) assert loggamma(oo) == oo assert loggamma(-oo) == zoo assert loggamma(I*oo) == zoo assert loggamma(-I*oo) == zoo assert loggamma(zoo) == zoo assert loggamma(nan) == nan L = loggamma(Rational(16, 3)) E = -5*log(3) + loggamma(Rational(1, 3)) + log(4) + log(7) + log(10) + log(13) assert expand_func(L).doit() == E assert L.evalf() == E.evalf() L = loggamma(Rational(19, 4)) E = -4*log(4) + loggamma(Rational(3, 4)) + log(3) + log(7) + log(11) + log(15) assert expand_func(L).doit() == E assert L.evalf() == E.evalf() L = loggamma(Rational(23, 7)) E = -3*log(7) + log(2) + loggamma(Rational(2, 7)) + log(9) + log(16) assert expand_func(L).doit() == E assert L.evalf() == E.evalf() L = loggamma(Rational(19, 4) - 7) E = -log(9) - log(5) + loggamma(Rational(3, 4)) + 3*log(4) - 3*I*pi assert expand_func(L).doit() == E assert L.evalf() == E.evalf() L = loggamma(Rational(23, 7) - 6) E = -log(19) - log(12) - log(5) + loggamma(Rational(2, 7)) + 3*log(7) - 3*I*pi assert expand_func(L).doit() == E assert L.evalf() == E.evalf() assert expand_func(loggamma(x)) == loggamma(x) assert expand_func(loggamma(Rational(1, 3))) == loggamma(Rational(1, 3)) assert loggamma(x).diff(x) == polygamma(0, x) s1 = loggamma(1/(x + sin(x)) + cos(x)).series(x, n=4) s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \ log(x)*x**2/2 assert (s1 - s2).expand(force=True).removeO() == 0 s1 = loggamma(1/x).series(x) s2 = (1/x - Rational(1, 2))*log(1/x) - 1/x + log(2*pi)/2 + \ x/12 - x**3/360 + x**5/1260 + O(x**7) assert ((s1 - s2).expand(force=True)).removeO() == 0 assert loggamma(x).rewrite('intractable') == log(gamma(x)) s1 = loggamma(x).series(x) assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \ pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6) assert s1 == loggamma(x).rewrite('intractable').series(x) assert conjugate(loggamma(x)) == conjugate(loggamma(x), evaluate=False) p = Symbol('p', positive=True) c = Symbol('c', complex=True, extended_real=False) assert conjugate(loggamma(p)) == loggamma(p) assert conjugate(loggamma(c)) == loggamma(conjugate(c)) assert conjugate(loggamma(0)) == conjugate(loggamma(0)) assert conjugate(loggamma(1)) == loggamma(conjugate(1)) assert conjugate(loggamma(-oo)) == conjugate(loggamma(-oo)) assert loggamma(x).is_extended_real is None y = Symbol('y', nonnegative=True) assert loggamma(y).is_extended_real assert loggamma(w).is_extended_real is None def tN(N, M): assert loggamma(1/x)._eval_nseries(x, n=N).getn() == M tN(0, 0) tN(1, 1) tN(2, 3) tN(3, 3) tN(4, 5) tN(5, 5)
def test_harmonic_rational(): ne = Integer(6) no = Integer(5) pe = Integer(8) po = Integer(9) qe = Integer(10) qo = Integer(13) Heee = harmonic(ne + pe / qe) Aeee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 - Rational(1, 4)) * log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi * (Rational(1, 4) + sqrt(5) / 4) / (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) + Rational(13944145, 4720968)) Heeo = harmonic(ne + pe / qo) Aeeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) + 2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) + 2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) - 2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) - 2 * log(sin(4 * pi / 13)) * cos(pi / 13) + pi * cot(5 * pi / 13) / 2 - 2 * log(sin(pi / 13)) * cos(3 * pi / 13) + Rational(2422020029, 702257080)) Heoe = harmonic(ne + po / qe) Aeoe = ( -log(20) + 2 * (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) + 2 * (Rational(-1, 4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 - Rational(1, 4)) * log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) + Rational(11818877030, 4286604231) + pi * (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8))) Heoo = harmonic(ne + po / qo) Aeoo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) + 2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) + 2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) - 2 * log(sin(5 * pi / 13)) * cos(pi / 13) - 2 * log(sin(pi / 13)) * cos(5 * pi / 13) + pi * cot(4 * pi / 13) / 2 - 2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) + Rational(11669332571, 3628714320)) Hoee = harmonic(no + pe / qe) Aoee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 - Rational(1, 4)) * log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi * (Rational(1, 4) + sqrt(5) / 4) / (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) + Rational(779405, 277704)) Hoeo = harmonic(no + pe / qo) Aoeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) + 2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) + 2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) - 2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) - 2 * log(sin(4 * pi / 13)) * cos(pi / 13) + pi * cot(5 * pi / 13) / 2 - 2 * log(sin(pi / 13)) * cos(3 * pi / 13) + Rational(53857323, 16331560)) Hooe = harmonic(no + po / qe) Aooe = ( -log(20) + 2 * (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) + 2 * (Rational(-1, 4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 - Rational(1, 4)) * log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 * (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) + Rational(486853480, 186374097) + pi * (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8))) Hooo = harmonic(no + po / qo) Aooo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) + 2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) + 2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) - 2 * log(sin(5 * pi / 13)) * cos(pi / 13) - 2 * log(sin(pi / 13)) * cos(5 * pi / 13) + pi * cot(4 * pi / 13) / 2 - 2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) + Rational(383693479, 125128080)) H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo] A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo] for h, a in zip(H, A): e = expand_func(h).doit() assert cancel(e / a) == 1 assert h.evalf() == a.evalf()
def test_RootOf_expand_func1(): r0 = RootOf(x**3 + x + 1, 0) assert expand_func(r0) == r0 r1 = RootOf(x**3 - sqrt(2)*x + I, 1) assert expand_func(r1) == RootOf(x**12 - 4*x**8 + 2*x**6 + 4*x**4 + 4*x**2 + 1, 7)
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)
def test_sympyissue_3686(): # remove this when fresnel itegrals are implemented assert expand_func(integrate(sin(x**2), x)) == \ sqrt(2)*sqrt(pi)*fresnels(sqrt(2)*x/sqrt(pi))/2
def test_rsolve(): f = y(n + 2) - y(n + 1) - y(n) h = sqrt(5)*(Rational(1, 2) + sqrt(5)/2)**n \ - sqrt(5)*(Rational(1, 2) - sqrt(5)/2)**n assert rsolve(f, y(n)) in [ C0 * (Rational(1, 2) - sqrt(5) / 2)**n + C1 * (Rational(1, 2) + sqrt(5) / 2)**n, C1 * (Rational(1, 2) - sqrt(5) / 2)**n + C0 * (Rational(1, 2) + sqrt(5) / 2)**n, ] assert rsolve(f, y(n), [0, 5]) == h assert rsolve(f, y(n), {0: 0, 1: 5}) == h assert rsolve(f, y(n), {y(0): 0, y(1): 5}) == h assert rsolve(y(n) - y(n - 1) - y(n - 2), y(n), [0, 5]) == h assert rsolve(Eq(y(n), y(n - 1) + y(n - 2)), y(n), [0, 5]) == h assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = (n - 1) * y(n + 2) - (n**2 + 3 * n - 2) * y(n + 1) + 2 * n * (n + 1) * y(n) g = C1 * factorial(n) + C0 * 2**n h = -3 * factorial(n) + 3 * 2**n assert rsolve(f, y(n)) == g assert rsolve(f, y(n), []) == g assert rsolve(f, y(n), {}) == g assert rsolve(f, y(n), [0, 3]) == h assert rsolve(f, y(n), {0: 0, 1: 3}) == h assert rsolve(f, y(n), {y(0): 0, y(1): 3}) == h assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = y(n) - y(n - 1) - 2 assert rsolve(f, y(n), {y(0): 0}) == 2 * n assert rsolve(f, y(n), {y(0): 1}) == 2 * n + 1 assert rsolve(f, y(n), {y(0): 0, y(1): 1}) is None assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = 3 * y(n - 1) - y(n) - 1 assert rsolve(f, y(n), {y(0): 0}) == -3**n / 2 + Rational(1, 2) assert rsolve(f, y(n), {y(0): 1}) == 3**n / 2 + Rational(1, 2) assert rsolve(f, y(n), {y(0): 2}) == 3 * 3**n / 2 + Rational(1, 2) assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = y(n) - 1 / n * y(n - 1) assert rsolve(f, y(n)) == C0 / factorial(n) assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = y(n) - 1 / n * y(n - 1) - 1 assert rsolve(f, y(n)) is None f = 2 * y(n - 1) + (1 - n) * y(n) / n assert rsolve(f, y(n), {y(1): 1}) == 2**(n - 1) * n assert rsolve(f, y(n), {y(1): 2}) == 2**(n - 1) * n * 2 assert rsolve(f, y(n), {y(1): 3}) == 2**(n - 1) * n * 3 assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 f = (n - 1) * (n - 2) * y(n + 2) - (n + 1) * (n + 2) * y(n) assert rsolve(f, y(n), {y(3): 6, y(4): 24}) == n * (n - 1) * (n - 2) assert rsolve(f, y(n), { y(3): 6, y(4): -24 }) == -n * (n - 1) * (n - 2) * (-1)**(n) assert f.subs(y, Lambda(k, rsolve(f, y(n)).subs(n, k))).simplify() == 0 assert rsolve(Eq(y(n + 1), a * y(n)), y(n), {y(1): a}).simplify() == a**n assert rsolve(y(n) - a*y(n-2), y(n), {y(1): sqrt(a)*(a + b), y(2): a*(a - b)}).simplify() == \ a**(n/2)*(-(-1)**n*b + a) f = (-16 * n**2 + 32 * n - 12) * y(n - 1) + (4 * n**2 - 12 * n + 9) * y(n) assert expand_func(rsolve(f, y(n), {y(1): binomial(2*n + 1, 3)}).rewrite(gamma)).simplify() == \ 2**(2*n)*n*(2*n - 1)*(4*n**2 - 1)/12 assert (rsolve(y(n) + a * (y(n + 1) + y(n - 1)) / 2, y(n)) - (C0 * (-sqrt(-1 + a**(-2)) - 1 / a)**n + C1 * (sqrt(-1 + a**(-2)) - 1 / a)**n)).simplify() == 0
def test_fresnel(): assert expand_func(integrate(sin(pi*x**2/2), x)) == fresnels(x) assert expand_func(integrate(cos(pi*x**2/2), x)) == fresnelc(x)