rray_insert_axes() inserts new axes with a dimension of 1.
Details
axes refer to axes of the result, not axes of x. Inserting 2 axes into
an array with a dimensionality of 3 gives a result with a dimensionality of
5 (2 + 3), so axes can be any value between 1 and 5.
To work out the result, write out the axes of the result and mark the ones
listed in axes with 1. Then use the dimensions of x to fill in the rest
in order.
This allows you to insert two axes side by side:
Inserted axes have no names. The axes of x keep their names and carry them
to their new locations.
Examples
x <- array(1:24, c(2, 3, 4))
# Insert one axis in the middle
# (2, 3, 4) -> (2, 1, 3, 4)
rray_dimensions(rray_insert_axes(x, 2))
#> [1] 2 1 3 4
# Insert at the front and at the back
# (2, 3, 4) -> (1, 2, 3, 4, 1)
rray_dimensions(rray_insert_axes(x, c(1, 5)))
#> [1] 1 2 3 4 1
# Two new axes side by side
# (2, 3, 4) -> (2, 1, 1, 3, 4)
rray_dimensions(rray_insert_axes(x, c(2, 3)))
#> [1] 2 1 1 3 4
# Inserting no axes returns `x` unchanged
rray_insert_axes(x, integer())
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 13 15 17
#> [2,] 14 16 18
#>
#> , , 4
#>
#> [,1] [,2] [,3]
#> [1,] 19 21 23
#> [2,] 20 22 24
#>
# `rray_remove_axes()` undoes an insertion
rray_remove_axes(rray_insert_axes(x, 2), 2)
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 13 15 17
#> [2,] 14 16 18
#>
#> , , 4
#>
#> [,1] [,2] [,3]
#> [1,] 19 21 23
#> [2,] 20 22 24
#>
