Skip to content

Remove Scope: standalone variables, stacked per problem - #45

Merged
dance858 merged 2 commits into
mainfrom
standalone-variables
Nov 30, 2026
Merged

dance858 merged 2 commits into
mainfrom
standalone-variables

Conversation

@dance858

Copy link
Copy Markdown
Collaborator

Summary

Breaking change to the public Python API: Scope is removed. Variables and parameters are standalone objects that hold their own values, and each compiled object decides how its variables are stacked.

theta = sp.Variable(d, 1)
w = sp.Variable(N, 1)
prob = sp.Problem(sp.sum(w * sp.logistic(A @ theta)), [sp.sum(w)])
theta.value, w.value = theta0, w0
grad = prob.gradient()
J = prob.jacobian()
H = prob.hessian(sigma, lam)
prob.variables        # [theta, w]

Behaviour

  • Constructors: sp.Variable(d1, d2) and sp.Parameter(d1, d2, value=None) replace scope.Variable and scope.Parameter. Both dimensions are required.
  • Values live on the variables: x.value = ... sets the point; reading it returns a read-only copy, or None if it was never set. Evaluating while a variable has no value raises ValueError (variables used to default to zero).
  • Stacking order is creation order: a problem stacks the variables it uses in the order their constructors ran, whatever the order they appear in the expressions. Variables it does not use are skipped, so a Jacobian no longer has columns for unrelated variables. There is no way to override the order.
  • The order is inspectable: variables on Problem and CompiledExpression lists the variables in stacking order.
  • Flat access: set_values(u) and get_values() on Problem and CompiledExpression take and return a 1-D vector in stacking order. n_vars is available on both.
  • Shared variables: a variable can be used in several problems. Each problem assigns its own offsets, and setting the variable's value moves the point of all of them.
  • Caching: each engine stage still runs at most once per point. Assigning a value that a variable or parameter already has keeps the caches, and set_values on one problem does not invalidate another problem unless a variable they share actually changed.
  • Thread safety: creation ids come from a lock-guarded counter, so variables and parameters can be created from several threads.

Implementation

  • _expression.py: Scope is deleted. Variable and Parameter share a small _Leaf base holding the shape, creation id, value and a version counter.
  • _compile.py: Problem collects 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.
  • No changes to sparsediffpy/_bindings or 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_values round-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.md and conda.recipe/test_sparsediffpy.py are unchanged: neither mentions Scope.
  • The staleness check is one comparison per variable and parameter on every evaluation call, where the scope counter was O(1).

🤖 Generated with Claude Code

dance858 and others added 2 commits November 30, 2026 06:42
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>
@dance858
dance858 merged commit fca5317 into main Nov 30, 2026
94 of 102 checks passed
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.

1 participant