Repository navigation
Fix add's Hessian when its children share a subexpression - #159
Merged
Merged
Conversation
add evaluated both children's Hessians and then summed them. When the children share a node, evaluating the right child re-evaluates that node with other weights and overwrites values the left child's Hessian still needed, so add(log x, exp(log x)) gave -2 instead of -4. Now the sum is filled incrementally with sum_n_matrices_fill_zero / fill_add, adding each child's Hessian right after it is evaluated. Pins sparsediff_linalg to cba21ac (#20, which adds fill_zero / fill_add). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 13, 2026
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
addevaluated both children's Hessians and then summed them. When the children share a node, evaluating the right child re-evaluates that node with different weights. That overwrites Hessian values the left child still needed before the sum reads them.add(log x, exp(log x))gave -2 where -4 is correct.sum_n_matrices_fill_zero/sum_n_matrices_fill_add, so each child's Hessian is added right after it is evaluated. The pattern comes fromsum_n_matrices_allocwith n = 2, andaddstill needs no struct of its own.sparsediff_linalgmoves tocba21ac(library Adds indexing atom implementation and other bindings #20, which addsfill_zero/fill_add).Other atoms that evaluate several children and sum afterwards (
multiply/matmulviasum_4,problem.c's Lagrange Hessian) likely have the same issue with shared nodes. They aren't changed here.Tests
test_wsum_hess_add_shared_subexprchecksadd(log x, exp(log x))andadd(exp(log x), log x)against the numerical Hessian. Both use a single sharedlog xnode.add.c(analytical -2, numerical -4) and passes with the fix.🤖 Generated with Claude Code