From 8ad7b1dacd16f8a70f2a464ddaf01d5e505fedf4 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 23 Aug 2024 13:21:20 +0530 Subject: [PATCH 1/2] Specialize broadcasting more unary functions over a OneElement --- src/oneelement.jl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/oneelement.jl b/src/oneelement.jl index 01cdea64..26439ab0 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -394,14 +394,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)) From bb32d2a83c2a438a19ba4b9cd88c4b78952ff917 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 23 Aug 2024 13:23:13 +0530 Subject: [PATCH 2/2] Add tests --- test/runtests.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 0163df8a..ed2a8717 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2667,9 +2667,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)