test_integers.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. from sympy.calculus.accumulationbounds import AccumBounds
  2. from sympy.core.numbers import (E, Float, I, Rational, nan, oo, pi, zoo)
  3. from sympy.core.relational import (Eq, Ge, Gt, Le, Lt, Ne)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Symbol, symbols)
  6. from sympy.functions.combinatorial.factorials import factorial
  7. from sympy.functions.elementary.exponential import (exp, log)
  8. from sympy.functions.elementary.integers import (ceiling, floor, frac)
  9. from sympy.functions.elementary.miscellaneous import sqrt
  10. from sympy.functions.elementary.trigonometric import sin, cos, tan
  11. from sympy.core.expr import unchanged
  12. from sympy.testing.pytest import XFAIL
  13. x = Symbol('x')
  14. i = Symbol('i', imaginary=True)
  15. y = Symbol('y', real=True)
  16. k, n = symbols('k,n', integer=True)
  17. def test_floor():
  18. assert floor(nan) is nan
  19. assert floor(oo) is oo
  20. assert floor(-oo) is -oo
  21. assert floor(zoo) is zoo
  22. assert floor(0) == 0
  23. assert floor(1) == 1
  24. assert floor(-1) == -1
  25. assert floor(E) == 2
  26. assert floor(-E) == -3
  27. assert floor(2*E) == 5
  28. assert floor(-2*E) == -6
  29. assert floor(pi) == 3
  30. assert floor(-pi) == -4
  31. assert floor(S.Half) == 0
  32. assert floor(Rational(-1, 2)) == -1
  33. assert floor(Rational(7, 3)) == 2
  34. assert floor(Rational(-7, 3)) == -3
  35. assert floor(-Rational(7, 3)) == -3
  36. assert floor(Float(17.0)) == 17
  37. assert floor(-Float(17.0)) == -17
  38. assert floor(Float(7.69)) == 7
  39. assert floor(-Float(7.69)) == -8
  40. assert floor(I) == I
  41. assert floor(-I) == -I
  42. e = floor(i)
  43. assert e.func is floor and e.args[0] == i
  44. assert floor(oo*I) == oo*I
  45. assert floor(-oo*I) == -oo*I
  46. assert floor(exp(I*pi/4)*oo) == exp(I*pi/4)*oo
  47. assert floor(2*I) == 2*I
  48. assert floor(-2*I) == -2*I
  49. assert floor(I/2) == 0
  50. assert floor(-I/2) == -I
  51. assert floor(E + 17) == 19
  52. assert floor(pi + 2) == 5
  53. assert floor(E + pi) == 5
  54. assert floor(I + pi) == 3 + I
  55. assert floor(floor(pi)) == 3
  56. assert floor(floor(y)) == floor(y)
  57. assert floor(floor(x)) == floor(x)
  58. assert unchanged(floor, x)
  59. assert unchanged(floor, 2*x)
  60. assert unchanged(floor, k*x)
  61. assert floor(k) == k
  62. assert floor(2*k) == 2*k
  63. assert floor(k*n) == k*n
  64. assert unchanged(floor, k/2)
  65. assert unchanged(floor, x + y)
  66. assert floor(x + 3) == floor(x) + 3
  67. assert floor(x + k) == floor(x) + k
  68. assert floor(y + 3) == floor(y) + 3
  69. assert floor(y + k) == floor(y) + k
  70. assert floor(3 + I*y + pi) == 6 + floor(y)*I
  71. assert floor(k + n) == k + n
  72. assert unchanged(floor, x*I)
  73. assert floor(k*I) == k*I
  74. assert floor(Rational(23, 10) - E*I) == 2 - 3*I
  75. assert floor(sin(1)) == 0
  76. assert floor(sin(-1)) == -1
  77. assert floor(exp(2)) == 7
  78. assert floor(log(8)/log(2)) != 2
  79. assert int(floor(log(8)/log(2)).evalf(chop=True)) == 3
  80. assert floor(factorial(50)/exp(1)) == \
  81. 11188719610782480504630258070757734324011354208865721592720336800
  82. assert (floor(y) < y) == False
  83. assert (floor(y) <= y) == True
  84. assert (floor(y) > y) == False
  85. assert (floor(y) >= y) == False
  86. assert (floor(x) <= x).is_Relational # x could be non-real
  87. assert (floor(x) > x).is_Relational
  88. assert (floor(x) <= y).is_Relational # arg is not same as rhs
  89. assert (floor(x) > y).is_Relational
  90. assert (floor(y) <= oo) == True
  91. assert (floor(y) < oo) == True
  92. assert (floor(y) >= -oo) == True
  93. assert (floor(y) > -oo) == True
  94. assert floor(y).rewrite(frac) == y - frac(y)
  95. assert floor(y).rewrite(ceiling) == -ceiling(-y)
  96. assert floor(y).rewrite(frac).subs(y, -pi) == floor(-pi)
  97. assert floor(y).rewrite(frac).subs(y, E) == floor(E)
  98. assert floor(y).rewrite(ceiling).subs(y, E) == -ceiling(-E)
  99. assert floor(y).rewrite(ceiling).subs(y, -pi) == -ceiling(pi)
  100. assert Eq(floor(y), y - frac(y))
  101. assert Eq(floor(y), -ceiling(-y))
  102. neg = Symbol('neg', negative=True)
  103. nn = Symbol('nn', nonnegative=True)
  104. pos = Symbol('pos', positive=True)
  105. np = Symbol('np', nonpositive=True)
  106. assert (floor(neg) < 0) == True
  107. assert (floor(neg) <= 0) == True
  108. assert (floor(neg) > 0) == False
  109. assert (floor(neg) >= 0) == False
  110. assert (floor(neg) <= -1) == True
  111. assert (floor(neg) >= -3) == (neg >= -3)
  112. assert (floor(neg) < 5) == (neg < 5)
  113. assert (floor(nn) < 0) == False
  114. assert (floor(nn) >= 0) == True
  115. assert (floor(pos) < 0) == False
  116. assert (floor(pos) <= 0) == (pos < 1)
  117. assert (floor(pos) > 0) == (pos >= 1)
  118. assert (floor(pos) >= 0) == True
  119. assert (floor(pos) >= 3) == (pos >= 3)
  120. assert (floor(np) <= 0) == True
  121. assert (floor(np) > 0) == False
  122. assert floor(neg).is_negative == True
  123. assert floor(neg).is_nonnegative == False
  124. assert floor(nn).is_negative == False
  125. assert floor(nn).is_nonnegative == True
  126. assert floor(pos).is_negative == False
  127. assert floor(pos).is_nonnegative == True
  128. assert floor(np).is_negative is None
  129. assert floor(np).is_nonnegative is None
  130. assert (floor(7, evaluate=False) >= 7) == True
  131. assert (floor(7, evaluate=False) > 7) == False
  132. assert (floor(7, evaluate=False) <= 7) == True
  133. assert (floor(7, evaluate=False) < 7) == False
  134. assert (floor(7, evaluate=False) >= 6) == True
  135. assert (floor(7, evaluate=False) > 6) == True
  136. assert (floor(7, evaluate=False) <= 6) == False
  137. assert (floor(7, evaluate=False) < 6) == False
  138. assert (floor(7, evaluate=False) >= 8) == False
  139. assert (floor(7, evaluate=False) > 8) == False
  140. assert (floor(7, evaluate=False) <= 8) == True
  141. assert (floor(7, evaluate=False) < 8) == True
  142. assert (floor(x) <= 5.5) == Le(floor(x), 5.5, evaluate=False)
  143. assert (floor(x) >= -3.2) == Ge(floor(x), -3.2, evaluate=False)
  144. assert (floor(x) < 2.9) == Lt(floor(x), 2.9, evaluate=False)
  145. assert (floor(x) > -1.7) == Gt(floor(x), -1.7, evaluate=False)
  146. assert (floor(y) <= 5.5) == (y < 6)
  147. assert (floor(y) >= -3.2) == (y >= -3)
  148. assert (floor(y) < 2.9) == (y < 3)
  149. assert (floor(y) > -1.7) == (y >= -1)
  150. assert (floor(y) <= n) == (y < n + 1)
  151. assert (floor(y) >= n) == (y >= n)
  152. assert (floor(y) < n) == (y < n)
  153. assert (floor(y) > n) == (y >= n + 1)
  154. def test_ceiling():
  155. assert ceiling(nan) is nan
  156. assert ceiling(oo) is oo
  157. assert ceiling(-oo) is -oo
  158. assert ceiling(zoo) is zoo
  159. assert ceiling(0) == 0
  160. assert ceiling(1) == 1
  161. assert ceiling(-1) == -1
  162. assert ceiling(E) == 3
  163. assert ceiling(-E) == -2
  164. assert ceiling(2*E) == 6
  165. assert ceiling(-2*E) == -5
  166. assert ceiling(pi) == 4
  167. assert ceiling(-pi) == -3
  168. assert ceiling(S.Half) == 1
  169. assert ceiling(Rational(-1, 2)) == 0
  170. assert ceiling(Rational(7, 3)) == 3
  171. assert ceiling(-Rational(7, 3)) == -2
  172. assert ceiling(Float(17.0)) == 17
  173. assert ceiling(-Float(17.0)) == -17
  174. assert ceiling(Float(7.69)) == 8
  175. assert ceiling(-Float(7.69)) == -7
  176. assert ceiling(I) == I
  177. assert ceiling(-I) == -I
  178. e = ceiling(i)
  179. assert e.func is ceiling and e.args[0] == i
  180. assert ceiling(oo*I) == oo*I
  181. assert ceiling(-oo*I) == -oo*I
  182. assert ceiling(exp(I*pi/4)*oo) == exp(I*pi/4)*oo
  183. assert ceiling(2*I) == 2*I
  184. assert ceiling(-2*I) == -2*I
  185. assert ceiling(I/2) == I
  186. assert ceiling(-I/2) == 0
  187. assert ceiling(E + 17) == 20
  188. assert ceiling(pi + 2) == 6
  189. assert ceiling(E + pi) == 6
  190. assert ceiling(I + pi) == I + 4
  191. assert ceiling(ceiling(pi)) == 4
  192. assert ceiling(ceiling(y)) == ceiling(y)
  193. assert ceiling(ceiling(x)) == ceiling(x)
  194. assert unchanged(ceiling, x)
  195. assert unchanged(ceiling, 2*x)
  196. assert unchanged(ceiling, k*x)
  197. assert ceiling(k) == k
  198. assert ceiling(2*k) == 2*k
  199. assert ceiling(k*n) == k*n
  200. assert unchanged(ceiling, k/2)
  201. assert unchanged(ceiling, x + y)
  202. assert ceiling(x + 3) == ceiling(x) + 3
  203. assert ceiling(x + k) == ceiling(x) + k
  204. assert ceiling(y + 3) == ceiling(y) + 3
  205. assert ceiling(y + k) == ceiling(y) + k
  206. assert ceiling(3 + pi + y*I) == 7 + ceiling(y)*I
  207. assert ceiling(k + n) == k + n
  208. assert unchanged(ceiling, x*I)
  209. assert ceiling(k*I) == k*I
  210. assert ceiling(Rational(23, 10) - E*I) == 3 - 2*I
  211. assert ceiling(sin(1)) == 1
  212. assert ceiling(sin(-1)) == 0
  213. assert ceiling(exp(2)) == 8
  214. assert ceiling(-log(8)/log(2)) != -2
  215. assert int(ceiling(-log(8)/log(2)).evalf(chop=True)) == -3
  216. assert ceiling(factorial(50)/exp(1)) == \
  217. 11188719610782480504630258070757734324011354208865721592720336801
  218. assert (ceiling(y) >= y) == True
  219. assert (ceiling(y) > y) == False
  220. assert (ceiling(y) < y) == False
  221. assert (ceiling(y) <= y) == False
  222. assert (ceiling(x) >= x).is_Relational # x could be non-real
  223. assert (ceiling(x) < x).is_Relational
  224. assert (ceiling(x) >= y).is_Relational # arg is not same as rhs
  225. assert (ceiling(x) < y).is_Relational
  226. assert (ceiling(y) >= -oo) == True
  227. assert (ceiling(y) > -oo) == True
  228. assert (ceiling(y) <= oo) == True
  229. assert (ceiling(y) < oo) == True
  230. assert ceiling(y).rewrite(floor) == -floor(-y)
  231. assert ceiling(y).rewrite(frac) == y + frac(-y)
  232. assert ceiling(y).rewrite(floor).subs(y, -pi) == -floor(pi)
  233. assert ceiling(y).rewrite(floor).subs(y, E) == -floor(-E)
  234. assert ceiling(y).rewrite(frac).subs(y, pi) == ceiling(pi)
  235. assert ceiling(y).rewrite(frac).subs(y, -E) == ceiling(-E)
  236. assert Eq(ceiling(y), y + frac(-y))
  237. assert Eq(ceiling(y), -floor(-y))
  238. neg = Symbol('neg', negative=True)
  239. nn = Symbol('nn', nonnegative=True)
  240. pos = Symbol('pos', positive=True)
  241. np = Symbol('np', nonpositive=True)
  242. assert (ceiling(neg) <= 0) == True
  243. assert (ceiling(neg) < 0) == (neg <= -1)
  244. assert (ceiling(neg) > 0) == False
  245. assert (ceiling(neg) >= 0) == (neg > -1)
  246. assert (ceiling(neg) > -3) == (neg > -3)
  247. assert (ceiling(neg) <= 10) == (neg <= 10)
  248. assert (ceiling(nn) < 0) == False
  249. assert (ceiling(nn) >= 0) == True
  250. assert (ceiling(pos) < 0) == False
  251. assert (ceiling(pos) <= 0) == False
  252. assert (ceiling(pos) > 0) == True
  253. assert (ceiling(pos) >= 0) == True
  254. assert (ceiling(pos) >= 1) == True
  255. assert (ceiling(pos) > 5) == (pos > 5)
  256. assert (ceiling(np) <= 0) == True
  257. assert (ceiling(np) > 0) == False
  258. assert ceiling(neg).is_positive == False
  259. assert ceiling(neg).is_nonpositive == True
  260. assert ceiling(nn).is_positive is None
  261. assert ceiling(nn).is_nonpositive is None
  262. assert ceiling(pos).is_positive == True
  263. assert ceiling(pos).is_nonpositive == False
  264. assert ceiling(np).is_positive == False
  265. assert ceiling(np).is_nonpositive == True
  266. assert (ceiling(7, evaluate=False) >= 7) == True
  267. assert (ceiling(7, evaluate=False) > 7) == False
  268. assert (ceiling(7, evaluate=False) <= 7) == True
  269. assert (ceiling(7, evaluate=False) < 7) == False
  270. assert (ceiling(7, evaluate=False) >= 6) == True
  271. assert (ceiling(7, evaluate=False) > 6) == True
  272. assert (ceiling(7, evaluate=False) <= 6) == False
  273. assert (ceiling(7, evaluate=False) < 6) == False
  274. assert (ceiling(7, evaluate=False) >= 8) == False
  275. assert (ceiling(7, evaluate=False) > 8) == False
  276. assert (ceiling(7, evaluate=False) <= 8) == True
  277. assert (ceiling(7, evaluate=False) < 8) == True
  278. assert (ceiling(x) <= 5.5) == Le(ceiling(x), 5.5, evaluate=False)
  279. assert (ceiling(x) >= -3.2) == Ge(ceiling(x), -3.2, evaluate=False)
  280. assert (ceiling(x) < 2.9) == Lt(ceiling(x), 2.9, evaluate=False)
  281. assert (ceiling(x) > -1.7) == Gt(ceiling(x), -1.7, evaluate=False)
  282. assert (ceiling(y) <= 5.5) == (y <= 5)
  283. assert (ceiling(y) >= -3.2) == (y > -4)
  284. assert (ceiling(y) < 2.9) == (y <= 2)
  285. assert (ceiling(y) > -1.7) == (y > -2)
  286. assert (ceiling(y) <= n) == (y <= n)
  287. assert (ceiling(y) >= n) == (y > n - 1)
  288. assert (ceiling(y) < n) == (y <= n - 1)
  289. assert (ceiling(y) > n) == (y > n)
  290. def test_frac():
  291. assert isinstance(frac(x), frac)
  292. assert frac(oo) == AccumBounds(0, 1)
  293. assert frac(-oo) == AccumBounds(0, 1)
  294. assert frac(zoo) is nan
  295. assert frac(n) == 0
  296. assert frac(nan) is nan
  297. assert frac(Rational(4, 3)) == Rational(1, 3)
  298. assert frac(-Rational(4, 3)) == Rational(2, 3)
  299. assert frac(Rational(-4, 3)) == Rational(2, 3)
  300. r = Symbol('r', real=True)
  301. assert frac(I*r) == I*frac(r)
  302. assert frac(1 + I*r) == I*frac(r)
  303. assert frac(0.5 + I*r) == 0.5 + I*frac(r)
  304. assert frac(n + I*r) == I*frac(r)
  305. assert frac(n + I*k) == 0
  306. assert unchanged(frac, x + I*x)
  307. assert frac(x + I*n) == frac(x)
  308. assert frac(x).rewrite(floor) == x - floor(x)
  309. assert frac(x).rewrite(ceiling) == x + ceiling(-x)
  310. assert frac(y).rewrite(floor).subs(y, pi) == frac(pi)
  311. assert frac(y).rewrite(floor).subs(y, -E) == frac(-E)
  312. assert frac(y).rewrite(ceiling).subs(y, -pi) == frac(-pi)
  313. assert frac(y).rewrite(ceiling).subs(y, E) == frac(E)
  314. assert Eq(frac(y), y - floor(y))
  315. assert Eq(frac(y), y + ceiling(-y))
  316. r = Symbol('r', real=True)
  317. p_i = Symbol('p_i', integer=True, positive=True)
  318. n_i = Symbol('p_i', integer=True, negative=True)
  319. np_i = Symbol('np_i', integer=True, nonpositive=True)
  320. nn_i = Symbol('nn_i', integer=True, nonnegative=True)
  321. p_r = Symbol('p_r', positive=True)
  322. n_r = Symbol('n_r', negative=True)
  323. np_r = Symbol('np_r', real=True, nonpositive=True)
  324. nn_r = Symbol('nn_r', real=True, nonnegative=True)
  325. # Real frac argument, integer rhs
  326. assert frac(r) <= p_i
  327. assert not frac(r) <= n_i
  328. assert (frac(r) <= np_i).has(Le)
  329. assert (frac(r) <= nn_i).has(Le)
  330. assert frac(r) < p_i
  331. assert not frac(r) < n_i
  332. assert not frac(r) < np_i
  333. assert (frac(r) < nn_i).has(Lt)
  334. assert not frac(r) >= p_i
  335. assert frac(r) >= n_i
  336. assert frac(r) >= np_i
  337. assert (frac(r) >= nn_i).has(Ge)
  338. assert not frac(r) > p_i
  339. assert frac(r) > n_i
  340. assert (frac(r) > np_i).has(Gt)
  341. assert (frac(r) > nn_i).has(Gt)
  342. assert not Eq(frac(r), p_i)
  343. assert not Eq(frac(r), n_i)
  344. assert Eq(frac(r), np_i).has(Eq)
  345. assert Eq(frac(r), nn_i).has(Eq)
  346. assert Ne(frac(r), p_i)
  347. assert Ne(frac(r), n_i)
  348. assert Ne(frac(r), np_i).has(Ne)
  349. assert Ne(frac(r), nn_i).has(Ne)
  350. # Real frac argument, real rhs
  351. assert (frac(r) <= p_r).has(Le)
  352. assert not frac(r) <= n_r
  353. assert (frac(r) <= np_r).has(Le)
  354. assert (frac(r) <= nn_r).has(Le)
  355. assert (frac(r) < p_r).has(Lt)
  356. assert not frac(r) < n_r
  357. assert not frac(r) < np_r
  358. assert (frac(r) < nn_r).has(Lt)
  359. assert (frac(r) >= p_r).has(Ge)
  360. assert frac(r) >= n_r
  361. assert frac(r) >= np_r
  362. assert (frac(r) >= nn_r).has(Ge)
  363. assert (frac(r) > p_r).has(Gt)
  364. assert frac(r) > n_r
  365. assert (frac(r) > np_r).has(Gt)
  366. assert (frac(r) > nn_r).has(Gt)
  367. assert not Eq(frac(r), n_r)
  368. assert Eq(frac(r), p_r).has(Eq)
  369. assert Eq(frac(r), np_r).has(Eq)
  370. assert Eq(frac(r), nn_r).has(Eq)
  371. assert Ne(frac(r), p_r).has(Ne)
  372. assert Ne(frac(r), n_r)
  373. assert Ne(frac(r), np_r).has(Ne)
  374. assert Ne(frac(r), nn_r).has(Ne)
  375. # Real frac argument, +/- oo rhs
  376. assert frac(r) < oo
  377. assert frac(r) <= oo
  378. assert not frac(r) > oo
  379. assert not frac(r) >= oo
  380. assert not frac(r) < -oo
  381. assert not frac(r) <= -oo
  382. assert frac(r) > -oo
  383. assert frac(r) >= -oo
  384. assert frac(r) < 1
  385. assert frac(r) <= 1
  386. assert not frac(r) > 1
  387. assert not frac(r) >= 1
  388. assert not frac(r) < 0
  389. assert (frac(r) <= 0).has(Le)
  390. assert (frac(r) > 0).has(Gt)
  391. assert frac(r) >= 0
  392. # Some test for numbers
  393. assert frac(r) <= sqrt(2)
  394. assert (frac(r) <= sqrt(3) - sqrt(2)).has(Le)
  395. assert not frac(r) <= sqrt(2) - sqrt(3)
  396. assert not frac(r) >= sqrt(2)
  397. assert (frac(r) >= sqrt(3) - sqrt(2)).has(Ge)
  398. assert frac(r) >= sqrt(2) - sqrt(3)
  399. assert not Eq(frac(r), sqrt(2))
  400. assert Eq(frac(r), sqrt(3) - sqrt(2)).has(Eq)
  401. assert not Eq(frac(r), sqrt(2) - sqrt(3))
  402. assert Ne(frac(r), sqrt(2))
  403. assert Ne(frac(r), sqrt(3) - sqrt(2)).has(Ne)
  404. assert Ne(frac(r), sqrt(2) - sqrt(3))
  405. assert frac(p_i, evaluate=False).is_zero
  406. assert frac(p_i, evaluate=False).is_finite
  407. assert frac(p_i, evaluate=False).is_integer
  408. assert frac(p_i, evaluate=False).is_real
  409. assert frac(r).is_finite
  410. assert frac(r).is_real
  411. assert frac(r).is_zero is None
  412. assert frac(r).is_integer is None
  413. assert frac(oo).is_finite
  414. assert frac(oo).is_real
  415. def test_series():
  416. x, y = symbols('x,y')
  417. assert floor(x).nseries(x, y, 100) == floor(y)
  418. assert ceiling(x).nseries(x, y, 100) == ceiling(y)
  419. assert floor(x).nseries(x, pi, 100) == 3
  420. assert ceiling(x).nseries(x, pi, 100) == 4
  421. assert floor(x).nseries(x, 0, 100) == 0
  422. assert ceiling(x).nseries(x, 0, 100) == 1
  423. assert floor(-x).nseries(x, 0, 100) == -1
  424. assert ceiling(-x).nseries(x, 0, 100) == 0
  425. def test_issue_14355():
  426. # This test checks the leading term and series for the floor and ceil
  427. # function when arg0 evaluates to S.NaN.
  428. assert floor((x**3 + x)/(x**2 - x)).as_leading_term(x, cdir = 1) == -2
  429. assert floor((x**3 + x)/(x**2 - x)).as_leading_term(x, cdir = -1) == -1
  430. assert floor((cos(x) - 1)/x).as_leading_term(x, cdir = 1) == -1
  431. assert floor((cos(x) - 1)/x).as_leading_term(x, cdir = -1) == 0
  432. assert floor(sin(x)/x).as_leading_term(x, cdir = 1) == 0
  433. assert floor(sin(x)/x).as_leading_term(x, cdir = -1) == 0
  434. assert floor(-tan(x)/x).as_leading_term(x, cdir = 1) == -2
  435. assert floor(-tan(x)/x).as_leading_term(x, cdir = -1) == -2
  436. assert floor(sin(x)/x/3).as_leading_term(x, cdir = 1) == 0
  437. assert floor(sin(x)/x/3).as_leading_term(x, cdir = -1) == 0
  438. assert ceiling((x**3 + x)/(x**2 - x)).as_leading_term(x, cdir = 1) == -1
  439. assert ceiling((x**3 + x)/(x**2 - x)).as_leading_term(x, cdir = -1) == 0
  440. assert ceiling((cos(x) - 1)/x).as_leading_term(x, cdir = 1) == 0
  441. assert ceiling((cos(x) - 1)/x).as_leading_term(x, cdir = -1) == 1
  442. assert ceiling(sin(x)/x).as_leading_term(x, cdir = 1) == 1
  443. assert ceiling(sin(x)/x).as_leading_term(x, cdir = -1) == 1
  444. assert ceiling(-tan(x)/x).as_leading_term(x, cdir = 1) == -1
  445. assert ceiling(-tan(x)/x).as_leading_term(x, cdir = 1) == -1
  446. assert ceiling(sin(x)/x/3).as_leading_term(x, cdir = 1) == 1
  447. assert ceiling(sin(x)/x/3).as_leading_term(x, cdir = -1) == 1
  448. # test for series
  449. assert floor(sin(x)/x).series(x, 0, 100, cdir = 1) == 0
  450. assert floor(sin(x)/x).series(x, 0, 100, cdir = 1) == 0
  451. assert floor((x**3 + x)/(x**2 - x)).series(x, 0, 100, cdir = 1) == -2
  452. assert floor((x**3 + x)/(x**2 - x)).series(x, 0, 100, cdir = -1) == -1
  453. assert ceiling(sin(x)/x).series(x, 0, 100, cdir = 1) == 1
  454. assert ceiling(sin(x)/x).series(x, 0, 100, cdir = -1) == 1
  455. assert ceiling((x**3 + x)/(x**2 - x)).series(x, 0, 100, cdir = 1) == -1
  456. assert ceiling((x**3 + x)/(x**2 - x)).series(x, 0, 100, cdir = -1) == 0
  457. def test_frac_leading_term():
  458. assert frac(x).as_leading_term(x) == x
  459. assert frac(x).as_leading_term(x, cdir = 1) == x
  460. assert frac(x).as_leading_term(x, cdir = -1) == 1
  461. assert frac(x + S.Half).as_leading_term(x, cdir = 1) == S.Half
  462. assert frac(x + S.Half).as_leading_term(x, cdir = -1) == S.Half
  463. assert frac(-2*x + 1).as_leading_term(x, cdir = 1) == S.One
  464. assert frac(-2*x + 1).as_leading_term(x, cdir = -1) == -2*x
  465. assert frac(sin(x) + 5).as_leading_term(x, cdir = 1) == x
  466. assert frac(sin(x) + 5).as_leading_term(x, cdir = -1) == S.One
  467. assert frac(sin(x**2) + 5).as_leading_term(x, cdir = 1) == x**2
  468. assert frac(sin(x**2) + 5).as_leading_term(x, cdir = -1) == x**2
  469. @XFAIL
  470. def test_issue_4149():
  471. assert floor(3 + pi*I + y*I) == 3 + floor(pi + y)*I
  472. assert floor(3*I + pi*I + y*I) == floor(3 + pi + y)*I
  473. assert floor(3 + E + pi*I + y*I) == 5 + floor(pi + y)*I
  474. def test_issue_21651():
  475. k = Symbol('k', positive=True, integer=True)
  476. exp = 2*2**(-k)
  477. assert isinstance(floor(exp), floor)
  478. def test_issue_11207():
  479. assert floor(floor(x)) == floor(x)
  480. assert floor(ceiling(x)) == ceiling(x)
  481. assert ceiling(floor(x)) == floor(x)
  482. assert ceiling(ceiling(x)) == ceiling(x)
  483. def test_nested_floor_ceiling():
  484. assert floor(-floor(ceiling(x**3)/y)) == -floor(ceiling(x**3)/y)
  485. assert ceiling(-floor(ceiling(x**3)/y)) == -floor(ceiling(x**3)/y)
  486. assert floor(ceiling(-floor(x**Rational(7, 2)/y))) == -floor(x**Rational(7, 2)/y)
  487. assert -ceiling(-ceiling(floor(x)/y)) == ceiling(floor(x)/y)
  488. def test_issue_18689():
  489. assert floor(floor(floor(x)) + 3) == floor(x) + 3
  490. assert ceiling(ceiling(ceiling(x)) + 1) == ceiling(x) + 1
  491. assert ceiling(ceiling(floor(x)) + 3) == floor(x) + 3
  492. def test_issue_18421():
  493. assert floor(float(0)) is S.Zero
  494. assert ceiling(float(0)) is S.Zero