Skip to content

Commit 8139d32

Browse files
committed
LU decomposition via Base
For now, a simple implementation which calls Base.lu, nothing fancy - just preserve the static sizes correctly in the output.
1 parent d733a53 commit 8139d32

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

src/StaticArrays.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Base: getindex, setindex!, size, similar, vec, show,
99
mapreduce, broadcast, broadcast!, conj, transpose, ctranspose,
1010
hcat, vcat, ones, zeros, eye, one, cross, vecdot, reshape, fill,
1111
fill!, det, inv, eig, eigvals, expm, sqrtm, trace, vecnorm, norm, dot, diagm, diag,
12-
svd, svdvals!, svdfact,
12+
lu, svd, svdvals!, svdfact,
1313
sum, diff, prod, count, any, all, minimum,
1414
maximum, extrema, mean, copy, rand, randn, randexp, rand!, randn!,
1515
randexp!, normalize, normalize!, read, read!, write
@@ -94,9 +94,10 @@ include("eigen.jl")
9494
include("expm.jl")
9595
include("sqrtm.jl")
9696
include("cholesky.jl")
97+
include("svd.jl")
98+
include("lu.jl")
9799
include("deque.jl")
98100
include("io.jl")
99-
include("svd.jl")
100101

101102
include("FixedSizeArrays.jl")
102103
include("ImmutableArrays.jl")

src/linalg.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,3 @@ end
324324
@inline Base.LinAlg.Symmetric(A::StaticMatrix, uplo::Char='U') = (Base.LinAlg.checksquare(A);Symmetric{eltype(A),typeof(A)}(A, uplo))
325325
@inline Base.LinAlg.Hermitian(A::StaticMatrix, uplo::Char='U') = (Base.LinAlg.checksquare(A);Hermitian{eltype(A),typeof(A)}(A, uplo))
326326

327-
328-
#-------------------------------------------------------------------------------
329-
# Wrappers for various matrix decompositions which correctly propagate the
330-
# StaticMatrix type in the returned factorization.
331-
332-
333-
#--------------------------------------------------
334-
# lu
335-
# TODO
336-
337-
338-
#--------------------------------------------------
339-
# qr
340-
# TODO
341-
342-
#--------------------------------------------------

src/lu.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# LU decomposition
2+
function lu(A::StaticMatrix, pivot::Union{Type{Val{false}},Type{Val{true}}}=Val{true})
3+
L,U,p = _lu(A, pivot)
4+
(L,U,p)
5+
end
6+
7+
# For the square version, return explicit lower and upper triangular matrices.
8+
# We would do this for the rectangular case too, but Base doesn't support that.
9+
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)
11+
(LowerTriangular(L), UpperTriangular(U), p)
12+
end
13+
14+
15+
@inline function _lu(A::StaticMatrix, pivot)
16+
# For now, just call through to Base.
17+
# TODO: statically sized LU without allocations!
18+
f = lufact(Matrix(A), pivot)
19+
T = eltype(A)
20+
T2 = typeof((one(T)*zero(T) + zero(T))/one(T))
21+
L = similar_type(A, T2, Size(Size(A)[1], diagsize(A)))(f[:L])
22+
U = similar_type(A, T2, Size(diagsize(A), Size(A)[2]))(f[:U])
23+
p = similar_type(A, Size(Size(A)[1]))(f[:p])
24+
(L,U,p)
25+
end
26+
27+
# Base.lufact() interface is fairly inherently type unstable. Punt on
28+
# implementing that, for now...

test/lu.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using StaticArrays, Base.Test
2+
3+
@testset "LU decomposition" begin
4+
# Square case
5+
m22 = @SMatrix [1 2; 3 4]
6+
@inferred(lu(m22))
7+
@test lu(m22)[1]::LowerTriangular{<:Any,<:StaticMatrix} lu(Matrix(m22))[1]
8+
@test lu(m22)[2]::UpperTriangular{<:Any,<:StaticMatrix} lu(Matrix(m22))[2]
9+
@test lu(m22)[3]::StaticVector lu(Matrix(m22))[3]
10+
11+
# Rectangular case
12+
m23 = @SMatrix Float64[3 9 4; 6 6 2]
13+
@inferred lu(m23)
14+
@test lu(m23)[1] lu(Matrix(m23))[1]
15+
@test lu(m23)[2] lu(Matrix(m23))[2]
16+
@test lu(m23)[3] lu(Matrix(m23))[3]
17+
18+
@test lu(m23')[1] lu(Matrix(m23'))[1]
19+
@test lu(m23')[2] lu(Matrix(m23'))[2]
20+
@test lu(m23')[3] lu(Matrix(m23'))[3]
21+
end

0 commit comments

Comments
 (0)