ENH: allow linspace to take 1D vectors as start and end
See original GitHub issueI 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:
- Created 11 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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).Implemented in #12388, so closing.