-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update variants to not convert unless necessary
Closes #126
- Loading branch information
1 parent
4806a00
commit 30b4a22
Showing
3 changed files
with
61 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,80 @@ | ||
using LinearAlgebra | ||
|
||
# Define a generic linear operator with preallocation | ||
function preallocated_LinearOperator(A) | ||
function preallocated_LinearOperator(A :: AbstractMatrix, T) | ||
(n, m) = size(A) | ||
Ap = zeros(n) | ||
Atq = zeros(m) | ||
return LinearOperator(n, m, false, false, p -> mul!(Ap, A, p), | ||
Ap = zeros(T, n) | ||
Atq = zeros(T, m) | ||
return LinearOperator(T, n, m, false, false, p -> mul!(Ap, A, p), | ||
q -> mul!(Atq, transpose(A), q), q -> mul!(Atq, transpose(A), q)) | ||
end | ||
|
||
# Variants where A is a matrix without specific properties | ||
for fn in (:cgls, :cgne, :craig, :craigmr, :crls, :crmr, :lslq, :lsmr, :lsqr, :dqgmres, :diom, :cgs, :bilq) | ||
@eval begin | ||
$fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, args...; kwargs...) where {TA, Tb <: Number} = | ||
$fn(preallocated_LinearOperator(A), convert(Vector{Float64}, b), args...; kwargs...) | ||
function $fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, args...; kwargs...) where {TA, Tb <: Number} | ||
S = promote_type(TA, Tb) | ||
if S <: Integer || S <: Complex{<: Integer} | ||
S = promote_type(S, Float64) | ||
end | ||
$fn(preallocated_LinearOperator(A, S), convert(Vector{S}, b), args...; kwargs...) | ||
end | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: SparseVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), convert(Vector{T}, b), args...; kwargs...) | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: AbstractVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), b, args...; kwargs...) | ||
end | ||
end | ||
|
||
# Variants for USYMQR | ||
for fn in (:usymqr,) | ||
@eval begin | ||
$fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, c :: AbstractVector{Tc}, args...; kwargs...) where {TA, Tb, Tc <: Number} = | ||
$fn(preallocated_LinearOperator(A), convert(Vector{Float64}, b), convert(Vector{Float64}, c), args...; kwargs...) | ||
function $fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, c :: AbstractVector{Tc}, args...; kwargs...) where {TA, Tb, Tc <: Number} | ||
S = promote_type(TA, Tb, Tc) | ||
if S <: Integer || S <: Complex{<: Integer} | ||
S = promote_type(S, Float64) | ||
end | ||
$fn(preallocated_LinearOperator(A, S), convert(Vector{S}, b), convert(Vector{S}, c), args...; kwargs...) | ||
end | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: SparseVector{T}, c :: SparseVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), convert(Vector{T}, b), convert(Vector{T}, c), args...; kwargs...) | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: AbstractVector{T}, c :: SparseVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), b, convert(Vector{T}, c), args...; kwargs...) | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: SparseVector{T}, c :: AbstractVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), convert(Vector{T}, b), c, args...; kwargs...) | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: AbstractVector{T}, c :: AbstractVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_LinearOperator(A, T), b, c, args...; kwargs...) | ||
end | ||
end | ||
|
||
# Define a symmetric linear operator with preallocation | ||
function preallocated_symmetric_LinearOperator(A) | ||
function preallocated_symmetric_LinearOperator(A :: AbstractMatrix, T) | ||
(n, m) = size(A) | ||
Ap = zeros(n) | ||
return LinearOperator(n, m, true, true, p -> mul!(Ap, A, p)) | ||
Ap = zeros(T, n) | ||
return LinearOperator(T, n, m, true, true, p -> mul!(Ap, A, p)) | ||
end | ||
|
||
# Variants where A must be symmetric | ||
for fn in (:cg_lanczos, :cg_lanczos_shift_seq, :cg, :cr, :minres, :minres_qlp, :symmlq) | ||
@eval begin | ||
$fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, args...; kwargs...) where {TA, Tb <: Number} = | ||
$fn(preallocated_symmetric_LinearOperator(A), convert(Vector{Float64}, b), args...; kwargs...) | ||
function $fn(A :: AbstractMatrix{TA}, b :: AbstractVector{Tb}, args...; kwargs...) where {TA, Tb <: Number} | ||
S = promote_type(TA, Tb) | ||
if S <: Integer || S <: Complex{<: Integer} | ||
S = promote_type(S, Float64) | ||
end | ||
$fn(preallocated_symmetric_LinearOperator(A, S), convert(Vector{S}, b), args...; kwargs...) | ||
end | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: SparseVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_symmetric_LinearOperator(A, T), convert(Vector{T}, b), args...; kwargs...) | ||
|
||
$fn(A :: AbstractMatrix{T}, b :: AbstractVector{T}, args...; kwargs...) where T <: AbstractFloat = | ||
$fn(preallocated_symmetric_LinearOperator(A, T), b, args...; kwargs...) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters