Skip to content

Commit 2cd59e5

Browse files
authored
Skip conj.(v) for real-valued vectors (#404)
1 parent eb7f2c4 commit 2cd59e5

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/adjtrans.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ function mul!(
126126
increase_nprod!(p)
127127
end
128128
conj!(res)
129+
vc = eltype(v) <: Real ? v : conj.(v) # avoid unnecessary allocations if v has real elements
129130
if hasmethod(tprod!, Tuple{typeof(res), typeof(v), typeof(α), typeof(β)})
130-
tprod!(res, conj.(v), conj(α), conj(β))
131+
tprod!(res, vc, conj(α), conj(β))
131132
else
132133
iszero(β) || !isempty(p.Mtu) || allocate_vectors_args3!(p)
133-
prod3!(res, tprod!, conj.(v), conj(α), conj(β), p.Mtu)
134+
prod3!(res, tprod!, vc, conj(α), conj(β), p.Mtu)
134135
end
135136
conj!(res)
136137
end
@@ -193,11 +194,12 @@ function mul!(
193194
increase_nprod!(p)
194195
end
195196
conj!(res)
197+
vc = eltype(v) <: Real ? v : conj.(v) # avoid unnecessary allocations when v has real elements
196198
if hasmethod(ctprod!, Tuple{typeof(res), typeof(v), typeof(α), typeof(β)})
197-
ctprod!(res, conj.(v), conj(α), conj(β))
199+
ctprod!(res, vc, conj(α), conj(β))
198200
else
199201
iszero(β) || !isempty(p.Mtu) || allocate_vectors_args3!(p)
200-
prod3!(res, ctprod!, conj.(v), conj(α), conj(β), p.Mtu)
202+
prod3!(res, ctprod!, vc, conj(α), conj(β), p.Mtu)
201203
end
202204
conj!(res)
203205
end
@@ -229,7 +231,20 @@ function mul!(
229231
β,
230232
) where {T, S}
231233
p = op.parent
232-
mul!(res, p, conj.(v), α, β)
234+
vc = eltype(v) <: Real ? v : conj.(v) # avoid unnecessary allocations if v has real elements
235+
mul!(res, p, vc, α, β)
236+
conj!(res)
237+
end
238+
239+
function mul!(
240+
res::AbstractVector,
241+
op::ConjugateLinearOperator{T, S},
242+
v::AbstractVector{<:Real},
243+
α,
244+
β,
245+
) where {T, S}
246+
p = op.parent
247+
mul!(res, p, v, α, β) # we can skip `conj.(v)` since it has real elements (this avoids unnecessary allocations)
233248
conj!(res)
234249
end
235250

test/test_adjtrans.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ function test_adjtrans()
2727
v = rand(5) + im * rand(5)
2828
@test aopA * v == adjoint(A) * v
2929
@test topA * v == transpose(A) * v
30+
v = rand(5)
31+
@test aopA * v adjoint(A) * v
32+
@test topA * v transpose(A) * v
3033

3134
v = rand(3) + im * rand(3)
32-
@test copA * v == conj(A) * v
35+
@test copA * v conj(A) * v
36+
v = rand(3)
37+
@test copA * v conj(A) * v
3338
end
3439
end
3540

@@ -74,9 +79,15 @@ function test_derived_adjoint()
7479
v = rand(5) + im * rand(5)
7580
@test aopA * v == adjoint(A) * v
7681
@test topA * v == transpose(A) * v
82+
v = rand(5)
83+
@test aopA * v adjoint(A) * v
84+
@test topA * v transpose(A) * v
85+
7786

7887
v = rand(3) + im * rand(3)
79-
@test copA * v == conj(A) * v
88+
@test copA * v conj(A) * v
89+
v = rand(3)
90+
@test copA * v conj(A) * v
8091
end
8192
end
8293

@@ -121,9 +132,15 @@ function test_derived_transpose()
121132
v = rand(5) + im * rand(5)
122133
@test aopA * v == adjoint(A) * v
123134
@test topA * v == transpose(A) * v
135+
v = rand(5)
136+
@test aopA * v adjoint(A) * v
137+
@test topA * v transpose(A) * v
138+
124139

125140
v = rand(3) + im * rand(3)
126-
@test copA * v == conj(A) * v
141+
@test copA * v conj(A) * v
142+
v = rand(3)
143+
@test copA * v conj(A) * v
127144
end
128145
end
129146

0 commit comments

Comments
 (0)