Skip to contents

Reshapes an array to a new shape using col-major semantics.

Usage

nv_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 given shape and the same data type as x.

See also

prim_reshape() for the underlying primitive.

Examples

# the elements keep their column-major order; the data type is untouched
x <- array(1:6, dim = c(3, 2))
nv_reshape(x, 6L)
#> AnvlArray
#>  1
#>  2
#>  3
#>  4
#>  5
#>  6
#> [ CPUi32{6} ] 
# the order base R reads them in, too
c(x)
#> [1] 1 2 3 4 5 6

# infer the size of the second axis
nv_reshape(x, c(2, -1))
#> AnvlArray
#>  1 3 5
#>  2 4 6
#> [ CPUi32{2,3} ] 
# flatten
nv_reshape(x, -1)
#> AnvlArray
#>  1
#>  2
#>  3
#>  4
#>  5
#>  6
#> [ CPUi32{6} ]