Skip to content

Commit de7aba9

Browse files
Fix copy method for StatefulJacobianOperator with Tuple parameters
- Add `_safe_copy` helper functions to handle immutable types (Tuple, NamedTuple, Number) that don't need copying - Fix the `Base.copy` method for `StatefulJacobianOperator` to use `_safe_copy(J.p)` instead of `applicable(copy, J.p) ? copy(J.p) : J.p` which fails for Tuple types - Fix typo: `nohting` -> `nothing` in `JacobianOperator.copy` This fixes the JFNK (Jacobian-Free Newton-Krylov) example from the documentation which was failing with: MethodError: no method matching copy(::NTuple{4, Float64}) Fixes #752 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent d2a9b8a commit de7aba9

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/SciMLJacobianOperators/src/SciMLJacobianOperators.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,30 @@ function get_dense_ad(ad::AutoSparse)
404404
return dense_ad
405405
end
406406

407+
# Helper functions for safe copying of parameters
408+
# Tuples and NamedTuples are immutable and don't need copying
409+
# Numbers are immutable as well
410+
_safe_copy(x::Tuple) = x
411+
_safe_copy(x::NamedTuple) = x
412+
_safe_copy(x::Number) = x
413+
_safe_copy(x) = copy(x)
414+
407415
function Base.copy(J::JacobianOperator{iip, T}) where {iip, T}
408-
return JacobianOperator{iip,T}(
416+
return JacobianOperator{iip, T}(
409417
J.mode,
410418
J.jvp_op,
411419
J.vjp_op,
412420
J.size,
413421
J.input_cache === nothing ? nothing : copy(J.input_cache),
414-
J.output_cache === nothing ? nohting : copy(J.output_cache)
422+
J.output_cache === nothing ? nothing : copy(J.output_cache)
415423
)
416424
end
417425

418426
function Base.copy(J::StatefulJacobianOperator)
419427
return StatefulJacobianOperator(
420428
copy(J.jac_op),
421429
J.u === nothing ? nothing : copy(J.u),
422-
applicable(copy, J.p) ? copy(J.p) : J.p
430+
_safe_copy(J.p)
423431
)
424432
end
425433

@@ -429,10 +437,8 @@ function Base.copy(J::StatefulJacobianNormalFormOperator{T}) where {T}
429437
J.jvp_operator === nothing ? nothing : copy(J.jvp_operator),
430438
J.cache === nothing ? nothing : copy(J.cache)
431439
)
432-
433440
end
434441

435-
436442
export JacobianOperator, VecJacOperator, JacVecOperator
437443
export StatefulJacobianOperator
438444
export StatefulJacobianNormalFormOperator

0 commit comments

Comments
 (0)