@@ -54,20 +54,64 @@ function _show_alg(io::IO, alg::Algorithm)
5454end
5555
5656@doc """
57- select_algorithm(f, A; kwargs...)
57+ MatrixAlgebraKit.select_algorithm(f, A, alg::AbstractAlgorithm)
58+ MatrixAlgebraKit.select_algorithm(f, A, alg::Symbol; kwargs...)
59+ MatrixAlgebraKit.select_algorithm(f, A, alg::Type; kwargs...)
60+ MatrixAlgebraKit.select_algorithm(f, A; kwargs...)
61+ MatrixAlgebraKit.select_algorithm(f, A, (; kwargs...))
5862
59- Given some keyword arguments and an input `A`, decide on an algrithm to use for
60- implementing the function `f` on inputs of type `A`.
63+ Decide on an algorithm to use for implementing the function `f` on inputs of type `A`.
64+
65+ If `alg` is an `AbstractAlgorithm` instance, it will be returned as-is.
66+
67+ If `alg` is a `Symbol` or a `Type` of algorithm, the return value is obtained
68+ by calling the corresponding algorithm constructor;
69+ keyword arguments in `kwargs` are passed along to this constructor.
70+
71+ If `alg` is not specified (or `nothing`), an algorithm will be selected
72+ automatically with [`MatrixAlgebraKit.default_algorithm`](@ref) and
73+ the keyword arguments in `kwargs` will be passed to the algorithm constructor.
74+ Finally, the same behavior is obtained when the keyword arguments are
75+ passed as the third positional argument in the form of a `NamedTuple`.
6176"""
6277function select_algorithm end
6378
64- function _select_algorithm (f, A:: AbstractMatrix , alg:: AbstractAlgorithm )
79+ function select_algorithm (f:: F , A, alg:: Alg = nothing ; kwargs... ) where {F,Alg}
80+ return _select_algorithm (f, A, alg; kwargs... )
81+ end
82+
83+ function _select_algorithm (f:: F , A, alg:: Nothing ; kwargs... ) where {F}
84+ return default_algorithm (f, A; kwargs... )
85+ end
86+ function _select_algorithm (f:: F , A, alg:: Symbol ; kwargs... ) where {F}
87+ return Algorithm {alg} (; kwargs... )
88+ end
89+ function _select_algorithm (f:: F , A, :: Type{Alg} ; kwargs... ) where {F,Alg}
90+ return Alg (; kwargs... )
91+ end
92+ function _select_algorithm (f:: F , A, alg:: NamedTuple ; kwargs... ) where {F}
93+ isempty (kwargs) ||
94+ throw (ArgumentError (" Additional keyword arguments are not allowed when algorithm parameters are specified." ))
95+ return default_algorithm (f, A; alg... )
96+ end
97+ function _select_algorithm (f:: F , A, alg:: AbstractAlgorithm ; kwargs... ) where {F}
98+ isempty (kwargs) ||
99+ throw (ArgumentError (" Additional keyword arguments are not allowed when an algorithm is specified." ))
65100 return alg
66101end
67- function _select_algorithm (f, A:: AbstractMatrix , alg:: NamedTuple )
68- return select_algorithm (f, A; alg... )
102+ function _select_algorithm (f:: F , A, alg; kwargs ... ) where {F}
103+ return throw ( ArgumentError ( " Unknown alg $ alg" ) )
69104end
70105
106+ @doc """
107+ MatrixAlgebraKit.default_algorithm(f, A; kwargs...)
108+
109+ Select the default algorithm for a given factorization function `f` and input `A`.
110+ In general, this is called by [`select_algorithm`](@ref) if no algorithm is specified
111+ explicitly.
112+ """
113+ function default_algorithm end
114+
71115@doc """
72116 copy_input(f, A)
73117
@@ -138,9 +182,11 @@ macro functiondef(f)
138182 $ f (A, alg:: AbstractAlgorithm ) = $ f! (copy_input ($ f, A), alg)
139183
140184 # fill in arguments
141- $ f! (A; kwargs... ) = $ f! (A, select_algorithm ($ f!, A; kwargs... ))
142- function $f! (A, out; kwargs... )
143- return $ f! (A, out, select_algorithm ($ f!, A; kwargs... ))
185+ function $f! (A; alg= nothing , kwargs... )
186+ return $ f! (A, select_algorithm ($ f!, A, alg; kwargs... ))
187+ end
188+ function $f! (A, out; alg= nothing , kwargs... )
189+ return $ f! (A, out, select_algorithm ($ f!, A, alg; kwargs... ))
144190 end
145191 function $f! (A, alg:: AbstractAlgorithm )
146192 return $ f! (A, initialize_output ($ f!, A, alg), alg)
0 commit comments