Skip to content

Commit 0fd60a5

Browse files
committed
Implement .P property for LU factorizations
This matches the Base API.
1 parent d83e43f commit 0fd60a5

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/lu.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ Base.iterate(S::LU, ::Val{:U}) = (S.U, Val(:p))
1111
Base.iterate(S::LU, ::Val{:p}) = (S.p, Val(:done))
1212
Base.iterate(S::LU, ::Val{:done}) = nothing
1313

14+
@inline function Base.getproperty(F::LU, s::Symbol)
15+
if s === :P
16+
U = getfield(F, :U)
17+
p = getfield(F, :p)
18+
one(similar_type(p, Size(U)))[:,invperm(p)]
19+
else
20+
getfield(F, s)
21+
end
22+
end
23+
24+
function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, F::LU)
25+
println(io, LU) # Don't show full type - this will be in the factors
26+
println(io, "L factor:")
27+
show(io, mime, F.L)
28+
println(io, "\nU factor:")
29+
show(io, mime, F.U)
30+
end
31+
1432
# LU decomposition
1533
function lu(A::StaticMatrix, pivot::Union{Val{false},Val{true}}=Val(true))
1634
L, U, p = _lu(A, pivot)
@@ -136,9 +154,6 @@ end
136154
:(SVector{$(M-1),Int}($(tuple(2:M...))))
137155
end
138156

139-
# Base.lufact() interface is fairly inherently type unstable. Punt on
140-
# implementing that, for now...
141-
142157
\(F::LU, v::AbstractVector) = F.U \ (F.L \ v[F.p])
143158
\(F::LU, B::AbstractMatrix) = F.U \ (F.L \ B[F.p,:])
144159

test/lu.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
using StaticArrays, Test, LinearAlgebra
22

3+
@testset "LU utils" begin
4+
F = lu(SA[1 2; 3 4])
5+
6+
@test @inferred((F->F.p)(F)) === SA[2, 1]
7+
@test @inferred((F->F.P)(F)) === SA[0 1; 1 0]
8+
9+
@test occursin(r"^StaticArrays.LU.*L factor.*U factor"s, sprint(show, MIME("text/plain"), F))
10+
end
11+
312
@testset "LU decomposition ($m×$n, pivot=$pivot)" for pivot in (true, false), m in [0:4..., 15], n in [0:4..., 15]
413
a = SMatrix{m,n,Int}(1:(m*n))
514
l, u, p = @inferred(lu(a, Val{pivot}()))

0 commit comments

Comments
 (0)