Skip to content

Commit

Permalink
[algebra] fix incomplete simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Jahn committed Jan 9, 2019
1 parent 7893441 commit 9e29e4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pySecDec/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,19 @@ def simplify(self, deep=True):
if type(self.coeffs[i]) is Polynomial and len(self.coeffs[i].coeffs) == 1 and (self.coeffs[i].expolist == 0).all():
self.coeffs[i] = self.coeffs[i].coeffs[0]

distance_to_nonzero_previous = 1
for i in range(1,len(self.coeffs)):
if self.coeffs[i] == 0: continue
previous_exponents = self.expolist[i-1]
if self.coeffs[i] == 0:
distance_to_nonzero_previous += 1
continue
# search `self.expolist` for the same term
# since `self.expolist` is sorted, must only compare with the previous term
if (previous_exponents == self.expolist[i]).all():
# since `self.expolist` is sorted, must only compare with the previous nonzero term
if (self.expolist[i-distance_to_nonzero_previous] == self.expolist[i]).all():
# add coefficients
self.coeffs[i] += self.coeffs[i-1]
self.coeffs[i] += self.coeffs[i-distance_to_nonzero_previous]
# mark previous term for removal by setting coefficient to zero
self.coeffs[i-1] = 0
self.coeffs[i-distance_to_nonzero_previous] = 0
distance_to_nonzero_previous = 1

# remove terms with zero coefficient
nonzero_coeffs = np.where(self.coeffs != 0)
Expand Down
7 changes: 7 additions & 0 deletions pySecDec/test_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ def test_simplify(self):
np.testing.assert_array_equal(expr2.expolist, [[2,1]])
np.testing.assert_array_equal(expr2.coeffs, [sp.symbols('A')])

#@attr('active')
def test_simplify_to_zero(self):
zero = Polynomial([[0,0,0]]*11,[-1,0,1,0,0,-2,0,0,0,1,1]).simplify()

np.testing.assert_array_equal(zero.coeffs, [0])
np.testing.assert_array_equal(zero.expolist, [[0,0,0]])

class TestFormerSNCPolynomial(unittest.TestCase):
def setUp(self):
self.p0 = Polynomial([(0,1),(1,0),(1,1)],[1,1,3])
Expand Down

0 comments on commit 9e29e4c

Please sign in to comment.