Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ function update_coefficients!(L::ScaledOperator, u, p, t)
end

getops(L::ScaledOperator) = (L.λ, L.L)

# Copy method to avoid aliasing
function Base.copy(L::ScaledOperator)
ScaledOperator(copy(L.λ), copy(L.L))
end

isconstant(L::ScaledOperator) = isconstant(L.L) & isconstant(L.λ)
islinear(L::ScaledOperator) = islinear(L.L)
Base.iszero(L::ScaledOperator) = iszero(L.L) | iszero(L.λ)
Expand Down Expand Up @@ -562,6 +568,12 @@ end
end

getops(L::AddedOperator) = L.ops

# Copy method to avoid aliasing
function Base.copy(L::AddedOperator)
AddedOperator(map(copy, L.ops))
end

islinear(L::AddedOperator) = all(islinear, getops(L))
Base.iszero(L::AddedOperator) = all(iszero, getops(L))
has_adjoint(L::AddedOperator) = all(has_adjoint, L.ops)
Expand Down
30 changes: 30 additions & 0 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,36 @@ using Test
@test L_copy.L.A[1, 1] != 999.0
end

# Test ScaledOperator
@testset "ScaledOperator" begin
α = ScalarOperator(2.0)
A = MatrixOperator(rand(5, 5))
L = α * A
L_copy = copy(L)

# Modify original
L.λ.val = 999.0
L.L.A[1, 1] = 888.0

# Check that copy is not affected
@test L_copy.λ.val == 2.0
@test L_copy.L.A[1, 1] != 888.0
end

# Test AddedOperator
@testset "AddedOperator" begin
A = MatrixOperator(rand(5, 5))
B = MatrixOperator(rand(5, 5))
L = A + B
L_copy = copy(L)

# Modify original
L.ops[1].A[1, 1] = 999.0

# Check that copy is not affected
@test L_copy.ops[1].A[1, 1] != 999.0
end

# Test that operators still work correctly after copying
@testset "Functionality after copy" begin
# MatrixOperator
Expand Down
7 changes: 6 additions & 1 deletion test/downstream/alloccheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ test_apply_noalloc(H, w, v, u, p, t) = @test (@allocations apply_op!(H, w, v, u,
op = AddedOperator(A, B)

apply_op!(op, w, v, u, p, t) # Warm up
test_apply_noalloc(op, w, v, u, p, t)
if VERSION < v"1.10" || VERSION >= v"1.11"
test_apply_noalloc(op, w, v, u, p, t)
else
# Julia 1.10 has a known allocation issue with AddedOperator
@test (@allocations apply_op!(op, w, v, u, p, t)) == 1
end

for T in (Float32, Float64, ComplexF32, ComplexF64)
N = 100
Expand Down
Loading