feat: ScalarFunction ± Number adds the constant function - #18
Merged
Merged
Conversation
A scalar added to a function means "add the constant function of that value", projected onto the basis: f + c == f + dg_function(dg, fill(c, n)). Only functions absorb a scalar this way — the other tensors have no canonical constant element, so +/- against a Number stays a MethodError there, matching upstream. Flips the `function plus scalar` @test_broken in the ported Python suite. Closes #11
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.
Closes #11.
Python supports
f + 5,5 + f,f - 5,5 - fon aFunction, meaning "add the constant function". Julia defined*and/against aNumberbut not+/-, so all four raisedMethodError.What changed
Four methods in
src/tensors/base_tensor/base_tensor.jl, plus a private_constant_function(f, c)helper that buildsdg_function(dg, fill(c, npoints(dg)))— the semantics the issue pins. Subtraction reuses the existing tensor arithmetic (f - cisf + (-c),c - fis(-f) + c), and batching falls out for free: tensor+already right-aligns and broadcasts the unbatched constant across batch rows.The methods dispatch on
ScalarFunction, notAbstractTensor, soVectorField/Form/Tensor02plus a scalar still raiseMethodError— the behaviour upstream deliberately keeps. The ported suite now asserts that in both operand orders.Verification
Checked against a real geometry before the suite: the coefficient difference lands entirely in φ₀ at
c * sqrt(sum(measure(dg)))(residual ~1e-14 elsewhere), and pointwise values shift by exactlyc.The
function plus scalar@test_brokenintest/test_pyclasses.jlflips to real assertions — value checks against the explicit constant function, the upstream φ₀ check, and all four operand orders in the batched case. Full suite passes at 2325 tests; the 4 remaining@test_brokenare the separate per-batch-vector-scaling gap, untouched here.