-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdecompositions.jl
More file actions
512 lines (412 loc) · 18.5 KB
/
decompositions.jl
File metadata and controls
512 lines (412 loc) · 18.5 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# TODO: module Decompositions?
# =================
# LAPACK ALGORITHMS
# =================
# reference for naming LAPACK algorithms:
# https://www.netlib.org/lapack/explore-html/topics.html
# QR, LQ, QL, RQ Decomposition
# ----------------------------
"""
Native_HouseholderQR()
Algorithm type to denote a native implementation for computing the QR decomposition of
a matrix using Householder reflectors. The diagonal elements of `R` will be non-negative
by construction.
"""
@algdef Native_HouseholderQR
"""
Native_HouseholderLQ()
Algorithm type to denote a native implementation for computing the LQ decomposition of
a matrix using Householder reflectors. The diagonal elements of `L` will be non-negative
by construction.
"""
@algdef Native_HouseholderLQ
"""
LAPACK_HouseholderQR(; blocksize, positive = false, pivoted = false)
Algorithm type to denote the standard LAPACK algorithm for computing the QR decomposition of
a matrix using Householder reflectors. The specific LAPACK function can be controlled using
the keyword arugments, i.e. `?geqrt` will be chosen if `blocksize > 1`. With
`blocksize == 1`, `?geqrf` will be chosen if `pivoted == false` and `?geqp3` will be chosen
if `pivoted == true`. The keyword `positive = true` can be used to ensure that the diagonal
elements of `R` are non-negative.
"""
@algdef LAPACK_HouseholderQR
"""
LAPACK_HouseholderLQ(; blocksize, positive = false)
Algorithm type to denote the standard LAPACK algorithm for computing the LQ decomposition of
a matrix using Householder reflectors. The specific LAPACK function can be controlled using
the keyword arugments, i.e. `?gelqt` will be chosen if `blocksize > 1` or `?gelqf` will be
chosen if `blocksize == 1`. The keyword `positive = true` can be used to ensure that the diagonal
elements of `L` are non-negative.
"""
@algdef LAPACK_HouseholderLQ
"""
GLA_HouseholderQR(; positive = false)
Algorithm type to denote the GenericLinearAlgebra.jl implementation for computing the QR decomposition
of a matrix using Householder reflectors. Currently, only `blocksize = 1` and `pivoted == false`
are supported. The keyword `positive = true` can be used to ensure that the diagonal elements
of `R` are non-negative.
"""
@algdef GLA_HouseholderQR
# TODO:
@algdef LAPACK_HouseholderQL
@algdef LAPACK_HouseholderRQ
# General Eigenvalue Decomposition
# -------------------------------
"""
LAPACK_Simple(; fixgauge::Bool = true)
Algorithm type to denote the simple LAPACK driver for computing the Schur or non-Hermitian
eigenvalue decomposition of a matrix.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigenvectors,
see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_Simple
"""
LAPACK_Expert(; fixgauge::Bool = true)
Algorithm type to denote the expert LAPACK driver for computing the Schur or non-Hermitian
eigenvalue decomposition of a matrix.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigenvectors,
see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_Expert
const LAPACK_EigAlgorithm = Union{LAPACK_Simple, LAPACK_Expert}
"""
GS_QRIteration()
Algorithm type to denote the GenericSchur.jl implementation for computing the
eigenvalue decomposition of a non-Hermitian matrix.
"""
@algdef GS_QRIteration
# Hermitian Eigenvalue Decomposition
# ----------------------------------
"""
LAPACK_QRIteration(; fixgauge::Bool = true)
Algorithm type to denote the LAPACK driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
QR Iteration algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_QRIteration
"""
LAPACK_Bisection(; fixgauge::Bool = true)
Algorithm type to denote the LAPACK driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
Bisection algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_Bisection
"""
LAPACK_DivideAndConquer(; fixgauge::Bool = true)
Algorithm type to denote the LAPACK driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
Divide and Conquer algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_DivideAndConquer
"""
LAPACK_MultipleRelativelyRobustRepresentations(; fixgauge::Bool = true)
Algorithm type to denote the LAPACK driver for computing the eigenvalue decomposition of a
Hermitian matrix using the Multiple Relatively Robust Representations algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigenvectors,
see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_MultipleRelativelyRobustRepresentations
const LAPACK_EighAlgorithm = Union{
LAPACK_QRIteration,
LAPACK_Bisection,
LAPACK_DivideAndConquer,
LAPACK_MultipleRelativelyRobustRepresentations,
}
"""
GLA_QRIteration(; fixgauge::Bool = true)
Algorithm type to denote the GenericLinearAlgebra.jl implementation for computing the
eigenvalue decomposition of a Hermitian matrix, or the singular value decomposition of
a general matrix.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef GLA_QRIteration
# Singular Value Decomposition
# ----------------------------
"""
LAPACK_Jacobi(; fixgauge::Bool = true)
Algorithm type to denote the LAPACK driver for computing the singular value decomposition of
a general matrix using the Jacobi algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the singular vectors,
see also [`gaugefix!`](@ref).
"""
@algdef LAPACK_Jacobi
const LAPACK_SVDAlgorithm = Union{
LAPACK_QRIteration,
LAPACK_Bisection,
LAPACK_DivideAndConquer,
LAPACK_Jacobi,
}
# =========================
# Polar decompositions
# =========================
"""
PolarViaSVD(svd_alg)
Algorithm for computing the polar decomposition of a matrix `A` via the singular value
decomposition (SVD) of `A`. The `svd_alg` argument specifies the SVD algorithm to use.
"""
struct PolarViaSVD{SVDAlg} <: AbstractAlgorithm
svd_alg::SVDAlg
end
"""
PolarNewton(; maxiter = 10, tol = defaulttol(A))
Algorithm for computing the polar decomposition of a matrix `A` via
scaled Newton iteration, with a maximum of `maxiter` iterations and
until convergence up to tolerance `tol`.
"""
@algdef PolarNewton
# =========================
# Varia
# =========================
"""
DiagonalAlgorithm(; kwargs...)
Algorithm type to denote a native Julia implementation of the decompositions making use of
the diagonal structure of the input and outputs.
"""
@algdef DiagonalAlgorithm
"""
LQViaTransposedQR(qr_alg)
Algorithm type to denote finding the LQ decomposition of `A` by computing the QR decomposition of `Aᵀ`.
The `qr_alg` specifies which QR-decomposition implementation to use.
"""
struct LQViaTransposedQR{A <: AbstractAlgorithm} <: AbstractAlgorithm
qr_alg::A
end
function Base.show(io::IO, alg::LQViaTransposedQR)
print(io, "LQViaTransposedQR(")
_show_alg(io, alg.qr_alg)
return print(io, ")")
end
# =========================
# CUSOLVER ALGORITHMS
# =========================
"""
CUSOLVER_HouseholderQR(; positive = false)
Algorithm type to denote the standard CUSOLVER algorithm for computing the QR decomposition of
a matrix using Householder reflectors. The keyword `positive = true` can be used to ensure that
the diagonal elements of `R` are non-negative.
"""
@algdef CUSOLVER_HouseholderQR
"""
CUSOLVER_QRIteration(; fixgauge::Bool = true)
Algorithm type to denote the CUSOLVER driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
QR Iteration algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef CUSOLVER_QRIteration
"""
CUSOLVER_SVDPolar(; fixgauge::Bool = true)
Algorithm type to denote the CUSOLVER driver for computing the singular value decomposition of
a general matrix by using Halley's iterative algorithm to compute the polar decompositon,
followed by the hermitian eigenvalue decomposition of the positive definite factor.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the singular
vectors, see also [`gaugefix!`](@ref).
"""
@algdef CUSOLVER_SVDPolar
"""
CUSOLVER_Jacobi(; fixgauge::Bool = true)
Algorithm type to denote the CUSOLVER driver for computing the singular value decomposition of
a general matrix using the Jacobi algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the singular
vectors, see also [`gaugefix!`](@ref).
"""
@algdef CUSOLVER_Jacobi
"""
CUSOLVER_Randomized(; k, p, niters)
Algorithm type to denote the CUSOLVER driver for computing the singular value decomposition of
a general matrix using the randomized SVD algorithm. Here, `k` denotes the number of singular
values that should be computed, therefore requiring `k <= min(size(A))`. This method is accurate
for small values of `k` compared to the size of the input matrix, where the accuracy can be
improved by increasing `p`, the number of additional values used for oversampling,
and `niters`, the number of iterations the solver uses, at the cost of increasing the runtime.
See also the [CUSOLVER documentation](https://docs.nvidia.com/cuda/cusolver/index.html#cusolverdnxgesvdr)
for more information.
"""
@algdef CUSOLVER_Randomized
does_truncate(::TruncatedAlgorithm{<:CUSOLVER_Randomized}) = true
"""
CUSOLVER_Simple(; fixgauge::Bool = true)
Algorithm type to denote the simple CUSOLVER driver for computing the non-Hermitian
eigenvalue decomposition of a matrix.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigenvectors,
see also [`gaugefix!`](@ref).
"""
@algdef CUSOLVER_Simple
const CUSOLVER_EigAlgorithm = Union{CUSOLVER_Simple}
"""
CUSOLVER_DivideAndConquer(; fixgauge::Bool = true)
Algorithm type to denote the CUSOLVER driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
Divide and Conquer algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef CUSOLVER_DivideAndConquer
const CUSOLVER_SVDAlgorithm = Union{
CUSOLVER_QRIteration, CUSOLVER_SVDPolar, CUSOLVER_Jacobi, CUSOLVER_Randomized,
}
# =========================
# ROCSOLVER ALGORITHMS
# =========================
"""
ROCSOLVER_HouseholderQR(; positive = false)
Algorithm type to denote the standard ROCSOLVER algorithm for computing the QR decomposition of
a matrix using Householder reflectors. The keyword `positive=true` can be used to ensure that
the diagonal elements of `R` are non-negative.
"""
@algdef ROCSOLVER_HouseholderQR
"""
ROCSOLVER_QRIteration(; fixgauge::Bool = true)
Algorithm type to denote the ROCSOLVER driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
QR Iteration algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef ROCSOLVER_QRIteration
"""
ROCSOLVER_Jacobi(; fixgauge::Bool = true)
Algorithm type to denote the ROCSOLVER driver for computing the singular value decomposition of
a general matrix using the Jacobi algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the singular
vectors, see also [`gaugefix!`](@ref).
"""
@algdef ROCSOLVER_Jacobi
"""
ROCSOLVER_Bisection(; fixgauge::Bool = true)
Algorithm type to denote the ROCSOLVER driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
Bisection algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef ROCSOLVER_Bisection
"""
ROCSOLVER_DivideAndConquer(; fixgauge::Bool = true)
Algorithm type to denote the ROCSOLVER driver for computing the eigenvalue decomposition of a
Hermitian matrix, or the singular value decomposition of a general matrix using the
Divide and Conquer algorithm.
The `fixgauge` keyword can be used to toggle whether or not to fix the gauge of the eigen or
singular vectors, see also [`gaugefix!`](@ref).
"""
@algdef ROCSOLVER_DivideAndConquer
const ROCSOLVER_SVDAlgorithm = Union{ROCSOLVER_QRIteration, ROCSOLVER_Jacobi}
# Various consts and unions
# -------------------------
const GPU_Simple = Union{CUSOLVER_Simple}
const GPU_EigAlgorithm = Union{GPU_Simple}
const GPU_QRIteration = Union{CUSOLVER_QRIteration, ROCSOLVER_QRIteration}
const GPU_Jacobi = Union{CUSOLVER_Jacobi, ROCSOLVER_Jacobi}
const GPU_DivideAndConquer = Union{CUSOLVER_DivideAndConquer, ROCSOLVER_DivideAndConquer}
const GPU_Bisection = Union{ROCSOLVER_Bisection}
const GPU_EighAlgorithm = Union{
GPU_QRIteration, GPU_Jacobi, GPU_DivideAndConquer, GPU_Bisection,
}
const GPU_SVDAlgorithm = Union{CUSOLVER_SVDAlgorithm, ROCSOLVER_SVDAlgorithm}
const GPU_SVDPolar = Union{CUSOLVER_SVDPolar}
const GPU_Randomized = Union{CUSOLVER_Randomized}
const QRAlgorithms = Union{LAPACK_HouseholderQR, CUSOLVER_HouseholderQR, ROCSOLVER_HouseholderQR}
const LQAlgorithms = Union{LAPACK_HouseholderLQ, LQViaTransposedQR}
const SVDAlgorithms = Union{LAPACK_SVDAlgorithm, GPU_SVDAlgorithm}
const PolarAlgorithms = Union{PolarViaSVD, PolarNewton}
# ================================
# ORTHOGONALIZATION ALGORITHMS
# ================================
"""
LeftOrthAlgorithm{Kind, Alg <: AbstractAlgorithm}(alg)
Wrapper type to denote the `Kind` of factorization that is used as a backend for [`left_orth`](@ref).
By default `Kind` is a symbol, which can be either `:qr`, `:polar` or `:svd`.
"""
struct LeftOrthAlgorithm{Kind, Alg <: AbstractAlgorithm} <: AbstractAlgorithm
alg::Alg
end
LeftOrthAlgorithm{Kind}(alg::Alg) where {Kind, Alg <: AbstractAlgorithm} = LeftOrthAlgorithm{Kind, Alg}(alg)
# Note: specific algorithm selection is handled by `left_orth_alg` in orthnull.jl
LeftOrthAlgorithm(alg::AbstractAlgorithm) = error(
"""
Unknown or invalid `left_orth` algorithm type `$(typeof(alg))`.
To register the algorithm type for `left_orth`, define
MatrixAlgebraKit.left_orth_alg(alg::CustomAlgorithm) = LeftOrthAlgorithm{kind}(alg)
where `kind` selects the factorization type that will be used.
By default, this is either `:qr`, `:polar` or `:svd`, to select [`qr_compact!`](@ref),
[`left_polar!`](@ref), [`svd_compact!`](@ref) or [`svd_trunc!`](@ref) respectively.
"""
)
const LeftOrthViaQR = LeftOrthAlgorithm{:qr}
const LeftOrthViaPolar = LeftOrthAlgorithm{:polar}
const LeftOrthViaSVD = LeftOrthAlgorithm{:svd}
"""
RightOrthAlgorithm{Kind, Alg <: AbstractAlgorithm}(alg)
Wrapper type to denote the `Kind` of factorization that is used as a backend for [`right_orth`](@ref).
By default `Kind` is a symbol, which can be either `:lq`, `:polar` or `:svd`.
"""
struct RightOrthAlgorithm{Kind, Alg <: AbstractAlgorithm} <: AbstractAlgorithm
alg::Alg
end
RightOrthAlgorithm{Kind}(alg::Alg) where {Kind, Alg <: AbstractAlgorithm} = RightOrthAlgorithm{Kind, Alg}(alg)
# Note: specific algorithm selection is handled by `right_orth_alg` in orthnull.jl
RightOrthAlgorithm(alg::AbstractAlgorithm) = error(
"""
Unknown or invalid `right_orth` algorithm type `$(typeof(alg))`.
To register the algorithm type for `right_orth`, define
MatrixAlgebraKit.right_orth_alg(alg::CustomAlgorithm) = RightOrthAlgorithm{kind}(alg)
where `kind` selects the factorization type that will be used.
By default, this is either `:lq`, `:polar` or `:svd`, to select [`lq_compact!`](@ref),
[`right_polar!`](@ref), [`svd_compact!`](@ref) or [`svd_trunc!`](@ref) respectively.
"""
)
const RightOrthViaLQ = RightOrthAlgorithm{:lq}
const RightOrthViaPolar = RightOrthAlgorithm{:polar}
const RightOrthViaSVD = RightOrthAlgorithm{:svd}
"""
LeftNullAlgorithm{Kind, Alg <: AbstractAlgorithm}(alg)
Wrapper type to denote the `Kind` of factorization that is used as a backend for [`left_null`](@ref).
By default `Kind` is a symbol, which can be either `:qr` or `:svd`.
"""
struct LeftNullAlgorithm{Kind, Alg <: AbstractAlgorithm} <: AbstractAlgorithm
alg::Alg
end
LeftNullAlgorithm{Kind}(alg::Alg) where {Kind, Alg <: AbstractAlgorithm} = LeftNullAlgorithm{Kind, Alg}(alg)
# Note: specific algorithm selection is handled by `left_null_alg` in orthnull.jl
LeftNullAlgorithm(alg::AbstractAlgorithm) = error(
"""
Unknown or invalid `left_null` algorithm type `$(typeof(alg))`.
To register the algorithm type for `left_null`, define
MatrixAlgebraKit.left_null_alg(alg::CustomAlgorithm) = LeftNullAlgorithm{kind}(alg)
where `kind` selects the factorization type that will be used.
By default, this is either `:qr` or `:svd`, to select [`qr_null!`](@ref),
[`svd_compact!`](@ref) or [`svd_trunc!`](@ref) respectively.
"""
)
const LeftNullViaQR = LeftNullAlgorithm{:qr}
const LeftNullViaSVD = LeftNullAlgorithm{:svd}
"""
RightNullAlgorithm{Kind, Alg <: AbstractAlgorithm}(alg)
Wrapper type to denote the `Kind` of factorization that is used as a backend for [`right_null`](@ref).
By default `Kind` is a symbol, which can be either `:lq` or `:svd`.
"""
struct RightNullAlgorithm{Kind, Alg <: AbstractAlgorithm} <: AbstractAlgorithm
alg::Alg
end
RightNullAlgorithm{Kind}(alg::Alg) where {Kind, Alg <: AbstractAlgorithm} = RightNullAlgorithm{Kind, Alg}(alg)
# Note: specific algorithm selection is handled by `right_null_alg` in orthnull.jl
RightNullAlgorithm(alg::AbstractAlgorithm) = error(
"""
Unknown or invalid `right_null` algorithm type `$(typeof(alg))`.
To register the algorithm type for `right_null`, define
MatrixAlgebraKit.right_null_alg(alg::CustomAlgorithm) = RightNullAlgorithm{kind}(alg)
where `kind` selects the factorization type that will be used.
By default, this is either `:lq` or `:svd`, to select [`lq_null!`](@ref),
[`svd_compact!`](@ref) or [`svd_trunc!`](@ref) respectively.
"""
)
const RightNullViaLQ = RightNullAlgorithm{:lq}
const RightNullViaSVD = RightNullAlgorithm{:svd}