Creates a 1-D array with the values from from to to in steps of by,
like R's seq(). The sequence counts down when to lies below from,
and stops before to when to is not reachable in whole steps:
nv_seq(0, 9, by = 2) ends at 8.
nv_seq_like() is a variant where dtype and device
default to those of like.
Usage
nv_seq(from, to, by = NULL, dtype = NULL, device = NULL)
nv_seq_like(like, from, to, by = NULL, dtype = NULL, device = NULL)Arguments
- from, to
(
integer(1))
First value and upper (or, when counting down, lower) limit of the sequence.- by
(
NULL|integer(1))
Step size, which must be a non-zero whole number pointing fromfromtowardsto.NULL(default) uses-1iffrom > toand1otherwise.- dtype
(
NULL|character(1)|DataType)
Data type of the result. Can be any numeric data type.NULL(default) uses the default integer data type (seedefault_dtypes()), since the values are whole. Fornv_seq_like(),NULLusesdtype(like).- device
(
NULL|character(1)| device)
The device the data lives on, given either as:a device string naming the platform (e.g.
"cpu","cuda","cuda:<n>"), which is resolved against the backend in use, ora device object as returned by
nv_device(): aPJRTDevicefor the"pjrt"backend or aquickr_devicefor the"quickr"backend. It must belong to the active backend (active_backend()); a device of another backend is an error.
The default (
NULL) usesdefault_device().- like
(
AnvlArray)
Existing array whose attributes are used as defaults (only fornv_seq_like()).
Value
(arrayish)
Has dtype and shape (to - from) %/% by + 1.
See also
nv_linspace() for a given number of evenly spaced values,
nv_iota() for values increasing along an axis of any shape,
prim_iota() for the underlying primitive.
Examples
nv_seq(3, 7)
#> AnvlArray
#> 3
#> 4
#> 5
#> 6
#> 7
#> [ CPUi32{5} ]
# a range that counts down needs no `by`
nv_seq(7, 3)
#> AnvlArray
#> 7
#> 6
#> 5
#> 4
#> 3
#> [ CPUi32{5} ]
# `to` is only reached where a whole number of steps lands on it
nv_seq(0, 9, by = 2)
#> AnvlArray
#> 0
#> 2
#> 4
#> 6
#> 8
#> [ CPUi32{5} ]
# a float data type gives the same values as floats
nv_seq(3, 7, dtype = "f32")
#> AnvlArray
#> 3
#> 4
#> 5
#> 6
#> 7
#> [ CPUf32{5} ]
# nv_seq_like() takes the data type and device from an existing array
x <- nv_array(c(1, 2, 3), dtype = "f64")
nv_seq_like(x, 1, 5)
#> AnvlArray
#> 1
#> 2
#> 3
#> 4
#> 5
#> [ CPUf64{5} ]