-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathdiagonal.jl
423 lines (376 loc) · 14.8 KB
/
diagonal.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
import Base.LinAlg: BlasFloat, BlasComplex, SingularException, A_rdiv_B!, A_rdiv_Bt!,
A_rdiv_Bc!
n=12 #Size of matrix problem to test
srand(1)
@testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
dd=convert(Vector{elty}, randn(n))
vv=convert(Vector{elty}, randn(n))
UU=convert(Matrix{elty}, randn(n,n))
if elty <: Complex
dd+=im*convert(Vector{elty}, randn(n))
vv+=im*convert(Vector{elty}, randn(n))
UU+=im*convert(Matrix{elty}, randn(n,n))
end
D = Diagonal(dd)
DM = Matrix(Diagonal(dd))
@testset "constructor" begin
for x in (dd, GenericArray(dd))
@test Diagonal(x)::Diagonal{elty,typeof(x)} == DM
@test Diagonal(x).diag === x
@test Diagonal{elty}(x)::Diagonal{elty,typeof(x)} == DM
@test Diagonal{elty}(x).diag === x
end
end
@testset "Basic properties" begin
@test_throws ArgumentError size(D,0)
@test typeof(convert(Diagonal{Complex64},D)) <: Diagonal{Complex64}
@test typeof(convert(AbstractMatrix{Complex64},D)) <: Diagonal{Complex64}
@test Array(real(D)) == real(DM)
@test Array(abs.(D)) == abs.(DM)
@test Array(imag(D)) == imag(DM)
@test parent(D) == dd
@test D[1,1] == dd[1]
@test D[1,2] == 0
@test issymmetric(D)
@test istriu(D)
@test istril(D)
if elty <: Real
@test ishermitian(D)
end
end
@testset "diag" begin
@test_throws ArgumentError diag(D, n+1)
@test_throws ArgumentError diag(D, -n-1)
@test (@inferred diag(D))::typeof(dd) == dd
@test (@inferred diag(D, 0))::typeof(dd) == dd
@test (@inferred diag(D, 1))::typeof(dd) == zeros(elty, n-1)
DG = Diagonal(GenericArray(dd))
@test (@inferred diag(DG))::typeof(GenericArray(dd)) == GenericArray(dd)
@test (@inferred diag(DG, 1))::typeof(GenericArray(dd)) == GenericArray(zeros(elty, n-1))
end
@testset "Simple unary functions" begin
for op in (-,)
@test op(D)==op(DM)
end
for func in (det, trace)
@test func(D) ≈ func(DM) atol=n^2*eps(relty)*(1+(elty<:Complex))
end
if relty <: BlasFloat
for func in (exp, sinh, cosh, tanh, sech, csch, coth)
@test func(D) ≈ func(DM) atol=n^3*eps(relty)
end
@test log(Diagonal(abs.(D.diag))) ≈ log(abs.(DM)) atol=n^3*eps(relty)
end
if elty <: BlasComplex
for func in (logdet, sqrt, sin, cos, tan, sec, csc, cot,
asin, acos, atan, asec, acsc, acot,
asinh, acosh, atanh, asech, acsch, acoth)
@test func(D) ≈ func(DM) atol=n^2*eps(relty)*2
end
end
end
@testset "Linear solve" begin
for (v, U) in ((vv, UU), (view(vv, 1:n), view(UU, 1:n, 1:2)))
@test D*v ≈ DM*v atol=n*eps(relty)*(1+(elty<:Complex))
@test D*U ≈ DM*U atol=n^2*eps(relty)*(1+(elty<:Complex))
@test U.'*D ≈ U.'*Array(D)
@test U'*D ≈ U'*Array(D)
if relty != BigFloat
atol_two = 2n^2 * eps(relty) * (1 + (elty <: Complex))
atol_three = 2n^3 * eps(relty) * (1 + (elty <: Complex))
@test D\v ≈ DM\v atol=atol_two
@test D\U ≈ DM\U atol=atol_three
@test A_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two
@test At_ldiv_B!(D, copy(v)) ≈ DM\v atol=atol_two
@test Ac_ldiv_B!(conj(D), copy(v)) ≈ DM\v atol=atol_two
@test A_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three
@test At_ldiv_B!(D, copy(U)) ≈ DM\U atol=atol_three
@test Ac_ldiv_B!(conj(D), copy(U)) ≈ DM\U atol=atol_three
Uc = adjoint(U)
target = scale!(Uc, inv.(D.diag))
@test A_rdiv_B!(Uc, D) ≈ target atol=atol_three
@test_throws DimensionMismatch A_rdiv_B!(eye(elty, n-1), D)
@test_throws SingularException A_rdiv_B!(Uc, zeros(D))
@test A_rdiv_Bt!(Uc, D) ≈ target atol=atol_three
@test A_rdiv_Bc!(Uc, conj(D)) ≈ target atol=atol_three
@test A_ldiv_B!(D, eye(D)) ≈ D\eye(D) atol=atol_three
@test_throws DimensionMismatch A_ldiv_B!(D, ones(elty, n + 1))
@test_throws SingularException A_ldiv_B!(Diagonal(zeros(relty, n)), copy(v))
b = rand(elty, n, n)
b = sparse(b)
@test A_ldiv_B!(D, copy(b)) ≈ Array(D)\Array(b)
@test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), copy(b))
b = view(rand(elty, n), collect(1:n))
b2 = copy(b)
c = A_ldiv_B!(D, b)
d = Array(D)\b2
@test c ≈ d
@test_throws SingularException A_ldiv_B!(Diagonal(zeros(elty, n)), b)
b = rand(elty, n+1, n+1)
b = sparse(b)
@test_throws DimensionMismatch A_ldiv_B!(D, copy(b))
b = view(rand(elty, n+1), collect(1:n+1))
@test_throws DimensionMismatch A_ldiv_B!(D, b)
end
end
end
d = convert(Vector{elty}, randn(n))
D2 = Diagonal(d)
DM2= Matrix(Diagonal(d))
@testset "Binary operations" begin
for op in (+, -, *)
@test Array(op(D, D2)) ≈ op(DM, DM2)
end
@testset "with plain numbers" begin
a = rand()
@test Array(a*D) ≈ a*DM
@test Array(D*a) ≈ DM*a
@test Array(D/a) ≈ DM/a
if relty <: BlasFloat
b = rand(elty,n,n)
b = sparse(b)
@test A_mul_B!(copy(D), copy(b)) ≈ Array(D)*Array(b)
@test At_mul_B!(copy(D), copy(b)) ≈ Array(D).'*Array(b)
@test Ac_mul_B!(copy(D), copy(b)) ≈ Array(D)'*Array(b)
end
end
#a few missing mults
bd = Bidiagonal(D2)
@test D*D2.' ≈ Array(D)*Array(D2).'
@test D2*D.' ≈ Array(D2)*Array(D).'
@test D2*D' ≈ Array(D2)*Array(D)'
#division of two Diagonals
@test D/D2 ≈ Diagonal(D.diag./D2.diag)
@test D\D2 ≈ Diagonal(D2.diag./D.diag)
# Performance specialisations for A*_mul_B!
vvv = similar(vv)
@test (r = Matrix(D) * vv ; A_mul_B!(vvv, D, vv) ≈ r ≈ vvv)
@test (r = Matrix(D)' * vv ; Ac_mul_B!(vvv, D, vv) ≈ r ≈ vvv)
@test (r = Matrix(D).' * vv ; At_mul_B!(vvv, D, vv) ≈ r ≈ vvv)
UUU = similar(UU)
@test (r = Matrix(D) * UU ; A_mul_B!(UUU, D, UU) ≈ r ≈ UUU)
@test (r = Matrix(D)' * UU ; Ac_mul_B!(UUU, D, UU) ≈ r ≈ UUU)
@test (r = Matrix(D).' * UU ; At_mul_B!(UUU, D, UU) ≈ r ≈ UUU)
# make sure that A_mul_B{c,t}! works with B as a Diagonal
VV = Array(D)
DD = copy(D)
r = VV * Matrix(D)
@test Array(A_mul_B!(VV, DD)) ≈ r ≈ Array(D)*Array(D)
DD = copy(D)
r = VV * (Array(D).')
@test Array(A_mul_Bt!(VV, DD)) ≈ r
DD = copy(D)
r = VV * (Array(D)')
@test Array(A_mul_Bc!(VV, DD)) ≈ r
end
@testset "triu/tril" begin
@test istriu(D)
@test istril(D)
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test triu(D,-1) == D
@test tril(D,1) == D
@test tril(D,-1) == zeros(D)
@test tril(D,0) == D
@test_throws ArgumentError tril(D, -n - 2)
@test_throws ArgumentError tril(D, n)
@test_throws ArgumentError triu(D, -n)
@test_throws ArgumentError triu(D, n + 2)
end
# factorize
@test factorize(D) == D
@testset "Eigensystem" begin
eigD = eigfact(D)
@test Diagonal(eigD[:values]) ≈ D
@test eigD[:vectors] == eye(D)
end
@testset "ldiv" begin
v = rand(n + 1)
@test_throws DimensionMismatch D\v
v = rand(n)
@test D\v ≈ DM\v
V = rand(n + 1, n)
@test_throws DimensionMismatch D\V
V = rand(n, n)
@test D\V ≈ DM\V
end
@testset "conj and transpose" begin
@test transpose(D) == D
if elty <: BlasComplex
@test Array(conj(D)) ≈ conj(DM)
@test adjoint(D) == conj(D)
end
# Translates to Ac/t_mul_B, which is specialized after issue 21286
@test(D' * vv == conj(D) * vv)
@test(D.' * vv == D * vv)
end
#logdet
if relty <: Real
ld=convert(Vector{relty},rand(n))
@test logdet(Diagonal(ld)) ≈ logdet(Matrix(Diagonal(ld)))
end
@testset "similar" begin
@test isa(similar(D), Diagonal{elty})
@test isa(similar(D, Int), Diagonal{Int})
@test isa(similar(D, (3,2)), SparseMatrixCSC{elty})
@test isa(similar(D, Int, (3,2)), SparseMatrixCSC{Int})
end
# Issue number 10036
# make sure issymmetric/ishermitian work for
# non-real diagonal matrices
@testset "issymmetric/hermitian for complex Diagonal" begin
@test issymmetric(D2)
@test ishermitian(D2)
if elty <: Complex
dc = d + im*convert(Vector{elty}, ones(n))
D3 = Diagonal(dc)
@test issymmetric(D3)
@test !ishermitian(D3)
end
end
@testset "svd (#11120/#11247)" begin
U, s, V = svd(D)
@test (U*Diagonal(s))*V' ≈ D
@test svdvals(D) == s
@test svdfact(D)[:V] == V
end
end
@testset "svdvals and eigvals (#11120/#11247)" begin
D = Diagonal(Matrix{Float64}[randn(3,3), randn(2,2)])
@test sort([svdvals(D)...;], rev = true) ≈ svdvals([D.diag[1] zeros(3,2); zeros(2,3) D.diag[2]])
@test [eigvals(D)...;] ≈ eigvals([D.diag[1] zeros(3,2); zeros(2,3) D.diag[2]])
end
@testset "isposdef" begin
@test isposdef(Diagonal(1.0 .+ rand(n)))
@test !isposdef(Diagonal(-1.0 * rand(n)))
end
@testset "getindex" begin
d = randn(n)
D = Diagonal(d)
# getindex bounds checking
@test_throws BoundsError D[0, 0]
@test_throws BoundsError D[-1, -2]
@test_throws BoundsError D[n, n + 1]
@test_throws BoundsError D[n + 1, n]
@test_throws BoundsError D[n + 1, n + 1]
# getindex on and off the diagonal
for i in 1:n, j in 1:n
@test D[i, j] == (i == j ? d[i] : 0)
end
end
@testset "setindex!" begin
d = randn(n)
D = Diagonal(d)
# setindex! bounds checking
@test_throws BoundsError D[0, 0] = 0
@test_throws BoundsError D[-1 , -2] = 0
@test_throws BoundsError D[n, n + 1] = 0
@test_throws BoundsError D[n + 1, n] = 0
@test_throws BoundsError D[n + 1, n + 1] = 0
for i in 1:n, j in 1:n
if i == j
# setindex on! the diagonal
@test ((D[i, j] = i) == i; D[i, j] == i)
else
# setindex! off the diagonal
@test ((D[i, j] = 0) == 0; iszero(D[i, j]))
@test_throws ArgumentError D[i, j] = 1
end
end
end
@testset "inverse" begin
for d in (randn(n), [1, 2, 3], [1im, 2im, 3im])
D = Diagonal(d)
@test inv(D) ≈ inv(Array(D))
end
@test_throws SingularException inv(Diagonal(zeros(n)))
@test_throws SingularException inv(Diagonal([0, 1, 2]))
@test_throws SingularException inv(Diagonal([0im, 1im, 2im]))
end
# allow construct from range
@test all(Diagonal(linspace(1,3,3)) .== Diagonal([1.0,2.0,3.0]))
# Issue 12803
for t in (Float32, Float64, Int, Complex{Float64}, Rational{Int})
@test Diagonal(Matrix{t}[ones(t, 2, 2), ones(t, 3, 3)])[2,1] == zeros(t, 3, 2)
end
# Issue 15401
@test eye(5) \ Diagonal(ones(5)) == eye(5)
@testset "Triangular and Diagonal" begin
for T in (LowerTriangular(randn(5,5)), LinAlg.UnitLowerTriangular(randn(5,5)))
D = Diagonal(randn(5))
@test T*D == Array(T)*Array(D)
@test T'D == Array(T)'*Array(D)
@test T.'D == Array(T).'*Array(D)
@test D*T' == Array(D)*Array(T)'
@test D*T.' == Array(D)*Array(T).'
@test D*T == Array(D)*Array(T)
end
end
let D1 = Diagonal(rand(5)), D2 = Diagonal(rand(5))
@test_throws MethodError A_mul_B!(D1,D2)
@test_throws MethodError At_mul_B!(D1,D2)
@test_throws MethodError Ac_mul_B!(D1,D2)
end
@testset "multiplication of QR Q-factor and Diagonal (#16615 spot test)" begin
D = Diagonal(randn(5))
Q = qrfact(randn(5, 5))[:Q]
@test D * Q' == Array(D) * Q'
Q = qrfact(randn(5, 5), Val(true))[:Q]
@test_throws MethodError A_mul_B!(Q, D)
end
@testset "block diagonal matrices" begin
D = Diagonal([[1 2; 3 4], [1 2; 3 4]])
Dherm = Diagonal([[1 1+im; 1-im 1], [1 1+im; 1-im 1]])
Dsym = Diagonal([[1 1+im; 1+im 1], [1 1+im; 1+im 1]])
@test D' == Diagonal([[1 3; 2 4], [1 3; 2 4]])
@test D.' == Diagonal([[1 3; 2 4], [1 3; 2 4]])
@test Dherm' == Dherm
@test Dherm.' == Diagonal([[1 1-im; 1+im 1], [1 1-im; 1+im 1]])
@test Dsym' == Diagonal([[1 1-im; 1-im 1], [1 1-im; 1-im 1]])
@test Dsym.' == Dsym
v = [[1, 2], [3, 4]]
@test Dherm' * v == Dherm * v
@test D.' * v == [[7, 10], [15, 22]]
@test issymmetric(D) == false
@test issymmetric(Dherm) == false
@test issymmetric(Dsym) == true
@test ishermitian(D) == false
@test ishermitian(Dherm) == true
@test ishermitian(Dsym) == false
@test exp(D) == Diagonal([exp([1 2; 3 4]), exp([1 2; 3 4])])
@test log(D) == Diagonal([log([1 2; 3 4]), log([1 2; 3 4])])
@test sqrt(D) == Diagonal([sqrt([1 2; 3 4]), sqrt([1 2; 3 4])])
end
@testset "multiplication with Symmetric/Hermitian" begin
for T in (Float64, Complex128)
D = Diagonal(randn(T, n))
A = randn(T, n, n); A = A'A
S = Symmetric(A)
H = Hermitian(A)
for f in (*, Ac_mul_B, A_mul_Bc, Ac_mul_Bc, At_mul_B, A_mul_Bt, At_mul_Bt)
@test f(D, S) ≈ f(Matrix(D), Matrix(S))
@test f(D, H) ≈ f(Matrix(D), Matrix(H))
@test f(S, D) ≈ f(Matrix(S), Matrix(D))
@test f(S, H) ≈ f(Matrix(S), Matrix(H))
end
end
end
@testset "multiplication of transposes of Diagonal (#22428)" begin
for T in (Float64, Complex{Float64})
D = Diagonal(randn(T, 5, 5))
B = Diagonal(randn(T, 5, 5))
DD = Diagonal([randn(T, 2, 2), rand(T, 2, 2)])
BB = Diagonal([randn(T, 2, 2), rand(T, 2, 2)])
fullDD = copy!(Matrix{Matrix{T}}(2, 2), DD)
fullBB = copy!(Matrix{Matrix{T}}(2, 2), BB)
for f in (*, Ac_mul_B, A_mul_Bc, Ac_mul_Bc, At_mul_B, A_mul_Bt, At_mul_Bt)
@test f(D, B)::typeof(D) ≈ f(Matrix(D), Matrix(B)) atol=2 * eps()
@test f(DD, BB)::typeof(DD) == f(fullDD, fullBB)
end
end
end
@testset "Diagonal of a RowVector (#23649)" begin
@test Diagonal([1,2,3].') == Diagonal([1 2 3])
end