From a734e61f050300842e8e6f524de67ed591ba1f7c Mon Sep 17 00:00:00 2001 From: Isuru Fernando <isuruf@gmail.com> Date: Fri, 10 Jul 2020 11:52:27 -0500 Subject: [PATCH] Use a frozen dictionary --- .test-conda-env-py3.yml | 1 + requirements.txt | 1 + sumpy/expansion/__init__.py | 2 +- sumpy/expansion/diff_op.py | 37 +++++++++++++++++++------------------ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/.test-conda-env-py3.yml b/.test-conda-env-py3.yml index f9ede8fc..eb7c3ddb 100644 --- a/.test-conda-env-py3.yml +++ b/.test-conda-env-py3.yml @@ -14,6 +14,7 @@ dependencies: - python=3 - python-symengine=0.6.0 - pyfmmlib +- pyrsistent - pip - pip: diff --git a/requirements.txt b/requirements.txt index efe91f94..072cc9ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ numpy sympy==1.1.1 +pyrsistent git+https://github.com/inducer/pytools git+https://github.com/inducer/pymbolic git+https://github.com/inducer/islpy diff --git a/sumpy/expansion/__init__.py b/sumpy/expansion/__init__.py index fb19822b..60647aac 100644 --- a/sumpy/expansion/__init__.py +++ b/sumpy/expansion/__init__.py @@ -354,7 +354,7 @@ class LinearPDEBasedExpansionTermsWrangler(ExpansionTermsWrangler): coeff_ident_enumerate_dict = {tuple(mi): i for (i, mi) in enumerate(mis)} - pde_dict = self.get_pde_as_diff_op().eq + pde_dict = self.get_pde_as_diff_op().mi_to_coeff for ident in pde_dict.keys(): if ident not in coeff_ident_enumerate_dict: # Order of the expansion is less than the order of the PDE. diff --git a/sumpy/expansion/diff_op.py b/sumpy/expansion/diff_op.py index 40dae7a4..73e85320 100644 --- a/sumpy/expansion/diff_op.py +++ b/sumpy/expansion/diff_op.py @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from pyrsistent import pmap from sumpy.tools import add_mi __doc__ = """ @@ -35,35 +36,35 @@ Differential operator interface class DifferentialOperator(object): r""" Represents a scalar, constant-coefficient DifferentialOperator of - dimension `dim`. It is represented by a dictionary. The dictionary - maps a multi-index given as a tuple to the coefficient. + dimension `dim`. It is represented by a frozen dictionary. + The dictionary maps a multi-index given as a tuple to the coefficient. This object is immutable. """ - def __init__(self, dim, eq): + def __init__(self, dim, mi_to_coeff): """ :arg dim: dimension of the DifferentialOperator - :arg eq: A dictionary mapping a multi-index to a coefficient + :arg mi_to_coeff: A dictionary mapping a multi-index to a coefficient """ self.dim = dim - self.eq = eq + self.mi_to_coeff = mi_to_coeff def __mul__(self, param): - eq = {} - for k, v in self.eq.items(): - eq[k] = v * param - return DifferentialOperator(self.dim, eq) + mi_to_coeff = {} + for k, v in self.mi_to_coeff.items(): + mi_to_coeff[k] = v * param + return DifferentialOperator(self.dim, pmap(mi_to_coeff)) __rmul__ = __mul__ def __add__(self, other_pde): assert self.dim == other_pde.dim - res = self.eq.copy() - for k, v in other_pde.eq.items(): + res = dict(self.mi_to_coeff) + for k, v in other_pde.mi_to_coeff.items(): if k in res: res[k] += v else: res[k] = v - return DifferentialOperator(self.dim, res) + return DifferentialOperator(self.dim, pmap(res)) __radd__ = __add__ @@ -71,12 +72,12 @@ class DifferentialOperator(object): return self + (-1)*other_pde def __repr__(self): - return f"DifferentialOperator({self.dim}, {repr(self.eq)})" + return f"DifferentialOperator({self.dim}, {repr(self.mi_to_coeff)})" def laplacian(pde): dim = pde.dim - res = DifferentialOperator(dim, {}) + res = DifferentialOperator(dim, pmap()) for j in range(dim): mi = [0]*dim mi[j] = 2 @@ -86,9 +87,9 @@ def laplacian(pde): def diff(pde, mi): res = {} - for eq_mi, v in pde.eq.items(): - res[add_mi(eq_mi, mi)] = v - return DifferentialOperator(pde.dim, res) + for mi_to_coeff_mi, v in pde.mi_to_coeff.items(): + res[add_mi(mi_to_coeff_mi, mi)] = v + return DifferentialOperator(pde.dim, pmap(res)) def make_identity_diff_op(dim): @@ -96,4 +97,4 @@ def make_identity_diff_op(dim): Returns the identity as a differential operator. """ mi = tuple([0]*dim) - return DifferentialOperator(dim, {mi: 1}) + return DifferentialOperator(dim, pmap({mi: 1})) -- GitLab