1+ using InfiniteArrays, BlockArrays, LazyArrays, Test
2+ using InfiniteArrays: OneToInf, RealInfinity
3+ using Base: oneto
4+ using LazyArrays: LazyArrayStyle
5+ using BlockArrays: BlockTridiagonal
6+
7+ const InfiniteArraysBlockArraysExt = Base. get_extension (InfiniteArrays, :InfiniteArraysBlockArraysExt )
8+
9+
10+
11+
12+ @testset " ∞-block arrays" begin
13+ @testset " blockedonetoinf" begin
14+ b = blockedrange (OneToInf ())
15+ b2 = b .+ b;
16+ for i in 1 : 10
17+ @test b2[Block (i)] == b[Block (i)] + b[Block (i)]
18+ end
19+ end
20+
21+ @testset " fixed block size" begin
22+ k = Base. OneTo .(oneto (∞))
23+ n = Fill .(oneto (∞), oneto (∞))
24+ @test broadcast (length, k) ≡ map (length, k) ≡ OneToInf ()
25+ @test broadcast (length, n) ≡ map (length, n) ≡ OneToInf ()
26+
27+ b = mortar (Fill ([1 , 2 ], ∞))
28+ @test blockaxes (b, 1 ) ≡ Block .(OneToInf ())
29+ @test b[Block (5 )] == [1 , 2 ]
30+ @test b[Block .(2 : ∞)][Block .(2 : 10 )] == b[Block .(3 : 11 )]
31+ @test exp .(b)[Block .(2 : ∞)][Block .(2 : 10 )] == exp .(b[Block .(3 : 11 )])
32+
33+ @test blockedrange (Vcat (2 , Fill (3 , ∞))) isa BlockedOneTo{<: Any ,<: InfiniteArrays.InfStepRange }
34+
35+ c = BlockedArray (1 : ∞, Vcat (2 , Fill (3 , ∞)))
36+ @test c[Block .(2 : ∞)][Block .(2 : 10 )] == c[Block .(3 : 11 )]
37+
38+ @test length (axes (b, 1 )) ≡ ℵ₀
39+ @test last (axes (b, 1 )) ≡ ℵ₀
40+ @test Base. BroadcastStyle (typeof (b)) isa LazyArrayStyle{1 }
41+ end
42+
43+ @testset " 1:∞ blocks" begin
44+ a = blockedrange (oneto (∞))
45+ @test axes (a, 1 ) == a
46+ o = Ones ((a,))
47+ @test Base. BroadcastStyle (typeof (a)) isa LazyArrayStyle{1 }
48+ b = exp .(a)
49+ @test axes (b, 1 ) == a
50+ @test o .* b isa typeof (b)
51+ @test b .* o isa typeof (b)
52+ end
53+
54+ @testset " padded" begin
55+ c = BlockedArray ([1 ; zeros (∞)], Vcat (2 , Fill (3 , ∞)))
56+ @test c + c isa BlockedVector
57+ end
58+
59+
60+ @testset " triangle recurrences" begin
61+ @testset " n and k" begin
62+ n = mortar (Fill .(oneto (∞), oneto (∞)))
63+ k = mortar (Base. OneTo .(oneto (∞)))
64+
65+ @test n[Block (5 )] ≡ layout_getindex (n, Block (5 )) ≡ view (n, Block (5 )) ≡ Fill (5 , 5 )
66+ @test k[Block (5 )] ≡ layout_getindex (k, Block (5 )) ≡ view (k, Block (5 )) ≡ Base. OneTo (5 )
67+ @test Base. BroadcastStyle (typeof (n)) isa LazyArrays. LazyArrayStyle{1 }
68+ @test Base. BroadcastStyle (typeof (k)) isa LazyArrays. LazyArrayStyle{1 }
69+
70+ N = 1000
71+ v = view (n, Block .(Base. OneTo (N)))
72+ @test view (v, Block (2 )) ≡ Fill (2 , 2 )
73+ @test axes (v) isa Tuple{BlockedOneTo{Int,ArrayLayouts. RangeCumsum{Int64,Base. OneTo{Int64}}}}
74+ @test @allocated (axes (v)) ≤ 40
75+
76+ dest = BlockedArray {Float64} (undef, axes (v))
77+ @test copyto! (dest, v) == v
78+ @test @allocated (copyto! (dest, v)) ≤ 40
79+
80+ v = view (k, Block .(Base. OneTo (N)))
81+ @test view (v, Block (2 )) ≡ Base. OneTo (2 )
82+ @test axes (v) isa Tuple{BlockedOneTo{Int,ArrayLayouts. RangeCumsum{Int64,Base. OneTo{Int64}}}}
83+ @test @allocated (axes (v)) ≤ 40
84+ @test copyto! (dest, v) == v
85+
86+ @testset " stack overflow" begin
87+ i = Base. to_indices (k, (Block .(2 : ∞),))[1 ]. indices
88+ @test last (i) == ℵ₀
89+ end
90+
91+ v = view (k, Block .(2 : ∞))
92+ @test Base. BroadcastStyle (typeof (v)) isa LazyArrayStyle{1 }
93+ @test v[Block (1 )] == 1 : 2
94+ @test v[Block (1 )] ≡ k[Block (2 )] ≡ Base. OneTo (2 )
95+
96+ @test axes (n, 1 ) isa BlockedOneTo{Int,ArrayLayouts. RangeCumsum{Int64,OneToInf{Int64}}}
97+ end
98+ end
99+
100+ @testset " blockdiag" begin
101+ D = Diagonal (mortar (Fill .((- (0 : ∞) - (0 : ∞) .^ 2 ), 1 : 2 : ∞)))
102+ x = [randn (5 ); zeros (∞)]
103+ x̃ = BlockedArray (x, (axes (D, 1 ),))
104+ @test (D* x)[1 : 10 ] == (D* x̃)[1 : 10 ]
105+ end
106+
107+ @testset " sortedunion" begin
108+ a = cumsum (1 : 2 : ∞)
109+ @test BlockArrays. sortedunion (a, a) ≡ a
110+ @test BlockArrays. sortedunion ([∞], a) ≡ BlockArrays. sortedunion (a, [∞]) ≡ a
111+ @test BlockArrays. sortedunion ([∞], [∞]) == [∞]
112+
113+ b = Vcat ([1 , 2 ], 3 : ∞)
114+ c = Vcat (1 , 3 : ∞)
115+ @test BlockArrays. sortedunion (b, b) ≡ b
116+ @test BlockArrays. sortedunion (c, c) ≡ c
117+ end
118+
119+ @testset " Algebra" begin
120+ @testset " Triangle OP recurrences" begin
121+ k = mortar (Base. OneTo .(1 : ∞))
122+ n = mortar (Fill .(1 : ∞, 1 : ∞))
123+ @test k[Block .(2 : 3 )] isa BlockArray
124+ @test n[Block .(2 : 3 )] isa BlockArray
125+ @test k[Block .(2 : 3 )] == [1 , 2 , 1 , 2 , 3 ]
126+ @test n[Block .(2 : 3 )] == [2 , 2 , 3 , 3 , 3 ]
127+ @test blocksize (BroadcastVector (exp, k)) == (ℵ₀,)
128+ @test BroadcastVector (exp, k)[Block .(2 : 3 )] == exp .([1 , 2 , 1 , 2 , 3 ])
129+ # BroadcastVector(+,k,n)
130+ end
131+ # Multivariate OPs Corollary (3)
132+ # n = 5
133+ # BlockTridiagonal(Zeros.(1:∞,2:∞),
134+ # (n -> Diagonal(((n+2).+(0:n)))/ (2n + 2)).(0:∞),
135+ # Zeros.(2:∞,1:∞))
136+ end
137+
138+ @testset " findblock at +∞, HarmonicOrthogonalPolynomials#88" begin
139+ @test findblock (blockedrange (1 : 2 : ∞), RealInfinity ()) == Block (ℵ₀)
140+ end
141+ end
0 commit comments