Skip to content

Commit 5a98ec5

Browse files
committed
Deprecate manually vectorized round methods in favor of compact broadcast syntax.
1 parent 44d7677 commit 5a98ec5

File tree

12 files changed

+63
-60
lines changed

12 files changed

+63
-60
lines changed

base/deprecated.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1168,4 +1168,13 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
11681168
end
11691169
end
11701170

1171+
# Deprecate manually vectorized round methods in favor of compact broadcast syntax
1172+
@deprecate round(M::Bidiagonal) round.(M)
1173+
@deprecate round(M::Tridiagonal) round.(M)
1174+
@deprecate round(M::SymTridiagonal) round.(M)
1175+
@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray) round.(T, x)
1176+
@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray, r::RoundingMode) round.(x, r)
1177+
@deprecate round(x::AbstractArray, r::RoundingMode) round.(x, r)
1178+
@deprecate round(x::AbstractArray, digits::Integer, base::Integer = 10) round.(x, digits, base)
1179+
11711180
# 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
@@ -253,10 +253,12 @@ end
253253

254254
#Elementary operations
255255
broadcast(::typeof(abs), M::Bidiagonal) = Bidiagonal(abs.(M.dv), abs.(M.ev), abs.(M.isupper))
256-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag)
256+
broadcast(::typeof(round), M::Bidiagonal) = Bidiagonal(round.(M.dv), round.(M.ev), M.isupper)
257+
for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag)
257258
@eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper)
258259
end
259-
for func in (:round, :trunc, :floor, :ceil)
260+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Bidiagonal) = Bidiagonal(round.(T, M.dv), round.(T, M.ev), M.isupper)
261+
for func in (:trunc, :floor, :ceil)
260262
@eval ($func){T<:Integer}(::Type{T}, M::Bidiagonal) = Bidiagonal(($func)(T,M.dv), ($func)(T,M.ev), M.isupper)
261263
end
262264

base/linalg/tridiag.jl

+9-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ similar{T}(S::SymTridiagonal, ::Type{T}) = SymTridiagonal{T}(similar(S.dv, T), s
9797

9898
#Elementary operations
9999
broadcast(::typeof(abs), M::SymTridiagonal) = SymTridiagonal(abs.(M.dv), abs.(M.ev))
100-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag)
100+
broadcast(::typeof(round), M::SymTridiagonal) = SymTridiagonal(round.(M.dv), round.(M.ev))
101+
for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag)
101102
@eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev))
102103
end
103-
for func in (:round, :trunc, :floor, :ceil)
104+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::SymTridiagonal) = SymTridiagonal(round.(T, M.dv), round.(T, M.ev))
105+
for func in ( :trunc, :floor, :ceil)
104106
@eval ($func){T<:Integer}(::Type{T},M::SymTridiagonal) = SymTridiagonal(($func)(T,M.dv), ($func)(T,M.ev))
105107
end
106108
transpose(M::SymTridiagonal) = M #Identity operation
@@ -464,12 +466,15 @@ copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl),
464466

465467
#Elementary operations
466468
broadcast(::typeof(abs), M::Tridiagonal) = Tridiagonal(abs.(M.dl), abs.(M.d), abs.(M.du), abs.(M.du2))
467-
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag)
469+
broadcast(::typeof(round), M::Tridiagonal) = Tridiagonal(round.(M.dl), round.(M.d), round.(M.du), round.(M.du2))
470+
for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag)
468471
@eval function ($func)(M::Tridiagonal)
469472
Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2))
470473
end
471474
end
472-
for func in (:round, :trunc, :floor, :ceil)
475+
broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Tridiagonal) =
476+
Tridiagonal(round.(T, M.dl), round.(T, M.d), round.(T, M.du), round.(T, M.du2))
477+
for func in (:trunc, :floor, :ceil)
473478
@eval function ($func){T<:Integer}(::Type{T},M::Tridiagonal)
474479
Tridiagonal(($func)(T,M.dl), ($func)(T,M.d), ($func)(T,M.du), ($func)(T,M.du2))
475480
end

base/sparse/sparsematrix.jl

-1
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,6 @@ conj!(A::SparseMatrixCSC) = (broadcast!(conj, A.nzval, A.nzval); A)
22792279
ceil{To}(::Type{To}, A::SparseMatrixCSC) = ceil.(To, A)
22802280
floor{To}(::Type{To}, A::SparseMatrixCSC) = floor.(To, A)
22812281
trunc{To}(::Type{To}, A::SparseMatrixCSC) = trunc.(To, A)
2282-
round{To}(::Type{To}, A::SparseMatrixCSC) = round.(To, A)
22832282

22842283

22852284
## Binary arithmetic and boolean operators

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
@@ -164,8 +164,8 @@ srand(1)
164164
@test isa(floor(Int,T), Bidiagonal)
165165
@test trunc(Int,T) == Bidiagonal(trunc(Int,T.dv),trunc(Int,T.ev),T.isupper)
166166
@test isa(trunc(Int,T), Bidiagonal)
167-
@test round(Int,T) == Bidiagonal(round(Int,T.dv),round(Int,T.ev),T.isupper)
168-
@test isa(round(Int,T), Bidiagonal)
167+
@test round.(Int, T) == Bidiagonal(round.(Int, T.dv), round.(Int, T.ev), T.isupper)
168+
@test isa(round.(Int, T), Bidiagonal)
169169
@test ceil(Int,T) == Bidiagonal(ceil(Int,T.dv),ceil(Int,T.ev),T.isupper)
170170
@test isa(ceil(Int,T), Bidiagonal)
171171
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
@@ -2026,13 +2026,21 @@ x = 0.0
20262026
@test approx_eq(round(pi,3,5), 3.144)
20272027
# vectorized trunc/round/floor/ceil with digits/base argument
20282028
a = rand(2, 2, 2)
2029-
for f in (trunc, round, floor, ceil)
2029+
for f in (trunc, floor, ceil)
20302030
@test f(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
20312031
@test f(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
20322032
@test f(a, 9, 2) == map(x->f(x, 9, 2), a)
20332033
@test f(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1])
20342034
@test f(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1])
20352035
@test f(a, 9, 2) == map(x->f(x, 9, 2), a)
2036+
end
2037+
for f in (round,)
2038+
@test f.(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
2039+
@test f.(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
2040+
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
2041+
@test f.(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1])
2042+
@test f.(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1])
2043+
@test f.(a, 9, 2) == map(x->f(x, 9, 2), a)
20362044
end
20372045
# significant digits (would be nice to have a smart vectorized
20382046
# version of signif)

test/sparse/sparse.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ let A = speye(Int, 5), I=1:10, X=reshape([trues(10); falses(15)],5,5)
754754
@test A[I] == A[X] == c
755755
end
756756

757-
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2)
757+
let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2)
758758
FS = Array(S)
759759
FI = Array(I)
760760
@test sparse(FS[FI]) == S[I] == S[FI]
@@ -782,7 +782,7 @@ let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30,
782782
@test sum(S) == sumS2 + sum(1:sum(FI))
783783
end
784784

785-
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100))
785+
let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100))
786786
N = length(S) >> 2
787787
I = randperm(N) .* 4
788788
J = randperm(N)
@@ -1451,7 +1451,7 @@ Ai = trunc(Int,Ar*100)
14511451
@test norm(Ai,1) norm(Array(Ai),1)
14521452
@test norm(Ai,Inf) norm(Array(Ai),Inf)
14531453
@test vecnorm(Ai) vecnorm(Array(Ai))
1454-
Ai = round(Int,Ar*100)
1454+
Ai = round.(Int,Ar*100)
14551455
@test norm(Ai,1) norm(Array(Ai),1)
14561456
@test norm(Ai,Inf) norm(Array(Ai),Inf)
14571457
@test vecnorm(Ai) vecnorm(Array(Ai))
@@ -1596,7 +1596,7 @@ end
15961596
# 16073
15971597
@inferred sprand(1, 1, 1.0)
15981598
@inferred sprand(1, 1, 1.0, rand, Float64)
1599-
@inferred sprand(1, 1, 1.0, x->round(Int,rand(x)*100))
1599+
@inferred sprand(1, 1, 1.0, x->round.(Int,rand(x)*100))
16001600

16011601
# Test that concatenations of combinations of sparse matrices with sparse matrices or dense
16021602
# matrices/vectors yield sparse arrays

test/sparse/sparsevector.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,13 @@ end
912912
# left-division operations involving triangular matrices and sparse vectors (#14005)
913913
let m = 10
914914
sparsefloatvecs = SparseVector[sprand(m, 0.4) for k in 1:3]
915-
sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs]
915+
sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round.(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs]
916916
sparsecomplexvecs = SparseVector[SparseVector(m, sprvec.nzind, complex(sprvec.nzval, sprvec.nzval)) for sprvec in sparsefloatvecs]
917917

918918
sprmat = sprand(m, m, 0.2)
919919
sparsefloatmat = speye(m) + sprmat/(2m)
920920
sparsecomplexmat = speye(m) + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, complex(sprmat.nzval, sprmat.nzval)/(4m))
921-
sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round(Int, sprmat.nzval*10))
921+
sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round.(Int, sprmat.nzval*10))
922922

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

0 commit comments

Comments
 (0)