-
Notifications
You must be signed in to change notification settings - Fork 150
/
SHermitianCompact.jl
261 lines (226 loc) · 9.16 KB
/
SHermitianCompact.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
module SHermitianCompactTest
using StaticArrays
using LinearAlgebra
using Random
using Test
macro test_noalloc(ex)
esc(quote
$ex
@test(@allocated($ex) == 0)
end)
end
function check_lower_triangle(a::SHermitianCompact, triangle::SVector)
N = size(a, 1)
i = 0
for col = 1 : N, row = 1 : N
if row >= col
i += 1
@test a[row, col] == triangle[i]
else
@test a[row, col] == adjoint(a[col, row])
end
end
end
function check_lower_triangle(a::SHermitianCompact{3}, triangle::SVector)
# because the generic check_lower_triangle uses almost the same code as what's being tested
@test a[1, 1] == triangle[1]
@test a[2, 1] == triangle[2]
@test a[3, 1] == triangle[3]
@test a[1, 2] == adjoint(a[2, 1])
@test a[2, 2] == triangle[4]
@test a[3, 2] == triangle[5]
@test a[1, 3] == adjoint(a[3, 1])
@test a[2, 3] == adjoint(a[3, 2])
@test a[3, 3] == triangle[6]
end
fill3(x) = fill(3, x)
@testset "SHermitianCompact" begin
@testset "Inner Constructor" begin
for (N, L) in ((3, 6), (4, 10), (6, 21))
for T in (Int32, Int64)
@eval begin
let lowertriangle = rand(SVector{$L, Int32})
a = SHermitianCompact{$N, $T, $L}(lowertriangle)
check_lower_triangle(a, lowertriangle)
@test a isa SHermitianCompact{$N, $T, $L}
@test(@allocated(SHermitianCompact{$N, $T, $L}(lowertriangle)) == 0)
end
end
end
end
@test_throws ArgumentError SHermitianCompact{3, Int64, 7}(rand(SVector{7, Int32}))
end
@testset "Outer Constructors" begin
for (N, L) in ((3, 6), (4, 10), (6, 21))
for T in (Int32, Int64)
@eval begin
let lowertriangle = rand(SVector{$L, Int32})
a1 = SHermitianCompact{$N, $T}(lowertriangle)
check_lower_triangle(a1, $T.(lowertriangle))
@test a1 isa SHermitianCompact{$N, $T, $L}
@test_noalloc SHermitianCompact{$N, $T}(lowertriangle)
a2 = SHermitianCompact{$N}(lowertriangle)
check_lower_triangle(a2, lowertriangle)
@test a2 isa SHermitianCompact{$N, Int32, $L}
@test_noalloc SHermitianCompact{$N}(lowertriangle)
a3 = SHermitianCompact(lowertriangle)
check_lower_triangle(a3, lowertriangle)
@test a3 isa SHermitianCompact{$N, Int32, $L}
@test_noalloc SHermitianCompact(lowertriangle)
a4 = SHermitianCompact{$N, $T}(a3)
@test a4 === a1
@test_noalloc SHermitianCompact{$N, $T}(a3)
end
let a = rand(SMatrix{$N, $N, Int32})
@test SHermitianCompact{$N, $T, $L}(a) isa SHermitianCompact{$N, $T, $L}
@test_noalloc SHermitianCompact{$N, $T, $L}(a)
@test SHermitianCompact{$N, $T, $L}(a) == Hermitian($T.(a), :L)
@test SHermitianCompact{$N, $T}(a) isa SHermitianCompact{$N, $T, $L}
@test_noalloc SHermitianCompact{$N, $T}(a)
@test SHermitianCompact{$N, $T}(a) == Hermitian($T.(a), :L)
@test SHermitianCompact{$N}(a) isa SHermitianCompact{$N, Int32, $L}
@test_noalloc SHermitianCompact{$N}(a)
@test SHermitianCompact{$N}(a) == Hermitian(a, :L)
@test SHermitianCompact(a) isa SHermitianCompact{$N, Int32, $L}
@test_noalloc SHermitianCompact(a)
@test SHermitianCompact(a) == Hermitian(a, :L)
end
end
end
end
end
@testset "convert" begin
a = SHermitianCompact(SMatrix{3, 3}(1 : 9))
@test convert(typeof(a), a) === a
@test convert(SHermitianCompact{3, Float64}, a) isa SHermitianCompact{3, Float64, 6}
@test convert(SHermitianCompact{3, Float64}, a) == Float64.(Array(a))
@test convert(SHermitianCompact{3, Float64, 6}, a) isa SHermitianCompact{3, Float64, 6}
end
@testset "setindex" begin
for (N, L) in ((3, 6), (4, 10), (6, 21))
@eval begin
let lowertriangle = zero(SVector{$L, Int32})
a = SHermitianCompact(lowertriangle)
@test_noalloc setindex(a, 2., 1, 1)
for col = 1 : $N, row = 1 : $N
x = Float64(rand(Int32))
a2 = setindex(a, x, row, col)
@test a2[row, col] === Int32(x)
@test setindex(a2, 0, row, col) === a
end
end
end
end
end
@testset "ishermitian / issymmetric" begin
a = rand(SHermitianCompact{5, Float64})
@test ishermitian(a)
@test issymmetric(a)
b = rand(SHermitianCompact{5, ComplexF64})
@test ishermitian(b)
@test issymmetric(b)
c = b + conj(b)
@test ishermitian(c)
@test issymmetric(c)
@test_noalloc ishermitian(c)
end
@testset "==" begin
a = SHermitianCompact(SVector{6}(-5 : 0))
b = setindex(a, 5, 3)
@test a == a
@test !(a == b)
@test_noalloc a == a
end
@testset "Arithmetic" begin
a = SHermitianCompact(SVector{6, Int}(1 : 6))
b = SHermitianCompact(SVector(-4, 5, 4, 8, -10, 11))
c = SMatrix{3, 3}(-9 : -1)
let a = a
@test -a == -SMatrix(a)
@test -a isa SHermitianCompact{3, Int, 6}
@test_noalloc -a
end
for (x, y) in ((a, b), (a, c), (c, a))
@eval begin
let x = $x, y = $y
@test x + y == SMatrix(x) + SMatrix(y)
@test_noalloc x + y
@test x - y == SMatrix(x) - SMatrix(y)
@test_noalloc x - y
if x isa SHermitianCompact && y isa SHermitianCompact
@test x + y isa SHermitianCompact{3, Int, 6}
@test x - y isa SHermitianCompact{3, Int, 6}
end
end
end
end
end
@testset "Scalar-array" begin
x = SHermitianCompact(SVector{6, Int}(1 : 6))
y = -5
for op in (:*, :/, :\)
if op != :\
@eval begin
@test $op($x, $y) == $op(SMatrix($x), $y)
@test_noalloc $op($x, $y)
end
end
if op != :/
@eval begin
@test $op($y, $x) == $op($y, SMatrix($x))
@test_noalloc $op($y, $x)
end
end
end
end
@testset "UniformScaling" begin
let a = SHermitianCompact(SVector{21, Int}(1 : 21))
@test a + 3I == SMatrix(a) + 3I
@test a + 3I isa typeof(a)
@test_noalloc a + 3I
@test a - 4I == SMatrix(a) - 4I
@test a - 4I isa typeof(a)
@test_noalloc a - 4I
@test a * 3I === a * 3
@test 3I * a === 3 * a
@test 3I \ a == 3 \ a
@test a / 3I == a / 3
end
end
@testset "transpose/adjoint" begin
a = Hermitian([[rand(Complex{Int}) for i = 1 : 2, j = 1 : 2] for row = 1 : 3, col = 1 : 3])
if VERSION < v"1.2.0-"
@test transpose(SHermitianCompact{3}(a)) == transpose(a)
else
# FIXME: This `transpose(a)` crashes in v1.2.0-rc1.
# See https://github.com/JuliaLang/julia/issues/32222
@test_skip transpose(SHermitianCompact{3}(a)) == transpose(a)
end
@test adjoint(SHermitianCompact{3}(a)) == adjoint(a)
b = Hermitian([rand(Complex{Int}) for i = 1 : 3, j = 1 : 3])
@test adjoint(SHermitianCompact{3}(b)) == adjoint(b)
end
@testset "one/ones/zeros/fill" begin
for N = 3 : 5, f in (:one, :ones, :zeros, :fill3)
@eval begin
@test $f(SHermitianCompact{$N, Int}) == $f(SMatrix{$N, $N, Int})
@test $f(SHermitianCompact{$N, Int}) isa SHermitianCompact{$N, Int}
@test_noalloc $f(SHermitianCompact{$N, Int})
@test $f(SHermitianCompact{$N}) == $f(SMatrix{$N, $N})
@test $f(SHermitianCompact{$N}) isa SHermitianCompact{$N, eltype($f(SMatrix{$N, $N}))}
@test_noalloc $f(SHermitianCompact{$N})
end
end
end
@testset "rand" begin
for N = 3 : 5, f in (:rand, :randn, :randexp)
@eval begin
@test_noalloc $f(SHermitianCompact{$N, Float32})
@test $f(SHermitianCompact{$N, Float32}) isa SHermitianCompact{$N, Float32}
@test_noalloc $f(SHermitianCompact{$N})
@test $f(SHermitianCompact{$N}) isa SHermitianCompact{$N, Float64}
end
end
end
end
end # module