Skip to content

Commit

Permalink
Deprecate manually vectorized trunc methods in favor of compact broad…
Browse files Browse the repository at this point in the history
…cast syntax.
  • Loading branch information
Sacha0 committed Sep 18, 2016
1 parent 2193638 commit 5ea8ba3
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 18 deletions.
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,11 @@ macro vectorize_2arg(S,f)
end
export @vectorize_1arg, @vectorize_2arg

# Deprecate manually vectorized trunc methods in favor of compact broadcast syntax
@deprecate trunc(M::Bidiagonal) trunc.(M)
@deprecate trunc(M::Tridiagonal) trunc.(M)
@deprecate trunc(M::SymTridiagonal) trunc.(M)
@deprecate trunc{T<:Integer}(::Type{T}, x::AbstractArray) trunc.(T, x)
@deprecate trunc(x::AbstractArray, digits::Integer, base::Integer = 10) trunc.(x, digits, base)

# End deprecations scheduled for 0.6
2 changes: 1 addition & 1 deletion 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 (:floor,:ceil,:round)
@eval begin
function ($f){T,R}(::Type{T}, x::AbstractArray{R,1})
[ ($f)(T, y)::T for y in x ]
Expand Down
7 changes: 5 additions & 2 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ function size(M::Bidiagonal, d::Integer)
end

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag, :abs)
broadcast(::typeof(trunc), M::Bidiagonal) = Bidiagonal(trunc.(M.dv), trunc.(M.ev), M.isupper)
for func in (:conj, :copy, :round, :floor, :ceil, :real, :imag, :abs)
@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(trunc), ::Type{T}, M::Bidiagonal) =
Bidiagonal(trunc.(T, M.dv), trunc.(T, M.ev), M.isupper)
for func in (:round, :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 @@ -71,10 +71,12 @@ end
similar{T}(S::SymTridiagonal, ::Type{T}) = SymTridiagonal{T}(similar(S.dv, T), similar(S.ev, T))

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
broadcast(::typeof(trunc), M::SymTridiagonal) = SymTridiagonal(trunc.(M.dv), trunc.(M.ev))
for func in (:conj, :copy, :round, :floor, :ceil, :abs, :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(trunc), ::Type{T}, M::SymTridiagonal) = SymTridiagonal(trunc.(T, M.dv), trunc.(T, M.ev))
for func in (:round, :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 @@ -388,12 +390,15 @@ end
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))

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
broadcast(::typeof(trunc), M::Tridiagonal) = Tridiagonal(trunc.(M.dl), trunc.(M.d), trunc.(M.du), trunc.(M.du2))
for func in (:conj, :copy, :round, :floor, :ceil, :abs, :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(trunc), ::Type{T}, M::Tridiagonal) =
Tridiagonal(trunc.(T, M.dl), trunc.(T, M.d), trunc.(T, M.du), trunc.(T, M.du2))
for func in (:round, :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
2 changes: 1 addition & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ broadcast{TTv}(::typeof(real), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_un
broadcast{TTv}(::typeof(imag), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2z_z2z_T(imag, A, TTv)
ceil{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(ceil, A, To)
floor{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(floor, A, To)
trunc{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(trunc, A, To)
broadcast{T<:Integer}(::typeof(trunc), ::Type{T}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(trunc, A, T)
round{To}(::Type{To}, A::SparseMatrixCSC) = _broadcast_unary_nz2z_z2z_T(round, A, To)

# Operations that map zeros to zeros and map nonzeros to nonzeros, yielding a sparse matrix
Expand Down
4 changes: 2 additions & 2 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
if elty <: BlasReal
@test floor(Int,T) == Bidiagonal(floor(Int,T.dv),floor(Int,T.ev),T.isupper)
@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 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 ceil(Int,T) == Bidiagonal(ceil(Int,T.dv),ceil(Int,T.ev),T.isupper)
Expand Down
2 changes: 1 addition & 1 deletion test/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
end

debug && println("Test forward error [JIN 5705] if this is not a BigFloat")
b = eltyb == Int ? trunc(Int,Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2))
b = eltyb == Int ? trunc.(Int,Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2))
x = Atri \ b
γ = n*ε/(1 - n*ε)
if eltya != BigFloat
Expand Down
8 changes: 4 additions & 4 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ let n = 12 #Size of matrix problem to test
if elty <: Real
@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 trunc.(Int,A) == trunc.(Int,fA)
@test isa(trunc.(Int,A), SymTridiagonal)
@test ceil(Int,A) == ceil(Int,fA)
@test isa(ceil(Int,A), SymTridiagonal)
@test floor(Int,A) == floor(Int,fA)
Expand Down Expand Up @@ -392,8 +392,8 @@ let n = 12 #Size of matrix problem to test
if elty <: Real
@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 trunc.(Int,A) == trunc.(Int,fA)
@test isa(trunc.(Int,A), Tridiagonal)
@test ceil(Int,A) == ceil(Int,fA)
@test isa(ceil(Int,A), Tridiagonal)
@test floor(Int,A) == floor(Int,fA)
Expand Down
14 changes: 11 additions & 3 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ end
for x=Int64(2)^53-2:Int64(2)^53+5,
y=[2.0^53-2 2.0^53-1 2.0^53 2.0^53+2 2.0^53+4]
u = UInt64(x)
@test y == Float64(trunc(Int64,y))
@test y == Float64(trunc.(Int64,y))

@test (x==y)==(y==x)
@test (x!=y)==!(x==y)
Expand Down Expand Up @@ -2021,14 +2021,22 @@ 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 (round, 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
end
for f in (trunc,)
@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)
@test approx_eq(signif(123.456,1), 100.)
Expand Down

0 comments on commit 5ea8ba3

Please sign in to comment.