Skip to content

Commit fe15974

Browse files
Add fallback Base.copy method for AbstractSciMLOperator
This adds a fallback copy method for AbstractSciMLOperator that provides safe copying behavior for operators that don't have specific copy methods defined. This prevents issues when LinearSolve.jl tries to copy operators in the initialization path, addressing CI failures in LinearSolve.jl PR #763. The fallback method: - For convertible operators: Creates a new operator from the copied concrete representation - For non-convertible operators: Falls back to deepcopy as a last resort - Includes appropriate warnings when using fallback behavior This ensures all SciMLOperators have a copy method available, preventing MethodError exceptions when copy() is called on complex operator types. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 260f9a3 commit fe15974

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/interface.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,34 @@ function LinearAlgebra.mul!(v::AbstractArray,
460460
end
461461
mul!(v, concretize(L), u, α, β)
462462
end
463+
464+
"""
465+
Base.copy(L::AbstractSciMLOperator)
466+
467+
Default fallback for copying SciMLOperators. This method provides a safe
468+
fallback for operators that don't have a specific copy method defined,
469+
avoiding the need for deepcopy in LinearSolve.jl initialization paths.
470+
471+
For operators that can be converted to concrete matrices, this creates a new
472+
operator from the copied concrete representation. For operators that cannot
473+
be easily converted, this falls back to deepcopy as a last resort.
474+
"""
475+
function Base.copy(L::AbstractSciMLOperator)
476+
# This is the fallback method - it should only be called if no specific
477+
# copy method is defined for the concrete type
478+
if isconvertible(L)
479+
# Try to reconstruct the operator from its concrete representation
480+
try
481+
concrete = concretize(L)
482+
return MatrixOperator(copy(concrete))
483+
catch
484+
@warn "Failed to copy via concrete representation, falling back to deepcopy"
485+
return deepcopy(L)
486+
end
487+
else
488+
@warn "Using deepcopy fallback for non-convertible operator in Base.copy"
489+
return deepcopy(L)
490+
end
491+
end
492+
463493
#

0 commit comments

Comments
 (0)