test_interval.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. from mpmath import *
  2. def test_interval_identity():
  3. iv.dps = 15
  4. assert mpi(2) == mpi(2, 2)
  5. assert mpi(2) != mpi(-2, 2)
  6. assert not (mpi(2) != mpi(2, 2))
  7. assert mpi(-1, 1) == mpi(-1, 1)
  8. assert str(mpi('0.1')) == "[0.099999999999999991673, 0.10000000000000000555]"
  9. assert repr(mpi('0.1')) == "mpi('0.099999999999999992', '0.10000000000000001')"
  10. u = mpi(-1, 3)
  11. assert -1 in u
  12. assert 2 in u
  13. assert 3 in u
  14. assert -1.1 not in u
  15. assert 3.1 not in u
  16. assert mpi(-1, 3) in u
  17. assert mpi(0, 1) in u
  18. assert mpi(-1.1, 2) not in u
  19. assert mpi(2.5, 3.1) not in u
  20. w = mpi(-inf, inf)
  21. assert mpi(-5, 5) in w
  22. assert mpi(2, inf) in w
  23. assert mpi(0, 2) in mpi(0, 10)
  24. assert not (3 in mpi(-inf, 0))
  25. def test_interval_hash():
  26. assert hash(mpi(3)) == hash(3)
  27. assert hash(mpi(3.25)) == hash(3.25)
  28. assert hash(mpi(3,4)) == hash(mpi(3,4))
  29. assert hash(iv.mpc(3)) == hash(3)
  30. assert hash(iv.mpc(3,4)) == hash(3+4j)
  31. assert hash(iv.mpc((1,3),(2,4))) == hash(iv.mpc((1,3),(2,4)))
  32. def test_interval_arithmetic():
  33. iv.dps = 15
  34. assert mpi(2) + mpi(3,4) == mpi(5,6)
  35. assert mpi(1, 2)**2 == mpi(1, 4)
  36. assert mpi(1) + mpi(0, 1e-50) == mpi(1, mpf('1.0000000000000002'))
  37. x = 1 / (1 / mpi(3))
  38. assert x.a < 3 < x.b
  39. x = mpi(2) ** mpi(0.5)
  40. iv.dps += 5
  41. sq = iv.sqrt(2)
  42. iv.dps -= 5
  43. assert x.a < sq < x.b
  44. assert mpi(1) / mpi(1, inf)
  45. assert mpi(2, 3) / inf == mpi(0, 0)
  46. assert mpi(0) / inf == 0
  47. assert mpi(0) / 0 == mpi(-inf, inf)
  48. assert mpi(inf) / 0 == mpi(-inf, inf)
  49. assert mpi(0) * inf == mpi(-inf, inf)
  50. assert 1 / mpi(2, inf) == mpi(0, 0.5)
  51. assert str((mpi(50, 50) * mpi(-10, -10)) / 3) == \
  52. '[-166.66666666666668561, -166.66666666666665719]'
  53. assert mpi(0, 4) ** 3 == mpi(0, 64)
  54. assert mpi(2,4).mid == 3
  55. iv.dps = 30
  56. a = mpi(iv.pi)
  57. iv.dps = 15
  58. b = +a
  59. assert b.a < a.a
  60. assert b.b > a.b
  61. a = mpi(iv.pi)
  62. assert a == +a
  63. assert abs(mpi(-1,2)) == mpi(0,2)
  64. assert abs(mpi(0.5,2)) == mpi(0.5,2)
  65. assert abs(mpi(-3,2)) == mpi(0,3)
  66. assert abs(mpi(-3,-0.5)) == mpi(0.5,3)
  67. assert mpi(0) * mpi(2,3) == mpi(0)
  68. assert mpi(2,3) * mpi(0) == mpi(0)
  69. assert mpi(1,3).delta == 2
  70. assert mpi(1,2) - mpi(3,4) == mpi(-3,-1)
  71. assert mpi(-inf,0) - mpi(0,inf) == mpi(-inf,0)
  72. assert mpi(-inf,0) - mpi(-inf,inf) == mpi(-inf,inf)
  73. assert mpi(0,inf) - mpi(-inf,1) == mpi(-1,inf)
  74. def test_interval_mul():
  75. assert mpi(-1, 0) * inf == mpi(-inf, 0)
  76. assert mpi(-1, 0) * -inf == mpi(0, inf)
  77. assert mpi(0, 1) * inf == mpi(0, inf)
  78. assert mpi(0, 1) * mpi(0, inf) == mpi(0, inf)
  79. assert mpi(-1, 1) * inf == mpi(-inf, inf)
  80. assert mpi(-1, 1) * mpi(0, inf) == mpi(-inf, inf)
  81. assert mpi(-1, 1) * mpi(-inf, inf) == mpi(-inf, inf)
  82. assert mpi(-inf, 0) * mpi(0, 1) == mpi(-inf, 0)
  83. assert mpi(-inf, 0) * mpi(0, 0) * mpi(-inf, 0)
  84. assert mpi(-inf, 0) * mpi(-inf, inf) == mpi(-inf, inf)
  85. assert mpi(-5,0)*mpi(-32,28) == mpi(-140,160)
  86. assert mpi(2,3) * mpi(-1,2) == mpi(-3,6)
  87. # Should be undefined?
  88. assert mpi(inf, inf) * 0 == mpi(-inf, inf)
  89. assert mpi(-inf, -inf) * 0 == mpi(-inf, inf)
  90. assert mpi(0) * mpi(-inf,2) == mpi(-inf,inf)
  91. assert mpi(0) * mpi(-2,inf) == mpi(-inf,inf)
  92. assert mpi(-2,inf) * mpi(0) == mpi(-inf,inf)
  93. assert mpi(-inf,2) * mpi(0) == mpi(-inf,inf)
  94. def test_interval_pow():
  95. assert mpi(3)**2 == mpi(9, 9)
  96. assert mpi(-3)**2 == mpi(9, 9)
  97. assert mpi(-3, 1)**2 == mpi(0, 9)
  98. assert mpi(-3, -1)**2 == mpi(1, 9)
  99. assert mpi(-3, -1)**3 == mpi(-27, -1)
  100. assert mpi(-3, 1)**3 == mpi(-27, 1)
  101. assert mpi(-2, 3)**2 == mpi(0, 9)
  102. assert mpi(-3, 2)**2 == mpi(0, 9)
  103. assert mpi(4) ** -1 == mpi(0.25, 0.25)
  104. assert mpi(-4) ** -1 == mpi(-0.25, -0.25)
  105. assert mpi(4) ** -2 == mpi(0.0625, 0.0625)
  106. assert mpi(-4) ** -2 == mpi(0.0625, 0.0625)
  107. assert mpi(0, 1) ** inf == mpi(0, 1)
  108. assert mpi(0, 1) ** -inf == mpi(1, inf)
  109. assert mpi(0, inf) ** inf == mpi(0, inf)
  110. assert mpi(0, inf) ** -inf == mpi(0, inf)
  111. assert mpi(1, inf) ** inf == mpi(1, inf)
  112. assert mpi(1, inf) ** -inf == mpi(0, 1)
  113. assert mpi(2, 3) ** 1 == mpi(2, 3)
  114. assert mpi(2, 3) ** 0 == 1
  115. assert mpi(1,3) ** mpi(2) == mpi(1,9)
  116. def test_interval_sqrt():
  117. assert mpi(4) ** 0.5 == mpi(2)
  118. def test_interval_div():
  119. assert mpi(0.5, 1) / mpi(-1, 0) == mpi(-inf, -0.5)
  120. assert mpi(0, 1) / mpi(0, 1) == mpi(0, inf)
  121. assert mpi(inf, inf) / mpi(inf, inf) == mpi(0, inf)
  122. assert mpi(inf, inf) / mpi(2, inf) == mpi(0, inf)
  123. assert mpi(inf, inf) / mpi(2, 2) == mpi(inf, inf)
  124. assert mpi(0, inf) / mpi(2, inf) == mpi(0, inf)
  125. assert mpi(0, inf) / mpi(2, 2) == mpi(0, inf)
  126. assert mpi(2, inf) / mpi(2, 2) == mpi(1, inf)
  127. assert mpi(2, inf) / mpi(2, inf) == mpi(0, inf)
  128. assert mpi(-4, 8) / mpi(1, inf) == mpi(-4, 8)
  129. assert mpi(-4, 8) / mpi(0.5, inf) == mpi(-8, 16)
  130. assert mpi(-inf, 8) / mpi(0.5, inf) == mpi(-inf, 16)
  131. assert mpi(-inf, inf) / mpi(0.5, inf) == mpi(-inf, inf)
  132. assert mpi(8, inf) / mpi(0.5, inf) == mpi(0, inf)
  133. assert mpi(-8, inf) / mpi(0.5, inf) == mpi(-16, inf)
  134. assert mpi(-4, 8) / mpi(inf, inf) == mpi(0, 0)
  135. assert mpi(0, 8) / mpi(inf, inf) == mpi(0, 0)
  136. assert mpi(0, 0) / mpi(inf, inf) == mpi(0, 0)
  137. assert mpi(-inf, 0) / mpi(inf, inf) == mpi(-inf, 0)
  138. assert mpi(-inf, 8) / mpi(inf, inf) == mpi(-inf, 0)
  139. assert mpi(-inf, inf) / mpi(inf, inf) == mpi(-inf, inf)
  140. assert mpi(-8, inf) / mpi(inf, inf) == mpi(0, inf)
  141. assert mpi(0, inf) / mpi(inf, inf) == mpi(0, inf)
  142. assert mpi(8, inf) / mpi(inf, inf) == mpi(0, inf)
  143. assert mpi(inf, inf) / mpi(inf, inf) == mpi(0, inf)
  144. assert mpi(-1, 2) / mpi(0, 1) == mpi(-inf, +inf)
  145. assert mpi(0, 1) / mpi(0, 1) == mpi(0.0, +inf)
  146. assert mpi(-1, 0) / mpi(0, 1) == mpi(-inf, 0.0)
  147. assert mpi(-0.5, -0.25) / mpi(0, 1) == mpi(-inf, -0.25)
  148. assert mpi(0.5, 1) / mpi(0, 1) == mpi(0.5, +inf)
  149. assert mpi(0.5, 4) / mpi(0, 1) == mpi(0.5, +inf)
  150. assert mpi(-1, -0.5) / mpi(0, 1) == mpi(-inf, -0.5)
  151. assert mpi(-4, -0.5) / mpi(0, 1) == mpi(-inf, -0.5)
  152. assert mpi(-1, 2) / mpi(-2, 0.5) == mpi(-inf, +inf)
  153. assert mpi(0, 1) / mpi(-2, 0.5) == mpi(-inf, +inf)
  154. assert mpi(-1, 0) / mpi(-2, 0.5) == mpi(-inf, +inf)
  155. assert mpi(-0.5, -0.25) / mpi(-2, 0.5) == mpi(-inf, +inf)
  156. assert mpi(0.5, 1) / mpi(-2, 0.5) == mpi(-inf, +inf)
  157. assert mpi(0.5, 4) / mpi(-2, 0.5) == mpi(-inf, +inf)
  158. assert mpi(-1, -0.5) / mpi(-2, 0.5) == mpi(-inf, +inf)
  159. assert mpi(-4, -0.5) / mpi(-2, 0.5) == mpi(-inf, +inf)
  160. assert mpi(-1, 2) / mpi(-1, 0) == mpi(-inf, +inf)
  161. assert mpi(0, 1) / mpi(-1, 0) == mpi(-inf, 0.0)
  162. assert mpi(-1, 0) / mpi(-1, 0) == mpi(0.0, +inf)
  163. assert mpi(-0.5, -0.25) / mpi(-1, 0) == mpi(0.25, +inf)
  164. assert mpi(0.5, 1) / mpi(-1, 0) == mpi(-inf, -0.5)
  165. assert mpi(0.5, 4) / mpi(-1, 0) == mpi(-inf, -0.5)
  166. assert mpi(-1, -0.5) / mpi(-1, 0) == mpi(0.5, +inf)
  167. assert mpi(-4, -0.5) / mpi(-1, 0) == mpi(0.5, +inf)
  168. assert mpi(-1, 2) / mpi(0.5, 1) == mpi(-2.0, 4.0)
  169. assert mpi(0, 1) / mpi(0.5, 1) == mpi(0.0, 2.0)
  170. assert mpi(-1, 0) / mpi(0.5, 1) == mpi(-2.0, 0.0)
  171. assert mpi(-0.5, -0.25) / mpi(0.5, 1) == mpi(-1.0, -0.25)
  172. assert mpi(0.5, 1) / mpi(0.5, 1) == mpi(0.5, 2.0)
  173. assert mpi(0.5, 4) / mpi(0.5, 1) == mpi(0.5, 8.0)
  174. assert mpi(-1, -0.5) / mpi(0.5, 1) == mpi(-2.0, -0.5)
  175. assert mpi(-4, -0.5) / mpi(0.5, 1) == mpi(-8.0, -0.5)
  176. assert mpi(-1, 2) / mpi(-2, -0.5) == mpi(-4.0, 2.0)
  177. assert mpi(0, 1) / mpi(-2, -0.5) == mpi(-2.0, 0.0)
  178. assert mpi(-1, 0) / mpi(-2, -0.5) == mpi(0.0, 2.0)
  179. assert mpi(-0.5, -0.25) / mpi(-2, -0.5) == mpi(0.125, 1.0)
  180. assert mpi(0.5, 1) / mpi(-2, -0.5) == mpi(-2.0, -0.25)
  181. assert mpi(0.5, 4) / mpi(-2, -0.5) == mpi(-8.0, -0.25)
  182. assert mpi(-1, -0.5) / mpi(-2, -0.5) == mpi(0.25, 2.0)
  183. assert mpi(-4, -0.5) / mpi(-2, -0.5) == mpi(0.25, 8.0)
  184. # Should be undefined?
  185. assert mpi(0, 0) / mpi(0, 0) == mpi(-inf, inf)
  186. assert mpi(0, 0) / mpi(0, 1) == mpi(-inf, inf)
  187. def test_interval_cos_sin():
  188. iv.dps = 15
  189. cos = iv.cos
  190. sin = iv.sin
  191. tan = iv.tan
  192. pi = iv.pi
  193. # Around 0
  194. assert cos(mpi(0)) == 1
  195. assert sin(mpi(0)) == 0
  196. assert cos(mpi(0,1)) == mpi(0.54030230586813965399, 1.0)
  197. assert sin(mpi(0,1)) == mpi(0, 0.8414709848078966159)
  198. assert cos(mpi(1,2)) == mpi(-0.4161468365471424069, 0.54030230586813976501)
  199. assert sin(mpi(1,2)) == mpi(0.84147098480789650488, 1.0)
  200. assert sin(mpi(1,2.5)) == mpi(0.59847214410395643824, 1.0)
  201. assert cos(mpi(-1, 1)) == mpi(0.54030230586813965399, 1.0)
  202. assert cos(mpi(-1, 0.5)) == mpi(0.54030230586813965399, 1.0)
  203. assert cos(mpi(-1, 1.5)) == mpi(0.070737201667702906405, 1.0)
  204. assert sin(mpi(-1,1)) == mpi(-0.8414709848078966159, 0.8414709848078966159)
  205. assert sin(mpi(-1,0.5)) == mpi(-0.8414709848078966159, 0.47942553860420300538)
  206. assert mpi(-0.8414709848078966159, 1.00000000000000002e-100) in sin(mpi(-1,1e-100))
  207. assert mpi(-2.00000000000000004e-100, 1.00000000000000002e-100) in sin(mpi(-2e-100,1e-100))
  208. # Same interval
  209. assert cos(mpi(2, 2.5))
  210. assert cos(mpi(3.5, 4)) == mpi(-0.93645668729079634129, -0.65364362086361182946)
  211. assert cos(mpi(5, 5.5)) == mpi(0.28366218546322624627, 0.70866977429126010168)
  212. assert mpi(0.59847214410395654927, 0.90929742682568170942) in sin(mpi(2, 2.5))
  213. assert sin(mpi(3.5, 4)) == mpi(-0.75680249530792831347, -0.35078322768961983646)
  214. assert sin(mpi(5, 5.5)) == mpi(-0.95892427466313856499, -0.70554032557039181306)
  215. # Higher roots
  216. iv.dps = 55
  217. w = 4*10**50 + mpi(0.5)
  218. for p in [15, 40, 80]:
  219. iv.dps = p
  220. assert 0 in sin(4*mpi(pi))
  221. assert 0 in sin(4*10**50*mpi(pi))
  222. assert 0 in cos((4+0.5)*mpi(pi))
  223. assert 0 in cos(w*mpi(pi))
  224. assert 1 in cos(4*mpi(pi))
  225. assert 1 in cos(4*10**50*mpi(pi))
  226. iv.dps = 15
  227. assert cos(mpi(2,inf)) == mpi(-1,1)
  228. assert sin(mpi(2,inf)) == mpi(-1,1)
  229. assert cos(mpi(-inf,2)) == mpi(-1,1)
  230. assert sin(mpi(-inf,2)) == mpi(-1,1)
  231. u = tan(mpi(0.5,1))
  232. assert mpf(u.a).ae(mp.tan(0.5))
  233. assert mpf(u.b).ae(mp.tan(1))
  234. v = iv.cot(mpi(0.5,1))
  235. assert mpf(v.a).ae(mp.cot(1))
  236. assert mpf(v.b).ae(mp.cot(0.5))
  237. # Sanity check of evaluation at n*pi and (n+1/2)*pi
  238. for n in range(-5,7,2):
  239. x = iv.cos(n*iv.pi)
  240. assert -1 in x
  241. assert x >= -1
  242. assert x != -1
  243. x = iv.sin((n+0.5)*iv.pi)
  244. assert -1 in x
  245. assert x >= -1
  246. assert x != -1
  247. for n in range(-6,8,2):
  248. x = iv.cos(n*iv.pi)
  249. assert 1 in x
  250. assert x <= 1
  251. if n:
  252. assert x != 1
  253. x = iv.sin((n+0.5)*iv.pi)
  254. assert 1 in x
  255. assert x <= 1
  256. assert x != 1
  257. for n in range(-6,7):
  258. x = iv.cos((n+0.5)*iv.pi)
  259. assert x.a < 0 < x.b
  260. x = iv.sin(n*iv.pi)
  261. if n:
  262. assert x.a < 0 < x.b
  263. def test_interval_complex():
  264. # TODO: many more tests
  265. iv.dps = 15
  266. mp.dps = 15
  267. assert iv.mpc(2,3) == 2+3j
  268. assert iv.mpc(2,3) != 2+4j
  269. assert iv.mpc(2,3) != 1+3j
  270. assert 1+3j in iv.mpc([1,2],[3,4])
  271. assert 2+5j not in iv.mpc([1,2],[3,4])
  272. assert iv.mpc(1,2) + 1j == 1+3j
  273. assert iv.mpc([1,2],[2,3]) + 2+3j == iv.mpc([3,4],[5,6])
  274. assert iv.mpc([2,4],[4,8]) / 2 == iv.mpc([1,2],[2,4])
  275. assert iv.mpc([1,2],[2,4]) * 2j == iv.mpc([-8,-4],[2,4])
  276. assert iv.mpc([2,4],[4,8]) / 2j == iv.mpc([2,4],[-2,-1])
  277. assert iv.exp(2+3j).ae(mp.exp(2+3j))
  278. assert iv.log(2+3j).ae(mp.log(2+3j))
  279. assert (iv.mpc(2,3) ** iv.mpc(0.5,2)).ae(mp.mpc(2,3) ** mp.mpc(0.5,2))
  280. assert 1j in (iv.mpf(-1) ** 0.5)
  281. assert 1j in (iv.mpc(-1) ** 0.5)
  282. assert abs(iv.mpc(0)) == 0
  283. assert abs(iv.mpc(inf)) == inf
  284. assert abs(iv.mpc(3,4)) == 5
  285. assert abs(iv.mpc(4)) == 4
  286. assert abs(iv.mpc(0,4)) == 4
  287. assert abs(iv.mpc(0,[2,3])) == iv.mpf([2,3])
  288. assert abs(iv.mpc(0,[-3,2])) == iv.mpf([0,3])
  289. assert abs(iv.mpc([3,5],[4,12])) == iv.mpf([5,13])
  290. assert abs(iv.mpc([3,5],[-4,12])) == iv.mpf([3,13])
  291. assert iv.mpc(2,3) ** 0 == 1
  292. assert iv.mpc(2,3) ** 1 == (2+3j)
  293. assert iv.mpc(2,3) ** 2 == (2+3j)**2
  294. assert iv.mpc(2,3) ** 3 == (2+3j)**3
  295. assert iv.mpc(2,3) ** 4 == (2+3j)**4
  296. assert iv.mpc(2,3) ** 5 == (2+3j)**5
  297. assert iv.mpc(2,2) ** (-1) == (2+2j) ** (-1)
  298. assert iv.mpc(2,2) ** (-2) == (2+2j) ** (-2)
  299. assert iv.cos(2).ae(mp.cos(2))
  300. assert iv.sin(2).ae(mp.sin(2))
  301. assert iv.cos(2+3j).ae(mp.cos(2+3j))
  302. assert iv.sin(2+3j).ae(mp.sin(2+3j))
  303. def test_interval_complex_arg():
  304. mp.dps = 15
  305. iv.dps = 15
  306. assert iv.arg(3) == 0
  307. assert iv.arg(0) == 0
  308. assert iv.arg([0,3]) == 0
  309. assert iv.arg(-3).ae(pi)
  310. assert iv.arg(2+3j).ae(iv.arg(2+3j))
  311. z = iv.mpc([-2,-1],[3,4])
  312. t = iv.arg(z)
  313. assert t.a.ae(mp.arg(-1+4j))
  314. assert t.b.ae(mp.arg(-2+3j))
  315. z = iv.mpc([-2,1],[3,4])
  316. t = iv.arg(z)
  317. assert t.a.ae(mp.arg(1+3j))
  318. assert t.b.ae(mp.arg(-2+3j))
  319. z = iv.mpc([1,2],[3,4])
  320. t = iv.arg(z)
  321. assert t.a.ae(mp.arg(2+3j))
  322. assert t.b.ae(mp.arg(1+4j))
  323. z = iv.mpc([1,2],[-2,3])
  324. t = iv.arg(z)
  325. assert t.a.ae(mp.arg(1-2j))
  326. assert t.b.ae(mp.arg(1+3j))
  327. z = iv.mpc([1,2],[-4,-3])
  328. t = iv.arg(z)
  329. assert t.a.ae(mp.arg(1-4j))
  330. assert t.b.ae(mp.arg(2-3j))
  331. z = iv.mpc([-1,2],[-4,-3])
  332. t = iv.arg(z)
  333. assert t.a.ae(mp.arg(-1-3j))
  334. assert t.b.ae(mp.arg(2-3j))
  335. z = iv.mpc([-2,-1],[-4,-3])
  336. t = iv.arg(z)
  337. assert t.a.ae(mp.arg(-2-3j))
  338. assert t.b.ae(mp.arg(-1-4j))
  339. z = iv.mpc([-2,-1],[-3,3])
  340. t = iv.arg(z)
  341. assert t.a.ae(-mp.pi)
  342. assert t.b.ae(mp.pi)
  343. z = iv.mpc([-2,2],[-3,3])
  344. t = iv.arg(z)
  345. assert t.a.ae(-mp.pi)
  346. assert t.b.ae(mp.pi)
  347. def test_interval_ae():
  348. iv.dps = 15
  349. x = iv.mpf([1,2])
  350. assert x.ae(1) is None
  351. assert x.ae(1.5) is None
  352. assert x.ae(2) is None
  353. assert x.ae(2.01) is False
  354. assert x.ae(0.99) is False
  355. x = iv.mpf(3.5)
  356. assert x.ae(3.5) is True
  357. assert x.ae(3.5+1e-15) is True
  358. assert x.ae(3.5-1e-15) is True
  359. assert x.ae(3.501) is False
  360. assert x.ae(3.499) is False
  361. assert x.ae(iv.mpf([3.5,3.501])) is None
  362. assert x.ae(iv.mpf([3.5,4.5+1e-15])) is None
  363. def test_interval_nstr():
  364. iv.dps = n = 30
  365. x = mpi(1, 2)
  366. # FIXME: error_dps should not be necessary
  367. assert iv.nstr(x, n, mode='plusminus', error_dps=6) == '1.5 +- 0.5'
  368. assert iv.nstr(x, n, mode='plusminus', use_spaces=False, error_dps=6) == '1.5+-0.5'
  369. assert iv.nstr(x, n, mode='percent') == '1.5 (33.33%)'
  370. assert iv.nstr(x, n, mode='brackets', use_spaces=False) == '[1.0,2.0]'
  371. assert iv.nstr(x, n, mode='brackets' , brackets=('<', '>')) == '<1.0, 2.0>'
  372. x = mpi('5.2582327113062393041', '5.2582327113062749951')
  373. assert iv.nstr(x, n, mode='diff') == '5.2582327113062[393041, 749951]'
  374. assert iv.nstr(iv.cos(mpi(1)), n, mode='diff', use_spaces=False) == '0.54030230586813971740093660744[2955,3053]'
  375. assert iv.nstr(mpi('1e123', '1e129'), n, mode='diff') == '[1.0e+123, 1.0e+129]'
  376. exp = iv.exp
  377. assert iv.nstr(iv.exp(mpi('5000.1')), n, mode='diff') == '3.2797365856787867069110487[0926, 1191]e+2171'
  378. iv.dps = 15
  379. def test_mpi_from_str():
  380. iv.dps = 15
  381. assert iv.convert('1.5 +- 0.5') == mpi(mpf('1.0'), mpf('2.0'))
  382. assert mpi(1, 2) in iv.convert('1.5 (33.33333333333333333333333333333%)')
  383. assert iv.convert('[1, 2]') == mpi(1, 2)
  384. assert iv.convert('1[2, 3]') == mpi(12, 13)
  385. assert iv.convert('1.[23,46]e-8') == mpi('1.23e-8', '1.46e-8')
  386. assert iv.convert('12[3.4,5.9]e4') == mpi('123.4e+4', '125.9e4')
  387. def test_interval_gamma():
  388. mp.dps = 15
  389. iv.dps = 15
  390. # TODO: need many more tests
  391. assert iv.rgamma(0) == 0
  392. assert iv.fac(0) == 1
  393. assert iv.fac(1) == 1
  394. assert iv.fac(2) == 2
  395. assert iv.fac(3) == 6
  396. assert iv.gamma(0) == [-inf,inf]
  397. assert iv.gamma(1) == 1
  398. assert iv.gamma(2) == 1
  399. assert iv.gamma(3) == 2
  400. assert -3.5449077018110320546 in iv.gamma(-0.5)
  401. assert iv.loggamma(1) == 0
  402. assert iv.loggamma(2) == 0
  403. assert 0.69314718055994530942 in iv.loggamma(3)
  404. # Test tight log-gamma endpoints based on monotonicity
  405. xs = [iv.mpc([2,3],[1,4]),
  406. iv.mpc([2,3],[-4,-1]),
  407. iv.mpc([2,3],[-1,4]),
  408. iv.mpc([2,3],[-4,1]),
  409. iv.mpc([2,3],[-4,4]),
  410. iv.mpc([-3,-2],[2,4]),
  411. iv.mpc([-3,-2],[-4,-2])]
  412. for x in xs:
  413. ys = [mp.loggamma(mp.mpc(x.a,x.c)),
  414. mp.loggamma(mp.mpc(x.b,x.c)),
  415. mp.loggamma(mp.mpc(x.a,x.d)),
  416. mp.loggamma(mp.mpc(x.b,x.d))]
  417. if 0 in x.imag:
  418. ys += [mp.loggamma(x.a), mp.loggamma(x.b)]
  419. min_real = min([y.real for y in ys])
  420. max_real = max([y.real for y in ys])
  421. min_imag = min([y.imag for y in ys])
  422. max_imag = max([y.imag for y in ys])
  423. z = iv.loggamma(x)
  424. assert z.a.ae(min_real)
  425. assert z.b.ae(max_real)
  426. assert z.c.ae(min_imag)
  427. assert z.d.ae(max_imag)
  428. def test_interval_conversions():
  429. mp.dps = 15
  430. iv.dps = 15
  431. for a, b in ((-0.0, 0), (0.0, 0.5), (1.0, 1), \
  432. ('-inf', 20.5), ('-inf', float(sqrt(2)))):
  433. r = mpi(a, b)
  434. assert int(r.b) == int(b)
  435. assert float(r.a) == float(a)
  436. assert float(r.b) == float(b)
  437. assert complex(r.a) == complex(a)
  438. assert complex(r.b) == complex(b)