test_numbers.py 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  1. import numbers as nums
  2. import decimal
  3. from sympy.concrete.summations import Sum
  4. from sympy.core import (EulerGamma, Catalan, TribonacciConstant,
  5. GoldenRatio)
  6. from sympy.core.containers import Tuple
  7. from sympy.core.expr import unchanged
  8. from sympy.core.logic import fuzzy_not
  9. from sympy.core.mul import Mul
  10. from sympy.core.numbers import (mpf_norm, mod_inverse, igcd, seterr,
  11. igcd_lehmer, Integer, I, pi, comp, ilcm, Rational, E, nan, igcd2,
  12. oo, AlgebraicNumber, igcdex, Number, Float, zoo, equal_valued)
  13. from sympy.core.power import Pow
  14. from sympy.core.relational import Ge, Gt, Le, Lt
  15. from sympy.core.singleton import S
  16. from sympy.core.symbol import Dummy, Symbol
  17. from sympy.core.sympify import sympify
  18. from sympy.functions.combinatorial.factorials import factorial
  19. from sympy.functions.elementary.integers import floor
  20. from sympy.functions.combinatorial.numbers import fibonacci
  21. from sympy.functions.elementary.exponential import exp, log
  22. from sympy.functions.elementary.miscellaneous import sqrt, cbrt
  23. from sympy.functions.elementary.trigonometric import cos, sin
  24. from sympy.polys.domains.realfield import RealField
  25. from sympy.printing.latex import latex
  26. from sympy.printing.repr import srepr
  27. from sympy.simplify import simplify
  28. from sympy.core.power import integer_nthroot, isqrt, integer_log
  29. from sympy.polys.domains.groundtypes import PythonRational
  30. from sympy.utilities.decorator import conserve_mpmath_dps
  31. from sympy.utilities.iterables import permutations
  32. from sympy.testing.pytest import XFAIL, raises, _both_exp_pow
  33. from mpmath import mpf
  34. from mpmath.rational import mpq
  35. import mpmath
  36. from sympy.core import numbers
  37. t = Symbol('t', real=False)
  38. _ninf = float(-oo)
  39. _inf = float(oo)
  40. def same_and_same_prec(a, b):
  41. # stricter matching for Floats
  42. return a == b and a._prec == b._prec
  43. def test_seterr():
  44. seterr(divide=True)
  45. raises(ValueError, lambda: S.Zero/S.Zero)
  46. seterr(divide=False)
  47. assert S.Zero / S.Zero is S.NaN
  48. def test_mod():
  49. x = S.Half
  50. y = Rational(3, 4)
  51. z = Rational(5, 18043)
  52. assert x % x == 0
  53. assert x % y == S.Half
  54. assert x % z == Rational(3, 36086)
  55. assert y % x == Rational(1, 4)
  56. assert y % y == 0
  57. assert y % z == Rational(9, 72172)
  58. assert z % x == Rational(5, 18043)
  59. assert z % y == Rational(5, 18043)
  60. assert z % z == 0
  61. a = Float(2.6)
  62. assert (a % .2) == 0.0
  63. assert (a % 2).round(15) == 0.6
  64. assert (a % 0.5).round(15) == 0.1
  65. p = Symbol('p', infinite=True)
  66. assert oo % oo is nan
  67. assert zoo % oo is nan
  68. assert 5 % oo is nan
  69. assert p % 5 is nan
  70. # In these two tests, if the precision of m does
  71. # not match the precision of the ans, then it is
  72. # likely that the change made now gives an answer
  73. # with degraded accuracy.
  74. r = Rational(500, 41)
  75. f = Float('.36', 3)
  76. m = r % f
  77. ans = Float(r % Rational(f), 3)
  78. assert m == ans and m._prec == ans._prec
  79. f = Float('8.36', 3)
  80. m = f % r
  81. ans = Float(Rational(f) % r, 3)
  82. assert m == ans and m._prec == ans._prec
  83. s = S.Zero
  84. assert s % float(1) == 0.0
  85. # No rounding required since these numbers can be represented
  86. # exactly.
  87. assert Rational(3, 4) % Float(1.1) == 0.75
  88. assert Float(1.5) % Rational(5, 4) == 0.25
  89. assert Rational(5, 4).__rmod__(Float('1.5')) == 0.25
  90. assert Float('1.5').__rmod__(Float('2.75')) == Float('1.25')
  91. assert 2.75 % Float('1.5') == Float('1.25')
  92. a = Integer(7)
  93. b = Integer(4)
  94. assert type(a % b) == Integer
  95. assert a % b == Integer(3)
  96. assert Integer(1) % Rational(2, 3) == Rational(1, 3)
  97. assert Rational(7, 5) % Integer(1) == Rational(2, 5)
  98. assert Integer(2) % 1.5 == 0.5
  99. assert Integer(3).__rmod__(Integer(10)) == Integer(1)
  100. assert Integer(10) % 4 == Integer(2)
  101. assert 15 % Integer(4) == Integer(3)
  102. def test_divmod():
  103. x = Symbol("x")
  104. assert divmod(S(12), S(8)) == Tuple(1, 4)
  105. assert divmod(-S(12), S(8)) == Tuple(-2, 4)
  106. assert divmod(S.Zero, S.One) == Tuple(0, 0)
  107. raises(ZeroDivisionError, lambda: divmod(S.Zero, S.Zero))
  108. raises(ZeroDivisionError, lambda: divmod(S.One, S.Zero))
  109. assert divmod(S(12), 8) == Tuple(1, 4)
  110. assert divmod(12, S(8)) == Tuple(1, 4)
  111. assert S(1024)//x == 1024//x == floor(1024/x)
  112. assert divmod(S("2"), S("3/2")) == Tuple(S("1"), S("1/2"))
  113. assert divmod(S("3/2"), S("2")) == Tuple(S("0"), S("3/2"))
  114. assert divmod(S("2"), S("3.5")) == Tuple(S("0"), S("2"))
  115. assert divmod(S("3.5"), S("2")) == Tuple(S("1"), S("1.5"))
  116. assert divmod(S("2"), S("1/3")) == Tuple(S("6"), S("0"))
  117. assert divmod(S("1/3"), S("2")) == Tuple(S("0"), S("1/3"))
  118. assert divmod(S("2"), S("1/10")) == Tuple(S("20"), S("0"))
  119. assert divmod(S("2"), S(".1"))[0] == 19
  120. assert divmod(S("0.1"), S("2")) == Tuple(S("0"), S("0.1"))
  121. assert divmod(S("2"), 2) == Tuple(S("1"), S("0"))
  122. assert divmod(2, S("2")) == Tuple(S("1"), S("0"))
  123. assert divmod(S("2"), 1.5) == Tuple(S("1"), S("0.5"))
  124. assert divmod(1.5, S("2")) == Tuple(S("0"), S("1.5"))
  125. assert divmod(0.3, S("2")) == Tuple(S("0"), S("0.3"))
  126. assert divmod(S("3/2"), S("3.5")) == Tuple(S("0"), S("3/2"))
  127. assert divmod(S("3.5"), S("3/2")) == Tuple(S("2"), S("0.5"))
  128. assert divmod(S("3/2"), S("1/3")) == Tuple(S("4"), S("1/6"))
  129. assert divmod(S("1/3"), S("3/2")) == Tuple(S("0"), S("1/3"))
  130. assert divmod(S("3/2"), S("0.1"))[0] == 14
  131. assert divmod(S("0.1"), S("3/2")) == Tuple(S("0"), S("0.1"))
  132. assert divmod(S("3/2"), 2) == Tuple(S("0"), S("3/2"))
  133. assert divmod(2, S("3/2")) == Tuple(S("1"), S("1/2"))
  134. assert divmod(S("3/2"), 1.5) == Tuple(S("1"), S("0"))
  135. assert divmod(1.5, S("3/2")) == Tuple(S("1"), S("0"))
  136. assert divmod(S("3/2"), 0.3) == Tuple(S("5"), S("0"))
  137. assert divmod(0.3, S("3/2")) == Tuple(S("0"), S("0.3"))
  138. assert divmod(S("1/3"), S("3.5")) == Tuple(S("0"), S("1/3"))
  139. assert divmod(S("3.5"), S("0.1")) == Tuple(S("35"), S("0"))
  140. assert divmod(S("0.1"), S("3.5")) == Tuple(S("0"), S("0.1"))
  141. assert divmod(S("3.5"), 2) == Tuple(S("1"), S("1.5"))
  142. assert divmod(2, S("3.5")) == Tuple(S("0"), S("2"))
  143. assert divmod(S("3.5"), 1.5) == Tuple(S("2"), S("0.5"))
  144. assert divmod(1.5, S("3.5")) == Tuple(S("0"), S("1.5"))
  145. assert divmod(0.3, S("3.5")) == Tuple(S("0"), S("0.3"))
  146. assert divmod(S("0.1"), S("1/3")) == Tuple(S("0"), S("0.1"))
  147. assert divmod(S("1/3"), 2) == Tuple(S("0"), S("1/3"))
  148. assert divmod(2, S("1/3")) == Tuple(S("6"), S("0"))
  149. assert divmod(S("1/3"), 1.5) == Tuple(S("0"), S("1/3"))
  150. assert divmod(0.3, S("1/3")) == Tuple(S("0"), S("0.3"))
  151. assert divmod(S("0.1"), 2) == Tuple(S("0"), S("0.1"))
  152. assert divmod(2, S("0.1"))[0] == 19
  153. assert divmod(S("0.1"), 1.5) == Tuple(S("0"), S("0.1"))
  154. assert divmod(1.5, S("0.1")) == Tuple(S("15"), S("0"))
  155. assert divmod(S("0.1"), 0.3) == Tuple(S("0"), S("0.1"))
  156. assert str(divmod(S("2"), 0.3)) == '(6, 0.2)'
  157. assert str(divmod(S("3.5"), S("1/3"))) == '(10, 0.166666666666667)'
  158. assert str(divmod(S("3.5"), 0.3)) == '(11, 0.2)'
  159. assert str(divmod(S("1/3"), S("0.1"))) == '(3, 0.0333333333333333)'
  160. assert str(divmod(1.5, S("1/3"))) == '(4, 0.166666666666667)'
  161. assert str(divmod(S("1/3"), 0.3)) == '(1, 0.0333333333333333)'
  162. assert str(divmod(0.3, S("0.1"))) == '(2, 0.1)'
  163. assert divmod(-3, S(2)) == (-2, 1)
  164. assert divmod(S(-3), S(2)) == (-2, 1)
  165. assert divmod(S(-3), 2) == (-2, 1)
  166. assert divmod(S(4), S(-3.1)) == Tuple(-2, -2.2)
  167. assert divmod(S(4), S(-2.1)) == divmod(4, -2.1)
  168. assert divmod(S(-8), S(-2.5) ) == Tuple(3, -0.5)
  169. assert divmod(oo, 1) == (S.NaN, S.NaN)
  170. assert divmod(S.NaN, 1) == (S.NaN, S.NaN)
  171. assert divmod(1, S.NaN) == (S.NaN, S.NaN)
  172. ans = [(-1, oo), (-1, oo), (0, 0), (0, 1), (0, 2)]
  173. OO = float('inf')
  174. ANS = [tuple(map(float, i)) for i in ans]
  175. assert [divmod(i, oo) for i in range(-2, 3)] == ans
  176. ans = [(0, -2), (0, -1), (0, 0), (-1, -oo), (-1, -oo)]
  177. ANS = [tuple(map(float, i)) for i in ans]
  178. assert [divmod(i, -oo) for i in range(-2, 3)] == ans
  179. assert [divmod(i, -OO) for i in range(-2, 3)] == ANS
  180. assert divmod(S(3.5), S(-2)) == divmod(3.5, -2)
  181. assert divmod(-S(3.5), S(-2)) == divmod(-3.5, -2)
  182. assert divmod(S(0.0), S(9)) == divmod(0.0, 9)
  183. assert divmod(S(0), S(9.0)) == divmod(0, 9.0)
  184. def test_igcd():
  185. assert igcd(0, 0) == 0
  186. assert igcd(0, 1) == 1
  187. assert igcd(1, 0) == 1
  188. assert igcd(0, 7) == 7
  189. assert igcd(7, 0) == 7
  190. assert igcd(7, 1) == 1
  191. assert igcd(1, 7) == 1
  192. assert igcd(-1, 0) == 1
  193. assert igcd(0, -1) == 1
  194. assert igcd(-1, -1) == 1
  195. assert igcd(-1, 7) == 1
  196. assert igcd(7, -1) == 1
  197. assert igcd(8, 2) == 2
  198. assert igcd(4, 8) == 4
  199. assert igcd(8, 16) == 8
  200. assert igcd(7, -3) == 1
  201. assert igcd(-7, 3) == 1
  202. assert igcd(-7, -3) == 1
  203. assert igcd(*[10, 20, 30]) == 10
  204. raises(TypeError, lambda: igcd())
  205. raises(TypeError, lambda: igcd(2))
  206. raises(ValueError, lambda: igcd(0, None))
  207. raises(ValueError, lambda: igcd(1, 2.2))
  208. for args in permutations((45.1, 1, 30)):
  209. raises(ValueError, lambda: igcd(*args))
  210. for args in permutations((1, 2, None)):
  211. raises(ValueError, lambda: igcd(*args))
  212. def test_igcd_lehmer():
  213. a, b = fibonacci(10001), fibonacci(10000)
  214. # len(str(a)) == 2090
  215. # small divisors, long Euclidean sequence
  216. assert igcd_lehmer(a, b) == 1
  217. c = fibonacci(100)
  218. assert igcd_lehmer(a*c, b*c) == c
  219. # big divisor
  220. assert igcd_lehmer(a, 10**1000) == 1
  221. # swapping argument
  222. assert igcd_lehmer(1, 2) == igcd_lehmer(2, 1)
  223. def test_igcd2():
  224. # short loop
  225. assert igcd2(2**100 - 1, 2**99 - 1) == 1
  226. # Lehmer's algorithm
  227. a, b = int(fibonacci(10001)), int(fibonacci(10000))
  228. assert igcd2(a, b) == 1
  229. def test_ilcm():
  230. assert ilcm(0, 0) == 0
  231. assert ilcm(1, 0) == 0
  232. assert ilcm(0, 1) == 0
  233. assert ilcm(1, 1) == 1
  234. assert ilcm(2, 1) == 2
  235. assert ilcm(8, 2) == 8
  236. assert ilcm(8, 6) == 24
  237. assert ilcm(8, 7) == 56
  238. assert ilcm(*[10, 20, 30]) == 60
  239. raises(ValueError, lambda: ilcm(8.1, 7))
  240. raises(ValueError, lambda: ilcm(8, 7.1))
  241. raises(TypeError, lambda: ilcm(8))
  242. def test_igcdex():
  243. assert igcdex(2, 3) == (-1, 1, 1)
  244. assert igcdex(10, 12) == (-1, 1, 2)
  245. assert igcdex(100, 2004) == (-20, 1, 4)
  246. assert igcdex(0, 0) == (0, 1, 0)
  247. assert igcdex(1, 0) == (1, 0, 1)
  248. def _strictly_equal(a, b):
  249. return (a.p, a.q, type(a.p), type(a.q)) == \
  250. (b.p, b.q, type(b.p), type(b.q))
  251. def _test_rational_new(cls):
  252. """
  253. Tests that are common between Integer and Rational.
  254. """
  255. assert cls(0) is S.Zero
  256. assert cls(1) is S.One
  257. assert cls(-1) is S.NegativeOne
  258. # These look odd, but are similar to int():
  259. assert cls('1') is S.One
  260. assert cls('-1') is S.NegativeOne
  261. i = Integer(10)
  262. assert _strictly_equal(i, cls('10'))
  263. assert _strictly_equal(i, cls('10'))
  264. assert _strictly_equal(i, cls(int(10)))
  265. assert _strictly_equal(i, cls(i))
  266. raises(TypeError, lambda: cls(Symbol('x')))
  267. def test_Integer_new():
  268. """
  269. Test for Integer constructor
  270. """
  271. _test_rational_new(Integer)
  272. assert _strictly_equal(Integer(0.9), S.Zero)
  273. assert _strictly_equal(Integer(10.5), Integer(10))
  274. raises(ValueError, lambda: Integer("10.5"))
  275. assert Integer(Rational('1.' + '9'*20)) == 1
  276. def test_Rational_new():
  277. """"
  278. Test for Rational constructor
  279. """
  280. _test_rational_new(Rational)
  281. n1 = S.Half
  282. assert n1 == Rational(Integer(1), 2)
  283. assert n1 == Rational(Integer(1), Integer(2))
  284. assert n1 == Rational(1, Integer(2))
  285. assert n1 == Rational(S.Half)
  286. assert 1 == Rational(n1, n1)
  287. assert Rational(3, 2) == Rational(S.Half, Rational(1, 3))
  288. assert Rational(3, 1) == Rational(1, Rational(1, 3))
  289. n3_4 = Rational(3, 4)
  290. assert Rational('3/4') == n3_4
  291. assert -Rational('-3/4') == n3_4
  292. assert Rational('.76').limit_denominator(4) == n3_4
  293. assert Rational(19, 25).limit_denominator(4) == n3_4
  294. assert Rational('19/25').limit_denominator(4) == n3_4
  295. assert Rational(1.0, 3) == Rational(1, 3)
  296. assert Rational(1, 3.0) == Rational(1, 3)
  297. assert Rational(Float(0.5)) == S.Half
  298. assert Rational('1e2/1e-2') == Rational(10000)
  299. assert Rational('1 234') == Rational(1234)
  300. assert Rational('1/1 234') == Rational(1, 1234)
  301. assert Rational(-1, 0) is S.ComplexInfinity
  302. assert Rational(1, 0) is S.ComplexInfinity
  303. # Make sure Rational doesn't lose precision on Floats
  304. assert Rational(pi.evalf(100)).evalf(100) == pi.evalf(100)
  305. raises(TypeError, lambda: Rational('3**3'))
  306. raises(TypeError, lambda: Rational('1/2 + 2/3'))
  307. # handle fractions.Fraction instances
  308. try:
  309. import fractions
  310. assert Rational(fractions.Fraction(1, 2)) == S.Half
  311. except ImportError:
  312. pass
  313. assert Rational(mpq(2, 6)) == Rational(1, 3)
  314. assert Rational(PythonRational(2, 6)) == Rational(1, 3)
  315. assert Rational(2, 4, gcd=1).q == 4
  316. n = Rational(2, -4, gcd=1)
  317. assert n.q == 4
  318. assert n.p == -2
  319. def test_issue_24543():
  320. for p in ('1.5', 1.5, 2):
  321. for q in ('1.5', 1.5, 2):
  322. assert Rational(p, q).as_numer_denom() == Rational('%s/%s'%(p,q)).as_numer_denom()
  323. assert Rational('0.5', '100') == Rational(1, 200)
  324. def test_Number_new():
  325. """"
  326. Test for Number constructor
  327. """
  328. # Expected behavior on numbers and strings
  329. assert Number(1) is S.One
  330. assert Number(2).__class__ is Integer
  331. assert Number(-622).__class__ is Integer
  332. assert Number(5, 3).__class__ is Rational
  333. assert Number(5.3).__class__ is Float
  334. assert Number('1') is S.One
  335. assert Number('2').__class__ is Integer
  336. assert Number('-622').__class__ is Integer
  337. assert Number('5/3').__class__ is Rational
  338. assert Number('5.3').__class__ is Float
  339. raises(ValueError, lambda: Number('cos'))
  340. raises(TypeError, lambda: Number(cos))
  341. a = Rational(3, 5)
  342. assert Number(a) is a # Check idempotence on Numbers
  343. u = ['inf', '-inf', 'nan', 'iNF', '+inf']
  344. v = [oo, -oo, nan, oo, oo]
  345. for i, a in zip(u, v):
  346. assert Number(i) is a, (i, Number(i), a)
  347. def test_Number_cmp():
  348. n1 = Number(1)
  349. n2 = Number(2)
  350. n3 = Number(-3)
  351. assert n1 < n2
  352. assert n1 <= n2
  353. assert n3 < n1
  354. assert n2 > n3
  355. assert n2 >= n3
  356. raises(TypeError, lambda: n1 < S.NaN)
  357. raises(TypeError, lambda: n1 <= S.NaN)
  358. raises(TypeError, lambda: n1 > S.NaN)
  359. raises(TypeError, lambda: n1 >= S.NaN)
  360. def test_Rational_cmp():
  361. n1 = Rational(1, 4)
  362. n2 = Rational(1, 3)
  363. n3 = Rational(2, 4)
  364. n4 = Rational(2, -4)
  365. n5 = Rational(0)
  366. n6 = Rational(1)
  367. n7 = Rational(3)
  368. n8 = Rational(-3)
  369. assert n8 < n5
  370. assert n5 < n6
  371. assert n6 < n7
  372. assert n8 < n7
  373. assert n7 > n8
  374. assert (n1 + 1)**n2 < 2
  375. assert ((n1 + n6)/n7) < 1
  376. assert n4 < n3
  377. assert n2 < n3
  378. assert n1 < n2
  379. assert n3 > n1
  380. assert not n3 < n1
  381. assert not (Rational(-1) > 0)
  382. assert Rational(-1) < 0
  383. raises(TypeError, lambda: n1 < S.NaN)
  384. raises(TypeError, lambda: n1 <= S.NaN)
  385. raises(TypeError, lambda: n1 > S.NaN)
  386. raises(TypeError, lambda: n1 >= S.NaN)
  387. def test_Float():
  388. def eq(a, b):
  389. t = Float("1.0E-15")
  390. return (-t < a - b < t)
  391. zeros = (0, S.Zero, 0., Float(0))
  392. for i, j in permutations(zeros, 2):
  393. assert i == j
  394. for z in zeros:
  395. assert z in zeros
  396. assert S.Zero.is_zero
  397. a = Float(2) ** Float(3)
  398. assert eq(a.evalf(), Float(8))
  399. assert eq((pi ** -1).evalf(), Float("0.31830988618379067"))
  400. a = Float(2) ** Float(4)
  401. assert eq(a.evalf(), Float(16))
  402. assert (S(.3) == S(.5)) is False
  403. mpf = (0, 5404319552844595, -52, 53)
  404. x_str = Float((0, '13333333333333', -52, 53))
  405. x_0xstr = Float((0, '0x13333333333333', -52, 53))
  406. x2_str = Float((0, '26666666666666', -53, 54))
  407. x_hex = Float((0, int(0x13333333333333), -52, 53))
  408. x_dec = Float(mpf)
  409. assert x_str == x_0xstr == x_hex == x_dec == Float(1.2)
  410. # x2_str was entered slightly malformed in that the mantissa
  411. # was even -- it should be odd and the even part should be
  412. # included with the exponent, but this is resolved by normalization
  413. # ONLY IF REQUIREMENTS of mpf_norm are met: the bitcount must
  414. # be exact: double the mantissa ==> increase bc by 1
  415. assert Float(1.2)._mpf_ == mpf
  416. assert x2_str._mpf_ == mpf
  417. assert Float((0, int(0), -123, -1)) is S.NaN
  418. assert Float((0, int(0), -456, -2)) is S.Infinity
  419. assert Float((1, int(0), -789, -3)) is S.NegativeInfinity
  420. # if you don't give the full signature, it's not special
  421. assert Float((0, int(0), -123)) == Float(0)
  422. assert Float((0, int(0), -456)) == Float(0)
  423. assert Float((1, int(0), -789)) == Float(0)
  424. raises(ValueError, lambda: Float((0, 7, 1, 3), ''))
  425. assert Float('0.0').is_finite is True
  426. assert Float('0.0').is_negative is False
  427. assert Float('0.0').is_positive is False
  428. assert Float('0.0').is_infinite is False
  429. assert Float('0.0').is_zero is True
  430. # rationality properties
  431. # if the integer test fails then the use of intlike
  432. # should be removed from gamma_functions.py
  433. assert Float(1).is_integer is False
  434. assert Float(1).is_rational is None
  435. assert Float(1).is_irrational is None
  436. assert sqrt(2).n(15).is_rational is None
  437. assert sqrt(2).n(15).is_irrational is None
  438. # do not automatically evalf
  439. def teq(a):
  440. assert (a.evalf() == a) is False
  441. assert (a.evalf() != a) is True
  442. assert (a == a.evalf()) is False
  443. assert (a != a.evalf()) is True
  444. teq(pi)
  445. teq(2*pi)
  446. teq(cos(0.1, evaluate=False))
  447. # long integer
  448. i = 12345678901234567890
  449. assert same_and_same_prec(Float(12, ''), Float('12', ''))
  450. assert same_and_same_prec(Float(Integer(i), ''), Float(i, ''))
  451. assert same_and_same_prec(Float(i, ''), Float(str(i), 20))
  452. assert same_and_same_prec(Float(str(i)), Float(i, ''))
  453. assert same_and_same_prec(Float(i), Float(i, ''))
  454. # inexact floats (repeating binary = denom not multiple of 2)
  455. # cannot have precision greater than 15
  456. assert Float(.125, 22) == .125
  457. assert Float(2.0, 22) == 2
  458. assert float(Float('.12500000000000001', '')) == .125
  459. raises(ValueError, lambda: Float(.12500000000000001, ''))
  460. # allow spaces
  461. assert Float('123 456.123 456') == Float('123456.123456')
  462. assert Integer('123 456') == Integer('123456')
  463. assert Rational('123 456.123 456') == Rational('123456.123456')
  464. assert Float(' .3e2') == Float('0.3e2')
  465. # allow underscore
  466. assert Float('1_23.4_56') == Float('123.456')
  467. assert Float('1_') == Float('1.0')
  468. assert Float('1_.') == Float('1.0')
  469. assert Float('1._') == Float('1.0')
  470. assert Float('1__2') == Float('12.0')
  471. # assert Float('1_23.4_5_6', 12) == Float('123.456', 12)
  472. # ...but not in all cases (per Py 3.6)
  473. raises(ValueError, lambda: Float('_1'))
  474. raises(ValueError, lambda: Float('_inf'))
  475. # allow auto precision detection
  476. assert Float('.1', '') == Float(.1, 1)
  477. assert Float('.125', '') == Float(.125, 3)
  478. assert Float('.100', '') == Float(.1, 3)
  479. assert Float('2.0', '') == Float('2', 2)
  480. raises(ValueError, lambda: Float("12.3d-4", ""))
  481. raises(ValueError, lambda: Float(12.3, ""))
  482. raises(ValueError, lambda: Float('.'))
  483. raises(ValueError, lambda: Float('-.'))
  484. zero = Float('0.0')
  485. assert Float('-0') == zero
  486. assert Float('.0') == zero
  487. assert Float('-.0') == zero
  488. assert Float('-0.0') == zero
  489. assert Float(0.0) == zero
  490. assert Float(0) == zero
  491. assert Float(0, '') == Float('0', '')
  492. assert Float(1) == Float(1.0)
  493. assert Float(S.Zero) == zero
  494. assert Float(S.One) == Float(1.0)
  495. assert Float(decimal.Decimal('0.1'), 3) == Float('.1', 3)
  496. assert Float(decimal.Decimal('nan')) is S.NaN
  497. assert Float(decimal.Decimal('Infinity')) is S.Infinity
  498. assert Float(decimal.Decimal('-Infinity')) is S.NegativeInfinity
  499. assert '{:.3f}'.format(Float(4.236622)) == '4.237'
  500. assert '{:.35f}'.format(Float(pi.n(40), 40)) == \
  501. '3.14159265358979323846264338327950288'
  502. # unicode
  503. assert Float('0.73908513321516064100000000') == \
  504. Float('0.73908513321516064100000000')
  505. assert Float('0.73908513321516064100000000', 28) == \
  506. Float('0.73908513321516064100000000', 28)
  507. # binary precision
  508. # Decimal value 0.1 cannot be expressed precisely as a base 2 fraction
  509. a = Float(S.One/10, dps=15)
  510. b = Float(S.One/10, dps=16)
  511. p = Float(S.One/10, precision=53)
  512. q = Float(S.One/10, precision=54)
  513. assert a._mpf_ == p._mpf_
  514. assert not a._mpf_ == q._mpf_
  515. assert not b._mpf_ == q._mpf_
  516. # Precision specifying errors
  517. raises(ValueError, lambda: Float("1.23", dps=3, precision=10))
  518. raises(ValueError, lambda: Float("1.23", dps="", precision=10))
  519. raises(ValueError, lambda: Float("1.23", dps=3, precision=""))
  520. raises(ValueError, lambda: Float("1.23", dps="", precision=""))
  521. # from NumberSymbol
  522. assert same_and_same_prec(Float(pi, 32), pi.evalf(32))
  523. assert same_and_same_prec(Float(Catalan), Catalan.evalf())
  524. # oo and nan
  525. u = ['inf', '-inf', 'nan', 'iNF', '+inf']
  526. v = [oo, -oo, nan, oo, oo]
  527. for i, a in zip(u, v):
  528. assert Float(i) is a
  529. def test_zero_not_false():
  530. # https://github.com/sympy/sympy/issues/20796
  531. assert (S(0.0) == S.false) is False
  532. assert (S.false == S(0.0)) is False
  533. assert (S(0) == S.false) is False
  534. assert (S.false == S(0)) is False
  535. @conserve_mpmath_dps
  536. def test_float_mpf():
  537. import mpmath
  538. mpmath.mp.dps = 100
  539. mp_pi = mpmath.pi()
  540. assert Float(mp_pi, 100) == Float(mp_pi._mpf_, 100) == pi.evalf(100)
  541. mpmath.mp.dps = 15
  542. assert Float(mp_pi, 100) == Float(mp_pi._mpf_, 100) == pi.evalf(100)
  543. def test_Float_RealElement():
  544. repi = RealField(dps=100)(pi.evalf(100))
  545. # We still have to pass the precision because Float doesn't know what
  546. # RealElement is, but make sure it keeps full precision from the result.
  547. assert Float(repi, 100) == pi.evalf(100)
  548. def test_Float_default_to_highprec_from_str():
  549. s = str(pi.evalf(128))
  550. assert same_and_same_prec(Float(s), Float(s, ''))
  551. def test_Float_eval():
  552. a = Float(3.2)
  553. assert (a**2).is_Float
  554. def test_Float_issue_2107():
  555. a = Float(0.1, 10)
  556. b = Float("0.1", 10)
  557. assert a - a == 0
  558. assert a + (-a) == 0
  559. assert S.Zero + a - a == 0
  560. assert S.Zero + a + (-a) == 0
  561. assert b - b == 0
  562. assert b + (-b) == 0
  563. assert S.Zero + b - b == 0
  564. assert S.Zero + b + (-b) == 0
  565. def test_issue_14289():
  566. from sympy.polys.numberfields import to_number_field
  567. a = 1 - sqrt(2)
  568. b = to_number_field(a)
  569. assert b.as_expr() == a
  570. assert b.minpoly(a).expand() == 0
  571. def test_Float_from_tuple():
  572. a = Float((0, '1L', 0, 1))
  573. b = Float((0, '1', 0, 1))
  574. assert a == b
  575. def test_Infinity():
  576. assert oo != 1
  577. assert 1*oo is oo
  578. assert 1 != oo
  579. assert oo != -oo
  580. assert oo != Symbol("x")**3
  581. assert oo + 1 is oo
  582. assert 2 + oo is oo
  583. assert 3*oo + 2 is oo
  584. assert S.Half**oo == 0
  585. assert S.Half**(-oo) is oo
  586. assert -oo*3 is -oo
  587. assert oo + oo is oo
  588. assert -oo + oo*(-5) is -oo
  589. assert 1/oo == 0
  590. assert 1/(-oo) == 0
  591. assert 8/oo == 0
  592. assert oo % 2 is nan
  593. assert 2 % oo is nan
  594. assert oo/oo is nan
  595. assert oo/-oo is nan
  596. assert -oo/oo is nan
  597. assert -oo/-oo is nan
  598. assert oo - oo is nan
  599. assert oo - -oo is oo
  600. assert -oo - oo is -oo
  601. assert -oo - -oo is nan
  602. assert oo + -oo is nan
  603. assert -oo + oo is nan
  604. assert oo + oo is oo
  605. assert -oo + oo is nan
  606. assert oo + -oo is nan
  607. assert -oo + -oo is -oo
  608. assert oo*oo is oo
  609. assert -oo*oo is -oo
  610. assert oo*-oo is -oo
  611. assert -oo*-oo is oo
  612. assert oo/0 is oo
  613. assert -oo/0 is -oo
  614. assert 0/oo == 0
  615. assert 0/-oo == 0
  616. assert oo*0 is nan
  617. assert -oo*0 is nan
  618. assert 0*oo is nan
  619. assert 0*-oo is nan
  620. assert oo + 0 is oo
  621. assert -oo + 0 is -oo
  622. assert 0 + oo is oo
  623. assert 0 + -oo is -oo
  624. assert oo - 0 is oo
  625. assert -oo - 0 is -oo
  626. assert 0 - oo is -oo
  627. assert 0 - -oo is oo
  628. assert oo/2 is oo
  629. assert -oo/2 is -oo
  630. assert oo/-2 is -oo
  631. assert -oo/-2 is oo
  632. assert oo*2 is oo
  633. assert -oo*2 is -oo
  634. assert oo*-2 is -oo
  635. assert 2/oo == 0
  636. assert 2/-oo == 0
  637. assert -2/oo == 0
  638. assert -2/-oo == 0
  639. assert 2*oo is oo
  640. assert 2*-oo is -oo
  641. assert -2*oo is -oo
  642. assert -2*-oo is oo
  643. assert 2 + oo is oo
  644. assert 2 - oo is -oo
  645. assert -2 + oo is oo
  646. assert -2 - oo is -oo
  647. assert 2 + -oo is -oo
  648. assert 2 - -oo is oo
  649. assert -2 + -oo is -oo
  650. assert -2 - -oo is oo
  651. assert S(2) + oo is oo
  652. assert S(2) - oo is -oo
  653. assert oo/I == -oo*I
  654. assert -oo/I == oo*I
  655. assert oo*float(1) == _inf and (oo*float(1)) is oo
  656. assert -oo*float(1) == _ninf and (-oo*float(1)) is -oo
  657. assert oo/float(1) == _inf and (oo/float(1)) is oo
  658. assert -oo/float(1) == _ninf and (-oo/float(1)) is -oo
  659. assert oo*float(-1) == _ninf and (oo*float(-1)) is -oo
  660. assert -oo*float(-1) == _inf and (-oo*float(-1)) is oo
  661. assert oo/float(-1) == _ninf and (oo/float(-1)) is -oo
  662. assert -oo/float(-1) == _inf and (-oo/float(-1)) is oo
  663. assert oo + float(1) == _inf and (oo + float(1)) is oo
  664. assert -oo + float(1) == _ninf and (-oo + float(1)) is -oo
  665. assert oo - float(1) == _inf and (oo - float(1)) is oo
  666. assert -oo - float(1) == _ninf and (-oo - float(1)) is -oo
  667. assert float(1)*oo == _inf and (float(1)*oo) is oo
  668. assert float(1)*-oo == _ninf and (float(1)*-oo) is -oo
  669. assert float(1)/oo == 0
  670. assert float(1)/-oo == 0
  671. assert float(-1)*oo == _ninf and (float(-1)*oo) is -oo
  672. assert float(-1)*-oo == _inf and (float(-1)*-oo) is oo
  673. assert float(-1)/oo == 0
  674. assert float(-1)/-oo == 0
  675. assert float(1) + oo is oo
  676. assert float(1) + -oo is -oo
  677. assert float(1) - oo is -oo
  678. assert float(1) - -oo is oo
  679. assert oo == float(oo)
  680. assert (oo != float(oo)) is False
  681. assert type(float(oo)) is float
  682. assert -oo == float(-oo)
  683. assert (-oo != float(-oo)) is False
  684. assert type(float(-oo)) is float
  685. assert Float('nan') is nan
  686. assert nan*1.0 is nan
  687. assert -1.0*nan is nan
  688. assert nan*oo is nan
  689. assert nan*-oo is nan
  690. assert nan/oo is nan
  691. assert nan/-oo is nan
  692. assert nan + oo is nan
  693. assert nan + -oo is nan
  694. assert nan - oo is nan
  695. assert nan - -oo is nan
  696. assert -oo * S.Zero is nan
  697. assert oo*nan is nan
  698. assert -oo*nan is nan
  699. assert oo/nan is nan
  700. assert -oo/nan is nan
  701. assert oo + nan is nan
  702. assert -oo + nan is nan
  703. assert oo - nan is nan
  704. assert -oo - nan is nan
  705. assert S.Zero * oo is nan
  706. assert oo.is_Rational is False
  707. assert isinstance(oo, Rational) is False
  708. assert S.One/oo == 0
  709. assert -S.One/oo == 0
  710. assert S.One/-oo == 0
  711. assert -S.One/-oo == 0
  712. assert S.One*oo is oo
  713. assert -S.One*oo is -oo
  714. assert S.One*-oo is -oo
  715. assert -S.One*-oo is oo
  716. assert S.One/nan is nan
  717. assert S.One - -oo is oo
  718. assert S.One + nan is nan
  719. assert S.One - nan is nan
  720. assert nan - S.One is nan
  721. assert nan/S.One is nan
  722. assert -oo - S.One is -oo
  723. def test_Infinity_2():
  724. x = Symbol('x')
  725. assert oo*x != oo
  726. assert oo*(pi - 1) is oo
  727. assert oo*(1 - pi) is -oo
  728. assert (-oo)*x != -oo
  729. assert (-oo)*(pi - 1) is -oo
  730. assert (-oo)*(1 - pi) is oo
  731. assert (-1)**S.NaN is S.NaN
  732. assert oo - _inf is S.NaN
  733. assert oo + _ninf is S.NaN
  734. assert oo*0 is S.NaN
  735. assert oo/_inf is S.NaN
  736. assert oo/_ninf is S.NaN
  737. assert oo**S.NaN is S.NaN
  738. assert -oo + _inf is S.NaN
  739. assert -oo - _ninf is S.NaN
  740. assert -oo*S.NaN is S.NaN
  741. assert -oo*0 is S.NaN
  742. assert -oo/_inf is S.NaN
  743. assert -oo/_ninf is S.NaN
  744. assert -oo/S.NaN is S.NaN
  745. assert abs(-oo) is oo
  746. assert all((-oo)**i is S.NaN for i in (oo, -oo, S.NaN))
  747. assert (-oo)**3 is -oo
  748. assert (-oo)**2 is oo
  749. assert abs(S.ComplexInfinity) is oo
  750. def test_Mul_Infinity_Zero():
  751. assert Float(0)*_inf is nan
  752. assert Float(0)*_ninf is nan
  753. assert Float(0)*_inf is nan
  754. assert Float(0)*_ninf is nan
  755. assert _inf*Float(0) is nan
  756. assert _ninf*Float(0) is nan
  757. assert _inf*Float(0) is nan
  758. assert _ninf*Float(0) is nan
  759. def test_Div_By_Zero():
  760. assert 1/S.Zero is zoo
  761. assert 1/Float(0) is zoo
  762. assert 0/S.Zero is nan
  763. assert 0/Float(0) is nan
  764. assert S.Zero/0 is nan
  765. assert Float(0)/0 is nan
  766. assert -1/S.Zero is zoo
  767. assert -1/Float(0) is zoo
  768. @_both_exp_pow
  769. def test_Infinity_inequations():
  770. assert oo > pi
  771. assert not (oo < pi)
  772. assert exp(-3) < oo
  773. assert _inf > pi
  774. assert not (_inf < pi)
  775. assert exp(-3) < _inf
  776. raises(TypeError, lambda: oo < I)
  777. raises(TypeError, lambda: oo <= I)
  778. raises(TypeError, lambda: oo > I)
  779. raises(TypeError, lambda: oo >= I)
  780. raises(TypeError, lambda: -oo < I)
  781. raises(TypeError, lambda: -oo <= I)
  782. raises(TypeError, lambda: -oo > I)
  783. raises(TypeError, lambda: -oo >= I)
  784. raises(TypeError, lambda: I < oo)
  785. raises(TypeError, lambda: I <= oo)
  786. raises(TypeError, lambda: I > oo)
  787. raises(TypeError, lambda: I >= oo)
  788. raises(TypeError, lambda: I < -oo)
  789. raises(TypeError, lambda: I <= -oo)
  790. raises(TypeError, lambda: I > -oo)
  791. raises(TypeError, lambda: I >= -oo)
  792. assert oo > -oo and oo >= -oo
  793. assert (oo < -oo) == False and (oo <= -oo) == False
  794. assert -oo < oo and -oo <= oo
  795. assert (-oo > oo) == False and (-oo >= oo) == False
  796. assert (oo < oo) == False # issue 7775
  797. assert (oo > oo) == False
  798. assert (-oo > -oo) == False and (-oo < -oo) == False
  799. assert oo >= oo and oo <= oo and -oo >= -oo and -oo <= -oo
  800. assert (-oo < -_inf) == False
  801. assert (oo > _inf) == False
  802. assert -oo >= -_inf
  803. assert oo <= _inf
  804. x = Symbol('x')
  805. b = Symbol('b', finite=True, real=True)
  806. assert (x < oo) == Lt(x, oo) # issue 7775
  807. assert b < oo and b > -oo and b <= oo and b >= -oo
  808. assert oo > b and oo >= b and (oo < b) == False and (oo <= b) == False
  809. assert (-oo > b) == False and (-oo >= b) == False and -oo < b and -oo <= b
  810. assert (oo < x) == Lt(oo, x) and (oo > x) == Gt(oo, x)
  811. assert (oo <= x) == Le(oo, x) and (oo >= x) == Ge(oo, x)
  812. assert (-oo < x) == Lt(-oo, x) and (-oo > x) == Gt(-oo, x)
  813. assert (-oo <= x) == Le(-oo, x) and (-oo >= x) == Ge(-oo, x)
  814. def test_NaN():
  815. assert nan is nan
  816. assert nan != 1
  817. assert 1*nan is nan
  818. assert 1 != nan
  819. assert -nan is nan
  820. assert oo != Symbol("x")**3
  821. assert 2 + nan is nan
  822. assert 3*nan + 2 is nan
  823. assert -nan*3 is nan
  824. assert nan + nan is nan
  825. assert -nan + nan*(-5) is nan
  826. assert 8/nan is nan
  827. raises(TypeError, lambda: nan > 0)
  828. raises(TypeError, lambda: nan < 0)
  829. raises(TypeError, lambda: nan >= 0)
  830. raises(TypeError, lambda: nan <= 0)
  831. raises(TypeError, lambda: 0 < nan)
  832. raises(TypeError, lambda: 0 > nan)
  833. raises(TypeError, lambda: 0 <= nan)
  834. raises(TypeError, lambda: 0 >= nan)
  835. assert nan**0 == 1 # as per IEEE 754
  836. assert 1**nan is nan # IEEE 754 is not the best choice for symbolic work
  837. # test Pow._eval_power's handling of NaN
  838. assert Pow(nan, 0, evaluate=False)**2 == 1
  839. for n in (1, 1., S.One, S.NegativeOne, Float(1)):
  840. assert n + nan is nan
  841. assert n - nan is nan
  842. assert nan + n is nan
  843. assert nan - n is nan
  844. assert n/nan is nan
  845. assert nan/n is nan
  846. def test_special_numbers():
  847. assert isinstance(S.NaN, Number) is True
  848. assert isinstance(S.Infinity, Number) is True
  849. assert isinstance(S.NegativeInfinity, Number) is True
  850. assert S.NaN.is_number is True
  851. assert S.Infinity.is_number is True
  852. assert S.NegativeInfinity.is_number is True
  853. assert S.ComplexInfinity.is_number is True
  854. assert isinstance(S.NaN, Rational) is False
  855. assert isinstance(S.Infinity, Rational) is False
  856. assert isinstance(S.NegativeInfinity, Rational) is False
  857. assert S.NaN.is_rational is not True
  858. assert S.Infinity.is_rational is not True
  859. assert S.NegativeInfinity.is_rational is not True
  860. def test_powers():
  861. assert integer_nthroot(1, 2) == (1, True)
  862. assert integer_nthroot(1, 5) == (1, True)
  863. assert integer_nthroot(2, 1) == (2, True)
  864. assert integer_nthroot(2, 2) == (1, False)
  865. assert integer_nthroot(2, 5) == (1, False)
  866. assert integer_nthroot(4, 2) == (2, True)
  867. assert integer_nthroot(123**25, 25) == (123, True)
  868. assert integer_nthroot(123**25 + 1, 25) == (123, False)
  869. assert integer_nthroot(123**25 - 1, 25) == (122, False)
  870. assert integer_nthroot(1, 1) == (1, True)
  871. assert integer_nthroot(0, 1) == (0, True)
  872. assert integer_nthroot(0, 3) == (0, True)
  873. assert integer_nthroot(10000, 1) == (10000, True)
  874. assert integer_nthroot(4, 2) == (2, True)
  875. assert integer_nthroot(16, 2) == (4, True)
  876. assert integer_nthroot(26, 2) == (5, False)
  877. assert integer_nthroot(1234567**7, 7) == (1234567, True)
  878. assert integer_nthroot(1234567**7 + 1, 7) == (1234567, False)
  879. assert integer_nthroot(1234567**7 - 1, 7) == (1234566, False)
  880. b = 25**1000
  881. assert integer_nthroot(b, 1000) == (25, True)
  882. assert integer_nthroot(b + 1, 1000) == (25, False)
  883. assert integer_nthroot(b - 1, 1000) == (24, False)
  884. c = 10**400
  885. c2 = c**2
  886. assert integer_nthroot(c2, 2) == (c, True)
  887. assert integer_nthroot(c2 + 1, 2) == (c, False)
  888. assert integer_nthroot(c2 - 1, 2) == (c - 1, False)
  889. assert integer_nthroot(2, 10**10) == (1, False)
  890. p, r = integer_nthroot(int(factorial(10000)), 100)
  891. assert p % (10**10) == 5322420655
  892. assert not r
  893. # Test that this is fast
  894. assert integer_nthroot(2, 10**10) == (1, False)
  895. # output should be int if possible
  896. assert type(integer_nthroot(2**61, 2)[0]) is int
  897. def test_integer_nthroot_overflow():
  898. assert integer_nthroot(10**(50*50), 50) == (10**50, True)
  899. assert integer_nthroot(10**100000, 10000) == (10**10, True)
  900. def test_integer_log():
  901. raises(ValueError, lambda: integer_log(2, 1))
  902. raises(ValueError, lambda: integer_log(0, 2))
  903. raises(ValueError, lambda: integer_log(1.1, 2))
  904. raises(ValueError, lambda: integer_log(1, 2.2))
  905. assert integer_log(1, 2) == (0, True)
  906. assert integer_log(1, 3) == (0, True)
  907. assert integer_log(2, 3) == (0, False)
  908. assert integer_log(3, 3) == (1, True)
  909. assert integer_log(3*2, 3) == (1, False)
  910. assert integer_log(3**2, 3) == (2, True)
  911. assert integer_log(3*4, 3) == (2, False)
  912. assert integer_log(3**3, 3) == (3, True)
  913. assert integer_log(27, 5) == (2, False)
  914. assert integer_log(2, 3) == (0, False)
  915. assert integer_log(-4, -2) == (2, False)
  916. assert integer_log(27, -3) == (3, False)
  917. assert integer_log(-49, 7) == (0, False)
  918. assert integer_log(-49, -7) == (2, False)
  919. def test_isqrt():
  920. from math import sqrt as _sqrt
  921. limit = 4503599761588223
  922. assert int(_sqrt(limit)) == integer_nthroot(limit, 2)[0]
  923. assert int(_sqrt(limit + 1)) != integer_nthroot(limit + 1, 2)[0]
  924. assert isqrt(limit + 1) == integer_nthroot(limit + 1, 2)[0]
  925. assert isqrt(limit + S.Half) == integer_nthroot(limit, 2)[0]
  926. assert isqrt(limit + 1 + S.Half) == integer_nthroot(limit + 1, 2)[0]
  927. assert isqrt(limit + 2 + S.Half) == integer_nthroot(limit + 2, 2)[0]
  928. # Regression tests for https://github.com/sympy/sympy/issues/17034
  929. assert isqrt(4503599761588224) == 67108864
  930. assert isqrt(9999999999999999) == 99999999
  931. # Other corner cases, especially involving non-integers.
  932. raises(ValueError, lambda: isqrt(-1))
  933. raises(ValueError, lambda: isqrt(-10**1000))
  934. raises(ValueError, lambda: isqrt(Rational(-1, 2)))
  935. tiny = Rational(1, 10**1000)
  936. raises(ValueError, lambda: isqrt(-tiny))
  937. assert isqrt(1-tiny) == 0
  938. assert isqrt(4503599761588224-tiny) == 67108864
  939. assert isqrt(10**100 - tiny) == 10**50 - 1
  940. # Check that using an inaccurate math.sqrt doesn't affect the results.
  941. from sympy.core import power
  942. old_sqrt = power._sqrt
  943. power._sqrt = lambda x: 2.999999999
  944. try:
  945. assert isqrt(9) == 3
  946. assert isqrt(10000) == 100
  947. finally:
  948. power._sqrt = old_sqrt
  949. def test_powers_Integer():
  950. """Test Integer._eval_power"""
  951. # check infinity
  952. assert S.One ** S.Infinity is S.NaN
  953. assert S.NegativeOne** S.Infinity is S.NaN
  954. assert S(2) ** S.Infinity is S.Infinity
  955. assert S(-2)** S.Infinity == zoo
  956. assert S(0) ** S.Infinity is S.Zero
  957. # check Nan
  958. assert S.One ** S.NaN is S.NaN
  959. assert S.NegativeOne ** S.NaN is S.NaN
  960. # check for exact roots
  961. assert S.NegativeOne ** Rational(6, 5) == - (-1)**(S.One/5)
  962. assert sqrt(S(4)) == 2
  963. assert sqrt(S(-4)) == I * 2
  964. assert S(16) ** Rational(1, 4) == 2
  965. assert S(-16) ** Rational(1, 4) == 2 * (-1)**Rational(1, 4)
  966. assert S(9) ** Rational(3, 2) == 27
  967. assert S(-9) ** Rational(3, 2) == -27*I
  968. assert S(27) ** Rational(2, 3) == 9
  969. assert S(-27) ** Rational(2, 3) == 9 * (S.NegativeOne ** Rational(2, 3))
  970. assert (-2) ** Rational(-2, 1) == Rational(1, 4)
  971. # not exact roots
  972. assert sqrt(-3) == I*sqrt(3)
  973. assert (3) ** (Rational(3, 2)) == 3 * sqrt(3)
  974. assert (-3) ** (Rational(3, 2)) == - 3 * sqrt(-3)
  975. assert (-3) ** (Rational(5, 2)) == 9 * I * sqrt(3)
  976. assert (-3) ** (Rational(7, 2)) == - I * 27 * sqrt(3)
  977. assert (2) ** (Rational(3, 2)) == 2 * sqrt(2)
  978. assert (2) ** (Rational(-3, 2)) == sqrt(2) / 4
  979. assert (81) ** (Rational(2, 3)) == 9 * (S(3) ** (Rational(2, 3)))
  980. assert (-81) ** (Rational(2, 3)) == 9 * (S(-3) ** (Rational(2, 3)))
  981. assert (-3) ** Rational(-7, 3) == \
  982. -(-1)**Rational(2, 3)*3**Rational(2, 3)/27
  983. assert (-3) ** Rational(-2, 3) == \
  984. -(-1)**Rational(1, 3)*3**Rational(1, 3)/3
  985. # join roots
  986. assert sqrt(6) + sqrt(24) == 3*sqrt(6)
  987. assert sqrt(2) * sqrt(3) == sqrt(6)
  988. # separate symbols & constansts
  989. x = Symbol("x")
  990. assert sqrt(49 * x) == 7 * sqrt(x)
  991. assert sqrt((3 - sqrt(pi)) ** 2) == 3 - sqrt(pi)
  992. # check that it is fast for big numbers
  993. assert (2**64 + 1) ** Rational(4, 3)
  994. assert (2**64 + 1) ** Rational(17, 25)
  995. # negative rational power and negative base
  996. assert (-3) ** Rational(-7, 3) == \
  997. -(-1)**Rational(2, 3)*3**Rational(2, 3)/27
  998. assert (-3) ** Rational(-2, 3) == \
  999. -(-1)**Rational(1, 3)*3**Rational(1, 3)/3
  1000. assert (-2) ** Rational(-10, 3) == \
  1001. (-1)**Rational(2, 3)*2**Rational(2, 3)/16
  1002. assert abs(Pow(-2, Rational(-10, 3)).n() -
  1003. Pow(-2, Rational(-10, 3), evaluate=False).n()) < 1e-16
  1004. # negative base and rational power with some simplification
  1005. assert (-8) ** Rational(2, 5) == \
  1006. 2*(-1)**Rational(2, 5)*2**Rational(1, 5)
  1007. assert (-4) ** Rational(9, 5) == \
  1008. -8*(-1)**Rational(4, 5)*2**Rational(3, 5)
  1009. assert S(1234).factors() == {617: 1, 2: 1}
  1010. assert Rational(2*3, 3*5*7).factors() == {2: 1, 5: -1, 7: -1}
  1011. # test that eval_power factors numbers bigger than
  1012. # the current limit in factor_trial_division (2**15)
  1013. from sympy.ntheory.generate import nextprime
  1014. n = nextprime(2**15)
  1015. assert sqrt(n**2) == n
  1016. assert sqrt(n**3) == n*sqrt(n)
  1017. assert sqrt(4*n) == 2*sqrt(n)
  1018. # check that factors of base with powers sharing gcd with power are removed
  1019. assert (2**4*3)**Rational(1, 6) == 2**Rational(2, 3)*3**Rational(1, 6)
  1020. assert (2**4*3)**Rational(5, 6) == 8*2**Rational(1, 3)*3**Rational(5, 6)
  1021. # check that bases sharing a gcd are exptracted
  1022. assert 2**Rational(1, 3)*3**Rational(1, 4)*6**Rational(1, 5) == \
  1023. 2**Rational(8, 15)*3**Rational(9, 20)
  1024. assert sqrt(8)*24**Rational(1, 3)*6**Rational(1, 5) == \
  1025. 4*2**Rational(7, 10)*3**Rational(8, 15)
  1026. assert sqrt(8)*(-24)**Rational(1, 3)*(-6)**Rational(1, 5) == \
  1027. 4*(-3)**Rational(8, 15)*2**Rational(7, 10)
  1028. assert 2**Rational(1, 3)*2**Rational(8, 9) == 2*2**Rational(2, 9)
  1029. assert 2**Rational(2, 3)*6**Rational(1, 3) == 2*3**Rational(1, 3)
  1030. assert 2**Rational(2, 3)*6**Rational(8, 9) == \
  1031. 2*2**Rational(5, 9)*3**Rational(8, 9)
  1032. assert (-2)**Rational(2, S(3))*(-4)**Rational(1, S(3)) == -2*2**Rational(1, 3)
  1033. assert 3*Pow(3, 2, evaluate=False) == 3**3
  1034. assert 3*Pow(3, Rational(-1, 3), evaluate=False) == 3**Rational(2, 3)
  1035. assert (-2)**Rational(1, 3)*(-3)**Rational(1, 4)*(-5)**Rational(5, 6) == \
  1036. -(-1)**Rational(5, 12)*2**Rational(1, 3)*3**Rational(1, 4) * \
  1037. 5**Rational(5, 6)
  1038. assert Integer(-2)**Symbol('', even=True) == \
  1039. Integer(2)**Symbol('', even=True)
  1040. assert (-1)**Float(.5) == 1.0*I
  1041. def test_powers_Rational():
  1042. """Test Rational._eval_power"""
  1043. # check infinity
  1044. assert S.Half ** S.Infinity == 0
  1045. assert Rational(3, 2) ** S.Infinity is S.Infinity
  1046. assert Rational(-1, 2) ** S.Infinity == 0
  1047. assert Rational(-3, 2) ** S.Infinity == zoo
  1048. # check Nan
  1049. assert Rational(3, 4) ** S.NaN is S.NaN
  1050. assert Rational(-2, 3) ** S.NaN is S.NaN
  1051. # exact roots on numerator
  1052. assert sqrt(Rational(4, 3)) == 2 * sqrt(3) / 3
  1053. assert Rational(4, 3) ** Rational(3, 2) == 8 * sqrt(3) / 9
  1054. assert sqrt(Rational(-4, 3)) == I * 2 * sqrt(3) / 3
  1055. assert Rational(-4, 3) ** Rational(3, 2) == - I * 8 * sqrt(3) / 9
  1056. assert Rational(27, 2) ** Rational(1, 3) == 3 * (2 ** Rational(2, 3)) / 2
  1057. assert Rational(5**3, 8**3) ** Rational(4, 3) == Rational(5**4, 8**4)
  1058. # exact root on denominator
  1059. assert sqrt(Rational(1, 4)) == S.Half
  1060. assert sqrt(Rational(1, -4)) == I * S.Half
  1061. assert sqrt(Rational(3, 4)) == sqrt(3) / 2
  1062. assert sqrt(Rational(3, -4)) == I * sqrt(3) / 2
  1063. assert Rational(5, 27) ** Rational(1, 3) == (5 ** Rational(1, 3)) / 3
  1064. # not exact roots
  1065. assert sqrt(S.Half) == sqrt(2) / 2
  1066. assert sqrt(Rational(-4, 7)) == I * sqrt(Rational(4, 7))
  1067. assert Rational(-3, 2)**Rational(-7, 3) == \
  1068. -4*(-1)**Rational(2, 3)*2**Rational(1, 3)*3**Rational(2, 3)/27
  1069. assert Rational(-3, 2)**Rational(-2, 3) == \
  1070. -(-1)**Rational(1, 3)*2**Rational(2, 3)*3**Rational(1, 3)/3
  1071. assert Rational(-3, 2)**Rational(-10, 3) == \
  1072. 8*(-1)**Rational(2, 3)*2**Rational(1, 3)*3**Rational(2, 3)/81
  1073. assert abs(Pow(Rational(-2, 3), Rational(-7, 4)).n() -
  1074. Pow(Rational(-2, 3), Rational(-7, 4), evaluate=False).n()) < 1e-16
  1075. # negative integer power and negative rational base
  1076. assert Rational(-2, 3) ** Rational(-2, 1) == Rational(9, 4)
  1077. a = Rational(1, 10)
  1078. assert a**Float(a, 2) == Float(a, 2)**Float(a, 2)
  1079. assert Rational(-2, 3)**Symbol('', even=True) == \
  1080. Rational(2, 3)**Symbol('', even=True)
  1081. def test_powers_Float():
  1082. assert str((S('-1/10')**S('3/10')).n()) == str(Float(-.1)**(.3))
  1083. def test_lshift_Integer():
  1084. assert Integer(0) << Integer(2) == Integer(0)
  1085. assert Integer(0) << 2 == Integer(0)
  1086. assert 0 << Integer(2) == Integer(0)
  1087. assert Integer(0b11) << Integer(0) == Integer(0b11)
  1088. assert Integer(0b11) << 0 == Integer(0b11)
  1089. assert 0b11 << Integer(0) == Integer(0b11)
  1090. assert Integer(0b11) << Integer(2) == Integer(0b11 << 2)
  1091. assert Integer(0b11) << 2 == Integer(0b11 << 2)
  1092. assert 0b11 << Integer(2) == Integer(0b11 << 2)
  1093. assert Integer(-0b11) << Integer(2) == Integer(-0b11 << 2)
  1094. assert Integer(-0b11) << 2 == Integer(-0b11 << 2)
  1095. assert -0b11 << Integer(2) == Integer(-0b11 << 2)
  1096. raises(TypeError, lambda: Integer(2) << 0.0)
  1097. raises(TypeError, lambda: 0.0 << Integer(2))
  1098. raises(ValueError, lambda: Integer(1) << Integer(-1))
  1099. def test_rshift_Integer():
  1100. assert Integer(0) >> Integer(2) == Integer(0)
  1101. assert Integer(0) >> 2 == Integer(0)
  1102. assert 0 >> Integer(2) == Integer(0)
  1103. assert Integer(0b11) >> Integer(0) == Integer(0b11)
  1104. assert Integer(0b11) >> 0 == Integer(0b11)
  1105. assert 0b11 >> Integer(0) == Integer(0b11)
  1106. assert Integer(0b11) >> Integer(2) == Integer(0)
  1107. assert Integer(0b11) >> 2 == Integer(0)
  1108. assert 0b11 >> Integer(2) == Integer(0)
  1109. assert Integer(-0b11) >> Integer(2) == Integer(-1)
  1110. assert Integer(-0b11) >> 2 == Integer(-1)
  1111. assert -0b11 >> Integer(2) == Integer(-1)
  1112. assert Integer(0b1100) >> Integer(2) == Integer(0b1100 >> 2)
  1113. assert Integer(0b1100) >> 2 == Integer(0b1100 >> 2)
  1114. assert 0b1100 >> Integer(2) == Integer(0b1100 >> 2)
  1115. assert Integer(-0b1100) >> Integer(2) == Integer(-0b1100 >> 2)
  1116. assert Integer(-0b1100) >> 2 == Integer(-0b1100 >> 2)
  1117. assert -0b1100 >> Integer(2) == Integer(-0b1100 >> 2)
  1118. raises(TypeError, lambda: Integer(0b10) >> 0.0)
  1119. raises(TypeError, lambda: 0.0 >> Integer(2))
  1120. raises(ValueError, lambda: Integer(1) >> Integer(-1))
  1121. def test_and_Integer():
  1122. assert Integer(0b01010101) & Integer(0b10101010) == Integer(0)
  1123. assert Integer(0b01010101) & 0b10101010 == Integer(0)
  1124. assert 0b01010101 & Integer(0b10101010) == Integer(0)
  1125. assert Integer(0b01010101) & Integer(0b11011011) == Integer(0b01010001)
  1126. assert Integer(0b01010101) & 0b11011011 == Integer(0b01010001)
  1127. assert 0b01010101 & Integer(0b11011011) == Integer(0b01010001)
  1128. assert -Integer(0b01010101) & Integer(0b11011011) == Integer(-0b01010101 & 0b11011011)
  1129. assert Integer(-0b01010101) & 0b11011011 == Integer(-0b01010101 & 0b11011011)
  1130. assert -0b01010101 & Integer(0b11011011) == Integer(-0b01010101 & 0b11011011)
  1131. assert Integer(0b01010101) & -Integer(0b11011011) == Integer(0b01010101 & -0b11011011)
  1132. assert Integer(0b01010101) & -0b11011011 == Integer(0b01010101 & -0b11011011)
  1133. assert 0b01010101 & Integer(-0b11011011) == Integer(0b01010101 & -0b11011011)
  1134. raises(TypeError, lambda: Integer(2) & 0.0)
  1135. raises(TypeError, lambda: 0.0 & Integer(2))
  1136. def test_xor_Integer():
  1137. assert Integer(0b01010101) ^ Integer(0b11111111) == Integer(0b10101010)
  1138. assert Integer(0b01010101) ^ 0b11111111 == Integer(0b10101010)
  1139. assert 0b01010101 ^ Integer(0b11111111) == Integer(0b10101010)
  1140. assert Integer(0b01010101) ^ Integer(0b11011011) == Integer(0b10001110)
  1141. assert Integer(0b01010101) ^ 0b11011011 == Integer(0b10001110)
  1142. assert 0b01010101 ^ Integer(0b11011011) == Integer(0b10001110)
  1143. assert -Integer(0b01010101) ^ Integer(0b11011011) == Integer(-0b01010101 ^ 0b11011011)
  1144. assert Integer(-0b01010101) ^ 0b11011011 == Integer(-0b01010101 ^ 0b11011011)
  1145. assert -0b01010101 ^ Integer(0b11011011) == Integer(-0b01010101 ^ 0b11011011)
  1146. assert Integer(0b01010101) ^ -Integer(0b11011011) == Integer(0b01010101 ^ -0b11011011)
  1147. assert Integer(0b01010101) ^ -0b11011011 == Integer(0b01010101 ^ -0b11011011)
  1148. assert 0b01010101 ^ Integer(-0b11011011) == Integer(0b01010101 ^ -0b11011011)
  1149. raises(TypeError, lambda: Integer(2) ^ 0.0)
  1150. raises(TypeError, lambda: 0.0 ^ Integer(2))
  1151. def test_or_Integer():
  1152. assert Integer(0b01010101) | Integer(0b10101010) == Integer(0b11111111)
  1153. assert Integer(0b01010101) | 0b10101010 == Integer(0b11111111)
  1154. assert 0b01010101 | Integer(0b10101010) == Integer(0b11111111)
  1155. assert Integer(0b01010101) | Integer(0b11011011) == Integer(0b11011111)
  1156. assert Integer(0b01010101) | 0b11011011 == Integer(0b11011111)
  1157. assert 0b01010101 | Integer(0b11011011) == Integer(0b11011111)
  1158. assert -Integer(0b01010101) | Integer(0b11011011) == Integer(-0b01010101 | 0b11011011)
  1159. assert Integer(-0b01010101) | 0b11011011 == Integer(-0b01010101 | 0b11011011)
  1160. assert -0b01010101 | Integer(0b11011011) == Integer(-0b01010101 | 0b11011011)
  1161. assert Integer(0b01010101) | -Integer(0b11011011) == Integer(0b01010101 | -0b11011011)
  1162. assert Integer(0b01010101) | -0b11011011 == Integer(0b01010101 | -0b11011011)
  1163. assert 0b01010101 | Integer(-0b11011011) == Integer(0b01010101 | -0b11011011)
  1164. raises(TypeError, lambda: Integer(2) | 0.0)
  1165. raises(TypeError, lambda: 0.0 | Integer(2))
  1166. def test_invert_Integer():
  1167. assert ~Integer(0b01010101) == Integer(-0b01010110)
  1168. assert ~Integer(0b01010101) == Integer(~0b01010101)
  1169. assert ~(~Integer(0b01010101)) == Integer(0b01010101)
  1170. def test_abs1():
  1171. assert Rational(1, 6) != Rational(-1, 6)
  1172. assert abs(Rational(1, 6)) == abs(Rational(-1, 6))
  1173. def test_accept_int():
  1174. assert Float(4) == 4
  1175. def test_dont_accept_str():
  1176. assert Float("0.2") != "0.2"
  1177. assert not (Float("0.2") == "0.2")
  1178. def test_int():
  1179. a = Rational(5)
  1180. assert int(a) == 5
  1181. a = Rational(9, 10)
  1182. assert int(a) == int(-a) == 0
  1183. assert 1/(-1)**Rational(2, 3) == -(-1)**Rational(1, 3)
  1184. # issue 10368
  1185. a = Rational(32442016954, 78058255275)
  1186. assert type(int(a)) is type(int(-a)) is int
  1187. def test_int_NumberSymbols():
  1188. assert int(Catalan) == 0
  1189. assert int(EulerGamma) == 0
  1190. assert int(pi) == 3
  1191. assert int(E) == 2
  1192. assert int(GoldenRatio) == 1
  1193. assert int(TribonacciConstant) == 1
  1194. for i in [Catalan, E, EulerGamma, GoldenRatio, TribonacciConstant, pi]:
  1195. a, b = i.approximation_interval(Integer)
  1196. ia = int(i)
  1197. assert ia == a
  1198. assert isinstance(ia, int)
  1199. assert b == a + 1
  1200. assert a.is_Integer and b.is_Integer
  1201. def test_real_bug():
  1202. x = Symbol("x")
  1203. assert str(2.0*x*x) in ["(2.0*x)*x", "2.0*x**2", "2.00000000000000*x**2"]
  1204. assert str(2.1*x*x) != "(2.0*x)*x"
  1205. def test_bug_sqrt():
  1206. assert ((sqrt(Rational(2)) + 1)*(sqrt(Rational(2)) - 1)).expand() == 1
  1207. def test_pi_Pi():
  1208. "Test that pi (instance) is imported, but Pi (class) is not"
  1209. from sympy import pi # noqa
  1210. with raises(ImportError):
  1211. from sympy import Pi # noqa
  1212. def test_no_len():
  1213. # there should be no len for numbers
  1214. raises(TypeError, lambda: len(Rational(2)))
  1215. raises(TypeError, lambda: len(Rational(2, 3)))
  1216. raises(TypeError, lambda: len(Integer(2)))
  1217. def test_issue_3321():
  1218. assert sqrt(Rational(1, 5)) == Rational(1, 5)**S.Half
  1219. assert 5 * sqrt(Rational(1, 5)) == sqrt(5)
  1220. def test_issue_3692():
  1221. assert ((-1)**Rational(1, 6)).expand(complex=True) == I/2 + sqrt(3)/2
  1222. assert ((-5)**Rational(1, 6)).expand(complex=True) == \
  1223. 5**Rational(1, 6)*I/2 + 5**Rational(1, 6)*sqrt(3)/2
  1224. assert ((-64)**Rational(1, 6)).expand(complex=True) == I + sqrt(3)
  1225. def test_issue_3423():
  1226. x = Symbol("x")
  1227. assert sqrt(x - 1).as_base_exp() == (x - 1, S.Half)
  1228. assert sqrt(x - 1) != I*sqrt(1 - x)
  1229. def test_issue_3449():
  1230. x = Symbol("x")
  1231. assert sqrt(x - 1).subs(x, 5) == 2
  1232. def test_issue_13890():
  1233. x = Symbol("x")
  1234. e = (-x/4 - S.One/12)**x - 1
  1235. f = simplify(e)
  1236. a = Rational(9, 5)
  1237. assert abs(e.subs(x,a).evalf() - f.subs(x,a).evalf()) < 1e-15
  1238. def test_Integer_factors():
  1239. def F(i):
  1240. return Integer(i).factors()
  1241. assert F(1) == {}
  1242. assert F(2) == {2: 1}
  1243. assert F(3) == {3: 1}
  1244. assert F(4) == {2: 2}
  1245. assert F(5) == {5: 1}
  1246. assert F(6) == {2: 1, 3: 1}
  1247. assert F(7) == {7: 1}
  1248. assert F(8) == {2: 3}
  1249. assert F(9) == {3: 2}
  1250. assert F(10) == {2: 1, 5: 1}
  1251. assert F(11) == {11: 1}
  1252. assert F(12) == {2: 2, 3: 1}
  1253. assert F(13) == {13: 1}
  1254. assert F(14) == {2: 1, 7: 1}
  1255. assert F(15) == {3: 1, 5: 1}
  1256. assert F(16) == {2: 4}
  1257. assert F(17) == {17: 1}
  1258. assert F(18) == {2: 1, 3: 2}
  1259. assert F(19) == {19: 1}
  1260. assert F(20) == {2: 2, 5: 1}
  1261. assert F(21) == {3: 1, 7: 1}
  1262. assert F(22) == {2: 1, 11: 1}
  1263. assert F(23) == {23: 1}
  1264. assert F(24) == {2: 3, 3: 1}
  1265. assert F(25) == {5: 2}
  1266. assert F(26) == {2: 1, 13: 1}
  1267. assert F(27) == {3: 3}
  1268. assert F(28) == {2: 2, 7: 1}
  1269. assert F(29) == {29: 1}
  1270. assert F(30) == {2: 1, 3: 1, 5: 1}
  1271. assert F(31) == {31: 1}
  1272. assert F(32) == {2: 5}
  1273. assert F(33) == {3: 1, 11: 1}
  1274. assert F(34) == {2: 1, 17: 1}
  1275. assert F(35) == {5: 1, 7: 1}
  1276. assert F(36) == {2: 2, 3: 2}
  1277. assert F(37) == {37: 1}
  1278. assert F(38) == {2: 1, 19: 1}
  1279. assert F(39) == {3: 1, 13: 1}
  1280. assert F(40) == {2: 3, 5: 1}
  1281. assert F(41) == {41: 1}
  1282. assert F(42) == {2: 1, 3: 1, 7: 1}
  1283. assert F(43) == {43: 1}
  1284. assert F(44) == {2: 2, 11: 1}
  1285. assert F(45) == {3: 2, 5: 1}
  1286. assert F(46) == {2: 1, 23: 1}
  1287. assert F(47) == {47: 1}
  1288. assert F(48) == {2: 4, 3: 1}
  1289. assert F(49) == {7: 2}
  1290. assert F(50) == {2: 1, 5: 2}
  1291. assert F(51) == {3: 1, 17: 1}
  1292. def test_Rational_factors():
  1293. def F(p, q, visual=None):
  1294. return Rational(p, q).factors(visual=visual)
  1295. assert F(2, 3) == {2: 1, 3: -1}
  1296. assert F(2, 9) == {2: 1, 3: -2}
  1297. assert F(2, 15) == {2: 1, 3: -1, 5: -1}
  1298. assert F(6, 10) == {3: 1, 5: -1}
  1299. def test_issue_4107():
  1300. assert pi*(E + 10) + pi*(-E - 10) != 0
  1301. assert pi*(E + 10**10) + pi*(-E - 10**10) != 0
  1302. assert pi*(E + 10**20) + pi*(-E - 10**20) != 0
  1303. assert pi*(E + 10**80) + pi*(-E - 10**80) != 0
  1304. assert (pi*(E + 10) + pi*(-E - 10)).expand() == 0
  1305. assert (pi*(E + 10**10) + pi*(-E - 10**10)).expand() == 0
  1306. assert (pi*(E + 10**20) + pi*(-E - 10**20)).expand() == 0
  1307. assert (pi*(E + 10**80) + pi*(-E - 10**80)).expand() == 0
  1308. def test_IntegerInteger():
  1309. a = Integer(4)
  1310. b = Integer(a)
  1311. assert a == b
  1312. def test_Rational_gcd_lcm_cofactors():
  1313. assert Integer(4).gcd(2) == Integer(2)
  1314. assert Integer(4).lcm(2) == Integer(4)
  1315. assert Integer(4).gcd(Integer(2)) == Integer(2)
  1316. assert Integer(4).lcm(Integer(2)) == Integer(4)
  1317. a, b = 720**99911, 480**12342
  1318. assert Integer(a).lcm(b) == a*b/Integer(a).gcd(b)
  1319. assert Integer(4).gcd(3) == Integer(1)
  1320. assert Integer(4).lcm(3) == Integer(12)
  1321. assert Integer(4).gcd(Integer(3)) == Integer(1)
  1322. assert Integer(4).lcm(Integer(3)) == Integer(12)
  1323. assert Rational(4, 3).gcd(2) == Rational(2, 3)
  1324. assert Rational(4, 3).lcm(2) == Integer(4)
  1325. assert Rational(4, 3).gcd(Integer(2)) == Rational(2, 3)
  1326. assert Rational(4, 3).lcm(Integer(2)) == Integer(4)
  1327. assert Integer(4).gcd(Rational(2, 9)) == Rational(2, 9)
  1328. assert Integer(4).lcm(Rational(2, 9)) == Integer(4)
  1329. assert Rational(4, 3).gcd(Rational(2, 9)) == Rational(2, 9)
  1330. assert Rational(4, 3).lcm(Rational(2, 9)) == Rational(4, 3)
  1331. assert Rational(4, 5).gcd(Rational(2, 9)) == Rational(2, 45)
  1332. assert Rational(4, 5).lcm(Rational(2, 9)) == Integer(4)
  1333. assert Rational(5, 9).lcm(Rational(3, 7)) == Rational(Integer(5).lcm(3),Integer(9).gcd(7))
  1334. assert Integer(4).cofactors(2) == (Integer(2), Integer(2), Integer(1))
  1335. assert Integer(4).cofactors(Integer(2)) == \
  1336. (Integer(2), Integer(2), Integer(1))
  1337. assert Integer(4).gcd(Float(2.0)) == Float(1.0)
  1338. assert Integer(4).lcm(Float(2.0)) == Float(8.0)
  1339. assert Integer(4).cofactors(Float(2.0)) == (Float(1.0), Float(4.0), Float(2.0))
  1340. assert S.Half.gcd(Float(2.0)) == Float(1.0)
  1341. assert S.Half.lcm(Float(2.0)) == Float(1.0)
  1342. assert S.Half.cofactors(Float(2.0)) == \
  1343. (Float(1.0), Float(0.5), Float(2.0))
  1344. def test_Float_gcd_lcm_cofactors():
  1345. assert Float(2.0).gcd(Integer(4)) == Float(1.0)
  1346. assert Float(2.0).lcm(Integer(4)) == Float(8.0)
  1347. assert Float(2.0).cofactors(Integer(4)) == (Float(1.0), Float(2.0), Float(4.0))
  1348. assert Float(2.0).gcd(S.Half) == Float(1.0)
  1349. assert Float(2.0).lcm(S.Half) == Float(1.0)
  1350. assert Float(2.0).cofactors(S.Half) == \
  1351. (Float(1.0), Float(2.0), Float(0.5))
  1352. def test_issue_4611():
  1353. assert abs(pi._evalf(50) - 3.14159265358979) < 1e-10
  1354. assert abs(E._evalf(50) - 2.71828182845905) < 1e-10
  1355. assert abs(Catalan._evalf(50) - 0.915965594177219) < 1e-10
  1356. assert abs(EulerGamma._evalf(50) - 0.577215664901533) < 1e-10
  1357. assert abs(GoldenRatio._evalf(50) - 1.61803398874989) < 1e-10
  1358. assert abs(TribonacciConstant._evalf(50) - 1.83928675521416) < 1e-10
  1359. x = Symbol("x")
  1360. assert (pi + x).evalf() == pi.evalf() + x
  1361. assert (E + x).evalf() == E.evalf() + x
  1362. assert (Catalan + x).evalf() == Catalan.evalf() + x
  1363. assert (EulerGamma + x).evalf() == EulerGamma.evalf() + x
  1364. assert (GoldenRatio + x).evalf() == GoldenRatio.evalf() + x
  1365. assert (TribonacciConstant + x).evalf() == TribonacciConstant.evalf() + x
  1366. @conserve_mpmath_dps
  1367. def test_conversion_to_mpmath():
  1368. assert mpmath.mpmathify(Integer(1)) == mpmath.mpf(1)
  1369. assert mpmath.mpmathify(S.Half) == mpmath.mpf(0.5)
  1370. assert mpmath.mpmathify(Float('1.23', 15)) == mpmath.mpf('1.23')
  1371. assert mpmath.mpmathify(I) == mpmath.mpc(1j)
  1372. assert mpmath.mpmathify(1 + 2*I) == mpmath.mpc(1 + 2j)
  1373. assert mpmath.mpmathify(1.0 + 2*I) == mpmath.mpc(1 + 2j)
  1374. assert mpmath.mpmathify(1 + 2.0*I) == mpmath.mpc(1 + 2j)
  1375. assert mpmath.mpmathify(1.0 + 2.0*I) == mpmath.mpc(1 + 2j)
  1376. assert mpmath.mpmathify(S.Half + S.Half*I) == mpmath.mpc(0.5 + 0.5j)
  1377. assert mpmath.mpmathify(2*I) == mpmath.mpc(2j)
  1378. assert mpmath.mpmathify(2.0*I) == mpmath.mpc(2j)
  1379. assert mpmath.mpmathify(S.Half*I) == mpmath.mpc(0.5j)
  1380. mpmath.mp.dps = 100
  1381. assert mpmath.mpmathify(pi.evalf(100) + pi.evalf(100)*I) == mpmath.pi + mpmath.pi*mpmath.j
  1382. assert mpmath.mpmathify(pi.evalf(100)*I) == mpmath.pi*mpmath.j
  1383. def test_relational():
  1384. # real
  1385. x = S(.1)
  1386. assert (x != cos) is True
  1387. assert (x == cos) is False
  1388. # rational
  1389. x = Rational(1, 3)
  1390. assert (x != cos) is True
  1391. assert (x == cos) is False
  1392. # integer defers to rational so these tests are omitted
  1393. # number symbol
  1394. x = pi
  1395. assert (x != cos) is True
  1396. assert (x == cos) is False
  1397. def test_Integer_as_index():
  1398. assert 'hello'[Integer(2):] == 'llo'
  1399. def test_Rational_int():
  1400. assert int( Rational(7, 5)) == 1
  1401. assert int( S.Half) == 0
  1402. assert int(Rational(-1, 2)) == 0
  1403. assert int(-Rational(7, 5)) == -1
  1404. def test_zoo():
  1405. b = Symbol('b', finite=True)
  1406. nz = Symbol('nz', nonzero=True)
  1407. p = Symbol('p', positive=True)
  1408. n = Symbol('n', negative=True)
  1409. im = Symbol('i', imaginary=True)
  1410. c = Symbol('c', complex=True)
  1411. pb = Symbol('pb', positive=True)
  1412. nb = Symbol('nb', negative=True)
  1413. imb = Symbol('ib', imaginary=True, finite=True)
  1414. for i in [I, S.Infinity, S.NegativeInfinity, S.Zero, S.One, S.Pi, S.Half, S(3), log(3),
  1415. b, nz, p, n, im, pb, nb, imb, c]:
  1416. if i.is_finite and (i.is_real or i.is_imaginary):
  1417. assert i + zoo is zoo
  1418. assert i - zoo is zoo
  1419. assert zoo + i is zoo
  1420. assert zoo - i is zoo
  1421. elif i.is_finite is not False:
  1422. assert (i + zoo).is_Add
  1423. assert (i - zoo).is_Add
  1424. assert (zoo + i).is_Add
  1425. assert (zoo - i).is_Add
  1426. else:
  1427. assert (i + zoo) is S.NaN
  1428. assert (i - zoo) is S.NaN
  1429. assert (zoo + i) is S.NaN
  1430. assert (zoo - i) is S.NaN
  1431. if fuzzy_not(i.is_zero) and (i.is_extended_real or i.is_imaginary):
  1432. assert i*zoo is zoo
  1433. assert zoo*i is zoo
  1434. elif i.is_zero:
  1435. assert i*zoo is S.NaN
  1436. assert zoo*i is S.NaN
  1437. else:
  1438. assert (i*zoo).is_Mul
  1439. assert (zoo*i).is_Mul
  1440. if fuzzy_not((1/i).is_zero) and (i.is_real or i.is_imaginary):
  1441. assert zoo/i is zoo
  1442. elif (1/i).is_zero:
  1443. assert zoo/i is S.NaN
  1444. elif i.is_zero:
  1445. assert zoo/i is zoo
  1446. else:
  1447. assert (zoo/i).is_Mul
  1448. assert (I*oo).is_Mul # allow directed infinity
  1449. assert zoo + zoo is S.NaN
  1450. assert zoo * zoo is zoo
  1451. assert zoo - zoo is S.NaN
  1452. assert zoo/zoo is S.NaN
  1453. assert zoo**zoo is S.NaN
  1454. assert zoo**0 is S.One
  1455. assert zoo**2 is zoo
  1456. assert 1/zoo is S.Zero
  1457. assert Mul.flatten([S.NegativeOne, oo, S(0)]) == ([S.NaN], [], None)
  1458. def test_issue_4122():
  1459. x = Symbol('x', nonpositive=True)
  1460. assert oo + x is oo
  1461. x = Symbol('x', extended_nonpositive=True)
  1462. assert (oo + x).is_Add
  1463. x = Symbol('x', finite=True)
  1464. assert (oo + x).is_Add # x could be imaginary
  1465. x = Symbol('x', nonnegative=True)
  1466. assert oo + x is oo
  1467. x = Symbol('x', extended_nonnegative=True)
  1468. assert oo + x is oo
  1469. x = Symbol('x', finite=True, real=True)
  1470. assert oo + x is oo
  1471. # similarly for negative infinity
  1472. x = Symbol('x', nonnegative=True)
  1473. assert -oo + x is -oo
  1474. x = Symbol('x', extended_nonnegative=True)
  1475. assert (-oo + x).is_Add
  1476. x = Symbol('x', finite=True)
  1477. assert (-oo + x).is_Add
  1478. x = Symbol('x', nonpositive=True)
  1479. assert -oo + x is -oo
  1480. x = Symbol('x', extended_nonpositive=True)
  1481. assert -oo + x is -oo
  1482. x = Symbol('x', finite=True, real=True)
  1483. assert -oo + x is -oo
  1484. def test_GoldenRatio_expand():
  1485. assert GoldenRatio.expand(func=True) == S.Half + sqrt(5)/2
  1486. def test_TribonacciConstant_expand():
  1487. assert TribonacciConstant.expand(func=True) == \
  1488. (1 + cbrt(19 - 3*sqrt(33)) + cbrt(19 + 3*sqrt(33))) / 3
  1489. def test_as_content_primitive():
  1490. assert S.Zero.as_content_primitive() == (1, 0)
  1491. assert S.Half.as_content_primitive() == (S.Half, 1)
  1492. assert (Rational(-1, 2)).as_content_primitive() == (S.Half, -1)
  1493. assert S(3).as_content_primitive() == (3, 1)
  1494. assert S(3.1).as_content_primitive() == (1, 3.1)
  1495. def test_hashing_sympy_integers():
  1496. # Test for issue 5072
  1497. assert {Integer(3)} == {int(3)}
  1498. assert hash(Integer(4)) == hash(int(4))
  1499. def test_rounding_issue_4172():
  1500. assert int((E**100).round()) == \
  1501. 26881171418161354484126255515800135873611119
  1502. assert int((pi**100).round()) == \
  1503. 51878483143196131920862615246303013562686760680406
  1504. assert int((Rational(1)/EulerGamma**100).round()) == \
  1505. 734833795660954410469466
  1506. @XFAIL
  1507. def test_mpmath_issues():
  1508. from mpmath.libmp.libmpf import _normalize
  1509. import mpmath.libmp as mlib
  1510. rnd = mlib.round_nearest
  1511. mpf = (0, int(0), -123, -1, 53, rnd) # nan
  1512. assert _normalize(mpf, 53) != (0, int(0), 0, 0)
  1513. mpf = (0, int(0), -456, -2, 53, rnd) # +inf
  1514. assert _normalize(mpf, 53) != (0, int(0), 0, 0)
  1515. mpf = (1, int(0), -789, -3, 53, rnd) # -inf
  1516. assert _normalize(mpf, 53) != (0, int(0), 0, 0)
  1517. from mpmath.libmp.libmpf import fnan
  1518. assert mlib.mpf_eq(fnan, fnan)
  1519. def test_Catalan_EulerGamma_prec():
  1520. n = GoldenRatio
  1521. f = Float(n.n(), 5)
  1522. assert f._mpf_ == (0, int(212079), -17, 18)
  1523. assert f._prec == 20
  1524. assert n._as_mpf_val(20) == f._mpf_
  1525. n = EulerGamma
  1526. f = Float(n.n(), 5)
  1527. assert f._mpf_ == (0, int(302627), -19, 19)
  1528. assert f._prec == 20
  1529. assert n._as_mpf_val(20) == f._mpf_
  1530. def test_Catalan_rewrite():
  1531. k = Dummy('k', integer=True, nonnegative=True)
  1532. assert Catalan.rewrite(Sum).dummy_eq(
  1533. Sum((-1)**k/(2*k + 1)**2, (k, 0, oo)))
  1534. assert Catalan.rewrite() == Catalan
  1535. def test_bool_eq():
  1536. assert 0 == False
  1537. assert S(0) == False
  1538. assert S(0) != S.false
  1539. assert 1 == True
  1540. assert S.One == True
  1541. assert S.One != S.true
  1542. def test_Float_eq():
  1543. # all .5 values are the same
  1544. assert Float(.5, 10) == Float(.5, 11) == Float(.5, 1)
  1545. # but floats that aren't exact in base-2 still
  1546. # don't compare the same because they have different
  1547. # underlying mpf values
  1548. assert Float(.12, 3) != Float(.12, 4)
  1549. assert Float(.12, 3) != .12
  1550. assert 0.12 != Float(.12, 3)
  1551. assert Float('.12', 22) != .12
  1552. # issue 11707
  1553. # but Float/Rational -- except for 0 --
  1554. # are exact so Rational(x) = Float(y) only if
  1555. # Rational(x) == Rational(Float(y))
  1556. assert Float('1.1') != Rational(11, 10)
  1557. assert Rational(11, 10) != Float('1.1')
  1558. # coverage
  1559. assert not Float(3) == 2
  1560. assert not Float(2**2) == S.Half
  1561. assert Float(2**2) == 4
  1562. assert not Float(2**-2) == 1
  1563. assert Float(2**-1) == S.Half
  1564. assert not Float(2*3) == 3
  1565. assert not Float(2*3) == S.Half
  1566. assert Float(2*3) == 6
  1567. assert not Float(2*3) == 8
  1568. assert Float(.75) == Rational(3, 4)
  1569. assert Float(5/18) == 5/18
  1570. # 4473
  1571. assert Float(2.) != 3
  1572. assert Float((0,1,-3)) == S.One/8
  1573. assert Float((0,1,-3)) != S.One/9
  1574. # 16196
  1575. assert 2 == Float(2) # as per Python
  1576. # but in a computation...
  1577. assert t**2 != t**2.0
  1578. def test_issue_6640():
  1579. from mpmath.libmp.libmpf import finf, fninf
  1580. # fnan is not included because Float no longer returns fnan,
  1581. # but otherwise, the same sort of test could apply
  1582. assert Float(finf).is_zero is False
  1583. assert Float(fninf).is_zero is False
  1584. assert bool(Float(0)) is False
  1585. def test_issue_6349():
  1586. assert Float('23.e3', '')._prec == 10
  1587. assert Float('23e3', '')._prec == 20
  1588. assert Float('23000', '')._prec == 20
  1589. assert Float('-23000', '')._prec == 20
  1590. def test_mpf_norm():
  1591. assert mpf_norm((1, 0, 1, 0), 10) == mpf('0')._mpf_
  1592. assert Float._new((1, 0, 1, 0), 10)._mpf_ == mpf('0')._mpf_
  1593. def test_latex():
  1594. assert latex(pi) == r"\pi"
  1595. assert latex(E) == r"e"
  1596. assert latex(GoldenRatio) == r"\phi"
  1597. assert latex(TribonacciConstant) == r"\text{TribonacciConstant}"
  1598. assert latex(EulerGamma) == r"\gamma"
  1599. assert latex(oo) == r"\infty"
  1600. assert latex(-oo) == r"-\infty"
  1601. assert latex(zoo) == r"\tilde{\infty}"
  1602. assert latex(nan) == r"\text{NaN}"
  1603. assert latex(I) == r"i"
  1604. def test_issue_7742():
  1605. assert -oo % 1 is nan
  1606. def test_simplify_AlgebraicNumber():
  1607. A = AlgebraicNumber
  1608. e = 3**(S.One/6)*(3 + (135 + 78*sqrt(3))**Rational(2, 3))/(45 + 26*sqrt(3))**(S.One/3)
  1609. assert simplify(A(e)) == A(12) # wester test_C20
  1610. e = (41 + 29*sqrt(2))**(S.One/5)
  1611. assert simplify(A(e)) == A(1 + sqrt(2)) # wester test_C21
  1612. e = (3 + 4*I)**Rational(3, 2)
  1613. assert simplify(A(e)) == A(2 + 11*I) # issue 4401
  1614. def test_Float_idempotence():
  1615. x = Float('1.23', '')
  1616. y = Float(x)
  1617. z = Float(x, 15)
  1618. assert same_and_same_prec(y, x)
  1619. assert not same_and_same_prec(z, x)
  1620. x = Float(10**20)
  1621. y = Float(x)
  1622. z = Float(x, 15)
  1623. assert same_and_same_prec(y, x)
  1624. assert not same_and_same_prec(z, x)
  1625. def test_comp1():
  1626. # sqrt(2) = 1.414213 5623730950...
  1627. a = sqrt(2).n(7)
  1628. assert comp(a, 1.4142129) is False
  1629. assert comp(a, 1.4142130)
  1630. # ...
  1631. assert comp(a, 1.4142141)
  1632. assert comp(a, 1.4142142) is False
  1633. assert comp(sqrt(2).n(2), '1.4')
  1634. assert comp(sqrt(2).n(2), Float(1.4, 2), '')
  1635. assert comp(sqrt(2).n(2), 1.4, '')
  1636. assert comp(sqrt(2).n(2), Float(1.4, 3), '') is False
  1637. assert comp(sqrt(2) + sqrt(3)*I, 1.4 + 1.7*I, .1)
  1638. assert not comp(sqrt(2) + sqrt(3)*I, (1.5 + 1.7*I)*0.89, .1)
  1639. assert comp(sqrt(2) + sqrt(3)*I, (1.5 + 1.7*I)*0.90, .1)
  1640. assert comp(sqrt(2) + sqrt(3)*I, (1.5 + 1.7*I)*1.07, .1)
  1641. assert not comp(sqrt(2) + sqrt(3)*I, (1.5 + 1.7*I)*1.08, .1)
  1642. assert [(i, j)
  1643. for i in range(130, 150)
  1644. for j in range(170, 180)
  1645. if comp((sqrt(2)+ I*sqrt(3)).n(3), i/100. + I*j/100.)] == [
  1646. (141, 173), (142, 173)]
  1647. raises(ValueError, lambda: comp(t, '1'))
  1648. raises(ValueError, lambda: comp(t, 1))
  1649. assert comp(0, 0.0)
  1650. assert comp(.5, S.Half)
  1651. assert comp(2 + sqrt(2), 2.0 + sqrt(2))
  1652. assert not comp(0, 1)
  1653. assert not comp(2, sqrt(2))
  1654. assert not comp(2 + I, 2.0 + sqrt(2))
  1655. assert not comp(2.0 + sqrt(2), 2 + I)
  1656. assert not comp(2.0 + sqrt(2), sqrt(3))
  1657. assert comp(1/pi.n(4), 0.3183, 1e-5)
  1658. assert not comp(1/pi.n(4), 0.3183, 8e-6)
  1659. def test_issue_9491():
  1660. assert oo**zoo is nan
  1661. def test_issue_10063():
  1662. assert 2**Float(3) == Float(8)
  1663. def test_issue_10020():
  1664. assert oo**I is S.NaN
  1665. assert oo**(1 + I) is S.ComplexInfinity
  1666. assert oo**(-1 + I) is S.Zero
  1667. assert (-oo)**I is S.NaN
  1668. assert (-oo)**(-1 + I) is S.Zero
  1669. assert oo**t == Pow(oo, t, evaluate=False)
  1670. assert (-oo)**t == Pow(-oo, t, evaluate=False)
  1671. def test_invert_numbers():
  1672. assert S(2).invert(5) == 3
  1673. assert S(2).invert(Rational(5, 2)) == S.Half
  1674. assert S(2).invert(5.) == S.Half
  1675. assert S(2).invert(S(5)) == 3
  1676. assert S(2.).invert(5) == 0.5
  1677. assert S(sqrt(2)).invert(5) == 1/sqrt(2)
  1678. assert S(sqrt(2)).invert(sqrt(3)) == 1/sqrt(2)
  1679. def test_mod_inverse():
  1680. assert mod_inverse(3, 11) == 4
  1681. assert mod_inverse(5, 11) == 9
  1682. assert mod_inverse(21124921, 521512) == 7713
  1683. assert mod_inverse(124215421, 5125) == 2981
  1684. assert mod_inverse(214, 12515) == 1579
  1685. assert mod_inverse(5823991, 3299) == 1442
  1686. assert mod_inverse(123, 44) == 39
  1687. assert mod_inverse(2, 5) == 3
  1688. assert mod_inverse(-2, 5) == 2
  1689. assert mod_inverse(2, -5) == -2
  1690. assert mod_inverse(-2, -5) == -3
  1691. assert mod_inverse(-3, -7) == -5
  1692. x = Symbol('x')
  1693. assert S(2).invert(x) == S.Half
  1694. raises(TypeError, lambda: mod_inverse(2, x))
  1695. raises(ValueError, lambda: mod_inverse(2, S.Half))
  1696. raises(ValueError, lambda: mod_inverse(2, cos(1)**2 + sin(1)**2))
  1697. def test_golden_ratio_rewrite_as_sqrt():
  1698. assert GoldenRatio.rewrite(sqrt) == S.Half + sqrt(5)*S.Half
  1699. def test_tribonacci_constant_rewrite_as_sqrt():
  1700. assert TribonacciConstant.rewrite(sqrt) == \
  1701. (1 + cbrt(19 - 3*sqrt(33)) + cbrt(19 + 3*sqrt(33))) / 3
  1702. def test_comparisons_with_unknown_type():
  1703. class Foo:
  1704. """
  1705. Class that is unaware of Basic, and relies on both classes returning
  1706. the NotImplemented singleton for equivalence to evaluate to False.
  1707. """
  1708. ni, nf, nr = Integer(3), Float(1.0), Rational(1, 3)
  1709. foo = Foo()
  1710. for n in ni, nf, nr, oo, -oo, zoo, nan:
  1711. assert n != foo
  1712. assert foo != n
  1713. assert not n == foo
  1714. assert not foo == n
  1715. raises(TypeError, lambda: n < foo)
  1716. raises(TypeError, lambda: foo > n)
  1717. raises(TypeError, lambda: n > foo)
  1718. raises(TypeError, lambda: foo < n)
  1719. raises(TypeError, lambda: n <= foo)
  1720. raises(TypeError, lambda: foo >= n)
  1721. raises(TypeError, lambda: n >= foo)
  1722. raises(TypeError, lambda: foo <= n)
  1723. class Bar:
  1724. """
  1725. Class that considers itself equal to any instance of Number except
  1726. infinities and nans, and relies on SymPy types returning the
  1727. NotImplemented singleton for symmetric equality relations.
  1728. """
  1729. def __eq__(self, other):
  1730. if other in (oo, -oo, zoo, nan):
  1731. return False
  1732. if isinstance(other, Number):
  1733. return True
  1734. return NotImplemented
  1735. def __ne__(self, other):
  1736. return not self == other
  1737. bar = Bar()
  1738. for n in ni, nf, nr:
  1739. assert n == bar
  1740. assert bar == n
  1741. assert not n != bar
  1742. assert not bar != n
  1743. for n in oo, -oo, zoo, nan:
  1744. assert n != bar
  1745. assert bar != n
  1746. assert not n == bar
  1747. assert not bar == n
  1748. for n in ni, nf, nr, oo, -oo, zoo, nan:
  1749. raises(TypeError, lambda: n < bar)
  1750. raises(TypeError, lambda: bar > n)
  1751. raises(TypeError, lambda: n > bar)
  1752. raises(TypeError, lambda: bar < n)
  1753. raises(TypeError, lambda: n <= bar)
  1754. raises(TypeError, lambda: bar >= n)
  1755. raises(TypeError, lambda: n >= bar)
  1756. raises(TypeError, lambda: bar <= n)
  1757. def test_NumberSymbol_comparison():
  1758. from sympy.core.tests.test_relational import rel_check
  1759. rpi = Rational('905502432259640373/288230376151711744')
  1760. fpi = Float(float(pi))
  1761. assert rel_check(rpi, fpi)
  1762. def test_Integer_precision():
  1763. # Make sure Integer inputs for keyword args work
  1764. assert Float('1.0', dps=Integer(15))._prec == 53
  1765. assert Float('1.0', precision=Integer(15))._prec == 15
  1766. assert type(Float('1.0', precision=Integer(15))._prec) == int
  1767. assert sympify(srepr(Float('1.0', precision=15))) == Float('1.0', precision=15)
  1768. def test_numpy_to_float():
  1769. from sympy.testing.pytest import skip
  1770. from sympy.external import import_module
  1771. np = import_module('numpy')
  1772. if not np:
  1773. skip('numpy not installed. Abort numpy tests.')
  1774. def check_prec_and_relerr(npval, ratval):
  1775. prec = np.finfo(npval).nmant + 1
  1776. x = Float(npval)
  1777. assert x._prec == prec
  1778. y = Float(ratval, precision=prec)
  1779. assert abs((x - y)/y) < 2**(-(prec + 1))
  1780. check_prec_and_relerr(np.float16(2.0/3), Rational(2, 3))
  1781. check_prec_and_relerr(np.float32(2.0/3), Rational(2, 3))
  1782. check_prec_and_relerr(np.float64(2.0/3), Rational(2, 3))
  1783. # extended precision, on some arch/compilers:
  1784. x = np.longdouble(2)/3
  1785. check_prec_and_relerr(x, Rational(2, 3))
  1786. y = Float(x, precision=10)
  1787. assert same_and_same_prec(y, Float(Rational(2, 3), precision=10))
  1788. raises(TypeError, lambda: Float(np.complex64(1+2j)))
  1789. raises(TypeError, lambda: Float(np.complex128(1+2j)))
  1790. def test_Integer_ceiling_floor():
  1791. a = Integer(4)
  1792. assert a.floor() == a
  1793. assert a.ceiling() == a
  1794. def test_ComplexInfinity():
  1795. assert zoo.floor() is zoo
  1796. assert zoo.ceiling() is zoo
  1797. assert zoo**zoo is S.NaN
  1798. def test_Infinity_floor_ceiling_power():
  1799. assert oo.floor() is oo
  1800. assert oo.ceiling() is oo
  1801. assert oo**S.NaN is S.NaN
  1802. assert oo**zoo is S.NaN
  1803. def test_One_power():
  1804. assert S.One**12 is S.One
  1805. assert S.NegativeOne**S.NaN is S.NaN
  1806. def test_NegativeInfinity():
  1807. assert (-oo).floor() is -oo
  1808. assert (-oo).ceiling() is -oo
  1809. assert (-oo)**11 is -oo
  1810. assert (-oo)**12 is oo
  1811. def test_issue_6133():
  1812. raises(TypeError, lambda: (-oo < None))
  1813. raises(TypeError, lambda: (S(-2) < None))
  1814. raises(TypeError, lambda: (oo < None))
  1815. raises(TypeError, lambda: (oo > None))
  1816. raises(TypeError, lambda: (S(2) < None))
  1817. def test_abc():
  1818. x = numbers.Float(5)
  1819. assert(isinstance(x, nums.Number))
  1820. assert(isinstance(x, numbers.Number))
  1821. assert(isinstance(x, nums.Real))
  1822. y = numbers.Rational(1, 3)
  1823. assert(isinstance(y, nums.Number))
  1824. assert(y.numerator == 1)
  1825. assert(y.denominator == 3)
  1826. assert(isinstance(y, nums.Rational))
  1827. z = numbers.Integer(3)
  1828. assert(isinstance(z, nums.Number))
  1829. assert(isinstance(z, numbers.Number))
  1830. assert(isinstance(z, nums.Rational))
  1831. assert(isinstance(z, numbers.Rational))
  1832. assert(isinstance(z, nums.Integral))
  1833. def test_floordiv():
  1834. assert S(2)//S.Half == 4
  1835. def test_negation():
  1836. assert -S.Zero is S.Zero
  1837. assert -Float(0) is not S.Zero and -Float(0) == 0.0
  1838. def test_exponentiation_of_0():
  1839. x = Symbol('x')
  1840. assert 0**-x == zoo**x
  1841. assert unchanged(Pow, 0, x)
  1842. x = Symbol('x', zero=True)
  1843. assert 0**-x == S.One
  1844. assert 0**x == S.One
  1845. def test_equal_valued():
  1846. x = Symbol('x')
  1847. equal_values = [
  1848. [1, 1.0, S(1), S(1.0), S(1).n(5)],
  1849. [2, 2.0, S(2), S(2.0), S(2).n(5)],
  1850. [-1, -1.0, -S(1), -S(1.0), -S(1).n(5)],
  1851. [0.5, S(0.5), S(1)/2],
  1852. [-0.5, -S(0.5), -S(1)/2],
  1853. [0, 0.0, S(0), S(0.0), S(0).n()],
  1854. [pi], [pi.n()], # <-- not equal
  1855. [S(1)/10], [0.1, S(0.1)], # <-- not equal
  1856. [S(0.1).n(5)],
  1857. [oo],
  1858. [cos(x/2)], [cos(0.5*x)], # <-- no recursion
  1859. ]
  1860. for m, values_m in enumerate(equal_values):
  1861. for value_i in values_m:
  1862. # All values in same list equal
  1863. for value_j in values_m:
  1864. assert equal_valued(value_i, value_j) is True
  1865. # Not equal to anything in any other list:
  1866. for n, values_n in enumerate(equal_values):
  1867. if n == m:
  1868. continue
  1869. for value_j in values_n:
  1870. assert equal_valued(value_i, value_j) is False