Repository navigation
Remove Scope: standalone variables, stacked per problem - #45
Merged
Merged
Conversation
Variables and parameters are created with sp.Variable(d1, d2) and sp.Parameter(d1, d2) and hold their own values. Each Problem and CompiledExpression stacks the variables it uses in the order they were created, whatever the order they appear in the expression, and skips variables it does not use. The order is exposed as `variables`, and the flat vector is set and read with set_values() and get_values(). Offsets and the staleness check move from the scope to the problem: each problem assigns its own variable and parameter offsets and compares a version counter per leaf, so a variable can be shared by several problems. Assigning a value a variable or parameter already has keeps the caches. Evaluating while a variable has no value raises. This is a breaking change; there is no Scope alias. The bindings and the engine are unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The analytic references used NumPy's A.T @ D @ A, which returns wrong numbers in the emulated aarch64 wheel tests (NumPy 2.5.5). The Hessians are still checked against finite differences. Also rewrite the symmetry test and tighten the free-threading sentence in the README. Co-Authored-By: Claude Fable 5.2 <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
Breaking change to the public Python API:
Scopeis removed. Variables and parameters are standalone objects that hold their own values, and each compiled object decides how its variables are stacked.Behaviour
sp.Variable(d1, d2)andsp.Parameter(d1, d2, value=None)replacescope.Variableandscope.Parameter. Both dimensions are required.x.value = ...sets the point; reading it returns a read-only copy, orNoneif it was never set. Evaluating while a variable has no value raisesValueError(variables used to default to zero).variablesonProblemandCompiledExpressionlists the variables in stacking order.set_values(u)andget_values()onProblemandCompiledExpressiontake and return a 1-D vector in stacking order.n_varsis available on both.set_valueson one problem does not invalidate another problem unless a variable they share actually changed.Implementation
_expression.py:Scopeis deleted.VariableandParametershare a small_Leafbase holding the shape, creation id, value and a version counter._compile.py:Problemcollects its leaves, sorts them by creation id, assigns its own variable and parameter offsets, and keeps its own flat buffer. Staleness is detected by comparing one version per leaf, which costs one extra O(n) copy of the values per new point. The reused Jacobian and Hessian matrices from clean up bindings #43 are unchanged.sparsediffpy/_bindingsor the engine.Tests
117 tests pass locally on CPython 3.12 (the 102 existing tests ported, 15 new in
tests/test_variables.py). The new tests cover creation order versus expression order,variables, unused variables, a variable shared by two problems, unset values,set_valuesround-tripping through.value, cache reuse at an unchanged point for variables and parameters, unique ids across threads, and two worked examples checked against analytic derivatives and finite differences. Every README code block was run.Not run locally on a free-threaded build.
Notes for review
RELEASES.mdandconda.recipe/test_sparsediffpy.pyare unchanged: neither mentionsScope.🤖 Generated with Claude Code