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