test_arit.py 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. from sympy.core.add import Add
  2. from sympy.core.basic import Basic
  3. from sympy.core.mod import Mod
  4. from sympy.core.mul import Mul
  5. from sympy.core.numbers import (Float, I, Integer, Rational, comp, nan,
  6. oo, pi, zoo)
  7. from sympy.core.power import Pow
  8. from sympy.core.singleton import S
  9. from sympy.core.symbol import (Dummy, Symbol, symbols)
  10. from sympy.core.sympify import sympify
  11. from sympy.functions.combinatorial.factorials import factorial
  12. from sympy.functions.elementary.complexes import (im, re, sign)
  13. from sympy.functions.elementary.exponential import (exp, log)
  14. from sympy.functions.elementary.integers import floor
  15. from sympy.functions.elementary.miscellaneous import (Max, sqrt)
  16. from sympy.functions.elementary.trigonometric import (atan, cos, sin)
  17. from sympy.polys.polytools import Poly
  18. from sympy.sets.sets import FiniteSet
  19. from sympy.core.parameters import distribute, evaluate
  20. from sympy.core.expr import unchanged
  21. from sympy.utilities.iterables import permutations
  22. from sympy.testing.pytest import XFAIL, raises, warns
  23. from sympy.utilities.exceptions import SymPyDeprecationWarning
  24. from sympy.core.random import verify_numerically
  25. from sympy.functions.elementary.trigonometric import asin
  26. from itertools import product
  27. a, c, x, y, z = symbols('a,c,x,y,z')
  28. b = Symbol("b", positive=True)
  29. def same_and_same_prec(a, b):
  30. # stricter matching for Floats
  31. return a == b and a._prec == b._prec
  32. def test_bug1():
  33. assert re(x) != x
  34. x.series(x, 0, 1)
  35. assert re(x) != x
  36. def test_Symbol():
  37. e = a*b
  38. assert e == a*b
  39. assert a*b*b == a*b**2
  40. assert a*b*b + c == c + a*b**2
  41. assert a*b*b - c == -c + a*b**2
  42. x = Symbol('x', complex=True, real=False)
  43. assert x.is_imaginary is None # could be I or 1 + I
  44. x = Symbol('x', complex=True, imaginary=False)
  45. assert x.is_real is None # could be 1 or 1 + I
  46. x = Symbol('x', real=True)
  47. assert x.is_complex
  48. x = Symbol('x', imaginary=True)
  49. assert x.is_complex
  50. x = Symbol('x', real=False, imaginary=False)
  51. assert x.is_complex is None # might be a non-number
  52. def test_arit0():
  53. p = Rational(5)
  54. e = a*b
  55. assert e == a*b
  56. e = a*b + b*a
  57. assert e == 2*a*b
  58. e = a*b + b*a + a*b + p*b*a
  59. assert e == 8*a*b
  60. e = a*b + b*a + a*b + p*b*a + a
  61. assert e == a + 8*a*b
  62. e = a + a
  63. assert e == 2*a
  64. e = a + b + a
  65. assert e == b + 2*a
  66. e = a + b*b + a + b*b
  67. assert e == 2*a + 2*b**2
  68. e = a + Rational(2) + b*b + a + b*b + p
  69. assert e == 7 + 2*a + 2*b**2
  70. e = (a + b*b + a + b*b)*p
  71. assert e == 5*(2*a + 2*b**2)
  72. e = (a*b*c + c*b*a + b*a*c)*p
  73. assert e == 15*a*b*c
  74. e = (a*b*c + c*b*a + b*a*c)*p - Rational(15)*a*b*c
  75. assert e == Rational(0)
  76. e = Rational(50)*(a - a)
  77. assert e == Rational(0)
  78. e = b*a - b - a*b + b
  79. assert e == Rational(0)
  80. e = a*b + c**p
  81. assert e == a*b + c**5
  82. e = a/b
  83. assert e == a*b**(-1)
  84. e = a*2*2
  85. assert e == 4*a
  86. e = 2 + a*2/2
  87. assert e == 2 + a
  88. e = 2 - a - 2
  89. assert e == -a
  90. e = 2*a*2
  91. assert e == 4*a
  92. e = 2/a/2
  93. assert e == a**(-1)
  94. e = 2**a**2
  95. assert e == 2**(a**2)
  96. e = -(1 + a)
  97. assert e == -1 - a
  98. e = S.Half*(1 + a)
  99. assert e == S.Half + a/2
  100. def test_div():
  101. e = a/b
  102. assert e == a*b**(-1)
  103. e = a/b + c/2
  104. assert e == a*b**(-1) + Rational(1)/2*c
  105. e = (1 - b)/(b - 1)
  106. assert e == (1 + -b)*((-1) + b)**(-1)
  107. def test_pow_arit():
  108. n1 = Rational(1)
  109. n2 = Rational(2)
  110. n5 = Rational(5)
  111. e = a*a
  112. assert e == a**2
  113. e = a*a*a
  114. assert e == a**3
  115. e = a*a*a*a**Rational(6)
  116. assert e == a**9
  117. e = a*a*a*a**Rational(6) - a**Rational(9)
  118. assert e == Rational(0)
  119. e = a**(b - b)
  120. assert e == Rational(1)
  121. e = (a + Rational(1) - a)**b
  122. assert e == Rational(1)
  123. e = (a + b + c)**n2
  124. assert e == (a + b + c)**2
  125. assert e.expand() == 2*b*c + 2*a*c + 2*a*b + a**2 + c**2 + b**2
  126. e = (a + b)**n2
  127. assert e == (a + b)**2
  128. assert e.expand() == 2*a*b + a**2 + b**2
  129. e = (a + b)**(n1/n2)
  130. assert e == sqrt(a + b)
  131. assert e.expand() == sqrt(a + b)
  132. n = n5**(n1/n2)
  133. assert n == sqrt(5)
  134. e = n*a*b - n*b*a
  135. assert e == Rational(0)
  136. e = n*a*b + n*b*a
  137. assert e == 2*a*b*sqrt(5)
  138. assert e.diff(a) == 2*b*sqrt(5)
  139. assert e.diff(a) == 2*b*sqrt(5)
  140. e = a/b**2
  141. assert e == a*b**(-2)
  142. assert sqrt(2*(1 + sqrt(2))) == (2*(1 + 2**S.Half))**S.Half
  143. x = Symbol('x')
  144. y = Symbol('y')
  145. assert ((x*y)**3).expand() == y**3 * x**3
  146. assert ((x*y)**-3).expand() == y**-3 * x**-3
  147. assert (x**5*(3*x)**(3)).expand() == 27 * x**8
  148. assert (x**5*(-3*x)**(3)).expand() == -27 * x**8
  149. assert (x**5*(3*x)**(-3)).expand() == x**2 * Rational(1, 27)
  150. assert (x**5*(-3*x)**(-3)).expand() == x**2 * Rational(-1, 27)
  151. # expand_power_exp
  152. _x = Symbol('x', zero=False)
  153. _y = Symbol('y', zero=False)
  154. assert (_x**(y**(x + exp(x + y)) + z)).expand(deep=False) == \
  155. _x**z*_x**(y**(x + exp(x + y)))
  156. assert (_x**(_y**(x + exp(x + y)) + z)).expand() == \
  157. _x**z*_x**(_y**x*_y**(exp(x)*exp(y)))
  158. n = Symbol('n', even=False)
  159. k = Symbol('k', even=True)
  160. o = Symbol('o', odd=True)
  161. assert unchanged(Pow, -1, x)
  162. assert unchanged(Pow, -1, n)
  163. assert (-2)**k == 2**k
  164. assert (-1)**k == 1
  165. assert (-1)**o == -1
  166. def test_pow2():
  167. # x**(2*y) is always (x**y)**2 but is only (x**2)**y if
  168. # x.is_positive or y.is_integer
  169. # let x = 1 to see why the following are not true.
  170. assert (-x)**Rational(2, 3) != x**Rational(2, 3)
  171. assert (-x)**Rational(5, 7) != -x**Rational(5, 7)
  172. assert ((-x)**2)**Rational(1, 3) != ((-x)**Rational(1, 3))**2
  173. assert sqrt(x**2) != x
  174. def test_pow3():
  175. assert sqrt(2)**3 == 2 * sqrt(2)
  176. assert sqrt(2)**3 == sqrt(8)
  177. def test_mod_pow():
  178. for s, t, u, v in [(4, 13, 497, 445), (4, -3, 497, 365),
  179. (3.2, 2.1, 1.9, 0.1031015682350942), (S(3)/2, 5, S(5)/6, S(3)/32)]:
  180. assert pow(S(s), t, u) == v
  181. assert pow(S(s), S(t), u) == v
  182. assert pow(S(s), t, S(u)) == v
  183. assert pow(S(s), S(t), S(u)) == v
  184. assert pow(S(2), S(10000000000), S(3)) == 1
  185. assert pow(x, y, z) == x**y%z
  186. raises(TypeError, lambda: pow(S(4), "13", 497))
  187. raises(TypeError, lambda: pow(S(4), 13, "497"))
  188. def test_pow_E():
  189. assert 2**(y/log(2)) == S.Exp1**y
  190. assert 2**(y/log(2)/3) == S.Exp1**(y/3)
  191. assert 3**(1/log(-3)) != S.Exp1
  192. assert (3 + 2*I)**(1/(log(-3 - 2*I) + I*pi)) == S.Exp1
  193. assert (4 + 2*I)**(1/(log(-4 - 2*I) + I*pi)) == S.Exp1
  194. assert (3 + 2*I)**(1/(log(-3 - 2*I, 3)/2 + I*pi/log(3)/2)) == 9
  195. assert (3 + 2*I)**(1/(log(3 + 2*I, 3)/2)) == 9
  196. # every time tests are run they will affirm with a different random
  197. # value that this identity holds
  198. while 1:
  199. b = x._random()
  200. r, i = b.as_real_imag()
  201. if i:
  202. break
  203. assert verify_numerically(b**(1/(log(-b) + sign(i)*I*pi).n()), S.Exp1)
  204. def test_pow_issue_3516():
  205. assert 4**Rational(1, 4) == sqrt(2)
  206. def test_pow_im():
  207. for m in (-2, -1, 2):
  208. for d in (3, 4, 5):
  209. b = m*I
  210. for i in range(1, 4*d + 1):
  211. e = Rational(i, d)
  212. assert (b**e - b.n()**e.n()).n(2, chop=1e-10) == 0
  213. e = Rational(7, 3)
  214. assert (2*x*I)**e == 4*2**Rational(1, 3)*(I*x)**e # same as Wolfram Alpha
  215. im = symbols('im', imaginary=True)
  216. assert (2*im*I)**e == 4*2**Rational(1, 3)*(I*im)**e
  217. args = [I, I, I, I, 2]
  218. e = Rational(1, 3)
  219. ans = 2**e
  220. assert Mul(*args, evaluate=False)**e == ans
  221. assert Mul(*args)**e == ans
  222. args = [I, I, I, 2]
  223. e = Rational(1, 3)
  224. ans = 2**e*(-I)**e
  225. assert Mul(*args, evaluate=False)**e == ans
  226. assert Mul(*args)**e == ans
  227. args.append(-3)
  228. ans = (6*I)**e
  229. assert Mul(*args, evaluate=False)**e == ans
  230. assert Mul(*args)**e == ans
  231. args.append(-1)
  232. ans = (-6*I)**e
  233. assert Mul(*args, evaluate=False)**e == ans
  234. assert Mul(*args)**e == ans
  235. args = [I, I, 2]
  236. e = Rational(1, 3)
  237. ans = (-2)**e
  238. assert Mul(*args, evaluate=False)**e == ans
  239. assert Mul(*args)**e == ans
  240. args.append(-3)
  241. ans = (6)**e
  242. assert Mul(*args, evaluate=False)**e == ans
  243. assert Mul(*args)**e == ans
  244. args.append(-1)
  245. ans = (-6)**e
  246. assert Mul(*args, evaluate=False)**e == ans
  247. assert Mul(*args)**e == ans
  248. assert Mul(Pow(-1, Rational(3, 2), evaluate=False), I, I) == I
  249. assert Mul(I*Pow(I, S.Half, evaluate=False)) == sqrt(I)*I
  250. def test_real_mul():
  251. assert Float(0) * pi * x == 0
  252. assert set((Float(1) * pi * x).args) == {Float(1), pi, x}
  253. def test_ncmul():
  254. A = Symbol("A", commutative=False)
  255. B = Symbol("B", commutative=False)
  256. C = Symbol("C", commutative=False)
  257. assert A*B != B*A
  258. assert A*B*C != C*B*A
  259. assert A*b*B*3*C == 3*b*A*B*C
  260. assert A*b*B*3*C != 3*b*B*A*C
  261. assert A*b*B*3*C == 3*A*B*C*b
  262. assert A + B == B + A
  263. assert (A + B)*C != C*(A + B)
  264. assert C*(A + B)*C != C*C*(A + B)
  265. assert A*A == A**2
  266. assert (A + B)*(A + B) == (A + B)**2
  267. assert A**-1 * A == 1
  268. assert A/A == 1
  269. assert A/(A**2) == 1/A
  270. assert A/(1 + A) == A/(1 + A)
  271. assert set((A + B + 2*(A + B)).args) == \
  272. {A, B, 2*(A + B)}
  273. def test_mul_add_identity():
  274. m = Mul(1, 2)
  275. assert isinstance(m, Rational) and m.p == 2 and m.q == 1
  276. m = Mul(1, 2, evaluate=False)
  277. assert isinstance(m, Mul) and m.args == (1, 2)
  278. m = Mul(0, 1)
  279. assert m is S.Zero
  280. m = Mul(0, 1, evaluate=False)
  281. assert isinstance(m, Mul) and m.args == (0, 1)
  282. m = Add(0, 1)
  283. assert m is S.One
  284. m = Add(0, 1, evaluate=False)
  285. assert isinstance(m, Add) and m.args == (0, 1)
  286. def test_ncpow():
  287. x = Symbol('x', commutative=False)
  288. y = Symbol('y', commutative=False)
  289. z = Symbol('z', commutative=False)
  290. a = Symbol('a')
  291. b = Symbol('b')
  292. c = Symbol('c')
  293. assert (x**2)*(y**2) != (y**2)*(x**2)
  294. assert (x**-2)*y != y*(x**2)
  295. assert 2**x*2**y != 2**(x + y)
  296. assert 2**x*2**y*2**z != 2**(x + y + z)
  297. assert 2**x*2**(2*x) == 2**(3*x)
  298. assert 2**x*2**(2*x)*2**x == 2**(4*x)
  299. assert exp(x)*exp(y) != exp(y)*exp(x)
  300. assert exp(x)*exp(y)*exp(z) != exp(y)*exp(x)*exp(z)
  301. assert exp(x)*exp(y)*exp(z) != exp(x + y + z)
  302. assert x**a*x**b != x**(a + b)
  303. assert x**a*x**b*x**c != x**(a + b + c)
  304. assert x**3*x**4 == x**7
  305. assert x**3*x**4*x**2 == x**9
  306. assert x**a*x**(4*a) == x**(5*a)
  307. assert x**a*x**(4*a)*x**a == x**(6*a)
  308. def test_powerbug():
  309. x = Symbol("x")
  310. assert x**1 != (-x)**1
  311. assert x**2 == (-x)**2
  312. assert x**3 != (-x)**3
  313. assert x**4 == (-x)**4
  314. assert x**5 != (-x)**5
  315. assert x**6 == (-x)**6
  316. assert x**128 == (-x)**128
  317. assert x**129 != (-x)**129
  318. assert (2*x)**2 == (-2*x)**2
  319. def test_Mul_doesnt_expand_exp():
  320. x = Symbol('x')
  321. y = Symbol('y')
  322. assert unchanged(Mul, exp(x), exp(y))
  323. assert unchanged(Mul, 2**x, 2**y)
  324. assert x**2*x**3 == x**5
  325. assert 2**x*3**x == 6**x
  326. assert x**(y)*x**(2*y) == x**(3*y)
  327. assert sqrt(2)*sqrt(2) == 2
  328. assert 2**x*2**(2*x) == 2**(3*x)
  329. assert sqrt(2)*2**Rational(1, 4)*5**Rational(3, 4) == 10**Rational(3, 4)
  330. assert (x**(-log(5)/log(3))*x)/(x*x**( - log(5)/log(3))) == sympify(1)
  331. def test_Mul_is_integer():
  332. k = Symbol('k', integer=True)
  333. n = Symbol('n', integer=True)
  334. nr = Symbol('nr', rational=False)
  335. ir = Symbol('ir', irrational=True)
  336. nz = Symbol('nz', integer=True, zero=False)
  337. e = Symbol('e', even=True)
  338. o = Symbol('o', odd=True)
  339. i2 = Symbol('2', prime=True, even=True)
  340. assert (k/3).is_integer is None
  341. assert (nz/3).is_integer is None
  342. assert (nr/3).is_integer is False
  343. assert (ir/3).is_integer is False
  344. assert (x*k*n).is_integer is None
  345. assert (e/2).is_integer is True
  346. assert (e**2/2).is_integer is True
  347. assert (2/k).is_integer is None
  348. assert (2/k**2).is_integer is None
  349. assert ((-1)**k*n).is_integer is True
  350. assert (3*k*e/2).is_integer is True
  351. assert (2*k*e/3).is_integer is None
  352. assert (e/o).is_integer is None
  353. assert (o/e).is_integer is False
  354. assert (o/i2).is_integer is False
  355. assert Mul(k, 1/k, evaluate=False).is_integer is None
  356. assert Mul(2., S.Half, evaluate=False).is_integer is None
  357. assert (2*sqrt(k)).is_integer is None
  358. assert (2*k**n).is_integer is None
  359. s = 2**2**2**Pow(2, 1000, evaluate=False)
  360. m = Mul(s, s, evaluate=False)
  361. assert m.is_integer
  362. # broken in 1.6 and before, see #20161
  363. xq = Symbol('xq', rational=True)
  364. yq = Symbol('yq', rational=True)
  365. assert (xq*yq).is_integer is None
  366. e_20161 = Mul(-1,Mul(1,Pow(2,-1,evaluate=False),evaluate=False),evaluate=False)
  367. assert e_20161.is_integer is not True # expand(e_20161) -> -1/2, but no need to see that in the assumption without evaluation
  368. def test_Add_Mul_is_integer():
  369. x = Symbol('x')
  370. k = Symbol('k', integer=True)
  371. n = Symbol('n', integer=True)
  372. nk = Symbol('nk', integer=False)
  373. nr = Symbol('nr', rational=False)
  374. nz = Symbol('nz', integer=True, zero=False)
  375. assert (-nk).is_integer is None
  376. assert (-nr).is_integer is False
  377. assert (2*k).is_integer is True
  378. assert (-k).is_integer is True
  379. assert (k + nk).is_integer is False
  380. assert (k + n).is_integer is True
  381. assert (k + x).is_integer is None
  382. assert (k + n*x).is_integer is None
  383. assert (k + n/3).is_integer is None
  384. assert (k + nz/3).is_integer is None
  385. assert (k + nr/3).is_integer is False
  386. assert ((1 + sqrt(3))*(-sqrt(3) + 1)).is_integer is not False
  387. assert (1 + (1 + sqrt(3))*(-sqrt(3) + 1)).is_integer is not False
  388. def test_Add_Mul_is_finite():
  389. x = Symbol('x', extended_real=True, finite=False)
  390. assert sin(x).is_finite is True
  391. assert (x*sin(x)).is_finite is None
  392. assert (x*atan(x)).is_finite is False
  393. assert (1024*sin(x)).is_finite is True
  394. assert (sin(x)*exp(x)).is_finite is None
  395. assert (sin(x)*cos(x)).is_finite is True
  396. assert (x*sin(x)*exp(x)).is_finite is None
  397. assert (sin(x) - 67).is_finite is True
  398. assert (sin(x) + exp(x)).is_finite is not True
  399. assert (1 + x).is_finite is False
  400. assert (1 + x**2 + (1 + x)*(1 - x)).is_finite is None
  401. assert (sqrt(2)*(1 + x)).is_finite is False
  402. assert (sqrt(2)*(1 + x)*(1 - x)).is_finite is False
  403. def test_Mul_is_even_odd():
  404. x = Symbol('x', integer=True)
  405. y = Symbol('y', integer=True)
  406. k = Symbol('k', odd=True)
  407. n = Symbol('n', odd=True)
  408. m = Symbol('m', even=True)
  409. assert (2*x).is_even is True
  410. assert (2*x).is_odd is False
  411. assert (3*x).is_even is None
  412. assert (3*x).is_odd is None
  413. assert (k/3).is_integer is None
  414. assert (k/3).is_even is None
  415. assert (k/3).is_odd is None
  416. assert (2*n).is_even is True
  417. assert (2*n).is_odd is False
  418. assert (2*m).is_even is True
  419. assert (2*m).is_odd is False
  420. assert (-n).is_even is False
  421. assert (-n).is_odd is True
  422. assert (k*n).is_even is False
  423. assert (k*n).is_odd is True
  424. assert (k*m).is_even is True
  425. assert (k*m).is_odd is False
  426. assert (k*n*m).is_even is True
  427. assert (k*n*m).is_odd is False
  428. assert (k*m*x).is_even is True
  429. assert (k*m*x).is_odd is False
  430. # issue 6791:
  431. assert (x/2).is_integer is None
  432. assert (k/2).is_integer is False
  433. assert (m/2).is_integer is True
  434. assert (x*y).is_even is None
  435. assert (x*x).is_even is None
  436. assert (x*(x + k)).is_even is True
  437. assert (x*(x + m)).is_even is None
  438. assert (x*y).is_odd is None
  439. assert (x*x).is_odd is None
  440. assert (x*(x + k)).is_odd is False
  441. assert (x*(x + m)).is_odd is None
  442. # issue 8648
  443. assert (m**2/2).is_even
  444. assert (m**2/3).is_even is False
  445. assert (2/m**2).is_odd is False
  446. assert (2/m).is_odd is None
  447. @XFAIL
  448. def test_evenness_in_ternary_integer_product_with_odd():
  449. # Tests that oddness inference is independent of term ordering.
  450. # Term ordering at the point of testing depends on SymPy's symbol order, so
  451. # we try to force a different order by modifying symbol names.
  452. x = Symbol('x', integer=True)
  453. y = Symbol('y', integer=True)
  454. k = Symbol('k', odd=True)
  455. assert (x*y*(y + k)).is_even is True
  456. assert (y*x*(x + k)).is_even is True
  457. def test_evenness_in_ternary_integer_product_with_even():
  458. x = Symbol('x', integer=True)
  459. y = Symbol('y', integer=True)
  460. m = Symbol('m', even=True)
  461. assert (x*y*(y + m)).is_even is None
  462. @XFAIL
  463. def test_oddness_in_ternary_integer_product_with_odd():
  464. # Tests that oddness inference is independent of term ordering.
  465. # Term ordering at the point of testing depends on SymPy's symbol order, so
  466. # we try to force a different order by modifying symbol names.
  467. x = Symbol('x', integer=True)
  468. y = Symbol('y', integer=True)
  469. k = Symbol('k', odd=True)
  470. assert (x*y*(y + k)).is_odd is False
  471. assert (y*x*(x + k)).is_odd is False
  472. def test_oddness_in_ternary_integer_product_with_even():
  473. x = Symbol('x', integer=True)
  474. y = Symbol('y', integer=True)
  475. m = Symbol('m', even=True)
  476. assert (x*y*(y + m)).is_odd is None
  477. def test_Mul_is_rational():
  478. x = Symbol('x')
  479. n = Symbol('n', integer=True)
  480. m = Symbol('m', integer=True, nonzero=True)
  481. assert (n/m).is_rational is True
  482. assert (x/pi).is_rational is None
  483. assert (x/n).is_rational is None
  484. assert (m/pi).is_rational is False
  485. r = Symbol('r', rational=True)
  486. assert (pi*r).is_rational is None
  487. # issue 8008
  488. z = Symbol('z', zero=True)
  489. i = Symbol('i', imaginary=True)
  490. assert (z*i).is_rational is True
  491. bi = Symbol('i', imaginary=True, finite=True)
  492. assert (z*bi).is_zero is True
  493. def test_Add_is_rational():
  494. x = Symbol('x')
  495. n = Symbol('n', rational=True)
  496. m = Symbol('m', rational=True)
  497. assert (n + m).is_rational is True
  498. assert (x + pi).is_rational is None
  499. assert (x + n).is_rational is None
  500. assert (n + pi).is_rational is False
  501. def test_Add_is_even_odd():
  502. x = Symbol('x', integer=True)
  503. k = Symbol('k', odd=True)
  504. n = Symbol('n', odd=True)
  505. m = Symbol('m', even=True)
  506. assert (k + 7).is_even is True
  507. assert (k + 7).is_odd is False
  508. assert (-k + 7).is_even is True
  509. assert (-k + 7).is_odd is False
  510. assert (k - 12).is_even is False
  511. assert (k - 12).is_odd is True
  512. assert (-k - 12).is_even is False
  513. assert (-k - 12).is_odd is True
  514. assert (k + n).is_even is True
  515. assert (k + n).is_odd is False
  516. assert (k + m).is_even is False
  517. assert (k + m).is_odd is True
  518. assert (k + n + m).is_even is True
  519. assert (k + n + m).is_odd is False
  520. assert (k + n + x + m).is_even is None
  521. assert (k + n + x + m).is_odd is None
  522. def test_Mul_is_negative_positive():
  523. x = Symbol('x', real=True)
  524. y = Symbol('y', extended_real=False, complex=True)
  525. z = Symbol('z', zero=True)
  526. e = 2*z
  527. assert e.is_Mul and e.is_positive is False and e.is_negative is False
  528. neg = Symbol('neg', negative=True)
  529. pos = Symbol('pos', positive=True)
  530. nneg = Symbol('nneg', nonnegative=True)
  531. npos = Symbol('npos', nonpositive=True)
  532. assert neg.is_negative is True
  533. assert (-neg).is_negative is False
  534. assert (2*neg).is_negative is True
  535. assert (2*pos)._eval_is_extended_negative() is False
  536. assert (2*pos).is_negative is False
  537. assert pos.is_negative is False
  538. assert (-pos).is_negative is True
  539. assert (2*pos).is_negative is False
  540. assert (pos*neg).is_negative is True
  541. assert (2*pos*neg).is_negative is True
  542. assert (-pos*neg).is_negative is False
  543. assert (pos*neg*y).is_negative is False # y.is_real=F; !real -> !neg
  544. assert nneg.is_negative is False
  545. assert (-nneg).is_negative is None
  546. assert (2*nneg).is_negative is False
  547. assert npos.is_negative is None
  548. assert (-npos).is_negative is False
  549. assert (2*npos).is_negative is None
  550. assert (nneg*npos).is_negative is None
  551. assert (neg*nneg).is_negative is None
  552. assert (neg*npos).is_negative is False
  553. assert (pos*nneg).is_negative is False
  554. assert (pos*npos).is_negative is None
  555. assert (npos*neg*nneg).is_negative is False
  556. assert (npos*pos*nneg).is_negative is None
  557. assert (-npos*neg*nneg).is_negative is None
  558. assert (-npos*pos*nneg).is_negative is False
  559. assert (17*npos*neg*nneg).is_negative is False
  560. assert (17*npos*pos*nneg).is_negative is None
  561. assert (neg*npos*pos*nneg).is_negative is False
  562. assert (x*neg).is_negative is None
  563. assert (nneg*npos*pos*x*neg).is_negative is None
  564. assert neg.is_positive is False
  565. assert (-neg).is_positive is True
  566. assert (2*neg).is_positive is False
  567. assert pos.is_positive is True
  568. assert (-pos).is_positive is False
  569. assert (2*pos).is_positive is True
  570. assert (pos*neg).is_positive is False
  571. assert (2*pos*neg).is_positive is False
  572. assert (-pos*neg).is_positive is True
  573. assert (-pos*neg*y).is_positive is False # y.is_real=F; !real -> !neg
  574. assert nneg.is_positive is None
  575. assert (-nneg).is_positive is False
  576. assert (2*nneg).is_positive is None
  577. assert npos.is_positive is False
  578. assert (-npos).is_positive is None
  579. assert (2*npos).is_positive is False
  580. assert (nneg*npos).is_positive is False
  581. assert (neg*nneg).is_positive is False
  582. assert (neg*npos).is_positive is None
  583. assert (pos*nneg).is_positive is None
  584. assert (pos*npos).is_positive is False
  585. assert (npos*neg*nneg).is_positive is None
  586. assert (npos*pos*nneg).is_positive is False
  587. assert (-npos*neg*nneg).is_positive is False
  588. assert (-npos*pos*nneg).is_positive is None
  589. assert (17*npos*neg*nneg).is_positive is None
  590. assert (17*npos*pos*nneg).is_positive is False
  591. assert (neg*npos*pos*nneg).is_positive is None
  592. assert (x*neg).is_positive is None
  593. assert (nneg*npos*pos*x*neg).is_positive is None
  594. def test_Mul_is_negative_positive_2():
  595. a = Symbol('a', nonnegative=True)
  596. b = Symbol('b', nonnegative=True)
  597. c = Symbol('c', nonpositive=True)
  598. d = Symbol('d', nonpositive=True)
  599. assert (a*b).is_nonnegative is True
  600. assert (a*b).is_negative is False
  601. assert (a*b).is_zero is None
  602. assert (a*b).is_positive is None
  603. assert (c*d).is_nonnegative is True
  604. assert (c*d).is_negative is False
  605. assert (c*d).is_zero is None
  606. assert (c*d).is_positive is None
  607. assert (a*c).is_nonpositive is True
  608. assert (a*c).is_positive is False
  609. assert (a*c).is_zero is None
  610. assert (a*c).is_negative is None
  611. def test_Mul_is_nonpositive_nonnegative():
  612. x = Symbol('x', real=True)
  613. k = Symbol('k', negative=True)
  614. n = Symbol('n', positive=True)
  615. u = Symbol('u', nonnegative=True)
  616. v = Symbol('v', nonpositive=True)
  617. assert k.is_nonpositive is True
  618. assert (-k).is_nonpositive is False
  619. assert (2*k).is_nonpositive is True
  620. assert n.is_nonpositive is False
  621. assert (-n).is_nonpositive is True
  622. assert (2*n).is_nonpositive is False
  623. assert (n*k).is_nonpositive is True
  624. assert (2*n*k).is_nonpositive is True
  625. assert (-n*k).is_nonpositive is False
  626. assert u.is_nonpositive is None
  627. assert (-u).is_nonpositive is True
  628. assert (2*u).is_nonpositive is None
  629. assert v.is_nonpositive is True
  630. assert (-v).is_nonpositive is None
  631. assert (2*v).is_nonpositive is True
  632. assert (u*v).is_nonpositive is True
  633. assert (k*u).is_nonpositive is True
  634. assert (k*v).is_nonpositive is None
  635. assert (n*u).is_nonpositive is None
  636. assert (n*v).is_nonpositive is True
  637. assert (v*k*u).is_nonpositive is None
  638. assert (v*n*u).is_nonpositive is True
  639. assert (-v*k*u).is_nonpositive is True
  640. assert (-v*n*u).is_nonpositive is None
  641. assert (17*v*k*u).is_nonpositive is None
  642. assert (17*v*n*u).is_nonpositive is True
  643. assert (k*v*n*u).is_nonpositive is None
  644. assert (x*k).is_nonpositive is None
  645. assert (u*v*n*x*k).is_nonpositive is None
  646. assert k.is_nonnegative is False
  647. assert (-k).is_nonnegative is True
  648. assert (2*k).is_nonnegative is False
  649. assert n.is_nonnegative is True
  650. assert (-n).is_nonnegative is False
  651. assert (2*n).is_nonnegative is True
  652. assert (n*k).is_nonnegative is False
  653. assert (2*n*k).is_nonnegative is False
  654. assert (-n*k).is_nonnegative is True
  655. assert u.is_nonnegative is True
  656. assert (-u).is_nonnegative is None
  657. assert (2*u).is_nonnegative is True
  658. assert v.is_nonnegative is None
  659. assert (-v).is_nonnegative is True
  660. assert (2*v).is_nonnegative is None
  661. assert (u*v).is_nonnegative is None
  662. assert (k*u).is_nonnegative is None
  663. assert (k*v).is_nonnegative is True
  664. assert (n*u).is_nonnegative is True
  665. assert (n*v).is_nonnegative is None
  666. assert (v*k*u).is_nonnegative is True
  667. assert (v*n*u).is_nonnegative is None
  668. assert (-v*k*u).is_nonnegative is None
  669. assert (-v*n*u).is_nonnegative is True
  670. assert (17*v*k*u).is_nonnegative is True
  671. assert (17*v*n*u).is_nonnegative is None
  672. assert (k*v*n*u).is_nonnegative is True
  673. assert (x*k).is_nonnegative is None
  674. assert (u*v*n*x*k).is_nonnegative is None
  675. def test_Add_is_negative_positive():
  676. x = Symbol('x', real=True)
  677. k = Symbol('k', negative=True)
  678. n = Symbol('n', positive=True)
  679. u = Symbol('u', nonnegative=True)
  680. v = Symbol('v', nonpositive=True)
  681. assert (k - 2).is_negative is True
  682. assert (k + 17).is_negative is None
  683. assert (-k - 5).is_negative is None
  684. assert (-k + 123).is_negative is False
  685. assert (k - n).is_negative is True
  686. assert (k + n).is_negative is None
  687. assert (-k - n).is_negative is None
  688. assert (-k + n).is_negative is False
  689. assert (k - n - 2).is_negative is True
  690. assert (k + n + 17).is_negative is None
  691. assert (-k - n - 5).is_negative is None
  692. assert (-k + n + 123).is_negative is False
  693. assert (-2*k + 123*n + 17).is_negative is False
  694. assert (k + u).is_negative is None
  695. assert (k + v).is_negative is True
  696. assert (n + u).is_negative is False
  697. assert (n + v).is_negative is None
  698. assert (u - v).is_negative is False
  699. assert (u + v).is_negative is None
  700. assert (-u - v).is_negative is None
  701. assert (-u + v).is_negative is None
  702. assert (u - v + n + 2).is_negative is False
  703. assert (u + v + n + 2).is_negative is None
  704. assert (-u - v + n + 2).is_negative is None
  705. assert (-u + v + n + 2).is_negative is None
  706. assert (k + x).is_negative is None
  707. assert (k + x - n).is_negative is None
  708. assert (k - 2).is_positive is False
  709. assert (k + 17).is_positive is None
  710. assert (-k - 5).is_positive is None
  711. assert (-k + 123).is_positive is True
  712. assert (k - n).is_positive is False
  713. assert (k + n).is_positive is None
  714. assert (-k - n).is_positive is None
  715. assert (-k + n).is_positive is True
  716. assert (k - n - 2).is_positive is False
  717. assert (k + n + 17).is_positive is None
  718. assert (-k - n - 5).is_positive is None
  719. assert (-k + n + 123).is_positive is True
  720. assert (-2*k + 123*n + 17).is_positive is True
  721. assert (k + u).is_positive is None
  722. assert (k + v).is_positive is False
  723. assert (n + u).is_positive is True
  724. assert (n + v).is_positive is None
  725. assert (u - v).is_positive is None
  726. assert (u + v).is_positive is None
  727. assert (-u - v).is_positive is None
  728. assert (-u + v).is_positive is False
  729. assert (u - v - n - 2).is_positive is None
  730. assert (u + v - n - 2).is_positive is None
  731. assert (-u - v - n - 2).is_positive is None
  732. assert (-u + v - n - 2).is_positive is False
  733. assert (n + x).is_positive is None
  734. assert (n + x - k).is_positive is None
  735. z = (-3 - sqrt(5) + (-sqrt(10)/2 - sqrt(2)/2)**2)
  736. assert z.is_zero
  737. z = sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) - sqrt(10 + 6*sqrt(3))
  738. assert z.is_zero
  739. def test_Add_is_nonpositive_nonnegative():
  740. x = Symbol('x', real=True)
  741. k = Symbol('k', negative=True)
  742. n = Symbol('n', positive=True)
  743. u = Symbol('u', nonnegative=True)
  744. v = Symbol('v', nonpositive=True)
  745. assert (u - 2).is_nonpositive is None
  746. assert (u + 17).is_nonpositive is False
  747. assert (-u - 5).is_nonpositive is True
  748. assert (-u + 123).is_nonpositive is None
  749. assert (u - v).is_nonpositive is None
  750. assert (u + v).is_nonpositive is None
  751. assert (-u - v).is_nonpositive is None
  752. assert (-u + v).is_nonpositive is True
  753. assert (u - v - 2).is_nonpositive is None
  754. assert (u + v + 17).is_nonpositive is None
  755. assert (-u - v - 5).is_nonpositive is None
  756. assert (-u + v - 123).is_nonpositive is True
  757. assert (-2*u + 123*v - 17).is_nonpositive is True
  758. assert (k + u).is_nonpositive is None
  759. assert (k + v).is_nonpositive is True
  760. assert (n + u).is_nonpositive is False
  761. assert (n + v).is_nonpositive is None
  762. assert (k - n).is_nonpositive is True
  763. assert (k + n).is_nonpositive is None
  764. assert (-k - n).is_nonpositive is None
  765. assert (-k + n).is_nonpositive is False
  766. assert (k - n + u + 2).is_nonpositive is None
  767. assert (k + n + u + 2).is_nonpositive is None
  768. assert (-k - n + u + 2).is_nonpositive is None
  769. assert (-k + n + u + 2).is_nonpositive is False
  770. assert (u + x).is_nonpositive is None
  771. assert (v - x - n).is_nonpositive is None
  772. assert (u - 2).is_nonnegative is None
  773. assert (u + 17).is_nonnegative is True
  774. assert (-u - 5).is_nonnegative is False
  775. assert (-u + 123).is_nonnegative is None
  776. assert (u - v).is_nonnegative is True
  777. assert (u + v).is_nonnegative is None
  778. assert (-u - v).is_nonnegative is None
  779. assert (-u + v).is_nonnegative is None
  780. assert (u - v + 2).is_nonnegative is True
  781. assert (u + v + 17).is_nonnegative is None
  782. assert (-u - v - 5).is_nonnegative is None
  783. assert (-u + v - 123).is_nonnegative is False
  784. assert (2*u - 123*v + 17).is_nonnegative is True
  785. assert (k + u).is_nonnegative is None
  786. assert (k + v).is_nonnegative is False
  787. assert (n + u).is_nonnegative is True
  788. assert (n + v).is_nonnegative is None
  789. assert (k - n).is_nonnegative is False
  790. assert (k + n).is_nonnegative is None
  791. assert (-k - n).is_nonnegative is None
  792. assert (-k + n).is_nonnegative is True
  793. assert (k - n - u - 2).is_nonnegative is False
  794. assert (k + n - u - 2).is_nonnegative is None
  795. assert (-k - n - u - 2).is_nonnegative is None
  796. assert (-k + n - u - 2).is_nonnegative is None
  797. assert (u - x).is_nonnegative is None
  798. assert (v + x + n).is_nonnegative is None
  799. def test_Pow_is_integer():
  800. x = Symbol('x')
  801. k = Symbol('k', integer=True)
  802. n = Symbol('n', integer=True, nonnegative=True)
  803. m = Symbol('m', integer=True, positive=True)
  804. assert (k**2).is_integer is True
  805. assert (k**(-2)).is_integer is None
  806. assert ((m + 1)**(-2)).is_integer is False
  807. assert (m**(-1)).is_integer is None # issue 8580
  808. assert (2**k).is_integer is None
  809. assert (2**(-k)).is_integer is None
  810. assert (2**n).is_integer is True
  811. assert (2**(-n)).is_integer is None
  812. assert (2**m).is_integer is True
  813. assert (2**(-m)).is_integer is False
  814. assert (x**2).is_integer is None
  815. assert (2**x).is_integer is None
  816. assert (k**n).is_integer is True
  817. assert (k**(-n)).is_integer is None
  818. assert (k**x).is_integer is None
  819. assert (x**k).is_integer is None
  820. assert (k**(n*m)).is_integer is True
  821. assert (k**(-n*m)).is_integer is None
  822. assert sqrt(3).is_integer is False
  823. assert sqrt(.3).is_integer is False
  824. assert Pow(3, 2, evaluate=False).is_integer is True
  825. assert Pow(3, 0, evaluate=False).is_integer is True
  826. assert Pow(3, -2, evaluate=False).is_integer is False
  827. assert Pow(S.Half, 3, evaluate=False).is_integer is False
  828. # decided by re-evaluating
  829. assert Pow(3, S.Half, evaluate=False).is_integer is False
  830. assert Pow(3, S.Half, evaluate=False).is_integer is False
  831. assert Pow(4, S.Half, evaluate=False).is_integer is True
  832. assert Pow(S.Half, -2, evaluate=False).is_integer is True
  833. assert ((-1)**k).is_integer
  834. # issue 8641
  835. x = Symbol('x', real=True, integer=False)
  836. assert (x**2).is_integer is None
  837. # issue 10458
  838. x = Symbol('x', positive=True)
  839. assert (1/(x + 1)).is_integer is False
  840. assert (1/(-x - 1)).is_integer is False
  841. assert (-1/(x + 1)).is_integer is False
  842. # issue 23287
  843. assert (x**2/2).is_integer is None
  844. # issue 8648-like
  845. k = Symbol('k', even=True)
  846. assert (k**3/2).is_integer
  847. assert (k**3/8).is_integer
  848. assert (k**3/16).is_integer is None
  849. assert (2/k).is_integer is None
  850. assert (2/k**2).is_integer is False
  851. o = Symbol('o', odd=True)
  852. assert (k/o).is_integer is None
  853. o = Symbol('o', odd=True, prime=True)
  854. assert (k/o).is_integer is False
  855. def test_Pow_is_real():
  856. x = Symbol('x', real=True)
  857. y = Symbol('y', positive=True)
  858. assert (x**2).is_real is True
  859. assert (x**3).is_real is True
  860. assert (x**x).is_real is None
  861. assert (y**x).is_real is True
  862. assert (x**Rational(1, 3)).is_real is None
  863. assert (y**Rational(1, 3)).is_real is True
  864. assert sqrt(-1 - sqrt(2)).is_real is False
  865. i = Symbol('i', imaginary=True)
  866. assert (i**i).is_real is None
  867. assert (I**i).is_extended_real is True
  868. assert ((-I)**i).is_extended_real is True
  869. assert (2**i).is_real is None # (2**(pi/log(2) * I)) is real, 2**I is not
  870. assert (2**I).is_real is False
  871. assert (2**-I).is_real is False
  872. assert (i**2).is_extended_real is True
  873. assert (i**3).is_extended_real is False
  874. assert (i**x).is_real is None # could be (-I)**(2/3)
  875. e = Symbol('e', even=True)
  876. o = Symbol('o', odd=True)
  877. k = Symbol('k', integer=True)
  878. assert (i**e).is_extended_real is True
  879. assert (i**o).is_extended_real is False
  880. assert (i**k).is_real is None
  881. assert (i**(4*k)).is_extended_real is True
  882. x = Symbol("x", nonnegative=True)
  883. y = Symbol("y", nonnegative=True)
  884. assert im(x**y).expand(complex=True) is S.Zero
  885. assert (x**y).is_real is True
  886. i = Symbol('i', imaginary=True)
  887. assert (exp(i)**I).is_extended_real is True
  888. assert log(exp(i)).is_imaginary is None # i could be 2*pi*I
  889. c = Symbol('c', complex=True)
  890. assert log(c).is_real is None # c could be 0 or 2, too
  891. assert log(exp(c)).is_real is None # log(0), log(E), ...
  892. n = Symbol('n', negative=False)
  893. assert log(n).is_real is None
  894. n = Symbol('n', nonnegative=True)
  895. assert log(n).is_real is None
  896. assert sqrt(-I).is_real is False # issue 7843
  897. i = Symbol('i', integer=True)
  898. assert (1/(i-1)).is_real is None
  899. assert (1/(i-1)).is_extended_real is None
  900. # test issue 20715
  901. from sympy.core.parameters import evaluate
  902. x = S(-1)
  903. with evaluate(False):
  904. assert x.is_negative is True
  905. f = Pow(x, -1)
  906. with evaluate(False):
  907. assert f.is_imaginary is False
  908. def test_real_Pow():
  909. k = Symbol('k', integer=True, nonzero=True)
  910. assert (k**(I*pi/log(k))).is_real
  911. def test_Pow_is_finite():
  912. xe = Symbol('xe', extended_real=True)
  913. xr = Symbol('xr', real=True)
  914. p = Symbol('p', positive=True)
  915. n = Symbol('n', negative=True)
  916. i = Symbol('i', integer=True)
  917. assert (xe**2).is_finite is None # xe could be oo
  918. assert (xr**2).is_finite is True
  919. assert (xe**xe).is_finite is None
  920. assert (xr**xe).is_finite is None
  921. assert (xe**xr).is_finite is None
  922. # FIXME: The line below should be True rather than None
  923. # assert (xr**xr).is_finite is True
  924. assert (xr**xr).is_finite is None
  925. assert (p**xe).is_finite is None
  926. assert (p**xr).is_finite is True
  927. assert (n**xe).is_finite is None
  928. assert (n**xr).is_finite is True
  929. assert (sin(xe)**2).is_finite is True
  930. assert (sin(xr)**2).is_finite is True
  931. assert (sin(xe)**xe).is_finite is None # xe, xr could be -pi
  932. assert (sin(xr)**xr).is_finite is None
  933. # FIXME: Should the line below be True rather than None?
  934. assert (sin(xe)**exp(xe)).is_finite is None
  935. assert (sin(xr)**exp(xr)).is_finite is True
  936. assert (1/sin(xe)).is_finite is None # if zero, no, otherwise yes
  937. assert (1/sin(xr)).is_finite is None
  938. assert (1/exp(xe)).is_finite is None # xe could be -oo
  939. assert (1/exp(xr)).is_finite is True
  940. assert (1/S.Pi).is_finite is True
  941. assert (1/(i-1)).is_finite is None
  942. def test_Pow_is_even_odd():
  943. x = Symbol('x')
  944. k = Symbol('k', even=True)
  945. n = Symbol('n', odd=True)
  946. m = Symbol('m', integer=True, nonnegative=True)
  947. p = Symbol('p', integer=True, positive=True)
  948. assert ((-1)**n).is_odd
  949. assert ((-1)**k).is_odd
  950. assert ((-1)**(m - p)).is_odd
  951. assert (k**2).is_even is True
  952. assert (n**2).is_even is False
  953. assert (2**k).is_even is None
  954. assert (x**2).is_even is None
  955. assert (k**m).is_even is None
  956. assert (n**m).is_even is False
  957. assert (k**p).is_even is True
  958. assert (n**p).is_even is False
  959. assert (m**k).is_even is None
  960. assert (p**k).is_even is None
  961. assert (m**n).is_even is None
  962. assert (p**n).is_even is None
  963. assert (k**x).is_even is None
  964. assert (n**x).is_even is None
  965. assert (k**2).is_odd is False
  966. assert (n**2).is_odd is True
  967. assert (3**k).is_odd is None
  968. assert (k**m).is_odd is None
  969. assert (n**m).is_odd is True
  970. assert (k**p).is_odd is False
  971. assert (n**p).is_odd is True
  972. assert (m**k).is_odd is None
  973. assert (p**k).is_odd is None
  974. assert (m**n).is_odd is None
  975. assert (p**n).is_odd is None
  976. assert (k**x).is_odd is None
  977. assert (n**x).is_odd is None
  978. def test_Pow_is_negative_positive():
  979. r = Symbol('r', real=True)
  980. k = Symbol('k', integer=True, positive=True)
  981. n = Symbol('n', even=True)
  982. m = Symbol('m', odd=True)
  983. x = Symbol('x')
  984. assert (2**r).is_positive is True
  985. assert ((-2)**r).is_positive is None
  986. assert ((-2)**n).is_positive is True
  987. assert ((-2)**m).is_positive is False
  988. assert (k**2).is_positive is True
  989. assert (k**(-2)).is_positive is True
  990. assert (k**r).is_positive is True
  991. assert ((-k)**r).is_positive is None
  992. assert ((-k)**n).is_positive is True
  993. assert ((-k)**m).is_positive is False
  994. assert (2**r).is_negative is False
  995. assert ((-2)**r).is_negative is None
  996. assert ((-2)**n).is_negative is False
  997. assert ((-2)**m).is_negative is True
  998. assert (k**2).is_negative is False
  999. assert (k**(-2)).is_negative is False
  1000. assert (k**r).is_negative is False
  1001. assert ((-k)**r).is_negative is None
  1002. assert ((-k)**n).is_negative is False
  1003. assert ((-k)**m).is_negative is True
  1004. assert (2**x).is_positive is None
  1005. assert (2**x).is_negative is None
  1006. def test_Pow_is_zero():
  1007. z = Symbol('z', zero=True)
  1008. e = z**2
  1009. assert e.is_zero
  1010. assert e.is_positive is False
  1011. assert e.is_negative is False
  1012. assert Pow(0, 0, evaluate=False).is_zero is False
  1013. assert Pow(0, 3, evaluate=False).is_zero
  1014. assert Pow(0, oo, evaluate=False).is_zero
  1015. assert Pow(0, -3, evaluate=False).is_zero is False
  1016. assert Pow(0, -oo, evaluate=False).is_zero is False
  1017. assert Pow(2, 2, evaluate=False).is_zero is False
  1018. a = Symbol('a', zero=False)
  1019. assert Pow(a, 3).is_zero is False # issue 7965
  1020. assert Pow(2, oo, evaluate=False).is_zero is False
  1021. assert Pow(2, -oo, evaluate=False).is_zero
  1022. assert Pow(S.Half, oo, evaluate=False).is_zero
  1023. assert Pow(S.Half, -oo, evaluate=False).is_zero is False
  1024. # All combinations of real/complex base/exponent
  1025. h = S.Half
  1026. T = True
  1027. F = False
  1028. N = None
  1029. pow_iszero = [
  1030. ['**', 0, h, 1, 2, -h, -1,-2,-2*I,-I/2,I/2,1+I,oo,-oo,zoo],
  1031. [ 0, F, T, T, T, F, F, F, F, F, F, N, T, F, N],
  1032. [ h, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
  1033. [ 1, F, F, F, F, F, F, F, F, F, F, F, F, F, N],
  1034. [ 2, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
  1035. [ -h, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
  1036. [ -1, F, F, F, F, F, F, F, F, F, F, F, F, F, N],
  1037. [ -2, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
  1038. [-2*I, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
  1039. [-I/2, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
  1040. [ I/2, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
  1041. [ 1+I, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
  1042. [ oo, F, F, F, F, T, T, T, F, F, F, F, F, T, N],
  1043. [ -oo, F, F, F, F, T, T, T, F, F, F, F, F, T, N],
  1044. [ zoo, F, F, F, F, T, T, T, N, N, N, N, F, T, N]
  1045. ]
  1046. def test_table(table):
  1047. n = len(table[0])
  1048. for row in range(1, n):
  1049. base = table[row][0]
  1050. for col in range(1, n):
  1051. exp = table[0][col]
  1052. is_zero = table[row][col]
  1053. # The actual test here:
  1054. assert Pow(base, exp, evaluate=False).is_zero is is_zero
  1055. test_table(pow_iszero)
  1056. # A zero symbol...
  1057. zo, zo2 = symbols('zo, zo2', zero=True)
  1058. # All combinations of finite symbols
  1059. zf, zf2 = symbols('zf, zf2', finite=True)
  1060. wf, wf2 = symbols('wf, wf2', nonzero=True)
  1061. xf, xf2 = symbols('xf, xf2', real=True)
  1062. yf, yf2 = symbols('yf, yf2', nonzero=True)
  1063. af, af2 = symbols('af, af2', positive=True)
  1064. bf, bf2 = symbols('bf, bf2', nonnegative=True)
  1065. cf, cf2 = symbols('cf, cf2', negative=True)
  1066. df, df2 = symbols('df, df2', nonpositive=True)
  1067. # Without finiteness:
  1068. zi, zi2 = symbols('zi, zi2')
  1069. wi, wi2 = symbols('wi, wi2', zero=False)
  1070. xi, xi2 = symbols('xi, xi2', extended_real=True)
  1071. yi, yi2 = symbols('yi, yi2', zero=False, extended_real=True)
  1072. ai, ai2 = symbols('ai, ai2', extended_positive=True)
  1073. bi, bi2 = symbols('bi, bi2', extended_nonnegative=True)
  1074. ci, ci2 = symbols('ci, ci2', extended_negative=True)
  1075. di, di2 = symbols('di, di2', extended_nonpositive=True)
  1076. pow_iszero_sym = [
  1077. ['**',zo,wf,yf,af,cf,zf,xf,bf,df,zi,wi,xi,yi,ai,bi,ci,di],
  1078. [ zo2, F, N, N, T, F, N, N, N, F, N, N, N, N, T, N, F, F],
  1079. [ wf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
  1080. [ yf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
  1081. [ af2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
  1082. [ cf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
  1083. [ zf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
  1084. [ xf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
  1085. [ bf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
  1086. [ df2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
  1087. [ zi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
  1088. [ wi2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
  1089. [ xi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
  1090. [ yi2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
  1091. [ ai2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
  1092. [ bi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
  1093. [ ci2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
  1094. [ di2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N]
  1095. ]
  1096. test_table(pow_iszero_sym)
  1097. # In some cases (x**x).is_zero is different from (x**y).is_zero even if y
  1098. # has the same assumptions as x.
  1099. assert (zo ** zo).is_zero is False
  1100. assert (wf ** wf).is_zero is False
  1101. assert (yf ** yf).is_zero is False
  1102. assert (af ** af).is_zero is False
  1103. assert (cf ** cf).is_zero is False
  1104. assert (zf ** zf).is_zero is None
  1105. assert (xf ** xf).is_zero is None
  1106. assert (bf ** bf).is_zero is False # None in table
  1107. assert (df ** df).is_zero is None
  1108. assert (zi ** zi).is_zero is None
  1109. assert (wi ** wi).is_zero is None
  1110. assert (xi ** xi).is_zero is None
  1111. assert (yi ** yi).is_zero is None
  1112. assert (ai ** ai).is_zero is False # None in table
  1113. assert (bi ** bi).is_zero is False # None in table
  1114. assert (ci ** ci).is_zero is None
  1115. assert (di ** di).is_zero is None
  1116. def test_Pow_is_nonpositive_nonnegative():
  1117. x = Symbol('x', real=True)
  1118. k = Symbol('k', integer=True, nonnegative=True)
  1119. l = Symbol('l', integer=True, positive=True)
  1120. n = Symbol('n', even=True)
  1121. m = Symbol('m', odd=True)
  1122. assert (x**(4*k)).is_nonnegative is True
  1123. assert (2**x).is_nonnegative is True
  1124. assert ((-2)**x).is_nonnegative is None
  1125. assert ((-2)**n).is_nonnegative is True
  1126. assert ((-2)**m).is_nonnegative is False
  1127. assert (k**2).is_nonnegative is True
  1128. assert (k**(-2)).is_nonnegative is None
  1129. assert (k**k).is_nonnegative is True
  1130. assert (k**x).is_nonnegative is None # NOTE (0**x).is_real = U
  1131. assert (l**x).is_nonnegative is True
  1132. assert (l**x).is_positive is True
  1133. assert ((-k)**x).is_nonnegative is None
  1134. assert ((-k)**m).is_nonnegative is None
  1135. assert (2**x).is_nonpositive is False
  1136. assert ((-2)**x).is_nonpositive is None
  1137. assert ((-2)**n).is_nonpositive is False
  1138. assert ((-2)**m).is_nonpositive is True
  1139. assert (k**2).is_nonpositive is None
  1140. assert (k**(-2)).is_nonpositive is None
  1141. assert (k**x).is_nonpositive is None
  1142. assert ((-k)**x).is_nonpositive is None
  1143. assert ((-k)**n).is_nonpositive is None
  1144. assert (x**2).is_nonnegative is True
  1145. i = symbols('i', imaginary=True)
  1146. assert (i**2).is_nonpositive is True
  1147. assert (i**4).is_nonpositive is False
  1148. assert (i**3).is_nonpositive is False
  1149. assert (I**i).is_nonnegative is True
  1150. assert (exp(I)**i).is_nonnegative is True
  1151. assert ((-l)**n).is_nonnegative is True
  1152. assert ((-l)**m).is_nonpositive is True
  1153. assert ((-k)**n).is_nonnegative is None
  1154. assert ((-k)**m).is_nonpositive is None
  1155. def test_Mul_is_imaginary_real():
  1156. r = Symbol('r', real=True)
  1157. p = Symbol('p', positive=True)
  1158. i1 = Symbol('i1', imaginary=True)
  1159. i2 = Symbol('i2', imaginary=True)
  1160. x = Symbol('x')
  1161. assert I.is_imaginary is True
  1162. assert I.is_real is False
  1163. assert (-I).is_imaginary is True
  1164. assert (-I).is_real is False
  1165. assert (3*I).is_imaginary is True
  1166. assert (3*I).is_real is False
  1167. assert (I*I).is_imaginary is False
  1168. assert (I*I).is_real is True
  1169. e = (p + p*I)
  1170. j = Symbol('j', integer=True, zero=False)
  1171. assert (e**j).is_real is None
  1172. assert (e**(2*j)).is_real is None
  1173. assert (e**j).is_imaginary is None
  1174. assert (e**(2*j)).is_imaginary is None
  1175. assert (e**-1).is_imaginary is False
  1176. assert (e**2).is_imaginary
  1177. assert (e**3).is_imaginary is False
  1178. assert (e**4).is_imaginary is False
  1179. assert (e**5).is_imaginary is False
  1180. assert (e**-1).is_real is False
  1181. assert (e**2).is_real is False
  1182. assert (e**3).is_real is False
  1183. assert (e**4).is_real is True
  1184. assert (e**5).is_real is False
  1185. assert (e**3).is_complex
  1186. assert (r*i1).is_imaginary is None
  1187. assert (r*i1).is_real is None
  1188. assert (x*i1).is_imaginary is None
  1189. assert (x*i1).is_real is None
  1190. assert (i1*i2).is_imaginary is False
  1191. assert (i1*i2).is_real is True
  1192. assert (r*i1*i2).is_imaginary is False
  1193. assert (r*i1*i2).is_real is True
  1194. # Github's issue 5874:
  1195. nr = Symbol('nr', real=False, complex=True) # e.g. I or 1 + I
  1196. a = Symbol('a', real=True, nonzero=True)
  1197. b = Symbol('b', real=True)
  1198. assert (i1*nr).is_real is None
  1199. assert (a*nr).is_real is False
  1200. assert (b*nr).is_real is None
  1201. ni = Symbol('ni', imaginary=False, complex=True) # e.g. 2 or 1 + I
  1202. a = Symbol('a', real=True, nonzero=True)
  1203. b = Symbol('b', real=True)
  1204. assert (i1*ni).is_real is False
  1205. assert (a*ni).is_real is None
  1206. assert (b*ni).is_real is None
  1207. def test_Mul_hermitian_antihermitian():
  1208. xz, yz = symbols('xz, yz', zero=True, antihermitian=True)
  1209. xf, yf = symbols('xf, yf', hermitian=False, antihermitian=False, finite=True)
  1210. xh, yh = symbols('xh, yh', hermitian=True, antihermitian=False, nonzero=True)
  1211. xa, ya = symbols('xa, ya', hermitian=False, antihermitian=True, zero=False, finite=True)
  1212. assert (xz*xh).is_hermitian is True
  1213. assert (xz*xh).is_antihermitian is True
  1214. assert (xz*xa).is_hermitian is True
  1215. assert (xz*xa).is_antihermitian is True
  1216. assert (xf*yf).is_hermitian is None
  1217. assert (xf*yf).is_antihermitian is None
  1218. assert (xh*yh).is_hermitian is True
  1219. assert (xh*yh).is_antihermitian is False
  1220. assert (xh*ya).is_hermitian is False
  1221. assert (xh*ya).is_antihermitian is True
  1222. assert (xa*ya).is_hermitian is True
  1223. assert (xa*ya).is_antihermitian is False
  1224. a = Symbol('a', hermitian=True, zero=False)
  1225. b = Symbol('b', hermitian=True)
  1226. c = Symbol('c', hermitian=False)
  1227. d = Symbol('d', antihermitian=True)
  1228. e1 = Mul(a, b, c, evaluate=False)
  1229. e2 = Mul(b, a, c, evaluate=False)
  1230. e3 = Mul(a, b, c, d, evaluate=False)
  1231. e4 = Mul(b, a, c, d, evaluate=False)
  1232. e5 = Mul(a, c, evaluate=False)
  1233. e6 = Mul(a, c, d, evaluate=False)
  1234. assert e1.is_hermitian is None
  1235. assert e2.is_hermitian is None
  1236. assert e1.is_antihermitian is None
  1237. assert e2.is_antihermitian is None
  1238. assert e3.is_antihermitian is None
  1239. assert e4.is_antihermitian is None
  1240. assert e5.is_antihermitian is None
  1241. assert e6.is_antihermitian is None
  1242. def test_Add_is_comparable():
  1243. assert (x + y).is_comparable is False
  1244. assert (x + 1).is_comparable is False
  1245. assert (Rational(1, 3) - sqrt(8)).is_comparable is True
  1246. def test_Mul_is_comparable():
  1247. assert (x*y).is_comparable is False
  1248. assert (x*2).is_comparable is False
  1249. assert (sqrt(2)*Rational(1, 3)).is_comparable is True
  1250. def test_Pow_is_comparable():
  1251. assert (x**y).is_comparable is False
  1252. assert (x**2).is_comparable is False
  1253. assert (sqrt(Rational(1, 3))).is_comparable is True
  1254. def test_Add_is_positive_2():
  1255. e = Rational(1, 3) - sqrt(8)
  1256. assert e.is_positive is False
  1257. assert e.is_negative is True
  1258. e = pi - 1
  1259. assert e.is_positive is True
  1260. assert e.is_negative is False
  1261. def test_Add_is_irrational():
  1262. i = Symbol('i', irrational=True)
  1263. assert i.is_irrational is True
  1264. assert i.is_rational is False
  1265. assert (i + 1).is_irrational is True
  1266. assert (i + 1).is_rational is False
  1267. def test_Mul_is_irrational():
  1268. expr = Mul(1, 2, 3, evaluate=False)
  1269. assert expr.is_irrational is False
  1270. expr = Mul(1, I, I, evaluate=False)
  1271. assert expr.is_rational is None # I * I = -1 but *no evaluation allowed*
  1272. # sqrt(2) * I * I = -sqrt(2) is irrational but
  1273. # this can't be determined without evaluating the
  1274. # expression and the eval_is routines shouldn't do that
  1275. expr = Mul(sqrt(2), I, I, evaluate=False)
  1276. assert expr.is_irrational is None
  1277. def test_issue_3531():
  1278. # https://github.com/sympy/sympy/issues/3531
  1279. # https://github.com/sympy/sympy/pull/18116
  1280. class MightyNumeric(tuple):
  1281. def __rtruediv__(self, other):
  1282. return "something"
  1283. assert sympify(1)/MightyNumeric((1, 2)) == "something"
  1284. def test_issue_3531b():
  1285. class Foo:
  1286. def __init__(self):
  1287. self.field = 1.0
  1288. def __mul__(self, other):
  1289. self.field = self.field * other
  1290. def __rmul__(self, other):
  1291. self.field = other * self.field
  1292. f = Foo()
  1293. x = Symbol("x")
  1294. assert f*x == x*f
  1295. def test_bug3():
  1296. a = Symbol("a")
  1297. b = Symbol("b", positive=True)
  1298. e = 2*a + b
  1299. f = b + 2*a
  1300. assert e == f
  1301. def test_suppressed_evaluation():
  1302. a = Add(0, 3, 2, evaluate=False)
  1303. b = Mul(1, 3, 2, evaluate=False)
  1304. c = Pow(3, 2, evaluate=False)
  1305. assert a != 6
  1306. assert a.func is Add
  1307. assert a.args == (0, 3, 2)
  1308. assert b != 6
  1309. assert b.func is Mul
  1310. assert b.args == (1, 3, 2)
  1311. assert c != 9
  1312. assert c.func is Pow
  1313. assert c.args == (3, 2)
  1314. def test_AssocOp_doit():
  1315. a = Add(x,x, evaluate=False)
  1316. b = Mul(y,y, evaluate=False)
  1317. c = Add(b,b, evaluate=False)
  1318. d = Mul(a,a, evaluate=False)
  1319. assert c.doit(deep=False).func == Mul
  1320. assert c.doit(deep=False).args == (2,y,y)
  1321. assert c.doit().func == Mul
  1322. assert c.doit().args == (2, Pow(y,2))
  1323. assert d.doit(deep=False).func == Pow
  1324. assert d.doit(deep=False).args == (a, 2*S.One)
  1325. assert d.doit().func == Mul
  1326. assert d.doit().args == (4*S.One, Pow(x,2))
  1327. def test_Add_Mul_Expr_args():
  1328. nonexpr = [Basic(), Poly(x, x), FiniteSet(x)]
  1329. for typ in [Add, Mul]:
  1330. for obj in nonexpr:
  1331. # The cache can mess with the stacklevel check
  1332. with warns(SymPyDeprecationWarning, test_stacklevel=False):
  1333. typ(obj, 1)
  1334. def test_Add_as_coeff_mul():
  1335. # issue 5524. These should all be (1, self)
  1336. assert (x + 1).as_coeff_mul() == (1, (x + 1,))
  1337. assert (x + 2).as_coeff_mul() == (1, (x + 2,))
  1338. assert (x + 3).as_coeff_mul() == (1, (x + 3,))
  1339. assert (x - 1).as_coeff_mul() == (1, (x - 1,))
  1340. assert (x - 2).as_coeff_mul() == (1, (x - 2,))
  1341. assert (x - 3).as_coeff_mul() == (1, (x - 3,))
  1342. n = Symbol('n', integer=True)
  1343. assert (n + 1).as_coeff_mul() == (1, (n + 1,))
  1344. assert (n + 2).as_coeff_mul() == (1, (n + 2,))
  1345. assert (n + 3).as_coeff_mul() == (1, (n + 3,))
  1346. assert (n - 1).as_coeff_mul() == (1, (n - 1,))
  1347. assert (n - 2).as_coeff_mul() == (1, (n - 2,))
  1348. assert (n - 3).as_coeff_mul() == (1, (n - 3,))
  1349. def test_Pow_as_coeff_mul_doesnt_expand():
  1350. assert exp(x + y).as_coeff_mul() == (1, (exp(x + y),))
  1351. assert exp(x + exp(x + y)) != exp(x + exp(x)*exp(y))
  1352. def test_issue_24751():
  1353. expr = Add(-2, -3, evaluate=False)
  1354. expr1 = Add(-1, expr, evaluate=False)
  1355. assert int(expr1) == int((-3 - 2) - 1)
  1356. def test_issue_3514_18626():
  1357. assert sqrt(S.Half) * sqrt(6) == 2 * sqrt(3)/2
  1358. assert S.Half*sqrt(6)*sqrt(2) == sqrt(3)
  1359. assert sqrt(6)/2*sqrt(2) == sqrt(3)
  1360. assert sqrt(6)*sqrt(2)/2 == sqrt(3)
  1361. assert sqrt(8)**Rational(2, 3) == 2
  1362. def test_make_args():
  1363. assert Add.make_args(x) == (x,)
  1364. assert Mul.make_args(x) == (x,)
  1365. assert Add.make_args(x*y*z) == (x*y*z,)
  1366. assert Mul.make_args(x*y*z) == (x*y*z).args
  1367. assert Add.make_args(x + y + z) == (x + y + z).args
  1368. assert Mul.make_args(x + y + z) == (x + y + z,)
  1369. assert Add.make_args((x + y)**z) == ((x + y)**z,)
  1370. assert Mul.make_args((x + y)**z) == ((x + y)**z,)
  1371. def test_issue_5126():
  1372. assert (-2)**x*(-3)**x != 6**x
  1373. i = Symbol('i', integer=1)
  1374. assert (-2)**i*(-3)**i == 6**i
  1375. def test_Rational_as_content_primitive():
  1376. c, p = S.One, S.Zero
  1377. assert (c*p).as_content_primitive() == (c, p)
  1378. c, p = S.Half, S.One
  1379. assert (c*p).as_content_primitive() == (c, p)
  1380. def test_Add_as_content_primitive():
  1381. assert (x + 2).as_content_primitive() == (1, x + 2)
  1382. assert (3*x + 2).as_content_primitive() == (1, 3*x + 2)
  1383. assert (3*x + 3).as_content_primitive() == (3, x + 1)
  1384. assert (3*x + 6).as_content_primitive() == (3, x + 2)
  1385. assert (3*x + 2*y).as_content_primitive() == (1, 3*x + 2*y)
  1386. assert (3*x + 3*y).as_content_primitive() == (3, x + y)
  1387. assert (3*x + 6*y).as_content_primitive() == (3, x + 2*y)
  1388. assert (3/x + 2*x*y*z**2).as_content_primitive() == (1, 3/x + 2*x*y*z**2)
  1389. assert (3/x + 3*x*y*z**2).as_content_primitive() == (3, 1/x + x*y*z**2)
  1390. assert (3/x + 6*x*y*z**2).as_content_primitive() == (3, 1/x + 2*x*y*z**2)
  1391. assert (2*x/3 + 4*y/9).as_content_primitive() == \
  1392. (Rational(2, 9), 3*x + 2*y)
  1393. assert (2*x/3 + 2.5*y).as_content_primitive() == \
  1394. (Rational(1, 3), 2*x + 7.5*y)
  1395. # the coefficient may sort to a position other than 0
  1396. p = 3 + x + y
  1397. assert (2*p).expand().as_content_primitive() == (2, p)
  1398. assert (2.0*p).expand().as_content_primitive() == (1, 2.*p)
  1399. p *= -1
  1400. assert (2*p).expand().as_content_primitive() == (2, p)
  1401. def test_Mul_as_content_primitive():
  1402. assert (2*x).as_content_primitive() == (2, x)
  1403. assert (x*(2 + 2*x)).as_content_primitive() == (2, x*(1 + x))
  1404. assert (x*(2 + 2*y)*(3*x + 3)**2).as_content_primitive() == \
  1405. (18, x*(1 + y)*(x + 1)**2)
  1406. assert ((2 + 2*x)**2*(3 + 6*x) + S.Half).as_content_primitive() == \
  1407. (S.Half, 24*(x + 1)**2*(2*x + 1) + 1)
  1408. def test_Pow_as_content_primitive():
  1409. assert (x**y).as_content_primitive() == (1, x**y)
  1410. assert ((2*x + 2)**y).as_content_primitive() == \
  1411. (1, (Mul(2, (x + 1), evaluate=False))**y)
  1412. assert ((2*x + 2)**3).as_content_primitive() == (8, (x + 1)**3)
  1413. def test_issue_5460():
  1414. u = Mul(2, (1 + x), evaluate=False)
  1415. assert (2 + u).args == (2, u)
  1416. def test_product_irrational():
  1417. assert (I*pi).is_irrational is False
  1418. # The following used to be deduced from the above bug:
  1419. assert (I*pi).is_positive is False
  1420. def test_issue_5919():
  1421. assert (x/(y*(1 + y))).expand() == x/(y**2 + y)
  1422. def test_Mod():
  1423. assert Mod(x, 1).func is Mod
  1424. assert pi % pi is S.Zero
  1425. assert Mod(5, 3) == 2
  1426. assert Mod(-5, 3) == 1
  1427. assert Mod(5, -3) == -1
  1428. assert Mod(-5, -3) == -2
  1429. assert type(Mod(3.2, 2, evaluate=False)) == Mod
  1430. assert 5 % x == Mod(5, x)
  1431. assert x % 5 == Mod(x, 5)
  1432. assert x % y == Mod(x, y)
  1433. assert (x % y).subs({x: 5, y: 3}) == 2
  1434. assert Mod(nan, 1) is nan
  1435. assert Mod(1, nan) is nan
  1436. assert Mod(nan, nan) is nan
  1437. assert Mod(0, x) == 0
  1438. with raises(ZeroDivisionError):
  1439. Mod(x, 0)
  1440. k = Symbol('k', integer=True)
  1441. m = Symbol('m', integer=True, positive=True)
  1442. assert (x**m % x).func is Mod
  1443. assert (k**(-m) % k).func is Mod
  1444. assert k**m % k == 0
  1445. assert (-2*k)**m % k == 0
  1446. # Float handling
  1447. point3 = Float(3.3) % 1
  1448. assert (x - 3.3) % 1 == Mod(1.*x + 1 - point3, 1)
  1449. assert Mod(-3.3, 1) == 1 - point3
  1450. assert Mod(0.7, 1) == Float(0.7)
  1451. e = Mod(1.3, 1)
  1452. assert comp(e, .3) and e.is_Float
  1453. e = Mod(1.3, .7)
  1454. assert comp(e, .6) and e.is_Float
  1455. e = Mod(1.3, Rational(7, 10))
  1456. assert comp(e, .6) and e.is_Float
  1457. e = Mod(Rational(13, 10), 0.7)
  1458. assert comp(e, .6) and e.is_Float
  1459. e = Mod(Rational(13, 10), Rational(7, 10))
  1460. assert comp(e, .6) and e.is_Rational
  1461. # check that sign is right
  1462. r2 = sqrt(2)
  1463. r3 = sqrt(3)
  1464. for i in [-r3, -r2, r2, r3]:
  1465. for j in [-r3, -r2, r2, r3]:
  1466. assert verify_numerically(i % j, i.n() % j.n())
  1467. for _x in range(4):
  1468. for _y in range(9):
  1469. reps = [(x, _x), (y, _y)]
  1470. assert Mod(3*x + y, 9).subs(reps) == (3*_x + _y) % 9
  1471. # denesting
  1472. t = Symbol('t', real=True)
  1473. assert Mod(Mod(x, t), t) == Mod(x, t)
  1474. assert Mod(-Mod(x, t), t) == Mod(-x, t)
  1475. assert Mod(Mod(x, 2*t), t) == Mod(x, t)
  1476. assert Mod(-Mod(x, 2*t), t) == Mod(-x, t)
  1477. assert Mod(Mod(x, t), 2*t) == Mod(x, t)
  1478. assert Mod(-Mod(x, t), -2*t) == -Mod(x, t)
  1479. for i in [-4, -2, 2, 4]:
  1480. for j in [-4, -2, 2, 4]:
  1481. for k in range(4):
  1482. assert Mod(Mod(x, i), j).subs({x: k}) == (k % i) % j
  1483. assert Mod(-Mod(x, i), j).subs({x: k}) == -(k % i) % j
  1484. # known difference
  1485. assert Mod(5*sqrt(2), sqrt(5)) == 5*sqrt(2) - 3*sqrt(5)
  1486. p = symbols('p', positive=True)
  1487. assert Mod(2, p + 3) == 2
  1488. assert Mod(-2, p + 3) == p + 1
  1489. assert Mod(2, -p - 3) == -p - 1
  1490. assert Mod(-2, -p - 3) == -2
  1491. assert Mod(p + 5, p + 3) == 2
  1492. assert Mod(-p - 5, p + 3) == p + 1
  1493. assert Mod(p + 5, -p - 3) == -p - 1
  1494. assert Mod(-p - 5, -p - 3) == -2
  1495. assert Mod(p + 1, p - 1).func is Mod
  1496. # handling sums
  1497. assert (x + 3) % 1 == Mod(x, 1)
  1498. assert (x + 3.0) % 1 == Mod(1.*x, 1)
  1499. assert (x - S(33)/10) % 1 == Mod(x + S(7)/10, 1)
  1500. a = Mod(.6*x + y, .3*y)
  1501. b = Mod(0.1*y + 0.6*x, 0.3*y)
  1502. # Test that a, b are equal, with 1e-14 accuracy in coefficients
  1503. eps = 1e-14
  1504. assert abs((a.args[0] - b.args[0]).subs({x: 1, y: 1})) < eps
  1505. assert abs((a.args[1] - b.args[1]).subs({x: 1, y: 1})) < eps
  1506. assert (x + 1) % x == 1 % x
  1507. assert (x + y) % x == y % x
  1508. assert (x + y + 2) % x == (y + 2) % x
  1509. assert (a + 3*x + 1) % (2*x) == Mod(a + x + 1, 2*x)
  1510. assert (12*x + 18*y) % (3*x) == 3*Mod(6*y, x)
  1511. # gcd extraction
  1512. assert (-3*x) % (-2*y) == -Mod(3*x, 2*y)
  1513. assert (.6*pi) % (.3*x*pi) == 0.3*pi*Mod(2, x)
  1514. assert (.6*pi) % (.31*x*pi) == pi*Mod(0.6, 0.31*x)
  1515. assert (6*pi) % (.3*x*pi) == 0.3*pi*Mod(20, x)
  1516. assert (6*pi) % (.31*x*pi) == pi*Mod(6, 0.31*x)
  1517. assert (6*pi) % (.42*x*pi) == pi*Mod(6, 0.42*x)
  1518. assert (12*x) % (2*y) == 2*Mod(6*x, y)
  1519. assert (12*x) % (3*5*y) == 3*Mod(4*x, 5*y)
  1520. assert (12*x) % (15*x*y) == 3*x*Mod(4, 5*y)
  1521. assert (-2*pi) % (3*pi) == pi
  1522. assert (2*x + 2) % (x + 1) == 0
  1523. assert (x*(x + 1)) % (x + 1) == (x + 1)*Mod(x, 1)
  1524. assert Mod(5.0*x, 0.1*y) == 0.1*Mod(50*x, y)
  1525. i = Symbol('i', integer=True)
  1526. assert (3*i*x) % (2*i*y) == i*Mod(3*x, 2*y)
  1527. assert Mod(4*i, 4) == 0
  1528. # issue 8677
  1529. n = Symbol('n', integer=True, positive=True)
  1530. assert factorial(n) % n == 0
  1531. assert factorial(n + 2) % n == 0
  1532. assert (factorial(n + 4) % (n + 5)).func is Mod
  1533. # Wilson's theorem
  1534. assert factorial(18042, evaluate=False) % 18043 == 18042
  1535. p = Symbol('n', prime=True)
  1536. assert factorial(p - 1) % p == p - 1
  1537. assert factorial(p - 1) % -p == -1
  1538. assert (factorial(3, evaluate=False) % 4).doit() == 2
  1539. n = Symbol('n', composite=True, odd=True)
  1540. assert factorial(n - 1) % n == 0
  1541. # symbolic with known parity
  1542. n = Symbol('n', even=True)
  1543. assert Mod(n, 2) == 0
  1544. n = Symbol('n', odd=True)
  1545. assert Mod(n, 2) == 1
  1546. # issue 10963
  1547. assert (x**6000%400).args[1] == 400
  1548. #issue 13543
  1549. assert Mod(Mod(x + 1, 2) + 1, 2) == Mod(x, 2)
  1550. x1 = Symbol('x1', integer=True)
  1551. assert Mod(Mod(x1 + 2, 4)*(x1 + 4), 4) == Mod(x1*(x1 + 2), 4)
  1552. assert Mod(Mod(x1 + 2, 4)*4, 4) == 0
  1553. # issue 15493
  1554. i, j = symbols('i j', integer=True, positive=True)
  1555. assert Mod(3*i, 2) == Mod(i, 2)
  1556. assert Mod(8*i/j, 4) == 4*Mod(2*i/j, 1)
  1557. assert Mod(8*i, 4) == 0
  1558. # rewrite
  1559. assert Mod(x, y).rewrite(floor) == x - y*floor(x/y)
  1560. assert ((x - Mod(x, y))/y).rewrite(floor) == floor(x/y)
  1561. # issue 21373
  1562. from sympy.functions.elementary.hyperbolic import sinh
  1563. from sympy.functions.elementary.piecewise import Piecewise
  1564. x_r, y_r = symbols('x_r y_r', real=True)
  1565. assert (Piecewise((x_r, y_r > x_r), (y_r, True)) / z) % 1
  1566. expr = exp(sinh(Piecewise((x_r, y_r > x_r), (y_r, True)) / z))
  1567. expr.subs({1: 1.0})
  1568. sinh(Piecewise((x_r, y_r > x_r), (y_r, True)) * z ** -1.0).is_zero
  1569. # issue 24215
  1570. from sympy.abc import phi
  1571. assert Mod(4.0*Mod(phi, 1) , 2) == 2.0*(Mod(2*(Mod(phi, 1)), 1))
  1572. def test_Mod_Pow():
  1573. # modular exponentiation
  1574. assert isinstance(Mod(Pow(2, 2, evaluate=False), 3), Integer)
  1575. assert Mod(Pow(4, 13, evaluate=False), 497) == Mod(Pow(4, 13), 497)
  1576. assert Mod(Pow(2, 10000000000, evaluate=False), 3) == 1
  1577. assert Mod(Pow(32131231232, 9**10**6, evaluate=False),10**12) == \
  1578. pow(32131231232,9**10**6,10**12)
  1579. assert Mod(Pow(33284959323, 123**999, evaluate=False),11**13) == \
  1580. pow(33284959323,123**999,11**13)
  1581. assert Mod(Pow(78789849597, 333**555, evaluate=False),12**9) == \
  1582. pow(78789849597,333**555,12**9)
  1583. # modular nested exponentiation
  1584. expr = Pow(2, 2, evaluate=False)
  1585. expr = Pow(2, expr, evaluate=False)
  1586. assert Mod(expr, 3**10) == 16
  1587. expr = Pow(2, expr, evaluate=False)
  1588. assert Mod(expr, 3**10) == 6487
  1589. expr = Pow(2, expr, evaluate=False)
  1590. assert Mod(expr, 3**10) == 32191
  1591. expr = Pow(2, expr, evaluate=False)
  1592. assert Mod(expr, 3**10) == 18016
  1593. expr = Pow(2, expr, evaluate=False)
  1594. assert Mod(expr, 3**10) == 5137
  1595. expr = Pow(2, 2, evaluate=False)
  1596. expr = Pow(expr, 2, evaluate=False)
  1597. assert Mod(expr, 3**10) == 16
  1598. expr = Pow(expr, 2, evaluate=False)
  1599. assert Mod(expr, 3**10) == 256
  1600. expr = Pow(expr, 2, evaluate=False)
  1601. assert Mod(expr, 3**10) == 6487
  1602. expr = Pow(expr, 2, evaluate=False)
  1603. assert Mod(expr, 3**10) == 38281
  1604. expr = Pow(expr, 2, evaluate=False)
  1605. assert Mod(expr, 3**10) == 15928
  1606. expr = Pow(2, 2, evaluate=False)
  1607. expr = Pow(expr, expr, evaluate=False)
  1608. assert Mod(expr, 3**10) == 256
  1609. expr = Pow(expr, expr, evaluate=False)
  1610. assert Mod(expr, 3**10) == 9229
  1611. expr = Pow(expr, expr, evaluate=False)
  1612. assert Mod(expr, 3**10) == 25708
  1613. expr = Pow(expr, expr, evaluate=False)
  1614. assert Mod(expr, 3**10) == 26608
  1615. expr = Pow(expr, expr, evaluate=False)
  1616. # XXX This used to fail in a nondeterministic way because of overflow
  1617. # error.
  1618. assert Mod(expr, 3**10) == 1966
  1619. def test_Mod_is_integer():
  1620. p = Symbol('p', integer=True)
  1621. q1 = Symbol('q1', integer=True)
  1622. q2 = Symbol('q2', integer=True, nonzero=True)
  1623. assert Mod(x, y).is_integer is None
  1624. assert Mod(p, q1).is_integer is None
  1625. assert Mod(x, q2).is_integer is None
  1626. assert Mod(p, q2).is_integer
  1627. def test_Mod_is_nonposneg():
  1628. n = Symbol('n', integer=True)
  1629. k = Symbol('k', integer=True, positive=True)
  1630. assert (n%3).is_nonnegative
  1631. assert Mod(n, -3).is_nonpositive
  1632. assert Mod(n, k).is_nonnegative
  1633. assert Mod(n, -k).is_nonpositive
  1634. assert Mod(k, n).is_nonnegative is None
  1635. def test_issue_6001():
  1636. A = Symbol("A", commutative=False)
  1637. eq = A + A**2
  1638. # it doesn't matter whether it's True or False; they should
  1639. # just all be the same
  1640. assert (
  1641. eq.is_commutative ==
  1642. (eq + 1).is_commutative ==
  1643. (A + 1).is_commutative)
  1644. B = Symbol("B", commutative=False)
  1645. # Although commutative terms could cancel we return True
  1646. # meaning "there are non-commutative symbols; aftersubstitution
  1647. # that definition can change, e.g. (A*B).subs(B,A**-1) -> 1
  1648. assert (sqrt(2)*A).is_commutative is False
  1649. assert (sqrt(2)*A*B).is_commutative is False
  1650. def test_polar():
  1651. from sympy.functions.elementary.complexes import polar_lift
  1652. p = Symbol('p', polar=True)
  1653. x = Symbol('x')
  1654. assert p.is_polar
  1655. assert x.is_polar is None
  1656. assert S.One.is_polar is None
  1657. assert (p**x).is_polar is True
  1658. assert (x**p).is_polar is None
  1659. assert ((2*p)**x).is_polar is True
  1660. assert (2*p).is_polar is True
  1661. assert (-2*p).is_polar is not True
  1662. assert (polar_lift(-2)*p).is_polar is True
  1663. q = Symbol('q', polar=True)
  1664. assert (p*q)**2 == p**2 * q**2
  1665. assert (2*q)**2 == 4 * q**2
  1666. assert ((p*q)**x).expand() == p**x * q**x
  1667. def test_issue_6040():
  1668. a, b = Pow(1, 2, evaluate=False), S.One
  1669. assert a != b
  1670. assert b != a
  1671. assert not (a == b)
  1672. assert not (b == a)
  1673. def test_issue_6082():
  1674. # Comparison is symmetric
  1675. assert Basic.compare(Max(x, 1), Max(x, 2)) == \
  1676. - Basic.compare(Max(x, 2), Max(x, 1))
  1677. # Equal expressions compare equal
  1678. assert Basic.compare(Max(x, 1), Max(x, 1)) == 0
  1679. # Basic subtypes (such as Max) compare different than standard types
  1680. assert Basic.compare(Max(1, x), frozenset((1, x))) != 0
  1681. def test_issue_6077():
  1682. assert x**2.0/x == x**1.0
  1683. assert x/x**2.0 == x**-1.0
  1684. assert x*x**2.0 == x**3.0
  1685. assert x**1.5*x**2.5 == x**4.0
  1686. assert 2**(2.0*x)/2**x == 2**(1.0*x)
  1687. assert 2**x/2**(2.0*x) == 2**(-1.0*x)
  1688. assert 2**x*2**(2.0*x) == 2**(3.0*x)
  1689. assert 2**(1.5*x)*2**(2.5*x) == 2**(4.0*x)
  1690. def test_mul_flatten_oo():
  1691. p = symbols('p', positive=True)
  1692. n, m = symbols('n,m', negative=True)
  1693. x_im = symbols('x_im', imaginary=True)
  1694. assert n*oo is -oo
  1695. assert n*m*oo is oo
  1696. assert p*oo is oo
  1697. assert x_im*oo != I*oo # i could be +/- 3*I -> +/-oo
  1698. def test_add_flatten():
  1699. # see https://github.com/sympy/sympy/issues/2633#issuecomment-29545524
  1700. a = oo + I*oo
  1701. b = oo - I*oo
  1702. assert a + b is nan
  1703. assert a - b is nan
  1704. # FIXME: This evaluates as:
  1705. # >>> 1/a
  1706. # 0*(oo + oo*I)
  1707. # which should not simplify to 0. Should be fixed in Pow.eval
  1708. #assert (1/a).simplify() == (1/b).simplify() == 0
  1709. a = Pow(2, 3, evaluate=False)
  1710. assert a + a == 16
  1711. def test_issue_5160_6087_6089_6090():
  1712. # issue 6087
  1713. assert ((-2*x*y**y)**3.2).n(2) == (2**3.2*(-x*y**y)**3.2).n(2)
  1714. # issue 6089
  1715. A, B, C = symbols('A,B,C', commutative=False)
  1716. assert (2.*B*C)**3 == 8.0*(B*C)**3
  1717. assert (-2.*B*C)**3 == -8.0*(B*C)**3
  1718. assert (-2*B*C)**2 == 4*(B*C)**2
  1719. # issue 5160
  1720. assert sqrt(-1.0*x) == 1.0*sqrt(-x)
  1721. assert sqrt(1.0*x) == 1.0*sqrt(x)
  1722. # issue 6090
  1723. assert (-2*x*y*A*B)**2 == 4*x**2*y**2*(A*B)**2
  1724. def test_float_int_round():
  1725. assert int(float(sqrt(10))) == int(sqrt(10))
  1726. assert int(pi**1000) % 10 == 2
  1727. assert int(Float('1.123456789012345678901234567890e20', '')) == \
  1728. int(112345678901234567890)
  1729. assert int(Float('1.123456789012345678901234567890e25', '')) == \
  1730. int(11234567890123456789012345)
  1731. # decimal forces float so it's not an exact integer ending in 000000
  1732. assert int(Float('1.123456789012345678901234567890e35', '')) == \
  1733. 112345678901234567890123456789000192
  1734. assert int(Float('123456789012345678901234567890e5', '')) == \
  1735. 12345678901234567890123456789000000
  1736. assert Integer(Float('1.123456789012345678901234567890e20', '')) == \
  1737. 112345678901234567890
  1738. assert Integer(Float('1.123456789012345678901234567890e25', '')) == \
  1739. 11234567890123456789012345
  1740. # decimal forces float so it's not an exact integer ending in 000000
  1741. assert Integer(Float('1.123456789012345678901234567890e35', '')) == \
  1742. 112345678901234567890123456789000192
  1743. assert Integer(Float('123456789012345678901234567890e5', '')) == \
  1744. 12345678901234567890123456789000000
  1745. assert same_and_same_prec(Float('123000e-2',''), Float('1230.00', ''))
  1746. assert same_and_same_prec(Float('123000e2',''), Float('12300000', ''))
  1747. assert int(1 + Rational('.9999999999999999999999999')) == 1
  1748. assert int(pi/1e20) == 0
  1749. assert int(1 + pi/1e20) == 1
  1750. assert int(Add(1.2, -2, evaluate=False)) == int(1.2 - 2)
  1751. assert int(Add(1.2, +2, evaluate=False)) == int(1.2 + 2)
  1752. assert int(Add(1 + Float('.99999999999999999', ''), evaluate=False)) == 1
  1753. raises(TypeError, lambda: float(x))
  1754. raises(TypeError, lambda: float(sqrt(-1)))
  1755. assert int(12345678901234567890 + cos(1)**2 + sin(1)**2) == \
  1756. 12345678901234567891
  1757. def test_issue_6611a():
  1758. assert Mul.flatten([3**Rational(1, 3),
  1759. Pow(-Rational(1, 9), Rational(2, 3), evaluate=False)]) == \
  1760. ([Rational(1, 3), (-1)**Rational(2, 3)], [], None)
  1761. def test_denest_add_mul():
  1762. # when working with evaluated expressions make sure they denest
  1763. eq = x + 1
  1764. eq = Add(eq, 2, evaluate=False)
  1765. eq = Add(eq, 2, evaluate=False)
  1766. assert Add(*eq.args) == x + 5
  1767. eq = x*2
  1768. eq = Mul(eq, 2, evaluate=False)
  1769. eq = Mul(eq, 2, evaluate=False)
  1770. assert Mul(*eq.args) == 8*x
  1771. # but don't let them denest unnecessarily
  1772. eq = Mul(-2, x - 2, evaluate=False)
  1773. assert 2*eq == Mul(-4, x - 2, evaluate=False)
  1774. assert -eq == Mul(2, x - 2, evaluate=False)
  1775. def test_mul_coeff():
  1776. # It is important that all Numbers be removed from the seq;
  1777. # This can be tricky when powers combine to produce those numbers
  1778. p = exp(I*pi/3)
  1779. assert p**2*x*p*y*p*x*p**2 == x**2*y
  1780. def test_mul_zero_detection():
  1781. nz = Dummy(real=True, zero=False)
  1782. r = Dummy(extended_real=True)
  1783. c = Dummy(real=False, complex=True)
  1784. c2 = Dummy(real=False, complex=True)
  1785. i = Dummy(imaginary=True)
  1786. e = nz*r*c
  1787. assert e.is_imaginary is None
  1788. assert e.is_extended_real is None
  1789. e = nz*c
  1790. assert e.is_imaginary is None
  1791. assert e.is_extended_real is False
  1792. e = nz*i*c
  1793. assert e.is_imaginary is False
  1794. assert e.is_extended_real is None
  1795. # check for more than one complex; it is important to use
  1796. # uniquely named Symbols to ensure that two factors appear
  1797. # e.g. if the symbols have the same name they just become
  1798. # a single factor, a power.
  1799. e = nz*i*c*c2
  1800. assert e.is_imaginary is None
  1801. assert e.is_extended_real is None
  1802. # _eval_is_extended_real and _eval_is_zero both employ trapping of the
  1803. # zero value so args should be tested in both directions and
  1804. # TO AVOID GETTING THE CACHED RESULT, Dummy MUST BE USED
  1805. # real is unknown
  1806. def test(z, b, e):
  1807. if z.is_zero and b.is_finite:
  1808. assert e.is_extended_real and e.is_zero
  1809. else:
  1810. assert e.is_extended_real is None
  1811. if b.is_finite:
  1812. if z.is_zero:
  1813. assert e.is_zero
  1814. else:
  1815. assert e.is_zero is None
  1816. elif b.is_finite is False:
  1817. if z.is_zero is None:
  1818. assert e.is_zero is None
  1819. else:
  1820. assert e.is_zero is False
  1821. for iz, ib in product(*[[True, False, None]]*2):
  1822. z = Dummy('z', nonzero=iz)
  1823. b = Dummy('f', finite=ib)
  1824. e = Mul(z, b, evaluate=False)
  1825. test(z, b, e)
  1826. z = Dummy('nz', nonzero=iz)
  1827. b = Dummy('f', finite=ib)
  1828. e = Mul(b, z, evaluate=False)
  1829. test(z, b, e)
  1830. # real is True
  1831. def test(z, b, e):
  1832. if z.is_zero and not b.is_finite:
  1833. assert e.is_extended_real is None
  1834. else:
  1835. assert e.is_extended_real is True
  1836. for iz, ib in product(*[[True, False, None]]*2):
  1837. z = Dummy('z', nonzero=iz, extended_real=True)
  1838. b = Dummy('b', finite=ib, extended_real=True)
  1839. e = Mul(z, b, evaluate=False)
  1840. test(z, b, e)
  1841. z = Dummy('z', nonzero=iz, extended_real=True)
  1842. b = Dummy('b', finite=ib, extended_real=True)
  1843. e = Mul(b, z, evaluate=False)
  1844. test(z, b, e)
  1845. def test_Mul_with_zero_infinite():
  1846. zer = Dummy(zero=True)
  1847. inf = Dummy(finite=False)
  1848. e = Mul(zer, inf, evaluate=False)
  1849. assert e.is_extended_positive is None
  1850. assert e.is_hermitian is None
  1851. e = Mul(inf, zer, evaluate=False)
  1852. assert e.is_extended_positive is None
  1853. assert e.is_hermitian is None
  1854. def test_Mul_does_not_cancel_infinities():
  1855. a, b = symbols('a b')
  1856. assert ((zoo + 3*a)/(3*a + zoo)) is nan
  1857. assert ((b - oo)/(b - oo)) is nan
  1858. # issue 13904
  1859. expr = (1/(a+b) + 1/(a-b))/(1/(a+b) - 1/(a-b))
  1860. assert expr.subs(b, a) is nan
  1861. def test_Mul_does_not_distribute_infinity():
  1862. a, b = symbols('a b')
  1863. assert ((1 + I)*oo).is_Mul
  1864. assert ((a + b)*(-oo)).is_Mul
  1865. assert ((a + 1)*zoo).is_Mul
  1866. assert ((1 + I)*oo).is_finite is False
  1867. z = (1 + I)*oo
  1868. assert ((1 - I)*z).expand() is oo
  1869. def test_issue_8247_8354():
  1870. from sympy.functions.elementary.trigonometric import tan
  1871. z = sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) - sqrt(10 + 6*sqrt(3))
  1872. assert z.is_positive is False # it's 0
  1873. z = S('''-2**(1/3)*(3*sqrt(93) + 29)**2 - 4*(3*sqrt(93) + 29)**(4/3) +
  1874. 12*sqrt(93)*(3*sqrt(93) + 29)**(1/3) + 116*(3*sqrt(93) + 29)**(1/3) +
  1875. 174*2**(1/3)*sqrt(93) + 1678*2**(1/3)''')
  1876. assert z.is_positive is False # it's 0
  1877. z = 2*(-3*tan(19*pi/90) + sqrt(3))*cos(11*pi/90)*cos(19*pi/90) - \
  1878. sqrt(3)*(-3 + 4*cos(19*pi/90)**2)
  1879. assert z.is_positive is not True # it's zero and it shouldn't hang
  1880. z = S('''9*(3*sqrt(93) + 29)**(2/3)*((3*sqrt(93) +
  1881. 29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) - 2) - 2*2**(1/3))**3 +
  1882. 72*(3*sqrt(93) + 29)**(2/3)*(81*sqrt(93) + 783) + (162*sqrt(93) +
  1883. 1566)*((3*sqrt(93) + 29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) -
  1884. 2) - 2*2**(1/3))**2''')
  1885. assert z.is_positive is False # it's 0 (and a single _mexpand isn't enough)
  1886. def test_Add_is_zero():
  1887. x, y = symbols('x y', zero=True)
  1888. assert (x + y).is_zero
  1889. # Issue 15873
  1890. e = -2*I + (1 + I)**2
  1891. assert e.is_zero is None
  1892. def test_issue_14392():
  1893. assert (sin(zoo)**2).as_real_imag() == (nan, nan)
  1894. def test_divmod():
  1895. assert divmod(x, y) == (x//y, x % y)
  1896. assert divmod(x, 3) == (x//3, x % 3)
  1897. assert divmod(3, x) == (3//x, 3 % x)
  1898. def test__neg__():
  1899. assert -(x*y) == -x*y
  1900. assert -(-x*y) == x*y
  1901. assert -(1.*x) == -1.*x
  1902. assert -(-1.*x) == 1.*x
  1903. assert -(2.*x) == -2.*x
  1904. assert -(-2.*x) == 2.*x
  1905. with distribute(False):
  1906. eq = -(x + y)
  1907. assert eq.is_Mul and eq.args == (-1, x + y)
  1908. with evaluate(False):
  1909. eq = -(x + y)
  1910. assert eq.is_Mul and eq.args == (-1, x + y)
  1911. def test_issue_18507():
  1912. assert Mul(zoo, zoo, 0) is nan
  1913. def test_issue_17130():
  1914. e = Add(b, -b, I, -I, evaluate=False)
  1915. assert e.is_zero is None # ideally this would be True
  1916. def test_issue_21034():
  1917. e = -I*log((re(asin(5)) + I*im(asin(5)))/sqrt(re(asin(5))**2 + im(asin(5))**2))/pi
  1918. assert e.round(2)
  1919. def test_issue_22021():
  1920. from sympy.calculus.accumulationbounds import AccumBounds
  1921. # these objects are special cases in Mul
  1922. from sympy.tensor.tensor import TensorIndexType, tensor_indices, tensor_heads
  1923. L = TensorIndexType("L")
  1924. i = tensor_indices("i", L)
  1925. A, B = tensor_heads("A B", [L])
  1926. e = A(i) + B(i)
  1927. assert -e == -1*e
  1928. e = zoo + x
  1929. assert -e == -1*e
  1930. a = AccumBounds(1, 2)
  1931. e = a + x
  1932. assert -e == -1*e
  1933. for args in permutations((zoo, a, x)):
  1934. e = Add(*args, evaluate=False)
  1935. assert -e == -1*e
  1936. assert 2*Add(1, x, x, evaluate=False) == 4*x + 2
  1937. def test_issue_22244():
  1938. assert -(zoo*x) == zoo*x
  1939. def test_issue_22453():
  1940. from sympy.utilities.iterables import cartes
  1941. e = Symbol('e', extended_positive=True)
  1942. for a, b in cartes(*[[oo, -oo, 3]]*2):
  1943. if a == b == 3:
  1944. continue
  1945. i = a + I*b
  1946. assert i**(1 + e) is S.ComplexInfinity
  1947. assert i**-e is S.Zero
  1948. assert unchanged(Pow, i, e)
  1949. assert 1/(oo + I*oo) is S.Zero
  1950. r, i = [Dummy(infinite=True, extended_real=True) for _ in range(2)]
  1951. assert 1/(r + I*i) is S.Zero
  1952. assert 1/(3 + I*i) is S.Zero
  1953. assert 1/(r + I*3) is S.Zero
  1954. def test_issue_22613():
  1955. assert (0**(x - 2)).as_content_primitive() == (1, 0**(x - 2))
  1956. assert (0**(x + 2)).as_content_primitive() == (1, 0**(x + 2))