Skip to content

Commit f533d6e

Browse files
dkarraschJutho
authored andcommitted
Simplify multiplication code (#51)
* simplify multiplication code, more consistent code style * remove eachcol again
1 parent 67ad5c4 commit f533d6e

File tree

5 files changed

+19
-92
lines changed

5 files changed

+19
-92
lines changed

src/LinearMaps.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ function LinearAlgebra.mul!(Y::AbstractMatrix, A::LinearMap{T}, X::AbstractMatri
5050
@inbounds @views for i = 1:size(X, 2)
5151
mul!(Y[:, i], A, X[:, i], α, β)
5252
end
53+
# starting from Julia v1.1, we could use the `eachcol` iterator
54+
# for (Xi, Yi) in zip(eachcol(X), eachcol(Y))
55+
# mul!(Yi, A, Xi, α, β)
56+
# end
5357
return Y
5458
end
5559

src/composition.jl

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -131,63 +131,7 @@ function A_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
131131
end
132132
return y
133133
end
134-
function At_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
135-
# no size checking, will be done by individual maps
136-
N = length(A.maps)
137-
if N == 1
138-
At_mul_B!(y, A.maps[1], x)
139-
else
140-
T = eltype(y)
141-
dest = Array{T}(undef, size(A.maps[N], 2))
142-
At_mul_B!(dest, A.maps[N], x)
143-
source = dest
144-
if N>2
145-
dest = Array{T}(undef, size(A.maps[N-1], 2))
146-
end
147-
for n = N-1:-1:2
148-
try
149-
resize!(dest, size(A.maps[n], 2))
150-
catch err
151-
if err == ErrorException("cannot resize array with shared data")
152-
dest = Array{T}(undef, size(A.maps[n], 2))
153-
else
154-
rethrow(err)
155-
end
156-
end
157-
At_mul_B!(dest, A.maps[n], source)
158-
dest, source = source, dest # alternate dest and source
159-
end
160-
At_mul_B!(y, A.maps[1], source)
161-
end
162-
return y
163-
end
164-
function Ac_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
165-
# no size checking, will be done by individual maps
166-
N = length(A.maps)
167-
if N == 1
168-
Ac_mul_B!(y, A.maps[1], x)
169-
else
170-
T = eltype(y)
171-
dest = Array{T}(undef, size(A.maps[N], 2))
172-
Ac_mul_B!(dest, A.maps[N], x)
173-
source = dest
174-
if N>2
175-
dest = Array{T}(undef, size(A.maps[N-1], 2))
176-
end
177-
for n = N-1:-1:2
178-
try
179-
resize!(dest, size(A.maps[n], 2))
180-
catch err
181-
if err == ErrorException("cannot resize array with shared data")
182-
dest = Array{T}(undef, size(A.maps[n], 2))
183-
else
184-
rethrow(err)
185-
end
186-
end
187-
Ac_mul_B!(dest, A.maps[n], source)
188-
dest, source = source, dest # alternate dest and source
189-
end
190-
Ac_mul_B!(y, A.maps[1], source)
191-
end
192-
return y
193-
end
134+
135+
At_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector) = A_mul_B!(y, transpose(A), x)
136+
137+
Ac_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector) = A_mul_B!(y, adjoint(A), x)

src/functionmap.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ ismutating(A::FunctionMap) = A._ismutating
3838
_ismutating(f) = first(methods(f)).nargs == 3
3939

4040
# multiplication with vector
41-
function A_mul_B!(y::AbstractVector, A::FunctionMap, x::AbstractVector)
42-
(length(x) == A.N && length(y) == A.M) || throw(DimensionMismatch())
43-
ismutating(A) ? A.f(y, x) : copyto!(y, A.f(x))
44-
return y
45-
end
4641
function Base.:(*)(A::FunctionMap, x::AbstractVector)
4742
length(x) == A.N || throw(DimensionMismatch())
4843
if ismutating(A)
@@ -54,6 +49,12 @@ function Base.:(*)(A::FunctionMap, x::AbstractVector)
5449
return y
5550
end
5651

52+
function A_mul_B!(y::AbstractVector, A::FunctionMap, x::AbstractVector)
53+
(length(x) == A.N && length(y) == A.M) || throw(DimensionMismatch())
54+
ismutating(A) ? A.f(y, x) : copyto!(y, A.f(x))
55+
return y
56+
end
57+
5758
function At_mul_B!(y::AbstractVector, A::FunctionMap, x::AbstractVector)
5859
issymmetric(A) && return A_mul_B!(y, A, x)
5960
(length(x) == A.M && length(y) == A.N) || throw(DimensionMismatch())

src/linearcombination.jl

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,7 @@ function A_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
5959
end
6060
return y
6161
end
62-
function At_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
63-
# no size checking, will be done by individual maps
64-
At_mul_B!(y, A.maps[1], x)
65-
l = length(A.maps)
66-
if l>1
67-
z = similar(y)
68-
for n = 2:l
69-
At_mul_B!(z, A.maps[n], x)
70-
y .+= z
71-
end
72-
end
73-
return y
74-
end
75-
function Ac_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector)
76-
# no size checking, will be done by individual maps
77-
Ac_mul_B!(y, A.maps[1], x)
78-
l = length(A.maps)
79-
if l>1
80-
z = similar(y)
81-
for n=2:l
82-
Ac_mul_B!(z, A.maps[n], x)
83-
y .+= z
84-
end
85-
end
86-
return y
87-
end
62+
63+
At_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector) = A_mul_B!(y, transpose(A), x)
64+
65+
Ac_mul_B!(y::AbstractVector, A::LinearCombination, x::AbstractVector) = A_mul_B!(y, adjoint(A), x)

src/wrappedmap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ Base.:(-)(A1::LinearMap, A2::AbstractMatrix) = -(A1, WrappedMap(A2))
4040
Base.:(-)(A1::AbstractMatrix, A2::LinearMap) = -(WrappedMap(A1), A2)
4141

4242
Base.:(*)(A1::LinearMap, A2::AbstractMatrix) = *(A1, WrappedMap(A2))
43-
Base.:(*)(A1::AbstractMatrix, A2::LinearMap) = *(WrappedMap(A1) ,A2)
43+
Base.:(*)(A1::AbstractMatrix, A2::LinearMap) = *(WrappedMap(A1), A2)

0 commit comments

Comments
 (0)