Skip to content

Commit 3b3cd69

Browse files
cakodkarrasch
authored andcommitted
Add fallback when resize! does not work (#31)
* Add fallback for when resize fails * Only catch shared data error from ` resize!` * Test composition of several maps
1 parent 2e4c5d1 commit 3b3cd69

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/composition.jl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ function A_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
9090
dest = Array{T}(undef, size(A.maps[2], 1))
9191
end
9292
for n=2:N-1
93-
resize!(dest, size(A.maps[n], 1))
93+
try
94+
resize!(dest, size(A.maps[n], 1))
95+
catch err
96+
if err == ErrorException("cannot resize array with shared data")
97+
dest = Array{T}(undef, size(A.maps[n], 1))
98+
else
99+
rethrow(err)
100+
end
101+
end
94102
A_mul_B!(dest, A.maps[n], source)
95103
dest, source = source, dest # alternate dest and source
96104
end
@@ -112,7 +120,15 @@ function At_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
112120
dest = Array{T}(undef, size(A.maps[N-1], 2))
113121
end
114122
for n = N-1:-1:2
115-
resize!(dest, size(A.maps[n], 2))
123+
try
124+
resize!(dest, size(A.maps[n], 2))
125+
catch err
126+
if err == ErrorException("cannot resize array with shared data")
127+
dest = Array{T}(undef, size(A.maps[n], 2))
128+
else
129+
rethrow(err)
130+
end
131+
end
116132
At_mul_B!(dest, A.maps[n], source)
117133
dest, source = source, dest # alternate dest and source
118134
end
@@ -134,7 +150,15 @@ function Ac_mul_B!(y::AbstractVector, A::CompositeMap, x::AbstractVector)
134150
dest = Array{T}(undef, size(A.maps[N-1], 2))
135151
end
136152
for n = N-1:-1:2
137-
resize!(dest, size(A.maps[n], 2))
153+
try
154+
resize!(dest, size(A.maps[n], 2))
155+
catch err
156+
if err == ErrorException("cannot resize array with shared data")
157+
dest = Array{T}(undef, size(A.maps[n], 2))
158+
else
159+
rethrow(err)
160+
end
161+
end
138162
Ac_mul_B!(dest, A.maps[n], source)
139163
dest, source = source, dest # alternate dest and source
140164
end

test/runtests.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,42 @@ mul!(w, F, v)
179179
@test_throws MethodError transpose(F) * v
180180
@test_throws MethodError mul!(w, adjoint(F), v)
181181
@test_throws MethodError mul!(w, transpose(F), v)
182+
183+
# test composition of several maps with shared data #31
184+
sizes = ( (5, 2), (3, 3), (3, 2), (2, 2), (9, 2), (7, 1) )
185+
N = length(sizes) - 1
186+
Lf = []
187+
Lt = []
188+
Lc = []
189+
190+
# build list of operators [LN, ..., L2, L1] for each mode
191+
for (fi, i) in [ (Symbol("f$i"), i) for i in 1:N]
192+
@eval begin
193+
function ($fi)(source)
194+
dest = ones(prod(sizes[$i + 1]))
195+
tmp = reshape(source, sizes[$i])
196+
return conj.($i * dest)
197+
end
198+
insert!(Lf, 1, LinearMap($fi, prod(sizes[$i + 1]), prod(sizes[$i])))
199+
insert!(Lt, 1, LinearMap(x -> x, $fi, prod(sizes[$i]), prod(sizes[$i + 1])))
200+
insert!(Lc, 1, LinearMap{ComplexF64}(x -> x, $fi, prod(sizes[$i]), prod(sizes[$i + 1])))
201+
end
202+
end
203+
204+
# multiply as composition and as recursion
205+
v1 = ones(prod(sizes[1]))
206+
u1 = ones(prod(sizes[1]))
207+
w1 = im.*ones(ComplexF64, prod(sizes[1]))
208+
for i = N:-1:1
209+
v2 = prod(Lf[i:N]) * ones(prod(sizes[1]))
210+
u2 = transpose(LinearMap(prod(Lt[N:-1:i]))) * ones(prod(sizes[1]))
211+
w2 = adjoint(LinearMap(prod(Lc[N:-1:i]))) * ones(prod(sizes[1]))
212+
213+
global v1 = Lf[i] * v1
214+
global u1 = transpose(Lt[i]) * u1
215+
global w1 = adjoint(Lc[i]) * w1
216+
217+
@test v1 == v2
218+
@test u1 == u2
219+
@test w1 == w2
220+
end

0 commit comments

Comments
 (0)