Skip to content

problem: build the Lagrange Hessian with sum_n_matrices_* - #163

Merged
dance858 merged 1 commit into
mainfrom
problem-sum-n
Oct 13, 2026
Merged

dance858 merged 1 commit into
mainfrom
problem-sum-n

Conversation

@dance858

Copy link
Copy Markdown
Collaborator

Moves problem.c's Lagrange Hessian assembly onto the library's sum_n_matrices_alloc / sum_n_matrices_fill_zero / sum_n_matrices_fill_add. The library is unchanged and the pin stays at cba21ac.

Changes

  • Removed: problem_lagrange_hess_fill_sparsity (about 90 lines), hess_idx_map and the accumulator loop.
  • lagrange_hessian: stays a CSR_matrix *, now the CSR view of the new hess_sum. The view is not a copy, and free_problem frees hess_sum. The bindings, the COO path and the tests don't change.
  • Shared subexpressions: each Hessian is added right after it is evaluated, so constraints that share a node no longer sum overwritten values. This is the same pattern as Fix add's Hessian when its children share a subexpression #159 (add) and hstack: form the Hessian with sum_n_matrices #161 (hstack). The new test test_problem_hessian_shared_subexpr uses constraints log(x) and exp(log(x)) with the same log(x) node. On the old code it fails with -4 where -1 is correct.
  • Affine constraints: they already skipped their eval, and now they skip the add too, since their Hessian is zero.
  • problem.c no longer uses accumulator, compose_csr_idx_map_for_spd or csr_kernels.h. That clears the way for a library PR making them private.

Performance

Old main vs this branch, both Release, 5 interleaved runs. The Hessian checksums are identical.

n_vars, constraints init_hessian old → new problem_hessian old → new
1000, 3000 (+10 affine) 32–42 ms → 30–32 ms 1515–1543 µs → 1415–1459 µs
10, 50000 (+10 affine) 7.2–7.5 ms → 4.3–4.4 ms 624–670 µs → 573–609 µs

Testing

  • All 303 tests pass in Debug, Release and an SP_TRACK_MEMORY build.
  • leaks --atExit reports 0 leaks.

🤖 Generated with Claude Code

Replace problem_lagrange_hess_fill_sparsity, hess_idx_map and the
accumulator loop with sum_n_matrices_alloc / fill_zero / fill_add.
lagrange_hessian stays a CSR_matrix *, now the CSR view of hess_sum.

Each Hessian is added right after it is evaluated, so constraints that
share a subexpression no longer sum overwritten values. Affine
constraints, whose Hessian is zero, skip the add.

problem.c no longer uses accumulator, compose_csr_idx_map_for_spd or
csr_kernels.h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dance858
dance858 merged commit 6dd0ff0 into main Oct 13, 2026
14 checks passed
@dance858
dance858 deleted the problem-sum-n branch October 15, 2026 05:39
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