|
1 | 1 | using StaticArrays, Base.Test
|
2 | 2 |
|
3 |
| -@testset "LU decomposition" begin |
4 |
| - # Square case |
5 |
| - m22 = @SMatrix [1 2; 3 4] |
6 |
| - @test @inferred(lu(m22)) isa Tuple{LowerTriangular{Float64,SMatrix{2,2,Float64,4}}, UpperTriangular{Float64,SMatrix{2,2,Float64,4}}, SVector{2,Int}} |
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] |
| 3 | +@testset "LU decomposition (pivot=$pivot)" for pivot in (true, false) |
| 4 | + @testset "$m×$n" for m in 0:4, n in 0:4 |
| 5 | + a = SMatrix{m,n,Int}(1:(m*n)) |
| 6 | + l, u, p = @inferred(lu(a, Val{pivot})) |
10 | 7 |
|
11 |
| - # Rectangular case |
12 |
| - m23 = @SMatrix Float64[3 9 4; 6 6 2] |
13 |
| - @test @inferred(lu(m23)) isa Tuple{SMatrix{2,2,Float64,4}, SMatrix{2,3,Float64,6}, SVector{2,Int}} |
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] |
| 8 | + # expected types |
| 9 | + @test p isa SVector{m,Int} |
| 10 | + if m==n |
| 11 | + @test l isa LowerTriangular{<:Any,<:SMatrix{m,n}} |
| 12 | + @test u isa UpperTriangular{<:Any,<:SMatrix{m,n}} |
| 13 | + else |
| 14 | + @test l isa SMatrix{m,min(m,n)} |
| 15 | + @test u isa SMatrix{min(m,n),n} |
| 16 | + end |
17 | 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] |
| 18 | + if pivot |
| 19 | + # p is a permutation |
| 20 | + @test sort(p) == collect(1:m) |
| 21 | + else |
| 22 | + @test p == collect(1:m) |
| 23 | + end |
| 24 | + |
| 25 | + # l is unit lower triangular |
| 26 | + for i=1:m, j=(i+1):size(l,2) |
| 27 | + @test iszero(l[i,j]) |
| 28 | + end |
| 29 | + for i=1:size(l,2) |
| 30 | + @test l[i,i] == 1 |
| 31 | + end |
| 32 | + |
| 33 | + # u is upper triangular |
| 34 | + for i=1:size(u,1), j=1:i-1 |
| 35 | + @test iszero(u[i,j]) |
| 36 | + end |
| 37 | + |
| 38 | + # decomposition is correct |
| 39 | + @test l*u ≈ a[p,:] |
| 40 | + end |
21 | 41 | end
|
0 commit comments