Skip to content

Commit a0dca0a

Browse files
committed
Deprecate manually vectorized imag methods in favor of compact broadcast syntax.
1 parent a6a8946 commit a0dca0a

20 files changed

+50
-32
lines changed

base/abstractarraymath.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ conj{T<:Real}(x::AbstractArray{T}) = x
8686
conj!{T<:Real}(x::AbstractArray{T}) = x
8787

8888
real{T<:Real}(x::AbstractArray{T}) = x
89-
imag{T<:Real}(x::AbstractArray{T}) = zero(x)
89+
broadcast{T<:Real}(::typeof(imag), x::AbstractArray{T}) = zero(x)
9090

9191
+{T<:Number}(x::AbstractArray{T}) = x
9292
*{T<:Number}(x::AbstractArray{T,2}) = x

base/arraymath.jl

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ end
3131
(-)(A::AbstractArray{Bool}) = reshape([ -A[i] for i in eachindex(A) ], size(A))
3232

3333
real(A::AbstractArray) = reshape([ real(x) for x in A ], size(A))
34-
imag(A::AbstractArray) = reshape([ imag(x) for x in A ], size(A))
3534

3635
function !(A::AbstractArray{Bool})
3736
F = similar(A)

base/broadcast.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Broadcast
55
using Base.Cartesian
66
using Base: promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex, linearindices, tail, OneTo, to_shape
77
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, , .%, .<<, .>>, .^
8-
export broadcast, broadcast!, bitbroadcast, dotview
8+
import Base: broadcast
9+
export broadcast!, bitbroadcast, dotview
910
export broadcast_getindex, broadcast_setindex!
1011

1112
## Broadcasting utilities ##

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1000,4 +1000,7 @@ macro vectorize_2arg(S,f)
10001000
end
10011001
export @vectorize_1arg, @vectorize_2arg
10021002

1003+
# Deprecate manually vectorized imag methods in favor of compact broadcast syntax
1004+
@deprecate imag(A::AbstractArray) imag.(A)
1005+
10031006
# End deprecations scheduled for 0.6

base/linalg/arpack.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ function eupd_wrapper(T, n::Integer, sym::Bool, cmplx::Bool, bmat::String,
122122
elseif which == "SR" || which == "SA"
123123
dmap = x->-real(x)
124124
elseif which == "LI"
125-
dmap = imag
125+
dmap = x -> imag.(x)
126126
elseif which == "SI"
127-
dmap = x->-imag(x)
127+
dmap = x -> map(t -> -imag(t), x)
128128
end
129129

130130
if cmplx

base/linalg/bidiag.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ function size(M::Bidiagonal, d::Integer)
189189
end
190190

191191
#Elementary operations
192-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag, :abs)
192+
broadcast(::typeof(imag), M::Bidiagonal) = Bidiagonal(imag.(M.dv), imag.(M.ev), M.isupper)
193+
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :abs)
193194
@eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper)
194195
end
195196
for func in (:round, :trunc, :floor, :ceil)

base/linalg/diagonal.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ factorize(D::Diagonal) = D
7979

8080
abs(D::Diagonal) = Diagonal(abs(D.diag))
8181
real(D::Diagonal) = Diagonal(real(D.diag))
82-
imag(D::Diagonal) = Diagonal(imag(D.diag))
82+
broadcast(::typeof(imag), D::Diagonal) = Diagonal(imag.(D.diag))
8383

8484
istriu(D::Diagonal) = true
8585
istril(D::Diagonal) = true

base/linalg/symmetric.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ copy{T,S}(A::Symmetric{T,S}) = (B = copy(A.data); Symmetric{T,typeof(B)}(B,A.upl
9797
copy{T,S}(A::Hermitian{T,S}) = (B = copy(A.data); Hermitian{T,typeof(B)}(B,A.uplo))
9898
ishermitian(A::Hermitian) = true
9999
ishermitian{T<:Real,S}(A::Symmetric{T,S}) = true
100-
ishermitian{T<:Complex,S}(A::Symmetric{T,S}) = all(imag(A.data) .== 0)
100+
ishermitian{T<:Complex,S}(A::Symmetric{T,S}) = all(imag.(A.data) .== 0)
101101
issymmetric{T<:Real,S}(A::Hermitian{T,S}) = true
102-
issymmetric{T<:Complex,S}(A::Hermitian{T,S}) = all(imag(A.data) .== 0)
102+
issymmetric{T<:Complex,S}(A::Hermitian{T,S}) = all(imag.(A.data) .== 0)
103103
issymmetric(A::Symmetric) = true
104104
transpose(A::Symmetric) = A
105105
ctranspose{T<:Real}(A::Symmetric{T}) = A

base/linalg/triangular.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ end
4343
LowerTriangular(U::UpperTriangular) = throw(ArgumentError("cannot create a LowerTriangular matrix from an UpperTriangular input"))
4444
UpperTriangular(U::LowerTriangular) = throw(ArgumentError("cannot create an UpperTriangular matrix from a LowerTriangular input"))
4545

46-
imag(A::UpperTriangular) = UpperTriangular(imag(A.data))
47-
imag(A::LowerTriangular) = LowerTriangular(imag(A.data))
48-
imag(A::UnitLowerTriangular) = LowerTriangular(tril!(imag(A.data),-1))
49-
imag(A::UnitUpperTriangular) = UpperTriangular(triu!(imag(A.data),1))
46+
broadcast(::typeof(imag), A::UpperTriangular) = UpperTriangular(imag.(A.data))
47+
broadcast(::typeof(imag), A::LowerTriangular) = LowerTriangular(imag.(A.data))
48+
broadcast(::typeof(imag), A::UnitLowerTriangular) = LowerTriangular(tril!(imag.(A.data), -1))
49+
broadcast(::typeof(imag), A::UnitUpperTriangular) = UpperTriangular(triu!(imag.(A.data), 1))
5050

5151
convert(::Type{Array}, A::AbstractTriangular) = convert(Matrix, A)
5252
full(A::AbstractTriangular) = convert(Array, A)

base/linalg/tridiag.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ end
7171
similar{T}(S::SymTridiagonal, ::Type{T}) = SymTridiagonal{T}(similar(S.dv, T), similar(S.ev, T))
7272

7373
#Elementary operations
74-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
74+
broadcast(::typeof(imag), M::SymTridiagonal) = SymTridiagonal(imag.(M.dv), imag.(M.ev))
75+
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real)
7576
@eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev))
7677
end
7778
for func in (:round, :trunc, :floor, :ceil)
@@ -388,7 +389,8 @@ end
388389
copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl), copy!(dest.d, src.d), copy!(dest.du, src.du), copy!(dest.du2, src.du2))
389390

390391
#Elementary operations
391-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
392+
broadcast(::typeof(imag), M::Tridiagonal) = Tridiagonal(imag.(M.dl), imag.(M.d), imag.(M.du), imag.(M.du2))
393+
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real)
392394
@eval function ($func)(M::Tridiagonal)
393395
Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2))
394396
end

base/sparse/cholmod.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ end
14911491
Ac_ldiv_B(L::FactorComponent, B) = ctranspose(L)\B
14921492

14931493
(\){T<:VTypes}(L::Factor{T}, B::Dense{T}) = solve(CHOLMOD_A, L, B)
1494-
(\)(L::Factor{Float64}, B::VecOrMat{Complex{Float64}}) = complex(L\real(B), L\imag(B))
1494+
(\)(L::Factor{Float64}, B::VecOrMat{Complex{Float64}}) = complex(L\real(B), L\imag.(B))
14951495
# First explicit TypeVars are necessary to avoid ambiguity errors with definition in
14961496
# linalg/factorizations.jl
14971497
(\){T<:VTypes}(L::Factor{T}, b::StridedVector) = Vector(L\convert(Dense{T}, b))

base/sparse/umfpack.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ for itype in UmfpackIndexTypes
213213
@isok ccall(($sym_c, :libumfpack), $itype,
214214
($itype, $itype, Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64}, Ptr{Void},
215215
Ptr{Float64}, Ptr{Float64}),
216-
U.m, U.n, U.colptr, U.rowval, real(U.nzval), imag(U.nzval), tmp,
216+
U.m, U.n, U.colptr, U.rowval, real(U.nzval), imag.(U.nzval), tmp,
217217
umf_ctrl, umf_info)
218218
U.symbolic = tmp[1]
219219
return U
@@ -240,7 +240,7 @@ for itype in UmfpackIndexTypes
240240
status = ccall(($num_c, :libumfpack), $itype,
241241
(Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64}, Ptr{Void}, Ptr{Void},
242242
Ptr{Float64}, Ptr{Float64}),
243-
U.colptr, U.rowval, real(U.nzval), imag(U.nzval), U.symbolic, tmp,
243+
U.colptr, U.rowval, real(U.nzval), imag.(U.nzval), U.symbolic, tmp,
244244
umf_ctrl, umf_info)
245245
if status != UMFPACK_WARNING_singular_matrix
246246
umferror(status)
@@ -396,7 +396,7 @@ for (f!, umfpack) in ((:A_ldiv_B!, :UMFPACK_A),
396396
r = similar(b, Float64)
397397
i = similar(b, Float64)
398398
solve!(r, lu, convert(Vector{Float64}, real(b)), $umfpack)
399-
solve!(i, lu, convert(Vector{Float64}, imag(b)), $umfpack)
399+
solve!(i, lu, convert(Vector{Float64}, imag.(b)), $umfpack)
400400
# We have checked size in solve!
401401
@inbounds for k in eachindex(x)
402402
x[k] = Tb(r[k] + im*i[k])

test/bitarray.jl

+17-5
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,30 @@ tc(r1,r2) = false
1010
bitcheck(b::BitArray) = length(b.chunks) == 0 || (b.chunks[end] == b.chunks[end] & Base._msk_end(b))
1111
bitcheck(x) = true
1212

13-
function check_bitop(ret_type, func, args...)
13+
function check_bitop_call(ret_type, func, args...)
1414
r1 = func(args...)
1515
r2 = func(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
16+
check_bitop_tests(ret_type, r1, r2)
17+
end
18+
function check_bitop_dotcall(ret_type, func, args...)
19+
r1 = func.(args...)
20+
r2 = func.(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
21+
check_bitop_tests(ret_type, r1, r2)
22+
end
23+
function check_bitop_tests(ret_type, r1, r2)
1624
@test isa(r1, ret_type)
1725
@test tc(r1, r2)
1826
@test isequal(r1, convert(ret_type, r2))
1927
@test bitcheck(r1)
2028
end
21-
2229
macro check_bit_operation(ex, ret_type)
23-
@assert Meta.isexpr(ex, :call)
24-
Expr(:call, :check_bitop, esc(ret_type), map(esc,ex.args)...)
30+
if Meta.isexpr(ex, :call)
31+
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
32+
elseif Meta.isexpr(ex, :.)
33+
Expr(:call, :check_bitop_dotcall, esc(ret_type), esc(ex.args[1]), map(esc, ex.args[2].args)...)
34+
else
35+
throw(ArgumentError("first argument to @check_bit_operation must be an expression with head either :call or :. !"))
36+
end
2537
end
2638

2739
let t0 = time()
@@ -583,7 +595,7 @@ b1 = bitrand(n1, n2)
583595
@check_bit_operation (-)(b1) Matrix{Int}
584596
@check_bit_operation sign(b1) BitMatrix
585597
@check_bit_operation real(b1) BitMatrix
586-
@check_bit_operation imag(b1) BitMatrix
598+
@check_bit_operation imag.(b1) BitMatrix
587599
@check_bit_operation conj(b1) BitMatrix
588600

589601
b0 = falses(0)

test/blas.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ for elty in [Float32, Float64, Complex64, Complex128]
106106
@test BLAS.asum(b) sum(abs.(b))
107107
@test BLAS.iamax(b) indmax(abs.(b))
108108
else
109-
@test BLAS.asum(b) sum(abs.(real(b))) + sum(abs.(imag(b)))
109+
@test BLAS.asum(b) sum(abs.(real(b))) + sum(abs.(imag.(b)))
110110
@test BLAS.iamax(b) == indmax(map(x -> abs(real(x)) + abs(imag(x)), b))
111111
end
112112

test/linalg/arnoldi.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ let
133133
@test d[1] 1. # largest eigenvalue should be 1.
134134
v=reshape(v,(50,50)) # reshape to matrix
135135
v/=trace(v) # factor out arbitrary phase
136-
@test isapprox(vecnorm(imag(v)),0.) # it should be real
136+
@test isapprox(vecnorm(imag.(v)),0.) # it should be real
137137
v=real(v)
138138
# @test isapprox(vecnorm(v-v')/2,0.) # it should be Hermitian
139139
# Since this fails sometimes (numerical precision error),this test is commented out

test/linalg/bidiag.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
6767
@test big(T) == T
6868
@test full(abs.(T)) == abs.(diagm(dv)) + abs.(diagm(ev, isupper?1:-1))
6969
@test full(real(T)) == real(diagm(dv)) + real(diagm(ev, isupper?1:-1))
70-
@test full(imag(T)) == imag(diagm(dv)) + imag(diagm(ev, isupper?1:-1))
70+
@test full(imag.(T)) == imag.(diagm(dv)) + imag.(diagm(ev, isupper?1:-1))
7171
z = zeros(elty, n)
7272

7373
debug && println("Idempotent tests")

test/linalg/diagonal.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
2929

3030
@test full(real(D)) == real(DM)
3131
@test full(abs.(D)) == abs.(DM)
32-
@test full(imag(D)) == imag(DM)
32+
@test full(imag.(D)) == imag.(DM)
3333

3434
@test parent(D) == d
3535
@test diag(D) == d

test/linalg/schur.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, Int)
3636
f = schurfact(a)
3737
@test f[:vectors]*f[:Schur]*f[:vectors]' a
3838
@test sort(real(f[:values])) sort(real(d))
39-
@test sort(imag(f[:values])) sort(imag(d))
39+
@test sort(imag.(f[:values])) sort(imag.(d))
4040
@test istriu(f[:Schur]) || eltype(a)<:Real
4141
@test full(f) a
4242
@test_throws KeyError f[:A]

test/linalg/triangular.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa
143143

144144
# real
145145
@test full(real(A1)) == real(full(A1))
146-
@test full(imag(A1)) == imag(full(A1))
146+
@test full(imag.(A1)) == imag.(full(A1))
147147
@test full(abs.(A1)) == abs.(full(A1))
148148

149149
# Unary operations

test/linalg/tridiag.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)
5151

5252
@test abs.(T) == Tridiagonal(abs.(dl),abs.(d),abs.(du))
5353
@test real(T) == Tridiagonal(real(dl),real(d),real(du))
54-
@test imag(T) == Tridiagonal(imag(dl),imag(d),imag(du))
54+
@test imag.(T) == Tridiagonal(imag.(dl),imag.(d),imag.(du))
5555
@test abs.(Ts) == SymTridiagonal(abs.(d),abs.(dl))
5656
@test real(Ts) == SymTridiagonal(real(d),real(dl))
57-
@test imag(Ts) == SymTridiagonal(imag(d),imag(dl))
57+
@test imag.(Ts) == SymTridiagonal(imag.(d),imag.(dl))
5858

5959
# test interconversion of Tridiagonal and SymTridiagonal
6060
@test Tridiagonal(dl, d, dl) == SymTridiagonal(d, dl)

0 commit comments

Comments
 (0)