question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: allow linspace to take 1D vectors as start and end

See original GitHub issue

I have a line segment in N-dimensional space, defined by its begin- and endpoints (both 1D vectors). For example in 3D:

start = array([0,1,2])
end = array([1,3,6])

I would like to be able segment the line in k segments, for example 4, or 5 including the endpoints. The result would then be:

 array([[ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
       [ 1.  ,  1.5 ,  2.  ,  2.5 ,  3.  ],
       [ 2.  ,  3.  ,  4.  ,  5.  ,  6.  ]]) 

I extended (and broke) linspace to not only accept scalar, but also 1D vector start- and endpoints:

def linspace(start,stop,num=50,endpoint=True,retstep=False):
    spannedRange = stop-start
    numberOfSteps = num - (1 if endpoint else 0)
    step = array(spannedRange/numberOfSteps,ndmin=2).T
    coefficientColumn = numpy.arange(num)
    samples = numpy.kron(coefficientColumn,step)+array(start,ndmin=2).T
    if retstep:
        return (samples,step)
    else:
        return samples

Would it be possible (without breaking linspace former behaviour) to incorporate this functionality into NumPy?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Jan 23, 2013

Not sure if its worth it (where do you add the new axis seems the biggest ambiguity), though unless someone already abused it for such a thing (you can with endpoint=False), I guess it cannot break anything.

In any case, you do not have to look so far, the current code basically supports it already, since it is just broadcasting really.

def linspace(start, stop, num=50, endpoint=True, retstep=False):
    # just pre-process start and stop a bit:
    start = np.asarray(start)[...,None]
    stop = np.asarray(stop)[...,None]
    num = int(num)
    if num <= 0:
        dtype = np.result_type(start, stop, 1.) # conserve dtype
        shape = np.broadcast(start, end).shape[:-1] + (0,)
        return np.empty(shape, dtype=dtype)
    if endpoint:
        if num == 1:
            dtype = np.result_type(start, stop, 1.)
            return start.astype(dtype)
        step = (stop-start)/float((num-1))
        y = _nx.arange(0, num) * step + start
        y[...,-1] = stop[...,0]
    else:
        step = (stop-start)/float(num)
        y = _nx.arange(0, num) * step + start
    if retstep:
        return y, step
    else:
        return y

should do the trick (as you see, mostly reshape at the start and fixing of the y assignment and the special cases, and some other smaller unrelated changes I smuggled in).

0reactions
mhvkcommented, Mar 22, 2019

Implemented in #12388, so closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

np.linspace(): Create Evenly or Non-Evenly Spaced Arrays
This code returns an ndarray with equally spaced intervals between the start and stop values. This is a vector space, also called a...
Read more >
How to Use the Numpy Linspace Function - Sharp Sight
The NumPy linspace function creates sequences of evenly spaced values within a defined interval. Essentally, you specify a starting point and an ...
Read more >
numpy.linspace — NumPy v1.24 Manual
Return evenly spaced numbers over a specified interval. Returns num evenly spaced samples, calculated over the interval [start, stop]. The endpoint of the ......
Read more >
FAQ - MATLAB Wiki - Fandom
Matrices with a ragged right edge are not allowed, unless you use a cell array. For example, this code will demonstrate the error:...
Read more >
Exploring NumPy's linspace() function | by Ahmed Gad
What is np. · Function Arguments (start, stop, num) · Create 1D Arrays · Possible Values for the num Argument · Use case:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found