Skip to contents

Extracts a slice from an array using static (compile-time) indices. All indices and strides are fixed R integers, and both ends are inclusive.

Use prim_dynamic_slice() instead when the start position must be computed at runtime (e.g. depends on array values).

Usage

prim_static_slice(x, start_indices, end_indices, strides)

Arguments

x

(arrayish)
One input. Can be any data type. An R value materializes at its default data type.

start_indices

(integer())
Start indices (inclusive), one per axis. Must satisfy 1 <= start_indices <= end_indices + 1 per axis, where start_indices == end_indices + 1 selects an empty axis.

end_indices

(integer())
End indices (inclusive), one per axis. Must satisfy end_indices <= shape(x) per axis. Unlike StableHLO's exclusive limit_indices, the element at end_indices is part of the slice.

strides

(integer())
Step sizes, one per axis. Must be >= 1. A stride of 1 selects every element; a stride of 2 selects every other element, etc.

Value

(arrayish)
Has the same data type as the input and shape ceiling((end_indices - start_indices + 1) / strides).

Implemented Rules

  • stablehlo

  • quickr

  • reverse

StableHLO

Lowers to hlo_slice(), specified under slice.

Examples

# 1-D: extract elements 2 through 5, the end being inclusive
x <- nv_array(1:10)
prim_static_slice(x, start_indices = 2L, end_indices = 5L, strides = 1L)
#> AnvlArray
#>  2
#>  3
#>  4
#>  5
#> [ CPUi32{4} ] 

# 1-D: every other element using strides
x <- nv_array(1:10)
prim_static_slice(x, start_indices = 1L, end_indices = 10L, strides = 2L)
#> AnvlArray
#>  1
#>  3
#>  5
#>  7
#>  9
#> [ CPUi32{5} ] 

# 2-D: extract the submatrix of rows 1-3 and columns 2-4
x <- nv_matrix(1:12, nrow = 3, ncol = 4)
prim_static_slice(x,
  start_indices = c(1L, 2L),
  end_indices = c(3L, 4L),
  strides = c(1L, 1L)
)
#> AnvlArray
#>   4  7 10
#>   5  8 11
#>   6  9 12
#> [ CPUi32{3,3} ]