# How to make linspace work with one point?

**URL:** <https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 11, 2017, 4:32pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335 "2017-08-11T16:32:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 11, 2017, 4:32pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/1 "2017-08-11T16:32:23Z")

</div>

Currently,

```julia
linspace(1,10,1)

```

returns

```julia
ERROR: ArgumentError: linspace(1.0, 10.0, 1): endpoints differ

```

* * *

What’s the way to use multiple dispatch to grab the value of 1 and make a method like:

```julia
linspace(a,b,1) = a:1.0:a

```

I remember reading about this somewhere

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 11, 2017, 5:11pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/2 "2017-08-11T17:11:10Z")

</div>

I’d consider that error a feature, not a bug. In my mind, the raison d’etre of `linspace` is to hit the endpoints exactly — that’s how it differs from the other constructions.

Multiple dispatch only works with types, not values, so you can’t use it to do something special when one of the arguments is the value `1`.

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [July 25, 2021, 11:53pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/4 "2021-07-25T23:53:36Z")

</div>

Range has the same behavior. It gives nonsensical enough results for length=0, but errors, rather than defaulting to the valid singleton range `0:0` that I expect.

```julia
julia> range(0, stop=2, length=0)
0.0:-2.0:2.0

julia> range(0, stop=2, length=1)
ERROR: ArgumentError: range(0.0, stop=2.0, length=1): endpoints differ
Stacktrace:
 [1] _linspace1(#unused#::Type{Float64}, start::Float64, stop::Float64, len::Int64)
   @ Base ./twiceprecision.jl:678
 [2] _linspace(#unused#::Type{Float64}, start_n::Int64, stop_n::Int64, len::Int64, den::Int64)
   @ Base ./twiceprecision.jl:662
 [3] _linspace
   @ ./twiceprecision.jl:660 [inlined]
 [4] _range
   @ ./range.jl:441 [inlined]
 [5] #range#57
   @ ./range.jl:91 [inlined]
 [6] top-level scope
   @ REPL[2]:1

julia> range(0, stop=2, length=2)
0.0:2.0:2.0

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 26, 2021, 4:49am UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/5 "2021-07-26T04:49:19Z")

</div>

That still makes some sense to me: an empty range is definitely what you asked for and definitely what you got back. A 1-length range with different endpoints is impossible to satisfy. Should it give you back `0:0` or `2:2`?

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [July 26, 2021, 3:58pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/6 "2021-07-26T15:58:04Z")

</div>

> Should it give you back `0:0` or `2:2` ?

IMO, Yes!

What is most consistent with most user’s expectations?

`range(2, stop=0, length=2)` works, so why not choose the convention that `range` includes the mandatory first argument `start`, so:  
`range(2, stop=0, length=1)` → `2:2`  
`range(0, stop=2, length=1)` → `0:0`

The assumption that a user would want an empty range, but never a semi-empty one seems inconsistent to me.

Otherwise, one works around this with clunky code blocks.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 26, 2021, 4:08pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/7 "2021-07-26T16:08:27Z")

</div>

> [@deszoeke](#):
>
> > Should it give you back `0:0` or `2:2` ?
> 
> IMO, Yes!

Maybe I misunderstood the question, but I don’t think “yes” is an answer to it.

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [July 26, 2021, 4:12pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/8 "2021-07-26T16:12:05Z")

</div>

In my opinion it should give back either `0:0` or `2:2`.

There might be no guarantee which of these is returned. However, I think returning a range containing `start` is preferable.

This has the advantage that it would interpolate from the predictable behaviors for `length=0, 2, 3, ..., n`

`range(0, stop=2, length=0)` → `0:-2:2` represents an empty range  
`range(0, stop=2, length=1)` → `0:0` (proposed)  
`range(0, stop=2, length=2)` → `0:2:2`  
`range(0, stop=2, length=3)` → `0:1:2`  
etc.

This would be convenient. It might be unpredictable in other contexts.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 26, 2021, 5:04pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/9 "2021-07-26T17:04:42Z")

</div>

As soon as you’re guessing what the user meant and just arbitrarily picking one possibility, you’re in dangerous API design territory. Dynamic languages have traditionally loved doing this “for convenience” but the upshot is that most of them have a bad reputation for being impossible to write reliable code in. When in doubt about what the user meant, throw an error and ask them to be more specific.

---

<div class="post-metadata">

**Author:** ![deszoeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deszoeke/32/2174_2.png) [@deszoeke](https://discourse.julialang.org/u/deszoeke)\
**Post date:** [July 26, 2021, 5:30pm UTC](https://discourse.julialang.org/t/how-to-make-linspace-work-with-one-point/5335/10 "2021-07-26T17:30:38Z")

</div>

Point taken.

`range(0,step=2,stop=3)` → `0:2:2`  
never returns `1:2:3`. It silently assumes the range stops somewhere other than `stop`.

This seems very conventional. Using `linspace` to interpolate a specified number of points is too. I’ll back away from wanting a (tricky) mixture of the `linspace` and `range` behavior.

The OP could try something like this:

```julia
linearpermissiverange(start; stop, length) = length==1 ? (start:start) : range(start, stop=stop, length=length)

```

to handle ranges of `length=1`.
