Skip to content

Commit

Permalink
Deprecate vectorized functions in base/math.jl in favor of compact br…
Browse files Browse the repository at this point in the history
…oadcast syntax.
  • Loading branch information
Sacha0 committed Sep 4, 2016
1 parent 7003a9d commit 898c512
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 46 deletions.
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,13 +888,15 @@ for f in (
:gamma, :lfact, :digamma, :trigamma, :zeta, :eta,# base/special/gamma.jl
:erfcx, :erfi, :dawson, # base/special/erf.jl
:airyprime, :airyai, :airyaiprime, :airybi, :airybiprime, :airy, :airyx, :besselj0, :besselj1, :bessely0, :bessely1, # base/special/bessel.jl
:cbrt, :sinh, :cosh, :tanh, :atan, :asinh, :exp, :erf, :erfc, :exp2, :expm1, :exp10, :sin, :cos, :tan, :asin, :acos, :acosh, :atanh, #=:log,=# :log2, :log10, :lgamma, #=:log1p,=# :sqrt, # base/math.jl
)
@eval @dep_vectorize_1arg Number $f
end
for f in (
:sind, :cosd, :tand, :asind, :acosd, :atand, :asecd, :acscd, :acotd, # base/special/trig.jl
:invdigamma, # base/special/gamma.jl
:erfinc, :erfcinv, # base/special/erf.jl
:rad2deg, :deg2rad, :exponent, :significand, # base/math.jl
)
@eval @dep_vectorize_1arg Real $f
end
Expand All @@ -903,8 +905,14 @@ end
for f in (
:polygamma, :zeta, :beta, :lbeta, # base/special/gamma.jl
:airy, :airyx, :besseli, :besselix, :besselj, :besseljx, :besselk, :besselkx, :bessely, :besselyx, :besselh, :besselhx, :hankelh1, :hankelh2, :hankelh1x, :hankelh2x, # base/special/bessel.jl
:log, :hypot, :atan2, # base/math.jl
)
@eval @dep_vectorize_2arg Number $f
end
for f in (
:max, :min, # base/math.jl
)
@eval @dep_vectorize_2arg Real $f
end

# End deprecations scheduled for 0.6
2 changes: 1 addition & 1 deletion base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ for T in (Irrational, Rational, Integer, Number)
^(::Irrational{:e}, x::T) = exp(x)
end
for T in (Range, BitArray, StridedArray, AbstractArray)
.^(::Irrational{:e}, x::T) = exp(x)
.^(::Irrational{:e}, x::T) = exp.(x)
end

log(::Irrational{:e}) = 1 # use 1 to correctly promote expressions like log(x)/log(e)
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ end
# identity matrices via eye(Diagonal{type},n)
eye{T}(::Type{Diagonal{T}}, n::Int) = Diagonal(ones(T,n))

expm(D::Diagonal) = Diagonal(exp(D.diag))
expm(D::Diagonal) = Diagonal(exp.(D.diag))
logm(D::Diagonal) = Diagonal(log.(D.diag))
sqrtm(D::Diagonal) = Diagonal(sqrt(D.diag))
sqrtm(D::Diagonal) = Diagonal(sqrt.(D.diag))

#Linear solver
function A_ldiv_B!(D::Diagonal, B::StridedVecOrMat)
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/GitHub/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ end
mapmap(f, xss) = map(xs->map(f, xs), xss)

colwidths(rows; len = length, min = 0) =
reduce(max, [min; convert(Vector{Vector{Int}}, mapmap(len, rows))])
reduce((x,y) -> max.(x,y), [min; convert(Vector{Vector{Int}}, mapmap(len, rows))])

padding(width, twidth, a) =
a == :l ? (0, twidth - width) :
Expand Down
14 changes: 0 additions & 14 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ julia> deg2rad(90)
deg2rad(z::AbstractFloat) = z * (oftype(z, pi) / 180)
rad2deg(z::Real) = rad2deg(float(z))
deg2rad(z::Real) = deg2rad(float(z))
@vectorize_1arg Real rad2deg
@vectorize_1arg Real deg2rad

log{T<:Number}(b::T, x::T) = log(x)/log(b)

Expand All @@ -165,7 +163,6 @@ julia> log(4,2)
```
"""
log(b::Number, x::Number) = log(promote(b,x)...)
@vectorize_2arg Number log

# type specific math functions

Expand All @@ -178,7 +175,6 @@ for f in (:cbrt, :sinh, :cosh, :tanh, :atan, :asinh, :exp, :erf, :erfc, :exp2, :
($f)(x::Float64) = ccall(($(string(f)),libm), Float64, (Float64,), x)
($f)(x::Float32) = ccall(($(string(f,"f")),libm), Float32, (Float32,), x)
($f)(x::Real) = ($f)(float(x))
@vectorize_1arg Number $f
end
end

Expand Down Expand Up @@ -230,7 +226,6 @@ end
exp10(x::Float64) = 10.0^x
exp10(x::Float32) = 10.0f0^x
exp10(x::Integer) = exp10(float(x))
@vectorize_1arg Number exp10

# utility for converting NaN return to DomainError
@inline nan_dom_err(f, x) = isnan(f) & !isnan(x) ? throw(DomainError()) : f
Expand All @@ -242,7 +237,6 @@ for f in (:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10,
($f)(x::Float64) = nan_dom_err(ccall(($(string(f)),libm), Float64, (Float64,), x), x)
($f)(x::Float32) = nan_dom_err(ccall(($(string(f,"f")),libm), Float32, (Float32,), x), x)
($f)(x::Real) = ($f)(float(x))
@vectorize_1arg Number $f
end
end

Expand All @@ -256,7 +250,6 @@ Return ``\\sqrt{x}``. Throws `DomainError` for negative `Real` arguments. Use co
negative arguments instead. The prefix operator `√` is equivalent to `sqrt`.
"""
sqrt(x::Real) = sqrt(float(x))
@vectorize_1arg Number sqrt

"""
hypot(x, y)
Expand Down Expand Up @@ -288,7 +281,6 @@ function hypot{T<:Number}(x::T, y::T)
return rr
end
end
@vectorize_2arg Number hypot

"""
hypot(x...)
Expand All @@ -308,16 +300,13 @@ atan2{T<:AbstractFloat}(y::T, x::T) = Base.no_op_err("atan2", T)

atan2(y::Float64, x::Float64) = ccall((:atan2,libm), Float64, (Float64, Float64,), y, x)
atan2(y::Float32, x::Float32) = ccall((:atan2f,libm), Float32, (Float32, Float32), y, x)
@vectorize_2arg Number atan2

max{T<:AbstractFloat}(x::T, y::T) = ifelse((y > x) | (signbit(y) < signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))

@vectorize_2arg Real max

min{T<:AbstractFloat}(x::T, y::T) = ifelse((y < x) | (signbit(y) > signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))
@vectorize_2arg Real min

minmax{T<:AbstractFloat}(x::T, y::T) = ifelse(isnan(x-y), ifelse(isnan(x), (y, y), (x, x)),
ifelse((y < x) | (signbit(y) > signbit(x)), (y, x),
Expand All @@ -332,7 +321,6 @@ Compute ``x \\times 2^n``.
"""
ldexp(x::Float64,e::Integer) = ccall((:scalbn,libm), Float64, (Float64,Int32), x, Int32(e))
ldexp(x::Float32,e::Integer) = ccall((:scalbnf,libm), Float32, (Float32,Int32), x, Int32(e))
# TODO: vectorize ldexp

"""
exponent(x) -> Int
Expand All @@ -353,7 +341,6 @@ function exponent{T<:AbstractFloat}(x::T)
end
k - exponent_bias(T)
end
@vectorize_1arg Real exponent

"""
significand(x)
Expand Down Expand Up @@ -386,7 +373,6 @@ function significand{T<:AbstractFloat}(x::T)
xu = (xu & ~exponent_mask(T)) | exponent_one(T)
reinterpret(T,xu)
end
@vectorize_1arg Real significand

"""
frexp(val)
Expand Down
8 changes: 4 additions & 4 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,8 @@ q[[1,3]] = true
@test map(^, p, q) == map((x,y)->x^y, p, q) == p .^ q
@test map(*, p, q) == map((x,y)->x*y, p, q) == p .* q

@test map(min, p, q) == map((x,y)->min(x,y), p, q) == min(p, q)
@test map(max, p, q) == map((x,y)->max(x,y), p, q) == max(p, q)
@test map(min, p, q) == map((x,y)->min(x,y), p, q) == min.(p, q)
@test map(max, p, q) == map((x,y)->max(x,y), p, q) == max.(p, q)

@test map(<, p, q) == map((x,y)->x<y, p, q) == (p .< q)
@test map(<=, p, q) == map((x,y)->x<=y, p, q) == (p .<= q)
Expand All @@ -1117,8 +1117,8 @@ r = falses(4)
@test map!(^, r, p, q) == map!((x,y)->x^y, r, p, q) == p .^ q == r
@test map!(*, r, p, q) == map!((x,y)->x*y, r, p, q) == p .* q == r

@test map!(min, r, p, q) == map!((x,y)->min(x,y), r, p, q) == min(p, q) == r
@test map!(max, r, p, q) == map!((x,y)->max(x,y), r, p, q) == max(p, q) == r
@test map!(min, r, p, q) == map!((x,y)->min(x,y), r, p, q) == min.(p, q) == r
@test map!(max, r, p, q) == map!((x,y)->max(x,y), r, p, q) == max.(p, q) == r

@test map!(<, r, p, q) == map!((x,y)->x<y, r, p, q) == (p .< q) == r
@test map!(<=, r, p, q) == map!((x,y)->x<=y, r, p, q) == (p .<= q) == r
Expand Down
4 changes: 2 additions & 2 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ let A = [sqrt(i)+j for i = 1:3, j=1:4]
@test atan2.(log.(A), sum(A,1)) == broadcast(atan2, broadcast(log, A), sum(A, 1))
end
let x = sin.(1:10)
@test atan2.((x->x+1).(x), (x->x+2).(x)) == atan2(x+1, x+2) == atan2(x.+1, x.+2)
@test atan2.((x->x+1).(x), (x->x+2).(x)) == broadcast(atan2, x+1, x+2) == broadcast(atan2, x.+1, x.+2)
@test sin.(atan2.([x+1,x+2]...)) == sin.(atan2.(x+1,x+2))
@test sin.(atan2.(x, 3.7)) == broadcast(x -> sin(atan2(x,3.7)), x)
@test atan2.(x, 3.7) == broadcast(x -> atan2(x,3.7), x) == broadcast(atan2, x, 3.7)
Expand All @@ -235,7 +235,7 @@ end
let x = sin.(1:10), a = [x]
@test cos.(x) == cos.(a...)
@test atan2.(x,x) == atan2.(a..., a...) == atan2.([x, x]...)
@test atan2.(x, cos.(x)) == atan2.(a..., cos.(x)) == atan2(x, cos.(a...)) == atan2(a..., cos.(a...))
@test atan2.(x, cos.(x)) == atan2.(a..., cos.(x)) == broadcast(atan2, x, cos.(a...)) == broadcast(atan2, a..., cos.(a...))
@test ((args...)->cos(args[1])).(x) == cos.(x) == ((y,args...)->cos(y)).(x)
end
@test atan2.(3,4) == atan2(3,4) == (() -> atan2(3,4)).()
Expand Down
4 changes: 2 additions & 2 deletions test/fft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ function fft_test{T<:Complex}(p::Base.DFT.Plan{T}, ntrials=4,
z = zeros(T, n)
i = rand(0:n-1)
z[i+1] = 1
X = exp(twopi_i*i)
X = exp.(twopi_i*i)
err = norm(p*z - X, Inf) / norm(X, Inf)
err <= tol || error("impulse-response error $err in $p")

# time-shift:
if n > 1
s = rand(1:n-1)
X = (p*x).*exp(twopi_i*s)
X = (p*x).*exp.(twopi_i*s)
err = norm(p*circshift(x,s) - X, Inf) / norm(X, Inf)
err <= tol || error("time-shift error $err in $p")
end
Expand Down
2 changes: 1 addition & 1 deletion test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# TODO: @test map!() much more thoroughly
let a = [1.0, 2.0]
map!(sin, a)
@test isequal(a, sin([1.0, 2.0]))
@test isequal(a, sin.([1.0, 2.0]))
end
# map -- ranges.jl
@test isequal(map(sqrt, 1:5), [sqrt(i) for i in 1:5])
Expand Down
2 changes: 1 addition & 1 deletion test/linalg/arnoldi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ end

debug && println("complex svds")
let # complex svds test
A = sparse([1, 1, 2, 3, 4], [2, 1, 1, 3, 1], exp(im*[2.0:2:10;]))
A = sparse([1, 1, 2, 3, 4], [2, 1, 1, 3, 1], exp.(im*[2.0:2:10;]))
S1 = svds(A, nsv = 2)
S2 = svd(full(A))

Expand Down
2 changes: 1 addition & 1 deletion test/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ end

#gebal/gebak
for elty in (Float32, Float64, Complex64, Complex128)
A = rand(elty,10,10) * Diagonal(exp10(linspace(-10,10,10)))
A = rand(elty,10,10) * Diagonal(exp10.(linspace(-10,10,10)))
B = copy(A)
ilo, ihi, scale = LAPACK.gebal!('S',B)
Bvs = eigvecs(B)
Expand Down
14 changes: 7 additions & 7 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ end
TAA = rand(2,2)
TAA = (TAA + TAA.')/2.
STAA = Symmetric(TAA)
@test full(atanh(STAA)) == atanh(TAA)
@test full(asinh(STAA)) == asinh(TAA)
@test full(acosh(STAA+Symmetric(ones(TAA)))) == acosh(TAA+ones(TAA))
@test full(acsch(STAA+Symmetric(ones(TAA)))) == acsch(TAA+ones(TAA))
@test full(acoth(STAA+Symmetric(ones(TAA)))) == acoth(TAA+ones(TAA))
@test full(atanh.(STAA)) == atanh.(TAA)
@test full(asinh.(STAA)) == asinh.(TAA)
@test full(acosh.(STAA+Symmetric(ones(TAA)))) == acosh.(TAA+ones(TAA))
@test full(acsch.(STAA+Symmetric(ones(TAA)))) == acsch.(TAA+ones(TAA))
@test full(acoth.(STAA+Symmetric(ones(TAA)))) == acoth.(TAA+ones(TAA))

# check exp2(::Integer) matches exp2(::Float)
for ii in -2048:2048
Expand All @@ -220,8 +220,8 @@ end

for T in (Int, Float64, BigFloat)
@test deg2rad(T(180)) 1pi
@test deg2rad(T[45, 60]) [pi/T(4), pi/T(3)]
@test rad2deg([pi/T(4), pi/T(3)]) [45, 60]
@test deg2rad.(T[45, 60]) [pi/T(4), pi/T(3)]
@test rad2deg.([pi/T(4), pi/T(3)]) [45, 60]
@test rad2deg(T(1)*pi) 180
@test rad2deg(T(1)) rad2deg(true)
@test deg2rad(T(1)) deg2rad(true)
Expand Down
8 changes: 4 additions & 4 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ e = eye(5)
a = [e[:,1], e[:,2], e[:,3], e[:,4], e[:,5]]
a1 = zeros(5)
c = [ones(Complex{Float64}, 5),
exp(-2*pi*im*(0:4)/5),
exp(-4*pi*im*(0:4)/5),
exp(-6*pi*im*(0:4)/5),
exp(-8*pi*im*(0:4)/5)]
exp.(-2*pi*im*(0:4)/5),
exp.(-4*pi*im*(0:4)/5),
exp.(-6*pi*im*(0:4)/5),
exp.(-8*pi*im*(0:4)/5)]
for s = -5:5
for i = 1:5
thisa = OffsetArray(a[i], (s,))
Expand Down
2 changes: 1 addition & 1 deletion test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ p = 1=>:foo
# issue #13144: max() with 4 or more array arguments
let xs = [[i:i+4;] for i in 1:10]
for n in 2:10
@test max(xs[1:n]...) == [n:n+4;]
@test max.(xs[1:n]...) == [n:n+4;]
end
end
2 changes: 1 addition & 1 deletion test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fz = float(z)
@test sum(sin, [3]) == sin(3.0)
a = sum(sin, z)
@test a sum(sin, fz)
@test a sum(sin(fz))
@test a sum(sin.(fz))

z = [-4, -3, 2, 5]
fz = float(z)
Expand Down
7 changes: 5 additions & 2 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ for f in (sum, prod, minimum, maximum)
end

# case where f(0) would throw
@test f(x->sqrt(x-1), pA+1) f(sqrt(pA))
@test f(x->sqrt(x-1), pA+1) f(sqrt.(pA))
# these actually throw due to #10533
# @test f(x->sqrt(x-1), pA+1, 1) ≈ f(sqrt(pA), 1)
# @test f(x->sqrt(x-1), pA+1, 2) ≈ f(sqrt(pA), 2)
Expand Down Expand Up @@ -1486,9 +1486,12 @@ let
@test min(A13024, B13024) == sparse([1,2,5], [1,2,5], fill(true,3))
@test typeof(min(A13024, B13024)) == SparseMatrixCSC{Bool,Int}

for op in (+, -, &, |, $, max, min)
for op in (+, -, &, |, $)
@test op(A13024, B13024) == op(full(A13024), full(B13024))
end
for op in (max, min)
@test op(A13024, B13024) == op.(full(A13024), full(B13024))
end
end

let A = 2. * speye(5,5)
Expand Down
4 changes: 2 additions & 2 deletions test/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ X = [2 3 1 -1; 7 4 5 -4]
@test std((1,2,3); mean=0) sqrt(7.0)
@test std((1,2,3); mean=0, corrected=false) sqrt(14.0/3)

@test std([1 2 3 4 5; 6 7 8 9 10], 2) sqrt([2.5 2.5]')
@test std([1 2 3 4 5; 6 7 8 9 10], 2; corrected=false) sqrt([2.0 2.0]')
@test std([1 2 3 4 5; 6 7 8 9 10], 2) sqrt.([2.5 2.5]')
@test std([1 2 3 4 5; 6 7 8 9 10], 2; corrected=false) sqrt.([2.0 2.0]')

A = Complex128[exp(i*im) for i in 1:10^4]
@test varm(A,0.) sum(map(abs2,A))/(length(A)-1)
Expand Down

0 comments on commit 898c512

Please sign in to comment.