test_integers.py 23 KB

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