Skip to content

feat: ScalarFunction ± Number adds the constant function - #18

Merged
wardjm merged 1 commit into
mainfrom
scalar-function-plus-number
Jul 11, 2026
Merged

wardjm merged 1 commit into
mainfrom
scalar-function-plus-number

Conversation

@wardjm

@wardjm wardjm commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Closes #11.

Python supports f + 5, 5 + f, f - 5, 5 - f on a Function, meaning "add the constant function". Julia defined * and / against a Number but not +/-, so all four raised MethodError.

What changed

Four methods in src/tensors/base_tensor/base_tensor.jl, plus a private _constant_function(f, c) helper that builds dg_function(dg, fill(c, npoints(dg))) — the semantics the issue pins. Subtraction reuses the existing tensor arithmetic (f - c is f + (-c), c - f is (-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, not AbstractTensor, so VectorField/Form/Tensor02 plus a scalar still raise MethodError — 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 exactly c.

The function plus scalar @test_broken in test/test_pyclasses.jl flips 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_broken are the separate per-batch-vector-scaling gap, untouched here.

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
@wardjm
wardjm merged commit dabc38e into main Jul 11, 2026
2 checks passed
@wardjm
wardjm deleted the scalar-function-plus-number branch August 3, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ScalarFunction ± Number is unimplemented

1 participant