Updates elements of an array at specified positions, returning a new array.
You can also use the [<- operator.
Usage
# S3 method for class 'AnvlArray'
x[...] <- value
nv_subset_assign(x, ..., value, update_fn = "replace", inplace = FALSE)Arguments
- x
(
arrayish)
The array to update. Can be any data type. An R object is materialized at its default data type.- ...
Subset specifications, one per axis. Omitted trailing axes select all elements.
A boolean mask (an R logical array such as
arr(TRUE, FALSE), or an arrayish value of dtypebool) selects the elements at theTRUEpositions. A mask for one axis must have as many elements as the size of that axis. A mask that is the only subscript and has the same shape asxselects across the whole array, yielding a 1-D result. Underjit(), the values of a mask must be known at compile time, because the number of selected elements determines the output shape: R logical arrays and arrays created in or closed over by the function work, a mask computed from the function's inputs does not.See the Subsetting article for details.
- value
(
arrayish)
Replacement values, or with anotherupdate_fnthe values to combine the subset with. Scalars are broadcast to the subset shape and non-scalar values must match it. It is brought tox's data type: an R value is built at it when its category can reach it (0Lserves an integer and a floatxalike, while1.5into ani32xis an error), and an array is converted unless that would narrow it or leave its category (anf64value for anf32xis an error).- update_fn
(
character(1)|function)
Howvaluecombines with the subset ofx:"replace"(the default):valuereplaces it,"add","sub","mul": it becomes the subset plus, minus or timesvalue,"max","min": it becomes the larger or the smaller of the two,
or a binary function
f(old, new)of two scalars, seeprim_scatter(). Unlikex[i] <- x[i] + valuein R, a position thatiselects more than once gets every one of its values: with"add",x[array(c(1L, 1L))]adds both tox[1].gradient()differentiates the names, but not a function; with"mul", not with respect tovaluewhereiselects several positions of an axis by an index array or a mask, since those may repeat. With[<-, pass it among the subscripts, e.g.x[1, update_fn = "add"] <- 1.- inplace
(
logical(1))
Whether to write into the memory ofxinstead of allocating a new array. This avoids the copy ofxthat an eager subset assignment otherwise makes, but it consumesx: its buffer is donated, sox– and any other R variable referring to the same array – can no longer be used afterwards. With[<-, pass it among the subscripts, e.g.x[1, inplace = TRUE] <- 0, which rebindsxto the result. It is an error inside a jitted function: there, other references toxwould stay valid, so the same code would behave differently in eager and in jit mode. Insidejit(), the compiler already avoids the copy anyway. Default isFALSE.
Value
(arrayish)
Has x's data type and shape, with the subset replaced (or combined with
value as update_fn says).
See also
nv_subset(), the Subsetting article for a
comprehensive guide.
Examples
x <- nv_matrix(1:12, nrow = 3)
# set row 1 to zeros
nv_subset_assign(x, 1, value = nv_scalar(0L))
#> AnvlArray
#> 0 0 0 0
#> 2 5 8 11
#> 3 6 9 12
#> [ CPUi32{3,4} ]
x[1, ] <- nv_scalar(0L)
x
#> AnvlArray
#> 0 0 0 0
#> 2 5 8 11
#> 3 6 9 12
#> [ CPUi32{3,4} ]
# Zero out every element greater than 6 (not in `jit()`, see `nv_subset()`)
x[x > 6] <- 0L
x
#> AnvlArray
#> 0 0 0 0
#> 2 5 0 0
#> 3 6 0 0
#> [ CPUi32{3,4} ]
# add to the subset instead of replacing it; a repeated index adds twice
nv_subset_assign(x, array(c(1L, 1L)), 1, value = nv_scalar(10L), update_fn = "add")
#> AnvlArray
#> 20 0 0 0
#> 2 5 0 0
#> 3 6 0 0
#> [ CPUi32{3,4} ]
# write into the memory of `x` instead of copying it
x[2, inplace = TRUE] <- nv_scalar(1L)
x
#> AnvlArray
#> 0 0 0 0
#> 1 1 1 1
#> 3 6 0 0
#> [ CPUi32{3,4} ]