2
2
3
3
module TestTriangular
4
4
5
- debug = false
6
5
using Test, LinearAlgebra, Random
7
6
using LinearAlgebra: BlasFloat, errorbounds, full!, transpose!,
8
7
UnitUpperTriangular, UnitLowerTriangular,
@@ -16,14 +15,13 @@ using .Main.SizedArrays
16
15
isdefined (Main, :FillArrays ) || @eval Main include (joinpath ($ (BASE_TEST_PATH), " testhelpers" , " FillArrays.jl" ))
17
16
using . Main. FillArrays
18
17
19
- debug && println (" Triangular matrices" )
20
-
21
18
n = 9
22
19
Random. seed! (123 )
23
20
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
27
25
28
26
struct MyTriangular{T, A<: LinearAlgebra.AbstractTriangular{T} } <: LinearAlgebra.AbstractTriangular{T}
29
27
data :: A
@@ -51,8 +49,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
51
49
t1t = t1 {elty1} (t1 (rand (Int8, n, n)))
52
50
@test typeof (t1t) == t1{elty1, Matrix{elty1}}
53
51
54
- debug && println (" elty1: $elty1 , A1: $t1 " )
55
-
56
52
# Convert
57
53
@test convert (AbstractMatrix{elty1}, A1) == A1
58
54
@test convert (Matrix, A1) == A1
@@ -356,8 +352,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
356
352
(LowerTriangular, :L ),
357
353
(UnitLowerTriangular, :L ))
358
354
359
- debug && println (" elty1: $elty1 , A1: $t1 , elty2: $elty2 , A2: $t2 " )
360
-
361
355
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' )))
362
356
M2 = Matrix (A2)
363
357
# Convert
@@ -451,8 +445,6 @@ Base.getindex(A::MyTriangular, i::Int, j::Int) = A.data[i,j]
451
445
452
446
B = convert (Matrix{eltyB}, (elty1 <: Complex ? real (A1) : A1)* fill (1. , n, n))
453
447
454
- debug && println (" elty1: $elty1 , A1: $t1 , B: $eltyB " )
455
-
456
448
Tri = Tridiagonal (rand (eltyB,n- 1 ),rand (eltyB,n),rand (eltyB,n- 1 ))
457
449
C = Matrix {promote_type(elty1,eltyB)} (undef, n, n)
458
450
mul! (C, Tri, A1)
@@ -639,83 +631,80 @@ Aimg = randn(n, n)/2
639
631
A2real = randn (n, n)/ 2
640
632
A2img = randn (n, n)/ 2
641
633
642
- for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int)
634
+ @testset for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int)
643
635
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)
645
636
εa = eps (abs (float (one (eltya))))
646
637
647
638
for eltyb in (Float32, Float64, ComplexF32, ComplexF64)
648
639
εb = eps (abs (float (one (eltyb))))
649
640
ε = max (εa,εb)
650
641
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
652
646
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
657
653
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
662
662
end
663
- end
664
- debug && println (" Test forward error [JIN 5705] if this is not a BigFloat" )
665
663
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]
671
665
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 )
673
667
end
674
668
end
675
669
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
680
674
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
685
681
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
690
690
end
691
- end
692
691
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]
700
693
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 )
702
695
end
703
696
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
709
697
end
710
698
end
711
699
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
719
708
720
709
# Test throwing in fallbacks for non BlasFloat/BlasComplex in A_rdiv_Bx!
721
710
let n = 5
0 commit comments