Skip to content

Commit 16d1dff

Browse files
authored
Test periodic Schrodinger with QL (#168)
* Test periodic Schrodinger with QL * Update infql.jl * ql for b-∞ periodic
1 parent 4209d46 commit 16d1dff

File tree

3 files changed

+76
-43
lines changed

3 files changed

+76
-43
lines changed

src/infql.jl

+14-7
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,16 @@ end
165165

166166
function blocktailiterate(c,a,b, d=c, e=a)
167167
z = zero(c)
168+
n = size(c,1)
168169
for _=1:1_000_000
169170
X = [c a b; z d e]
170171
F = ql!(X)
171-
d̃,ẽ = F.L[1:2,1:2], F.L[1:2,3:4]
172+
d̃,ẽ = F.L[1:n,1:n], F.L[1:n,n+1:2n]
172173

173-
d̃,ẽ = QLPackedQ(F.factors[1:2,3:4],F.τ[1:2])*d̃,QLPackedQ(F.factors[1:2,3:4],F.τ[1:2])*# undo last rotation
174+
d̃,ẽ = QLPackedQ(F.factors[1:n,n+1:2n],F.τ[1:n])*d̃,QLPackedQ(F.factors[1:n,n+1:2n],F.τ[1:n])*# undo last rotation
174175
if (d̃, d; atol=1E-10) && (ẽ, e; atol=1E-10)
175-
X[1:2,1:2] = d̃; X[1:2,3:4] =
176-
return PseudoBlockArray(X,fill(2,2), fill(2,3)), F.τ[3:end]
176+
X[1:n,1:n] = d̃; X[1:n,n+1:2n] =
177+
return PseudoBlockArray(X,fill(n,2), fill(n,3)), F.τ[n+1:2n]
177178
end
178179
d,e = d̃,ẽ
179180
end
@@ -191,8 +192,8 @@ function _blocktripert_ql(A, d, e)
191192
P,τ = blocktailiterate(c,a,b,d,e)
192193
B = BlockBandedMatrix(A,(2,1))
193194

194-
195-
BB = _BlockBandedMatrix(B.data.args[1], fill(2,N+2), fill(2,N), (2,1))
195+
n = size(c,1)
196+
BB = _BlockBandedMatrix(B.data.args[1], fill(n,N+2), fill(n,N), (2,1))
196197
BB[Block(N),Block.(N-1:N)] .= P[Block(1), Block.(1:2)]
197198
F = ql!(view(BB, Block.(1:N), Block.(1:N)))
198199
BB[Block(N+1),Block.(N-1:N)] .= P[Block(2), Block.(1:2)]
@@ -241,7 +242,7 @@ function lmul!(adjA::AdjointQtype{<:Any,<:QLPackedQ{<:Any,<:InfBlockBandedMatrix
241242
B
242243
end
243244

244-
getindex(Q::QLPackedQ{T,<:InfBlockBandedMatrix{T}}, i::Integer, j::Integer) where T =
245+
getindex(Q::QLPackedQ{T,<:InfBlockBandedMatrix{T}}, i::Int, j::Int) where T =
245246
(Q'*Vcat(Zeros{T}(i-1), one(T), Zeros{T}(∞)))[j]'
246247
getindex(Q::QLPackedQ{<:Any,<:InfBlockBandedMatrix}, I::AbstractVector{Int}, J::AbstractVector{Int}) =
247248
[Q[i,j] for i in I, j in J]
@@ -256,6 +257,12 @@ function (*)(A::AdjointQtype{T,<:QLPackedQ{T,<:InfBlockBandedMatrix}}, x::Abstra
256257
lmul!(A, cache(convert(AbstractVector{TS},x)))
257258
end
258259

260+
function (*)(A::AdjointQtype{T,<:QLPackedQ{T,<:InfBlockBandedMatrix}}, x::LayoutVector{S}) where {T,S}
261+
TS = promote_op(matprod, T, S)
262+
lmul!(A, cache(convert(AbstractVector{TS},x)))
263+
end
264+
265+
259266
ldiv!(F::QLProduct, b::AbstractVector) = ldiv!(F.L, lmul!(F.Q',b))
260267
ldiv!(F::QLProduct, b::LayoutVector) = ldiv!(F.L, lmul!(F.Q',b))
261268

test/runtests.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,5 @@ include("test_infql.jl")
472472
include("test_infqr.jl")
473473
include("test_inful.jl")
474474
include("test_infcholesky.jl")
475-
include("test_infreversecholesky.jl")
475+
include("test_periodic.jl")
476+
include("test_infreversecholesky.jl")

test/test_periodic.jl

+60-35
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,62 @@
1+
using InfiniteLinearAlgebra, BlockBandedMatrices, LinearAlgebra, Test
2+
13
@testset "periodic" begin
2-
A = BlockTridiagonal(Vcat([[0. 1.; 0. 0.]],Fill([0. 1.; 0. 0.], ∞)),
3-
Vcat([[-1. 1.; 1. 1.]], Fill([-1. 1.; 1. 1.], ∞)),
4-
Vcat([[0. 0.; 1. 0.]], Fill([0. 0.; 1. 0.], ∞)))
5-
6-
7-
Q,L = ql(A);
8-
@test Q.factors isa InfiniteLinearAlgebra.InfBlockBandedMatrix
9-
Q̃,L̃ = ql(BlockBandedMatrix(A)[Block.(1:100),Block.(1:100)])
10-
11-
@test.factors[1:100,1:100] Q.factors[1:100,1:100]
12-
@test.τ[1:100] Q.τ[1:100]
13-
@test L[1:100,1:100] L̃[1:100,1:100]
14-
@test Q[1:10,1:10] Q̃[1:10,1:10]
15-
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
16-
17-
# complex non-selfadjoint
18-
c,a,b = [0 0.5; 0 0],[0 2.0; 0.5 0],[0 0.0; 2.0 0];
19-
A = BlockTridiagonal(Vcat([c], Fill(c,∞)),
20-
Vcat([a], Fill(a,∞)),
21-
Vcat([b], Fill(b,∞))) - 5im*I
22-
Q,L = ql(A)
23-
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
24-
25-
c,a,b = [0 0.5; 0 0],[0 2.0; 0.5 0],[0 0.0; 2.0 0];
26-
A = BlockTridiagonal(Vcat([c], Fill(c,∞)),
27-
Vcat([a], Fill(a,∞)),
28-
Vcat([b], Fill(b,∞)))
29-
Q,L = ql(A)
30-
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
31-
@test L[1,1] == 0 # degenerate
32-
33-
34-
Q,L = ql(A')
35-
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]'
36-
@test L[1,1] 0 # non-degenerate
4+
@testset "single-∞" begin
5+
A = BlockTridiagonal(Vcat([[0. 1.; 0. 0.]],Fill([0. 1.; 0. 0.], ∞)),
6+
Vcat([[-1. 1.; 1. 1.]], Fill([-1. 1.; 1. 1.], ∞)),
7+
Vcat([[0. 0.; 1. 0.]], Fill([0. 0.; 1. 0.], ∞)))
8+
9+
10+
Q,L = ql(A);
11+
@test Q.factors isa InfiniteLinearAlgebra.InfBlockBandedMatrix
12+
Q̃,L̃ = ql(BlockBandedMatrix(A)[Block.(1:100),Block.(1:100)])
13+
14+
@test.factors[1:100,1:100] Q.factors[1:100,1:100]
15+
@test.τ[1:100] Q.τ[1:100]
16+
@test L[1:100,1:100] L̃[1:100,1:100]
17+
@test Q[1:10,1:10] Q̃[1:10,1:10]
18+
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
19+
20+
# complex non-selfadjoint
21+
c,a,b = [0 0.5; 0 0],[0 2.0; 0.5 0],[0 0.0; 2.0 0];
22+
A = BlockTridiagonal(Vcat([c], Fill(c,∞)),
23+
Vcat([a], Fill(a,∞)),
24+
Vcat([b], Fill(b,∞))) - 5im*I
25+
Q,L = ql(A)
26+
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
27+
28+
c,a,b = [0 0.5; 0 0],[0 2.0; 0.5 0],[0 0.0; 2.0 0];
29+
A = BlockTridiagonal(Vcat([c], Fill(c,∞)),
30+
Vcat([a], Fill(a,∞)),
31+
Vcat([b], Fill(b,∞)))
32+
Q,L = ql(A)
33+
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
34+
@test abs(L[1,1] ) 1E-11 # degenerate
35+
36+
37+
Q,L = ql(A')
38+
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]'
39+
@test L[1,1] 0 # non-degenerate
40+
end
41+
42+
@testset "bi" begin
43+
B = [ 0 0 1 0;
44+
0 0 0 1;
45+
0 0 0 0;
46+
0 0 0 0]/2
47+
A₀ = [ 1 1/2 1/2 0 ;
48+
1/2 -1 0 1/2;
49+
1/2 0 -1 0 ;
50+
0 1/2 0 1]
51+
A = [ 1 0 1/2 0 ;
52+
0 -1 0 1/2 ;
53+
1/2 0 -1 0 ;
54+
0 1/2 0 1 ]
55+
56+
A = BlockTridiagonal(Vcat([B],Fill(B, ∞)),
57+
Vcat([A₀], Fill(A, ∞)),
58+
Vcat([copy(B')], Fill(copy(B'), ∞)))
59+
Q,L = ql(A)
60+
@test Q[1:10,1:12]*L[1:12,1:10] A[1:10,1:10]
61+
end
3762
end

0 commit comments

Comments
 (0)