Skip to content

Commit e09b5f6

Browse files
committed
specialize real/imag/conj for real arrays
1 parent fc1b1ed commit e09b5f6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/fillbroadcast.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ function broadcast_preserving_0d(f, As...)
145145
end
146146
for f in (:real, :imag, :conj)
147147
@eval ($f)(A::AbstractFill) = broadcast_preserving_0d($f, A)
148+
@eval ($f)(A::AbstractZeros) = A
149+
end
150+
for T in (:AbstractOnes, :(AbstractFill{<:Real}))
151+
@eval real(A::$T) = A
152+
@eval imag(A::$T) = Zeros{eltype(A)}(axes(A))
153+
@eval conj(A::$T) = A
148154
end
149155

150156
### Binary broadcasting

test/runtests.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,25 @@ end
11701170
end
11711171

11721172
@testset "preserve 0d" begin
1173-
@test real(Fill(4 + 5im)) == real(fill(4 + 5im))
1174-
@test imag(Fill(4 + 5im)) == imag(fill(4 + 5im))
1175-
@test conj(Fill(4 + 5im)) == conj(fill(4 + 5im))
1173+
for f in (real, imag, conj),
1174+
(F, A) in ((Fill(4 + 5im), fill(4 + 5im)),
1175+
(Zeros{ComplexF64}(), zeros(ComplexF64)),
1176+
(Zeros(), zeros()),
1177+
(Ones(), ones()),
1178+
(Ones{ComplexF64}(), ones(ComplexF64)),
1179+
)
1180+
x = f(F)
1181+
y = f(A)
1182+
@test x == y
1183+
@test x isa FillArrays.AbstractFill
1184+
if F[] isa Real
1185+
if f === imag
1186+
@test x isa Zeros
1187+
else
1188+
@test x isa typeof(F)
1189+
end
1190+
end
1191+
end
11761192
end
11771193
end
11781194

0 commit comments

Comments
 (0)