Skip to content

Commit

Permalink
better is_integer for 1/expr
Browse files Browse the repository at this point in the history
  • Loading branch information
smichr committed Oct 10, 2021
1 parent 7d104b6 commit 9bb7873
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sympy/core/mul.py
Expand Up @@ -1339,7 +1339,9 @@ def _eval_is_integer(self):

numerators = []
denominators = []
unknown = False
for a in self.args:
hit = False
if a.is_integer:
if abs(a) is not S.One:
numerators.append(a)
Expand All @@ -1351,10 +1353,11 @@ def _eval_is_integer(self):
denominators.append(d)
elif a.is_Pow:
b, e = a.as_base_exp()
if not b.is_integer or not e.is_integer: return
if not b.is_integer or not e.is_integer:
hit = unknown = True
if e.is_negative:
denominators.append(2 if a is S.Half else Pow(a, S.NegativeOne))
else:
elif not hit:
# for integer b and positive integer e: a = b**e would be integer
assert not e.is_positive
# for self being rational and e equal to zero: a = b**e would be 1
Expand All @@ -1363,14 +1366,19 @@ def _eval_is_integer(self):
else:
return

if not denominators:
if not denominators and not unknown:
return True

allodd = lambda x: all(i.is_odd for i in x)
alleven = lambda x: all(i.is_even for i in x)
anyeven = lambda x: any(i.is_even for i in x)

if allodd(numerators) and anyeven(denominators):
if not numerators and denominators and all((_ - 1).is_extended_positive
for _ in denominators):
return False
elif unknown:
return
elif allodd(numerators) and anyeven(denominators):
return False
elif anyeven(numerators) and denominators == [2]:
return True
Expand Down
1 change: 1 addition & 0 deletions sympy/core/tests/test_arit.py
Expand Up @@ -1056,6 +1056,7 @@ def test_Pow_is_integer():
x = Symbol('x', positive=True)
assert (1/(x + 1)).is_integer is False
assert (1/(-x - 1)).is_integer is False
assert (-1/(x + 1)).is_integer is False

# issue 8648-like
k = Symbol('k', even=True)
Expand Down

0 comments on commit 9bb7873

Please sign in to comment.