22
33module TestTriangular
44
5- debug = false
65using Test, LinearAlgebra, Random
76using LinearAlgebra: BlasFloat, errorbounds, full!, transpose!,
87 UnitUpperTriangular, UnitLowerTriangular,
@@ -16,14 +15,13 @@ using .Main.SizedArrays
1615isdefined (Main, :FillArrays ) || @eval Main include (joinpath ($ (BASE_TEST_PATH), " testhelpers" , " FillArrays.jl" ))
1716using . Main. FillArrays
1817
19- debug && println (" Triangular matrices" )
20-
2118n = 9
2219Random. seed! (123 )
2320
24- debug && println (" Test basic type functionality" )
25- @test_throws DimensionMismatch LowerTriangular (randn (5 , 4 ))
26- @test LowerTriangular (randn (3 , 3 )) |> t -> [size (t, i) for i = 1 : 3 ] == [size (Matrix (t), i) for i = 1 : 3 ]
21+ @testset " Test basic type functionality" begin
22+ @test_throws DimensionMismatch LowerTriangular (randn (5 , 4 ))
23+ @test LowerTriangular (randn (3 , 3 )) |> t -> [size (t, i) for i = 1 : 3 ] == [size (Matrix (t), i) for i = 1 : 3 ]
24+ end
2725
2826struct MyTriangular{T, A<: LinearAlgebra.AbstractTriangular{T} } <: LinearAlgebra.AbstractTriangular{T}
2927 data :: A
@@ -51,8 +49,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
5149 t1t = t1 {elty1} (t1 (rand (Int8, n, n)))
5250 @test typeof (t1t) == t1{elty1, Matrix{elty1}}
5351
54- debug && println (" elty1: $elty1 , A1: $t1 " )
55-
5652 # Convert
5753 @test convert (AbstractMatrix{elty1}, A1) == A1
5854 @test convert (Matrix, A1) == A1
@@ -356,8 +352,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
356352 (LowerTriangular, :L ),
357353 (UnitLowerTriangular, :L ))
358354
359- debug && println (" elty1: $elty1 , A1: $t1 , elty2: $elty2 , A2: $t2 " )
360-
361355 A2 = t2 (elty2 == Int ? rand (1 : 7 , n, n) : convert (Matrix{elty2}, (elty2 <: Complex ? complex .(randn (n, n), randn (n, n)) : randn (n, n)) |> t -> cholesky (t' t). U |> t -> uplo2 === :U ? t : copy (t' )))
362356 M2 = Matrix (A2)
363357 # Convert
@@ -451,8 +445,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
451445
452446 B = convert (Matrix{eltyB}, (elty1 <: Complex ? real (A1) : A1)* fill (1. , n, n))
453447
454- debug && println (" elty1: $elty1 , A1: $t1 , B: $eltyB " )
455-
456448 Tri = Tridiagonal (rand (eltyB,n- 1 ),rand (eltyB,n),rand (eltyB,n- 1 ))
457449 C = Matrix {promote_type(elty1,eltyB)} (undef, n, n)
458450 mul! (C, Tri, A1)
@@ -639,83 +631,80 @@ Aimg = randn(n, n)/2
639631A2real = randn (n, n)/ 2
640632A2img = randn (n, n)/ 2
641633
642- for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int)
634+ @testset for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int)
643635 A = eltya == Int ? rand (1 : 7 , n, n) : convert (Matrix{eltya}, eltya <: Complex ? complex .(Areal, Aimg) : Areal)
644- # a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(a2real, a2img) : a2real)
645636 εa = eps (abs (float (one (eltya))))
646637
647638 for eltyb in (Float32, Float64, ComplexF32, ComplexF64)
648639 εb = eps (abs (float (one (eltyb))))
649640 ε = max (εa,εb)
650641
651- debug && println (" \n type of A: " , eltya, " type of b: " , eltyb, " \n " )
642+ @testset " Solve upper triangular system" begin
643+ Atri = UpperTriangular (lu (A). U) |> t -> eltya <: Complex && eltyb <: Real ? real (t) : t # Here the triangular matrix can't be too badly conditioned
644+ b = convert (Matrix{eltyb}, Matrix (Atri)* fill (1. , n, 2 ))
645+ x = Atri \ b
652646
653- debug && println (" Solve upper triangular system" )
654- Atri = UpperTriangular (lu (A). U) |> t -> eltya <: Complex && eltyb <: Real ? real (t) : t # Here the triangular matrix can't be too badly conditioned
655- b = convert (Matrix{eltyb}, Matrix (Atri)* fill (1. , n, 2 ))
656- x = Matrix (Atri) \ b
647+ # Test error estimates
648+ if eltya != BigFloat && eltyb != BigFloat
649+ for i = 1 : 2
650+ @test norm (x[:,1 ] .- 1 ) <= errorbounds (UpperTriangular (A), x, b)[1 ][i]
651+ end
652+ end
657653
658- debug && println (" Test error estimates" )
659- if eltya != BigFloat && eltyb != BigFloat
660- for i = 1 : 2
661- @test norm (x[:,1 ] .- 1 ) <= errorbounds (UpperTriangular (A), x, b)[1 ][i]
654+ # Test forward error [JIN 5705] if this is not a BigFloat
655+ γ = n* ε/ (1 - n* ε)
656+ if eltya != BigFloat
657+ bigA = big .(Atri)
658+ x̂ = fill (1. , n, 2 )
659+ for i = 1 : size (b, 2 )
660+ @test norm (x̂[:,i] - x[:,i], Inf )/ norm (x̂[:,i], Inf ) <= condskeel (bigA, x̂[:,i])* γ/ (1 - condskeel (bigA)* γ)
661+ end
662662 end
663- end
664- debug && println (" Test forward error [JIN 5705] if this is not a BigFloat" )
665663
666- x = Atri \ b
667- γ = n* ε/ (1 - n* ε)
668- if eltya != BigFloat
669- bigA = big .(Atri)
670- x̂ = fill (1. , n, 2 )
664+ # Test backward error [JIN 5705]
671665 for i = 1 : size (b, 2 )
672- @test norm (x̂ [:,i] - x[:,i], Inf )/ norm (x̂[:,i], Inf ) <= condskeel (bigA, x̂ [:,i]) * γ / ( 1 - condskeel (bigA) * γ )
666+ @test norm (abs .(b [:,i] - Atri * x[:,i]) , Inf ) <= γ * norm (Atri, Inf ) * norm (x [:,i], Inf )
673667 end
674668 end
675669
676- debug && println ( " Test backward error [JIN 5705] " )
677- for i = 1 : size (b, 2 )
678- @test norm ( abs .(b[:,i] - Atri * x[:,i]), Inf ) <= γ * norm (Atri, Inf ) * norm (x[:,i], Inf )
679- end
670+ @testset " Solve lower triangular system " begin
671+ Atri = LowerTriangular ( lu (A) . L) |> t -> eltya <: Complex && eltyb <: Real ? real (t) : t # Here the triangular matrix can't be too badly conditioned
672+ b = convert (Matrix{eltyb}, Matrix (Atri) * fill ( 1. , n, 2 ) )
673+ x = Atri \ b
680674
681- debug && println (" Solve lower triangular system" )
682- Atri = UpperTriangular (lu (A). U) |> t -> eltya <: Complex && eltyb <: Real ? real (t) : t # Here the triangular matrix can't be too badly conditioned
683- b = convert (Matrix{eltyb}, Matrix (Atri)* fill (1. , n, 2 ))
684- x = Matrix (Atri)\ b
675+ # Test error estimates
676+ if eltya != BigFloat && eltyb != BigFloat
677+ for i = 1 : 2
678+ @test norm (x[:,1 ] .- 1 ) <= errorbounds (LowerTriangular (A), x, b)[1 ][i]
679+ end
680+ end
685681
686- debug && println (" Test error estimates" )
687- if eltya != BigFloat && eltyb != BigFloat
688- for i = 1 : 2
689- @test norm (x[:,1 ] .- 1 ) <= errorbounds (UpperTriangular (A), x, b)[1 ][i]
682+ # Test forward error [JIN 5705] if this is not a BigFloat
683+ γ = n* ε/ (1 - n* ε)
684+ if eltya != BigFloat
685+ bigA = big .(Atri)
686+ x̂ = fill (1. , n, 2 )
687+ for i = 1 : size (b, 2 )
688+ @test norm (x̂[:,i] - x[:,i], Inf )/ norm (x̂[:,i], Inf ) <= condskeel (bigA, x̂[:,i])* γ/ (1 - condskeel (bigA)* γ)
689+ end
690690 end
691- end
692691
693- debug && println (" Test forward error [JIN 5705] if this is not a BigFloat" )
694- b = (b0 = Atri* fill (1 , n, 2 ); convert (Matrix{eltyb}, eltyb == Int ? trunc .(b0) : b0))
695- x = Atri \ b
696- γ = n* ε/ (1 - n* ε)
697- if eltya != BigFloat
698- bigA = big .(Atri)
699- x̂ = fill (1. , n, 2 )
692+ # Test backward error [JIN 5705]
700693 for i = 1 : size (b, 2 )
701- @test norm (x̂ [:,i] - x[:,i], Inf )/ norm (x̂[:,i], Inf ) <= condskeel (bigA, x̂ [:,i]) * γ / ( 1 - condskeel (bigA) * γ )
694+ @test norm (abs .(b [:,i] - Atri * x[:,i]) , Inf ) <= γ * norm (Atri, Inf ) * norm (x [:,i], Inf )
702695 end
703696 end
704-
705- debug && println (" Test backward error [JIN 5705]" )
706- for i = 1 : size (b, 2 )
707- @test norm (abs .(b[:,i] - Atri* x[:,i]), Inf ) <= γ * norm (Atri, Inf ) * norm (x[:,i], Inf )
708- end
709697 end
710698end
711699
712- # Issue 10742 and similar
713- @test istril (UpperTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
714- @test istriu (LowerTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
715- @test isdiag (UpperTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
716- @test isdiag (LowerTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
717- @test ! isdiag (UpperTriangular (rand (4 , 4 )))
718- @test ! isdiag (LowerTriangular (rand (4 , 4 )))
700+ @testset " triangularity/diagonality of triangular views (#10742)" begin
701+ @test istril (UpperTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
702+ @test istriu (LowerTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
703+ @test isdiag (UpperTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
704+ @test isdiag (LowerTriangular (diagm (0 => [1 ,2 ,3 ,4 ])))
705+ @test ! isdiag (UpperTriangular (rand (4 , 4 )))
706+ @test ! isdiag (LowerTriangular (rand (4 , 4 )))
707+ end
719708
720709# Test throwing in fallbacks for non BlasFloat/BlasComplex in A_rdiv_Bx!
721710let n = 5
0 commit comments