Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MatrixAlgebraKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export LAPACK_HouseholderQR, LAPACK_HouseholderLQ, LAPACK_Simple, LAPACK_Expert,
export GLA_HouseholderQR, GLA_QRIteration, GS_QRIteration
export LQViaTransposedQR
export PolarViaSVD, PolarNewton
export DefaultAlgorithm
export DiagonalAlgorithm
export NativeBlocked
export CUSOLVER_Simple, CUSOLVER_HouseholderQR, CUSOLVER_QRIteration, CUSOLVER_SVDPolar,
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/eig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ function initialize_output(::typeof(eig_vals!), A::Diagonal, ::DiagonalAlgorithm
return T <: Complex ? diagview(A) : similar(A, complex(T), size(A, 1))
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:eig_full!, :eig_vals!, :eig_trunc!, :eig_trunc_no_error!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation
# --------------
function eig_full!(A::AbstractMatrix, DV, alg::LAPACK_EigAlgorithm)
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/eigh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ function initialize_output(::typeof(eigh_vals!), A::Diagonal, ::DiagonalAlgorith
return eltype(A) <: Real ? diagview(A) : similar(A, real(eltype(A)), size(A, 1))
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:eigh_full!, :eigh_vals!, :eigh_trunc!, :eigh_trunc_no_error!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation
# --------------
function eigh_full!(A::AbstractMatrix, DV, alg::LAPACK_EighAlgorithm)
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/gen_eig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ function initialize_output(::typeof(gen_eig_vals!), A::AbstractMatrix, B::Abstra
return D
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:gen_eig_full!, :gen_eig_vals!)
@eval function $f!(A, B, alg::DefaultAlgorithm)
return $f!(A, B, select_algorithm($f!, (A, B), nothing; alg.kwargs...))
end
@eval function $f!(A, B, out, alg::DefaultAlgorithm)
return $f!(A, B, out, select_algorithm($f!, (A, B), nothing; alg.kwargs...))
end
end

# Implementation
# --------------
# actual implementation
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ for f! in (:lq_full!, :lq_compact!)
end
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:lq_full!, :lq_compact!, :lq_null!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# ==========================
# IMPLEMENTATIONS
# ==========================
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/orthnull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ initialize_output(::typeof(right_null!), A, alg::RightNullViaLQ) =
initialize_output(lq_null!, A, alg.alg)
initialize_output(::typeof(right_null!), A, alg::RightNullViaSVD) = nothing

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:left_orth!, :right_orth!, :left_null!, :right_null!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation of orth functions
# --------------------------------
left_orth!(A, VC, alg::AbstractAlgorithm) = left_orth!(A, VC, left_orth_alg(alg))
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/polar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ function initialize_output(::typeof(right_polar!), A::AbstractMatrix, ::Abstract
return (P, Wᴴ)
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:left_polar!, :right_polar!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation via SVD
# -----------------------
function left_polar!(A::AbstractMatrix, WP, alg::PolarViaSVD)
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/projections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ function initialize_output(::typeof(project_isometric!), A::AbstractMatrix, ::Ab
return similar(A)
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:project_hermitian!, :project_antihermitian!, :project_isometric!)
@eval function $f!(A::AbstractMatrix, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A::AbstractMatrix, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation
# --------------
function project_hermitian!(A::AbstractMatrix, Aₕ, alg::NativeBlocked)
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ for f! in (:qr_full!, :qr_compact!)
end
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:qr_full!, :qr_compact!, :qr_null!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# ==========================
# IMPLEMENTATIONS
# ==========================
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ function initialize_output(::typeof(schur_vals!), A::AbstractMatrix, ::AbstractA
return vals
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:schur_full!, :schur_vals!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# Implementation
# --------------
function schur_full!(A::AbstractMatrix, TZv, alg::LAPACK_EigAlgorithm)
Expand Down
11 changes: 11 additions & 0 deletions src/implementations/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ function initialize_output(::typeof(svd_vals!), A::Diagonal, ::DiagonalAlgorithm
return eltype(A) <: Real ? diagview(A) : similar(A, real(eltype(A)), size(A, 1))
end

# DefaultAlgorithm intercepts
# ---------------------------
for f! in (:svd_full!, :svd_compact!, :svd_vals!, :svd_trunc!, :svd_trunc_no_error!)
@eval function $f!(A, alg::DefaultAlgorithm)
return $f!(A, select_algorithm($f!, A, nothing; alg.kwargs...))
end
@eval function $f!(A, out, alg::DefaultAlgorithm)
return $f!(A, out, select_algorithm($f!, A, nothing; alg.kwargs...))
end
end

# ==========================
# IMPLEMENTATIONS
# ==========================
Expand Down
21 changes: 21 additions & 0 deletions src/interface/decompositions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,27 @@ until convergence up to tolerance `tol`.
# =========================
# Varia
# =========================
"""
DefaultAlgorithm(; kwargs...)

Algorithm sentinel that resolves to the algorithm selection procedure for a given function and input type at call time.
This provides a unified approach for package developers to store both keyword argument and direct algorithm inputs.
Any keyword arguments stored in the instance are forwarded at runtime to [`select_algorithm`](@ref).

For example, the following calls are equivalent:

```julia
A = rand(3, 3)

# specifying keyword arguments
Q, R = qr_compact(A; positive = true)

# wrapping keyword arguments in DefaultAlgorithm
alg = DefaultAlgorithm(; positive = true)
Q, R = qr_compact(A; alg)
"""
@algdef DefaultAlgorithm

"""
DiagonalAlgorithm(; kwargs...)

Expand Down
Loading