Skip to content

Dynamic slice copy into statically-shaped array #17854

Answered by jakevdp
kc9jud asked this question in Q&A
Discussion options

You must be logged in to vote

You can't do this operation via standarding indexing assignments, because it would require constructing dynamically-shaped intermediate arrays. But you can do this operation using other approaches; for example this should work:

def extract_slice(a: Array, indices: Array, row: int, max_size: int):
    x = jnp.zeros(max_size)
    start = indices[row]
    stop = indices[row+1]
    count = stop - start
    a = jnp.roll(a, -start)[:max_size]  # move relevant entries to front
    return jnp.where(jnp.arange(max_size) < count, a, x)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kc9jud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants