Skip to content

Commit

Permalink
Merge branch 'main' into thg_subst_applier
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener authored Jun 1, 2022
2 parents 4d2c7e4 + 853eeac commit 9ab7f47
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pymbolic/geometric_algebra/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ def map_product(self, expr):

result = new_result

from pymbolic.primitives import flattened_sum
from pymbolic.primitives import flattened_product, flattened_sum
return flattened_sum([
type(expr)(tuple([
flattened_product([
self.rec(prod_term) for prod_term in prod_term_list
]))
])
for prod_term_list in result
])

Expand Down
5 changes: 5 additions & 0 deletions pymbolic/mapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,11 @@ def __init__(self):
super().__init__()
self.result_cache = {}

from warnings import warn
warn("CachingMapperMixin is deprecated and will be removed "
"in version 2023.x. Use CachedMapper instead.",
DeprecationWarning, stacklevel=2)

def rec(self, expr):
try:
return self.result_cache[expr]
Expand Down
30 changes: 22 additions & 8 deletions pymbolic/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
.. autofunction:: is_zero
.. autofunction:: is_constant
.. autofunction:: flattened_sum
.. autofunction:: flattened_product
.. autofunction:: register_constant_class
.. autofunction:: unregister_constant_class
.. autofunction:: variables
Expand All @@ -159,7 +161,6 @@
.. autofunction:: make_sym_vector
.. autofunction:: make_sym_array
Constants
---------
Expand Down Expand Up @@ -1609,9 +1610,14 @@ def subscript(expression, index):
return Subscript(expression, index)


def flattened_sum(components):
# flatten any potential sub-sums
queue = list(components)
def flattened_sum(terms):
r"""Recursively flattens all the top level :class:`Sum`\ s in *terms*.
:arg terms: an :class:`~collections.abc.Iterable` of expressions.
:returns: a :class:`Sum` expression or, if there is only one term in
the sum, the respective term.
"""
queue = list(terms)
done = []

while queue:
Expand Down Expand Up @@ -1639,17 +1645,25 @@ def linear_combination(coefficients, expressions):
if coefficient and expression)


def flattened_product(components):
# flatten any potential sub-products
queue = list(components)
def flattened_product(terms):
r"""Recursively flattens all the top level :class:`Product`\ s in *terms*.
This operation does not change the order of the terms in the products, so
it does not require the product to be commutative.
:arg terms: an :class:`~collections.abc.Iterable` of expressions.
:returns: a :class:`Product` expression or, if there is only one term in
the product, the respective term.
"""
queue = list(terms)
done = []

while queue:
item = queue.pop(0)

if is_zero(item):
return 0
if is_zero(item-1):
if is_zero(item - 1):
continue

if isinstance(item, Product):
Expand Down

0 comments on commit 9ab7f47

Please sign in to comment.