Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .basedpyright/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -17503,14 +17503,6 @@
"lineCount": 1
}
},
{
"code": "reportIncompatibleMethodOverride",
"range": {
"startColumn": 8,
"endColumn": 21,
"lineCount": 1
}
},
{
"code": "reportImplicitOverride",
"range": {
Expand Down Expand Up @@ -17703,14 +17695,6 @@
"lineCount": 1
}
},
{
"code": "reportIncompatibleMethodOverride",
"range": {
"startColumn": 8,
"endColumn": 21,
"lineCount": 1
}
},
{
"code": "reportImplicitOverride",
"range": {
Expand Down Expand Up @@ -39321,14 +39305,6 @@
"lineCount": 1
}
},
{
"code": "reportAssignmentType",
"range": {
"startColumn": 8,
"endColumn": 19,
"lineCount": 1
}
},
{
"code": "reportUnknownVariableType",
"range": {
Expand Down
51 changes: 26 additions & 25 deletions pytential/symbolic/reduce_fmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,44 +462,45 @@ def map_power(self, expr: prim.Power, /) -> PolyCoeffMapping:

@override
def map_sum(self, expr: prim.Sum, /) -> PolyCoeffMapping:
stride_dicts = [self.rec(ch) for ch in expr.children]

result: PolyCoeffMapping = {}
for stride_dict in stride_dicts:
for var, stride in stride_dict.items():
for child in expr.children:
ch = self.rec(child)
for var, stride in ch.items():
result[var] = result.get(var, 0) + stride

return result

@override
def map_product(self, expr: prim.Product, /) -> PolyCoeffMapping:
if len(expr.children) == 1:
return self.rec(expr.children[0])
children: list[PolyCoeffMapping] = []
for child in expr.children:
child_rec = self.rec(child)
if not child_rec:
return {}

if len(expr.children) == 0:
return {1: 1}
children.append(child_rec)

if len(expr.children) > 2:
# rewrite products of more than two children as a nested
# product and recurse to make it easier to handle.
left = expr.children[0]
right = prim.Product(tuple(expr.children[1:]))
new_prod = prim.Product((left, right))
return self.rec(new_prod)
from itertools import product

left, right = expr.children
d_left = self.rec(left)
d_right = self.rec(right)
result: PolyCoeffMapping = {}
for coeffs in product(*(c.items() for c in children)):
# gather terms
factors: list[ArithmeticExpression] = []
coeff = 1
for var_i, coeff_i in coeffs:
if var_i != 1:
factors.append(var_i)

from pytential.symbolic.mappers import flatten
coeff = coeff * coeff_i

d: PolyCoeffMapping = {}
for var_left, coeff_left in d_left.items():
for var_right, coeff_right in d_right.items():
key = flatten(var_left * var_right)
d[key] = d.get(key, 0) + coeff_left * coeff_right
# normalize the key so that `a * b <=> b * a`
key = prim.flattened_product(factors)
if isinstance(key, prim.Product):
key = prim.Product(tuple(sorted(key.children, key=str)))

return d
result[key] = result.get(key, 0) + coeff

return result

def map_node_coordinate_component(
self, expr: NodeCoordinateComponent
Expand Down
15 changes: 10 additions & 5 deletions test/test_symbolic_reduce_fmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ def test_coefficient_collector() -> None:
terms = collect(a * b + a * c + b * c, [a, b, c])
assert terms == {a * b: 1, a * c: 1, b * c: 1}

# product factor ordering is canonicalized
assert collect(b * a, [a, b]) == {a * b: 1}
assert collect(c * (a * b), [a, b, c]) == {prim.Product((a, b, c)): 1}
assert collect(a * b + b * a, [a, b]) == {a * b: 2}

# powers
assert collect(a**3, [a]) == {a**3: 1}
assert collect(a**n, [a]) == {a**n: 1}
Expand Down Expand Up @@ -626,8 +631,8 @@ def test_coefficient_collector_elasticity_densities() -> None:
# stresslet-style: w / 6 * (sigma[k] * normal[j] + sigma[j] * normal[k])
expr = w / 6 * (q[0] * normal[1] + q[1] * normal[0])
assert collect(expr, basis) == {
q[0] * normal[1]: w / 6,
q[1] * normal[0]: w / 6,
normal[1] * q[0]: w / 6,
normal[0] * q[1]: w / 6,
}

# Tornberg-style: products of normal, density and source coordinates
Expand All @@ -643,12 +648,12 @@ def test_coefficient_collector_elasticity_densities() -> None:
# common-density products: (sum_k source[k] * sigma[k]) * normal[j]
expr = (source[0] * q[0] + source[1] * q[1]) * normal[0]
assert collect(expr, basis) == {
prim.Product((source[0], q[0], normal[0])): 1,
prim.Product((source[1], q[1], normal[0])): 1,
prim.Product((normal[0], q[0], source[0])): 1,
prim.Product((normal[0], q[1], source[1])): 1,
}

# stokeslet-style: w * source[k] * sigma[k]
assert collect(w * source[0] * q[0], basis) == {source[0] * q[0]: w}
assert collect(w * source[0] * q[0], basis) == {q[0] * source[0]: w}

# CSE densities (e.g. `sym.normal` components or Hsiao-Kress' meanless
# sigma) are treated as a single source-dependent expression
Expand Down
Loading