Repository navigation
refactor: remove DDScalar::resize - #77
Merged
Merged
Conversation
resize() set m_size and resized the flat buffer, nothing else. That is
correct for first order, where the layout is [f, g0..g(s-1)] and the moving
boundary sits at the end — growing appends zeros, shrinking drops trailing
variables. For second order the g/h boundary sits in the middle and the
triangular row lengths depend on the size, so the same bytes get
reinterpreted:
size 2: [1, 2, 3, 4, 5, 6] f=1 g=[2,3] H=[[4,5],[5,6]]
resize(3): [1, 2, 3, 4, 5, 6, 0,0,0,0] f=1 g=[2,3,4] H=[[5,6,0],[6,0,0],[0,0,0]]
h00 becomes g2, and h01/h11 shift into h00/h01.
The intent was to change the number of variables without a remap, in order
to overwrite the contents afterwards. In practice a new instance is the
clearer way to do that, and every real use is already covered: keep the
derivatives with pad_left/pad_right, discard them with empty(size) or
zero(size).
Nothing used it. The only internal resize call was the m_data.resize inside
it, and no C++ test or benchmark referenced it. Verified that pad_left and
pad_right do remap correctly before removing the alternative: 96 cases over
{order 1, 2} x size {0,1,2,3,5,8} x padding {0,1,3,7}, compared bitwise
against a reference built independently from f, g and H, with unique values
throughout so a permutation could not hide.
Tests: test_resize never touched resize — it only called pad_right and was a
weaker duplicate of test_pad_right, so its size assertion moved into
test_pad_right and test_pad_left. The negative-size case for resize goes with
resize. The "# resizing" section now sits above the pad tests where it
belongs.
This is a breaking change to the Python API. Removing it outright rather than
deprecating it, because for second-order scalars it silently produced wrong
derivatives.
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.
What it did
Set
m_size, resized the flat buffer, nothing else — no remap.That is correct for first order, where the layout is
[f, g₀…g_{s-1}]and the moving boundary sits at the end:For second order the g/h boundary sits in the middle and the triangular row lengths depend on the size, so the same bytes get reinterpreted:
h00becomesg2, andh01/h11shift intoh00/h01. Shrinking does the same in reverse:resize(1)givesH=[[3]], where the 3 is the oldg1.Why remove rather than fix
The intent was to change the number of variables without a remap, in order to overwrite the contents afterwards. A new instance is the clearer way to express that, and every real use is already covered:
pad_left(new_size)/pad_right(new_size)empty(size)/zero(size)Making
resizeremap would just duplicatepad_right.Nothing used it: the only internal
resizecall was them_data.resizeinside it, and no C++ test or benchmark referenced it.Verified pad_left/pad_right first
Before removing the alternative, I checked that the remapping ones are actually correct — 96 cases over
{order 1, 2} × size {0,1,2,3,5,8} × padding {0,1,3,7}, compared bitwise (array_equal, notallclose) against a reference built independently fromf,gandH. Values were unique throughout (100, 101, 102, …) so a permutation could not hide behind repeated numbers. 0 deviations.The index arithmetic also holds analytically: for
new_size = s + t, rowi < shas(s-i) + tentries — old row thentzeros — and the new rowss+khavet-kentries, matching the final loop. The element counts sum to exactly1 + N + N(N+1)/2for both functions.Tests
test_resizenever touchedresize— it only calledpad_rightand was a weaker duplicate oftest_pad_right. Itssizeassertion moved intotest_pad_rightandtest_pad_left, which check the full data anyway; the test itself is gone. The negative-size case forresize(from #73) goes withresize. The# resizingsection header now sits above the pad tests instead of abovetest_eval.Deliberately not adding a test that asserts
resizeis absent: if someone later adds a correct, remappingresize, such a test would fail for no good reason.After: C++ 42/42 test cases, 421 assertions · Python 162 passed (166 − 4 removed parametrizations) ·
clang-format --Werrorandruff format --checkclean ·resizegone from all four Python types, and the replacement paths verified:Notes
This is a breaking change to the Python API. Removing it outright rather than deprecating, per discussion: for second-order scalars it silently produced wrong derivatives, so there is no behaviour worth preserving through a deprecation cycle.
README gained a sentence in the
DDScalarsection pointing atpad_left/pad_rightandempty/zero—resizeitself was never documented.Based on
main; independent of #75 and #76.