ITADN

Fix repeated evaluation of fx0 in forward gradient computation

#203Pull RequestChrisRackauckas-Claude 创建于 2025-08-16已合并
## Summary - Fixed repeated evaluation of `fx0 = f(x)` inside the loop in `finite_difference_gradient!` - Optimizes function evaluations from 2N to N+1 for forward differences when computing gradients - Maintains full compatibility with both cached and uncached function values ## Problem As reported in #202, when computing forward differences for gradients, the function `f(x)` was being evaluated N times inside the loop (once per iteration) in addition to the N evaluations for perturbed inputs, resulting in 2N total function evaluations. ## Solution - Moved the `fx0 = f(x)` computation outside the loop - Use conditional assignment: `fx0 = typeof(fx) != Nothing ? fx : f(x)` - Simplified the loop logic by eliminating conditional branches - Applied the same optimization to both real and complex gradient computations ## Test plan - [x] Verified gradient accuracy remains unchanged - [x] Confirmed function evaluation count is reduced from 2N to N+1 - [x] Ran existing test suite (passed core functionality tests) - [x] Applied SciMLStyle formatting ## Performance Impact For a vector of length N, this reduces function evaluations by ~50%, providing significant performance improvement for expensive functions. Fixes #202 🤖 Generated with [Claude Code](https://claude.ai/code)
合并状态:已合并 合并于 2025-08-16 关闭于 2025-08-16 0 条评论