Skip to content

Commit

Permalink
Deprecate vectorized two-argument complex methods in favor of compact…
Browse files Browse the repository at this point in the history
… broadcast syntax.
  • Loading branch information
Sacha0 committed Jan 1, 2017
1 parent d9318d5 commit 997785b
Show file tree
Hide file tree
Showing 37 changed files with 114 additions and 159 deletions.
46 changes: 0 additions & 46 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,49 +867,3 @@ end
## promotion to complex ##

_default_type(T::Type{Complex}) = Complex{Int}

function complex{S<:Real,T<:Real}(A::AbstractArray{S}, B::AbstractArray{T})
if size(A) != size(B); throw(DimensionMismatch()); end
F = similar(A, typeof(complex(zero(S),zero(T))))
RF, RA, RB = eachindex(F), eachindex(A), eachindex(B)
if RF == RA == RB
for i in RA
@inbounds F[i] = complex(A[i], B[i])
end
else
for (iF, iA, iB) in zip(RF, RA, RB)
@inbounds F[iF] = complex(A[iA], B[iB])
end
end
return F
end

function complex{T<:Real}(A::Real, B::AbstractArray{T})
F = similar(B, typeof(complex(A,zero(T))))
RF, RB = eachindex(F), eachindex(B)
if RF == RB
for i in RB
@inbounds F[i] = complex(A, B[i])
end
else
for (iF, iB) in zip(RF, RB)
@inbounds F[iF] = complex(A, B[iB])
end
end
return F
end

function complex{T<:Real}(A::AbstractArray{T}, B::Real)
F = similar(A, typeof(complex(zero(T),B)))
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
@inbounds F[i] = complex(A[i], B)
end
else
for (iF, iA) in zip(RF, RA)
@inbounds F[iF] = complex(A[iA], B)
end
end
return F
end
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,11 @@ for f in (:sec, :sech, :secd, :asec, :asech,
@eval @deprecate $f{T<:Number}(A::AbstractArray{T}) $f.(A)
end

# Deprecate vectorized two-argument complex in favor of compact broadcast syntax
@deprecate complex(A::AbstractArray, b::Real) complex.(A, b)
@deprecate complex(a::Real, B::AbstractArray) complex.(a, B)
@deprecate complex(A::AbstractArray, B::AbstractArray) complex.(A, B)

# Deprecate manually vectorized clamp methods in favor of compact broadcast syntax
@deprecate clamp(A::AbstractArray, lo, hi) clamp.(A, lo, hi)

Expand Down
4 changes: 2 additions & 2 deletions base/linalg/arpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function eupd_wrapper(T, n::Integer, sym::Bool, cmplx::Bool, bmat::String,
if info[1] != 0
throw(ARPACKException(info[1]))
end
evec = complex(Array{T}(n, nev+1), Array{T}(n, nev+1))
evec = complex.(Array{T}(n, nev+1), Array{T}(n, nev+1))

j = 1
while j <= nev
Expand All @@ -193,7 +193,7 @@ function eupd_wrapper(T, n::Integer, sym::Bool, cmplx::Bool, bmat::String,
end
end

d = complex(dr,di)
d = complex.(dr, di)

if j == nev+1
p = sortperm(dmap(d[1:nev]), rev=true)
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ conjugating the first vector.
# Example:
```jldoctest
julia> Base.BLAS.dotc(10, im*ones(10), 1, complex(ones(20), ones(20)), 2)
julia> Base.BLAS.dotc(10, im*ones(10), 1, complex.(ones(20), ones(20)), 2)
10.0 - 10.0im
```
"""
Expand All @@ -249,7 +249,7 @@ with stride `incx` and `n` elements of array `Y` with stride `incy`.
# Example:
```jldoctest
julia> Base.BLAS.dotu(10, im*ones(10), 1, complex(ones(20), ones(20)), 2)
julia> Base.BLAS.dotu(10, im*ones(10), 1, complex.(ones(20), ones(20)), 2)
-10.0 + 10.0im
```
"""
Expand Down
8 changes: 4 additions & 4 deletions base/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function eigfact!{T<:BlasReal}(A::StridedMatrix{T}; permute::Bool=true, scale::B
end
j += 1
end
return Eigen(complex(WR, WI), evec)
return Eigen(complex.(WR, WI), evec)
end

function eigfact!{T<:BlasComplex}(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true)
Expand Down Expand Up @@ -162,7 +162,7 @@ make rows and columns more equal in norm.
function eigvals!{T<:BlasReal}(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true)
issymmetric(A) && return eigvals!(Symmetric(A))
_, valsre, valsim, _ = LAPACK.geevx!(permute ? (scale ? 'B' : 'P') : (scale ? 'S' : 'N'), 'N', 'N', 'N', A)
return iszero(valsim) ? valsre : complex(valsre, valsim)
return iszero(valsim) ? valsre : complex.(valsre, valsim)
end
function eigvals!{T<:BlasComplex}(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true)
ishermitian(A) && return eigvals(Hermitian(A))
Expand Down Expand Up @@ -297,7 +297,7 @@ function eigfact!{T<:BlasReal}(A::StridedMatrix{T}, B::StridedMatrix{T})
end
j += 1
end
return GeneralizedEigen(complex(alphar, alphai)./beta, vecs)
return GeneralizedEigen(complex.(alphar, alphai)./beta, vecs)
end

function eigfact!{T<:BlasComplex}(A::StridedMatrix{T}, B::StridedMatrix{T})
Expand Down Expand Up @@ -364,7 +364,7 @@ Same as [`eigvals`](@ref), but saves space by overwriting the input `A` (and `B`
function eigvals!{T<:BlasReal}(A::StridedMatrix{T}, B::StridedMatrix{T})
issymmetric(A) && isposdef(B) && return eigvals!(Symmetric(A), Symmetric(B))
alphar, alphai, beta, vl, vr = LAPACK.ggev!('N', 'N', A, B)
return (iszero(alphai) ? alphar : complex(alphar, alphai))./beta
return (iszero(alphai) ? alphar : complex.(alphar, alphai))./beta
end
function eigvals!{T<:BlasComplex}(A::StridedMatrix{T}, B::StridedMatrix{T})
ishermitian(A) && isposdef(B) && return eigvals!(Hermitian(A), Hermitian(B))
Expand Down
8 changes: 4 additions & 4 deletions base/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5504,7 +5504,7 @@ for (gees, gges, elty) in
work = Array{$elty}(lwork)
end
end
A, vs, iszero(wi) ? wr : complex(wr, wi)
A, vs, iszero(wi) ? wr : complex.(wr, wi)
end

# * .. Scalar Arguments ..
Expand Down Expand Up @@ -5553,7 +5553,7 @@ for (gees, gges, elty) in
work = Array{$elty}(lwork)
end
end
A, B, complex(alphar, alphai), beta, vsl[1:(jobvsl == 'V' ? n : 0),:], vsr[1:(jobvsr == 'V' ? n : 0),:]
A, B, complex.(alphar, alphai), beta, vsl[1:(jobvsl == 'V' ? n : 0),:], vsr[1:(jobvsr == 'V' ? n : 0),:]
end
end
end
Expand Down Expand Up @@ -5753,7 +5753,7 @@ for (trexc, trsen, tgsen, elty) in
iwork = Array{BlasInt}(liwork)
end
end
T, Q, iszero(wi) ? wr : complex(wr, wi)
T, Q, iszero(wi) ? wr : complex.(wr, wi)
end
trsen!(select::StridedVector{BlasInt}, T::StridedMatrix{$elty}, Q::StridedMatrix{$elty}) =
trsen!('N', 'V', select, T, Q)
Expand Down Expand Up @@ -5822,7 +5822,7 @@ for (trexc, trsen, tgsen, elty) in
iwork = Array{BlasInt}(liwork)
end
end
S, T, complex(alphar, alphai), beta, Q, Z
S, T, complex.(alphar, alphai), beta, Q, Z
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,6 @@ function copy!(S::SharedArray, R::SharedArray)
return S
end

complex(S1::SharedArray,S2::SharedArray) = convert(SharedArray, complex(S1.s, S2.s))

function print_shmem_limits(slen)
try
if is_linux()
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ end
Ac_ldiv_B(L::FactorComponent, B) = ctranspose(L)\B

(\){T<:VTypes}(L::Factor{T}, B::Dense{T}) = solve(CHOLMOD_A, L, B)
(\)(L::Factor{Float64}, B::VecOrMat{Complex{Float64}}) = complex(L\real(B), L\imag(B))
(\)(L::Factor{Float64}, B::VecOrMat{Complex{Float64}}) = complex.(L\real(B), L\imag(B))
# First explicit TypeVars are necessary to avoid ambiguity errors with definition in
# linalg/factorizations.jl
(\){T<:VTypes}(L::Factor{T}, b::StridedVector) = Vector(L\convert(Dense{T}, b))
Expand Down
2 changes: 0 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,6 @@ float(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.row

complex(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), complex(copy(S.nzval)))

complex(A::SparseMatrixCSC, B::SparseMatrixCSC) = A + im*B

# Construct a sparse vector

# Note that unlike `vec` for arrays, this does not share data
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/umfpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ for itype in UmfpackIndexTypes
Up,Ui,Ux,Uz,
P, Q, C_NULL, C_NULL,
&0, Rs, lu.numeric)
(transpose(SparseMatrixCSC(min(n_row, n_col), n_row, increment!(Lp), increment!(Lj), complex(Lx, Lz))),
SparseMatrixCSC(min(n_row, n_col), n_col, increment!(Up), increment!(Ui), complex(Ux, Uz)),
(transpose(SparseMatrixCSC(min(n_row, n_col), n_row, increment!(Lp), increment!(Lj), complex.(Lx, Lz))),
SparseMatrixCSC(min(n_row, n_col), n_col, increment!(Up), increment!(Ui), complex.(Ux, Uz)),
increment!(P), increment!(Q), Rs)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ end
# Handle block matrices
A = [randn(2,2) for i = 1:2, j = 1:2]
@test issymmetric(A.'A)
A = [complex(randn(2,2), randn(2,2)) for i = 1:2, j = 1:2]
A = [complex.(randn(2,2), randn(2,2)) for i = 1:2, j = 1:2]
@test ishermitian(A'A)

# issue #7197
Expand Down
18 changes: 9 additions & 9 deletions test/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ for elty in (Float32, Float64, Complex64, Complex128)
U = randn(5,2)
V = randn(5,2)
if elty == Complex64 || elty == Complex128
U = complex(U, U)
V = complex(V, V)
U = complex.(U, U)
V = complex.(V, V)
end
U = convert(Array{elty, 2}, U)
V = convert(Array{elty, 2}, V)
Expand All @@ -23,8 +23,8 @@ for elty in (Complex64, Complex128)
U = randn(5,2)
V = randn(5,2)
if elty == Complex64 || elty == Complex128
U = complex(U, U)
V = complex(V, V)
U = complex.(U, U)
V = complex.(V, V)
end
U = convert(Array{elty, 2}, U)
V = convert(Array{elty, 2}, V)
Expand Down Expand Up @@ -58,8 +58,8 @@ for elty in [Float32, Float64, Complex64, Complex128]
@test BLAS.dot(x1,x2) sum(x1.*x2)
@test_throws DimensionMismatch BLAS.dot(x1,rand(elty, n + 1))
else
z1 = convert(Vector{elty}, complex(randn(n),randn(n)))
z2 = convert(Vector{elty}, complex(randn(n),randn(n)))
z1 = convert(Vector{elty}, complex.(randn(n),randn(n)))
z2 = convert(Vector{elty}, complex.(randn(n),randn(n)))
@test BLAS.dotc(z1,z2) sum(conj(z1).*z2)
@test BLAS.dotu(z1,z2) sum(z1.*z2)
@test_throws DimensionMismatch BLAS.dotc(z1,rand(elty, n + 1))
Expand All @@ -71,7 +71,7 @@ for elty in [Float32, Float64, Complex64, Complex128]
x = convert(Vector{elty}, randn(n))
@test BLAS.iamax(x) == indmax(abs.(x))
else
z = convert(Vector{elty}, complex(randn(n),randn(n)))
z = convert(Vector{elty}, complex.(randn(n),randn(n)))
@test BLAS.iamax(z) == indmax(map(x -> abs(real(x)) + abs(imag(x)), z))
end

Expand All @@ -87,8 +87,8 @@ for elty in [Float32, Float64, Complex64, Complex128]
@test_throws ArgumentError BLAS.axpy!(α, copy(x1), 1:div(n,2), copy(x2), 0:(div(n, 2) - 1))
@test BLAS.axpy!(α,copy(x1),1:n,copy(x2),1:n) x2 + α*x1
else
z1 = convert(Vector{elty}, complex(randn(n), randn(n)))
z2 = convert(Vector{elty}, complex(randn(n), randn(n)))
z1 = convert(Vector{elty}, complex.(randn(n), randn(n)))
z2 = convert(Vector{elty}, complex.(randn(n), randn(n)))
α = rand(elty)
@test BLAS.axpy!(α, copy(z1), copy(z2)) z2 + α * z1
@test_throws DimensionMismatch BLAS.axpy!(α, copy(z1), rand(elty, n + 1))
Expand Down
4 changes: 2 additions & 2 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,8 @@ end
@test_throws DomainError complex(2,2)^(-2)
@test complex(2.0,2.0)^(-2) === complex(0.0, -0.125)

@test complex(1.0,[1.0,1.0]) == [complex(1.0,1.0), complex(1.0,1.0)]
@test complex([1.0,1.0],1.0) == [complex(1.0,1.0), complex(1.0,1.0)]
@test complex.(1.0, [1.0, 1.0]) == [complex(1.0, 1.0), complex(1.0, 1.0)]
@test complex.([1.0, 1.0], 1.0) == [complex(1.0, 1.0), complex(1.0, 1.0)]
# robust division of Float64
# hard complex divisions from Fig 6 of arxiv.1210.4539
z7 = Complex{Float64}(3.898125604559113300e289, 8.174961907852353577e295)
Expand Down
2 changes: 1 addition & 1 deletion test/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ si = [0.9967207836936347,-1.4940914728163142,1.2841226760316475,-0.4524417279474
a = [1., 2., 1., 2.]
b = [1., 2., 3.]
@test conv(a, b) [1., 4., 8., 10., 7., 6.]
@test conv(complex(a, ones(4)), complex(b)) complex([1., 4., 8., 10., 7., 6.], [1., 3., 6., 6., 5., 3.])
@test conv(complex.(a, ones(4)), complex(b)) complex.([1., 4., 8., 10., 7., 6.], [1., 3., 6., 6., 5., 3.])

# Discrete cosine transform (DCT) tests

Expand Down
4 changes: 2 additions & 2 deletions test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ using Base.Test
n = 10
areal = sprandn(n,n,0.4)
breal = sprandn(n,n,0.4)
acmplx = complex(sprandn(n,n,0.4), sprandn(n,n,0.4))
bcmplx = complex(sprandn(n,n,0.4), sprandn(n,n,0.4))
acmplx = complex.(sprandn(n,n,0.4), sprandn(n,n,0.4))
bcmplx = complex.(sprandn(n,n,0.4), sprandn(n,n,0.4))

testtol = 1e-6

Expand Down
6 changes: 3 additions & 3 deletions test/linalg/bunchkaufman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ breal = randn(n,2)/2
bimg = randn(n,2)/2

@testset for eltya in (Float32, Float64, Complex64, Complex128, Int)
a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(areal, aimg) : areal)
a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(a2real, a2img) : a2real)
a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal)
a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(a2real, a2img) : a2real)
@testset for atype in ("Array", "SubArray")
asym = a'+a # symmetric indefinite
apd = a'*a # symmetric positive-definite
Expand All @@ -37,7 +37,7 @@ bimg = randn(n,2)/2
ε = εa = eps(abs(float(one(eltya))))

@testset for eltyb in (Float32, Float64, Complex64, Complex128, Int)
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex(breal, bimg) : breal)
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex.(breal, bimg) : breal)
@testset for btype in ("Array", "SubArray")
if btype == "Array"
b = b
Expand Down
12 changes: 6 additions & 6 deletions test/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ breal = randn(n,2)/2
bimg = randn(n,2)/2

for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(areal, aimg) : areal)
a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex(a2real, a2img) : a2real)
a = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal)
a2 = eltya == Int ? rand(1:7, n, n) : convert(Matrix{eltya}, eltya <: Complex ? complex.(a2real, a2img) : a2real)
apd = a'*a # symmetric positive-definite

apds = Symmetric(apd)
Expand Down Expand Up @@ -144,7 +144,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
end

for eltyb in (Float32, Float64, Complex64, Complex128, Int)
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex(breal, bimg) : breal)
b = eltyb == Int ? rand(1:5, n, 2) : convert(Matrix{eltyb}, eltyb <: Complex ? complex.(breal, bimg) : breal)
εb = eps(abs(float(one(eltyb))))
ε = max(εa,εb)

Expand Down Expand Up @@ -200,7 +200,7 @@ end
# Test generic cholfact!
for elty in (Float32, Float64, Complex{Float32}, Complex{Float64})
if elty <: Complex
A = complex(randn(5,5), randn(5,5))
A = complex.(randn(5,5), randn(5,5))
else
A = randn(5,5)
end
Expand All @@ -210,7 +210,7 @@ for elty in (Float32, Float64, Complex{Float32}, Complex{Float64})
end

# Test up- and downdates
let A = complex(randn(10,5), randn(10, 5)), v = complex(randn(5), randn(5))
let A = complex.(randn(10,5), randn(10, 5)), v = complex.(randn(5), randn(5))
for uplo in (:U, :L)
AcA = A'A
BcB = AcA + v*v'
Expand Down Expand Up @@ -257,7 +257,7 @@ end

# Fail if non-Hermitian
@test_throws ArgumentError cholfact(randn(5,5))
@test_throws ArgumentError cholfact(complex(randn(5,5), randn(5,5)))
@test_throws ArgumentError cholfact(complex.(randn(5,5), randn(5,5)))
@test_throws ArgumentError Base.LinAlg.chol!(randn(5,5))
@test_throws ArgumentError Base.LinAlg.cholfact!(randn(5,5),:U,Val{false})
@test_throws ArgumentError Base.LinAlg.cholfact!(randn(5,5),:U,Val{true})
Expand Down
Loading

0 comments on commit 997785b

Please sign in to comment.