Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent return types (and error) with arithmetic on LinSpace #11049

Closed
yuyichao opened this issue Apr 29, 2015 · 3 comments
Closed

Inconsistent return types (and error) with arithmetic on LinSpace #11049

yuyichao opened this issue Apr 29, 2015 · 3 comments

Comments

@yuyichao
Copy link
Contributor

Trying to do simple arithmetics on the LinSpace object returns inconsistent result (or error).

julia> typeof(linspace(0, 10, 20))
LinSpace{Float64}

julia> typeof(linspace(0, 10, 20) + 1)
FloatRange{Float64}

julia> typeof(linspace(0, 10, 20) - 1)
Array{Float64,1}

julia> typeof(linspace(0, 10, 20) * 1)
Array{Float64,1}

julia> typeof(linspace(0, 10, 20) / 1)
Array{Float64,1}

julia> typeof(+linspace(0, 10, 20))
LinSpace{Float64}

julia> typeof(-linspace(0, 10, 20))
ERROR: MethodError: `-` has no method matching -(::LinSpace{Float64})
Closest candidates are:
  -{S,T}(::Range{S}, ::Range{T})
  -{S,T}(::AbstractArray{S,N}, ::Range{T})
  -{S,T}(::Range{S}, ::AbstractArray{T,N})
  ...

OTOH, doing the same on ranges seems to be pretty consistent.

julia> typeof(0:2:20)
StepRange{Int64,Int64}

julia> typeof((0:2:20) + 1)
StepRange{Int64,Int64}

julia> typeof((0:2:20) - 1)
StepRange{Int64,Int64}

julia> typeof((0:2:20) * 1)
StepRange{Int64,Int64}

julia> typeof((0:2:20) / 1)
FloatRange{Float64}

julia> typeof(+(0:2:20))
StepRange{Int64,Int64}

julia> typeof(-(0:2:20))
StepRange{Int64,Int64}

P.S. what's the difference between a range and a linspace?

@yuyichao
Copy link
Contributor Author

Maybe this is a better comparison...

julia> typeof(0:2:20.)
FloatRange{Float64}

julia> typeof((0:2:20.) + 1)
FloatRange{Float64}

julia> typeof((0:2:20.) - 1)
FloatRange{Float64}

julia> typeof((0:2:20.) * 1)
FloatRange{Float64}

julia> typeof((0:2:20.) / 1)
FloatRange{Float64}

julia> typeof(+(0:2:20.))
FloatRange{Float64}

julia> typeof(-(0:2:20.))
FloatRange{Float64}

anyway... The fact taht linspace(0, 10, 20) + (-1) and linspace(0, 10, 20) - 1 returns different types and that -linspace(0, 10, 20) throws an error is quite weird.

The latter causes a test failure on LsqFit.jl.

@Gnimuc
Copy link
Contributor

Gnimuc commented Apr 30, 2015

OrdinalRange is a subtype of Range.
FloatRange is a subtype of Range.
LinSpace is a subtype of Range.
But there is no - operation on LinSpace in range.jl (line 535).

## linear operations on ranges ##

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

maybe Stefan forgot to overload it 42db285

@jhlq
Copy link

jhlq commented Apr 30, 2015

This is probably the cause of some mysterious deprecation warnings that
initially said a range constructed from ints with subtraction of ints was
not a range of ints...
On Apr 30, 2015 8:41 AM, "Gnimuc Key" notifications@github.com wrote:

OrdinalRange is a subtype of Range.
FloatRange is a subtype of Range.
LinSpace is a subtype of Range.
But there is no - operation on LinSpace in range.jl (line 535).

linear operations on ranges

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

maybe Stefan forgot to overload it 42db285
42db285


Reply to this email directly or view it on GitHub
#11049 (comment).

yuyichao added a commit to yuyichao/julia that referenced this issue May 11, 2015
yuyichao added a commit to yuyichao/julia that referenced this issue May 11, 2015
yuyichao added a commit to yuyichao/julia that referenced this issue May 11, 2015
yuyichao added a commit to yuyichao/julia that referenced this issue May 15, 2015
StefanKarpinski added a commit that referenced this issue May 22, 2015
Missing operations for LinSpace. Fix #11049
mbauman pushed a commit to mbauman/julia that referenced this issue Jun 6, 2015
tkelman pushed a commit to tkelman/julia that referenced this issue Jun 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants