From 640ddacca775e112b448e5232e64e8467c8580e8 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Fri, 9 Feb 2018 11:42:30 -0800 Subject: [PATCH 1/4] Deprecate positional args in range to keyword args --- NEWS.md | 6 +- base/deprecated.jl | 4 ++ base/range.jl | 99 +++++++++++++++++++------------ base/twiceprecision.jl | 2 +- stdlib/Dates/src/deprecated.jl | 4 +- stdlib/Dates/test/ranges.jl | 16 ++--- stdlib/LinearAlgebra/src/dense.jl | 2 +- test/euler.jl | 4 +- test/ranges.jl | 12 +++- 9 files changed, 93 insertions(+), 56 deletions(-) diff --git a/NEWS.md b/NEWS.md index bdd72624e70d9..fcdbf197057ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1034,6 +1034,9 @@ Deprecated or removed * `scale!` has been deprecated in favor of `mul!`, `lmul!`, and `rmul!` ([#25701], [#25812]). + * The methods of `range` based on positional arguments have been deprecated in favor of + keyword arguments ([#25896]). + * `endof(a)` has been renamed to `lastindex(a)`, and the `end` keyword in indexing expressions now lowers to either `lastindex(a)` (in the case with only one index) or `lastindex(a, d)` (in cases where there is more than one index and `end` appears at dimension `d`) ([#23554], [#25763]). @@ -1305,4 +1308,5 @@ Command-line option changes [#25655]: https://github.com/JuliaLang/julia/issues/25655 [#25725]: https://github.com/JuliaLang/julia/issues/25725 [#25745]: https://github.com/JuliaLang/julia/issues/25745 -[#25998]: https://github.com/JuliaLang/julia/issues/25998 \ No newline at end of file +[#25896]: https://github.com/JuliaLang/julia/issues/25896 +[#25998]: https://github.com/JuliaLang/julia/issues/25998 diff --git a/base/deprecated.jl b/base/deprecated.jl index a358b0aac201d..e4ceab4561f34 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1312,6 +1312,10 @@ export readandwrite @deprecate indmin argmin @deprecate indmax argmax +# PR #25896 +@deprecate range(start, length) range(start, length=length) +@deprecate range(start, step, length) range(start, step=step, length=length) + @deprecate runtests(tests, ncores; kw...) runtests(tests; ncores = ncores, kw...) false @deprecate code_lowered(f, types, generated) code_lowered(f, types, generated = generated) diff --git a/base/range.jl b/base/range.jl index 18472f2bc9518..d8fd686b8f7df 100644 --- a/base/range.jl +++ b/base/range.jl @@ -4,17 +4,20 @@ colon(a::Real, b::Real) = colon(promote(a,b)...) colon(start::T, stop::T) where {T<:Real} = UnitRange{T}(start, stop) -range(a::Real, len::Integer) = UnitRange{typeof(a)}(a, oftype(a, a+len-1)) - colon(start::T, stop::T) where {T} = colon(start, oftype(stop-start, 1), stop) -range(a, len::Integer) = range(a, oftype(a-a, 1), len) - # first promote start and stop, leaving step alone colon(start::A, step, stop::C) where {A<:Real,C<:Real} = colon(convert(promote_type(A,C),start), step, convert(promote_type(A,C),stop)) colon(start::T, step::Real, stop::T) where {T<:Real} = colon(promote(start, step, stop)...) +# AbstractFloat specializations +colon(a::T, b::T) where {T<:AbstractFloat} = colon(a, T(1), b) + +colon(a::T, b::AbstractFloat, c::T) where {T<:Real} = colon(promote(a,b,c)...) +colon(a::T, b::AbstractFloat, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) +colon(a::T, b::Real, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) + """ colon(start, [step], stop) @@ -53,27 +56,45 @@ function _colon(start::T, step, stop::T) where T end """ - range(start, [step], length) + range(start; length, stop, step=1) -Construct a range by length, given a starting value and optional step (defaults to 1). +Given a starting value, construct a range either by length or from `start` to `stop`, +optionally with a given step (defaults to 1). One of `length` or `step` is required. +If both are specified, they must agree. """ -range(a::T, step, len::Integer) where {T} = _range(OrderStyle(T), ArithmeticStyle(T), a, step, len) -_range(::Ordered, ::ArithmeticWraps, a::T, step::S, len::Integer) where {T,S} = +range(start; length::Union{Integer,Nothing}=nothing, stop=nothing, step=nothing) = + _range(start, step, stop, length) + +# Range from start to stop: range(a, [step=s,] stop=b), no length +_range(start, step, stop, ::Nothing) = colon(start, step, stop) +_range(start, ::Nothing, stop, ::Nothing) = colon(start, stop) + +# Range of a given length: range(a, [step=s,] length=l), no stop +_range(a::Real, ::Nothing, ::Nothing, len::Integer) = UnitRange{typeof(a)}(a, oftype(a, a+len-1)) +_range(a::AbstractFloat, ::Nothing, ::Nothing, len::Integer) = _range(a, oftype(a, 1), nothing, len) +_range(a::AbstractFloat, st::AbstractFloat, ::Nothing, len::Integer) = _range(promote(a, st)..., nothing, len) +_range(a::Real, st::AbstractFloat, ::Nothing, len::Integer) = _range(float(a), st, nothing, len) +_range(a::AbstractFloat, st::Real, ::Nothing, len::Integer) = _range(a, float(st), nothing, len) +_range(a, ::Nothing, ::Nothing, len::Integer) = _range(a, oftype(a-a, 1), nothing, len) + +_range(a::T, step, ::Nothing, len::Integer) where {T} = + _rangestyle(OrderStyle(T), ArithmeticStyle(T), a, step, len) +_rangestyle(::Ordered, ::ArithmeticWraps, a::T, step::S, len::Integer) where {T,S} = StepRange{T,S}(a, step, convert(T, a+step*(len-1))) -_range(::Any, ::Any, a::T, step::S, len::Integer) where {T,S} = +_rangestyle(::Any, ::Any, a::T, step::S, len::Integer) where {T,S} = StepRangeLen{typeof(a+0*step),T,S}(a, step, len) -# AbstractFloat specializations -colon(a::T, b::T) where {T<:AbstractFloat} = colon(a, T(1), b) -range(a::AbstractFloat, len::Integer) = range(a, oftype(a, 1), len) - -colon(a::T, b::AbstractFloat, c::T) where {T<:Real} = colon(promote(a,b,c)...) -colon(a::T, b::AbstractFloat, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) -colon(a::T, b::Real, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) - -range(a::AbstractFloat, st::AbstractFloat, len::Integer) = range(promote(a, st)..., len) -range(a::Real, st::AbstractFloat, len::Integer) = range(float(a), st, len) -range(a::AbstractFloat, st::Real, len::Integer) = range(a, float(st), len) +# Malformed calls +_range(start, step, ::Nothing, ::Nothing) = # range(a, step=s) + throw(ArgumentError("At least one of `length` or `stop` must be specified")) +_range(start, ::Nothing, ::Nothing, ::Nothing) = # range(a) + throw(ArgumentError("At least one of `length` or `stop` must be specified")) +_range(::Nothing, ::Nothing, ::Nothing, ::Nothing) = # range(nothing) + throw(ArgumentError("At least one of `length` or `stop` must be specified")) +_range(start::Real, step::Real, stop::Real, length::Integer) = # range(a, step=s, stop=b, length=l) + throw(ArgumentError("Too many arguments specified; try passing only one of `stop` or `length`")) +_range(::Nothing, ::Nothing, ::Nothing, ::Integer) = # range(nothing, length=l) + throw(ArgumentError("Can't start a range at `nothing`")) ## 1-dimensional ranges ## @@ -545,7 +566,7 @@ function getindex(r::AbstractUnitRange, s::AbstractUnitRange{<:Integer}) @boundscheck checkbounds(r, s) f = first(r) st = oftype(f, f + first(s)-1) - range(st, length(s)) + range(st, length=length(s)) end function getindex(r::OneTo{T}, s::OneTo) where T @@ -558,14 +579,14 @@ function getindex(r::AbstractUnitRange, s::StepRange{<:Integer}) @_inline_meta @boundscheck checkbounds(r, s) st = oftype(first(r), first(r) + s.start-1) - range(st, step(s), length(s)) + range(st, step=step(s), length=length(s)) end function getindex(r::StepRange, s::AbstractRange{<:Integer}) @_inline_meta @boundscheck checkbounds(r, s) st = oftype(r.start, r.start + (first(s)-1)*step(r)) - range(st, step(r)*step(s), length(s)) + range(st, step=step(r)*step(s), length=length(s)) end function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T} @@ -627,7 +648,7 @@ intersect(r::AbstractUnitRange{<:Integer}, i::Integer) = intersect(i, r) function intersect(r::AbstractUnitRange{<:Integer}, s::StepRange{<:Integer}) if isempty(s) - range(first(r), 0) + range(first(r), length=0) elseif step(s) == 0 intersect(first(s), r) elseif step(s) < 0 @@ -654,7 +675,7 @@ end function intersect(r::StepRange, s::StepRange) if isempty(r) || isempty(s) - return range(first(r), step(r), 0) + return range(first(r), step=step(r), length=0) elseif step(s) < 0 return intersect(r, reverse(s)) elseif step(r) < 0 @@ -684,7 +705,7 @@ function intersect(r::StepRange, s::StepRange) if rem(start1 - start2, g) != 0 # Unaligned, no overlap possible. - return range(start1, a, 0) + return range(start1, step=a, length=0) end z = div(start1 - start2, g) @@ -729,39 +750,39 @@ end ## linear operations on ranges ## --(r::OrdinalRange) = range(-first(r), -step(r), length(r)) +-(r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r)) -(r::StepRangeLen{T,R,S}) where {T,R,S} = StepRangeLen{T,R,S}(-r.ref, -r.step, length(r), r.offset) -(r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) -*(x::Number, r::AbstractRange) = range(x*first(r), x*step(r), length(r)) +*(x::Number, r::AbstractRange) = range(x*first(r), step=x*step(r), length=length(r)) *(x::Number, r::StepRangeLen{T}) where {T} = StepRangeLen{typeof(x*T(r.ref))}(x*r.ref, x*r.step, length(r), r.offset) *(x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) # separate in case of noncommutative multiplication -*(r::AbstractRange, x::Number) = range(first(r)*x, step(r)*x, length(r)) +*(r::AbstractRange, x::Number) = range(first(r)*x, step=step(r)*x, length=length(r)) *(r::StepRangeLen{T}, x::Number) where {T} = StepRangeLen{typeof(T(r.ref)*x)}(r.ref*x, r.step*x, length(r), r.offset) *(r::LinSpace, x::Number) = LinSpace(r.start * x, r.stop * x, r.len) -/(r::AbstractRange, x::Number) = range(first(r)/x, step(r)/x, length(r)) +/(r::AbstractRange, x::Number) = range(first(r)/x, step=step(r)/x, length=length(r)) /(r::StepRangeLen{T}, x::Number) where {T} = StepRangeLen{typeof(T(r.ref)/x)}(r.ref/x, r.step/x, length(r), r.offset) /(r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) # also, separate in case of noncommutative multiplication (division) -\(x::Number, r::AbstractRange) = range(x\first(r), x\step(r), x\length(r)) +\(x::Number, r::AbstractRange) = range(x\first(r), step=x\step(r), length=x\length(r)) \(x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) \(x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) ## scalar-range broadcast operations ## -broadcast(::typeof(-), r::OrdinalRange) = range(-first(r), -step(r), length(r)) +broadcast(::typeof(-), r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r)) broadcast(::typeof(-), r::StepRangeLen) = StepRangeLen(-r.ref, -r.step, length(r), r.offset) broadcast(::typeof(-), r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) -broadcast(::typeof(+), x::Real, r::AbstractUnitRange) = range(x + first(r), length(r)) +broadcast(::typeof(+), x::Real, r::AbstractUnitRange) = range(x + first(r), length=length(r)) # For #18336 we need to prevent promotion of the step type: -broadcast(::typeof(+), x::Number, r::AbstractUnitRange) = range(x + first(r), step(r), length(r)) +broadcast(::typeof(+), x::Number, r::AbstractUnitRange) = range(x + first(r), step=step(r), length=length(r)) broadcast(::typeof(+), x::Number, r::AbstractRange) = (x+first(r)):step(r):(x+last(r)) function broadcast(::typeof(+), x::Number, r::StepRangeLen{T}) where T newref = x + r.ref @@ -780,19 +801,19 @@ end broadcast(::typeof(-), r::AbstractRange, x::Number) = broadcast(+, -x, r) # assumes addition is commutative -broadcast(::typeof(*), x::Number, r::AbstractRange) = range(x*first(r), x*step(r), length(r)) +broadcast(::typeof(*), x::Number, r::AbstractRange) = range(x*first(r), step=x*step(r), length=length(r)) broadcast(::typeof(*), x::Number, r::StepRangeLen) = StepRangeLen(x*r.ref, x*r.step, length(r), r.offset) broadcast(::typeof(*), x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) # separate in case of noncommutative multiplication -broadcast(::typeof(*), r::AbstractRange, x::Number) = range(first(r)*x, step(r)*x, length(r)) +broadcast(::typeof(*), r::AbstractRange, x::Number) = range(first(r)*x, step=step(r)*x, length=length(r)) broadcast(::typeof(*), r::StepRangeLen, x::Number) = StepRangeLen(r.ref*x, r.step*x, length(r), r.offset) broadcast(::typeof(*), r::LinSpace, x::Number) = LinSpace(r.start * x, r.stop * x, r.len) -broadcast(::typeof(/), r::AbstractRange, x::Number) = range(first(r)/x, step(r)/x, length(r)) +broadcast(::typeof(/), r::AbstractRange, x::Number) = range(first(r)/x, step=step(r)/x, length=length(r)) broadcast(::typeof(/), r::StepRangeLen, x::Number) = StepRangeLen(r.ref/x, r.step/x, length(r), r.offset) broadcast(::typeof(/), r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) # also, separate in case of noncommutative multiplication (division) -broadcast(::typeof(\), x::Number, r::AbstractRange) = range(x\first(r), x\step(r), x\length(r)) +broadcast(::typeof(\), x::Number, r::AbstractRange) = range(x\first(r), step=x\step(r), length=x\length(r)) broadcast(::typeof(\), x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) broadcast(::typeof(\), x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) @@ -948,7 +969,7 @@ function _define_range_op(@nospecialize f) r1l = length(r1) (r1l == length(r2) || throw(DimensionMismatch("argument dimensions must match"))) - range($f(first(r1),first(r2)), $f(step(r1),step(r2)), r1l) + range($f(first(r1), first(r2)), step=$f(step(r1), step(r2)), length=r1l) end function $f(r1::LinSpace{T}, r2::LinSpace{T}) where T diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 305d3a8c507cd..8abb28ce1cb53 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -421,7 +421,7 @@ function colon(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float6 steprangelen_hp(T, start, step, 0, len, 1) end -function range(a::T, st::T, len::Integer) where T<:Union{Float16,Float32,Float64} +function _range(a::T, st::T, ::Nothing, len::Integer) where T<:Union{Float16,Float32,Float64} start_n, start_d = rat(a) step_n, step_d = rat(st) if start_d != 0 && step_d != 0 && diff --git a/stdlib/Dates/src/deprecated.jl b/stdlib/Dates/src/deprecated.jl index e12ff13be6804..e7f74d328a916 100644 --- a/stdlib/Dates/src/deprecated.jl +++ b/stdlib/Dates/src/deprecated.jl @@ -40,8 +40,8 @@ import Base.range @deprecate colon(start::T, stop::T) where {T<:Date} start:Day(1):stop false @deprecate colon(start::T, stop::T) where {T<:Time} start:Second(1):stop false -@deprecate range(start::DateTime, len::Integer) range(start, Day(1), len) false -@deprecate range(start::Date, len::Integer) range(start, Day(1), len) false +@deprecate range(start::DateTime, len::Integer) range(start, step=Day(1), length=len) false +@deprecate range(start::Date, len::Integer) range(start, step=Day(1), length=len) false # PR #23724 @deprecate DateTime() DateTime(1) diff --git a/stdlib/Dates/test/ranges.jl b/stdlib/Dates/test/ranges.jl index 3edf61dff123a..4ac7d3e58ff53 100644 --- a/stdlib/Dates/test/ranges.jl +++ b/stdlib/Dates/test/ranges.jl @@ -297,7 +297,7 @@ drs2 = map(x->Dates.Date(first(x)):step(x):Dates.Date(last(x)), drs) @test map(length, drs) == map(x->length(reverse(x)), drs) @test all(x->findall(occursin(x), x)==[1:length(x);], drs[1:4]) @test isempty(dr2) -@test all(x->reverse(x) == range(last(x), -step(x), length(x)), drs) +@test all(x->reverse(x) == range(last(x), step=-step(x), length=length(x)), drs) @test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end]) @test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), drs[4:end]) @test all(drs[1:3]) do dd @@ -441,11 +441,11 @@ c = Dates.Date(2013, 6, 1) @test [a:Dates.Month(2):Dates.Date(2013, 1, 2);] == [a] @test [c:Dates.Month(-1):a;] == reverse([a:Dates.Month(1):c;]) -@test length(range(Date(2000), Dates.Day(1), 366)) == 366 +@test length(range(Date(2000), step=Dates.Day(1), length=366)) == 366 let n = 100000 local a = Dates.Date(2000) for i = 1:n - @test length(range(a, Dates.Day(1), i)) == i + @test length(range(a, step=Dates.Day(1), length=i)) == i end return a + Dates.Day(n) end @@ -518,11 +518,11 @@ end @test_throws OverflowError length(typemin(Dates.Year):Dates.Year(1):typemax(Dates.Year)) @test_throws MethodError Dates.Date(0):Dates.DateTime(2000) @test_throws MethodError Dates.Date(0):Dates.Year(10) -@test length(range(Dates.Date(2000), Dates.Day(1), 366)) == 366 -@test last(range(Dates.Date(2000), Dates.Day(1), 366)) == Dates.Date(2000, 12, 31) -@test last(range(Dates.Date(2001), Dates.Day(1), 365)) == Dates.Date(2001, 12, 31) -@test last(range(Dates.Date(2000), Dates.Day(1), 367)) == last(range(Dates.Date(2000), Dates.Month(12), 2)) == last(range(Dates.Date(2000), Dates.Year(1), 2)) -@test last(range(Dates.DateTime(2000), Dates.Day(366), 2)) == last(range(Dates.DateTime(2000), Dates.Hour(8784), 2)) +@test length(range(Dates.Date(2000), step=Dates.Day(1), length=366)) == 366 +@test last(range(Dates.Date(2000), step=Dates.Day(1), length=366)) == Dates.Date(2000, 12, 31) +@test last(range(Dates.Date(2001), step=Dates.Day(1), length=365)) == Dates.Date(2001, 12, 31) +@test last(range(Dates.Date(2000), step=Dates.Day(1), length=367)) == last(range(Dates.Date(2000), step=Dates.Month(12), length=2)) == last(range(Dates.Date(2000), step=Dates.Year(1), length=2)) +@test last(range(Dates.DateTime(2000), step=Dates.Day(366), length=2)) == last(range(Dates.DateTime(2000), step=Dates.Hour(8784), length=2)) # Issue 5 lastdaysofmonth = [Dates.Date(2014, i, Dates.daysinmonth(2014, i)) for i=1:12] diff --git a/stdlib/LinearAlgebra/src/dense.jl b/stdlib/LinearAlgebra/src/dense.jl index 2bc0f4d8a8fb8..9e58b1494fffd 100644 --- a/stdlib/LinearAlgebra/src/dense.jl +++ b/stdlib/LinearAlgebra/src/dense.jl @@ -249,7 +249,7 @@ function diagind(m::Integer, n::Integer, k::Integer=0) throw(ArgumentError(string("requested diagonal, $k, must be at least $(-m) and ", "at most $n in an $m-by-$n matrix"))) end - k <= 0 ? range(1-k, m+1, min(m+k, n)) : range(k*m+1, m+1, min(m, n-k)) + k <= 0 ? range(1-k, step=m+1, length=min(m+k, n)) : range(k*m+1, step=m+1, length=min(m, n-k)) end """ diff --git a/test/euler.jl b/test/euler.jl index 2475b42e2ee3c..9af79a44cc0d3 100644 --- a/test/euler.jl +++ b/test/euler.jl @@ -70,8 +70,8 @@ function euler11(grid,n) j = n:size(grid,2)-n+1, di = -1:1, dj = -1:1 di == dj == 0 && continue - i_idxs = di==0 ? fill(i,n) : range(i,di,n) - j_idxs = dj==0 ? fill(j,n) : range(j,dj,n) + i_idxs = di==0 ? fill(i,n) : range(i, step=di, length=n) + j_idxs = dj==0 ? fill(j,n) : range(j, step=dj, length=n) idx = tolinear[CartesianIndex.(i_idxs, j_idxs)] m = max(m,prod(grid[idx])) end diff --git a/test/ranges.jl b/test/ranges.jl index 554317142397c..454ed08f0ffb1 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1197,8 +1197,8 @@ isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl") using .Main.TestHelpers: Furlong @testset "dimensional correctness" begin @test length(Vector(Furlong(2):Furlong(10))) == 9 - @test length(range(Furlong(2), 9)) == 9 - @test Vector(Furlong(2):Furlong(1):Furlong(10)) == Vector(range(Furlong(2),Furlong(1),9)) == Furlong.(2:10) + @test length(range(Furlong(2), length=9)) == 9 + @test Vector(Furlong(2):Furlong(1):Furlong(10)) == Vector(range(Furlong(2), step=Furlong(1), length=9)) == Furlong.(2:10) @test Vector(Furlong(1.0):Furlong(0.5):Furlong(10.0)) == Vector(Furlong(1):Furlong(0.5):Furlong(10)) == Furlong.(1:0.5:10) end @@ -1228,3 +1228,11 @@ end @test map(Float16, x) === Float16(-5.0):Float16(1.0):Float16(5.0) @test map(BigFloat, x) === x end + +@testset "Bad range calls" begin + @test_throws ArgumentError range(1) + @test_throws ArgumentError range(nothing) + @test_throws ArgumentError range(1, step=4) + @test_throws ArgumentError range(nothing, length=2) + @test_throws ArgumentError range(1.0, step=0.25, stop=2.0, length=5) +end From 6c0e39d060b007bd83453c9ba1c02a316da9bf62 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Tue, 13 Feb 2018 11:12:31 -0800 Subject: [PATCH 2/4] Deprecate linspace in favor of range methods --- NEWS.md | 3 + base/deprecated.jl | 4 +- base/exports.jl | 1 - base/range.jl | 52 +++--- base/show.jl | 4 +- base/twiceprecision.jl | 14 +- doc/src/base/arrays.md | 1 - doc/src/manual/arrays.md | 2 +- doc/src/manual/noteworthy-differences.md | 2 +- doc/src/manual/parallel-computing.md | 2 +- stdlib/LinearAlgebra/test/diagonal.jl | 2 +- stdlib/LinearAlgebra/test/generic.jl | 6 +- stdlib/LinearAlgebra/test/lapack.jl | 2 +- stdlib/LinearAlgebra/test/qr.jl | 2 +- test/arrayops.jl | 2 +- test/hashing.jl | 4 +- test/ranges.jl | 217 +++++++++++------------ test/reduce.jl | 2 +- test/statistics.jl | 4 +- 19 files changed, 161 insertions(+), 165 deletions(-) diff --git a/NEWS.md b/NEWS.md index fcdbf197057ef..424ba3195cfcd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1037,6 +1037,9 @@ Deprecated or removed * The methods of `range` based on positional arguments have been deprecated in favor of keyword arguments ([#25896]). + * `linspace` has been deprecated in favor of `range` with `stop` and `length` keyword + arguments ([#25896]). + * `endof(a)` has been renamed to `lastindex(a)`, and the `end` keyword in indexing expressions now lowers to either `lastindex(a)` (in the case with only one index) or `lastindex(a, d)` (in cases where there is more than one index and `end` appears at dimension `d`) ([#23554], [#25763]). diff --git a/base/deprecated.jl b/base/deprecated.jl index e4ceab4561f34..b1856a6b8faaf 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -840,7 +840,7 @@ end @deprecate trues(A::AbstractArray) trues(size(A)) # issue #24794 -@deprecate linspace(start, stop) linspace(start, stop, 50) +@deprecate linspace(start, stop) range(start, stop=stop, length=50) @deprecate logspace(start, stop) logspace(start, stop, 50) # 24490 - warnings and messages @@ -1315,6 +1315,8 @@ export readandwrite # PR #25896 @deprecate range(start, length) range(start, length=length) @deprecate range(start, step, length) range(start, step=step, length=length) +@deprecate linspace(start, stop, length::Integer) range(start, stop=stop, length=length) +@deprecate linspace(start, stop, length::Real) range(start, stop=stop, length=Int(length)) @deprecate runtests(tests, ncores; kw...) runtests(tests; ncores = ncores, kw...) false @deprecate code_lowered(f, types, generated) code_lowered(f, types, generated = generated) diff --git a/base/exports.jl b/base/exports.jl index e3af2c480c7e3..82374676ce5ce 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -395,7 +395,6 @@ export issorted, last, linearindices, - linspace, logspace, mapslices, max, diff --git a/base/range.jl b/base/range.jl index d8fd686b8f7df..2a3b01993dd4b 100644 --- a/base/range.jl +++ b/base/range.jl @@ -60,7 +60,10 @@ end Given a starting value, construct a range either by length or from `start` to `stop`, optionally with a given step (defaults to 1). One of `length` or `step` is required. -If both are specified, they must agree. +If `length`, `stop`, and `step` are all specified, they must agree. + +If `length` and `stop` are provided and `step` is not, the step size will be computed +automatically such that there are `length` linearly spaced elements in the range. """ range(start; length::Union{Integer,Nothing}=nothing, stop=nothing, step=nothing) = _range(start, step, stop, length) @@ -232,7 +235,7 @@ StepRangeLen(ref::R, step::S, len::Integer, offset::Integer = 1) where {R,S} = StepRangeLen{T}(ref::R, step::S, len::Integer, offset::Integer = 1) where {T,R,S} = StepRangeLen{T,R,S}(ref, step, len, offset) -## linspace and logspace +## range with computed step, and logspace struct LinSpace{T} <: AbstractRange{T} start::T @@ -241,9 +244,9 @@ struct LinSpace{T} <: AbstractRange{T} lendiv::Int function LinSpace{T}(start,stop,len) where T - len >= 0 || throw(ArgumentError("linspace($start, $stop, $len): negative length")) + len >= 0 || throw(ArgumentError("range($start, stop=$stop, length=$len): negative length")) if len == 1 - start == stop || throw(ArgumentError("linspace($start, $stop, $len): endpoints differ")) + start == stop || throw(ArgumentError("range($start, stop=$stop, length=$len): endpoints differ")) return new(start, stop, 1, 1) end new(start,stop,len,max(len-1,1)) @@ -255,31 +258,22 @@ function LinSpace(start, stop, len::Integer) LinSpace{T}(start, stop, len) end -""" - linspace(start, stop, n) - -Construct a range of `n` linearly spaced elements from `start` to `stop`. - -```jldoctest -julia> linspace(1.3,2.9,9) -1.3:0.2:2.9 -``` -""" -linspace(start, stop, len::Real) = linspace(promote(start, stop)..., Int(len)) -linspace(start::T, stop::T, len::Real) where {T} = linspace(start, stop, Int(len)) - -linspace(start::Real, stop::Real, len::Integer) = linspace(promote(start, stop)..., len) -linspace(start::T, stop::T, len::Integer) where {T<:Integer} = linspace(Float64, start, stop, len, 1) -# for Float16, Float32, and Float64 see twiceprecision.jl -linspace(start::T, stop::T, len::Integer) where {T<:Real} = LinSpace{T}(start, stop, len) -linspace(start::T, stop::T, len::Integer) where {T} = LinSpace{T}(start, stop, len) +function _range(start::T, ::Nothing, stop::S, len::Integer) where {T,S} + a, b = promote(start, stop) + _range(a, nothing, b, len) +end +_range(start::T, ::Nothing, stop::T, len::Integer) where {T<:Real} = LinSpace{T}(start, stop, len) +_range(start::T, ::Nothing, stop::T, len::Integer) where {T} = LinSpace{T}(start, stop, len) +_range(start::T, ::Nothing, stop::T, len::Integer) where {T<:Integer} = + _linspace(Float64, start, stop, len, 1) +## for Float16, Float32, and Float64 see twiceprecision.jl function show(io::IO, r::LinSpace) - print(io, "linspace(") + print(io, "range(") show(io, first(r)) - print(io, ',') + print(io, ", stop=") show(io, last(r)) - print(io, ',') + print(io, ", length=") show(io, length(r)) print(io, ')') end @@ -364,7 +358,7 @@ julia> logspace(1.,10.,5,base=2) 1024.0 ``` """ -logspace(start::Real, stop::Real, n::Integer; base=10) = base.^linspace(start, stop, n) +logspace(start::Real, stop::Real, n::Integer; base=10) = base.^range(start, stop=stop, length=n) ## interface implementations @@ -390,7 +384,7 @@ julia> step(1:2:10) julia> step(2.5:0.3:10.9) 0.3 -julia> step(linspace(2.5,10.9,85)) +julia> step(range(2.5, stop=10.9, length=85)) 0.1 ``` """ @@ -976,8 +970,8 @@ function _define_range_op(@nospecialize f) len = r1.len (len == r2.len || throw(DimensionMismatch("argument dimensions must match"))) - linspace(convert(T, $f(first(r1), first(r2))), - convert(T, $f(last(r1), last(r2))), len) + LinSpace{T}(convert(T, $f(first(r1), first(r2))), + convert(T, $f(last(r1), last(r2))), len) end $f(r1::Union{StepRangeLen, OrdinalRange, LinSpace}, diff --git a/base/show.jl b/base/show.jl index 01d50820ddc35..a835e23916dac 100644 --- a/base/show.jl +++ b/base/show.jl @@ -5,8 +5,8 @@ show(io::IO, ::MIME"text/plain", r::AbstractRange) = show(io, r) # always use the compact form for printing ranges function show(io::IO, ::MIME"text/plain", r::LinSpace) - # show for linspace, e.g. - # linspace(1,3,7) + # show for LinSpace, e.g. + # range(1, stop=3, length=7) # 7-element LinSpace{Float64}: # 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0 print(io, summary(r)) diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 8abb28ce1cb53..17cd3eb90b239 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -569,8 +569,8 @@ end ## LinSpace -# For Float16, Float32, and Float64, linspace returns a StepRangeLen -function linspace(start::T, stop::T, len::Integer) where {T<:IEEEFloat} +# For Float16, Float32, and Float64, this returns a StepRangeLen +function _range(start::T, ::Nothing, stop::T, len::Integer) where {T<:IEEEFloat} len < 2 && return _linspace1(T, start, stop, len) if start == stop return steprangelen_hp(T, start, zero(T), 0, len, 1) @@ -585,7 +585,7 @@ function linspace(start::T, stop::T, len::Integer) where {T<:IEEEFloat} start_n = round(Int, den*start) stop_n = round(Int, den*stop) if T(start_n/den) == start && T(stop_n/den) == stop - return linspace(T, start_n, stop_n, len, den) + return _linspace(T, start_n, stop_n, len, den) end end end @@ -634,9 +634,9 @@ function _linspace(start::T, stop::T, len::Integer) where {T<:IEEEFloat} steprangelen_hp(T, (ref, ref_lo), (step_hi, step_lo), 0, Int(len), imin) end -# linspace for rational numbers, start = start_n/den, stop = stop_n/den +# range for rational numbers, start = start_n/den, stop = stop_n/den # Note this returns a StepRangeLen -function linspace(::Type{T}, start_n::Integer, stop_n::Integer, len::Integer, den::Integer) where T +function _linspace(::Type{T}, start_n::Integer, stop_n::Integer, len::Integer, den::Integer) where T len < 2 && return _linspace1(T, start_n/den, stop_n/den, len) start_n == stop_n && return steprangelen_hp(T, (start_n, den), (zero(start_n), den), 0, len) tmin = -start_n/(Float64(stop_n) - Float64(start_n)) @@ -651,9 +651,9 @@ end # For len < 2 function _linspace1(::Type{T}, start, stop, len::Integer) where T - len >= 0 || throw(ArgumentError("linspace($start, $stop, $len): negative length")) + len >= 0 || throw(ArgumentError("range($start, stop=$stop, length=$len): negative length")) if len <= 1 - len == 1 && (start == stop || throw(ArgumentError("linspace($start, $stop, $len): endpoints differ"))) + len == 1 && (start == stop || throw(ArgumentError("range($start, stop=$stop, length=$len): endpoints differ"))) # Ensure that first(r)==start and last(r)==stop even for len==0 # The output type must be consistent with steprangelen_hp if T<:Union{Float32,Float16} diff --git a/doc/src/base/arrays.md b/doc/src/base/arrays.md index 96ae6757593b1..399f5b6d4244e 100644 --- a/doc/src/base/arrays.md +++ b/doc/src/base/arrays.md @@ -32,7 +32,6 @@ Base.fill Base.fill! Base.similar(::AbstractArray) Base.similar(::Any, ::Tuple) -Base.linspace Base.logspace ``` diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 91f806fcd7653..f7432063cd774 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -63,7 +63,7 @@ omitted it will default to [`Float64`](@ref). | [`rand(T, dims...)`](@ref) | an `Array` with random, iid [^1] and uniformly distributed values in the half-open interval ``[0, 1)`` | | [`randn(T, dims...)`](@ref) | an `Array` with random, iid and standard normally distributed values | | [`Matrix{T}(I, m, n)`](@ref) | `m`-by-`n` identity matrix | -| [`linspace(start, stop, n)`](@ref) | range of `n` linearly spaced elements from `start` to `stop` | +| [`range(start, stop=stop, length=n)`](@ref) | range of `n` linearly spaced elements from `start` to `stop` | | [`fill!(A, x)`](@ref) | fill the array `A` with the value `x` | | [`fill(x, dims...)`](@ref) | an `Array` filled with the value `x` | diff --git a/doc/src/manual/noteworthy-differences.md b/doc/src/manual/noteworthy-differences.md index 12b3ce249925e..dd0a8c1d70e60 100644 --- a/doc/src/manual/noteworthy-differences.md +++ b/doc/src/manual/noteworthy-differences.md @@ -38,7 +38,7 @@ may trip up Julia users accustomed to MATLAB: use [`collect(a:b)`](@ref). Generally, there is no need to call `collect` though. An `AbstractRange` object will act like a normal array in most cases but is more efficient because it lazily computes its values. This pattern of creating specialized objects instead of full arrays is used frequently, and is - also seen in functions such as [`linspace`](@ref), or with iterators such as `enumerate`, and + also seen in functions such as [`range`](@ref), or with iterators such as `enumerate`, and `zip`. The special objects can mostly be used as if they were normal arrays. * Functions in Julia return values from their last expression or the `return` keyword instead of listing the names of variables to return in the function definition (see [The return Keyword](@ref) diff --git a/doc/src/manual/parallel-computing.md b/doc/src/manual/parallel-computing.md index 514e36d084b13..195d039f152f2 100644 --- a/doc/src/manual/parallel-computing.md +++ b/doc/src/manual/parallel-computing.md @@ -956,7 +956,7 @@ julia> @everywhere function myrange(q::SharedArray) return 1:0, 1:0 end nchunks = length(procs(q)) - splits = [round(Int, s) for s in linspace(0,size(q,2),nchunks+1)] + splits = [round(Int, s) for s in range(0, stop=size(q,2), length=nchunks+1)] 1:size(q,1), splits[idx]+1:splits[idx+1] end ``` diff --git a/stdlib/LinearAlgebra/test/diagonal.jl b/stdlib/LinearAlgebra/test/diagonal.jl index 746596183ae89..2d3cb70a42f98 100644 --- a/stdlib/LinearAlgebra/test/diagonal.jl +++ b/stdlib/LinearAlgebra/test/diagonal.jl @@ -325,7 +325,7 @@ end end # allow construct from range -@test all(Diagonal(linspace(1,3,3)) .== Diagonal([1.0,2.0,3.0])) +@test all(Diagonal(range(1, stop=3, length=3)) .== Diagonal([1.0,2.0,3.0])) # Issue 12803 for t in (Float32, Float64, Int, Complex{Float64}, Rational{Int}) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 081f563e3a648..a33f6a9a643c4 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -120,7 +120,7 @@ end @test [linreg(view(x,1:6),view(y,1:6))...] ≈ [3.8366666666666642,1.3271428571428574] # check (LinSpace, UnitRange) - x = linspace(1.0, 12.0, 100) + x = range(1.0, stop=12.0, length=100) y = -100:-1 @test [linreg(x, y)...] ≈ [-109.0, 9.0] @@ -130,8 +130,8 @@ end @test [linreg(x, y)...] ≈ [13.0, -1.0] # check (LinSpace, LinSpace) - x = linspace(-5, 10, 100) - y = linspace(50, 200, 100) + x = range(-5, stop=10, length=100) + y = range(50, stop=200, length=100) @test [linreg(x, y)...] ≈ [100.0, 10.0] # check (Array, Array) diff --git a/stdlib/LinearAlgebra/test/lapack.jl b/stdlib/LinearAlgebra/test/lapack.jl index d3fcd87319b4a..de7074a9d5370 100644 --- a/stdlib/LinearAlgebra/test/lapack.jl +++ b/stdlib/LinearAlgebra/test/lapack.jl @@ -196,7 +196,7 @@ end @testset "gebal/gebak" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - A = rand(elty,10,10) * Diagonal(exp10.(linspace(-10,10,10))) + A = rand(elty,10,10) * Diagonal(exp10.(range(-10, stop=10, length=10))) B = copy(A) ilo, ihi, scale = LAPACK.gebal!('S',B) Bvs = eigvecs(B) diff --git a/stdlib/LinearAlgebra/test/qr.jl b/stdlib/LinearAlgebra/test/qr.jl index ed10e0e3e6678..916bd8326bc07 100644 --- a/stdlib/LinearAlgebra/test/qr.jl +++ b/stdlib/LinearAlgebra/test/qr.jl @@ -211,7 +211,7 @@ end @testset "Issue 24107" begin A = rand(200,2) - @test A \ linspace(0,1,200) == A \ Vector(linspace(0,1,200)) + @test A \ range(0, stop=1, length=200) == A \ Vector(range(0, stop=1, length=200)) end @testset "Issue 24589. Promotion of rational matrices" begin diff --git a/test/arrayops.jl b/test/arrayops.jl index 59addb462c6a8..9b75d2ffad4de 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -147,7 +147,7 @@ end end @testset "reshape with colon" begin # Reshape with an omitted dimension - let A = linspace(1, 60, 60) + let A = range(1, stop=60, length=60) @test size(reshape(A, :)) == (60,) @test size(reshape(A, :, 1)) == (60, 1) @test size(reshape(A, (:, 2))) == (30, 2) diff --git a/test/hashing.jl b/test/hashing.jl index f316e495d852b..90c9eafe01e4f 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -89,7 +89,7 @@ vals = Any[ sparse(fill(1., 2, 2)), fill(1., 2, 2), sparse([0 0; 1 0]), [0 0; 1 0], [-0. 0; -0. 0.], SparseMatrixCSC(2, 2, [1, 3, 3], [1, 2], [-0., -0.]), # issue #16364 - 1:4, 1:1:4, 1:-1:0, 1.0:4.0, 1.0:1.0:4.0, linspace(1, 4, 4), + 1:4, 1:1:4, 1:-1:0, 1.0:4.0, 1.0:1.0:4.0, range(1, stop=4, length=4), 'a':'e', ['a', 'b', 'c', 'd', 'e'], # check that hash is still consistent with heterogeneous arrays for which - is defined # for some pairs and not others @@ -165,7 +165,7 @@ vals = Any[ 0.0:0.1:0.3, 0.3:-0.1:0.0, 0:-1:1, 0.0:-1.0:1.0, 0.0:1.1:10.0, -4:10, 'a':'e', 'b':'a', - linspace(1, 1, 1), linspace(0.3, 1.0, 3), linspace(1, 1.1, 20) + range(1, stop=1, length=1), range(0.3, stop=1.0, length=3), range(1, stop=1.1, length=20) ] for a in vals diff --git a/test/ranges.jl b/test/ranges.jl index 454ed08f0ffb1..ef2d995d12eee 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -198,16 +198,15 @@ end end @testset "indexing" begin - L32 = @inferred(linspace(Int32(1), Int32(4), 4)) - L64 = @inferred(linspace(Int64(1), Int64(4), 4)) + L32 = @inferred(range(Int32(1), stop=Int32(4), length=4)) + L64 = @inferred(range(Int64(1), stop=Int64(4), length=4)) @test @inferred(L32[1]) === 1.0 && @inferred(L64[1]) === 1.0 @test L32[2] == 2 && L64[2] == 2 @test L32[3] == 3 && L64[3] == 3 @test L32[4] == 4 && L64[4] == 4 - @test @inferred(linspace(1.0, 2.0, 2.0f0)) === linspace(1.0, 2.0, 2) - @test @inferred(linspace(1.0, 2.0, 2))[1] === 1.0 - @test @inferred(linspace(1.0f0, 2.0f0, 2))[1] === 1.0f0 - @test @inferred(linspace(Float16(1.0), Float16(2.0), 2))[1] === Float16(1.0) + @test @inferred(range(1.0, stop=2.0, length=2))[1] === 1.0 + @test @inferred(range(1.0f0, stop=2.0f0, length=2))[1] === 1.0f0 + @test @inferred(range(Float16(1.0), stop=Float16(2.0), length=2))[1] === Float16(1.0) let r = 5:-1:1 @test r[1]==5 @@ -518,7 +517,7 @@ end @test hash(r) == hash(ra) if len > 0 - l = linspace(start/10, stop/10, len) + l = range(start/10, stop=stop/10, length=len) la = Vector(l) @test a == l @@ -534,10 +533,10 @@ end end end - @test 1.0:1/49:27.0 == linspace(1.0,27.0,1275) == [49:1323;]./49 - @test isequal(1.0:1/49:27.0, linspace(1.0,27.0,1275)) + @test 1.0:1/49:27.0 == range(1.0, stop=27.0, length=1275) == [49:1323;]./49 + @test isequal(1.0:1/49:27.0, range(1.0, stop=27.0, length=1275)) @test isequal(1.0:1/49:27.0, Vector(49:1323)./49) - @test hash(1.0:1/49:27.0) == hash(linspace(1.0,27.0,1275)) == hash(Vector(49:1323)./49) + @test hash(1.0:1/49:27.0) == hash(range(1.0, stop=27.0, length=1275)) == hash(Vector(49:1323)./49) @test [prevfloat(0.1):0.1:0.3;] == [prevfloat(0.1), 0.2, 0.3] @test [nextfloat(0.1):0.1:0.3;] == [nextfloat(0.1), 0.2] @@ -565,7 +564,7 @@ end vals = T[a:s:(a + (n - 1) * s); ] ./ denom r = strt:Δ:stop @test [r;] == vals - @test [linspace(strt, stop, length(r));] == vals + @test [range(strt, stop=stop, length=length(r));] == vals n = length(r) @test [r[1:n];] == [r;] @test [r[2:n];] == [r;][2:end] @@ -576,10 +575,10 @@ end end @testset "issue #20373 (unliftable ranges with exact end points)" begin - @test [3*0.05:0.05:0.2;] == [linspace(3*0.05,0.2,2);] == [3*0.05,0.2] - @test [0.2:-0.05:3*0.05;] == [linspace(0.2,3*0.05,2);] == [0.2,3*0.05] - @test [-3*0.05:-0.05:-0.2;] == [linspace(-3*0.05,-0.2,2);] == [-3*0.05,-0.2] - @test [-0.2:0.05:-3*0.05;] == [linspace(-0.2,-3*0.05,2);] == [-0.2,-3*0.05] + @test [3*0.05:0.05:0.2;] == [range(3*0.05, stop=0.2, length=2);] == [3*0.05,0.2] + @test [0.2:-0.05:3*0.05;] == [range(0.2, stop=3*0.05, length=2);] == [0.2,3*0.05] + @test [-3*0.05:-0.05:-0.2;] == [range(-3*0.05, stop=-0.2, length=2);] == [-3*0.05,-0.2] + @test [-0.2:0.05:-3*0.05;] == [range(-0.2, stop=-3*0.05, length=2);] == [-0.2,-3*0.05] end function range_fuzztests(::Type{T}, niter, nrange) where {T} @@ -599,7 +598,7 @@ function range_fuzztests(::Type{T}, niter, nrange) where {T} @test strt == first(r) @test Δ == step(r) @test_skip stop == last(r) - l = linspace(strt,stop,n) + l = range(strt, stop=stop, length=n) @test n == length(l) @test strt == first(l) @test stop == last(l) @@ -610,56 +609,56 @@ end end @testset "Inexact errors on 32 bit architectures. #22613" begin - @test first(linspace(log(0.2), log(10.0), 10)) == log(0.2) - @test last(linspace(log(0.2), log(10.0), 10)) == log(10.0) + @test first(range(log(0.2), stop=log(10.0), length=10)) == log(0.2) + @test last(range(log(0.2), stop=log(10.0), length=10)) == log(10.0) @test length(Base.floatrange(-3e9, 1.0, 1, 1.0)) == 1 end -@testset "linspace & ranges with very small endpoints for type $T" for T = (Float32, Float64) +@testset "ranges with very small endpoints for type $T" for T = (Float32, Float64) z = zero(T) u = eps(z) - @test first(linspace(u,u,0)) == u - @test last(linspace(u,u,0)) == u - @test first(linspace(-u,u,0)) == -u - @test last(linspace(-u,u,0)) == u - @test [linspace(-u,u,0);] == [] - @test [linspace(-u,-u,1);] == [-u] - @test [linspace(-u,u,2);] == [-u,u] - @test [linspace(-u,u,3);] == [-u,0,u] - @test first(linspace(-u,-u,0)) == -u - @test last(linspace(-u,-u,0)) == -u - @test first(linspace(u,-u,0)) == u - @test last(linspace(u,-u,0)) == -u - @test [linspace(u,-u,0);] == [] - @test [linspace(u,u,1);] == [u] - @test [linspace(u,-u,2);] == [u,-u] - @test [linspace(u,-u,3);] == [u,0,-u] - v = linspace(-u,u,12) + @test first(range(u, stop=u, length=0)) == u + @test last(range(u, stop=u, length=0)) == u + @test first(range(-u, stop=u, length=0)) == -u + @test last(range(-u, stop=u, length=0)) == u + @test [range(-u, stop=u, length=0);] == [] + @test [range(-u, stop=-u, length=1);] == [-u] + @test [range(-u, stop=u, length=2);] == [-u,u] + @test [range(-u, stop=u, length=3);] == [-u,0,u] + @test first(range(-u, stop=-u, length=0)) == -u + @test last(range(-u, stop=-u, length=0)) == -u + @test first(range(u, stop=-u, length=0)) == u + @test last(range(u, stop=-u, length=0)) == -u + @test [range(u, stop=-u, length=0);] == [] + @test [range(u, stop=u, length=1);] == [u] + @test [range(u, stop=-u, length=2);] == [u,-u] + @test [range(u, stop=-u, length=3);] == [u,0,-u] + v = range(-u, stop=u, length=12) @test length(v) == 12 - @test [-3u:u:3u;] == [linspace(-3u,3u,7);] == [-3:3;].*u - @test [3u:-u:-3u;] == [linspace(3u,-3u,7);] == [3:-1:-3;].*u + @test [-3u:u:3u;] == [range(-3u, stop=3u, length=7);] == [-3:3;].*u + @test [3u:-u:-3u;] == [range(3u, stop=-3u, length=7);] == [3:-1:-3;].*u end -@testset "linspace with very large endpoints for type $T" for T = (Float32, Float64) +@testset "range with very large endpoints for type $T" for T = (Float32, Float64) largeint = Int(min(maxintfloat(T), typemax(Int))) a = realmax() for i = 1:5 - @test [linspace(a,a,1);] == [a] - @test [linspace(-a,-a,1);] == [-a] + @test [range(a, stop=a, length=1);] == [a] + @test [range(-a, stop=-a, length=1);] == [-a] b = realmax() for j = 1:5 - @test [linspace(-a,b,0);] == [] - @test [linspace(-a,b,2);] == [-a,b] - @test [linspace(-a,b,3);] == [-a,(b-a)/2,b] - @test [linspace(a,-b,0);] == [] - @test [linspace(a,-b,2);] == [a,-b] - @test [linspace(a,-b,3);] == [a,(a-b)/2,-b] + @test [range(-a, stop=b, length=0);] == [] + @test [range(-a, stop=b, length=2);] == [-a,b] + @test [range(-a, stop=b, length=3);] == [-a,(b-a)/2,b] + @test [range(a, stop=-b, length=0);] == [] + @test [range(a, stop=-b, length=2);] == [a,-b] + @test [range(a, stop=-b, length=3);] == [a,(a-b)/2,-b] for c = largeint-3:largeint - s = linspace(-a,b,c) + s = range(-a, stop=b, length=c) @test first(s) == -a @test last(s) == b @test length(s) == c - s = linspace(a,-b,c) + s = range(a, stop=-b, length=c) @test first(s) == a @test last(s) == -b @test length(s) == c @@ -675,9 +674,9 @@ let r = LinSpace(1,4,4) @test isa(r[1:4], LinSpace) end -@testset "linspace with 1 or 0 elements (whose step length is NaN)" begin - @test issorted(linspace(1,1,0)) - @test issorted(linspace(1,1,1)) +@testset "range with 1 or 0 elements (whose step length is NaN)" begin + @test issorted(range(1, stop=1, length=0)) + @test issorted(range(1, stop=1, length=1)) end # near-equal ranges @test 0.0:0.1:1.0 != 0.0f0:0.1f0:1.0f0 @@ -688,7 +687,7 @@ end map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0, 0.0:0.1:1.0, map(Float32,0.0:0.1:1.0), 1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994, - linspace(0, 1, 20), map(Float32, linspace(0, 1, 20))] + range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20))] for r in Rs local r ar = Vector(r) @@ -761,15 +760,15 @@ end @test_throws BoundsError r[1:10000] end - let r = linspace(1/3, 5/7, 6) + let r = range(1/3, stop=5/7, length=6) @test length(r) == 6 @test r[1] == 1/3 @test abs(r[end] - 5/7) <= eps(5/7) end - let r = linspace(0.25, 0.25, 1) + let r = range(0.25, stop=0.25, length=1) @test length(r) == 1 - @test_throws ArgumentError linspace(0.25,0.5,1) + @test_throws ArgumentError range(0.25, stop=0.5, length=1) end end @@ -887,9 +886,9 @@ end end @testset "Issue #11245" begin io = IOBuffer() - show(io, linspace(1, 2, 3)) + show(io, range(1, stop=2, length=3)) str = String(take!(io)) -# @test str == "linspace(1.0,2.0,3)" +# @test str == "range(1.0, stop=2.0, length=3)" @test str == "1.0:0.5:2.0" end @@ -905,20 +904,20 @@ end end @testset "stringmime/repr" begin - # stringmime/show should display the range or linspace nicely + # stringmime/show should display the range nicely # to test print_range in range.jl replstrmime(x) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80)), MIME("text/plain"), x), x) @test replstrmime(1:4) == "1:4" @test stringmime("text/plain", 1:4) == "1:4" - @test stringmime("text/plain", linspace(1,5,7)) == "1.0:0.6666666666666666:5.0" + @test stringmime("text/plain", range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0" @test stringmime("text/plain", LinSpace{Float64}(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" - @test repr(linspace(1,5,7)) == "1.0:0.6666666666666666:5.0" - @test repr(LinSpace{Float64}(1,5,7)) == "linspace(1.0,5.0,7)" + @test repr(range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0" + @test repr(LinSpace{Float64}(1,5,7)) == "range(1.0, stop=5.0, length=7)" @test replstrmime(0:100.) == "0.0:1.0:100.0" # next is to test a very large range, which should be fast because print_range # only examines spacing of the left and right edges of the range, sufficient # to cover the designated screen size. - @test replstrmime(linspace(0,100, 10000)) == "0.0:0.010001000100010001:100.0" + @test replstrmime(range(0, stop=100, length=10000)) == "0.0:0.010001000100010001:100.0" @test replstrmime(LinSpace{Float64}(0,100, 10000)) == "10000-element LinSpace{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0" @test sprint(show, UnitRange(1, 2)) == "1:2" @@ -926,13 +925,13 @@ end end @testset "Issue 11049 and related" begin - @test promote(linspace(0f0, 1f0, 3), linspace(0., 5., 2)) === - (linspace(0., 1., 3), linspace(0., 5., 2)) - @test convert(LinSpace{Float64}, linspace(0., 1., 3)) === LinSpace(0., 1., 3) - @test convert(LinSpace{Float64}, linspace(0f0, 1f0, 3)) === LinSpace(0., 1., 3) + @test promote(range(0f0, stop=1f0, length=3), range(0., stop=5., length=2)) === + (range(0., stop=1., length=3), range(0., stop=5., length=2)) + @test convert(LinSpace{Float64}, range(0., stop=1., length=3)) === LinSpace(0., 1., 3) + @test convert(LinSpace{Float64}, range(0f0, stop=1f0, length=3)) === LinSpace(0., 1., 3) - @test promote(linspace(0., 1., 3), 0:5) === (linspace(0., 1., 3), - linspace(0., 5., 6)) + @test promote(range(0., stop=1., length=3), 0:5) === (range(0., stop=1., length=3), + range(0., stop=5., length=6)) @test convert(LinSpace{Float64}, 0:5) === LinSpace(0., 5., 6) @test convert(LinSpace{Float64}, 0:1:5) === LinSpace(0., 5., 6) @test convert(LinSpace, 0:5) === LinSpace{Int}(0, 5, 6) @@ -942,13 +941,13 @@ end @test typeof(r[s]) == typeof(r) @test [r;][s] == [r[s];] end - test_range_index(linspace(0.1, 0.3, 3), 1:2) - test_range_index(linspace(0.1, 0.3, 3), 1:0) - test_range_index(linspace(1.0, 1.0, 1), 1:1) - test_range_index(linspace(1.0, 1.0, 1), 1:0) - test_range_index(linspace(1.0, 2.0, 0), 1:0) + test_range_index(range(0.1, stop=0.3, length=3), 1:2) + test_range_index(range(0.1, stop=0.3, length=3), 1:0) + test_range_index(range(1.0, stop=1.0, length=1), 1:1) + test_range_index(range(1.0, stop=1.0, length=1), 1:0) + test_range_index(range(1.0, stop=2.0, length=0), 1:0) - function test_linspace_identity(r::AbstractRange{T}, mr) where T + function test_range_identity(r::AbstractRange{T}, mr) where T @test -r == mr @test -Vector(r) == Vector(mr) @test isa(-r, typeof(r)) @@ -974,22 +973,22 @@ end Vector(r / T(0.5)) == -Vector(mr * T(2.0))) end - test_linspace_identity(linspace(1.0, 27.0, 10), linspace(-1.0, -27.0, 10)) - test_linspace_identity(linspace(1f0, 27f0, 10), linspace(-1f0, -27f0, 10)) + test_range_identity(range(1.0, stop=27.0, length=10), range(-1.0, stop=-27.0, length=10)) + test_range_identity(range(1f0, stop=27f0, length=10), range(-1f0, stop=-27f0, length=10)) - test_linspace_identity(linspace(1.0, 27.0, 0), linspace(-1.0, -27.0, 0)) - test_linspace_identity(linspace(1f0, 27f0, 0), linspace(-1f0, -27f0, 0)) + test_range_identity(range(1.0, stop=27.0, length=0), range(-1.0, stop=-27.0, length=0)) + test_range_identity(range(1f0, stop=27f0, length=0), range(-1f0, stop=-27f0, length=0)) - test_linspace_identity(linspace(1.0, 1.0, 1), linspace(-1.0, -1.0, 1)) - test_linspace_identity(linspace(1f0, 1f0, 1), linspace(-1f0, -1f0, 1)) + test_range_identity(range(1.0, stop=1.0, length=1), range(-1.0, stop=-1.0, length=1)) + test_range_identity(range(1f0, stop=1f0, length=1), range(-1f0, stop=-1f0, length=1)) - @test reverse(linspace(1.0, 27.0, 1275)) == linspace(27.0, 1.0, 1275) - @test [reverse(linspace(1.0, 27.0, 1275));] == - reverse([linspace(1.0, 27.0, 1275);]) + @test reverse(range(1.0, stop=27.0, length=1275)) == range(27.0, stop=1.0, length=1275) + @test [reverse(range(1.0, stop=27.0, length=1275));] == + reverse([range(1.0, stop=27.0, length=1275);]) end @testset "PR 12200 and related" begin for _r in (1:2:100, 1:100, 1f0:2f0:100f0, 1.0:2.0:100.0, - linspace(1, 100, 10), linspace(1f0, 100f0, 10)) + range(1, stop=100, length=10), range(1f0, stop=100f0, length=10)) float_r = float(_r) big_r = big.(_r) @test typeof(big_r).name === typeof(_r).name @@ -1003,10 +1002,10 @@ end end end - @test_throws DimensionMismatch linspace(1.,5.,5) + linspace(1.,5.,6) - @test_throws DimensionMismatch linspace(1.,5.,5) - linspace(1.,5.,6) - @test_throws DimensionMismatch linspace(1.,5.,5) .* linspace(1.,5.,6) - @test_throws DimensionMismatch linspace(1.,5.,5) ./ linspace(1.,5.,6) + @test_throws DimensionMismatch range(1., stop=5., length=5) + range(1., stop=5., length=6) + @test_throws DimensionMismatch range(1., stop=5., length=5) - range(1., stop=5., length=6) + @test_throws DimensionMismatch range(1., stop=5., length=5) .* range(1., stop=5., length=6) + @test_throws DimensionMismatch range(1., stop=5., length=5) ./ range(1., stop=5., length=6) @test_throws DimensionMismatch (1:5) + (1:6) @test_throws DimensionMismatch (1:5) - (1:6) @@ -1032,14 +1031,14 @@ end test_range_sum_diff(1:5, 0:2:8, 1:3:13, 1:-1:-3) test_range_sum_diff(1.:5., 0.:2.:8., 1.:3.:13., 1.:-1.:-3.) - test_range_sum_diff(linspace(1.,5.,5), linspace(0.,-4.,5), - linspace(1.,1.,5), linspace(1.,9.,5)) + test_range_sum_diff(range(1., stop=5., length=5), range(0., stop=-4., length=5), + range(1., stop=1., length=5), range(1., stop=9., length=5)) test_range_sum_diff(1:5, 0.:2.:8., 1.:3.:13., 1.:-1.:-3.) - test_range_sum_diff(1:5, linspace(0, 8, 5), - linspace(1, 13, 5), linspace(1, -3, 5)) - test_range_sum_diff(1.:5., linspace(0, 8, 5), - linspace(1, 13, 5), linspace(1, -3, 5)) + test_range_sum_diff(1:5, range(0, stop=8, length=5), + range(1, stop=13, length=5), range(1, stop=-3, length=5)) + test_range_sum_diff(1.:5., range(0, stop=8, length=5), + range(1, stop=13, length=5), range(1, stop=-3, length=5)) end # Issue #12388 let r = 0x02:0x05 @@ -1123,8 +1122,8 @@ end end end -@testset "linspace of other types" begin - let r = linspace(0, 3//10, 4) +@testset "range of other types" begin + let r = range(0, stop=3//10, length=4) @test eltype(r) == Rational{Int} @test r[2] === 1//10 end @@ -1133,14 +1132,14 @@ end b = nextfloat(1.0), ba = BigFloat(a), bb = BigFloat(b), - r = linspace(ba, bb, 3) + r = range(ba, stop=bb, length=3) @test eltype(r) == BigFloat @test r[1] == a && r[3] == b @test r[2] == (ba+bb)/2 end let (a, b) = (rand(10), rand(10)), - r = linspace(a, b, 5) + r = range(a, stop=b, length=5) @test r[1] == a && r[5] == b for i = 2:4 x = ((5 - i) // 4) * a + ((i - 1) // 4) * b @@ -1149,7 +1148,7 @@ end end end @testset "issue #23178" begin - r = linspace(Float16(0.1094), Float16(0.9697), 300) + r = range(Float16(0.1094), stop=Float16(0.9697), length=300) @test r[1] == Float16(0.1094) @test r[end] == Float16(0.9697) end @@ -1160,25 +1159,25 @@ let r = @inferred(colon(big(1.0),big(2.0),big(5.0))) end @testset "issue #14420" begin - for r in (linspace(0.10000000000000045, 1, 50), 0.10000000000000045:(1-0.10000000000000045)/49:1) + for r in (range(0.10000000000000045, stop=1, length=50), 0.10000000000000045:(1-0.10000000000000045)/49:1) local r @test r[1] === 0.10000000000000045 @test r[end] === 1.0 end end @testset "issue #20381" begin - r = linspace(-big(1.0),big(1.0),4) + r = range(-big(1.0), stop=big(1.0), length=4) @test isa(@inferred(r[2]), BigFloat) @test r[2] ≈ big(-1.0)/3 end @testset "issue #20520" begin - r = linspace(1.3173739f0, 1.3173739f0, 3) + r = range(1.3173739f0, stop=1.3173739f0, length=3) @test length(r) == 3 @test first(r) === 1.3173739f0 @test last(r) === 1.3173739f0 @test r[2] === 1.3173739f0 - r = linspace(1.0, 3+im, 4) + r = range(1.0, stop=3+im, length=4) @test r[1] === 1.0+0.0im @test r[2] ≈ (5/3)+(1/3)im @test r[3] ≈ (7/3)+(2/3)im @@ -1204,7 +1203,7 @@ using .Main.TestHelpers: Furlong end @testset "issue #22270" begin - linsp = linspace(1.0, 2.0, 10) + linsp = range(1.0, stop=2.0, length=10) @test typeof(linsp.ref) == Base.TwicePrecision{Float64} @test Float32(linsp.ref) === convert(Float32, linsp.ref) @test Float32(linsp.ref) ≈ linsp.ref.hi + linsp.ref.lo @@ -1213,11 +1212,11 @@ end @testset "logspace" begin n = 10; a = 2; b = 4 # test default values; base = 10 - @test logspace(a, b, 50) == 10 .^ linspace(a, b, 50) - @test logspace(a, b, n) == 10 .^ linspace(a, b, n) + @test logspace(a, b, 50) == 10 .^ range(a, stop=b, length=50) + @test logspace(a, b, n) == 10 .^ range(a, stop=b, length=n) for base in (10, 2, ℯ) - @test logspace(a, b, 50, base=base) == base.^linspace(a, b, 50) - @test logspace(a, b, n, base=base) == base.^linspace(a, b, n) + @test logspace(a, b, 50, base=base) == base.^range(a, stop=b, length=50) + @test logspace(a, b, n, base=base) == base.^range(a, stop=b, length=n) end end diff --git a/test/reduce.jl b/test/reduce.jl index 66ac705fab643..e5071f2745d1d 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -45,7 +45,7 @@ using Random @test mapreduce(-, +, [-10]) == 10 @test mapreduce(abs2, +, [-9, -3]) == 81 + 9 @test mapreduce(-, +, [-9, -3, -4, 8, -2]) == (9 + 3 + 4 - 8 + 2) -@test mapreduce(-, +, Vector(linspace(1.0, 10000.0, 10000))) == -50005000.0 +@test mapreduce(-, +, Vector(range(1.0, stop=10000.0, length=10000))) == -50005000.0 # empty mr @test mapreduce(abs2, +, Float64[]) === 0.0 @test mapreduce(abs2, max, Float64[]) === 0.0 diff --git a/test/statistics.jl b/test/statistics.jl index 3e2b2ee9715e2..eb65090780627 100644 --- a/test/statistics.jl +++ b/test/statistics.jl @@ -344,7 +344,7 @@ end @test cor(repeat(1:17, 1, 17))[2] <= 1.0 @test cor(1:17, 1:17) <= 1.0 @test cor(1:17, 18:34) <= 1.0 - let tmp = linspace(1, 85, 100) + let tmp = range(1, stop=85, length=100) tmp2 = Vector(tmp) @test cor(tmp, tmp) <= 1.0 @test cor(tmp, tmp2) <= 1.0 @@ -369,7 +369,7 @@ end # StatsBase issue 164 let y = [0.40003674665581906, 0.4085630862624367, 0.41662034698690303, 0.41662034698690303, 0.42189053966652057, 0.42189053966652057, 0.42553514344518345, 0.43985732442991354] - @test issorted(quantile(y, linspace(0.01, 0.99, 17))) + @test issorted(quantile(y, range(0.01, stop=0.99, length=17))) end @testset "variance of complex arrays (#13309)" begin From 19c8b1489c2600a444f527a88dd62c64eb220839 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Tue, 13 Feb 2018 15:06:50 -0800 Subject: [PATCH 3/4] Rename LinSpace to LinRange This name more accurately reflects what's being constructed. It's likely that the name `LinSpace` was chosen for Julia since it matches what's used by Matlab. NumPy uses this name as well, but they likely also chose it for Matlab familiarity. In Matlab, the name `linspace` is short for "linearly spaced," and it returns a vector of linearly spaced elements. In Julia, we're often more careful with our terminology: in this case, the returned object is an `AbstractRange`, not some kind of vector space or any other kind of space. Thus calling it `LinRange` is more indicative of the functionality. --- NEWS.md | 2 + base/abstractarray.jl | 4 +- base/deprecated.jl | 1 + base/exports.jl | 2 +- base/float.jl | 8 +-- base/range.jl | 92 ++++++++++++++-------------- base/show.jl | 6 +- base/twiceprecision.jl | 2 +- stdlib/LinearAlgebra/test/generic.jl | 4 +- test/ranges.jl | 46 +++++++------- test/specificity.jl | 2 +- 11 files changed, 86 insertions(+), 83 deletions(-) diff --git a/NEWS.md b/NEWS.md index 424ba3195cfcd..2c7596d62ff6e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1040,6 +1040,8 @@ Deprecated or removed * `linspace` has been deprecated in favor of `range` with `stop` and `length` keyword arguments ([#25896]). + * `LinSpace` has been renamed to `LinRange` ([#25896]). + * `endof(a)` has been renamed to `lastindex(a)`, and the `end` keyword in indexing expressions now lowers to either `lastindex(a)` (in the case with only one index) or `lastindex(a, d)` (in cases where there is more than one index and `end` appears at dimension `d`) ([#23554], [#25763]). diff --git a/base/abstractarray.jl b/base/abstractarray.jl index d8df97bb1f879..1e520009dd8b5 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -883,8 +883,8 @@ of_indices(x, y) = similar(dims->y, oftype(axes(x), axes(y))) map(::Type{T}, r::StepRange) where {T<:Real} = T(r.start):T(r.step):T(last(r)) map(::Type{T}, r::UnitRange) where {T<:Real} = T(r.start):T(last(r)) map(::Type{T}, r::StepRangeLen) where {T<:AbstractFloat} = convert(StepRangeLen{T}, r) -function map(::Type{T}, r::LinSpace) where T<:AbstractFloat - LinSpace(T(r.start), T(r.stop), length(r)) +function map(::Type{T}, r::LinRange) where T<:AbstractFloat + LinRange(T(r.start), T(r.stop), length(r)) end ## unsafe/pointer conversions ## diff --git a/base/deprecated.jl b/base/deprecated.jl index b1856a6b8faaf..3fa7c9e97ef16 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1317,6 +1317,7 @@ export readandwrite @deprecate range(start, step, length) range(start, step=step, length=length) @deprecate linspace(start, stop, length::Integer) range(start, stop=stop, length=length) @deprecate linspace(start, stop, length::Real) range(start, stop=stop, length=Int(length)) +@deprecate_binding LinSpace LinRange @deprecate runtests(tests, ncores; kw...) runtests(tests; ncores = ncores, kw...) false @deprecate code_lowered(f, types, generated) code_lowered(f, types, generated = generated) diff --git a/base/exports.jl b/base/exports.jl index 82374676ce5ce..6c51c57e48d35 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -52,7 +52,7 @@ export BitSet, IOBuffer, IOStream, - LinSpace, + LinRange, Irrational, Matrix, MergeSort, diff --git a/base/float.jl b/base/float.jl index 9ae56d8119522..62dc732d107d5 100644 --- a/base/float.jl +++ b/base/float.jl @@ -874,8 +874,8 @@ float(r::StepRange) = float(r.start):float(r.step):float(last(r)) float(r::UnitRange) = float(r.start):float(last(r)) float(r::StepRangeLen{T}) where {T} = StepRangeLen{typeof(float(T(r.ref)))}(float(r.ref), float(r.step), length(r), r.offset) -function float(r::LinSpace) - LinSpace(float(r.start), float(r.stop), length(r)) +function float(r::LinRange) + LinRange(float(r.start), float(r.stop), length(r)) end # big, broadcast over arrays @@ -884,6 +884,6 @@ function big end # no prior definitions of big in sysimg.jl, necessitating this broadcast(::typeof(big), r::UnitRange) = big(r.start):big(last(r)) broadcast(::typeof(big), r::StepRange) = big(r.start):big(r.step):big(last(r)) broadcast(::typeof(big), r::StepRangeLen) = StepRangeLen(big(r.ref), big(r.step), length(r), r.offset) -function broadcast(::typeof(big), r::LinSpace) - LinSpace(big(r.start), big(r.stop), length(r)) +function broadcast(::typeof(big), r::LinRange) + LinRange(big(r.start), big(r.stop), length(r)) end diff --git a/base/range.jl b/base/range.jl index 2a3b01993dd4b..1ee8ea84a1eeb 100644 --- a/base/range.jl +++ b/base/range.jl @@ -237,13 +237,13 @@ StepRangeLen{T}(ref::R, step::S, len::Integer, offset::Integer = 1) where {T,R,S ## range with computed step, and logspace -struct LinSpace{T} <: AbstractRange{T} +struct LinRange{T} <: AbstractRange{T} start::T stop::T len::Int lendiv::Int - function LinSpace{T}(start,stop,len) where T + function LinRange{T}(start,stop,len) where T len >= 0 || throw(ArgumentError("range($start, stop=$stop, length=$len): negative length")) if len == 1 start == stop || throw(ArgumentError("range($start, stop=$stop, length=$len): endpoints differ")) @@ -253,22 +253,22 @@ struct LinSpace{T} <: AbstractRange{T} end end -function LinSpace(start, stop, len::Integer) +function LinRange(start, stop, len::Integer) T = typeof((stop-start)/len) - LinSpace{T}(start, stop, len) + LinRange{T}(start, stop, len) end function _range(start::T, ::Nothing, stop::S, len::Integer) where {T,S} a, b = promote(start, stop) _range(a, nothing, b, len) end -_range(start::T, ::Nothing, stop::T, len::Integer) where {T<:Real} = LinSpace{T}(start, stop, len) -_range(start::T, ::Nothing, stop::T, len::Integer) where {T} = LinSpace{T}(start, stop, len) +_range(start::T, ::Nothing, stop::T, len::Integer) where {T<:Real} = LinRange{T}(start, stop, len) +_range(start::T, ::Nothing, stop::T, len::Integer) where {T} = LinRange{T}(start, stop, len) _range(start::T, ::Nothing, stop::T, len::Integer) where {T<:Integer} = _linspace(Float64, start, stop, len, 1) ## for Float16, Float32, and Float64 see twiceprecision.jl -function show(io::IO, r::LinSpace) +function show(io::IO, r::LinRange) print(io, "range(") show(io, first(r)) print(io, ", stop=") @@ -368,7 +368,7 @@ isempty(r::StepRange) = (r.start != r.stop) & ((r.step > zero(r.step)) != (r.stop > r.start)) isempty(r::AbstractUnitRange) = first(r) > last(r) isempty(r::StepRangeLen) = length(r) == 0 -isempty(r::LinSpace) = length(r) == 0 +isempty(r::LinRange) = length(r) == 0 """ step(r) @@ -391,7 +391,7 @@ julia> step(range(2.5, stop=10.9, length=85)) step(r::StepRange) = r.step step(r::AbstractUnitRange) = 1 step(r::StepRangeLen{T}) where {T} = T(r.step) -step(r::LinSpace) = (last(r)-first(r))/r.lendiv +step(r::LinRange) = (last(r)-first(r))/r.lendiv step_hp(r::StepRangeLen) = r.step step_hp(r::AbstractRange) = step(r) @@ -408,7 +408,7 @@ unsafe_length(r::OneTo) = r.stop length(r::AbstractUnitRange) = unsafe_length(r) length(r::OneTo) = unsafe_length(r) length(r::StepRangeLen) = r.len -length(r::LinSpace) = r.len +length(r::LinRange) = r.len function length(r::StepRange{T}) where T<:Union{Int,UInt,Int64,UInt64} isempty(r) && return zero(T) @@ -448,11 +448,11 @@ end first(r::OrdinalRange{T}) where {T} = convert(T, r.start) first(r::OneTo{T}) where {T} = oneunit(T) first(r::StepRangeLen) = unsafe_getindex(r, 1) -first(r::LinSpace) = r.start +first(r::LinRange) = r.start last(r::OrdinalRange{T}) where {T} = convert(T, r.stop) last(r::StepRangeLen) = unsafe_getindex(r, length(r)) -last(r::LinSpace) = r.stop +last(r::LinRange) = r.stop minimum(r::AbstractUnitRange) = isempty(r) ? throw(ArgumentError("range must be non-empty")) : first(r) maximum(r::AbstractUnitRange) = isempty(r) ? throw(ArgumentError("range must be non-empty")) : last(r) @@ -465,9 +465,9 @@ copy(r::AbstractRange) = r ## iteration -start(r::LinSpace) = 1 -done(r::LinSpace, i::Int) = length(r) < i -function next(r::LinSpace, i::Int) +start(r::LinRange) = 1 +done(r::LinRange, i::Int) = length(r) < i +function next(r::LinRange, i::Int) @_inline_meta unsafe_getindex(r, i), i+1 end @@ -526,7 +526,7 @@ function getindex(v::AbstractRange{T}, i::Integer) where T ret end -function getindex(r::Union{StepRangeLen,LinSpace}, i::Integer) +function getindex(r::Union{StepRangeLen,LinRange}, i::Integer) @_inline_meta @boundscheck checkbounds(r, i) unsafe_getindex(r, i) @@ -543,7 +543,7 @@ function _getindex_hiprec(r::StepRangeLen, i::Integer) # without rounding by T r.ref + u*r.step end -function unsafe_getindex(r::LinSpace, i::Integer) +function unsafe_getindex(r::LinRange, i::Integer) lerpi.(i-1, r.lendiv, r.start, r.stop) end @@ -593,12 +593,12 @@ function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T} return StepRangeLen{T}(ref, r.step*step(s), length(s), offset) end -function getindex(r::LinSpace, s::OrdinalRange{<:Integer}) +function getindex(r::LinRange, s::OrdinalRange{<:Integer}) @_inline_meta @boundscheck checkbounds(r, s) vfirst = unsafe_getindex(r, first(s)) vlast = unsafe_getindex(r, last(s)) - return LinSpace(vfirst, vlast, length(s)) + return LinRange(vfirst, vlast, length(s)) end show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r))) @@ -609,7 +609,7 @@ show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")") (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) ==(r::OrdinalRange, s::OrdinalRange) = (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) -==(r::T, s::T) where {T<:Union{StepRangeLen,LinSpace}} = +==(r::T, s::T) where {T<:Union{StepRangeLen,LinRange}} = (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) ==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T} = (first(r) == first(s)) & (last(r) == last(s)) & (step(r) == step(s)) @@ -747,32 +747,32 @@ end -(r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r)) -(r::StepRangeLen{T,R,S}) where {T,R,S} = StepRangeLen{T,R,S}(-r.ref, -r.step, length(r), r.offset) --(r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) +-(r::LinRange) = LinRange(-r.start, -r.stop, length(r)) *(x::Number, r::AbstractRange) = range(x*first(r), step=x*step(r), length=length(r)) *(x::Number, r::StepRangeLen{T}) where {T} = StepRangeLen{typeof(x*T(r.ref))}(x*r.ref, x*r.step, length(r), r.offset) -*(x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) +*(x::Number, r::LinRange) = LinRange(x * r.start, x * r.stop, r.len) # separate in case of noncommutative multiplication *(r::AbstractRange, x::Number) = range(first(r)*x, step=step(r)*x, length=length(r)) *(r::StepRangeLen{T}, x::Number) where {T} = StepRangeLen{typeof(T(r.ref)*x)}(r.ref*x, r.step*x, length(r), r.offset) -*(r::LinSpace, x::Number) = LinSpace(r.start * x, r.stop * x, r.len) +*(r::LinRange, x::Number) = LinRange(r.start * x, r.stop * x, r.len) /(r::AbstractRange, x::Number) = range(first(r)/x, step=step(r)/x, length=length(r)) /(r::StepRangeLen{T}, x::Number) where {T} = StepRangeLen{typeof(T(r.ref)/x)}(r.ref/x, r.step/x, length(r), r.offset) -/(r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) +/(r::LinRange, x::Number) = LinRange(r.start / x, r.stop / x, r.len) # also, separate in case of noncommutative multiplication (division) \(x::Number, r::AbstractRange) = range(x\first(r), step=x\step(r), length=x\length(r)) \(x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) -\(x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) +\(x::Number, r::LinRange) = LinRange(x \ r.start, x \ r.stop, r.len) ## scalar-range broadcast operations ## broadcast(::typeof(-), r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r)) broadcast(::typeof(-), r::StepRangeLen) = StepRangeLen(-r.ref, -r.step, length(r), r.offset) -broadcast(::typeof(-), r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) +broadcast(::typeof(-), r::LinRange) = LinRange(-r.start, -r.stop, length(r)) broadcast(::typeof(+), x::Real, r::AbstractUnitRange) = range(x + first(r), length=length(r)) # For #18336 we need to prevent promotion of the step type: @@ -782,34 +782,34 @@ function broadcast(::typeof(+), x::Number, r::StepRangeLen{T}) where T newref = x + r.ref StepRangeLen{typeof(T(r.ref) + x)}(newref, r.step, length(r), r.offset) end -function broadcast(::typeof(+), x::Number, r::LinSpace) - LinSpace(x + r.start, x + r.stop, r.len) +function broadcast(::typeof(+), x::Number, r::LinRange) + LinRange(x + r.start, x + r.stop, r.len) end broadcast(::typeof(+), r::AbstractRange, x::Number) = broadcast(+, x, r) # assumes addition is commutative broadcast(::typeof(-), x::Number, r::AbstractRange) = (x-first(r)):-step(r):(x-last(r)) broadcast(::typeof(-), x::Number, r::StepRangeLen) = broadcast(+, x, -r) -function broadcast(::typeof(-), x::Number, r::LinSpace) - LinSpace(x - r.start, x - r.stop, r.len) +function broadcast(::typeof(-), x::Number, r::LinRange) + LinRange(x - r.start, x - r.stop, r.len) end broadcast(::typeof(-), r::AbstractRange, x::Number) = broadcast(+, -x, r) # assumes addition is commutative broadcast(::typeof(*), x::Number, r::AbstractRange) = range(x*first(r), step=x*step(r), length=length(r)) broadcast(::typeof(*), x::Number, r::StepRangeLen) = StepRangeLen(x*r.ref, x*r.step, length(r), r.offset) -broadcast(::typeof(*), x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) +broadcast(::typeof(*), x::Number, r::LinRange) = LinRange(x * r.start, x * r.stop, r.len) # separate in case of noncommutative multiplication broadcast(::typeof(*), r::AbstractRange, x::Number) = range(first(r)*x, step=step(r)*x, length=length(r)) broadcast(::typeof(*), r::StepRangeLen, x::Number) = StepRangeLen(r.ref*x, r.step*x, length(r), r.offset) -broadcast(::typeof(*), r::LinSpace, x::Number) = LinSpace(r.start * x, r.stop * x, r.len) +broadcast(::typeof(*), r::LinRange, x::Number) = LinRange(r.start * x, r.stop * x, r.len) broadcast(::typeof(/), r::AbstractRange, x::Number) = range(first(r)/x, step=step(r)/x, length=length(r)) broadcast(::typeof(/), r::StepRangeLen, x::Number) = StepRangeLen(r.ref/x, r.step/x, length(r), r.offset) -broadcast(::typeof(/), r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) +broadcast(::typeof(/), r::LinRange, x::Number) = LinRange(r.start / x, r.stop / x, r.len) # also, separate in case of noncommutative multiplication (division) broadcast(::typeof(\), x::Number, r::AbstractRange) = range(x\first(r), step=x\step(r), length=x\length(r)) broadcast(::typeof(\), x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) -broadcast(::typeof(\), x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) +broadcast(::typeof(\), x::Number, r::LinRange) = LinRange(x \ r.start, x \ r.stop, r.len) # promote eltype if at least one container wouldn't change, otherwise join container types. el_same(::Type{T}, a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{T,n}}) where {T,n} = a @@ -869,16 +869,16 @@ StepRangeLen{T}(r::AbstractRange) where {T} = StepRangeLen(T(first(r)), T(step(r)), length(r)) StepRangeLen(r::AbstractRange) = StepRangeLen{eltype(r)}(r) -promote_rule(a::Type{LinSpace{T1}}, b::Type{LinSpace{T2}}) where {T1,T2} = +promote_rule(a::Type{LinRange{T1}}, b::Type{LinRange{T2}}) where {T1,T2} = el_same(promote_type(T1,T2), a, b) -LinSpace{T}(r::LinSpace{T}) where {T} = r -LinSpace{T}(r::AbstractRange) where {T} = LinSpace{T}(first(r), last(r), length(r)) -LinSpace(r::AbstractRange{T}) where {T} = LinSpace{T}(r) +LinRange{T}(r::LinRange{T}) where {T} = r +LinRange{T}(r::AbstractRange) where {T} = LinRange{T}(first(r), last(r), length(r)) +LinRange(r::AbstractRange{T}) where {T} = LinRange{T}(r) -promote_rule(a::Type{LinSpace{T}}, ::Type{OR}) where {T,OR<:OrdinalRange} = - promote_rule(a, LinSpace{eltype(OR)}) +promote_rule(a::Type{LinRange{T}}, ::Type{OR}) where {T,OR<:OrdinalRange} = + promote_rule(a, LinRange{eltype(OR)}) -promote_rule(::Type{LinSpace{L}}, b::Type{StepRangeLen{T,R,S}}) where {L,T,R,S} = +promote_rule(::Type{LinRange{L}}, b::Type{StepRangeLen{T,R,S}}) where {L,T,R,S} = promote_rule(StepRangeLen{L,L,L}, b) # +/- of ranges is defined in operators.jl (to be able to use @eval etc.) @@ -904,7 +904,7 @@ collect(r::AbstractRange) = vcat(r) reverse(r::OrdinalRange) = colon(last(r), -step(r), first(r)) reverse(r::StepRangeLen) = StepRangeLen(r.ref, -r.step, length(r), length(r)-r.offset+1) -reverse(r::LinSpace) = LinSpace(r.stop, r.start, length(r)) +reverse(r::LinRange) = LinRange(r.stop, r.start, length(r)) ## sorting ## @@ -966,16 +966,16 @@ function _define_range_op(@nospecialize f) range($f(first(r1), first(r2)), step=$f(step(r1), step(r2)), length=r1l) end - function $f(r1::LinSpace{T}, r2::LinSpace{T}) where T + function $f(r1::LinRange{T}, r2::LinRange{T}) where T len = r1.len (len == r2.len || throw(DimensionMismatch("argument dimensions must match"))) - LinSpace{T}(convert(T, $f(first(r1), first(r2))), + LinRange{T}(convert(T, $f(first(r1), first(r2))), convert(T, $f(last(r1), last(r2))), len) end - $f(r1::Union{StepRangeLen, OrdinalRange, LinSpace}, - r2::Union{StepRangeLen, OrdinalRange, LinSpace}) = + $f(r1::Union{StepRangeLen, OrdinalRange, LinRange}, + r2::Union{StepRangeLen, OrdinalRange, LinRange}) = $f(promote(r1, r2)...) end end diff --git a/base/show.jl b/base/show.jl index a835e23916dac..d85d462344105 100644 --- a/base/show.jl +++ b/base/show.jl @@ -4,10 +4,10 @@ show(io::IO, ::MIME"text/plain", r::AbstractRange) = show(io, r) # always use the compact form for printing ranges -function show(io::IO, ::MIME"text/plain", r::LinSpace) - # show for LinSpace, e.g. +function show(io::IO, ::MIME"text/plain", r::LinRange) + # show for LinRange, e.g. # range(1, stop=3, length=7) - # 7-element LinSpace{Float64}: + # 7-element LinRange{Float64}: # 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0 print(io, summary(r)) if !isempty(r) diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 17cd3eb90b239..dddbb3c1364d3 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -567,7 +567,7 @@ function +(r1::StepRangeLen{T,R}, r2::StepRangeLen{T,R}) where T where R<:TwiceP StepRangeLen{T,typeof(ref),typeof(step)}(ref, step, len, imid) end -## LinSpace +## LinRange # For Float16, Float32, and Float64, this returns a StepRangeLen function _range(start::T, ::Nothing, stop::T, len::Integer) where {T<:IEEEFloat} diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index a33f6a9a643c4..9f583cfe0df20 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -119,7 +119,7 @@ end @test [linreg(x,y)...] ≈ [2.5559090909090867, 1.6960139860139862] @test [linreg(view(x,1:6),view(y,1:6))...] ≈ [3.8366666666666642,1.3271428571428574] - # check (LinSpace, UnitRange) + # check (LinRange, UnitRange) x = range(1.0, stop=12.0, length=100) y = -100:-1 @test [linreg(x, y)...] ≈ [-109.0, 9.0] @@ -129,7 +129,7 @@ end y = 12:-1:1 @test [linreg(x, y)...] ≈ [13.0, -1.0] - # check (LinSpace, LinSpace) + # check (LinRange, LinRange) x = range(-5, stop=10, length=100) y = range(50, stop=200, length=100) @test [linreg(x, y)...] ≈ [100.0, 10.0] diff --git a/test/ranges.jl b/test/ranges.jl index ef2d995d12eee..3c69148f88253 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -670,8 +670,8 @@ end end # issue #20380 -let r = LinSpace(1,4,4) - @test isa(r[1:4], LinSpace) +let r = LinRange(1,4,4) + @test isa(r[1:4], LinRange) end @testset "range with 1 or 0 elements (whose step length is NaN)" begin @@ -866,23 +866,23 @@ end @test convert(StepRangeLen, 0:5) == 0:5 @test convert(StepRangeLen, 0:1:5) == 0:1:5 - @test convert(LinSpace{Float64}, 0.0:0.1:0.3) === LinSpace{Float64}(0.0, 0.3, 4) - @test convert(LinSpace, 0.0:0.1:0.3) === LinSpace{Float64}(0.0, 0.3, 4) - @test convert(LinSpace, 0:3) === LinSpace{Int}(0, 3, 4) + @test convert(LinRange{Float64}, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) + @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) + @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) end -@testset "LinSpace ops" begin - @test start(LinSpace(0,3,4)) == 1 - @test 2*LinSpace(0,3,4) == LinSpace(0,6,4) - @test LinSpace(0,3,4)*2 == LinSpace(0,6,4) - @test LinSpace(0,3,4)/3 == LinSpace(0,1,4) - @test broadcast(-, 2, LinSpace(0,3,4)) == LinSpace(2,-1,4) - @test broadcast(+, 2, LinSpace(0,3,4)) == LinSpace(2,5,4) - @test -LinSpace(0,3,4) == LinSpace(0,-3,4) - @test reverse(LinSpace(0,3,4)) == LinSpace(3,0,4) +@testset "LinRange ops" begin + @test start(LinRange(0,3,4)) == 1 + @test 2*LinRange(0,3,4) == LinRange(0,6,4) + @test LinRange(0,3,4)*2 == LinRange(0,6,4) + @test LinRange(0,3,4)/3 == LinRange(0,1,4) + @test broadcast(-, 2, LinRange(0,3,4)) == LinRange(2,-1,4) + @test broadcast(+, 2, LinRange(0,3,4)) == LinRange(2,5,4) + @test -LinRange(0,3,4) == LinRange(0,-3,4) + @test reverse(LinRange(0,3,4)) == LinRange(3,0,4) end @testset "Issue #11245" begin io = IOBuffer() @@ -910,15 +910,15 @@ end @test replstrmime(1:4) == "1:4" @test stringmime("text/plain", 1:4) == "1:4" @test stringmime("text/plain", range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0" - @test stringmime("text/plain", LinSpace{Float64}(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" + @test stringmime("text/plain", LinRange{Float64}(1,5,7)) == "7-element LinRange{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" @test repr(range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0" - @test repr(LinSpace{Float64}(1,5,7)) == "range(1.0, stop=5.0, length=7)" + @test repr(LinRange{Float64}(1,5,7)) == "range(1.0, stop=5.0, length=7)" @test replstrmime(0:100.) == "0.0:1.0:100.0" # next is to test a very large range, which should be fast because print_range # only examines spacing of the left and right edges of the range, sufficient # to cover the designated screen size. @test replstrmime(range(0, stop=100, length=10000)) == "0.0:0.010001000100010001:100.0" - @test replstrmime(LinSpace{Float64}(0,100, 10000)) == "10000-element LinSpace{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0" + @test replstrmime(LinRange{Float64}(0,100, 10000)) == "10000-element LinRange{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0" @test sprint(show, UnitRange(1, 2)) == "1:2" @test sprint(show, StepRange(1, 2, 5)) == "1:2:5" @@ -927,15 +927,15 @@ end @testset "Issue 11049 and related" begin @test promote(range(0f0, stop=1f0, length=3), range(0., stop=5., length=2)) === (range(0., stop=1., length=3), range(0., stop=5., length=2)) - @test convert(LinSpace{Float64}, range(0., stop=1., length=3)) === LinSpace(0., 1., 3) - @test convert(LinSpace{Float64}, range(0f0, stop=1f0, length=3)) === LinSpace(0., 1., 3) + @test convert(LinRange{Float64}, range(0., stop=1., length=3)) === LinRange(0., 1., 3) + @test convert(LinRange{Float64}, range(0f0, stop=1f0, length=3)) === LinRange(0., 1., 3) @test promote(range(0., stop=1., length=3), 0:5) === (range(0., stop=1., length=3), range(0., stop=5., length=6)) - @test convert(LinSpace{Float64}, 0:5) === LinSpace(0., 5., 6) - @test convert(LinSpace{Float64}, 0:1:5) === LinSpace(0., 5., 6) - @test convert(LinSpace, 0:5) === LinSpace{Int}(0, 5, 6) - @test convert(LinSpace, 0:1:5) === LinSpace{Int}(0, 5, 6) + @test convert(LinRange{Float64}, 0:5) === LinRange(0., 5., 6) + @test convert(LinRange{Float64}, 0:1:5) === LinRange(0., 5., 6) + @test convert(LinRange, 0:5) === LinRange{Int}(0, 5, 6) + @test convert(LinRange, 0:1:5) === LinRange{Int}(0, 5, 6) function test_range_index(r, s) @test typeof(r[s]) == typeof(r) diff --git a/test/specificity.jl b/test/specificity.jl index bc56c9e9ddb45..10dbc4c0732c9 100644 --- a/test/specificity.jl +++ b/test/specificity.jl @@ -134,7 +134,7 @@ f17016(f, t1::Tuple) = 1 @test args_morespecific(Tuple{Union{Base.StepRange{T, S} where S, Base.StepRangeLen{T, T, S} where S}, Union{Base.StepRange{T, S} where S, Base.StepRangeLen{T, T, S} where S}} where T, - Tuple{T, T} where T<:Union{Base.StepRangeLen, Base.LinSpace}) + Tuple{T, T} where T<:Union{Base.StepRangeLen, Base.LinRange}) @test args_morespecific(Tuple{Type{Tuple}, Any, Any}, Tuple{Type{Tuple{Vararg{E, N} where N}}, Any, Any} where E) From a2cddffec8bbfc00c23f044b68ca272b17c7ceb7 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Tue, 13 Feb 2018 15:24:34 -0800 Subject: [PATCH 4/4] Deprecate logspace to its definition --- NEWS.md | 2 ++ base/deprecated.jl | 3 ++- base/exports.jl | 1 - base/range.jl | 27 +-------------------------- doc/src/base/arrays.md | 1 - examples/lru_test.jl | 2 +- test/compiler/compiler.jl | 5 +++-- test/ranges.jl | 11 ----------- 8 files changed, 9 insertions(+), 43 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2c7596d62ff6e..d55352225dde4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1042,6 +1042,8 @@ Deprecated or removed * `LinSpace` has been renamed to `LinRange` ([#25896]). + * `logspace` has been deprecated to its definition ([#25896]). + * `endof(a)` has been renamed to `lastindex(a)`, and the `end` keyword in indexing expressions now lowers to either `lastindex(a)` (in the case with only one index) or `lastindex(a, d)` (in cases where there is more than one index and `end` appears at dimension `d`) ([#23554], [#25763]). diff --git a/base/deprecated.jl b/base/deprecated.jl index 3fa7c9e97ef16..5c8b575b9b745 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -841,7 +841,7 @@ end # issue #24794 @deprecate linspace(start, stop) range(start, stop=stop, length=50) -@deprecate logspace(start, stop) logspace(start, stop, 50) +@deprecate logspace(start, stop) exp10.(range(start, stop=stop, length=50)) # 24490 - warnings and messages const log_info_to = Dict{Tuple{Union{Module,Nothing},Union{Symbol,Nothing}},IO}() @@ -1318,6 +1318,7 @@ export readandwrite @deprecate linspace(start, stop, length::Integer) range(start, stop=stop, length=length) @deprecate linspace(start, stop, length::Real) range(start, stop=stop, length=Int(length)) @deprecate_binding LinSpace LinRange +@deprecate logspace(start, stop, n; base=10) base.^range(start, stop=stop, length=n) @deprecate runtests(tests, ncores; kw...) runtests(tests; ncores = ncores, kw...) false @deprecate code_lowered(f, types, generated) code_lowered(f, types, generated = generated) diff --git a/base/exports.jl b/base/exports.jl index 6c51c57e48d35..7c53e12a1b858 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -395,7 +395,6 @@ export issorted, last, linearindices, - logspace, mapslices, max, maximum!, diff --git a/base/range.jl b/base/range.jl index 1ee8ea84a1eeb..26f994a0d14d9 100644 --- a/base/range.jl +++ b/base/range.jl @@ -235,7 +235,7 @@ StepRangeLen(ref::R, step::S, len::Integer, offset::Integer = 1) where {R,S} = StepRangeLen{T}(ref::R, step::S, len::Integer, offset::Integer = 1) where {T,R,S} = StepRangeLen{T,R,S}(ref, step, len, offset) -## range with computed step, and logspace +## range with computed step struct LinRange{T} <: AbstractRange{T} start::T @@ -335,31 +335,6 @@ function print_range(io::IO, r::AbstractRange, end end -""" - logspace(start::Real, stop::Real, n::Integer; base=10) - -Construct a vector of `n` logarithmically spaced numbers from `base^start` to `base^stop`. - -```jldoctest -julia> logspace(1.,10.,5) -5-element Array{Float64,1}: - 10.0 - 1778.2794100389228 - 316227.7660168379 - 5.623413251903491e7 - 1.0e10 - -julia> logspace(1.,10.,5,base=2) -5-element Array{Float64,1}: - 2.0 - 9.513656920021768 - 45.254833995939045 - 215.2694823049509 - 1024.0 -``` -""" -logspace(start::Real, stop::Real, n::Integer; base=10) = base.^range(start, stop=stop, length=n) - ## interface implementations size(r::AbstractRange) = (length(r),) diff --git a/doc/src/base/arrays.md b/doc/src/base/arrays.md index 399f5b6d4244e..6ea60b55c4d19 100644 --- a/doc/src/base/arrays.md +++ b/doc/src/base/arrays.md @@ -32,7 +32,6 @@ Base.fill Base.fill! Base.similar(::AbstractArray) Base.similar(::Any, ::Tuple) -Base.logspace ``` ## Basic functions diff --git a/examples/lru_test.jl b/examples/lru_test.jl index 21b4e5b668cdc..5c797c2a57906 100644 --- a/examples/lru_test.jl +++ b/examples/lru_test.jl @@ -10,7 +10,7 @@ get_str(i) = String(vcat(map(x->[x>>4; x&0x0F], reinterpret(UInt8, [Int32(i)])). isbounded(::Type{L}) where {L<:LRUExample.LRU} = any(map(n->n==:maxsize, fieldnames(L))) isbounded(l::L) where {L<:LRUExample.LRU} = isbounded(L) -nmax = round.(Int, logspace(2, 5, 4)) +nmax = map(x->round(Int, exp10(x)), range(2, stop=5, length=4)) function lrutest() #println("LRU consistency tests") diff --git a/test/compiler/compiler.jl b/test/compiler/compiler.jl index 4a398562a708a..0e98dd31d242d 100644 --- a/test/compiler/compiler.jl +++ b/test/compiler/compiler.jl @@ -585,8 +585,9 @@ tpara18457(::Type{A}) where {A<:AbstractMyType18457} = tpara18457(supertype(A)) function FOO_19322(Y::AbstractMatrix; frac::Float64=0.3, nbins::Int=100, n_sims::Int=100) num_iters, num_chains = size(Y) - start_iters = unique([1; [round(Int64, s) for s in logspace(log(10,100), - log(10,num_iters/2),nbins-1)]]) + start_iters = unique([1; map(s->round(Int64, exp10(s)), range(log(10,100), + stop=log(10,num_iters/2), + length=nbins-1))]) result = zeros(Float64, 10, length(start_iters) * num_chains) j=1 for c in 1:num_chains diff --git a/test/ranges.jl b/test/ranges.jl index 3c69148f88253..570570638e107 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1209,17 +1209,6 @@ end @test Float32(linsp.ref) ≈ linsp.ref.hi + linsp.ref.lo end -@testset "logspace" begin - n = 10; a = 2; b = 4 - # test default values; base = 10 - @test logspace(a, b, 50) == 10 .^ range(a, stop=b, length=50) - @test logspace(a, b, n) == 10 .^ range(a, stop=b, length=n) - for base in (10, 2, ℯ) - @test logspace(a, b, 50, base=base) == base.^range(a, stop=b, length=50) - @test logspace(a, b, n, base=base) == base.^range(a, stop=b, length=n) - end -end - @testset "issue #23300" begin x = -5:big(1.0):5 @test map(Float64, x) === -5.0:1.0:5.0