rougier / from-python-to-numpy

An open-access book on numpy vectorization techniques, Nicolas P. Rougier, 2017
http://www.labri.fr/perso/nrougier/from-python-to-numpy
Other
2.03k stars 339 forks source link

4.2. Uniform vectorization/Python implementation bug #89

Closed vladislavneon closed 4 years ago

vladislavneon commented 4 years ago

There's a shape inconsistency in compute_neighbours function:

def compute_neighbours(Z):
    shape = len(Z), len(Z[0])
    N  = [[0,]*(shape[0]) for i in range(shape[1])]
    for x in range(1,shape[0]-1):
        for y in range(1,shape[1]-1):
            N[x][y] = Z[x-1][y-1]+Z[x][y-1]+Z[x+1][y-1] \
                    + Z[x-1][y]            +Z[x+1][y]   \
                    + Z[x-1][y+1]+Z[x][y+1]+Z[x+1][y+1]
    return N

Shape of N is currently a transposed shape of Z, which would result in index of out range exception in case of non-square field.

Not critical for the example, but a bit confusing anyway.

rougier commented 4 years ago

Good point.