Skip to content

Negate a contracted Interpolate with free arguments as a form - #550

Merged
jorgensd merged 2 commits into
mainfrom
pbrubeck/fix/interpolate-form-negation
Nov 5, 2026
Merged

jorgensd merged 2 commits into
mainfrom
pbrubeck/fix/interpolate-form-negation

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Let d = Interpolate(v, cofunction), where v is a test function. This is a 1-form, but _value_parent_type follows the target space, which is primal, so -d was a Product, and for a Form F, F - d raised TypeError: unsupported operand type(s) for +: 'Form' and 'Product'.

 def _value_parent_type(self):
+    dual_slot_arguments = self.argument_slots()[0].arguments()
+    if self.arguments() and not any(isinstance(a, Coargument) for a in dual_slot_arguments):
+        return BaseForm
     function_space = self._function_space
Expression Type of its negation before After
Interpolate(u, V) Product Product
Interpolate(v, V) Product Product
Interpolate(u, 2 * coargument) Product Product
Interpolate(u, cofunction) Product Product
Interpolate(v, cofunction) Product FormSum
Interpolate(u, form) Product FormSum

Evidence

  • Before: the new test_negation_of_contracted_interpolation_with_arguments fails, and test_negation_of_interpolation_with_scaled_coargument guards the dual slots that must stay expressions. In Irksome, all of tests/test_delta.py fails on UFL main with the TypeError above (or, for test_delta, an unrelated extract_domains assertion that I relaxed locally for this run).
    After: the UFL test suite passes on main (1069). With the patch applied to 2026.2.0, everything passes except test_negation_of_interpolation_with_scaled_coargument, which also needs a Coargument import there and then depends on PYTHONHASHSEED: 2026.2.0 takes its target space from v.arguments()[0], which Fix dual-slot form argument counting #527 replaced. A backport should leave that test out. Irksome's tests/test_delta.py passes 10/10 on both, with that assertion relaxed.
  • Firedrake: the interpolate, vertex-only and fml tests match the baseline on UFL main, where 3 test_zeroform[False-*] tests fail with and without this change. They all pass (553) with the patch applied to 2026.2.0. These runs are serial.

The full Firedrake suite, FFCx and DOLFINx were not run.

Merge Danger

Door: two-way

Blast Radius: interpolate-arithmetic

Only Interpolate objects with a contracted dual slot and free arguments change. For these, 2 * d is now a FormSum, and d * 2 and d * Constant(...) now raise a TypeError, matching Form for the scalar case. d / 2 is still a Division.

This regression shipped in 2026.2.0, so it may also be worth backporting.

🤖 Generated with Claude Code

pbrubeck and others added 2 commits November 4, 2026 14:56
Interpolate(v, cofunction) is a 1-form, but _value_parent_type follows the
target space, which is primal, so negating or scaling it gave an expression.
Subtracting it from a Form then raised a TypeError.

Return BaseForm when the dual slot contracts the value and free arguments
remain. Interpolate(v, V) and the 0-form Interpolate(u, cofunction) stay
expressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 * Coargument and sums of Coarguments are FormSums, so checking the type of
the dual slot treated their output as contracted, and negation gave a
FormSum. Check for a Coargument among the arguments of the dual slot instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jorgensd

jorgensd commented Nov 5, 2026

Copy link
Copy Markdown
Member

This looks good to me. It doesn't interfere with any of the FFCx/DOLFINx developments, as we haven't planned how to do interpolate(v, V) outside of forms at the moment. Only minor suggestions (see slack).

@jorgensd
jorgensd added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit 1b8d1e2 Nov 5, 2026
21 checks passed
@jorgensd
jorgensd deleted the pbrubeck/fix/interpolate-form-negation branch November 5, 2026 12:25
pbrubeck added a commit that referenced this pull request Nov 25, 2026
It tests the target space chosen for a FormSum dual slot, which #527 fixed
and release does not have. It fails on release with or without #550.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jorgensd pushed a commit that referenced this pull request Nov 25, 2026
…574)

* Negate a contracted Interpolate with free arguments as a form (#550)

* Negate a contracted Interpolate with free arguments as a form

Interpolate(v, cofunction) is a 1-form, but _value_parent_type follows the
target space, which is primal, so negating or scaling it gave an expression.
Subtracting it from a Form then raised a TypeError.

Return BaseForm when the dual slot contracts the value and free arguments
remain. Interpolate(v, V) and the 0-form Interpolate(u, cofunction) stay
expressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Treat a scaled Coargument dual slot as a free output

2 * Coargument and sums of Coarguments are FormSums, so checking the type of
the dual slot treated their output as contracted, and negation gave a
FormSum. Check for a Coargument among the arguments of the dual slot instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 1b8d1e2)

* Keep release interpolation target selection in the backport

* Fix dual-slot form argument counting (#527)

* Fix dual-slot form argument counting

* Compare the stored function space in Interpolate.__eq__

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Fix dual-slot form argument handling

* Remove outer_form argument plumbing

* Materialize dual-slot arguments

* Annotate delayed imports

* Contract the highest-numbered argument of a form dual slot

A form dual slot now contracts its highest-numbered primal argument, as
the left operand of an Action does, so Interpolate(u, F) numbers its
free arguments like Action(F, Interpolate(u, V)), starting at 0.
Coarguments in the slot stay free.

The stored slot is renumbered once, at construction, and argument
analysis reads it back. BaseFormOperatorDerivative rejects duplicate
argument numbers, and apply_derivatives numbers the direction after the
highest operator argument instead of after the argument count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Simplify the contracted argument renumbering

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Test that dual-slot contraction ignores numbering gaps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep arguments contracted inside a dual slot when renumbering

Renumbering the slot's own arguments with replace could rename one onto an
argument that an Action inside the slot contracts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Contract a form dual slot as an Action does

Renumbering the free arguments in the constructor rewrote the user's form
with replace, which needed _contained_arguments to avoid arguments that inner
Actions contract. That traversal was exponential in the depth of shared
subexpressions.

The free arguments now keep their numbers, and N(u; F) contracts the argument
that an Action would. If the primal slots use number 0, the output of N is
numbered last, so N is the left operand and F's lowest argument is contracted.
Otherwise F is the left operand and its highest argument is contracted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit 6a89eab)

* Revert "Fix dual-slot form argument counting (#527)"

This reverts commit ca2a2a7.

* Drop the scaled-Coargument regression from the backport

It tests the target space chosen for a FormSum dual slot, which #527 fixed
and release does not have. It fails on release with or without #550.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Alias datetime as dt in the docs configuration

Ruff 0.17 flags a plain datetime import. main made the same change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants