Skip to content

Commit 81e48de

Browse files
committed
Missing operations for LinSpace. Fix #11049
1 parent 4603006 commit 81e48de

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

base/range.jl

+30
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ function getindex(r::FloatRange, s::OrdinalRange)
390390
FloatRange(r.start + (first(s)-1)*r.step, step(s)*r.step, sl, r.divisor)
391391
end
392392

393+
function getindex{T}(r::LinSpace{T}, s::OrdinalRange)
394+
sl = check_indexingrange(s, r)
395+
ifirst = first(s)
396+
ilast = last(s)
397+
LinSpace{T}(((r.len - ifirst) * r.start + (ifirst - 1) * r.stop) / r.divisor,
398+
((r.len - ilast) * r.start + (ilast - 1) * r.stop) / r.divisor,
399+
sl, sl - 1)
400+
end
401+
393402
function show(io::IO, r::Range)
394403
print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))
395404
end
@@ -538,27 +547,34 @@ end
538547

539548
-(r::OrdinalRange) = range(-r.start, -step(r), length(r))
540549
-(r::FloatRange) = FloatRange(-r.start, -r.step, r.len, r.divisor)
550+
-(r::LinSpace) = LinSpace(-r.start, -r.stop, r.len, r.divisor)
541551

542552
.+(x::Real, r::UnitRange) = range(x + r.start, length(r))
543553
.+(x::Real, r::Range) = (x+first(r)):step(r):(x+last(r))
544554
#.+(x::Real, r::StepRange) = range(x + r.start, r.step, length(r))
545555
.+(x::Real, r::FloatRange) = FloatRange(r.divisor*x + r.start, r.step, r.len, r.divisor)
556+
.+(x::Real, r::LinSpace) = LinSpace(x + r.start, x + r.stop, r.len, r.divisor)
546557
.+(r::Range, x::Real) = x + r
547558
#.+(r::FloatRange, x::Real) = x + r
548559

549560
.-(x::Real, r::Range) = (x-first(r)):-step(r):(x-last(r))
550561
.-(x::Real, r::FloatRange) = FloatRange(r.divisor*x - r.start, -r.step, r.len, r.divisor)
562+
.-(x::Real, r::LinSpace) = LinSpace(x - r.start, x - r.stop, r.len, r.divisor)
551563
.-(r::UnitRange, x::Real) = range(r.start-x, length(r))
552564
.-(r::StepRange , x::Real) = range(r.start-x, r.step, length(r))
553565
.-(r::FloatRange, x::Real) = FloatRange(r.start - r.divisor*x, r.step, r.len, r.divisor)
566+
.-(r::LinSpace, x::Real) = LinSpace(r.start - x, r.stop - x, r.len, r.divisor)
554567

555568
.*(x::Real, r::OrdinalRange) = range(x*r.start, x*step(r), length(r))
556569
.*(x::Real, r::FloatRange) = FloatRange(x*r.start, x*r.step, r.len, r.divisor)
570+
.*(x::Real, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len, r.divisor)
557571
.*(r::Range, x::Real) = x .* r
558572
.*(r::FloatRange, x::Real) = x .* r
573+
.*(r::LinSpace, x::Real) = x .* r
559574

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

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

608+
promote_rule{T1,T2}(::Type{LinSpace{T1}},::Type{LinSpace{T2}}) =
609+
LinSpace{promote_type(T1,T2)}
610+
convert{T}(::Type{LinSpace{T}}, r::LinSpace{T}) = r
611+
convert{T}(::Type{LinSpace{T}}, r::LinSpace) =
612+
LinSpace{T}(r.start, r.stop, r.len, r.divisor)
613+
614+
promote_rule{F,OR<:OrdinalRange}(::Type{LinSpace{F}}, ::Type{OR}) =
615+
LinSpace{promote_type(F,eltype(OR))}
616+
convert{T}(::Type{LinSpace{T}}, r::OrdinalRange) =
617+
LinSpace{T}(first(r), step(r), length(r), one(T))
618+
convert{T}(::Type{LinSpace}, r::OrdinalRange{T}) =
619+
LinSpace{typeof(float(first(r)))}(first(r), last(r), length(r), one(T))
620+
592621

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

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

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

634664
## sorting ##
635665

test/ranges.jl

+28
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,31 @@ end
524524
@test convert(FloatRange{Float64}, 0:1:5) === 0.:1.:5.
525525
@test convert(FloatRange, 0:5) === 0.:1.:5.
526526
@test convert(FloatRange, 0:1:5) === 0.:1.:5.
527+
528+
# Issue 11049 and related
529+
function test_range_index(r, s)
530+
@test [r;][s] == [r[s];]
531+
end
532+
test_range_index(linspace(0.1, 0.3, 3), 1:2)
533+
test_range_index(linspace(1.0, 27.0, 17), 1:10)
534+
535+
function test_linspace_identity(r, mr)
536+
@test -r == mr
537+
@test isa(-r, LinSpace)
538+
539+
@test 1 + r + (-1) == r
540+
@test isa(1 + r + (-1), LinSpace)
541+
@test 1 - r - 1 == mr
542+
@test isa(1 - r - 1, LinSpace)
543+
544+
@test 1 * r * 1 == r
545+
@test isa(1 * r * 1, LinSpace)
546+
@test r / 1 == r
547+
@test isa(r / 1, LinSpace)
548+
end
549+
550+
test_linspace_identity(linspace(1.0, 27.0, 1275), linspace(-1.0, -27.0, 1275))
551+
552+
@test reverse(linspace(1.0, 27.0, 1275)) == linspace(27.0, 1.0, 1275)
553+
@test [reverse(linspace(1.0, 27.0, 1275));] ==
554+
reverse([linspace(1.0, 27.0, 1275);])

0 commit comments

Comments
 (0)