Skip to content

Commit 774f0d9

Browse files
authored
don't use mul! inplace (#482)
* don't use mul! inplace * accept temp args in inplace mul_coefficients
1 parent 9f5a288 commit 774f0d9

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

src/Operators/SubOperator.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,16 @@ function mul_coefficients(A::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},
379379
if size(A,2) == length(b)
380380
AbstractMatrix(A)*b
381381
else
382-
view(A,:,1:length(b))*b
382+
AbstractMatrix(view(A,:,1:length(b)))*b
383383
end
384384
end
385-
function mul_coefficients!(A::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}}, b)
385+
function mul_coefficients!(A::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}}, b,
386+
temp = similar(b, promote_type(eltype(A), eltype(b)), size(A,1)))
386387
if size(A,2) == length(b)
387-
mul!(b, AbstractMatrix(A), b)
388+
mul!(temp, AbstractMatrix(A), b)
388389
else
389-
mul!(b, view(A,:,1:length(b)), b)
390+
mul!(temp, AbstractMatrix(view(A,:,1:length(b))), b)
390391
end
392+
b .= temp
391393
return b
392394
end

src/Operators/banded/ConstantOperator.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function mul_coefficients(A::ConstantOperator, b)
7878
A.λ * b
7979
end
8080
end
81-
function mul_coefficients!(A::ConstantOperator, b)
81+
function mul_coefficients!(A::ConstantOperator, b, args...)
8282
λ = A.λ
8383
if !isone(λ)
8484
b .*= λ

src/Operators/general/algebra.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,27 +694,27 @@ end
694694
## Operations
695695
for mulcoeff in [:mul_coefficients, :mul_coefficients!]
696696
@eval begin
697-
function $mulcoeff(A::Operator, b)
698-
mulcoeff_maybeconvert($mulcoeff, A, b)
697+
function $mulcoeff(A::Operator, b, args...)
698+
mulcoeff_maybeconvert($mulcoeff, A, b, args...)
699699
end
700700

701-
function $mulcoeff(A::TimesOperator, b)
701+
function $mulcoeff(A::TimesOperator, b, args...)
702702
ret = b
703703
for k = length(A.ops):-1:1
704-
ret = $mulcoeff(A.ops[k], ret)
704+
ret = $mulcoeff(A.ops[k], ret, args...)
705705
end
706706

707707
ret
708708
end
709709
end
710710
end
711711

712-
function _mulcoeff_maybeconvert(f::F, A, b) where {F}
712+
function _mulcoeff_maybeconvert(f::F, A, b, args...) where {F}
713713
n = size(b, 1)
714-
n > 0 ? f(view(A, FiniteRange, 1:n), b) : b
714+
n > 0 ? f(view(A, FiniteRange, 1:n), b, args...) : b
715715
end
716716

717-
mulcoeff_maybeconvert(f, A, b) = _mulcoeff_maybeconvert(f, A, b)
717+
mulcoeff_maybeconvert(args...) = _mulcoeff_maybeconvert(args...)
718718

719719
function mulcoeff_maybeconvert(f::typeof(mul_coefficients), A, b::Vector)
720720
v = _mulcoeff_maybeconvert(f, A, b)

src/Operators/ldiv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
\(A::Operator,B::MatrixFun;kwds...) = \(A,Array(B);kwds...)
4848

4949
ldiv_coefficients(A::Operator,b;kwds...) = ldiv_coefficients(qr(A),b;kwds...)
50-
ldiv_coefficients!(A::Operator,b;kwds...) = ldiv_coefficients!(qr(A),b;kwds...)
50+
ldiv_coefficients!(A::Operator,b,args...;kwds...) = ldiv_coefficients!(qr(A),b,args...;kwds...)
5151

5252
\(A::Operator,B::Operator) = TimesOperator(inv(A),B)
5353

src/Operators/qr.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ det(A::Operator) = det(qr(A))
143143

144144
mul_coefficients(At::Transpose{T,<:QROperatorQ{T}},B::AbstractVector{T}) where {T<:Real} = parent(At)'*B
145145
mul_coefficients(At::Transpose{T,<:QROperatorQ{T}},B::AbstractMatrix{T}) where {T<:Real} = parent(At)'*B
146-
mul_coefficients!(At::Transpose{T,<:QROperatorQ{T}},B::AbstractVector{T}) where {T<:Real} = mul!(B, parent(At)', B)
147-
mul_coefficients!(At::Transpose{T,<:QROperatorQ{T}},B::AbstractMatrix{T}) where {T<:Real} = mul!(B, parent(At)', B)
146+
function mul_coefficients!(At::Transpose{T,<:QROperatorQ{T}},B::AbstractVector{T},args...) where {T<:Real}
147+
mul_coefficients!(parent(At)', B, args...)
148+
end
149+
function mul_coefficients!(At::Transpose{T,<:QROperatorQ{T}},B::AbstractMatrix{T},args...) where {T<:Real}
150+
mul_coefficients!(parent(At)', B, args...)
151+
end
148152

149153
function mul_coefficients(Ac::Adjoint{T,<:QROperatorQ{<:Any,T}},B::AbstractVector{T};
150154
tolerance=eps(eltype(Ac))/10,maxlength=1000000) where {T}
151155
mulpars(Ac,B,tolerance,maxlength)
152156
end
153-
function mul_coefficients!(Ac::Adjoint{T,<:QROperatorQ{<:Any,T}},B::AbstractVector{T};
157+
function mul_coefficients!(Ac::Adjoint{T,<:QROperatorQ{<:Any,T}},B::AbstractVector{T}, args...;
154158
tolerance=eps(eltype(Ac))/10,maxlength=1000000) where {T}
155159
mulpars(Ac,B,tolerance,maxlength,Val(true))
156160
end
@@ -171,7 +175,7 @@ end
171175

172176

173177
ldiv_coefficients(A::QROperatorQ, B; opts...) = mul_coefficients(A', B; opts...)
174-
ldiv_coefficients!(A::QROperatorQ, B; opts...) = mul_coefficients!(A', B; opts...)
178+
ldiv_coefficients!(A::QROperatorQ, B, args...; opts...) = mul_coefficients!(A', B, args...; opts...)
175179
\(A::QROperatorQ, B::Fun; opts...) = *(A', B; opts...)
176180

177181

@@ -187,7 +191,7 @@ function ldiv_coefficients(R::QROperatorR, b::AbstractVector)
187191
U = uppertriangularview!(R, length(b))
188192
U \ b
189193
end
190-
function ldiv_coefficients!(R::QROperatorR, b::AbstractVector)
194+
function ldiv_coefficients!(R::QROperatorR, b::AbstractVector, args...)
191195
U = uppertriangularview!(R, length(b))
192196
ldiv!(U, b)
193197
end
@@ -226,8 +230,8 @@ ldiv_coefficients(QR::QROperator{<:Any,<:Any,<:Complex}, b::AbstractVector{<:Rea
226230
ldiv_coefficients(QR, Vector{eltype(QR)}(b);kwds...)
227231

228232
# fallback methods that may not be very efficient
229-
function ldiv_coefficients!(QR::QROperator{<:Any,<:Any,<:Real}, b::AbstractVector{<:Number}; kwds...)
230-
ldiv_coefficients!(convert(Operator{eltype(b)}, QR), b)
233+
function ldiv_coefficients!(QR::QROperator{<:Any,<:Any,<:Real}, b::AbstractVector{<:Number}, args...; kwds...)
234+
ldiv_coefficients!(convert(Operator{eltype(b)}, QR), b, args...; kwds...)
231235
end
232236

233237
\(A::QROperator,b::Fun;kwds...) =

0 commit comments

Comments
 (0)