Skip to content

Commit ac3ed4d

Browse files
committed
Minor tweaks and fixes to static LU
* _lu() dispatch on `Size` * Fix permutation to have `Int` eltype.
1 parent 12217c7 commit ac3ed4d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/lu.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
# LU decomposition
22
function lu(A::StaticMatrix, pivot::Union{Type{Val{false}},Type{Val{true}}}=Val{true})
3-
L,U,p = _lu(A, pivot)
3+
L,U,p = _lu(Size(A), A, pivot)
44
(L,U,p)
55
end
66

77
# For the square version, return explicit lower and upper triangular matrices.
88
# We would do this for the rectangular case too, but Base doesn't support that.
99
function lu(A::StaticMatrix{N,N}, pivot::Union{Type{Val{false}},Type{Val{true}}}=Val{true}) where {N}
10-
L,U,p = _lu(A, pivot)
10+
L,U,p = _lu(Size(A), A, pivot)
1111
(LowerTriangular(L), UpperTriangular(U), p)
1212
end
1313

1414

15-
@inline function _lu(A::StaticMatrix, pivot)
15+
@inline function _lu(::Size{S}, A::StaticMatrix, pivot) where {S}
1616
# For now, just call through to Base.
1717
# TODO: statically sized LU without allocations!
1818
f = lufact(Matrix(A), pivot)
1919
T = eltype(A)
20+
# Trick to get the output eltype - can't rely on the result of f[:L] as
21+
# it's not type inferrable.
2022
T2 = typeof((one(T)*zero(T) + zero(T))/one(T))
2123
L = similar_type(A, T2, Size(Size(A)[1], diagsize(A)))(f[:L])
2224
U = similar_type(A, T2, Size(diagsize(A), Size(A)[2]))(f[:U])
23-
p = similar_type(A, Size(Size(A)[1]))(f[:p])
25+
p = similar_type(A, Int, Size(Size(A)[1]))(f[:p])
2426
(L,U,p)
2527
end
2628

test/lu.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ using StaticArrays, Base.Test
33
@testset "LU decomposition" begin
44
# Square case
55
m22 = @SMatrix [1 2; 3 4]
6-
@inferred(lu(m22))
6+
@test @inferred(lu(m22)) isa Tuple{LowerTriangular{Float64,SMatrix{2,2,Float64,4}}, UpperTriangular{Float64,SMatrix{2,2,Float64,4}}, SVector{2,Int}}
77
@test lu(m22)[1]::LowerTriangular{<:Any,<:StaticMatrix} lu(Matrix(m22))[1]
88
@test lu(m22)[2]::UpperTriangular{<:Any,<:StaticMatrix} lu(Matrix(m22))[2]
99
@test lu(m22)[3]::StaticVector lu(Matrix(m22))[3]
1010

1111
# Rectangular case
1212
m23 = @SMatrix Float64[3 9 4; 6 6 2]
13-
@inferred lu(m23)
13+
@test @inferred(lu(m23)) isa Tuple{SMatrix{2,2,Float64,4}, SMatrix{2,3,Float64,6}, SVector{2,Int}}
1414
@test lu(m23)[1] lu(Matrix(m23))[1]
1515
@test lu(m23)[2] lu(Matrix(m23))[2]
1616
@test lu(m23)[3] lu(Matrix(m23))[3]

0 commit comments

Comments
 (0)