Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix InvertibleOperator caching, add tests #188

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ islinear(L::InvertibleOperator) = islinear(L.L)
has_ldiv(L::InvertibleOperator) = has_mul(L.F)
has_ldiv!(L::InvertibleOperator) = has_ldiv!(L.F)

function cache_internals(L::InvertibleOperator, u::AbstractVecOrMat)

@set! L.L = cache_operator(L.L, u)
@set! L.F = cache_operator(L.F, u)

L
end

# operator application
Base.:*(L::InvertibleOperator, x::AbstractVecOrMat) = L.L * x
Base.:\(L::InvertibleOperator, x::AbstractVecOrMat) = L.F \ x
Expand Down
28 changes: 27 additions & 1 deletion test/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SciMLOperators, LinearAlgebra
using Random

using SciMLOperators: InvertibleOperator, ⊗
using SciMLOperators: InvertibleOperator, InvertedOperator,
using FFTW

Random.seed!(0)
Expand Down Expand Up @@ -60,6 +60,32 @@ K = 19
v=rand(N,K); w=copy(v); @test mul!(v, AA, u, α, β) ≈ α*A*u + β*w
end

@testset "InvertibleOperator test" begin
u = rand(N,K)
p = nothing
t = 0
α = rand()
β = rand()

d = rand(N, K)
D = DiagonalOperator(d)
Di = DiagonalOperator(inv.(d)) |> InvertedOperator

L = InvertibleOperator(D, Di)
L = cache_operator(L, u)

@test iscached(L)

@test L * u ≈ d .* u
@test L \ u ≈ d .\ u

v=rand(N,K); @test mul!(v, L, u) ≈ d .* u
v=rand(N,K); w=copy(v); @test mul!(v, L, u, α, β) ≈ α*(d .* u) + β*w

v=rand(N,K); @test ldiv!(v, L, u) ≈ d .\ u
v=copy(u); @test ldiv!(L, v) ≈ d .\ u
end

@testset "MatrixOperator update test" begin
u = rand(N,K)
p = rand(N)
Expand Down