Skip to content

Commit

Permalink
Rename LinSpace to LinRange
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ararslan committed Feb 14, 2018
1 parent 6c0e39d commit 19c8b14
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 83 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down
4 changes: 2 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export
BitSet,
IOBuffer,
IOStream,
LinSpace,
LinRange,
Irrational,
Matrix,
MergeSort,
Expand Down
8 changes: 4 additions & 4 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
92 changes: 46 additions & 46 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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=")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)))
Expand All @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.)
Expand All @@ -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 ##

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
Loading

0 comments on commit 19c8b14

Please sign in to comment.