# [Numpy-discussion] an np.arange for arrays?

David Goldsmith d_l_goldsmith@yahoo....
Fri Jul 10 12:26:22 CDT 2009

```It's also 5 LOSC using linspace:

def interp_grid(x, y, nrows):
''' A linearly-interpolated (across rows) 2-D grid generator '''
## Put data quality checking here - confirm len(x) == len(y),
## nrows > 2, decide on behavior if inputs bad
ncols = len(x)
grid = np.zeros((nrows, ncols)) # allocate memory
for i in range(ncols):
grid[:,i] = np.linspace(x[i], y[i], nrows)
return grid

>>> nrows=10
>>> ncols = len(x)
>>> x; y; nrows, ncols
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array([10, 11, 12, 13, 14, 15, 16, 17, 18])
(10, 9)
>>> def interp_grid(x, y, nrows):
...     ncols = len(x)
...     grid = np.zeros((nrows, ncols)) # allocate memory
...     for i in range(ncols):
...         grid[:,i] = np.linspace(x[i], y[i], nrows)
...     return grid
...
>>> interp_grid(x, y, nrows)
array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
[  2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
[  3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.],
[  5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.],
[  6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.],
[  7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.],
[  8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.],
[  9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.],
[ 10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.]])

Or, if you prefer one-liners, using list comprehension:

grid = np.array([np.linspace(x[i],y[i],nrows) for i in range(len(x))]).T

(the transposing at the end is necessary 'cause the result of linspace is interpreted as a row vector, but you want the interpolated values in columns; if you can have your data be in the first and last columns and the interpolation be in the rows, you can omit the final .T)

>>> grid = np.array([np.linspace(x[i],y[i],nrows) for i in range(len(x))]).T
>>> grid
array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
[  2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.],
[  3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.],
[  4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.],
[  5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.],
[  6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.],
[  7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.],
[  8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.],
[  9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.],
[ 10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.]])

DG

--- On Fri, 7/10/09, Chris Colbert <sccolbert@gmail.com> wrote:

> From: Chris Colbert <sccolbert@gmail.com>
> Subject: Re: [Numpy-discussion] an np.arange for arrays?
> To: "Discussion of Numerical Python" <numpy-discussion@scipy.org>
> Date: Friday, July 10, 2009, 7:44 AM
> Oh cool, I couldn't figure out with
> mgrid.
> here's what ended up with using broadcasting:
>
> >>> import numpy as np
> >>> X = np.zeros((10))
> >>> Y = np.arange(10, 20)
> >>> M = 10
> >>> increments = np.arange(1, M+1)
> >>> delta = Y - X
> >>> dl = (delta / M).reshape(-1, 1)
> >>> interps = dl * increments
> >>> lines = X + interps
> >>> lines
> array([[  1. ,   2.
> ,   3. ,   4.
> ,   5. ,   6.
> ,   7. ,   8.
> ,   9. ,  10. ],
>        [
> 1.1,   2.2,   3.3,   4.4,   5.5,   6.6,   7.7,   8.8,   9.9,
> 11. ],
>        [
> 1.2,   2.4,   3.6,   4.8,   6.
> ,   7.2,   8.4,   9.6,
> 10.8,  12. ],
>        [
> 1.3,   2.6,   3.9,   5.2,   6.5,   7.8,   9.1,
> 10.4,  11.7,  13. ],
>        [
> 1.4,   2.8,   4.2,   5.6,   7.
> ,   8.4,   9.8,
> 11.2,  12.6,  14. ],
>        [
> 1.5,   3.
> ,   4.5,   6.
> ,   7.5,   9. ,
> 10.5,  12. ,  13.5,  15. ],
>        [
> 1.6,   3.2,   4.8,   6.4,   8.
> ,   9.6,  11.2,  12.8,
> 14.4,  16. ],
>        [
> 1.7,   3.4,   5.1,   6.8,   8.5,
> 10.2,  11.9,  13.6,  15.3,  17. ],
>        [
> 1.8,   3.6,   5.4,   7.2,   9.
> ,  10.8,  12.6,  14.4,  16.2,  18.
> ],
>        [
> 1.9,   3.8,   5.7,   7.6,   9.5,
> 11.4,  13.3,  15.2,  17.1,  19. ]])
>
>
>
>
> On Fri, Jul 10, 2009 at 4:49 AM, David Warde-Farley<dwf@cs.toronto.edu>
> wrote:
> >
> > On 10-Jul-09, at 1:25 AM, Chris Colbert wrote:
> >
> >> actually what would be better is if i can pass two
> 1d arrays X and Y
> >> both size Nx1
> >> and get back a 2d array of size NxM where the
> [n,:] row is the linear
> >> interpolation of X[n] to Y[n]
> >
> > This could be more efficient, but here's a solution
> using mgrid and
> >
> > def interp_grid_thingie(X, Y, M):
> >        frac = np.mgrid[0:1:M*1j]
> >        return (1 - frac[np.newaxis, :]) * X[:,
> np.newaxis] +
> > frac[np.newaxis, :] * Y[:, np.newaxis]
> >
> > In [11]: interp_grid_thingie(arange(1,6), arange(0,5),
> 5)
> > Out[11]:
> > array([[ 1.  ,  0.75,  0.5 ,  0.25,  0.  ],
> >        [ 2.  ,  1.75,  1.5 ,  1.25,  1.  ],
> >        [ 3.  ,  2.75,  2.5 ,  2.25,  2.  ],
> >        [ 4.  ,  3.75,  3.5 ,  3.25,  3.  ],
> >        [ 5.  ,  4.75,  4.5 ,  4.25,  4.
>  ]])
> >
> > David
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

```