From 33e6f993c92a349e38f8ec50f3a8d946192d971e Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Mon, 19 Sep 2016 13:28:56 -0700 Subject: [PATCH] Deprecate manually vectorized round methods in favor of compact broadcast syntax. --- base/deprecated.jl | 9 +++++++++ base/dsp.jl | 6 +++--- base/floatfuncs.jl | 22 +--------------------- base/linalg/bidiag.jl | 6 ++++-- base/linalg/tridiag.jl | 13 +++++++++---- base/sparse/sparsematrix.jl | 1 - examples/lru_test.jl | 2 +- test/floatfuncs.jl | 32 ++++++++++++++++---------------- test/linalg/bidiag.jl | 4 ++-- test/linalg/tridiag.jl | 8 ++++---- test/numbers.jl | 10 +++++++++- test/sparse/sparse.jl | 8 ++++---- test/sparse/sparsevector.jl | 4 ++-- 13 files changed, 64 insertions(+), 61 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 02781b4948114e..75148d014f8868 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1168,4 +1168,13 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs), end end +# Deprecate manually vectorized round methods in favor of compact broadcast syntax +@deprecate round(M::Bidiagonal) round.(M) +@deprecate round(M::Tridiagonal) round.(M) +@deprecate round(M::SymTridiagonal) round.(M) +@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray) round.(T, x) +@deprecate round{T<:Integer}(::Type{T}, x::AbstractArray, r::RoundingMode) round.(x, r) +@deprecate round(x::AbstractArray, r::RoundingMode) round.(x, r) +@deprecate round(x::AbstractArray, digits::Integer, base::Integer = 10) round.(x, digits, base) + # End deprecations scheduled for 0.6 diff --git a/base/dsp.jl b/base/dsp.jl index f7a7bd02cff6ac..a3c7b924d37198 100644 --- a/base/dsp.jl +++ b/base/dsp.jl @@ -141,7 +141,7 @@ function conv{T<:Base.LinAlg.BlasFloat}(u::StridedVector{T}, v::StridedVector{T} end return y[1:n] end -conv{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}) = round(Int,conv(float(u), float(v))) +conv{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}) = round.(Int,conv(float(u), float(v))) conv{T<:Integer, S<:Base.LinAlg.BlasFloat}(u::StridedVector{T}, v::StridedVector{S}) = conv(float(u), v) conv{T<:Integer, S<:Base.LinAlg.BlasFloat}(u::StridedVector{S}, v::StridedVector{T}) = conv(u, float(v)) @@ -184,8 +184,8 @@ function conv2{T}(A::StridedMatrix{T}, B::StridedMatrix{T}) end return C end -conv2{T<:Integer}(A::StridedMatrix{T}, B::StridedMatrix{T}) = round(Int,conv2(float(A), float(B))) -conv2{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}, A::StridedMatrix{T}) = round(Int,conv2(float(u), float(v), float(A))) +conv2{T<:Integer}(A::StridedMatrix{T}, B::StridedMatrix{T}) = round.(Int,conv2(float(A), float(B))) +conv2{T<:Integer}(u::StridedVector{T}, v::StridedVector{T}, A::StridedMatrix{T}) = round.(Int,conv2(float(u), float(v), float(A))) """ xcorr(u,v) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 492c3d5f25d361..8a19c3ee19b783 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -112,7 +112,7 @@ function round(x::AbstractFloat, ::RoundingMode{:NearestTiesUp}) end round{T<:Integer}(::Type{T}, x::AbstractFloat, r::RoundingMode) = trunc(T,round(x,r)) -for f in (:trunc,:floor,:ceil,:round) +for f in (:trunc,:floor,:ceil) @eval begin function ($f){T,R}(::Type{T}, x::AbstractArray{R,1}) [ ($f)(T, y)::T for y in x ] @@ -135,26 +135,6 @@ for f in (:trunc,:floor,:ceil,:round) end end -function round{R}(x::AbstractArray{R,1}, r::RoundingMode) - [ round(y, r) for y in x ] -end -function round{R}(x::AbstractArray{R,2}, r::RoundingMode) - [ round(x[i,j], r) for i = 1:size(x,1), j = 1:size(x,2) ] -end -function round(x::AbstractArray, r::RoundingMode) - reshape([ round(y, r) for y in x ], size(x)) -end - -function round{T,R}(::Type{T}, x::AbstractArray{R,1}, r::RoundingMode) - [ round(T, y, r)::T for y in x ] -end -function round{T,R}(::Type{T}, x::AbstractArray{R,2}, r::RoundingMode) - [ round(T, x[i,j], r)::T for i = 1:size(x,1), j = 1:size(x,2) ] -end -function round{T}(::Type{T}, x::AbstractArray, r::RoundingMode) - reshape([ round(T, y, r)::T for y in x ], size(x)) -end - # adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212 # for round, og is the power of 10 relative to the decimal point # for signif, og is the absolute power of 10 diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index 2dc1882f4e16e1..d9eb7df00c668e 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -253,10 +253,12 @@ end #Elementary operations broadcast(::typeof(abs), M::Bidiagonal) = Bidiagonal(abs.(M.dv), abs.(M.ev), abs.(M.isupper)) -for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag) +broadcast(::typeof(round), M::Bidiagonal) = Bidiagonal(round.(M.dv), round.(M.ev), M.isupper) +for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag) @eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper) end -for func in (:round, :trunc, :floor, :ceil) +broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Bidiagonal) = Bidiagonal(round.(T, M.dv), round.(T, M.ev), M.isupper) +for func in (:trunc, :floor, :ceil) @eval ($func){T<:Integer}(::Type{T}, M::Bidiagonal) = Bidiagonal(($func)(T,M.dv), ($func)(T,M.ev), M.isupper) end diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index 55bf04368d7b7f..c8a7728396eec1 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -97,10 +97,12 @@ similar{T}(S::SymTridiagonal, ::Type{T}) = SymTridiagonal{T}(similar(S.dv, T), s #Elementary operations broadcast(::typeof(abs), M::SymTridiagonal) = SymTridiagonal(abs.(M.dv), abs.(M.ev)) -for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag) +broadcast(::typeof(round), M::SymTridiagonal) = SymTridiagonal(round.(M.dv), round.(M.ev)) +for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag) @eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev)) end -for func in (:round, :trunc, :floor, :ceil) +broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::SymTridiagonal) = SymTridiagonal(round.(T, M.dv), round.(T, M.ev)) +for func in ( :trunc, :floor, :ceil) @eval ($func){T<:Integer}(::Type{T},M::SymTridiagonal) = SymTridiagonal(($func)(T,M.dv), ($func)(T,M.ev)) end transpose(M::SymTridiagonal) = M #Identity operation @@ -464,12 +466,15 @@ copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl), #Elementary operations broadcast(::typeof(abs), M::Tridiagonal) = Tridiagonal(abs.(M.dl), abs.(M.d), abs.(M.du), abs.(M.du2)) -for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag) +broadcast(::typeof(round), M::Tridiagonal) = Tridiagonal(round.(M.dl), round.(M.d), round.(M.du), round.(M.du2)) +for func in (:conj, :copy, :trunc, :floor, :ceil, :real, :imag) @eval function ($func)(M::Tridiagonal) Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2)) end end -for func in (:round, :trunc, :floor, :ceil) +broadcast{T<:Integer}(::typeof(round), ::Type{T}, M::Tridiagonal) = + Tridiagonal(round.(T, M.dl), round.(T, M.d), round.(T, M.du), round.(T, M.du2)) +for func in (:trunc, :floor, :ceil) @eval function ($func){T<:Integer}(::Type{T},M::Tridiagonal) Tridiagonal(($func)(T,M.dl), ($func)(T,M.d), ($func)(T,M.du), ($func)(T,M.du2)) end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index e2da90d6723597..80d71f61bb3291 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -2279,7 +2279,6 @@ conj!(A::SparseMatrixCSC) = (broadcast!(conj, A.nzval, A.nzval); A) ceil{To}(::Type{To}, A::SparseMatrixCSC) = ceil.(To, A) floor{To}(::Type{To}, A::SparseMatrixCSC) = floor.(To, A) trunc{To}(::Type{To}, A::SparseMatrixCSC) = trunc.(To, A) -round{To}(::Type{To}, A::SparseMatrixCSC) = round.(To, A) ## Binary arithmetic and boolean operators diff --git a/examples/lru_test.jl b/examples/lru_test.jl index 92117f027ba5e6..62aa29fbf6a037 100644 --- a/examples/lru_test.jl +++ b/examples/lru_test.jl @@ -10,7 +10,7 @@ get_str(i) = String(vcat(map(x->[x>>4; x&0x0F], reinterpret(UInt8, [Int32(i)])). isbounded{L<:LRUExample.LRU}(::Type{L}) = any(map(n->n==:maxsize, fieldnames(L))) isbounded{L<:LRUExample.LRU}(l::L) = isbounded(L) -nmax = round(Int,logspace(2, 5, 4)) +nmax = round.(Int, logspace(2, 5, 4)) function lrutest() #println("LRU consistency tests") diff --git a/test/floatfuncs.jl b/test/floatfuncs.jl index 78546eae1b3ff8..94a751ad447ce6 100644 --- a/test/floatfuncs.jl +++ b/test/floatfuncs.jl @@ -45,26 +45,26 @@ end for elty in (Float32,Float64) x = rand(elty) A = fill(x,(10,10)) - @test round(A,RoundToZero) == fill(trunc(x),(10,10)) - @test round(A,RoundUp) == fill(ceil(x),(10,10)) - @test round(A,RoundDown) == fill(floor(x),(10,10)) + @test round.(A,RoundToZero) == fill(trunc(x),(10,10)) + @test round.(A,RoundUp) == fill(ceil(x),(10,10)) + @test round.(A,RoundDown) == fill(floor(x),(10,10)) A = fill(x,(10,10,10)) - @test round(A,RoundToZero) == fill(trunc(x),(10,10,10)) - @test round(A,RoundUp) == fill(ceil(x),(10,10,10)) - @test round(A,RoundDown) == fill(floor(x),(10,10,10)) + @test round.(A,RoundToZero) == fill(trunc(x),(10,10,10)) + @test round.(A,RoundUp) == fill(ceil(x),(10,10,10)) + @test round.(A,RoundDown) == fill(floor(x),(10,10,10)) for elty2 in (Int32,Int64) A = fill(x,(10,)) - @test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,)) - @test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,)) - @test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,)) + @test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,)) + @test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,)) + @test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,)) A = fill(x,(10,10)) - @test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10)) - @test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10)) - @test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10)) + @test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10)) + @test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10)) + @test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10)) A = fill(x,(10,10,10)) - @test round(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10,10)) - @test round(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10,10)) - @test round(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10,10)) - @test round(elty2,A) == fill(round(elty2,x),(10,10,10)) + @test round.(elty2,A,RoundToZero) == fill(trunc(elty2,x),(10,10,10)) + @test round.(elty2,A,RoundUp) == fill(ceil(elty2,x),(10,10,10)) + @test round.(elty2,A,RoundDown) == fill(floor(elty2,x),(10,10,10)) + @test round.(elty2,A) == fill(round(elty2,x),(10,10,10)) end end diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index b5b8110a49b6e6..915698397ebba4 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -164,8 +164,8 @@ srand(1) @test isa(floor(Int,T), Bidiagonal) @test trunc(Int,T) == Bidiagonal(trunc(Int,T.dv),trunc(Int,T.ev),T.isupper) @test isa(trunc(Int,T), Bidiagonal) - @test round(Int,T) == Bidiagonal(round(Int,T.dv),round(Int,T.ev),T.isupper) - @test isa(round(Int,T), Bidiagonal) + @test round.(Int, T) == Bidiagonal(round.(Int, T.dv), round.(Int, T.ev), T.isupper) + @test isa(round.(Int, T), Bidiagonal) @test ceil(Int,T) == Bidiagonal(ceil(Int,T.dv),ceil(Int,T.ev),T.isupper) @test isa(ceil(Int,T), Bidiagonal) end diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 4c50fc2d6794ac..568234808d36e0 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -272,8 +272,8 @@ let n = 12 #Size of matrix problem to test debug && println("Rounding to Ints") if elty <: Real - @test round(Int,A) == round(Int,fA) - @test isa(round(Int,A), SymTridiagonal) + @test round.(Int,A) == round.(Int,fA) + @test isa(round.(Int,A), SymTridiagonal) @test trunc(Int,A) == trunc(Int,fA) @test isa(trunc(Int,A), SymTridiagonal) @test ceil(Int,A) == ceil(Int,fA) @@ -390,8 +390,8 @@ let n = 12 #Size of matrix problem to test debug && println("Rounding to Ints") if elty <: Real - @test round(Int,A) == round(Int,fA) - @test isa(round(Int,A), Tridiagonal) + @test round.(Int,A) == round.(Int,fA) + @test isa(round.(Int,A), Tridiagonal) @test trunc(Int,A) == trunc(Int,fA) @test isa(trunc(Int,A), Tridiagonal) @test ceil(Int,A) == ceil(Int,fA) diff --git a/test/numbers.jl b/test/numbers.jl index d09f15a2763949..ff59f616aa1501 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2026,13 +2026,21 @@ x = 0.0 @test approx_eq(round(pi,3,5), 3.144) # vectorized trunc/round/floor/ceil with digits/base argument a = rand(2, 2, 2) -for f in (trunc, round, floor, ceil) +for f in (trunc, floor, ceil) @test f(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1]) @test f(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1]) @test f(a, 9, 2) == map(x->f(x, 9, 2), a) @test f(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1]) @test f(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1]) @test f(a, 9, 2) == map(x->f(x, 9, 2), a) +end +for f in (round,) + @test f.(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1]) + @test f.(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1]) + @test f.(a, 9, 2) == map(x->f(x, 9, 2), a) + @test f.(a[:, 1, 1], 9, 2) == map(x->f(x, 9, 2), a[:, 1, 1]) + @test f.(a[:, :, 1], 9, 2) == map(x->f(x, 9, 2), a[:, :, 1]) + @test f.(a, 9, 2) == map(x->f(x, 9, 2), a) end # significant digits (would be nice to have a smart vectorized # version of signif) diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 34ffd9add98f65..2089f293aba362 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -754,7 +754,7 @@ let A = speye(Int, 5), I=1:10, X=reshape([trues(10); falses(15)],5,5) @test A[I] == A[X] == c end -let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2) +let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2) FS = Array(S) FI = Array(I) @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, @test sum(S) == sumS2 + sum(1:sum(FI)) end -let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)) +let S = sprand(50, 30, 0.5, x->round.(Int,rand(x)*100)) N = length(S) >> 2 I = randperm(N) .* 4 J = randperm(N) @@ -1451,7 +1451,7 @@ Ai = trunc(Int,Ar*100) @test norm(Ai,1) ≈ norm(Array(Ai),1) @test norm(Ai,Inf) ≈ norm(Array(Ai),Inf) @test vecnorm(Ai) ≈ vecnorm(Array(Ai)) -Ai = round(Int,Ar*100) +Ai = round.(Int,Ar*100) @test norm(Ai,1) ≈ norm(Array(Ai),1) @test norm(Ai,Inf) ≈ norm(Array(Ai),Inf) @test vecnorm(Ai) ≈ vecnorm(Array(Ai)) @@ -1596,7 +1596,7 @@ end # 16073 @inferred sprand(1, 1, 1.0) @inferred sprand(1, 1, 1.0, rand, Float64) -@inferred sprand(1, 1, 1.0, x->round(Int,rand(x)*100)) +@inferred sprand(1, 1, 1.0, x->round.(Int,rand(x)*100)) # Test that concatenations of combinations of sparse matrices with sparse matrices or dense # matrices/vectors yield sparse arrays diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 0c0e27d08fe54d..d90ebda65da634 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -912,13 +912,13 @@ end # left-division operations involving triangular matrices and sparse vectors (#14005) let m = 10 sparsefloatvecs = SparseVector[sprand(m, 0.4) for k in 1:3] - sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs] + sparseintvecs = SparseVector[SparseVector(m, sprvec.nzind, round.(Int, sprvec.nzval*10)) for sprvec in sparsefloatvecs] sparsecomplexvecs = SparseVector[SparseVector(m, sprvec.nzind, complex(sprvec.nzval, sprvec.nzval)) for sprvec in sparsefloatvecs] sprmat = sprand(m, m, 0.2) sparsefloatmat = speye(m) + sprmat/(2m) sparsecomplexmat = speye(m) + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, complex(sprmat.nzval, sprmat.nzval)/(4m)) - sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round(Int, sprmat.nzval*10)) + sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round.(Int, sprmat.nzval*10)) denseintmat = eye(Int, m)*10m + rand(1:m, m, m) densefloatmat = eye(m) + randn(m, m)/(2m)