functional: lazy functional algebra (mirror LinOp)#60
Merged
Conversation
Add the scalar half of the functional algebra (0.4.2 W4), mirroring linop/_algebra.py: ScaledFunctional (value = a*F.value; grad = X.scale(a, F.grad) in the domain geometry, not raw *), the make_scaled_functional factory (unit passthrough + nested-scalar fold), and Functional.__neg__/__mul__/__rmul__ delegating to it (NotImplemented for non-scalar operands). Exported at package and top level. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add the additive half of the functional algebra (0.4.2 W4): SumFunctional (value = sum of term values; grad = the term grads folded via X.add in the domain geometry, correct on tree/stacked domains), make_functional_sum (flattens nested sums, unwraps a lone term, validates a common domain), and Functional.__add__/__radd__/__sub__/__rsub__ (0 + F enables builtin sum(); NotImplemented for non-Functional operands). Exported at package and top level. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…erage Complete the functional algebra (0.4.2 W4): - ShiftedFunctional (F + c: value shifted, gradient unchanged) and the make_shifted_functional factory (zero-offset passthrough, nested-offset fold). - ZeroFunctional (value 0, grad = X.zeros()) as the additive identity; make_scaled_functional now returns it for a zero scalar and make_functional_sum drops it (empty sum -> ZeroFunctional). - Functional.__add__/__radd__/__sub__/__rsub__ route a scalar operand to the affine shift (the one place functionals extend beyond LinOp). - Register the four algebra nodes in functional_cases() so the registry- completeness check and the generic functional laws (value / gradient / directional-derivative / conversion) exercise them. - Tests: affine + zero behavior, factory zero-handling, and an end-to-end minimize_optax(0.5F + 0.5F) integration. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…dation Adversarial review caught two bugs: - ScaledFunctional.grad/value_and_grad must conjugate a complex scalar: the Riesz gradient of a*F is conj(a)*grad(F) (the domain inner product conjugates its first argument), matching ScaledLinOp.rapply. Value still scales by a; only the metric-gradient side conjugates. Real scalars are unaffected. - make_functional_sum dropped ZeroFunctional terms BEFORE validating domains, so F(X) + Zero(Y) silently returned F. Validate all flattened terms' domains (which fold in the backend/dtype context) before dropping zeros, mirroring linop make_sum. Regression tests: the complex Riesz identity <grad(aF), h> == a<c, h>, and a mismatched-domain Zero term raising instead of being swallowed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Gives
Functionalthe same lazy additive/scalar algebraLinOpalready has (0.4.2 W4), so objectives compose naturally:The key correctness invariant: a functional's
gradis a metric (Riesz) gradient — a domain element — so gradients combine throughX.add/X.scale(not raw+/*), which is correct on tree/stacked/weighted domains.Changes (four gradual commits)
ScaledFunctional(grad = X.scale(a, F.grad)),make_scaled_functional(unit passthrough, nested-scalar fold),__neg__/__mul__/__rmul__.SumFunctional(grad= term grads folded viaX.add),make_functional_sum(flatten nested sums, single-unwrap, shared-domain validation),__add__/__radd__/__sub__/__rsub__(0 + Fenables builtinsum).ShiftedFunctional(F + c, grad unchanged),ZeroFunctional(additive identity;make_scaledreturns it for a zero scalar,make_functional_sumdrops it). Scalar operands route to the affine shift. Registered all four infunctional_cases()so the registry-completeness check + generic functional laws (value / gradient / directional-derivative / conversion) exercise them.Adversarial verify — two bugs caught and fixed pre-merge
A multi-agent review (gradient correctness, factory laws, overload dispatch, jit integration) found:
a·Fisconj(a)·grad(F)(the domain inner product conjugates its first argument), matchingScaledLinOp.rapply.ScaledFunctional.gradwas scaling bya, notconj(a)— wrong for complex scalars. Fixed; value still scales bya.make_functional_sumdroppedZeroFunctionalterms before validating domains, soF(X) + Zero(Y)silently returnedF. Now validates all flattened terms' domains (folding in backend/dtype) before dropping zeros, mirroringlinop.make_sum.Both are covered by regression tests (complex Riesz identity; mismatched-domain Zero raising). Two jit-edge findings were reviewed and dismissed as unreachable.
Testing
tests/functional/test_algebra.py(31 tests): scalar/additive/affine/zero algebra, factory simplifications, tree-domain grad viaX.add/X.scale, complex Riesz identity, domain-mismatch guards, pytree round-trips, and an end-to-endminimize_optax(0.5F + 0.5F).🤖 Generated with Claude Code