Skip to content

Commit

Permalink
Deprecate manually vectorized round methods in favor of compact broad…
Browse files Browse the repository at this point in the history
…cast syntax.
  • Loading branch information
Sacha0 committed Dec 24, 2016
1 parent 44d7677 commit 33e6f99
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 61 deletions.
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions base/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 1 addition & 21 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/lru_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
32 changes: 16 additions & 16 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 33e6f99

Please sign in to comment.