Skip to contents

Reshapes an array to a new shape without changing the underlying data. The elements keep their column-major order, exactly as base R's dim<- does.

Usage

prim_reshape(x, shape)

Arguments

x

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

shape

(integer())
Target shape. Must have the same number of elements as x. At most one entry may be -1, in which case its extent is inferred from the remaining entries and the number of elements of x.

Value

(arrayish)
Has the same data type as the input and the given shape.

Implemented Rules

  • stablehlo

  • quickr

  • reverse

StableHLO

Lowers to hlo_reshape(), specified under reshape. The lowering wraps it in two hlo_transpose() calls that reverse every axis, which is what turns stablehlo's row-major reshape into a column-major one; each is skipped where that side has at most one axis.

See also

Examples

# the elements keep their column-major order; the data type is untouched
x <- nv_array(1:6)
prim_reshape(x, shape = c(2, 3))
#> AnvlArray
#>  1 3 5
#>  2 4 6
#> [ CPUi32{2,3} ]