Skip to content

feat: add non-mutating set_scalar_constants#165

Open
MilesCranmerBot wants to merge 1 commit intoSymbolicML:masterfrom
MilesCranmerBot:autopr/2026-02-15-set-scalar-constants
Open

feat: add non-mutating set_scalar_constants#165
MilesCranmerBot wants to merge 1 commit intoSymbolicML:masterfrom
MilesCranmerBot:autopr/2026-02-15-set-scalar-constants

Conversation

@MilesCranmerBot
Copy link
Contributor

This adds a non-mutating set_scalar_constants helper that returns a new tree with updated scalar constants.

Motivation (empirical): trying to run ForwardDiff.gradient through set_scalar_constants! on a Node{Float64} fails because constant node fields are concretely typed (e.g. cannot assign Dual into Float64). The new helper promotes the tree number type to accommodate eltype(constants) (so Duals work), sets the constants, and returns the new tree.

Changes:

  • Add set_scalar_constants(tree, constants) (non-mutating; promotes number type if needed).
  • Add set_scalar_constants(ex::Expression, constants) wrapper.
  • Add a regression test demonstrating ForwardDiff gradients w.r.t. constants.

Tests:

  • Pkg.test()

@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.53%. Comparing base (68d60d4) to head (141ecab).

Files with missing lines Patch % Lines
src/NodeUtils.jl 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #165      +/-   ##
==========================================
+ Coverage   59.33%   59.53%   +0.20%     
==========================================
  Files          30       30              
  Lines        2673     2684      +11     
==========================================
+ Hits         1586     1598      +12     
+ Misses       1087     1086       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MilesCranmerBot MilesCranmerBot force-pushed the autopr/2026-02-15-set-scalar-constants branch from f18c3ac to 141ecab Compare February 15, 2026 11:19
@github-actions
Copy link
Contributor

github-actions bot commented Feb 15, 2026

Benchmark Results (Julia v1)

Time benchmarks
master 141ecab... master / 141ecab...
eval/ComplexF32/evaluation 6.9 ± 0.47 ms 6.88 ± 0.46 ms 1 ± 0.096
eval/ComplexF64/evaluation 10.3 ± 0.75 ms 10.2 ± 0.84 ms 1 ± 0.11
eval/Float32/derivative 11.1 ± 1.1 ms 10.8 ± 1 ms 1.03 ± 0.14
eval/Float32/derivative_turbo 11 ± 1 ms 10.8 ± 0.95 ms 1.03 ± 0.13
eval/Float32/evaluation 2.44 ± 0.24 ms 2.46 ± 0.23 ms 0.992 ± 0.14
eval/Float32/evaluation_bumper 0.598 ± 0.017 ms 0.601 ± 0.016 ms 0.996 ± 0.038
eval/Float32/evaluation_turbo 0.52 ± 0.034 ms 0.526 ± 0.032 ms 0.987 ± 0.088
eval/Float32/evaluation_turbo_bumper 0.598 ± 0.017 ms 0.602 ± 0.017 ms 0.993 ± 0.039
eval/Float64/derivative 14.1 ± 1.8 ms 14.4 ± 1.8 ms 0.979 ± 0.17
eval/Float64/derivative_turbo 14 ± 1.4 ms 14.2 ± 1.4 ms 0.985 ± 0.14
eval/Float64/evaluation 2.87 ± 0.28 ms 2.88 ± 0.27 ms 0.997 ± 0.14
eval/Float64/evaluation_bumper 1.26 ± 0.041 ms 1.26 ± 0.045 ms 0.993 ± 0.048
eval/Float64/evaluation_turbo 1.02 ± 0.064 ms 1.02 ± 0.063 ms 1 ± 0.088
eval/Float64/evaluation_turbo_bumper 1.25 ± 0.044 ms 1.27 ± 0.043 ms 0.986 ± 0.048
utils/combine_operators/break_sharing 0.0424 ± 0.0013 ms 0.0416 ± 0.001 ms 1.02 ± 0.04
utils/convert/break_sharing 28 ± 3.6 μs 27.7 ± 3.6 μs 1.01 ± 0.19
utils/convert/preserve_sharing 0.1 ± 0.01 ms 0.0993 ± 0.0098 ms 1.01 ± 0.14
utils/copy/break_sharing 28.3 ± 4.9 μs 28 ± 4 μs 1.01 ± 0.23
utils/copy/preserve_sharing 0.101 ± 0.0096 ms 0.0988 ± 0.0089 ms 1.02 ± 0.13
utils/count_constant_nodes/break_sharing 13.5 ± 1.2 μs 12.9 ± 0.9 μs 1.05 ± 0.12
utils/count_constant_nodes/preserve_sharing 0.0871 ± 0.0091 ms 0.0858 ± 0.007 ms 1.01 ± 0.13
utils/count_depth/break_sharing 13.9 ± 1.5 μs 14.6 ± 0.98 μs 0.954 ± 0.12
utils/count_nodes/break_sharing 12.7 ± 1 μs 13.1 ± 1.2 μs 0.972 ± 0.12
utils/count_nodes/preserve_sharing 0.0871 ± 0.0082 ms 0.0871 ± 0.0078 ms 1 ± 0.13
utils/get_set_constants!/break_sharing 0.0334 ± 0.0039 ms 0.0331 ± 0.0038 ms 1.01 ± 0.17
utils/get_set_constants!/preserve_sharing 0.174 ± 0.011 ms 0.175 ± 0.011 ms 0.993 ± 0.089
utils/get_set_constants_parametric 0.0449 ± 0.0046 ms 0.0464 ± 0.0063 ms 0.968 ± 0.17
utils/has_constants/break_sharing 7.96 ± 1.6 μs 8.09 ± 1.4 μs 0.984 ± 0.26
utils/has_operators/break_sharing 2.62 ± 0.27 μs 2.65 ± 0.26 μs 0.992 ± 0.14
utils/hash/break_sharing 24 ± 2.3 μs 23.4 ± 1.4 μs 1.03 ± 0.12
utils/hash/preserve_sharing 0.103 ± 0.011 ms 0.102 ± 0.0084 ms 1.01 ± 0.14
utils/index_constant_nodes/break_sharing 29 ± 2.5 μs 29.6 ± 3.3 μs 0.979 ± 0.14
utils/index_constant_nodes/preserve_sharing 0.103 ± 0.01 ms 0.102 ± 0.0083 ms 1.01 ± 0.13
utils/is_constant/break_sharing 8.47 ± 1.5 μs 8.39 ± 1.3 μs 1.01 ± 0.23
utils/simplify_tree/break_sharing 31.2 ± 1.3 μs 31.1 ± 1.4 μs 1 ± 0.063
utils/simplify_tree/preserve_sharing 0.117 ± 0.0094 ms 0.111 ± 0.0071 ms 1.05 ± 0.11
utils/string_tree/break_sharing 0.45 ± 0.022 ms 0.428 ± 0.018 ms 1.05 ± 0.069
utils/string_tree/preserve_sharing 0.55 ± 0.03 ms 0.527 ± 0.023 ms 1.04 ± 0.072
time_to_load 0.167 ± 0.0024 s 0.167 ± 0.0039 s 1 ± 0.027
Memory benchmarks
master 141ecab... master / 141ecab...
eval/ComplexF32/evaluation 0.987 k allocs: 2.53 MB 0.987 k allocs: 2.53 MB 1
eval/ComplexF64/evaluation 0.99 k allocs: 5.04 MB 0.99 k allocs: 5.04 MB 1
eval/Float32/derivative 4.62 k allocs: 17.4 MB 4.62 k allocs: 17.4 MB 1
eval/Float32/derivative_turbo 4.7 k allocs: 17.7 MB 4.59 k allocs: 17.3 MB 1.02
eval/Float32/evaluation 0.984 k allocs: 1.28 MB 0.978 k allocs: 1.27 MB 1.01
eval/Float32/evaluation_bumper 0.303 k allocs: 0.393 MB 0.303 k allocs: 0.393 MB 1
eval/Float32/evaluation_turbo 0.96 k allocs: 1.25 MB 0.948 k allocs: 1.23 MB 1.01
eval/Float32/evaluation_turbo_bumper 0.303 k allocs: 0.393 MB 0.303 k allocs: 0.393 MB 1
eval/Float64/derivative 4.81 k allocs: 0.0352 GB 4.77 k allocs: 0.0349 GB 1.01
eval/Float64/derivative_turbo 4.79 k allocs: 0.035 GB 4.76 k allocs: 0.0348 GB 1.01
eval/Float64/evaluation 0.996 k allocs: 2.55 MB 0.99 k allocs: 2.53 MB 1.01
eval/Float64/evaluation_bumper 0.303 k allocs: 0.771 MB 0.303 k allocs: 0.771 MB 1
eval/Float64/evaluation_turbo 0.993 k allocs: 2.54 MB 0.972 k allocs: 2.49 MB 1.02
eval/Float64/evaluation_turbo_bumper 0.303 k allocs: 0.771 MB 0.303 k allocs: 0.771 MB 1
utils/combine_operators/break_sharing 4 allocs: 0.953 kB 4 allocs: 0.953 kB 1
utils/convert/break_sharing 2 k allocs: 0.123 MB 2 k allocs: 0.123 MB 1
utils/convert/preserve_sharing 2.4 k allocs: 0.192 MB 2.4 k allocs: 0.192 MB 1
utils/copy/break_sharing 2 k allocs: 0.123 MB 2 k allocs: 0.123 MB 1
utils/copy/preserve_sharing 2.4 k allocs: 0.192 MB 2.4 k allocs: 0.192 MB 1
utils/count_constant_nodes/break_sharing 4 allocs: 0.953 kB 4 allocs: 0.953 kB 1
utils/count_constant_nodes/preserve_sharing 0.404 k allocs: 0.0696 MB 0.404 k allocs: 0.0696 MB 1
utils/count_depth/break_sharing 4 allocs: 0.953 kB 4 allocs: 0.953 kB 1
utils/count_nodes/break_sharing 4 allocs: 0.953 kB 4 allocs: 0.953 kB 1
utils/count_nodes/preserve_sharing 0.404 k allocs: 0.0696 MB 0.404 k allocs: 0.0696 MB 1
utils/get_set_constants!/break_sharing 0.898 k allocs: 25.2 kB 0.898 k allocs: 25.2 kB 1
utils/get_set_constants!/preserve_sharing 1.7 k allocs: 0.138 MB 1.7 k allocs: 0.138 MB 1
utils/get_set_constants_parametric 1.42 k allocs: 0.0663 MB 1.42 k allocs: 0.0663 MB 1
utils/has_constants/break_sharing 4 allocs: 0.203 kB 4 allocs: 0.203 kB 1
utils/has_operators/break_sharing 4 allocs: 0.203 kB 4 allocs: 0.203 kB 1
utils/hash/break_sharing 0.104 k allocs: 2.52 kB 0.104 k allocs: 2.52 kB 1
utils/hash/preserve_sharing 0.504 k allocs: 0.0711 MB 0.504 k allocs: 0.0711 MB 1
utils/index_constant_nodes/break_sharing 2.1 k allocs: 0.094 MB 2.1 k allocs: 0.094 MB 1
utils/index_constant_nodes/preserve_sharing 2.5 k allocs: 0.163 MB 2.5 k allocs: 0.163 MB 1
utils/is_constant/break_sharing 4 allocs: 0.203 kB 4 allocs: 0.203 kB 1
utils/simplify_tree/break_sharing 0.104 k allocs: 2.52 kB 0.104 k allocs: 2.52 kB 1
utils/simplify_tree/preserve_sharing 0.504 k allocs: 0.0711 MB 0.504 k allocs: 0.0711 MB 1
utils/string_tree/break_sharing 11.9 k allocs: 0.999 MB 11.9 k allocs: 0.999 MB 1
utils/string_tree/preserve_sharing 12.3 k allocs: 1.07 MB 12.3 k allocs: 1.07 MB 1
time_to_load 0.145 k allocs: 11 kB 0.145 k allocs: 11 kB 1

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

Comments