@@ -5,38 +5,31 @@ using Test
5
5
6
6
@testset " base_maths.jl" begin
7
7
rng = MersenneTwister (123456 )
8
- N1, N2, N3 = 3 , 4 , 5
9
- N = N1 + N2 + N3
10
- blocks1 = [rand (rng, N1, N1), rand (rng, N2, N2), rand (rng, N3, N3)]
11
- blocks2 = [rand (rng, N1, N1), rand (rng, N3, N3), rand (rng, N2, N2)]
12
- blocks3 = [rand (rng, N1, N1), rand (rng, N2, N2), rand (rng, N2, N2)]
13
-
14
- @testset " $T " for (T, (b1, b2, b3)) in (
15
- Tuple => (BlockDiagonal (Tuple (blocks1)), BlockDiagonal (Tuple (blocks2)), BlockDiagonal (Tuple (blocks3))),
16
- Vector => (BlockDiagonal (blocks1), BlockDiagonal (blocks2), BlockDiagonal (blocks3)),
17
- )
18
- A = rand (rng, N, N + N1)
19
- B = rand (rng, N + N1, N + N2)
20
- A′, B′ = A' , B'
21
- a = rand (rng, N)
22
- b = rand (rng, N + N1)
8
+ blocks1 = [rand (rng, 3 , 3 ), rand (rng, 4 , 4 )]
9
+ blocks2 = [rand (rng, 3 , 3 ), rand (rng, 5 , 5 )]
10
+
11
+ @testset for V in (Tuple, Vector)
12
+ b1 = BlockDiagonal (V (blocks1))
13
+ b2 = BlockDiagonal (V (blocks2))
14
+ N = size (b1, 1 )
15
+ A = rand (rng, N, N + 1 )
23
16
24
17
@testset " Addition" begin
25
18
@testset " BlockDiagonal + BlockDiagonal" begin
26
19
@test b1 + b1 isa BlockDiagonal
27
20
@test Matrix (b1 + b1) == Matrix (b1) + Matrix (b1)
28
- @test_throws DimensionMismatch b1 + b3
21
+ @test_throws DimensionMismatch b1 + b2
29
22
end
30
23
31
24
@testset " BlockDiagonal + Matrix" begin
32
25
@test b1 + Matrix (b1) isa Matrix
33
26
@test b1 + Matrix (b1) == b1 + b1
34
- @test_throws DimensionMismatch b1 + Matrix (b3 )
27
+ @test_throws DimensionMismatch b1 + Matrix (b2 )
35
28
36
29
# Matrix + BlockDiagonal
37
30
@test Matrix (b1) + b1 isa Matrix
38
31
@test Matrix (b1) + b1 == b1 + b1
39
- @test_throws DimensionMismatch Matrix (b1) + b3
32
+ @test_throws DimensionMismatch Matrix (b1) + b2
40
33
41
34
# If the AbstractMatrix is diagonal, we should return a BlockDiagonal.
42
35
# Test the StridedMatrix method.
@@ -50,7 +43,7 @@ using Test
50
43
51
44
@testset " BlockDiagonal + Diagonal" begin
52
45
D = Diagonal (randn (rng, N))
53
- D′ = Diagonal (randn (rng, N + N1 ))
46
+ D′ = Diagonal (randn (rng, N + 1 ))
54
47
55
48
@test b1 + D isa BlockDiagonal
56
49
@test b1 + D == Matrix (b1) + D
@@ -73,11 +66,10 @@ using Test
73
66
end # Addition
74
67
75
68
@testset " Multiplication" begin
76
-
77
69
@testset " BlockDiagonal * BlockDiagonal" begin
78
70
@test b1 * b1 isa BlockDiagonal
79
71
@test Matrix (b1 * b1) ≈ Matrix (b1) * Matrix (b1)
80
- @test_throws DimensionMismatch b3 * b1
72
+ @test_throws DimensionMismatch b2 * b1
81
73
end
82
74
83
75
@testset " BlockDiagonal * Number" begin
@@ -88,11 +80,14 @@ using Test
88
80
end
89
81
90
82
@testset " BlockDiagonal * Vector" begin
83
+ a = rand (rng, N)
91
84
@test b1 * a isa Vector
92
85
@test b1 * a ≈ Matrix (b1) * a
86
+ b = rand (rng, N + 1 )
93
87
@test_throws DimensionMismatch b1 * b
94
88
end
95
89
@testset " Vector^T * BlockDiagonal" begin
90
+ a = rand (rng, N)
96
91
@test a' * b1 isa Adjoint{<: Number , <: Vector }
97
92
@test transpose (a) * b1 isa Transpose{<: Number , <: Vector }
98
93
@test a' * b1 ≈ a' * Matrix (b1)
@@ -102,11 +97,13 @@ using Test
102
97
@testset " BlockDiagonal * Matrix" begin
103
98
@test b1 * A isa Matrix
104
99
@test b1 * A ≈ Matrix (b1) * A
100
+
101
+ B = rand (rng, N + 1 , N)
105
102
@test_throws DimensionMismatch b1 * B
106
103
107
104
# Matrix * BlockDiagonal
108
- @test A′ * b1 isa Matrix
109
- @test A′ * b1 ≈ A′ * Matrix (b1)
105
+ @test A' * b1 isa Matrix
106
+ @test A' * b1 ≈ A' * Matrix (b1)
110
107
@test_throws DimensionMismatch A * b1
111
108
112
109
# degenerate cases
@@ -119,7 +116,7 @@ using Test
119
116
120
117
@testset " BlockDiagonal * Diagonal" begin
121
118
D = Diagonal (randn (rng, N))
122
- D′ = Diagonal (randn (rng, N + N1 ))
119
+ D′ = Diagonal (randn (rng, N + 1 ))
123
120
124
121
@test b1 * D isa BlockDiagonal
125
122
@test b1 * D ≈ Matrix (b1) * D
@@ -132,8 +129,8 @@ using Test
132
129
end
133
130
134
131
@testset " Non-Square BlockDiagonal * Non-Square BlockDiagonal" begin
135
- b4 = BlockDiagonal (T ([ones (2 , 4 ), 2 * ones (3 , 2 )]))
136
- b5 = BlockDiagonal (T ([3 * ones (2 , 2 ), 2 * ones (4 , 1 )]))
132
+ b4 = BlockDiagonal (V ([ones (2 , 4 ), 2 * ones (3 , 2 )]))
133
+ b5 = BlockDiagonal (V ([3 * ones (2 , 2 ), 2 * ones (4 , 1 )]))
137
134
138
135
@test b4 * b5 isa Array
139
136
@test b4 * b5 == [6 * ones (2 , 2 ) 4 * ones (2 , 1 ); zeros (3 , 2 ) 8 * ones (3 , 1 )]
@@ -142,5 +139,5 @@ using Test
142
139
@test sum (size .(b5. blocks, 2 )) == size (b4 * b5, 2 )
143
140
end
144
141
end # Multiplication
145
- end
142
+ end # V
146
143
end
0 commit comments