Skip to content

Commit 778a563

Browse files
authored
refactor inv to also work for lu (#1267)
* refactor inv to also work for lu * handle non-square case like LA * bump v1.9.7 * test non-square case
1 parent 12fd8f7 commit 778a563

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.9.6"
3+
version = "1.9.7"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/inv.jl

+9-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,17 @@ end
7676
if prod(S) 14*14
7777
quote
7878
@_inline_meta
79-
LUp = lu(A)
80-
LUp.U \ (LUp.L \ typeof(A)(I)[LUp.p,:])
79+
inv(lu(A))
8180
end
8281
else
8382
:(@_inline_meta; similar_type(A)(inv(Matrix(A))))
8483
end
8584
end
85+
86+
function inv(LUp::LU)
87+
if !(LUp.L isa LowerTriangular)
88+
checksquare(LUp.L)
89+
checksquare(LUp.U)
90+
end
91+
LUp.U \ (LUp.L \ typeof(parent(LUp.L))(I)[LUp.p,:])
92+
end

test/inv.jl

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ end
9797
@test inv(A) inv(SA)
9898
end
9999

100+
@testset "LU to inverse" for sz in (5, 8, 15), typ in (Float64, Complex{Float64})
101+
A = rand(typ, sz, sz)
102+
SA = SMatrix{sz,sz,typ}(A)
103+
@test inv(lu(A)) inv(lu(SA))
104+
@test_throws DimensionMismatch inv(lu(SMatrix{sz,sz+1,typ}(rand(typ, sz, sz+1))))
105+
@test_throws DimensionMismatch inv(lu(SMatrix{sz+1,sz,typ}(rand(typ, sz+1, sz))))
106+
end
107+
100108
#-------------------------------------------------------------------------------
101109
# More comprehensive but qualitative testing for inv() accuracy
102110
#=

0 commit comments

Comments
 (0)