Skip to content

Commit

Permalink
Specialize broadcasting more unary functions over a OneElement (#384)
Browse files Browse the repository at this point in the history
* Specialize broadcasting more unary functions over a OneElement

* Add tests
  • Loading branch information
jishnub authored Aug 27, 2024
1 parent 414dba7 commit f72e220
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,10 @@ end

# broadcast

function broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::OneElement{<:Any,N}) where {N}
OneElement(conj(r.val), r.ind, axes(r))
end
function broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::OneElement{<:Any,N}) where {N}
OneElement(real(r.val), r.ind, axes(r))
end
function broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::OneElement{<:Any,N}) where {N}
OneElement(imag(r.val), r.ind, axes(r))
for f in (:abs, :abs2, :conj, :real, :imag)
@eval function broadcasted(::DefaultArrayStyle{N}, ::typeof($f), r::OneElement{<:Any,N}) where {N}
OneElement($f(r.val), r.ind, axes(r))
end
end
function broadcasted(::DefaultArrayStyle{N}, ::typeof(^), r::OneElement{<:Any,N}, x::Number) where {N}
OneElement(r.val^x, r.ind, axes(r))
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2669,9 +2669,11 @@ end
end

@testset "broadcasting" begin
for v in (OneElement(2, 3, 4), OneElement(2im, (1,2), (3,4)))
for v in (OneElement(-2, 3, 4), OneElement(2im, (1,2), (3,4)))
w = Array(v)
n = 2
@test abs.(v) == abs.(w)
@test abs2.(v) == abs2.(w)
@test real.(v) == real.(w)
@test imag.(v) == imag.(w)
@test conj.(v) == conj.(w)
Expand Down

0 comments on commit f72e220

Please sign in to comment.