Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid allocation for length-1 linear combinations #34

Merged
merged 4 commits into from
Oct 30, 2018
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
35 changes: 22 additions & 13 deletions src/linearcombination.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct LinearCombination{T, As<:Tuple{Vararg{LinearMap}}, Ts<:Tuple} <: LinearMa
coeffs::Ts
function LinearCombination{T, As, Ts}(maps::As, coeffs::Ts) where {T, As, Ts}
N = length(maps)
N == length(coeffs) || error("Number of coefficients doesn't match number of terms")
N == length(coeffs) || error("number of coefficients doesn't match number of linear maps")
sz = size(maps[1])
for n = 1:N
size(maps[n]) == sz || throw(DimensionMismatch("LinearCombination"))
Expand Down Expand Up @@ -97,32 +97,41 @@ function A_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
# no size checking, will be done by individual maps
A_mul_B!(y, A.maps[1], x)
A.coeffs[1] == 1 || lmul!(A.coeffs[1], y)
z = similar(y)
for n=2:length(A.maps)
A_mul_B!(z, A.maps[n], x)
axpy!(A.coeffs[n], z, y)
l = length(A.maps)
if l>1
z = similar(y)
for n=2:l
A_mul_B!(z, A.maps[n], x)
axpy!(A.coeffs[n], z, y)
end
end
return y
end
function At_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
# no size checking, will be done by individual maps
At_mul_B!(y, A.maps[1], x)
A.coeffs[1] == 1 || lmul!(A.coeffs[1], y)
z = similar(y)
for n = 2:length(A.maps)
At_mul_B!(z, A.maps[n], x)
axpy!(A.coeffs[n], z, y)
l = length(A.maps)
if l>1
z = similar(y)
for n = 2:l
At_mul_B!(z, A.maps[n], x)
axpy!(A.coeffs[n], z, y)
end
end
return y
end
function Ac_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
# no size checking, will be done by individual maps
Ac_mul_B!(y, A.maps[1], x)
A.coeffs[1] == 1 || lmul!(conj(A.coeffs[1]), y)
z = similar(y)
for n=2:length(A.maps)
Ac_mul_B!(z, A.maps[n], x)
axpy!(conj(A.coeffs[n]), z, y)
l = length(A.maps)
if l>1
z = similar(y)
for n=2:l
Ac_mul_B!(z, A.maps[n], x)
axpy!(conj(A.coeffs[n]), z, y)
end
end
return y
end
Loading