Skip to content

Commit

Permalink
Fix FormSum weights
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Dec 21, 2024
1 parent eabdb00 commit 7643192
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions ufl/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,21 @@ def __new__(cls, *args, **kw):
if isinstance(right, (Coargument, Argument)):
return left

if isinstance(left, (FormSum, Sum)):
# Action distributes over sums on the LHS
return FormSum(*[(Action(component, right), 1) for component in left.ufl_operands])
if isinstance(right, (FormSum, Sum)):
# Action also distributes over sums on the RHS
return FormSum(*[(Action(left, component), 1) for component in right.ufl_operands])
# Action distributes over sums on the LHS
if isinstance(left, Sum):
return FormSum(*((Action(component, right), 1) for component in left.ufl_operands))
elif isinstance(left, FormSum):
return FormSum(
*((Action(c, right), w) for c, w in zip(left.components(), left.weights()))
)

# Action also distributes over sums on the RHS
if isinstance(right, Sum):
return FormSum(*((Action(left, component), 1) for component in right.ufl_operands))
elif isinstance(right, FormSum):
return FormSum(
*((Action(left, c), w) for c, w in zip(right.components(), right.weights()))
)

return super(Action, cls).__new__(cls)

Expand Down
2 changes: 1 addition & 1 deletion ufl/adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __new__(cls, *args, **kw):
return form._form
elif isinstance(form, FormSum):
# Adjoint distributes over sums
return FormSum(*[(Adjoint(component), 1) for component in form.components()])
return FormSum(*((Adjoint(c), w) for c, w in zip(form.components(), form.weights())))
elif isinstance(form, Coargument):
# The adjoint of a coargument `c: V* -> V*` is the identity
# matrix mapping from V to V (i.e. V x V* -> R).
Expand Down
2 changes: 1 addition & 1 deletion ufl/algorithms/map_integrands.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def map_integrands(function, form, only_integral_type=None):
# Simplification of `BaseForm` objects may turn `FormSum` into a sum of `Expr` objects
# that are not `BaseForm`, i.e. into a `Sum` object.
# Example: `Action(Adjoint(c*), u)` with `c*` a `Coargument` and u a `Coefficient`.
return sum([component for component, _ in nonzero_components])
return sum(component * w for component, w in nonzero_components)
return FormSum(*nonzero_components)
elif isinstance(form, Adjoint):
# Zeros are caught inside `Adjoint.__new__`
Expand Down

0 comments on commit 7643192

Please sign in to comment.