-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsvd.jl
More file actions
253 lines (219 loc) · 8.27 KB
/
svd.jl
File metadata and controls
253 lines (219 loc) · 8.27 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
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
using MatrixAlgebraKit: MatrixAlgebraKit, svd_compact!, svd_full!
"""
BlockPermutedDiagonalAlgorithm(A::MatrixAlgebraKit.AbstractAlgorithm)
A wrapper for `MatrixAlgebraKit.AbstractAlgorithm` that implements the wrapped algorithm on
a block-by-block basis, which is possible if the input matrix is a block-diagonal matrix or
a block permuted block-diagonal matrix.
"""
struct BlockPermutedDiagonalAlgorithm{A<:MatrixAlgebraKit.AbstractAlgorithm} <:
MatrixAlgebraKit.AbstractAlgorithm
alg::A
end
# TODO: this is a hardcoded for now to get around this function not being defined in the
# type domain
function MatrixAlgebraKit.default_svd_algorithm(A::AbstractBlockSparseMatrix; kwargs...)
blocktype(A) <: StridedMatrix{<:LinearAlgebra.BLAS.BlasFloat} ||
error("unsupported type: $(blocktype(A))")
alg = MatrixAlgebraKit.LAPACK_DivideAndConquer(; kwargs...)
return BlockPermutedDiagonalAlgorithm(alg)
end
function similar_output(
::typeof(svd_compact!),
A,
s_axis::AbstractUnitRange,
alg::MatrixAlgebraKit.AbstractAlgorithm,
)
U = similar(A, axes(A, 1), s_axis)
T = real(eltype(A))
# TODO: this should be replaced with a more general similar function that can handle setting
# the blocktype and element type - something like S = similar(A, BlockType(...))
S = BlockSparseMatrix{T,Diagonal{T,Vector{T}}}(undef, (s_axis, s_axis))
Vt = similar(A, s_axis, axes(A, 2))
return U, S, Vt
end
function MatrixAlgebraKit.initialize_output(
::typeof(svd_compact!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
)
bm, bn = blocksize(A)
bmn = min(bm, bn)
brows = eachblockaxis(axes(A, 1))
bcols = eachblockaxis(axes(A, 2))
s_axeses = Vector{eltype(brows)}(undef, bmn)
# fill in values for blocks that are present
bIs = collect(eachblockstoredindex(A))
browIs = Int.(first.(Tuple.(bIs)))
bcolIs = Int.(last.(Tuple.(bIs)))
for bI in eachblockstoredindex(A)
row, col = Int.(Tuple(bI))
nrows = brows[row]
ncols = bcols[col]
s_axeses[col] = min(nrows, ncols)
end
# fill in values for blocks that aren't present, pairing them in order of occurence
# this is a convention, which at least gives the expected results for blockdiagonal
emptyrows = setdiff(1:bm, browIs)
emptycols = setdiff(1:bn, bcolIs)
for (row, col) in zip(emptyrows, emptycols)
s_axeses[col] = min(brows[row], bcols[col])
end
s_axis = mortar_axis(s_axeses)
U, S, Vt = similar_output(svd_compact!, A, s_axis, alg)
# allocate output
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
U[brow, bcol], S[bcol, bcol], Vt[bcol, bcol] = MatrixAlgebraKit.initialize_output(
svd_compact!, @view!(A[bI]), alg.alg
)
end
# allocate output for blocks that aren't present -- do we also fill identities here?
for (row, col) in zip(emptyrows, emptycols)
@view!(U[Block(row, col)])
@view!(Vt[Block(col, col)])
end
return U, S, Vt
end
function similar_output(
::typeof(svd_full!), A, s_axis::AbstractUnitRange, alg::MatrixAlgebraKit.AbstractAlgorithm
)
U = similar(A, axes(A, 1), s_axis)
T = real(eltype(A))
S = similar(A, T, (s_axis, axes(A, 2)))
Vt = similar(A, axes(A, 2), axes(A, 2))
return U, S, Vt
end
function MatrixAlgebraKit.initialize_output(
::typeof(svd_full!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
)
bm, bn = blocksize(A)
brows = eachblockaxis(axes(A, 1))
s_axes = copy(brows)
# fill in values for blocks that are present
bIs = collect(eachblockstoredindex(A))
browIs = Int.(first.(Tuple.(bIs)))
bcolIs = Int.(last.(Tuple.(bIs)))
for bI in eachblockstoredindex(A)
row, col = Int.(Tuple(bI))
nrows = brows[row]
s_axes[col] = nrows
end
# fill in values for blocks that aren't present, pairing them in order of occurence
# this is a convention, which at least gives the expected results for blockdiagonal
emptyrows = setdiff(1:bm, browIs)
emptycols = setdiff(1:bn, bcolIs)
for (row, col) in zip(emptyrows, emptycols)
s_axes[col] = brows[row]
end
for (i, k) in enumerate((length(emptycols) + 1):length(emptyrows))
s_axes[bn + i] = brows[emptyrows[k]]
end
s_axis = mortar_axis(s_axes)
U, S, Vt = similar_output(svd_full!, A, s_axis, alg)
# allocate output
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
U[brow, bcol], S[bcol, bcol], Vt[bcol, bcol] = MatrixAlgebraKit.initialize_output(
svd_full!, @view!(A[bI]), alg.alg
)
end
# allocate output for blocks that aren't present -- do we also fill identities here?
for (row, col) in zip(emptyrows, emptycols)
@view!(U[Block(row, col)])
@view!(Vt[Block(col, col)])
end
# also handle extra rows/cols
for i in (length(emptyrows) + 1):length(emptycols)
@view!(Vt[Block(emptycols[i], emptycols[i])])
end
for (i, k) in enumerate((length(emptycols) + 1):length(emptyrows))
@view!(U[Block(emptyrows[k], bn + i)])
end
return U, S, Vt
end
function MatrixAlgebraKit.check_input(
::typeof(svd_compact!), A::AbstractBlockSparseMatrix, USVᴴ
)
U, S, Vt = USVᴴ
@assert isa(U, AbstractBlockSparseMatrix) &&
isa(S, AbstractBlockSparseMatrix) &&
isa(Vt, AbstractBlockSparseMatrix)
@assert eltype(A) == eltype(U) == eltype(Vt)
@assert real(eltype(A)) == eltype(S)
@assert axes(A, 1) == axes(U, 1) && axes(A, 2) == axes(Vt, 2)
@assert axes(S, 1) == axes(S, 2)
return nothing
end
function MatrixAlgebraKit.check_input(
::typeof(svd_full!), A::AbstractBlockSparseMatrix, USVᴴ
)
U, S, Vt = USVᴴ
@assert isa(U, AbstractBlockSparseMatrix) &&
isa(S, AbstractBlockSparseMatrix) &&
isa(Vt, AbstractBlockSparseMatrix)
@assert eltype(A) == eltype(U) == eltype(Vt)
@assert real(eltype(A)) == eltype(S)
@assert axes(A, 1) == axes(U, 1) && axes(A, 2) == axes(Vt, 1) == axes(Vt, 2)
@assert axes(S, 2) == axes(A, 2)
return nothing
end
function MatrixAlgebraKit.svd_compact!(
A::AbstractBlockSparseMatrix, USVᴴ, alg::BlockPermutedDiagonalAlgorithm
)
MatrixAlgebraKit.check_input(svd_compact!, A, USVᴴ)
U, S, Vt = USVᴴ
# do decomposition on each block
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
usvᴴ = (@view!(U[brow, bcol]), @view!(S[bcol, bcol]), @view!(Vt[bcol, bcol]))
usvᴴ′ = svd_compact!(@view!(A[bI]), usvᴴ, alg.alg)
@assert usvᴴ === usvᴴ′ "svd_compact! might not be in-place"
end
# fill in identities for blocks that aren't present
bIs = collect(eachblockstoredindex(A))
browIs = Int.(first.(Tuple.(bIs)))
bcolIs = Int.(last.(Tuple.(bIs)))
emptyrows = setdiff(1:blocksize(A, 1), browIs)
emptycols = setdiff(1:blocksize(A, 2), bcolIs)
# needs copyto! instead because size(::LinearAlgebra.I) doesn't work
# U[Block(row, col)] = LinearAlgebra.I
# Vt[Block(col, col)] = LinearAlgebra.I
for (row, col) in zip(emptyrows, emptycols)
copyto!(@view!(U[Block(row, col)]), LinearAlgebra.I)
copyto!(@view!(Vt[Block(col, col)]), LinearAlgebra.I)
end
return USVᴴ
end
function MatrixAlgebraKit.svd_full!(
A::AbstractBlockSparseMatrix, USVᴴ, alg::BlockPermutedDiagonalAlgorithm
)
MatrixAlgebraKit.check_input(svd_full!, A, USVᴴ)
U, S, Vt = USVᴴ
# do decomposition on each block
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
usvᴴ = (@view!(U[brow, bcol]), @view!(S[bcol, bcol]), @view!(Vt[bcol, bcol]))
usvᴴ′ = svd_full!(@view!(A[bI]), usvᴴ, alg.alg)
@assert usvᴴ === usvᴴ′ "svd_full! might not be in-place"
end
# fill in identities for blocks that aren't present
bIs = collect(eachblockstoredindex(A))
browIs = Int.(first.(Tuple.(bIs)))
bcolIs = Int.(last.(Tuple.(bIs)))
emptyrows = setdiff(1:blocksize(A, 1), browIs)
emptycols = setdiff(1:blocksize(A, 2), bcolIs)
# needs copyto! instead because size(::LinearAlgebra.I) doesn't work
# U[Block(row, col)] = LinearAlgebra.I
# Vt[Block(col, col)] = LinearAlgebra.I
for (row, col) in zip(emptyrows, emptycols)
copyto!(@view!(U[Block(row, col)]), LinearAlgebra.I)
copyto!(@view!(Vt[Block(col, col)]), LinearAlgebra.I)
end
# also handle extra rows/cols
for i in (length(emptyrows) + 1):length(emptycols)
copyto!(@view!(Vt[Block(emptycols[i], emptycols[i])]), LinearAlgebra.I)
end
bn = blocksize(A, 2)
for (i, k) in enumerate((length(emptycols) + 1):length(emptyrows))
copyto!(@view!(U[Block(emptyrows[k], bn + i)]), LinearAlgebra.I)
end
return USVᴴ
end