[Python] Arbitrary Stencil Slicing in Numpy

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a simple syntax for creating references to an arbitrary number of neighbouring array elements in numpy?

    The syntax is relatively straightforward when the number of neighbours is hard-coded. A stencil width of three for example is

    import numpy as np

    x = np.arange(8)

    # Hard-coded stencil width of 3
    x_neighbours = (
    x[ :-2]
    x[ 1:-1]
    x[ 2: ]
    )



    However, my attempt at arbitrary width stencils is not particularly readable


    nStencil = 3
    x_neighbours = (
    x[indexStart:indexStop]
    for indexStart, indexStop in zip(
    (None, *range(1,nStencil)),
    (*range(1-nStencil,0), None),
    )
    )



    Is there a better approach?

    Continue reading...

Compartilhe esta Página