Like the OP's this use of linspace
assumes the start is 0 for all rows.
x=np.linspace(0,1,N)[:,None]*np.arange(0,2*N,2)
(edit - this is the transpose of what I should get; either transpose it or switch the use of [:,None]
)
For N=3000, it's noticeably faster than @Divaker's
solution. I'm not entirely sure why.
In [132]: timeit N=3000;x=np.linspace(0,1,N)[:,None]*np.arange(0,2*N,2)
10 loops, best of 3: 91.7 ms per loop
In [133]: timeit create_ranges(np.zeros(N),np.arange(0,2*N,2),N)
1 loop, best of 3: 197 ms per loop
In [134]: def foo(N):
...: D=np.ones((N,N))*np.arange(N)
...: D=D/D[:,-1]
...: W=np.arange(0,2*N,2)
...: return (D.T*W).T
...:
In [135]: timeit foo(3000)
1 loop, best of 3: 454 ms per loop
============
With starts and stops I could use:
In [201]: starts=np.array([1,4,2]); stops=np.array([6,7,8])
In [202]: x=(np.linspace(0,1,5)[:,None]*(stops-starts)+starts).T
In [203]: x
Out[203]:
array([[ 1. , 2.25, 3.5 , 4.75, 6. ],
[ 4. , 4.75, 5.5 , 6.25, 7. ],
[ 2. , 3.5 , 5. , 6.5 , 8. ]])
With the extra calculations that makes it a bit slower than create_ranges
.
In [208]: timeit N=3000;starts=np.zeros(N);stops=np.arange(0,2*N,2);x=(np.linspace(0,1,N)[:,None]*(stops-starts)+starts).T
1 loop, best of 3: 227 ms per loop
All these solutions are just variations the idea of doing a linear interpolation between the starts
and stops
.
pd.Series(W).apply(lambda e: np.linspace(0, e, 3))
– Lamentation