Skip to content

Commit 54de981

Browse files
committed
Deprecate manually vectorized round methods in favor of compact broadcast syntax.
1 parent a6a8946 commit 54de981

File tree

12 files changed

+63
-59
lines changed

12 files changed

+63
-59
lines changed

base/deprecated.jl

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

1003+
# Deprecate manually vectorized round methods in favor of compact broadcast syntax
1004+
@deprecate round(M::Bidiagonal) round.(M)
1005+
@deprecate round(M::Tridiagonal) round.(M)
1006+
@deprecate round(M::SymTridiagonal) round.(M)
1007+
@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray) round.(T, x)
1008+
@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray, r::RoundingMode) round.(x, r)
1009+
@deprecate round(x::AbstractArray, r::RoundingMode) round.(x, r)
1010+
@deprecate round(x::AbstractArray, digits::Integer, base::Integer = 10) round.(x, digits, base)
1011+
10031012
# End deprecations scheduled for 0.6

base/dsp.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function conv{T<:Base.LinAlg.BlasFloat}(u::StridedVector{T}, v::StridedVector{T}
141141
end
142142
return y[1:n]
143143
end
144-
conv{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}) = round(Int,conv(float(u), float(v)))
144+
conv{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}) = round.(Int,conv(float(u), float(v)))
145145
conv{T<:Integer, S<:Base.LinAlg.BlasFloat}(u::StridedVector{T}, v::StridedVector{S}) = conv(float(u), v)
146146
conv{T<:Integer, S<:Base.LinAlg.BlasFloat}(u::StridedVector{S}, v::StridedVector{T}) = conv(u, float(v))
147147

@@ -184,8 +184,8 @@ function conv2{T}(A::StridedMatrix{T}, B::StridedMatrix{T})
184184
end
185185
return C
186186
end
187-
conv2{T<:Integer}(A::StridedMatrix{T}, B::StridedMatrix{T}) = round(Int,conv2(float(A), float(B)))
188-
conv2{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}, A::StridedMatrix{T}) = round(Int,conv2(float(u), float(v), float(A)))
187+
conv2{T<:Integer}(A::StridedMatrix{T}, B::StridedMatrix{T}) = round.(Int,conv2(float(A), float(B)))
188+
conv2{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}, A::StridedMatrix{T}) = round.(Int,conv2(float(u), float(v), float(A)))
189189

190190
"""
191191
xcorr(u,v)

base/floatfuncs.jl

+1-21
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function round(x::AbstractFloat, ::RoundingMode{:NearestTiesUp})
112112
end
113113
round{T<:Integer}(::Type{T}, x::AbstractFloat, r::RoundingMode) = trunc(T,round(x,r))
114114

115-
for f in (:trunc,:floor,:ceil,:round)
115+
for f in (:trunc,:floor,:ceil)
116116
@eval begin
117117
function ($f){T,R}(::Type{T}, x::AbstractArray{R,1})
118118
[ ($f)(T, y)::T for y in x ]
@@ -135,26 +135,6 @@ for f in (:trunc,:floor,:ceil,:round)
135135
end
136136
end
137137

138-
function round{R}(x::AbstractArray{R,1}, r::RoundingMode)
139-
[ round(y, r) for y in x ]
140-
end
141-
function round{R}(x::AbstractArray{R,2}, r::RoundingMode)
142-
[ round(x[i,j], r) for i = 1:size(x,1), j = 1:size(x,2) ]
143-
end
144-
function round(x::AbstractArray, r::RoundingMode)
145-
reshape([ round(y, r) for y in x ], size(x))
146-
end
147-
148-
function round{T,R}(::Type{T}, x::AbstractArray{R,1}, r::RoundingMode)
149-
[ round(T, y, r)::T for y in x ]
150-
end
151-
function round{T,R}(::Type{T}, x::AbstractArray{R,2}, r::RoundingMode)
152-
[ round(T, x[i,j], r)::T for i = 1:size(x,1), j = 1:size(x,2) ]
153-
end
154-
function round{T}(::Type{T}, x::AbstractArray, r::RoundingMode)
155-
reshape([ round(T, y, r)::T for y in x ], size(x))
156-
end
157-
158138
# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212
159139
# for round, og is the power of 10 relative to the decimal point
160140
# for signif, og is the absolute power of 10

base/linalg/bidiag.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ 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(round), M::Bidiagonal) = Bidiagonal(round.(M.dv), round.(M.ev), M.isupper)
193+
for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag, :abs)
193194
@eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper)
194195
end
195-
for func in (:round, :trunc, :floor, :ceil)
196+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Bidiagonal) = Bidiagonal(round.(T, M.dv), round.(T, M.ev), M.isupper)
197+
for func in (:trunc, :floor, :ceil)
196198
@eval ($func){T<:Integer}(::Type{T}, M::Bidiagonal) = Bidiagonal(($func)(T,M.dv), ($func)(T,M.ev), M.isupper)
197199
end
198200

base/linalg/tridiag.jl

+9-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ 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(round), M::SymTridiagonal) = SymTridiagonal(round.(M.dv), round.(M.ev))
75+
for func in (:conj, :copy, :trunc, :floor, :ceil, :abs, :real, :imag)
7576
@eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev))
7677
end
77-
for func in (:round, :trunc, :floor, :ceil)
78+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::SymTridiagonal) = SymTridiagonal(round.(T, M.dv), round.(T, M.ev))
79+
for func in ( :trunc, :floor, :ceil)
7880
@eval ($func){T<:Integer}(::Type{T},M::SymTridiagonal) = SymTridiagonal(($func)(T,M.dv), ($func)(T,M.ev))
7981
end
8082
transpose(M::SymTridiagonal) = M #Identity operation
@@ -388,12 +390,15 @@ end
388390
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))
389391

390392
#Elementary operations
391-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
393+
broadcast(::typeof(round), M::Tridiagonal) = Tridiagonal(round.(M.dl), round.(M.d), round.(M.du), round.(M.du2))
394+
for func in (:conj, :copy, :trunc, :floor, :ceil, :abs, :real, :imag)
392395
@eval function ($func)(M::Tridiagonal)
393396
Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2))
394397
end
395398
end
396-
for func in (:round, :trunc, :floor, :ceil)
399+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Tridiagonal) =
400+
Tridiagonal(round.(T, M.dl), round.(T, M.d), round.(T, M.du), round.(T, M.du2))
401+
for func in (:trunc, :floor, :ceil)
397402
@eval function ($func){T<:Integer}(::Type{T},M::Tridiagonal)
398403
Tridiagonal(($func)(T,M.dl), ($func)(T,M.d), ($func)(T,M.du), ($func)(T,M.du2))
399404
end

base/sparse/sparsematrix.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ broadcast{TTv}(::typeof(imag), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_un
14431443
ceil{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(ceil, A, To)
14441444
floor{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(floor, A, To)
14451445
trunc{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(trunc, A, To)
1446-
round{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(round, A, To)
1446+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(round, A, T)
14471447

14481448
# Operations that map zeros to zeros and map nonzeros to nonzeros, yielding a sparse matrix
14491449
"""

test/floatfuncs.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,26 @@ end
4545
for elty in (Float32,Float64)
4646
x = rand(elty)
4747
A = fill(x,(10,10))
48-
@test round(A,RoundToZero) == fill(trunc(x),(10,10))
49-
@test round(A,RoundUp) == fill(ceil(x),(10,10))
50-
@test round(A,RoundDown) == fill(floor(x),(10,10))
48+
@test round.(A,RoundToZero) == fill(trunc(x),(10,10))
49+
@test round.(A,RoundUp) == fill(ceil(x),(10,10))
50+
@test round.(A,RoundDown) == fill(floor(x),(10,10))
5151
A = fill(x,(10,10,10))
52-
@test round(A,RoundToZero) == fill(trunc(x),(10,10,10))
53-
@test round(A,RoundUp) == fill(ceil(x),(10,10,10))
54-
@test round(A,RoundDown) == fill(floor(x),(10,10,10))
52+
@test round.(A,RoundToZero) == fill(trunc(x),(10,10,10))
53+
@test round.(A,RoundUp) == fill(ceil(x),(10,10,10))
54+
@test round.(A,RoundDown) == fill(floor(x),(10,10,10))
5555
for elty2 in (Int32,Int64)
5656
A = fill(x,(10,))
57-
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,))
58-
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,))
59-
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,))
57+
@test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,))
58+
@test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,))
59+
@test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,))
6060
A = fill(x,(10,10))
61-
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10))
62-
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10))
63-
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10))
61+
@test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10))
62+
@test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10))
63+
@test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10))
6464
A = fill(x,(10,10,10))
65-
@test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10,10))
66-
@test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10,10))
67-
@test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10,10))
68-
@test round(elty2,A) == fill(round(elty2,x),(10,10,10))
65+
@test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10,10))
66+
@test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10,10))
67+
@test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10,10))
68+
@test round.(elty2,A) == fill(round(elty2,x),(10,10,10))
6969
end
7070
end

test/linalg/bidiag.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
162162
@test isa(floor(Int,T), Bidiagonal)
163163
@test trunc(Int,T) == Bidiagonal(trunc(Int,T.dv),trunc(Int,T.ev),T.isupper)
164164
@test isa(trunc(Int,T), Bidiagonal)
165-
@test round(Int,T) == Bidiagonal(round(Int,T.dv),round(Int,T.ev),T.isupper)
166-
@test isa(round(Int,T), Bidiagonal)
165+
@test round.(Int,T) == Bidiagonal(round.(Int,T.dv),round.(Int,T.ev),T.isupper)
166+
@test isa(round.(Int,T), Bidiagonal)
167167
@test ceil(Int,T) == Bidiagonal(ceil(Int,T.dv),ceil(Int,T.ev),T.isupper)
168168
@test isa(ceil(Int,T), Bidiagonal)
169169
end

test/linalg/tridiag.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ let n = 12 #Size of matrix problem to test
272272

273273
debug && println("Rounding to Ints")
274274
if elty <: Real
275-
@test round(Int,A) == round(Int,fA)
276-
@test isa(round(Int,A), SymTridiagonal)
275+
@test round.(Int,A) == round.(Int,fA)
276+
@test isa(round.(Int,A), SymTridiagonal)
277277
@test trunc(Int,A) == trunc(Int,fA)
278278
@test isa(trunc(Int,A), SymTridiagonal)
279279
@test ceil(Int,A) == ceil(Int,fA)
@@ -390,8 +390,8 @@ let n = 12 #Size of matrix problem to test
390390

391391
debug && println("Rounding to Ints")
392392
if elty <: Real
393-
@test round(Int,A) == round(Int,fA)
394-
@test isa(round(Int,A), Tridiagonal)
393+
@test round.(Int,A) == round.(Int,fA)
394+
@test isa(round.(Int,A), Tridiagonal)
395395
@test trunc(Int,A) == trunc(Int,fA)
396396
@test isa(trunc(Int,A), Tridiagonal)
397397
@test ceil(Int,A) == ceil(Int,fA)

test/numbers.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -2021,13 +2021,21 @@ x = 0.0
20212021
@test approx_eq(round(pi,3,5), 3.144)
20222022
# vectorized trunc/round/floor/ceil with digits/base argument
20232023
a = rand(2, 2, 2)
2024-
for f in (trunc, round, floor, ceil)
2024+
for f in (trunc, floor, ceil)
20252025
@test f(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
20262026
@test f(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
20272027
@test f(a, 9, 2) == map(x->f(x, 9, 2), a)
20282028
@test f(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1])
20292029
@test f(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1])
20302030
@test f(a, 9, 2) == map(x->f(x, 9, 2), a)
2031+
end
2032+
for f in (round,)
2033+
@test f.(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
2034+
@test f.(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
2035+
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
2036+
@test f.(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1])
2037+
@test f.(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1])
2038+
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
20312039
end
20322040
# significant digits (would be nice to have a smart vectorized
20332041
# version of signif)

test/sparsedir/sparse.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ let A = speye(Int, 5), I=1:10, X=reshape([trues(10); falses(15)],5,5)
740740
@test A[I] == A[X] == c
741741
end
742742

743-
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2)
743+
let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2)
744744
FS = full(S)
745745
FI = full(I)
746746
@test sparse(FS[FI]) == S[I] == S[FI]
@@ -768,7 +768,7 @@ let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30,
768768
@test sum(S) == sumS2 + sum(1:sum(FI))
769769
end
770770

771-
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100))
771+
let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100))
772772
N = length(S) >> 2
773773
I = randperm(N) .* 4
774774
J = randperm(N)
@@ -1567,7 +1567,7 @@ end
15671567
# 16073
15681568
@inferred sprand(1, 1, 1.0)
15691569
@inferred sprand(1, 1, 1.0, rand, Float64)
1570-
@inferred sprand(1, 1, 1.0, x->round(Int,rand(x)*100))
1570+
@inferred sprand(1, 1, 1.0, x->round.(Int,rand(x)*100))
15711571

15721572
# Test that concatenations of combinations of sparse matrices with sparse matrices or dense
15731573
# matrices/vectors yield sparse arrays

test/sparsedir/sparsevector.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,13 @@ end
872872
# left-division operations involving triangular matrices and sparse vectors (#14005)
873873
let m = 10
874874
sparsefloatvecs = SparseVector[sprand(m, 0.4) for k in 1:3]
875-
sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs]
875+
sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round.(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs]
876876
sparsecomplexvecs = SparseVector[SparseVector(m, sprvec.nzind, complex(sprvec.nzval, sprvec.nzval)) for sprvec in sparsefloatvecs]
877877

878878
sprmat = sprand(m, m, 0.2)
879879
sparsefloatmat = speye(m) + sprmat/(2m)
880880
sparsecomplexmat = speye(m) + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, complex(sprmat.nzval, sprmat.nzval)/(4m))
881-
sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round(Int, sprmat.nzval*10))
881+
sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round.(Int, sprmat.nzval*10))
882882

883883
denseintmat = eye(Int, m)*10m + rand(1:m, m, m)
884884
densefloatmat = eye(m) + randn(m, m)/(2m)

0 commit comments

Comments
 (0)