From 228b5a528dece06be26cbcd0180184b0a5abaf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Thu, 24 Sep 2026 22:53:10 +0200 Subject: [PATCH] Pin `+`/`-` with untracked elements (#175) `Diagonal` and `diagm` of a `Vector{Real}` give tracked diagonal entries and untracked zeros, whose reverse pass threw `increment_deriv!(::Float64, ::Float64)` until #300 made it skip untracked elements. Check against the analytical Jacobians, directly and on a compiled tape. Co-Authored-By: Claude Opus 5.5 (1M context) --- test/derivatives/LinAlgTests.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/derivatives/LinAlgTests.jl b/test/derivatives/LinAlgTests.jl index 014d8c77..415883b8 100644 --- a/test/derivatives/LinAlgTests.jl +++ b/test/derivatives/LinAlgTests.jl @@ -339,4 +339,19 @@ for f in (transpose, adjoint) end end +@testset "`+`/`-` with untracked elements (#175)" begin + # the diagonal entries are tracked, the off-diagonal zeros are not + dense(u) = reshape(u[1:9], 3, 3) + diagreal(u) = Real[u[10], u[11], u[12]] + E = Matrix(I(9))[:, [1, 5, 9]] + for (f, J) in ( + (u -> dense(u) + Diagonal(diagreal(u)), [I(9) E]), + (u -> dense(u) - diagm(diagreal(u)), [I(9) -E]), + (u -> diagm(diagreal(u)) - dense(u), [-I(9) E]), + (u -> dense(u) + Real[i == j ? u[9 + i] : 0.0 for i in 1:3, j in 1:3], [I(9) E]), + ) + test_jacobian(f, rand(12), J) + end +end + end # module