Skip to content

Commit 0568e7e

Browse files
committed
Deprecate manually vectorized float methods in favor of compact broadcast syntax.
1 parent fa4c02c commit 0568e7e

16 files changed

+54
-41
lines changed

base/broadcast.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Broadcast
55
using Base.Cartesian
66
using Base: promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex, linearindices, tail, OneTo, to_shape
77
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, , .%, .<<, .>>, .^
8-
export broadcast, broadcast!, bitbroadcast, dotview
8+
import Base: broadcast
9+
export broadcast!, bitbroadcast, dotview
910
export broadcast_getindex, broadcast_setindex!
1011

1112
## Broadcasting utilities ##

base/deprecated.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,4 +995,16 @@ macro vectorize_2arg(S,f)
995995
end
996996
export @vectorize_1arg, @vectorize_2arg
997997

998+
# Deprecate manually-vectorized float methods in favor of compact broadcast syntax
999+
@deprecate float(r::UnitRange) float.(r)
1000+
@deprecate float(r::StepRange) float.(r)
1001+
@deprecate float(r::FloatRange) float.(r)
1002+
@deprecate float(r::LinSpace) float.(r)
1003+
@deprecate float{T}(A::AbstractArray{T}) float.(A)
1004+
@deprecate float{T<:AbstractFloat}(A::AbstractArray{T}) float.(A)
1005+
@deprecate float{S<:AbstractString}(a::AbstractArray{S}) float.(a)
1006+
@deprecate float(S::SparseMatrixCSC) float.(S)
1007+
@deprecate float(x::AbstractSparseVector) float.(x)
1008+
@deprecate float{Tv<:AbstractFloat}(x::AbstractSparseVector{Tv}) float.(x)
1009+
9981010
# End deprecations scheduled for 0.6

base/float.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,17 @@ significand_mask(::Type{Float32}) = 0x007f_ffff
532532

533533
## Array operations on floating point numbers ##
534534

535-
float{T<:AbstractFloat}(A::AbstractArray{T}) = A
536-
537-
function float{T}(A::AbstractArray{T})
538-
if !isleaftype(T)
539-
error("`float` not defined on abstractly-typed arrays; please convert to a more specific type")
540-
end
541-
convert(AbstractArray{typeof(float(zero(T)))}, A)
535+
# float, broadcast over arrays
536+
broadcast{T<:AbstractFloat}(::typeof(float), A::AbstractArray{T}) = A
537+
broadcast(::typeof(float), r::UnitRange) = float(r.start):float(last(r))
538+
broadcast(::typeof(float), r::StepRange) = float(r.start):float(r.step):float(last(r))
539+
broadcast(::typeof(float), r::FloatRange) = FloatRange(float(r.start), float(r.step), r.len, float(r.divisor))
540+
function broadcast(::typeof(float), r::LinSpace)
541+
float(r.len) == r.len || error(string(r, ": too long for ", float))
542+
LinSpace(float(r.start), float(r.stop), float(r.len), float(r.divisor))
542543
end
543544

544-
for fn in (:float,:big)
545+
for fn in (:big,)
545546
@eval begin
546547
$fn(r::StepRange) = $fn(r.start):$fn(r.step):$fn(last(r))
547548
$fn(r::UnitRange) = $fn(r.start):$fn(last(r))

base/linalg/dense.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ used, otherwise the scaling and squaring algorithm (see [^H05]) is chosen.
262262
263263
"""
264264
expm{T<:BlasFloat}(A::StridedMatrix{T}) = expm!(copy(A))
265-
expm{T<:Integer}(A::StridedMatrix{T}) = expm!(float(A))
265+
expm{T<:Integer}(A::StridedMatrix{T}) = expm!(float.(A))
266266
expm(x::Number) = exp(x)
267267

268268
## Destructive matrix exponential using algorithm from Higham, 2008,
@@ -686,7 +686,7 @@ function sylvester{T<:BlasFloat}(A::StridedMatrix{T},B::StridedMatrix{T},C::Stri
686686
Y, scale = LAPACK.trsyl!('N','N', RA, RB, D)
687687
scale!(QA*A_mul_Bc(Y,QB), inv(scale))
688688
end
689-
sylvester{T<:Integer}(A::StridedMatrix{T},B::StridedMatrix{T},C::StridedMatrix{T}) = sylvester(float(A), float(B), float(C))
689+
sylvester{T<:Integer}(A::StridedMatrix{T},B::StridedMatrix{T},C::StridedMatrix{T}) = sylvester(float.(A), float.(B), float.(C))
690690

691691
# AX + XA' + C = 0
692692

@@ -704,5 +704,5 @@ function lyap{T<:BlasFloat}(A::StridedMatrix{T},C::StridedMatrix{T})
704704
Y, scale = LAPACK.trsyl!('N', T <: Complex ? 'C' : 'T', R, R, D)
705705
scale!(Q*A_mul_Bc(Y,Q), inv(scale))
706706
end
707-
lyap{T<:Integer}(A::StridedMatrix{T},C::StridedMatrix{T}) = lyap(float(A), float(C))
707+
lyap{T<:Integer}(A::StridedMatrix{T},C::StridedMatrix{T}) = lyap(float.(A), float.(C))
708708
lyap{T<:Number}(a::T, c::T) = -c/(2a)

base/parse.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ function parse{T<:AbstractFloat}(::Type{T}, s::AbstractString)
168168
end
169169

170170
float(x::AbstractString) = parse(Float64,x)
171-
172-
float{S<:AbstractString}(a::AbstractArray{S}) = map!(float, similar(a,typeof(float(0))), a)
171+
broadcast{S<:AbstractString}(::typeof(float), a::AbstractArray{S}) = map!(float, similar(a,typeof(float(0))), a)
173172

174173
## interface to parser ##
175174

base/sparse/sparsematrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ julia> full(A)
348348
"""
349349
full
350350

351-
float(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), float.(S.nzval))
351+
broadcast(::typeof(float), S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), float.(S.nzval))
352352

353353
complex(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), complex(copy(S.nzval)))
354354

base/sparse/sparsevector.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,9 @@ function reinterpret{T,Tv}(::Type{T}, x::AbstractSparseVector{Tv})
758758
SparseVector(length(x), copy(nonzeroinds(x)), reinterpret(T, nonzeros(x)))
759759
end
760760

761-
float{Tv<:AbstractFloat}(x::AbstractSparseVector{Tv}) = x
762-
float(x::AbstractSparseVector) =
763-
SparseVector(length(x), copy(nonzeroinds(x)), float(nonzeros(x)))
761+
broadcast{Tv<:AbstractFloat}(::typeof(float), x::AbstractSparseVector{Tv}) = x
762+
broadcast(::typeof(float), x::AbstractSparseVector) =
763+
SparseVector(length(x), copy(nonzeroinds(x)), float.(nonzeros(x)))
764764

765765
complex{Tv<:Complex}(x::AbstractSparseVector{Tv}) = x
766766
complex(x::AbstractSparseVector) =

base/sparse/umfpack.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ lufact{T<:AbstractFloat}(A::Union{SparseMatrixCSC{T},SparseMatrixCSC{Complex{T}}
156156
"Try lufact(convert(SparseMatrixCSC{Float64/Complex128,Int}, A)) for ",
157157
"sparse floating point LU using UMFPACK or lufact(full(A)) for generic ",
158158
"dense LU.")))
159-
lufact(A::SparseMatrixCSC) = lufact(float(A))
159+
lufact(A::SparseMatrixCSC) = lufact(float.(A))
160160

161161

162162
size(F::UmfpackLU) = (F.m, F.n)

test/linalg/lu.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ H = Rational{BigInt}[1//(i+j-1) for i = 1:nHilbert,j = 1:nHilbert]
170170
Hinv = Rational{BigInt}[(-1)^(i+j)*(i+j-1)*binomial(nHilbert+i-1,nHilbert-j)*binomial(nHilbert+j-1,nHilbert-i)*binomial(i+j-2,i-1)^2 for i = big(1):nHilbert,j=big(1):nHilbert]
171171
@test inv(H) == Hinv
172172
setprecision(2^10) do
173-
@test norm(Array{Float64}(inv(float(H)) - float(Hinv))) < 1e-100
173+
@test norm(Array{Float64}(inv(float.(H)) - float.(Hinv))) < 1e-100
174174
end
175175

176176
# Test balancing in eigenvector calculations

test/ranges.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ test_linspace_identity(linspace(1f0, 1f0, 1), linspace(-1f0, -1f0, 1))
658658
# PR 12200 and related
659659
for _r in (1:2:100, 1:100, 1f0:2f0:100f0, 1.0:2.0:100.0,
660660
linspace(1, 100, 10), linspace(1f0, 100f0, 10))
661-
float_r = float(_r)
661+
float_r = float.(_r)
662662
big_r = big(_r)
663663
@test typeof(big_r).name === typeof(_r).name
664664
if eltype(_r) <: AbstractFloat

test/reduce.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
@test sum([3.0]) === 3.0
4747

4848
z = reshape(1:16, (2,2,2,2))
49-
fz = float(z)
49+
fz = float.(z)
5050
@test sum(z) === 136
5151
@test sum(fz) === 136.0
5252

@@ -58,7 +58,7 @@ a = sum(sin, z)
5858
@test a sum(sin.(fz))
5959

6060
z = [-4, -3, 2, 5]
61-
fz = float(z)
61+
fz = float.(z)
6262
a = randn(32) # need >16 elements to trigger BLAS code path
6363
b = complex(randn(32), randn(32))
6464
@test sumabs(Float64[]) === 0.0

test/sorting.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ for n in [0:10; 100; 101; 1000; 1001]
230230
for rev in [false,true]
231231
# insertion sort (stable) as reference
232232
pi = sortperm(v, alg=InsertionSort, rev=rev)
233-
@test pi == sortperm(float(v), alg=InsertionSort, rev=rev)
233+
@test pi == sortperm(float.(v), alg=InsertionSort, rev=rev)
234234
@test isperm(pi)
235235
si = v[pi]
236236
@test [sum(si .== x) for x in r] == h
@@ -245,7 +245,7 @@ for n in [0:10; 100; 101; 1000; 1001]
245245
# stable algorithms
246246
for alg in [MergeSort]
247247
p = sortperm(v, alg=alg, rev=rev)
248-
@test p == sortperm(float(v), alg=alg, rev=rev)
248+
@test p == sortperm(float.(v), alg=alg, rev=rev)
249249
@test p == pi
250250
s = copy(v)
251251
permute!(s, p)
@@ -257,7 +257,7 @@ for n in [0:10; 100; 101; 1000; 1001]
257257
# unstable algorithms
258258
for alg in [QuickSort, PartialQuickSort(n)]
259259
p = sortperm(v, alg=alg, rev=rev)
260-
@test p == sortperm(float(v), alg=alg, rev=rev)
260+
@test p == sortperm(float.(v), alg=alg, rev=rev)
261261
@test isperm(p)
262262
@test v[p] == si
263263
s = copy(v)

test/sparsedir/cholmod.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,12 @@ for elty in (Float64, Complex{Float64})
470470
@test CHOLMOD.Sparse(CHOLMOD.Dense(A1Sparse)) == A1Sparse
471471
end
472472

473-
Af = float([4 12 -16; 12 37 -43; -16 -43 98])
473+
Af = float.([4 12 -16; 12 37 -43; -16 -43 98])
474474
As = sparse(Af)
475-
Lf = float([2 0 0; 6 1 0; -8 5 3])
476-
LDf = float([4 0 0; 3 1 0; -4 5 9]) # D is stored along the diagonal
477-
L_f = float([1 0 0; 3 1 0; -4 5 1]) # L by itself in LDLt of Af
478-
D_f = float([4 0 0; 0 1 0; 0 0 9])
475+
Lf = float.([2 0 0; 6 1 0; -8 5 3])
476+
LDf = float.([4 0 0; 3 1 0; -4 5 9]) # D is stored along the diagonal
477+
L_f = float.([1 0 0; 3 1 0; -4 5 1]) # L by itself in LDLt of Af
478+
D_f = float.([4 0 0; 0 1 0; 0 0 9])
479479

480480
# cholfact, no permutation
481481
Fs = cholfact(As, perm=[1:3;])

test/sparsedir/sparse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,9 +1189,9 @@ A = speye(5)
11891189

11901190
# test float
11911191
A = sprand(Bool, 5,5,0.0)
1192-
@test eltype(float(A)) == Float64 # issue #11658
1192+
@test eltype(float.(A)) == Float64 # issue #11658
11931193
A = sprand(Bool, 5,5,0.2)
1194-
@test float(A) == float(full(A))
1194+
@test float.(A) == float.(full(A))
11951195

11961196
# test sparsevec
11971197
A = sparse(ones(5,5))

test/sparsedir/sparsevector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ let a = SparseVector(8, [2, 5, 6], Int32[12, 35, 72])
255255
@test exact_equal(au, SparseVector(8, [2, 5, 6], UInt32[12, 35, 72]))
256256

257257
# float
258-
af = float(a)
259-
@test float(af) == af
258+
af = float.(a)
259+
@test float.(af) == af
260260
@test isa(af, SparseVector{Float64,Int})
261261
@test exact_equal(af, SparseVector(8, [2, 5, 6], [12., 35., 72.]))
262262
@test sparsevec(transpose(transpose(af))) == af

test/sparsedir/umfpack.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ for Tv in (Float64, Complex128)
2727

2828
b = [8., 45., -3., 3., 19.]
2929
x = lua\b
30-
@test x float([1:5;])
30+
@test x float.([1:5;])
3131

3232
@test norm(A*x-b,1) < eps(1e4)
3333
z = complex(b,zeros(b))
3434
x = Base.SparseArrays.A_ldiv_B!(lua, z)
35-
@test x float([1:5;])
35+
@test x float.([1:5;])
3636
@test z === x
3737
y = similar(z)
3838
A_ldiv_B!(y, lua, complex(b,zeros(b)))
@@ -42,24 +42,24 @@ for Tv in (Float64, Complex128)
4242

4343
b = [8., 20., 13., 6., 17.]
4444
x = lua'\b
45-
@test x float([1:5;])
45+
@test x float.([1:5;])
4646

4747
@test norm(A'*x-b,1) < eps(1e4)
4848
z = complex(b,zeros(b))
4949
x = Base.SparseArrays.Ac_ldiv_B!(lua, z)
50-
@test x float([1:5;])
50+
@test x float.([1:5;])
5151
@test x === z
5252
y = similar(x)
5353
Base.SparseArrays.Ac_ldiv_B!(y, lua, complex(b,zeros(b)))
5454
@test y x
5555

5656
@test norm(A'*x-b,1) < eps(1e4)
5757
x = lua.'\b
58-
@test x float([1:5;])
58+
@test x float.([1:5;])
5959

6060
@test norm(A.'*x-b,1) < eps(1e4)
6161
x = Base.SparseArrays.At_ldiv_B!(lua,complex(b,zeros(b)))
62-
@test x float([1:5;])
62+
@test x float.([1:5;])
6363
y = similar(x)
6464
Base.SparseArrays.At_ldiv_B!(y, lua,complex(b,zeros(b)))
6565
@test y x

0 commit comments

Comments
 (0)