Skip to content

Commit 6d6e3f1

Browse files
committed
more or less give up and just force things to be type stable
1 parent d8af318 commit 6d6e3f1

5 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/algorithms.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ Finally, the same behavior is obtained when the keyword arguments are
100100
passed as the third positional argument in the form of a `NamedTuple`.
101101
""" select_algorithm
102102

103-
@inline function select_algorithm(f::F, A, alg::Alg = nothing; kwargs...) where {F, Alg}
103+
# WARNING: In order to keep everything type stable, this function is marked as foldable.
104+
# This mostly means that the `default_algorithm` implementation must be foldable as well
105+
Base.@assume_effects :foldable function select_algorithm(f::F, A, alg::Alg = nothing; kwargs...) where {F, Alg}
104106
if isnothing(alg)
105107
return default_algorithm(f, A; kwargs...)
106108
elseif alg isa Symbol
@@ -129,8 +131,10 @@ In general, this is called by [`select_algorithm`](@ref) if no algorithm is spec
129131
explicitly.
130132
New types should prefer to register their default algorithms in the type domain.
131133
""" default_algorithm
132-
default_algorithm(f::F, A; kwargs...) where {F} = default_algorithm(f, typeof(A); kwargs...)
133-
default_algorithm(f::F, A, B; kwargs...) where {F} = default_algorithm(f, typeof(A), typeof(B); kwargs...)
134+
@inline default_algorithm(f::F, A; kwargs...) where {F} =
135+
default_algorithm(f, typeof(A); kwargs...)
136+
@inline default_algorithm(f::F, A, B; kwargs...) where {F} =
137+
default_algorithm(f, typeof(A), typeof(B); kwargs...)
134138
# avoid infinite recursion:
135139
function default_algorithm(f::F, ::Type{T}; kwargs...) where {F, T}
136140
throw(MethodError(default_algorithm, (f, T)))

src/interface/decompositions.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ The optional `driver` symbol can be used to choose between different implementat
7979
Depending on the driver, various other keywords may be (un)available to customize the implementation.
8080
"""
8181
@algdef Householder
82-
8382
function Householder(;
84-
driver::Driver = DefaultDriver(), blocksize::Int = 0,
83+
blocksize::Int = 0, driver::Driver = DefaultDriver(),
8584
pivoted::Bool = false, positive::Bool = true
8685
)
87-
return Householder((; driver, blocksize, pivoted, positive))
86+
return Householder((; blocksize, driver, pivoted, positive))
8887
end
8988

9089
default_householder_driver(A) = default_householder_driver(typeof(A))

src/interface/lq.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ See also [`qr_full(!)`](@ref lq_full) and [`qr_compact(!)`](@ref lq_compact).
7171
default_lq_algorithm(A; kwargs...) = default_lq_algorithm(typeof(A); kwargs...)
7272

7373
default_lq_algorithm(T::Type; kwargs...) = throw(MethodError(default_lq_algorithm, (T,)))
74-
default_lq_algorithm(::Type{T}; driver = default_householder_driver(T), kwargs...) where {T <: AbstractMatrix} =
75-
Householder(; driver, kwargs...)
74+
default_lq_algorithm(::Type{T}; kwargs...) where {T <: AbstractMatrix} =
75+
Householder(; kwargs...)
7676
default_lq_algorithm(::Type{T}; kwargs...) where {T <: Diagonal} =
7777
DiagonalAlgorithm(; kwargs...)
7878
default_lq_algorithm(::Type{<:Base.ReshapedArray{T, N, A}}) where {T, N, A} =

src/interface/orthnull.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,79 +334,79 @@ See also [`left_null(!)`](@ref left_null), [`left_orth(!)`](@ref left_orth) and
334334
@inline select_algorithm(::typeof(right_null!), A, alg::Symbol; kwargs...) =
335335
select_algorithm(right_null!, A, Val(alg); kwargs...)
336336

337-
function select_algorithm(::typeof(left_orth!), A, ::Val{:qr}; trunc = nothing, kwargs...)
337+
@inline function select_algorithm(::typeof(left_orth!), A, ::Val{:qr}; trunc = nothing, kwargs...)
338338
isnothing(trunc) ||
339339
throw(ArgumentError("QR-based `left_orth` is incompatible with specifying `trunc`"))
340340
alg′ = select_algorithm(qr_compact!, A; kwargs...)
341341
return LeftOrthViaQR(alg′)
342342
end
343-
function select_algorithm(::typeof(left_orth!), A, ::Val{:polar}; trunc = nothing, kwargs...)
343+
@inline function select_algorithm(::typeof(left_orth!), A, ::Val{:polar}; trunc = nothing, kwargs...)
344344
isnothing(trunc) ||
345345
throw(ArgumentError("Polar-based `left_orth` is incompatible with specifying `trunc`"))
346346
alg′ = select_algorithm(left_polar!, A; kwargs...)
347347
return LeftOrthViaPolar(alg′)
348348
end
349-
function select_algorithm(::typeof(left_orth!), A, ::Val{:svd}; trunc = nothing, kwargs...)
349+
@inline function select_algorithm(::typeof(left_orth!), A, ::Val{:svd}; trunc = nothing, kwargs...)
350350
alg′ = isnothing(trunc) ? select_algorithm(svd_compact!, A; kwargs...) :
351351
select_algorithm(svd_trunc!, A; trunc, kwargs...)
352352
return LeftOrthViaSVD(alg′)
353353
end
354354

355-
function select_algorithm(::typeof(right_orth!), A, ::Val{:lq}; trunc = nothing, kwargs...)
355+
@inline function select_algorithm(::typeof(right_orth!), A, ::Val{:lq}; trunc = nothing, kwargs...)
356356
isnothing(trunc) ||
357357
throw(ArgumentError("LQ-based `right_orth` is incompatible with specifying `trunc`"))
358358
alg = select_algorithm(lq_compact!, A; kwargs...)
359359
return RightOrthViaLQ(alg)
360360
end
361-
function select_algorithm(::typeof(right_orth!), A, ::Val{:polar}; trunc = nothing, kwargs...)
361+
@inline function select_algorithm(::typeof(right_orth!), A, ::Val{:polar}; trunc = nothing, kwargs...)
362362
isnothing(trunc) ||
363363
throw(ArgumentError("Polar-based `right_orth` is incompatible with specifying `trunc`"))
364364
alg = select_algorithm(right_polar!, A; kwargs...)
365365
return RightOrthViaPolar(alg)
366366
end
367-
function select_algorithm(::typeof(right_orth!), A, ::Val{:svd}; trunc = nothing, kwargs...)
367+
@inline function select_algorithm(::typeof(right_orth!), A, ::Val{:svd}; trunc = nothing, kwargs...)
368368
alg′ = isnothing(trunc) ? select_algorithm(svd_compact!, A; kwargs...) :
369369
select_algorithm(svd_trunc!, A; trunc, kwargs...)
370370
return RightOrthViaSVD(alg′)
371371
end
372372

373-
function select_algorithm(::typeof(left_null!), A, ::Val{:qr}; trunc = nothing, kwargs...)
373+
@inline function select_algorithm(::typeof(left_null!), A, ::Val{:qr}; trunc = nothing, kwargs...)
374374
isnothing(trunc) ||
375375
throw(ArgumentError("QR-based `left_null` is incompatible with specifying `trunc`"))
376376
alg = select_algorithm(qr_null!, A; kwargs...)
377377
return LeftNullViaQR(alg)
378378
end
379-
function select_algorithm(::typeof(left_null!), A, ::Val{:svd}; trunc = nothing, kwargs...)
379+
@inline function select_algorithm(::typeof(left_null!), A, ::Val{:svd}; trunc = nothing, kwargs...)
380380
alg_svd = select_algorithm(svd_full!, A, get(kwargs, :svd, nothing))
381381
alg = TruncatedAlgorithm(alg_svd, select_null_truncation(trunc))
382382
return LeftNullViaSVD(alg)
383383
end
384384

385-
function select_algorithm(::typeof(right_null!), A, ::Val{:lq}; trunc = nothing, kwargs...)
385+
@inline function select_algorithm(::typeof(right_null!), A, ::Val{:lq}; trunc = nothing, kwargs...)
386386
isnothing(trunc) ||
387387
throw(ArgumentError("LQ-based `right_null` is incompatible with specifying `trunc`"))
388388
alg = select_algorithm(lq_null!, A; kwargs...)
389389
return RightNullViaLQ(alg)
390390
end
391-
function select_algorithm(::typeof(right_null!), A, ::Val{:svd}; trunc = nothing, kwargs...)
391+
@inline function select_algorithm(::typeof(right_null!), A, ::Val{:svd}; trunc = nothing, kwargs...)
392392
alg_svd = select_algorithm(svd_full!, A; kwargs...)
393393
alg = TruncatedAlgorithm(alg_svd, select_null_truncation(trunc))
394394
return RightNullViaSVD(alg)
395395
end
396396

397-
default_algorithm(::typeof(left_orth!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
397+
@inline default_algorithm(::typeof(left_orth!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
398398
isnothing(trunc) ? select_algorithm(left_orth!, A, Val(:qr); kwargs...) :
399399
select_algorithm(left_orth!, A, Val(:svd); trunc, kwargs...)
400400

401-
default_algorithm(::typeof(right_orth!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
401+
@inline default_algorithm(::typeof(right_orth!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
402402
isnothing(trunc) ? select_algorithm(right_orth!, A, Val(:lq); kwargs...) :
403403
select_algorithm(right_orth!, A, Val(:svd); trunc, kwargs...)
404404

405-
default_algorithm(::typeof(left_null!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
405+
@inline default_algorithm(::typeof(left_null!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
406406
isnothing(trunc) ? select_algorithm(left_null!, A, Val(:qr); kwargs...) :
407407
select_algorithm(left_null!, A, Val(:svd); trunc, kwargs...)
408408

409-
default_algorithm(::typeof(right_null!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
409+
@inline default_algorithm(::typeof(right_null!), ::Type{A}; trunc = nothing, kwargs...) where {A} =
410410
isnothing(trunc) ? select_algorithm(right_null!, A, Val(:lq); kwargs...) :
411411
select_algorithm(right_null!, A, Val(:svd); trunc, kwargs...)
412412

src/interface/qr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ See also [`lq_full(!)`](@ref lq_full) and [`lq_compact(!)`](@ref lq_compact).
7171
default_qr_algorithm(A; kwargs...) = default_qr_algorithm(typeof(A); kwargs...)
7272

7373
default_qr_algorithm(T::Type; kwargs...) = throw(MethodError(default_qr_algorithm, (T,)))
74-
default_qr_algorithm(::Type{T}; driver = default_householder_driver(T), kwargs...) where {T <: AbstractMatrix} =
75-
Householder(; driver, kwargs...)
74+
default_qr_algorithm(::Type{T}; kwargs...) where {T <: AbstractMatrix} =
75+
Householder(; kwargs...)
7676
default_qr_algorithm(::Type{T}; kwargs...) where {T <: Diagonal} =
7777
DiagonalAlgorithm(; kwargs...)
7878
default_qr_algorithm(::Type{<:Base.ReshapedArray{T, N, A}}) where {T, N, A} =

0 commit comments

Comments
 (0)