Skip to content

Commit 72e249f

Browse files
committed
add simple implementation for tests
1 parent 255ff11 commit 72e249f

File tree

2 files changed

+104
-58
lines changed

2 files changed

+104
-58
lines changed

src/BandedBlockBandedMatrix.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ function check_data_sizes(data::AbstractMatrix, raxis, (l,u), (λ,μ))
1010
end
1111

1212

13+
abstract type AbstractBandedBlockBandedMatrix{T} <: AbstractBlockBandedMatrix{T} end
14+
MemoryLayout(::Type{<:AbstractBandedBlockBandedMatrix}) = BandedBlockBandedLayout()
15+
1316
function _BandedBlockBandedMatrix end
1417

1518

@@ -19,7 +22,7 @@ function _BandedBlockBandedMatrix end
1922
# BandedMatrix
2023
#
2124

22-
struct BandedBlockBandedMatrix{T, BLOCKS, RAXIS<:AbstractUnitRange{Int}} <: AbstractBlockBandedMatrix{T}
25+
struct BandedBlockBandedMatrix{T, BLOCKS, RAXIS<:AbstractUnitRange{Int}} <: AbstractBandedBlockBandedMatrix{T}
2326
data::BLOCKS
2427
raxis::RAXIS
2528

@@ -398,6 +401,7 @@ sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockS
398401
sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockSlice{<:BlockRange1},BlockSlice{<:BlockRange1}}} = bandedblockbandedcolumns(sublayout(ML(), II))
399402
sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockSlice1,BlockSlice{<:BlockRange1}}} = bandedblockbandedcolumns(sublayout(ML(), II))
400403
sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockSlice{<:BlockRange1},BlockSlice1}} = bandedblockbandedcolumns(sublayout(ML(), II))
404+
sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockSlice{<:BlockIndexRange1},BlockSlice{<:BlockRange1}}} = bandedblockbandedcolumns(sublayout(ML(), II))
401405
sublayout(::BandedBlockBandedColumns{ML}, ::Type{II}) where {ML,II<:Tuple{BlockSlice{<:BlockRange1},BlockSlice{<:BlockIndexRange1}}} = bandedblockbandedcolumns(sublayout(ML(), II))
402406

403407
blockbandshift(A::BlockSlice, B::BlockSlice) = BandedMatrices.bandshift(Int.(A.block), Int.(B.block))

test/test_misc.jl

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,108 @@
11
using ArrayLayouts, BlockBandedMatrices, BandedMatrices, BlockArrays, LinearAlgebra, Test
2-
import Base: getindex, size
2+
import BlockBandedMatrices: AbstractBandedBlockBandedMatrix, block, blockindex
33
import BandedMatrices: bandwidths, AbstractBandedMatrix, BandedStyle, bandeddata, BandedColumns
44

5+
struct MyBandedBlockBandedMatrix <: AbstractBandedBlockBandedMatrix{Float64}
6+
A::BlockMatrix{Float64}
7+
end
58

6-
@testset "Diagonal interface" begin
7-
n = 10
8-
D = Diagonal(randn(n^2))
9-
@test blockbandwidths(D) == subblockbandwidths(D) == (0,0)
9+
BlockBandedMatrices.blockbandwidths(::MyBandedBlockBandedMatrix) = (1,1)
10+
BlockBandedMatrices.subblockbandwidths(::MyBandedBlockBandedMatrix) = (1,1)
11+
Base.axes(A::MyBandedBlockBandedMatrix) = axes(A.A)
12+
function Base.getindex(A::MyBandedBlockBandedMatrix, k::Int, j::Int)
13+
Kk, Jj = findblockindex(axes(A,1), k), findblockindex(axes(A,2), j)
14+
-1  Int(block(Kk)-block(Jj))  1 || return 0.0
15+
-1  Int(blockindex(Kk)-blockindex(Jj))  1 || return 0.0
16+
A.A[k,j]
17+
end
1018

11-
PD = PseudoBlockArray(D, Fill(n,n), Fill(n,n))
12-
@test blockbandwidths(PD) == bandwidths(PD) == (0,0)
13-
@test MemoryLayout(typeof(PD)) isa DiagonalLayout{DenseColumnMajor}
14-
@test bandeddata(PD) == bandeddata(D)
15-
V = view(PD, Block(1,1))
1619

17-
@test bandwidths(V) == (0,0)
18-
@test_broken Broadcast.BroadcastStyle(typeof(V)) == BandedStyle()
19-
@test MemoryLayout(typeof(V)) isa BandedColumns{DenseColumnMajor}
20-
@test bandeddata(V) == bandeddata(PD)[:,1:n]
21-
@test BandedMatrix(V) == V
22-
end
20+
@testset "Interfaces" begin
21+
@testset "MyBandedBlockBandedMatrix" begin
22+
A = MyBandedBlockBandedMatrix(BlockMatrix(randn(6,6), 1:3, 1:3))
23+
@test MemoryLayout(A) isa BlockBandedMatrices.BandedBlockBandedLayout
24+
@test A[Block(3,3)] == BandedMatrix(A.A[Block(3,3)],(1,1))
25+
@test A[Block.(1:3),Block.(1:3)] == A
2326

24-
@testset "Block Diagonal" begin
25-
A = BlockBandedMatrices.BlockDiagonal(fill([1 2],3))
26-
@test blockbandwidths(A) == (0,0)
27-
@test isblockbanded(A)
28-
@test A[Block(1,1)] == [1 2]
29-
@test @inferred(A[Block(1,2)]) == [0 0]
30-
@test_throws DimensionMismatch A+I
31-
A = BlockBandedMatrices.BlockDiagonal(fill([1 2; 1 2],3))
32-
@test A+I == I+A == mortar(Diagonal(fill([2 2; 1 3],3))) == Matrix(A) + I
33-
end
27+
@test A[Block(3,3)] isa BandedMatrix
28+
@test A[Block(3)[2:3],Block(3)[1:2]] isa BandedMatrix
29+
@test A[Block(3)[2:3],Block(3)] isa BandedMatrix
30+
@test A[Block(3),Block(3)[1:2]] isa BandedMatrix
31+
@test A[Block.(1:3),Block.(1:3)] isa BandedBlockBandedMatrix
32+
@test A[Block(1),Block.(2:3)] isa PseudoBlockArray
33+
@test A[Block.(2:3),Block(1)] isa PseudoBlockArray
34+
@test A[Block.(2:3),Block(2)[1:2]] isa PseudoBlockArray
35+
@test A[Block(2)[1:2],Block.(2:3)] isa PseudoBlockArray
36+
end
3437

35-
@testset "Block Bidiagonal" begin
36-
Bu = BlockBidiagonal(fill([1 2],4), fill([3 4],3), :U)
37-
Bl = BlockBidiagonal(fill([1 2],4), fill([3 4],3), :L)
38-
@test blockbandwidths(Bu) == (0,1)
39-
@test blockbandwidths(Bl) == (1,0)
40-
@test isblockbanded(Bu)
41-
@test isblockbanded(Bl)
42-
@test Bu[Block(1,1)] == Bl[Block(1,1)] == [1 2]
43-
@test @inferred(Bu[Block(1,2)]) == @inferred(Bl[Block(2,1)]) == [3 4]
44-
@test @inferred(view(Bu,Block(1,3))) == @inferred(Bu[Block(1,3)]) == [0 0]
45-
@test_throws DimensionMismatch Bu+I
46-
Bu = BlockBidiagonal(fill([1 2; 1 2],4), fill([3 4; 3 4],3), :U)
47-
Bl = BlockBidiagonal(fill([1 2; 1 2],4), fill([3 4; 3 4],3), :L)
48-
@test Bu+I == I+Bu == mortar(Bidiagonal(fill([2 2; 1 3],4), fill([3 4; 3 4],3), :U)) == Matrix(Bu) + I
49-
@test Bl+I == I+Bl == mortar(Bidiagonal(fill([2 2; 1 3],4), fill([3 4; 3 4],3), :L)) == Matrix(Bl) + I
50-
@test Bu-I == mortar(Bidiagonal(fill([0 2; 1 1],4), fill([3 4; 3 4],3), :U)) == Matrix(Bu) - I
51-
@test I-Bu == mortar(Bidiagonal(fill([0 -2; -1 -1],4), fill(-[3 4; 3 4],3), :U)) == I - Matrix(Bu)
52-
end
38+
@testset "Zeros" begin
39+
Z = Zeros(blockedrange(1:3), blockedrange(1:3))
40+
B = BandedBlockBandedMatrix(Z)
41+
@test B == Z
42+
@test blockisequal(axes(Z), axes(B))
43+
@test blockbandwidths(B) == blockbandwidths(Z) == (-1,-1)
44+
@test subblockbandwidths(B) == subblockbandwidths(Z) == (-1,-1)
45+
end
5346

54-
@testset "Block Tridiagonal" begin
55-
A = BlockTridiagonal(fill([1 2],3), fill([3 4],4), fill([4 5],3))
56-
@test blockbandwidths(A) == (1,1)
57-
@test isblockbanded(A)
58-
@test A[Block(1,1)] == [3 4]
59-
@test @inferred(A[Block(1,2)]) == [4 5]
60-
@test @inferred(view(A,Block(1,3))) == @inferred(A[Block(1,3)]) == [0 0]
61-
@test_throws DimensionMismatch A+I
62-
A = BlockTridiagonal(fill([1 2; 1 2],3), fill([3 4; 3 4],4), fill([4 5; 4 5],3))
63-
@test A+I == I+A == mortar(Tridiagonal(fill([1 2; 1 2],3), fill([4 4; 3 5],4), fill([4 5; 4 5],3))) == Matrix(A) + I
64-
@test A-I == mortar(Tridiagonal(fill([1 2; 1 2],3), fill([2 4; 3 3],4), fill([4 5; 4 5],3))) == Matrix(A) - I
65-
@test I-A == mortar(Tridiagonal(fill(-[1 2; 1 2],3), fill([-2 -4; -3 -3],4), fill(-[4 5; 4 5],3))) == I - Matrix(A)
66-
end
47+
@testset "Diagonal interface" begin
48+
n = 10
49+
D = Diagonal(randn(n^2))
50+
@test blockbandwidths(D) == subblockbandwidths(D) == (0,0)
51+
52+
PD = PseudoBlockArray(D, Fill(n,n), Fill(n,n))
53+
@test blockbandwidths(PD) == bandwidths(PD) == (0,0)
54+
@test MemoryLayout(typeof(PD)) isa DiagonalLayout{DenseColumnMajor}
55+
@test bandeddata(PD) == bandeddata(D)
56+
V = view(PD, Block(1,1))
57+
58+
@test bandwidths(V) == (0,0)
59+
@test_broken Broadcast.BroadcastStyle(typeof(V)) == BandedStyle()
60+
@test MemoryLayout(typeof(V)) isa BandedColumns{DenseColumnMajor}
61+
@test bandeddata(V) == bandeddata(PD)[:,1:n]
62+
@test BandedMatrix(V) == V
63+
end
64+
65+
@testset "Block Diagonal" begin
66+
A = BlockBandedMatrices.BlockDiagonal(fill([1 2],3))
67+
@test blockbandwidths(A) == (0,0)
68+
@test isblockbanded(A)
69+
@test A[Block(1,1)] == [1 2]
70+
@test @inferred(A[Block(1,2)]) == [0 0]
71+
@test_throws DimensionMismatch A+I
72+
A = BlockBandedMatrices.BlockDiagonal(fill([1 2; 1 2],3))
73+
@test A+I == I+A == mortar(Diagonal(fill([2 2; 1 3],3))) == Matrix(A) + I
74+
end
75+
76+
@testset "Block Bidiagonal" begin
77+
Bu = BlockBidiagonal(fill([1 2],4), fill([3 4],3), :U)
78+
Bl = BlockBidiagonal(fill([1 2],4), fill([3 4],3), :L)
79+
@test blockbandwidths(Bu) == (0,1)
80+
@test blockbandwidths(Bl) == (1,0)
81+
@test isblockbanded(Bu)
82+
@test isblockbanded(Bl)
83+
@test Bu[Block(1,1)] == Bl[Block(1,1)] == [1 2]
84+
@test @inferred(Bu[Block(1,2)]) == @inferred(Bl[Block(2,1)]) == [3 4]
85+
@test @inferred(view(Bu,Block(1,3))) == @inferred(Bu[Block(1,3)]) == [0 0]
86+
@test_throws DimensionMismatch Bu+I
87+
Bu = BlockBidiagonal(fill([1 2; 1 2],4), fill([3 4; 3 4],3), :U)
88+
Bl = BlockBidiagonal(fill([1 2; 1 2],4), fill([3 4; 3 4],3), :L)
89+
@test Bu+I == I+Bu == mortar(Bidiagonal(fill([2 2; 1 3],4), fill([3 4; 3 4],3), :U)) == Matrix(Bu) + I
90+
@test Bl+I == I+Bl == mortar(Bidiagonal(fill([2 2; 1 3],4), fill([3 4; 3 4],3), :L)) == Matrix(Bl) + I
91+
@test Bu-I == mortar(Bidiagonal(fill([0 2; 1 1],4), fill([3 4; 3 4],3), :U)) == Matrix(Bu) - I
92+
@test I-Bu == mortar(Bidiagonal(fill([0 -2; -1 -1],4), fill(-[3 4; 3 4],3), :U)) == I - Matrix(Bu)
93+
end
94+
95+
@testset "Block Tridiagonal" begin
96+
A = BlockTridiagonal(fill([1 2],3), fill([3 4],4), fill([4 5],3))
97+
@test blockbandwidths(A) == (1,1)
98+
@test isblockbanded(A)
99+
@test A[Block(1,1)] == [3 4]
100+
@test @inferred(A[Block(1,2)]) == [4 5]
101+
@test @inferred(view(A,Block(1,3))) == @inferred(A[Block(1,3)]) == [0 0]
102+
@test_throws DimensionMismatch A+I
103+
A = BlockTridiagonal(fill([1 2; 1 2],3), fill([3 4; 3 4],4), fill([4 5; 4 5],3))
104+
@test A+I == I+A == mortar(Tridiagonal(fill([1 2; 1 2],3), fill([4 4; 3 5],4), fill([4 5; 4 5],3))) == Matrix(A) + I
105+
@test A-I == mortar(Tridiagonal(fill([1 2; 1 2],3), fill([2 4; 3 3],4), fill([4 5; 4 5],3))) == Matrix(A) - I
106+
@test I-A == mortar(Tridiagonal(fill(-[1 2; 1 2],3), fill([-2 -4; -3 -3],4), fill(-[4 5; 4 5],3))) == I - Matrix(A)
107+
end
108+
end

0 commit comments

Comments
 (0)