Skip to content

Commit f72e220

Browse files
authored
Specialize broadcasting more unary functions over a OneElement (#384)
* Specialize broadcasting more unary functions over a OneElement * Add tests
1 parent 414dba7 commit f72e220

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/oneelement.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,10 @@ end
397397

398398
# broadcast
399399

400-
function broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::OneElement{<:Any,N}) where {N}
401-
OneElement(conj(r.val), r.ind, axes(r))
402-
end
403-
function broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::OneElement{<:Any,N}) where {N}
404-
OneElement(real(r.val), r.ind, axes(r))
405-
end
406-
function broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::OneElement{<:Any,N}) where {N}
407-
OneElement(imag(r.val), r.ind, axes(r))
400+
for f in (:abs, :abs2, :conj, :real, :imag)
401+
@eval function broadcasted(::DefaultArrayStyle{N}, ::typeof($f), r::OneElement{<:Any,N}) where {N}
402+
OneElement($f(r.val), r.ind, axes(r))
403+
end
408404
end
409405
function broadcasted(::DefaultArrayStyle{N}, ::typeof(^), r::OneElement{<:Any,N}, x::Number) where {N}
410406
OneElement(r.val^x, r.ind, axes(r))

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,9 +2669,11 @@ end
26692669
end
26702670

26712671
@testset "broadcasting" begin
2672-
for v in (OneElement(2, 3, 4), OneElement(2im, (1,2), (3,4)))
2672+
for v in (OneElement(-2, 3, 4), OneElement(2im, (1,2), (3,4)))
26732673
w = Array(v)
26742674
n = 2
2675+
@test abs.(v) == abs.(w)
2676+
@test abs2.(v) == abs2.(w)
26752677
@test real.(v) == real.(w)
26762678
@test imag.(v) == imag.(w)
26772679
@test conj.(v) == conj.(w)

0 commit comments

Comments
 (0)