-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlq.jl
More file actions
221 lines (189 loc) · 6.89 KB
/
lq.jl
File metadata and controls
221 lines (189 loc) · 6.89 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
using MatrixAlgebraKit: MatrixAlgebraKit, lq_compact!, lq_full!
# TODO: this is a hardcoded for now to get around this function not being defined in the
# type domain
function default_blocksparse_lq_algorithm(A::AbstractMatrix; kwargs...)
blocktype(A) <: StridedMatrix{<:LinearAlgebra.BLAS.BlasFloat} ||
error("unsupported type: $(blocktype(A))")
alg = MatrixAlgebraKit.LAPACK_HouseholderLQ(; kwargs...)
return BlockPermutedDiagonalAlgorithm(alg)
end
function MatrixAlgebraKit.default_algorithm(
::typeof(lq_compact!), A::AbstractBlockSparseMatrix; kwargs...
)
return default_blocksparse_lq_algorithm(A; kwargs...)
end
function MatrixAlgebraKit.default_algorithm(
::typeof(lq_full!), A::AbstractBlockSparseMatrix; kwargs...
)
return default_blocksparse_lq_algorithm(A; kwargs...)
end
function similar_output(
::typeof(lq_compact!), A, L_axis, alg::MatrixAlgebraKit.AbstractAlgorithm
)
L = similar(A, axes(A, 1), L_axis)
Q = similar(A, L_axis, axes(A, 2))
return L, Q
end
function similar_output(
::typeof(lq_full!), A, L_axis, alg::MatrixAlgebraKit.AbstractAlgorithm
)
L = similar(A, axes(A, 1), L_axis)
Q = similar(A, L_axis, axes(A, 2))
return L, Q
end
function MatrixAlgebraKit.initialize_output(
::typeof(lq_compact!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
)
bm, bn = blocksize(A)
bmn = min(bm, bn)
brows = eachblockaxis(axes(A, 1))
bcols = eachblockaxis(axes(A, 2))
l_axes = similar(brows, 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))
len = minimum(length, (brows[row], bcols[col]))
l_axes[row] = bcols[col][Base.OneTo(len)]
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)
len = minimum(length, (brows[row], bcols[col]))
l_axes[row] = bcols[col][Base.OneTo(len)]
end
l_axis = mortar_axis(l_axes)
L, Q = similar_output(lq_compact!, A, l_axis, alg)
# allocate output
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
L[brow, brow], Q[brow, bcol] = MatrixAlgebraKit.initialize_output(
lq_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!(Q[Block(row, col)])
end
return L, Q
end
function MatrixAlgebraKit.initialize_output(
::typeof(lq_full!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
)
bm, bn = blocksize(A)
bcols = eachblockaxis(axes(A, 2))
l_axes = copy(bcols)
# 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))
l_axes[row] = bcols[col]
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)
l_axes[row] = bcols[col]
end
for (i, k) in enumerate((length(emptycols) + 1):length(emptyrows))
l_axes[bn + i] = bcols[emptycols[k]]
end
l_axis = mortar_axis(l_axes)
L, Q = similar_output(lq_full!, A, l_axis, alg)
# allocate output
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
L[brow, brow], Q[brow, bcol] = MatrixAlgebraKit.initialize_output(
lq_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!(Q[Block(row, col)])
end
# also handle extra rows/cols
for (i, k) in enumerate((length(emptyrows) + 1):length(emptycols))
@view!(Q[Block(bm + i, emptycols[k])])
end
return L, Q
end
function MatrixAlgebraKit.check_input(
::typeof(lq_compact!), A::AbstractBlockSparseMatrix, LQ
)
L, Q = LQ
@assert isa(L, AbstractBlockSparseMatrix) && isa(Q, AbstractBlockSparseMatrix)
@assert eltype(A) == eltype(L) == eltype(Q)
@assert axes(A, 1) == axes(L, 1) && axes(A, 2) == axes(Q, 2)
@assert axes(L, 2) == axes(Q, 1)
return nothing
end
function MatrixAlgebraKit.check_input(::typeof(lq_full!), A::AbstractBlockSparseMatrix, LQ)
L, Q = LQ
@assert isa(L, AbstractBlockSparseMatrix) && isa(Q, AbstractBlockSparseMatrix)
@assert eltype(A) == eltype(L) == eltype(Q)
@assert axes(A, 1) == axes(L, 1) && axes(A, 2) == axes(Q, 2)
@assert axes(L, 2) == axes(Q, 1)
return nothing
end
function MatrixAlgebraKit.lq_compact!(
A::AbstractBlockSparseMatrix, LQ, alg::BlockPermutedDiagonalAlgorithm
)
MatrixAlgebraKit.check_input(lq_compact!, A, LQ)
L, Q = LQ
# do decomposition on each block
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
lq = (@view!(L[brow, brow]), @view!(Q[brow, bcol]))
lq′ = lq_compact!(@view!(A[bI]), lq, alg.alg)
@assert lq === lq′ "lq_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
# Q[Block(row, col)] = LinearAlgebra.I
for (row, col) in zip(emptyrows, emptycols)
copyto!(@view!(Q[Block(row, col)]), LinearAlgebra.I)
end
return LQ
end
function MatrixAlgebraKit.lq_full!(
A::AbstractBlockSparseMatrix, LQ, alg::BlockPermutedDiagonalAlgorithm
)
MatrixAlgebraKit.check_input(lq_full!, A, LQ)
L, Q = LQ
# do decomposition on each block
for bI in eachblockstoredindex(A)
brow, bcol = Tuple(bI)
lq = (@view!(L[brow, brow]), @view!(Q[brow, bcol]))
lq′ = lq_full!(@view!(A[bI]), lq, alg.alg)
@assert lq === lq′ "lq_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
# Q[Block(row, col)] = LinearAlgebra.I
for (row, col) in zip(emptyrows, emptycols)
copyto!(@view!(Q[Block(row, col)]), LinearAlgebra.I)
end
# also handle extra rows/cols
bm = blocksize(A, 1)
for (i, k) in enumerate((length(emptyrows) + 1):length(emptycols))
copyto!(@view!(Q[Block(bm + i, emptycols[k])]), LinearAlgebra.I)
end
return LQ
end