-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathqr.jl
More file actions
167 lines (149 loc) · 5.38 KB
/
Copy pathqr.jl
File metadata and controls
167 lines (149 loc) · 5.38 KB
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
using MatrixAlgebraKit
using MatrixAlgebraKit: diagview
using Test
using TestExtras
using StableRNGs
using AMDGPU
using LinearAlgebra
include(joinpath("..", "utilities.jl"))
BLASFloats = (Float32, Float64, ComplexF32, ComplexF64)
@testset "qr_compact! and qr_null! for T = $T" for T in BLASFloats
rng = StableRNG(123)
m = 54
for n in (37, m, 63)
minmn = min(m, n)
A = ROCArray(randn(rng, T, m, n))
Q, R = @constinferred qr_compact(A)
@test Q isa ROCMatrix{T} && size(Q) == (m, minmn)
@test R isa ROCMatrix{T} && size(R) == (minmn, n)
@test Q * R ≈ A
N = @constinferred qr_null(A)
@test N isa ROCMatrix{T} && size(N) == (m, m - minmn)
@test isapproxone(Q' * Q)
@test maximum(abs, A' * N) < eps(real(T))^(2 / 3)
@test isapproxone(N' * N)
Ac = similar(A)
Q2, R2 = @constinferred qr_compact!(copy!(Ac, A), (Q, R))
@test Q2 === Q
@test R2 === R
N2 = @constinferred qr_null!(copy!(Ac, A), N)
@test N2 === N
# noR
Q2 = similar(Q)
noR = similar(A, minmn, 0)
qr_compact!(copy!(Ac, A), (Q2, noR))
@test Q == Q2
# positive
qr_compact!(copy!(Ac, A), (Q, R); positive = true)
@test Q * R ≈ A
@test isapproxone(Q' * Q)
@test all(>=(zero(real(T))), real(diagview(R)))
qr_compact!(copy!(Ac, A), (Q2, noR); positive = true)
@test Q == Q2
# explicit blocksize
qr_compact!(copy!(Ac, A), (Q, R); blocksize = 1)
@test Q * R ≈ A
@test isapproxone(Q' * Q)
qr_compact!(copy!(Ac, A), (Q2, noR); blocksize = 1)
@test Q == Q2
qr_compact!(copy!(Ac, A), (Q2, noR); blocksize = 1)
qr_null!(copy!(Ac, A), N; blocksize = 1)
@test maximum(abs, A' * N) < eps(real(T))^(2 / 3)
@test isapproxone(N' * N)
if n <= m
qr_compact!(copy!(Q2, A), (Q2, noR); blocksize = 1) # in-place Q
@test Q ≈ Q2
# these do not work because of the in-place Q
@test_throws ArgumentError qr_compact!(copy!(Q2, A), (Q2, R2))
@test_throws ArgumentError qr_compact!(copy!(Q2, A), (Q2, noR); positive = true)
end
# no blocked CUDA
@test_throws ArgumentError qr_compact!(copy!(Ac, A), (Q2, R); blocksize = 8)
end
end
@testset "qr_full! for T = $T" for T in BLASFloats
rng = StableRNG(123)
m = 63
for n in (37, m, 63)
minmn = min(m, n)
A = ROCArray(randn(rng, T, m, n))
Q, R = qr_full(A)
@test Q isa ROCMatrix{T} && size(Q) == (m, m)
@test R isa ROCMatrix{T} && size(R) == (m, n)
@test Q * R ≈ A
@test isapproxone(Q' * Q)
Ac = similar(A)
Q2 = similar(Q)
noR = similar(A, m, 0)
Q2, R2 = @constinferred qr_full!(copy!(Ac, A), (Q, R))
@test Q2 === Q
@test R2 === R
@test Q * R ≈ A
@test isapproxone(Q' * Q)
qr_full!(copy!(Ac, A), (Q2, noR))
@test Q == Q2
# noR
noR = similar(A, m, 0)
Q2 = similar(Q)
qr_full!(copy!(Ac, A), (Q2, noR))
@test Q == Q2
# positive
qr_full!(copy!(Ac, A), (Q, R); positive = true)
@test Q * R ≈ A
@test isapproxone(Q' * Q)
@test all(>=(zero(real(T))), real(diagview(R)))
qr_full!(copy!(Ac, A), (Q2, noR); positive = true)
@test Q == Q2
# explicit blocksize
qr_full!(copy!(Ac, A), (Q, R); blocksize = 1)
@test Q * R ≈ A
@test isapproxone(Q' * Q)
qr_full!(copy!(Ac, A), (Q2, noR); blocksize = 1)
@test Q == Q2
if n == m
qr_full!(copy!(Q2, A), (Q2, noR); blocksize = 1) # in-place Q
@test Q ≈ Q2
@test_throws ArgumentError qr_full!(copy!(Q2, A), (Q2, R2))
@test_throws ArgumentError qr_full!(copy!(Q2, A), (Q2, noR); positive = true)
end
# no blocked CUDA
@test_throws ArgumentError qr_full!(copy!(Ac, A), (Q, R); blocksize = 8)
end
end
@testset "qr_compact, qr_full and qr_null for Diagonal{$T}" for T in BLASFloats
rng = StableRNG(123)
atol = eps(real(T))^(3 / 4)
for m in (54, 0)
Ad = ROCArray(randn(rng, T, m))
A = Diagonal(Ad)
# compact
Q, R = @constinferred qr_compact(A)
@test Q isa Diagonal{T} && size(Q) == (m, m)
@test R isa Diagonal{T} && size(R) == (m, m)
@test Q * R ≈ A
@test isunitary(Q)
# compact and positive
Qp, Rp = @constinferred qr_compact(A; positive = true)
@test Qp isa Diagonal{T} && size(Qp) == (m, m)
@test Rp isa Diagonal{T} && size(Rp) == (m, m)
@test Qp * Rp ≈ A
@test isunitary(Qp)
@test all(isposdef.(diagview(Rp)))
# full
Q, R = @constinferred qr_full(A)
@test Q isa Diagonal{T} && size(Q) == (m, m)
@test R isa Diagonal{T} && size(R) == (m, m)
@test Q * R ≈ A
@test isunitary(Q)
# full and positive
Qp, Rp = @constinferred qr_full(A; positive = true)
@test Qp isa Diagonal{T} && size(Qp) == (m, m)
@test Rp isa Diagonal{T} && size(Rp) == (m, m)
@test Qp * Rp ≈ A
@test isunitary(Qp)
@test all(isposdef.(diagview(Rp)))
# null
N = @constinferred qr_null(A)
@test N isa AbstractMatrix{T} && size(N) == (m, 0)
end
end