Skip to content

Commit

Permalink
Missing operations for LinSpace. Fix JuliaLang#11049
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed May 11, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b458ea3 commit 42a2ed7
Showing 2 changed files with 58 additions and 0 deletions.
30 changes: 30 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
@@ -390,6 +390,15 @@ function getindex(r::FloatRange, s::OrdinalRange)
FloatRange(r.start + (first(s)-1)*r.step, step(s)*r.step, sl, r.divisor)
end

function getindex{T}(r::LinSpace{T}, s::OrdinalRange)
sl = check_indexingrange(s, r)
ifirst = first(s)
ilast = last(s)
LinSpace{T}(((r.len - ifirst) * r.start + (ifirst - 1) * r.stop) / r.divisor,
((r.len - ilast) * r.start + (ilast - 1) * r.stop) / r.divisor,
sl, sl - 1)
end

function show(io::IO, r::Range)
print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))
end
@@ -538,27 +547,34 @@ end

-(r::OrdinalRange) = range(-r.start, -step(r), length(r))
-(r::FloatRange) = FloatRange(-r.start, -r.step, r.len, r.divisor)
-(r::LinSpace) = LinSpace(-r.start, -r.stop, r.len, r.divisor)

.+(x::Real, r::UnitRange) = range(x + r.start, length(r))
.+(x::Real, r::Range) = (x+first(r)):step(r):(x+last(r))
#.+(x::Real, r::StepRange) = range(x + r.start, r.step, length(r))
.+(x::Real, r::FloatRange) = FloatRange(r.divisor*x + r.start, r.step, r.len, r.divisor)
.+(x::Real, r::LinSpace) = LinSpace(x + r.start, x + r.stop, r.len, r.divisor)
.+(r::Range, x::Real) = x + r
#.+(r::FloatRange, x::Real) = x + r

.-(x::Real, r::Range) = (x-first(r)):-step(r):(x-last(r))
.-(x::Real, r::FloatRange) = FloatRange(r.divisor*x - r.start, -r.step, r.len, r.divisor)
.-(x::Real, r::LinSpace) = LinSpace(x - r.start, x - r.stop, r.len, r.divisor)
.-(r::UnitRange, x::Real) = range(r.start-x, length(r))
.-(r::StepRange , x::Real) = range(r.start-x, r.step, length(r))
.-(r::FloatRange, x::Real) = FloatRange(r.start - r.divisor*x, r.step, r.len, r.divisor)
.-(r::LinSpace, x::Real) = LinSpace(r.start - x, r.stop - x, r.len, r.divisor)

.*(x::Real, r::OrdinalRange) = range(x*r.start, x*step(r), length(r))
.*(x::Real, r::FloatRange) = FloatRange(x*r.start, x*r.step, r.len, r.divisor)
.*(x::Real, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len, r.divisor)
.*(r::Range, x::Real) = x .* r
.*(r::FloatRange, x::Real) = x .* r
.*(r::LinSpace, x::Real) = x .* r

./(r::OrdinalRange, x::Real) = range(r.start/x, step(r)/x, length(r))
./(r::FloatRange, x::Real) = FloatRange(r.start/x, r.step/x, r.len, r.divisor)
./(r::LinSpace, x::Real) = LinSpace(r.start / x, r.stop / x, r.len, r.divisor)

promote_rule{T1,T2}(::Type{UnitRange{T1}},::Type{UnitRange{T2}}) =
UnitRange{promote_type(T1,T2)}
@@ -589,6 +605,19 @@ convert{T}(::Type{FloatRange{T}}, r::OrdinalRange) =
convert{T}(::Type{FloatRange}, r::OrdinalRange{T}) =
FloatRange{typeof(float(first(r)))}(first(r), step(r), length(r), one(T))

promote_rule{T1,T2}(::Type{LinSpace{T1}},::Type{LinSpace{T2}}) =
LinSpace{promote_type(T1,T2)}
convert{T}(::Type{LinSpace{T}}, r::LinSpace{T}) = r
convert{T}(::Type{LinSpace{T}}, r::LinSpace) =
LinSpace{T}(r.start, r.stop, r.len, r.divisor)

promote_rule{F,OR<:OrdinalRange}(::Type{LinSpace{F}}, ::Type{OR}) =
LinSpace{promote_type(F,eltype(OR))}
convert{T}(::Type{LinSpace{T}}, r::OrdinalRange) =
LinSpace{T}(first(r), step(r), length(r), one(T))
convert{T}(::Type{LinSpace}, r::OrdinalRange{T}) =
LinSpace{typeof(float(first(r)))}(first(r), last(r), length(r), one(T))


# +/- of ranges is defined in operators.jl (to be able to use @eval etc.)

@@ -630,6 +659,7 @@ collect(r::Range) = vcat(r)

reverse(r::OrdinalRange) = colon(last(r), -step(r), first(r))
reverse(r::FloatRange) = FloatRange(r.start + (r.len-1)*r.step, -r.step, r.len, r.divisor)
reverse(r::LinSpace) = LinSpace(r.stop, r.start, r.len, r.divisor)

## sorting ##

28 changes: 28 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
@@ -524,3 +524,31 @@ end
@test convert(FloatRange{Float64}, 0:1:5) === 0.:1.:5.
@test convert(FloatRange, 0:5) === 0.:1.:5.
@test convert(FloatRange, 0:1:5) === 0.:1.:5.

# Issue 11049 and related
function test_range_index(r, s)
@test [r;][s] == [r[s];]
end
test_range_index(linspace(0.1, 0.3, 3), 1:2)
test_range_index(linspace(1.0, 27.0, 17), 1:10)

function test_linspace_identity(r, mr)
@test -r == mr
@test isa(-r, LinSpace)

@test 1 + r + (-1) == r
@test isa(1 + r + (-1), LinSpace)
@test 1 - r - 1 == mr
@test isa(1 - r - 1, LinSpace)

@test 1 * r * 1 == r
@test isa(1 * r * 1, LinSpace)
@test r / 1 == r
@test isa(r / 1, LinSpace)
end

test_linspace_identity(linspace(1.0, 27.0, 1275), linspace(-1.0, -27.0, 1275))

@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);])

0 comments on commit 42a2ed7

Please sign in to comment.