From 3832bf5cd17b456a28d816bee060e3d500b1f07b Mon Sep 17 00:00:00 2001 From: Michael Schneeberger Date: Tue, 6 Dec 2022 13:08:25 +0100 Subject: remove polymatrix entry when two entries add up to zero --- polymatrix/expression/mixins/additionexprmixin.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/polymatrix/expression/mixins/additionexprmixin.py b/polymatrix/expression/mixins/additionexprmixin.py index c4f3113..20ff5d0 100644 --- a/polymatrix/expression/mixins/additionexprmixin.py +++ b/polymatrix/expression/mixins/additionexprmixin.py @@ -1,6 +1,7 @@ import abc import collections +import math import typing import dataclass_abc @@ -83,7 +84,11 @@ class AdditionExprMixin(ExpressionBaseMixin): else: terms_row_col[monomial] += value - terms[row, col] = terms_row_col + if math.isclose(terms_row_col[monomial], 0): + del terms_row_col[monomial] + + if 0 < len(terms_row_col): + terms[row, col] = terms_row_col poly_matrix = init_poly_matrix( terms=terms, -- cgit v1.2.1