test_comb_numbers.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. import string
  2. from sympy.concrete.products import Product
  3. from sympy.concrete.summations import Sum
  4. from sympy.core.function import (diff, expand_func)
  5. from sympy.core import (EulerGamma, TribonacciConstant)
  6. from sympy.core.numbers import (Float, I, Rational, oo, pi)
  7. from sympy.core.singleton import S
  8. from sympy.core.symbol import (Dummy, Symbol, symbols)
  9. from sympy.functions.combinatorial.numbers import carmichael
  10. from sympy.functions.elementary.complexes import (im, re)
  11. from sympy.functions.elementary.integers import floor
  12. from sympy.polys.polytools import cancel
  13. from sympy.series.limits import limit, Limit
  14. from sympy.series.order import O
  15. from sympy.functions import (
  16. bernoulli, harmonic, bell, fibonacci, tribonacci, lucas, euler, catalan,
  17. genocchi, andre, partition, motzkin, binomial, gamma, sqrt, cbrt, hyper, log, digamma,
  18. trigamma, polygamma, factorial, sin, cos, cot, polylog, zeta, dirichlet_eta)
  19. from sympy.functions.combinatorial.numbers import _nT
  20. from sympy.core.expr import unchanged
  21. from sympy.core.numbers import GoldenRatio, Integer
  22. from sympy.testing.pytest import raises, nocache_fail, warns_deprecated_sympy
  23. from sympy.abc import x
  24. def test_carmichael():
  25. assert carmichael.find_carmichael_numbers_in_range(0, 561) == []
  26. assert carmichael.find_carmichael_numbers_in_range(561, 562) == [561]
  27. assert carmichael.find_carmichael_numbers_in_range(561, 1105) == carmichael.find_carmichael_numbers_in_range(561,
  28. 562)
  29. assert carmichael.find_first_n_carmichaels(5) == [561, 1105, 1729, 2465, 2821]
  30. raises(ValueError, lambda: carmichael.is_carmichael(-2))
  31. raises(ValueError, lambda: carmichael.find_carmichael_numbers_in_range(-2, 2))
  32. raises(ValueError, lambda: carmichael.find_carmichael_numbers_in_range(22, 2))
  33. with warns_deprecated_sympy():
  34. assert carmichael.is_prime(2821) == False
  35. def test_bernoulli():
  36. assert bernoulli(0) == 1
  37. assert bernoulli(1) == Rational(1, 2)
  38. assert bernoulli(2) == Rational(1, 6)
  39. assert bernoulli(3) == 0
  40. assert bernoulli(4) == Rational(-1, 30)
  41. assert bernoulli(5) == 0
  42. assert bernoulli(6) == Rational(1, 42)
  43. assert bernoulli(7) == 0
  44. assert bernoulli(8) == Rational(-1, 30)
  45. assert bernoulli(10) == Rational(5, 66)
  46. assert bernoulli(1000001) == 0
  47. assert bernoulli(0, x) == 1
  48. assert bernoulli(1, x) == x - S.Half
  49. assert bernoulli(2, x) == x**2 - x + Rational(1, 6)
  50. assert bernoulli(3, x) == x**3 - (3*x**2)/2 + x/2
  51. # Should be fast; computed with mpmath
  52. b = bernoulli(1000)
  53. assert b.p % 10**10 == 7950421099
  54. assert b.q == 342999030
  55. b = bernoulli(10**6, evaluate=False).evalf()
  56. assert str(b) == '-2.23799235765713e+4767529'
  57. # Issue #8527
  58. l = Symbol('l', integer=True)
  59. m = Symbol('m', integer=True, nonnegative=True)
  60. n = Symbol('n', integer=True, positive=True)
  61. assert isinstance(bernoulli(2 * l + 1), bernoulli)
  62. assert isinstance(bernoulli(2 * m + 1), bernoulli)
  63. assert bernoulli(2 * n + 1) == 0
  64. assert bernoulli(x, 1) == bernoulli(x)
  65. assert str(bernoulli(0.0, 2.3).evalf(n=10)) == '1.000000000'
  66. assert str(bernoulli(1.0).evalf(n=10)) == '0.5000000000'
  67. assert str(bernoulli(1.2).evalf(n=10)) == '0.4195995367'
  68. assert str(bernoulli(1.2, 0.8).evalf(n=10)) == '0.2144830348'
  69. assert str(bernoulli(1.2, -0.8).evalf(n=10)) == '-1.158865646 - 0.6745558744*I'
  70. assert str(bernoulli(3.0, 1j).evalf(n=10)) == '1.5 - 0.5*I'
  71. assert str(bernoulli(I).evalf(n=10)) == '0.9268485643 - 0.5821580598*I'
  72. assert str(bernoulli(I, I).evalf(n=10)) == '0.1267792071 + 0.01947413152*I'
  73. assert bernoulli(x).evalf() == bernoulli(x)
  74. def test_bernoulli_rewrite():
  75. from sympy.functions.elementary.piecewise import Piecewise
  76. n = Symbol('n', integer=True, nonnegative=True)
  77. assert bernoulli(-1).rewrite(zeta) == pi**2/6
  78. assert bernoulli(-2).rewrite(zeta) == 2*zeta(3)
  79. assert not bernoulli(n, -3).rewrite(zeta).has(harmonic)
  80. assert bernoulli(-4, x).rewrite(zeta) == 4*zeta(5, x)
  81. assert isinstance(bernoulli(n, x).rewrite(zeta), Piecewise)
  82. assert bernoulli(n+1, x).rewrite(zeta) == -(n+1) * zeta(-n, x)
  83. def test_fibonacci():
  84. assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3]
  85. assert fibonacci(100) == 354224848179261915075
  86. assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7]
  87. assert lucas(100) == 792070839848372253127
  88. assert fibonacci(1, x) == 1
  89. assert fibonacci(2, x) == x
  90. assert fibonacci(3, x) == x**2 + 1
  91. assert fibonacci(4, x) == x**3 + 2*x
  92. # issue #8800
  93. n = Dummy('n')
  94. assert fibonacci(n).limit(n, S.Infinity) is S.Infinity
  95. assert lucas(n).limit(n, S.Infinity) is S.Infinity
  96. assert fibonacci(n).rewrite(sqrt) == \
  97. 2**(-n)*sqrt(5)*((1 + sqrt(5))**n - (-sqrt(5) + 1)**n) / 5
  98. assert fibonacci(n).rewrite(sqrt).subs(n, 10).expand() == fibonacci(10)
  99. assert fibonacci(n).rewrite(GoldenRatio).subs(n,10).evalf() == \
  100. Float(fibonacci(10))
  101. assert lucas(n).rewrite(sqrt) == \
  102. (fibonacci(n-1).rewrite(sqrt) + fibonacci(n+1).rewrite(sqrt)).simplify()
  103. assert lucas(n).rewrite(sqrt).subs(n, 10).expand() == lucas(10)
  104. raises(ValueError, lambda: fibonacci(-3, x))
  105. def test_tribonacci():
  106. assert [tribonacci(n) for n in range(8)] == [0, 1, 1, 2, 4, 7, 13, 24]
  107. assert tribonacci(100) == 98079530178586034536500564
  108. assert tribonacci(0, x) == 0
  109. assert tribonacci(1, x) == 1
  110. assert tribonacci(2, x) == x**2
  111. assert tribonacci(3, x) == x**4 + x
  112. assert tribonacci(4, x) == x**6 + 2*x**3 + 1
  113. assert tribonacci(5, x) == x**8 + 3*x**5 + 3*x**2
  114. n = Dummy('n')
  115. assert tribonacci(n).limit(n, S.Infinity) is S.Infinity
  116. w = (-1 + S.ImaginaryUnit * sqrt(3)) / 2
  117. a = (1 + cbrt(19 + 3*sqrt(33)) + cbrt(19 - 3*sqrt(33))) / 3
  118. b = (1 + w*cbrt(19 + 3*sqrt(33)) + w**2*cbrt(19 - 3*sqrt(33))) / 3
  119. c = (1 + w**2*cbrt(19 + 3*sqrt(33)) + w*cbrt(19 - 3*sqrt(33))) / 3
  120. assert tribonacci(n).rewrite(sqrt) == \
  121. (a**(n + 1)/((a - b)*(a - c))
  122. + b**(n + 1)/((b - a)*(b - c))
  123. + c**(n + 1)/((c - a)*(c - b)))
  124. assert tribonacci(n).rewrite(sqrt).subs(n, 4).simplify() == tribonacci(4)
  125. assert tribonacci(n).rewrite(GoldenRatio).subs(n,10).evalf() == \
  126. Float(tribonacci(10))
  127. assert tribonacci(n).rewrite(TribonacciConstant) == floor(
  128. 3*TribonacciConstant**n*(102*sqrt(33) + 586)**Rational(1, 3)/
  129. (-2*(102*sqrt(33) + 586)**Rational(1, 3) + 4 + (102*sqrt(33)
  130. + 586)**Rational(2, 3)) + S.Half)
  131. raises(ValueError, lambda: tribonacci(-1, x))
  132. @nocache_fail
  133. def test_bell():
  134. assert [bell(n) for n in range(8)] == [1, 1, 2, 5, 15, 52, 203, 877]
  135. assert bell(0, x) == 1
  136. assert bell(1, x) == x
  137. assert bell(2, x) == x**2 + x
  138. assert bell(5, x) == x**5 + 10*x**4 + 25*x**3 + 15*x**2 + x
  139. assert bell(oo) is S.Infinity
  140. raises(ValueError, lambda: bell(oo, x))
  141. raises(ValueError, lambda: bell(-1))
  142. raises(ValueError, lambda: bell(S.Half))
  143. X = symbols('x:6')
  144. # X = (x0, x1, .. x5)
  145. # at the same time: X[1] = x1, X[2] = x2 for standard readablity.
  146. # but we must supply zero-based indexed object X[1:] = (x1, .. x5)
  147. assert bell(6, 2, X[1:]) == 6*X[5]*X[1] + 15*X[4]*X[2] + 10*X[3]**2
  148. assert bell(
  149. 6, 3, X[1:]) == 15*X[4]*X[1]**2 + 60*X[3]*X[2]*X[1] + 15*X[2]**3
  150. X = (1, 10, 100, 1000, 10000)
  151. assert bell(6, 2, X) == (6 + 15 + 10)*10000
  152. X = (1, 2, 3, 3, 5)
  153. assert bell(6, 2, X) == 6*5 + 15*3*2 + 10*3**2
  154. X = (1, 2, 3, 5)
  155. assert bell(6, 3, X) == 15*5 + 60*3*2 + 15*2**3
  156. # Dobinski's formula
  157. n = Symbol('n', integer=True, nonnegative=True)
  158. # For large numbers, this is too slow
  159. # For nonintegers, there are significant precision errors
  160. for i in [0, 2, 3, 7, 13, 42, 55]:
  161. # Running without the cache this is either very slow or goes into an
  162. # infinite loop.
  163. assert bell(i).evalf() == bell(n).rewrite(Sum).evalf(subs={n: i})
  164. m = Symbol("m")
  165. assert bell(m).rewrite(Sum) == bell(m)
  166. assert bell(n, m).rewrite(Sum) == bell(n, m)
  167. # issue 9184
  168. n = Dummy('n')
  169. assert bell(n).limit(n, S.Infinity) is S.Infinity
  170. def test_harmonic():
  171. n = Symbol("n")
  172. m = Symbol("m")
  173. assert harmonic(n, 0) == n
  174. assert harmonic(n).evalf() == harmonic(n)
  175. assert harmonic(n, 1) == harmonic(n)
  176. assert harmonic(1, n) == 1
  177. assert harmonic(0, 1) == 0
  178. assert harmonic(1, 1) == 1
  179. assert harmonic(2, 1) == Rational(3, 2)
  180. assert harmonic(3, 1) == Rational(11, 6)
  181. assert harmonic(4, 1) == Rational(25, 12)
  182. assert harmonic(0, 2) == 0
  183. assert harmonic(1, 2) == 1
  184. assert harmonic(2, 2) == Rational(5, 4)
  185. assert harmonic(3, 2) == Rational(49, 36)
  186. assert harmonic(4, 2) == Rational(205, 144)
  187. assert harmonic(0, 3) == 0
  188. assert harmonic(1, 3) == 1
  189. assert harmonic(2, 3) == Rational(9, 8)
  190. assert harmonic(3, 3) == Rational(251, 216)
  191. assert harmonic(4, 3) == Rational(2035, 1728)
  192. assert harmonic(oo, -1) is S.NaN
  193. assert harmonic(oo, 0) is oo
  194. assert harmonic(oo, S.Half) is oo
  195. assert harmonic(oo, 1) is oo
  196. assert harmonic(oo, 2) == (pi**2)/6
  197. assert harmonic(oo, 3) == zeta(3)
  198. assert harmonic(oo, Dummy(negative=True)) is S.NaN
  199. ip = Dummy(integer=True, positive=True)
  200. if (1/ip <= 1) is True: #---------------------------------+
  201. assert None, 'delete this if-block and the next line' #|
  202. ip = Dummy(even=True, positive=True) #--------------------+
  203. assert harmonic(oo, 1/ip) is oo
  204. assert harmonic(oo, 1 + ip) is zeta(1 + ip)
  205. assert harmonic(0, m) == 0
  206. assert harmonic(-1, -1) == 0
  207. assert harmonic(-1, 0) == -1
  208. assert harmonic(-1, 1) is S.ComplexInfinity
  209. assert harmonic(-1, 2) is S.NaN
  210. assert harmonic(-3, -2) == -5
  211. assert harmonic(-3, -3) == 9
  212. def test_harmonic_rational():
  213. ne = S(6)
  214. no = S(5)
  215. pe = S(8)
  216. po = S(9)
  217. qe = S(10)
  218. qo = S(13)
  219. Heee = harmonic(ne + pe/qe)
  220. Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
  221. + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
  222. + pi*sqrt(2*sqrt(5)/5 + 1)/2 + Rational(13944145, 4720968))
  223. Heeo = harmonic(ne + pe/qo)
  224. Aeeo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(4, 13)) + 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(32, 13))
  225. + 2*log(sin(pi*Rational(5, 13)))*cos(pi*Rational(80, 13)) - 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(5, 13))
  226. - 2*log(sin(pi*Rational(4, 13)))*cos(pi/13) + pi*cot(pi*Rational(5, 13))/2 - 2*log(sin(pi/13))*cos(pi*Rational(3, 13))
  227. + Rational(2422020029, 702257080))
  228. Heoe = harmonic(ne + po/qe)
  229. Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
  230. + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
  231. + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
  232. + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
  233. + Rational(11818877030, 4286604231) + pi*sqrt(2*sqrt(5) + 5)/2)
  234. Heoo = harmonic(ne + po/qo)
  235. Aeoo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(54, 13)) + 2*log(sin(pi*Rational(4, 13)))*cos(pi*Rational(6, 13))
  236. + 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(108, 13)) - 2*log(sin(pi*Rational(5, 13)))*cos(pi/13)
  237. - 2*log(sin(pi/13))*cos(pi*Rational(5, 13)) + pi*cot(pi*Rational(4, 13))/2
  238. - 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(3, 13)) + Rational(11669332571, 3628714320))
  239. Hoee = harmonic(no + pe/qe)
  240. Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
  241. + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
  242. + pi*sqrt(2*sqrt(5)/5 + 1)/2 + Rational(779405, 277704))
  243. Hoeo = harmonic(no + pe/qo)
  244. Aoeo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(4, 13)) + 2*log(sin(pi*Rational(2, 13)))*cos(pi*Rational(32, 13))
  245. + 2*log(sin(pi*Rational(5, 13)))*cos(pi*Rational(80, 13)) - 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(5, 13))
  246. - 2*log(sin(pi*Rational(4, 13)))*cos(pi/13) + pi*cot(pi*Rational(5, 13))/2
  247. - 2*log(sin(pi/13))*cos(pi*Rational(3, 13)) + Rational(53857323, 16331560))
  248. Hooe = harmonic(no + po/qe)
  249. Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
  250. + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
  251. + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
  252. + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
  253. + Rational(486853480, 186374097) + pi*sqrt(2*sqrt(5) + 5)/2)
  254. Hooo = harmonic(no + po/qo)
  255. Aooo = (-log(26) + 2*log(sin(pi*Rational(3, 13)))*cos(pi*Rational(54, 13)) + 2*log(sin(pi*Rational(4, 13)))*cos(pi*Rational(6, 13))
  256. + 2*log(sin(pi*Rational(6, 13)))*cos(pi*Rational(108, 13)) - 2*log(sin(pi*Rational(5, 13)))*cos(pi/13)
  257. - 2*log(sin(pi/13))*cos(pi*Rational(5, 13)) + pi*cot(pi*Rational(4, 13))/2
  258. - 2*log(sin(pi*Rational(2, 13)))*cos(3*pi/13) + Rational(383693479, 125128080))
  259. H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
  260. A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]
  261. for h, a in zip(H, A):
  262. e = expand_func(h).doit()
  263. assert cancel(e/a) == 1
  264. assert abs(h.n() - a.n()) < 1e-12
  265. def test_harmonic_evalf():
  266. assert str(harmonic(1.5).evalf(n=10)) == '1.280372306'
  267. assert str(harmonic(1.5, 2).evalf(n=10)) == '1.154576311' # issue 7443
  268. assert str(harmonic(4.0, -3).evalf(n=10)) == '100.0000000'
  269. assert str(harmonic(7.0, 1.0).evalf(n=10)) == '2.592857143'
  270. assert str(harmonic(1, pi).evalf(n=10)) == '1.000000000'
  271. assert str(harmonic(2, pi).evalf(n=10)) == '1.113314732'
  272. assert str(harmonic(1000.0, pi).evalf(n=10)) == '1.176241563'
  273. assert str(harmonic(I).evalf(n=10)) == '0.6718659855 + 1.076674047*I'
  274. assert str(harmonic(I, I).evalf(n=10)) == '-0.3970915266 + 1.9629689*I'
  275. assert harmonic(-1.0, 1).evalf() is S.NaN
  276. assert harmonic(-2.0, 2.0).evalf() is S.NaN
  277. def test_harmonic_rewrite():
  278. from sympy.functions.elementary.piecewise import Piecewise
  279. n = Symbol("n")
  280. m = Symbol("m", integer=True, positive=True)
  281. x1 = Symbol("x1", positive=True)
  282. x2 = Symbol("x2", negative=True)
  283. assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma
  284. assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma
  285. assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma
  286. assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2
  287. assert isinstance(harmonic(n,m).rewrite(polygamma), Piecewise)
  288. assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1)
  289. assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n
  290. assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma)
  291. assert harmonic(n, x1).rewrite("tractable") == harmonic(n, x1)
  292. assert harmonic(n, x1 + 1).rewrite("tractable") == zeta(x1 + 1) - zeta(x1 + 1, n + 1)
  293. assert harmonic(n, x2).rewrite("tractable") == zeta(x2) - zeta(x2, n + 1)
  294. _k = Dummy("k")
  295. assert harmonic(n).rewrite(Sum).dummy_eq(Sum(1/_k, (_k, 1, n)))
  296. assert harmonic(n, m).rewrite(Sum).dummy_eq(Sum(_k**(-m), (_k, 1, n)))
  297. def test_harmonic_calculus():
  298. y = Symbol("y", positive=True)
  299. z = Symbol("z", negative=True)
  300. assert harmonic(x, 1).limit(x, 0) == 0
  301. assert harmonic(x, y).limit(x, 0) == 0
  302. assert harmonic(x, 1).series(x, y, 2) == \
  303. harmonic(y) + (x - y)*zeta(2, y + 1) + O((x - y)**2, (x, y))
  304. assert limit(harmonic(x, y), x, oo) == harmonic(oo, y)
  305. assert limit(harmonic(x, y + 1), x, oo) == zeta(y + 1)
  306. assert limit(harmonic(x, y - 1), x, oo) == harmonic(oo, y - 1)
  307. assert limit(harmonic(x, z), x, oo) == Limit(harmonic(x, z), x, oo, dir='-')
  308. assert limit(harmonic(x, z + 1), x, oo) == oo
  309. assert limit(harmonic(x, z + 2), x, oo) == harmonic(oo, z + 2)
  310. assert limit(harmonic(x, z - 1), x, oo) == Limit(harmonic(x, z - 1), x, oo, dir='-')
  311. def test_euler():
  312. assert euler(0) == 1
  313. assert euler(1) == 0
  314. assert euler(2) == -1
  315. assert euler(3) == 0
  316. assert euler(4) == 5
  317. assert euler(6) == -61
  318. assert euler(8) == 1385
  319. assert euler(20, evaluate=False) != 370371188237525
  320. n = Symbol('n', integer=True)
  321. assert euler(n) != -1
  322. assert euler(n).subs(n, 2) == -1
  323. assert euler(-1) == S.Pi / 2
  324. assert euler(-1, 1) == 2*log(2)
  325. assert euler(-2).evalf() == (2*S.Catalan).evalf()
  326. assert euler(-3).evalf() == (S.Pi**3 / 16).evalf()
  327. assert str(euler(2.3).evalf(n=10)) == '-1.052850274'
  328. assert str(euler(1.2, 3.4).evalf(n=10)) == '3.575613489'
  329. assert str(euler(I).evalf(n=10)) == '1.248446443 - 0.7675445124*I'
  330. assert str(euler(I, I).evalf(n=10)) == '0.04812930469 + 0.01052411008*I'
  331. assert euler(20).evalf() == 370371188237525.0
  332. assert euler(20, evaluate=False).evalf() == 370371188237525.0
  333. assert euler(n).rewrite(Sum) == euler(n)
  334. n = Symbol('n', integer=True, nonnegative=True)
  335. assert euler(2*n + 1).rewrite(Sum) == 0
  336. _j = Dummy('j')
  337. _k = Dummy('k')
  338. assert euler(2*n).rewrite(Sum).dummy_eq(
  339. I*Sum((-1)**_j*2**(-_k)*I**(-_k)*(-2*_j + _k)**(2*n + 1)*
  340. binomial(_k, _j)/_k, (_j, 0, _k), (_k, 1, 2*n + 1)))
  341. def test_euler_odd():
  342. n = Symbol('n', odd=True, positive=True)
  343. assert euler(n) == 0
  344. n = Symbol('n', odd=True)
  345. assert euler(n) != 0
  346. def test_euler_polynomials():
  347. assert euler(0, x) == 1
  348. assert euler(1, x) == x - S.Half
  349. assert euler(2, x) == x**2 - x
  350. assert euler(3, x) == x**3 - (3*x**2)/2 + Rational(1, 4)
  351. m = Symbol('m')
  352. assert isinstance(euler(m, x), euler)
  353. from sympy.core.numbers import Float
  354. A = Float('-0.46237208575048694923364757452876131e8') # from Maple
  355. B = euler(19, S.Pi).evalf(32)
  356. assert abs((A - B)/A) < 1e-31
  357. def test_euler_polynomial_rewrite():
  358. m = Symbol('m')
  359. A = euler(m, x).rewrite('Sum');
  360. assert A.subs({m:3, x:5}).doit() == euler(3, 5)
  361. def test_catalan():
  362. n = Symbol('n', integer=True)
  363. m = Symbol('m', integer=True, positive=True)
  364. k = Symbol('k', integer=True, nonnegative=True)
  365. p = Symbol('p', nonnegative=True)
  366. catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
  367. for i, c in enumerate(catalans):
  368. assert catalan(i) == c
  369. assert catalan(n).rewrite(factorial).subs(n, i) == c
  370. assert catalan(n).rewrite(Product).subs(n, i).doit() == c
  371. assert unchanged(catalan, x)
  372. assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1)
  373. assert catalan(S.Half).rewrite(gamma) == 8/(3*pi)
  374. assert catalan(S.Half).rewrite(factorial).rewrite(gamma) ==\
  375. 8 / (3 * pi)
  376. assert catalan(3*x).rewrite(gamma) == 4**(
  377. 3*x)*gamma(3*x + S.Half)/(sqrt(pi)*gamma(3*x + 2))
  378. assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1)
  379. assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1)
  380. * factorial(n))
  381. assert isinstance(catalan(n).rewrite(Product), catalan)
  382. assert isinstance(catalan(m).rewrite(Product), Product)
  383. assert diff(catalan(x), x) == (polygamma(
  384. 0, x + S.Half) - polygamma(0, x + 2) + log(4))*catalan(x)
  385. assert catalan(x).evalf() == catalan(x)
  386. c = catalan(S.Half).evalf()
  387. assert str(c) == '0.848826363156775'
  388. c = catalan(I).evalf(3)
  389. assert str((re(c), im(c))) == '(0.398, -0.0209)'
  390. # Assumptions
  391. assert catalan(p).is_positive is True
  392. assert catalan(k).is_integer is True
  393. assert catalan(m+3).is_composite is True
  394. def test_genocchi():
  395. genocchis = [0, -1, -1, 0, 1, 0, -3, 0, 17]
  396. for n, g in enumerate(genocchis):
  397. assert genocchi(n) == g
  398. m = Symbol('m', integer=True)
  399. n = Symbol('n', integer=True, positive=True)
  400. assert unchanged(genocchi, m)
  401. assert genocchi(2*n + 1) == 0
  402. gn = 2 * (1 - 2**n) * bernoulli(n)
  403. assert genocchi(n).rewrite(bernoulli).factor() == gn.factor()
  404. gnx = 2 * (bernoulli(n, x) - 2**n * bernoulli(n, (x+1) / 2))
  405. assert genocchi(n, x).rewrite(bernoulli).factor() == gnx.factor()
  406. assert genocchi(2 * n).is_odd
  407. assert genocchi(2 * n).is_even is False
  408. assert genocchi(2 * n + 1).is_even
  409. assert genocchi(n).is_integer
  410. assert genocchi(4 * n).is_positive
  411. # these are the only 2 prime Genocchi numbers
  412. assert genocchi(6, evaluate=False).is_prime == S(-3).is_prime
  413. assert genocchi(8, evaluate=False).is_prime
  414. assert genocchi(4 * n + 2).is_negative
  415. assert genocchi(4 * n + 1).is_negative is False
  416. assert genocchi(4 * n - 2).is_negative
  417. g0 = genocchi(0, evaluate=False)
  418. assert g0.is_positive is False
  419. assert g0.is_negative is False
  420. assert g0.is_even is True
  421. assert g0.is_odd is False
  422. assert genocchi(0, x) == 0
  423. assert genocchi(1, x) == -1
  424. assert genocchi(2, x) == 1 - 2*x
  425. assert genocchi(3, x) == 3*x - 3*x**2
  426. assert genocchi(4, x) == -1 + 6*x**2 - 4*x**3
  427. y = Symbol("y")
  428. assert genocchi(5, (x+y)**100) == -5*(x+y)**400 + 10*(x+y)**300 - 5*(x+y)**100
  429. assert str(genocchi(5.0, 4.0).evalf(n=10)) == '-660.0000000'
  430. assert str(genocchi(Rational(5, 4)).evalf(n=10)) == '-1.104286457'
  431. assert str(genocchi(-2).evalf(n=10)) == '3.606170709'
  432. assert str(genocchi(1.3, 3.7).evalf(n=10)) == '-1.847375373'
  433. assert str(genocchi(I, 1.0).evalf(n=10)) == '-0.3161917278 - 1.45311955*I'
  434. n = Symbol('n')
  435. assert genocchi(n, x).rewrite(dirichlet_eta) == -2*n * dirichlet_eta(1-n, x)
  436. def test_andre():
  437. nums = [1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]
  438. for n, a in enumerate(nums):
  439. assert andre(n) == a
  440. assert andre(S.Infinity) == S.Infinity
  441. assert andre(-1) == -log(2)
  442. assert andre(-2) == -2*S.Catalan
  443. assert andre(-3) == 3*zeta(3)/16
  444. assert andre(-5) == -15*zeta(5)/256
  445. # In fact andre(-2*n) is related to the Dirichlet *beta* function
  446. # at 2*n, but SymPy doesn't implement that (or general L-functions)
  447. assert unchanged(andre, -4)
  448. n = Symbol('n', integer=True, nonnegative=True)
  449. assert unchanged(andre, n)
  450. assert andre(n).is_integer is True
  451. assert andre(n).is_positive is True
  452. assert str(andre(10, evaluate=False).evalf(n=10)) == '50521.00000'
  453. assert str(andre(-1, evaluate=False).evalf(n=10)) == '-0.6931471806'
  454. assert str(andre(-2, evaluate=False).evalf(n=10)) == '-1.831931188'
  455. assert str(andre(-4, evaluate=False).evalf(n=10)) == '1.977889103'
  456. assert str(andre(I, evaluate=False).evalf(n=10)) == '2.378417833 + 0.6343322845*I'
  457. assert andre(x).rewrite(polylog) == \
  458. (-I)**(x+1) * polylog(-x, I) + I**(x+1) * polylog(-x, -I)
  459. assert andre(x).rewrite(zeta) == \
  460. 2 * gamma(x+1) / (2*pi)**(x+1) * \
  461. (zeta(x+1, Rational(1,4)) - cos(pi*x) * zeta(x+1, Rational(3,4)))
  462. @nocache_fail
  463. def test_partition():
  464. partition_nums = [1, 1, 2, 3, 5, 7, 11, 15, 22]
  465. for n, p in enumerate(partition_nums):
  466. assert partition(n) == p
  467. x = Symbol('x')
  468. y = Symbol('y', real=True)
  469. m = Symbol('m', integer=True)
  470. n = Symbol('n', integer=True, negative=True)
  471. p = Symbol('p', integer=True, nonnegative=True)
  472. assert partition(m).is_integer
  473. assert not partition(m).is_negative
  474. assert partition(m).is_nonnegative
  475. assert partition(n).is_zero
  476. assert partition(p).is_positive
  477. assert partition(x).subs(x, 7) == 15
  478. assert partition(y).subs(y, 8) == 22
  479. raises(ValueError, lambda: partition(Rational(5, 4)))
  480. def test__nT():
  481. assert [_nT(i, j) for i in range(5) for j in range(i + 2)] == [
  482. 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 1, 0]
  483. check = [_nT(10, i) for i in range(11)]
  484. assert check == [0, 1, 5, 8, 9, 7, 5, 3, 2, 1, 1]
  485. assert all(type(i) is int for i in check)
  486. assert _nT(10, 5) == 7
  487. assert _nT(100, 98) == 2
  488. assert _nT(100, 100) == 1
  489. assert _nT(10, 3) == 8
  490. def test_nC_nP_nT():
  491. from sympy.utilities.iterables import (
  492. multiset_permutations, multiset_combinations, multiset_partitions,
  493. partitions, subsets, permutations)
  494. from sympy.functions.combinatorial.numbers import (
  495. nP, nC, nT, stirling, _stirling1, _stirling2, _multiset_histogram, _AOP_product)
  496. from sympy.combinatorics.permutations import Permutation
  497. from sympy.core.random import choice
  498. c = string.ascii_lowercase
  499. for i in range(100):
  500. s = ''.join(choice(c) for i in range(7))
  501. u = len(s) == len(set(s))
  502. try:
  503. tot = 0
  504. for i in range(8):
  505. check = nP(s, i)
  506. tot += check
  507. assert len(list(multiset_permutations(s, i))) == check
  508. if u:
  509. assert nP(len(s), i) == check
  510. assert nP(s) == tot
  511. except AssertionError:
  512. print(s, i, 'failed perm test')
  513. raise ValueError()
  514. for i in range(100):
  515. s = ''.join(choice(c) for i in range(7))
  516. u = len(s) == len(set(s))
  517. try:
  518. tot = 0
  519. for i in range(8):
  520. check = nC(s, i)
  521. tot += check
  522. assert len(list(multiset_combinations(s, i))) == check
  523. if u:
  524. assert nC(len(s), i) == check
  525. assert nC(s) == tot
  526. if u:
  527. assert nC(len(s)) == tot
  528. except AssertionError:
  529. print(s, i, 'failed combo test')
  530. raise ValueError()
  531. for i in range(1, 10):
  532. tot = 0
  533. for j in range(1, i + 2):
  534. check = nT(i, j)
  535. assert check.is_Integer
  536. tot += check
  537. assert sum(1 for p in partitions(i, j, size=True) if p[0] == j) == check
  538. assert nT(i) == tot
  539. for i in range(1, 10):
  540. tot = 0
  541. for j in range(1, i + 2):
  542. check = nT(range(i), j)
  543. tot += check
  544. assert len(list(multiset_partitions(list(range(i)), j))) == check
  545. assert nT(range(i)) == tot
  546. for i in range(100):
  547. s = ''.join(choice(c) for i in range(7))
  548. u = len(s) == len(set(s))
  549. try:
  550. tot = 0
  551. for i in range(1, 8):
  552. check = nT(s, i)
  553. tot += check
  554. assert len(list(multiset_partitions(s, i))) == check
  555. if u:
  556. assert nT(range(len(s)), i) == check
  557. if u:
  558. assert nT(range(len(s))) == tot
  559. assert nT(s) == tot
  560. except AssertionError:
  561. print(s, i, 'failed partition test')
  562. raise ValueError()
  563. # tests for Stirling numbers of the first kind that are not tested in the
  564. # above
  565. assert [stirling(9, i, kind=1) for i in range(11)] == [
  566. 0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1, 0]
  567. perms = list(permutations(range(4)))
  568. assert [sum(1 for p in perms if Permutation(p).cycles == i)
  569. for i in range(5)] == [0, 6, 11, 6, 1] == [
  570. stirling(4, i, kind=1) for i in range(5)]
  571. # http://oeis.org/A008275
  572. assert [stirling(n, k, signed=1)
  573. for n in range(10) for k in range(1, n + 1)] == [
  574. 1, -1,
  575. 1, 2, -3,
  576. 1, -6, 11, -6,
  577. 1, 24, -50, 35, -10,
  578. 1, -120, 274, -225, 85, -15,
  579. 1, 720, -1764, 1624, -735, 175, -21,
  580. 1, -5040, 13068, -13132, 6769, -1960, 322, -28,
  581. 1, 40320, -109584, 118124, -67284, 22449, -4536, 546, -36, 1]
  582. # https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
  583. assert [stirling(n, k, kind=1)
  584. for n in range(10) for k in range(n+1)] == [
  585. 1,
  586. 0, 1,
  587. 0, 1, 1,
  588. 0, 2, 3, 1,
  589. 0, 6, 11, 6, 1,
  590. 0, 24, 50, 35, 10, 1,
  591. 0, 120, 274, 225, 85, 15, 1,
  592. 0, 720, 1764, 1624, 735, 175, 21, 1,
  593. 0, 5040, 13068, 13132, 6769, 1960, 322, 28, 1,
  594. 0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1]
  595. # https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind
  596. assert [stirling(n, k, kind=2)
  597. for n in range(10) for k in range(n+1)] == [
  598. 1,
  599. 0, 1,
  600. 0, 1, 1,
  601. 0, 1, 3, 1,
  602. 0, 1, 7, 6, 1,
  603. 0, 1, 15, 25, 10, 1,
  604. 0, 1, 31, 90, 65, 15, 1,
  605. 0, 1, 63, 301, 350, 140, 21, 1,
  606. 0, 1, 127, 966, 1701, 1050, 266, 28, 1,
  607. 0, 1, 255, 3025, 7770, 6951, 2646, 462, 36, 1]
  608. assert stirling(3, 4, kind=1) == stirling(3, 4, kind=1) == 0
  609. raises(ValueError, lambda: stirling(-2, 2))
  610. # Assertion that the return type is SymPy Integer.
  611. assert isinstance(_stirling1(6, 3), Integer)
  612. assert isinstance(_stirling2(6, 3), Integer)
  613. def delta(p):
  614. if len(p) == 1:
  615. return oo
  616. return min(abs(i[0] - i[1]) for i in subsets(p, 2))
  617. parts = multiset_partitions(range(5), 3)
  618. d = 2
  619. assert (sum(1 for p in parts if all(delta(i) >= d for i in p)) ==
  620. stirling(5, 3, d=d) == 7)
  621. # other coverage tests
  622. assert nC('abb', 2) == nC('aab', 2) == 2
  623. assert nP(3, 3, replacement=True) == nP('aabc', 3, replacement=True) == 27
  624. assert nP(3, 4) == 0
  625. assert nP('aabc', 5) == 0
  626. assert nC(4, 2, replacement=True) == nC('abcdd', 2, replacement=True) == \
  627. len(list(multiset_combinations('aabbccdd', 2))) == 10
  628. assert nC('abcdd') == sum(nC('abcdd', i) for i in range(6)) == 24
  629. assert nC(list('abcdd'), 4) == 4
  630. assert nT('aaaa') == nT(4) == len(list(partitions(4))) == 5
  631. assert nT('aaab') == len(list(multiset_partitions('aaab'))) == 7
  632. assert nC('aabb'*3, 3) == 4 # aaa, bbb, abb, baa
  633. assert dict(_AOP_product((4,1,1,1))) == {
  634. 0: 1, 1: 4, 2: 7, 3: 8, 4: 8, 5: 7, 6: 4, 7: 1}
  635. # the following was the first t that showed a problem in a previous form of
  636. # the function, so it's not as random as it may appear
  637. t = (3, 9, 4, 6, 6, 5, 5, 2, 10, 4)
  638. assert sum(_AOP_product(t)[i] for i in range(55)) == 58212000
  639. raises(ValueError, lambda: _multiset_histogram({1:'a'}))
  640. def test_PR_14617():
  641. from sympy.functions.combinatorial.numbers import nT
  642. for n in (0, []):
  643. for k in (-1, 0, 1):
  644. if k == 0:
  645. assert nT(n, k) == 1
  646. else:
  647. assert nT(n, k) == 0
  648. def test_issue_8496():
  649. n = Symbol("n")
  650. k = Symbol("k")
  651. raises(TypeError, lambda: catalan(n, k))
  652. def test_issue_8601():
  653. n = Symbol('n', integer=True, negative=True)
  654. assert catalan(n - 1) is S.Zero
  655. assert catalan(Rational(-1, 2)) is S.ComplexInfinity
  656. assert catalan(-S.One) == Rational(-1, 2)
  657. c1 = catalan(-5.6).evalf()
  658. assert str(c1) == '6.93334070531408e-5'
  659. c2 = catalan(-35.4).evalf()
  660. assert str(c2) == '-4.14189164517449e-24'
  661. def test_motzkin():
  662. assert motzkin.is_motzkin(4) == True
  663. assert motzkin.is_motzkin(9) == True
  664. assert motzkin.is_motzkin(10) == False
  665. assert motzkin.find_motzkin_numbers_in_range(10,200) == [21, 51, 127]
  666. assert motzkin.find_motzkin_numbers_in_range(10,400) == [21, 51, 127, 323]
  667. assert motzkin.find_motzkin_numbers_in_range(10,1600) == [21, 51, 127, 323, 835]
  668. assert motzkin.find_first_n_motzkins(5) == [1, 1, 2, 4, 9]
  669. assert motzkin.find_first_n_motzkins(7) == [1, 1, 2, 4, 9, 21, 51]
  670. assert motzkin.find_first_n_motzkins(10) == [1, 1, 2, 4, 9, 21, 51, 127, 323, 835]
  671. raises(ValueError, lambda: motzkin.eval(77.58))
  672. raises(ValueError, lambda: motzkin.eval(-8))
  673. raises(ValueError, lambda: motzkin.find_motzkin_numbers_in_range(-2,7))
  674. raises(ValueError, lambda: motzkin.find_motzkin_numbers_in_range(13,7))
  675. raises(ValueError, lambda: motzkin.find_first_n_motzkins(112.8))
  676. def test_nD_derangements():
  677. from sympy.utilities.iterables import (partitions, multiset,
  678. multiset_derangements, multiset_permutations)
  679. from sympy.functions.combinatorial.numbers import nD
  680. got = []
  681. for i in partitions(8, k=4):
  682. s = []
  683. it = 0
  684. for k, v in i.items():
  685. for i in range(v):
  686. s.extend([it]*k)
  687. it += 1
  688. ms = multiset(s)
  689. c1 = sum(1 for i in multiset_permutations(s) if
  690. all(i != j for i, j in zip(i, s)))
  691. assert c1 == nD(ms) == nD(ms, 0) == nD(ms, 1)
  692. v = [tuple(i) for i in multiset_derangements(s)]
  693. c2 = len(v)
  694. assert c2 == len(set(v))
  695. assert c1 == c2
  696. got.append(c1)
  697. assert got == [1, 4, 6, 12, 24, 24, 61, 126, 315, 780, 297, 772,
  698. 2033, 5430, 14833]
  699. assert nD('1112233456', brute=True) == nD('1112233456') == 16356
  700. assert nD('') == nD([]) == nD({}) == 0
  701. assert nD({1: 0}) == 0
  702. raises(ValueError, lambda: nD({1: -1}))
  703. assert nD('112') == 0
  704. assert nD(i='112') == 0
  705. assert [nD(n=i) for i in range(6)] == [0, 0, 1, 2, 9, 44]
  706. assert nD((i for i in range(4))) == nD('0123') == 9
  707. assert nD(m=(i for i in range(4))) == 3
  708. assert nD(m={0: 1, 1: 1, 2: 1, 3: 1}) == 3
  709. assert nD(m=[0, 1, 2, 3]) == 3
  710. raises(TypeError, lambda: nD(m=0))
  711. raises(TypeError, lambda: nD(-1))
  712. assert nD({-1: 1, -2: 1}) == 1
  713. assert nD(m={0: 3}) == 0
  714. raises(ValueError, lambda: nD(i='123', n=3))
  715. raises(ValueError, lambda: nD(i='123', m=(1,2)))
  716. raises(ValueError, lambda: nD(n=0, m=(1,2)))
  717. raises(ValueError, lambda: nD({1: -1}))
  718. raises(ValueError, lambda: nD(m={-1: 1, 2: 1}))
  719. raises(ValueError, lambda: nD(m={1: -1, 2: 1}))
  720. raises(ValueError, lambda: nD(m=[-1, 2]))
  721. raises(TypeError, lambda: nD({1: x}))
  722. raises(TypeError, lambda: nD(m={1: x}))
  723. raises(TypeError, lambda: nD(m={x: 1}))