Skip to content

Commit 814d8ef

Browse files
committed
Describe LinearOperator{T, S}(args...) in docs
Add documentation for JuliaSmoothOptimizers#383 Also cleans up an unnecessarily large number of `LinearOperator(M)` methods, preferring to consolidate them by supplying `M`-specific defaults for the `symmetric` and `hermitian` keyword arguments.
1 parent 40edb65 commit 814d8ef

2 files changed

Lines changed: 39 additions & 41 deletions

File tree

src/abstract.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,21 @@ LinearOperator{T}(
124124
nctprod,
125125
)
126126

127+
"""
128+
LinearOperator{T, S}(nrow, ncol, symmetric, hermitian, prod!, tprod!, ctprod! = nothing) where {T, S}
129+
130+
Construct a linear operator with a specific temporary array storage type `S`, which should typically have element type `T`.
131+
132+
This is an inferrable variant of constructors that supply `S` as a keyword argument, and is recommended for performance-sensitive applications.
133+
"""
127134
LinearOperator{T, S}(
128135
nrow::I,
129136
ncol::I,
130137
symmetric::Bool,
131138
hermitian::Bool,
132139
prod!,
133140
tprod!,
134-
ctprod!,
141+
ctprod! = nothing,
135142
) where {T, S, I <: Integer} =
136143
LinearOperator{T, S}(nrow, ncol, symmetric, hermitian, prod!, tprod!, ctprod!, 0, 0, 0)
137144

src/constructors.jl

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,41 @@
11
# Constructors.
22
"""
3-
LinearOperator(M::AbstractMatrix{T}; symmetric=false, hermitian=false, S = Vector{T}) where {T}
4-
Construct a linear operator from a dense or sparse matrix.
5-
Use the optional keyword arguments to indicate whether the operator
6-
is symmetric and/or hermitian.
7-
Change `S` to use LinearOperators on GPU.
3+
LinearOperator(M::AbstractMatrix{T}; symmetric=defaultsymmetric(M), hermitian=defaulthermitian(M), S = Vector{T}) where {T}
4+
Construct a linear operator from a dense or sparse matrix. Use the optional
5+
keyword arguments to indicate whether the operator is symmetric and/or
6+
hermitian. Change `S` to use LinearOperators on GPU.
7+
8+
!!! tip
9+
In performance-sensitive applications, it may be advisable to use
10+
11+
LinearOperator{T, S}(M; kwargs...)
12+
13+
instead.
814
"""
915
function LinearOperator(
1016
M::AbstractMatrix{T};
11-
symmetric = false,
12-
hermitian = false,
1317
S = storage_type(M),
18+
kwargs...,
1419
) where {T}
20+
return LinearOperator{T, S}(M; kwargs...)
21+
end
22+
23+
function LinearOperator{T, S}(M::AbstractMatrix{T}; symmetric = defaultsymmetric(M), hermitian = defaulthermitian(M)) where {T, S}
1524
nrow, ncol = size(M)
1625
prod! = @closure (res, v, α, β) -> mul!(res, M, v, α, β)
1726
tprod! = @closure (res, u, α, β) -> mul!(res, transpose(M), u, α, β)
1827
ctprod! = @closure (res, w, α, β) -> mul!(res, adjoint(M), w, α, β)
1928
LinearOperator{T, S}(nrow, ncol, symmetric, hermitian, prod!, tprod!, ctprod!)
2029
end
2130

22-
"""
23-
LinearOperator(M::Symmetric{T}, S = Vector{T}) where {T <: Real} =
24-
25-
Construct a linear operator from a symmetric real square matrix `M`.
26-
Change `S` to use LinearOperators on GPU.
27-
"""
28-
LinearOperator(M::Symmetric{T}, S = Vector{T}) where {T <: Real} =
29-
LinearOperator(M, symmetric = true, hermitian = true, S = S)
30-
31-
"""
32-
LinearOperator(M::SymTridiagonal{T}, S = Vector{T}) where {T}
33-
34-
Constructs a linear operator from a symmetric tridiagonal matrix. If
35-
its elements are real, it is also Hermitian, otherwise complex
36-
symmetric.
37-
Change `S` to use LinearOperators on GPU.
38-
"""
39-
function LinearOperator(M::SymTridiagonal{T}, S = Vector{T}) where {T}
40-
hermitian = eltype(M) <: Real
41-
LinearOperator(M, symmetric = true, hermitian = hermitian, S = S)
42-
end
43-
44-
"""
45-
LinearOperator(M::Hermitian{T}, S = Vector{T}) where {T}
46-
47-
Constructs a linear operator from a Hermitian matrix. If
48-
its elements are real, it is also symmetric.
49-
Change `S` to use LinearOperators on GPU.
50-
"""
51-
function LinearOperator(M::Hermitian{T}, S = Vector{T}) where {T}
52-
symmetric = eltype(M) <: Real
53-
LinearOperator(M, symmetric = symmetric, hermitian = true, S = S)
54-
end
31+
defaultsymmetric(M) = false
32+
defaulthermitian(M) = false
33+
defaultsymmetric(M::Symmetric) = true
34+
defaulthermitian(M::Symmetric{<:Real}) = true
35+
defaultsymmetric(M::Hermitian) = eltype(M) <: Real
36+
defaulthermitian(M::Hermitian) = true
37+
defaultsymmetric(M::SymTridiagonal) = true
38+
defaulthermitian(M::SymTridiagonal{<:Real}) = true
5539

5640
"""
5741
LinearOperator(type::Type{T}, nrow, ncol, symmetric, hermitian, prod!,
@@ -104,6 +88,13 @@ op = LinearOperator(Float64, 2, 2, false, false,
10488
```
10589
10690
The 3-args `mul!` also works when applying the operator on a matrix.
91+
92+
!!! tip
93+
In performance-sensitive applications, it may be advisable to use
94+
95+
LinearOperator{T, S}(nrow, ncol, symmetric, hermitian, prod!, tprod!=nothing, ctprod!=nothing)
96+
97+
instead.
10798
"""
10899
function LinearOperator(
109100
::Type{T},

0 commit comments

Comments
 (0)