From ea7594e4b5372cd4e6768b765ca1ce79b0ba9d65 Mon Sep 17 00:00:00 2001 From: lxvm Date: Wed, 26 Jun 2024 18:29:21 -0400 Subject: [PATCH] refactor inv to also work for lu --- src/inv.jl | 5 +++-- test/inv.jl | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/inv.jl b/src/inv.jl index 0e493a169..d7e58e0c8 100644 --- a/src/inv.jl +++ b/src/inv.jl @@ -76,10 +76,11 @@ end if prod(S) ≤ 14*14 quote @_inline_meta - LUp = lu(A) - LUp.U \ (LUp.L \ typeof(A)(I)[LUp.p,:]) + inv(lu(A)) end else :(@_inline_meta; similar_type(A)(inv(Matrix(A)))) end end + +inv(LUp::LU) = LUp.U \ (LUp.L \ typeof(LUp.L.data)(I)[LUp.p,:]) diff --git a/test/inv.jl b/test/inv.jl index 829414313..185e67948 100644 --- a/test/inv.jl +++ b/test/inv.jl @@ -97,6 +97,12 @@ end @test inv(A) ≈ inv(SA) end +@testset "LU to inverse" for sz in (5, 8, 15), typ in (Float64, Complex{Float64}) + A = rand(typ, sz, sz) + SA = SMatrix{sz,sz,typ}(A) + @test inv(lu(A)) ≈ inv(lu(SA)) +end + #------------------------------------------------------------------------------- # More comprehensive but qualitative testing for inv() accuracy #=