@@ -186,5 +186,130 @@ using LazyArrays: simplifiable
186
186
@testset " SubArray broadcasting" begin
187
187
A = BandedMatrix (2 => 1 : ∞)
188
188
@test exp .(A[1 : 2 : ∞,1 : 2 : ∞])[1 : 10 ,1 : 10 ] ≈ exp .(A[1 : 2 : 20 ,1 : 2 : 20 ])
189
+ @test A[band (2 )][1 : 5 ] == 1 : 5
190
+ @test _BandedMatrix ((1 : ∞)' , ∞, - 1 ,1 )[band (1 )][1 : 5 ] == 2 : 6
191
+ @test exp .(view (A,band (2 )))[1 : 10 ] ≈ exp .(1 : 10 )
192
+
193
+ @test BandedMatrices. banded_similar (Int, (∞,5 ), (1 ,1 )) isa BandedMatrix
194
+ @test BandedMatrices. banded_similar (Int, (5 ,∞), (1 ,1 )) isa Adjoint{<: Any ,<: BandedMatrix }
195
+
196
+ A = BandedMatrix {Int} ((2 => 1 : ∞,), (∞,∞), (0 ,2 ))
197
+ @test eltype (A) == Int
198
+ @test bandwidths (A) == (0 ,2 )
199
+
200
+ A = BandedMatrix {Int} ((2 => Vcat ([1 ,2 ], Fill (2 ,∞)),), (∞,∞), (0 ,2 ))
201
+ @test A[band (2 )][1 : 5 ] == [1 ; fill (2 ,4 )]
189
202
end
203
+
204
+ @testset " Algebra" begin
205
+ A = BandedMatrix (- 3 => Fill (7 / 10 , ∞), - 2 => 1 : ∞, 1 => Fill (2im , ∞))
206
+ @test A isa BandedMatrix{ComplexF64}
207
+ @test A[1 : 10 , 1 : 10 ] == diagm (- 3 => Fill (7 / 10 , 7 ), - 2 => 1 : 8 , 1 => Fill (2im , 9 ))
208
+
209
+ A = BandedMatrix (0 => Vcat ([1 , 2 , 3 ], Zeros (∞)), 1 => Vcat (1 , Zeros (∞)))
210
+ @test A[1 , 2 ] == 1
211
+
212
+ A = BandedMatrix (- 3 => Fill (7 / 10 , ∞), - 2 => Fill (1 , ∞), 1 => Fill (2im , ∞))
213
+ Ac = BandedMatrix (A' )
214
+ At = BandedMatrix (transpose (A))
215
+ @test Ac[1 : 10 , 1 : 10 ] ≈ (A' )[1 : 10 , 1 : 10 ] ≈ A[1 : 10 , 1 : 10 ]'
216
+ @test At[1 : 10 , 1 : 10 ] ≈ transpose (A)[1 : 10 , 1 : 10 ] ≈ transpose (A[1 : 10 , 1 : 10 ])
217
+
218
+ A = BandedMatrix (- 1 => Vcat (Float64[], Fill (1 / 4 , ∞)), 0 => Vcat ([1.0 + im], Fill (0 , ∞)), 1 => Vcat (Float64[], Fill (1 , ∞)))
219
+ @test MemoryLayout (typeof (view (A. data, :, 1 : 10 ))) == ApplyLayout {typeof(hcat)} ()
220
+ Ac = BandedMatrix (A' )
221
+ At = BandedMatrix (transpose (A))
222
+ @test Ac[1 : 10 , 1 : 10 ] ≈ (A' )[1 : 10 , 1 : 10 ] ≈ A[1 : 10 , 1 : 10 ]'
223
+ @test At[1 : 10 , 1 : 10 ] ≈ transpose (A)[1 : 10 , 1 : 10 ] ≈ transpose (A[1 : 10 , 1 : 10 ])
224
+
225
+ A = BandedMatrix (- 2 => Vcat (Float64[], Fill (1 / 4 , ∞)), 0 => Vcat ([1.0 + im, 2 , 3 ], Fill (0 , ∞)), 1 => Vcat (Float64[], Fill (1 , ∞)))
226
+ Ac = BandedMatrix (A' )
227
+ At = BandedMatrix (transpose (A))
228
+ @test Ac[1 : 10 , 1 : 10 ] ≈ (A' )[1 : 10 , 1 : 10 ] ≈ A[1 : 10 , 1 : 10 ]'
229
+ @test At[1 : 10 , 1 : 10 ] ≈ transpose (A)[1 : 10 , 1 : 10 ] ≈ transpose (A[1 : 10 , 1 : 10 ])
230
+
231
+ A = _BandedMatrix (Fill (1 , 4 , ∞), ℵ₀, 1 , 2 )
232
+ @test A^ 2 isa BandedMatrix
233
+ @test (A^ 2 )[1 : 10 , 1 : 10 ] == (A* A)[1 : 10 , 1 : 10 ] == (A[1 : 100 , 1 : 100 ]^ 2 )[1 : 10 , 1 : 10 ]
234
+ @test A^ 3 isa ApplyMatrix{<: Any ,typeof (* )}
235
+ @test (A^ 3 )[1 : 10 , 1 : 10 ] == (A* A* A)[1 : 10 , 1 : 10 ] == ((A* A)* A)[1 : 10 , 1 : 10 ] == (A* (A* A))[1 : 10 , 1 : 10 ] == (A[1 : 100 , 1 : 100 ]^ 3 )[1 : 10 , 1 : 10 ]
236
+
237
+ @testset " ∞ x finite" begin
238
+ A = BandedMatrix (1 => 1 : ∞) + BandedMatrix (- 1 => Fill (2 , ∞))
239
+ B = _BandedMatrix (randn (3 , 5 ), ℵ₀, 1 , 1 )
240
+
241
+ @test lmul! (2.0 , copy (B)' )[:, 1 : 10 ] == (2 B' )[:, 1 : 10 ]
242
+
243
+ @test_throws ArgumentError BandedMatrix (A)
244
+ @test A * B isa MulMatrix
245
+ @test B' A isa MulMatrix
246
+
247
+ @test all (diag (A[1 : 6 , 1 : 6 ]) .=== zeros (6 ))
248
+
249
+ @test (A* B)[1 : 7 , 1 : 5 ] ≈ A[1 : 7 , 1 : 6 ] * B[1 : 6 , 1 : 5 ]
250
+ @test (B' A)[1 : 5 , 1 : 7 ] ≈ (B' )[1 : 5 , 1 : 6 ] * A[1 : 6 , 1 : 7 ]
251
+ end
252
+ end
253
+
254
+ @testset " Fill" begin
255
+ A = _BandedMatrix (Ones (1 , ∞), ℵ₀, - 1 , 1 )
256
+ @test 1.0 .* A isa BandedMatrix{Float64,<: Fill }
257
+ @test Zeros (∞) .* A ≡ Zeros (∞, ∞) .* A ≡ A .* Zeros (1 , ∞) ≡ A .* Zeros (∞, ∞) ≡ Zeros (∞, ∞)
258
+ @test Ones (∞) .* A isa BandedMatrix{Float64,<: Ones }
259
+ @test A .* Ones (1 , ∞) isa BandedMatrix{Float64,<: Ones }
260
+ @test 2.0 .* A isa BandedMatrix{Float64,<: Fill }
261
+ @test A .* 2.0 isa BandedMatrix{Float64,<: Fill }
262
+ @test Eye (∞) * A isa BandedMatrix{Float64,<: Ones }
263
+ @test A * Eye (∞) isa BandedMatrix{Float64,<: Ones }
264
+
265
+ @test A * A isa BandedMatrix
266
+ @test (A* A)[1 : 10 , 1 : 10 ] == BandedMatrix (2 => Ones (8 ))
267
+
268
+ Ã = _BandedMatrix (Fill (1 , 1 , ∞), ℵ₀, - 1 , 1 )
269
+ @test A * Ã isa BandedMatrix
270
+ @test à * A isa BandedMatrix
271
+ @test à * à isa BandedMatrix
272
+
273
+ B = _BandedMatrix (Ones (1 , 10 ), ℵ₀, - 1 , 1 )
274
+ C = _BandedMatrix (Ones (1 , 10 ), 10 , - 1 , 1 )
275
+ D = _BandedMatrix (Ones (1 , ∞), 10 , - 1 , 1 )
276
+
277
+ @test (A* B)[1 : 10 , 1 : 10 ] == (B* C)[1 : 10 , 1 : 10 ] == (D* A)[1 : 10 , 1 : 10 ] == D * B == (C* D)[1 : 10 , 1 : 10 ] == BandedMatrix (2 => Ones (8 ))
278
+ end
279
+
280
+ @testset " Banded Broadcast" begin
281
+ A = _BandedMatrix ((1 : ∞)' , ℵ₀, - 1 , 1 )
282
+ @test 2.0 .* A isa BandedMatrix{Float64,<: Adjoint }
283
+ @test A .* 2.0 isa BandedMatrix{Float64,<: Adjoint }
284
+ @test Eye (∞) * A isa BandedMatrix{Float64,<: Adjoint }
285
+ @test A * Eye (∞) isa BandedMatrix{Float64,<: Adjoint }
286
+ A = _BandedMatrix (Vcat ((1 : ∞)' , Ones (1 , ∞)), ℵ₀, 0 , 1 )
287
+ @test 2.0 .* A isa BandedMatrix
288
+ @test A .* 2.0 isa BandedMatrix
289
+ @test Eye (∞) * A isa BandedMatrix
290
+ @test A * Eye (∞) isa BandedMatrix
291
+ b = 1 : ∞
292
+ @test bandwidths (b .* A) == (0 , 1 )
293
+
294
+ @test colsupport (b .* A, 1 ) == 1 : 1
295
+ @test Base. replace_in_print_matrix (b .* A, 2 , 1 , " 0.0" ) == " ⋅ "
296
+ @test bandwidths (A .* b) == (0 , 1 )
297
+ @test A .* b' isa BroadcastArray
298
+ @test bandwidths (A .* b' ) == bandwidths (A .* b' )
299
+ @test colsupport (A .* b' , 3 ) == 2 : 3
300
+
301
+ A = _BandedMatrix (Ones {Int} (1 , ∞), ℵ₀, 0 , 0 )'
302
+ B = _BandedMatrix ((- 2 : - 2 : - ∞)' , ℵ₀, - 1 , 1 )
303
+ C = Diagonal (2 ./ (1 : 2 : ∞))
304
+ @test bandwidths (A * (B * C)) == (- 1 , 1 )
305
+ @test bandwidths ((A * B) * C) == (- 1 , 1 )
306
+
307
+ A = _BandedMatrix (Ones {Int} (1 , ∞), ℵ₀, 0 , 0 )'
308
+ B = _BandedMatrix ((- 2 : - 2 : - ∞)' , ℵ₀, - 1 , 1 )
309
+ @test MemoryLayout (A + B) isa BroadcastBandedLayout{typeof (+ )}
310
+ @test MemoryLayout (2 * (A + B)) isa BroadcastBandedLayout{typeof (* )}
311
+ @test bandwidths (A + B) == (0 , 1 )
312
+ @test bandwidths (2 * (A + B)) == (0 , 1 )
313
+ end
314
+
190
315
end
0 commit comments