From 0d922761e81888e097371518caf5426ddaabbdd1 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sun, 18 Sep 2016 16:07:10 -0700 Subject: [PATCH] Deprecate manually vectorized trunc methods in favor of compact broadcast syntax. --- base/deprecated.jl | 7 +++++++ base/floatfuncs.jl | 2 +- base/linalg/bidiag.jl | 7 +++++-- base/linalg/tridiag.jl | 13 +++++++++---- base/sparse/sparsematrix.jl | 1 - test/linalg/bidiag.jl | 4 ++-- test/linalg/triangular.jl | 2 +- test/linalg/tridiag.jl | 8 ++++---- test/numbers.jl | 12 ++++++++++-- 9 files changed, 39 insertions(+), 17 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 02781b4948114e..3bf004238d28c6 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1168,4 +1168,11 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs), end end +# 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 diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 492c3d5f25d361..97dcba095cac94 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 (:floor,:ceil,:round) @eval begin function ($f){T,R}(::Type{T}, x::AbstractArray{R,1}) [ ($f)(T, y)::T for y in x ] diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index 2dc1882f4e16e1..5f3cf3a9ed1d08 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -253,10 +253,13 @@ 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(trunc), M::Bidiagonal) = Bidiagonal(trunc.(M.dv), trunc.(M.ev), M.isupper) +for func in (:conj, :copy, :round, :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(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 diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index 55bf04368d7b7f..1625d2faad7677 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(trunc), M::SymTridiagonal) = SymTridiagonal(trunc.(M.dv), trunc.(M.ev)) +for func in (:conj, :copy, :round, :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(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 @@ -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(trunc), M::Tridiagonal) = Tridiagonal(trunc.(M.dl), trunc.(M.d), trunc.(M.du), trunc.(M.du2)) +for func in (:conj, :copy, :round, :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(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 diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index e2da90d6723597..910ae833ebc074 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -2278,7 +2278,6 @@ conj!(A::SparseMatrixCSC) = (broadcast!(conj, A.nzval, A.nzval); A) # TODO: The following definitions should be deprecated. 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) diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index b5b8110a49b6e6..299db1347f9197 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -162,8 +162,8 @@ srand(1) 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) diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 3cb032ed9aa350..290ef3d74784fd 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -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 diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 4c50fc2d6794ac..0297e7610635ad 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -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) @@ -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) diff --git a/test/numbers.jl b/test/numbers.jl index d09f15a2763949..e892b2d8d5af1d 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2026,14 +2026,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.)