@@ -47,13 +47,20 @@ import BandedMatrices: MemoryLayout, SymmetricLayout, HermitianLayout, BandedCol
4747 # (generalized) eigen & eigvals
4848 Random. seed! (0 )
4949
50- A = brand (Float64, 100 , 100 , 2 , 4 )
51- std = eigvals (Symmetric (Matrix (A)))
52- @test eigvals (Symmetric (A)) ≈ std
53- @test eigvals (Hermitian (A)) ≈ std
54- @test eigvals (Hermitian (big .(A))) ≈ std
50+ @testset for T in (Float32, Float64)
51+ A = brand (T, 10 , 10 , 2 , 4 )
52+ std = eigvals (Symmetric (Matrix (A)))
53+ @test eigvals (Symmetric (A)) ≈ std
54+ @test eigvals (Symmetric (A), 2 : 4 ) ≈ std[2 : 4 ]
55+ @test eigvals (Symmetric (A), 1 , 2 ) ≈ std[1 .<= std .<= 2 ]
56+ @test eigvals! (copy (Symmetric (A))) ≈ std
57+ @test eigvals! (copy (Symmetric (A)), 2 : 4 ) ≈ std[2 : 4 ]
58+ @test eigvals! (copy (Symmetric (A)), 1 , 2 ) ≈ std[1 .<= std .<= 2 ]
59+
60+ @test eigvals (Symmetric (A, :L )) ≈ eigvals (Symmetric (Matrix (A), :L ))
61+ end
5562
56- A = brand (ComplexF64, 100 , 100 , 4 , 0 )
63+ A = brand (ComplexF64, 20 , 20 , 4 , 0 )
5764 @test Symmetric (A)[2 : 10 ,1 : 9 ] isa BandedMatrix
5865 @test Hermitian (A)[2 : 10 ,1 : 9 ] isa BandedMatrix
5966 @test isempty (Hermitian (A)[1 : 0 ,1 : 9 ])
@@ -62,11 +69,7 @@ import BandedMatrices: MemoryLayout, SymmetricLayout, HermitianLayout, BandedCol
6269 @test [Symmetric (A)[k,j] for k= 2 : 10 , j= 1 : 9 ] == Symmetric (A)[2 : 10 ,1 : 9 ]
6370 @test [Hermitian (A)[k,j] for k= 2 : 10 , j= 1 : 9 ] == Hermitian (A)[2 : 10 ,1 : 9 ]
6471
65- std = eigvals (Hermitian (Matrix (A), :L ))
66- @test eigvals (Hermitian (A, :L )) ≈ std
67- @test eigvals (Hermitian (big .(A), :L )) ≈ std
68-
69- A = Symmetric (brand (Float64, 100 , 100 , 2 , 4 ))
72+ A = Symmetric (brand (Float64, 10 , 10 , 2 , 4 ))
7073 F = eigen (A)
7174 Λ, Q = F
7275 @test Q' Matrix (A)* Q ≈ Diagonal (Λ)
@@ -79,6 +82,16 @@ import BandedMatrices: MemoryLayout, SymmetricLayout, HermitianLayout, BandedCol
7982 @test Q[:,3 ] ≈ MQ[:,3 ]
8083 @test Q[1 ,2 ] ≈ MQ[1 ,2 ]
8184
85+ F = eigen (A, 2 : 4 )
86+ Λ, Q = F
87+ QM = Matrix (Q)
88+ @test QM' * (Matrix (A)* QM) ≈ Diagonal (Λ)
89+
90+ F = eigen (A, 1 , 2 )
91+ Λ, Q = F
92+ QM = Matrix (Q)
93+ @test QM' * (Matrix (A)* QM) ≈ Diagonal (Λ)
94+
8295 function An (:: Type{T} , N:: Int ) where {T}
8396 A = Symmetric (BandedMatrix (Zeros {T} (N,N), (0 , 2 )))
8497 for n = 0 : N- 1
177190 @test all (A* x .=== muladd! (one (T),A,x,zero (T),copy (x)) .===
178191 materialize! (MulAdd (one (T),A,x,zero (T),copy (x))) .===
179192 BLAS. hbmv! (' L' , 1 , one (T), view (parent (A). data,3 : 4 ,:), x, zero (T), similar (x)))
193+
194+ @testset " eigen" begin
195+ @testset for T in (Float32, Float64, ComplexF64, ComplexF32), uplo in (:L , :U )
196+ A = brand (T, 20 , 20 , 4 , 2 )
197+ std = eigvals (Hermitian (Matrix (A), uplo))
198+ @test eigvals (Hermitian (A, uplo)) ≈ std
199+ @test eigvals (Hermitian (A, uplo), 2 : 4 ) ≈ std[2 : 4 ]
200+ @test eigvals (Hermitian (A, uplo), 1 , 2 ) ≈ std[1 .<= std .<= 2 ]
201+ @test eigvals (Hermitian (big .(A), uplo)) ≈ std
202+ @test eigvals! (copy (Hermitian (A, uplo))) ≈ std
203+ @test eigvals! (copy (Hermitian (A, uplo)), 2 : 4 ) ≈ std[2 : 4 ]
204+ @test eigvals! (copy (Hermitian (A, uplo)), 1 , 2 ) ≈ std[1 .<= std .<= 2 ]
205+ end
206+
207+ @testset for T in (Float32, Float64, ComplexF32, ComplexF64), uplo in (:L , :U )
208+ A = Hermitian (brand (T, 20 , 20 , 2 , 4 ), uplo)
209+ MA = Hermitian (Matrix (A), uplo)
210+ λ1, v1 = eigen! (copy (A))
211+ λ2, v2 = eigen! (copy (MA))
212+ @test λ1 ≈ λ2
213+ @test v1' * MA * v1 ≈ Diagonal (λ1)
214+
215+ λ3, v3 = eigen! (copy (A), 2 : 4 )
216+ @test λ3 ≈ λ1[2 : 4 ]
217+ @test v3' * (MA * v3) ≈ Diagonal (λ3)
218+
219+ λ4, v4 = eigen (A, 2 : 4 )
220+ @test λ4 ≈ λ3
221+ @test v4' * (MA * v4) ≈ Diagonal (λ4)
222+
223+ λ3, v3 = eigen! (copy (A), 1 , 2 )
224+ @test λ3 ≈ λ1[1 .<= λ1 .<= 2 ]
225+ @test v3' * (MA * v3) ≈ Diagonal (λ3)
226+
227+ λ4, v4 = eigen! (copy (A), 1 , 2 )
228+ @test λ4 ≈ λ1[1 .<= λ1 .<= 2 ]
229+ @test v4' * (MA * v4) ≈ Diagonal (λ4)
230+ end
231+ end
180232end
181233
182234@testset " LDLᵀ" begin
0 commit comments