Skip to contents

Creates a 1-D array with the values from start to end in steps of by, like R's seq(start, end, by). The sequence counts down when end lies below start, and stops before end when end 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(start, end, by = NULL, dtype = NULL, device = NULL)

nv_seq_like(like, start, end, by = NULL, dtype = NULL, device = NULL)

Arguments

start, end

(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 from start towards end. NULL (default) uses -1 if start > end and 1 otherwise.

dtype

(NULL | character(1) | DataType)
Data type of the result. Can be any numeric data type. NULL (default) uses the default integer data type (see default_dtypes()), since the values are whole. For nv_seq_like(), NULL uses dtype(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, or

  • a device object as returned by nv_device(): a PJRTDevice for the "pjrt" backend or a quickr_device for the "quickr" backend. Because a device object is backend-specific, it also determines the backend.

The default (NULL) uses default_device().

like

(AnvlArray)
Existing array whose attributes are used as defaults (only for nv_seq_like()).

Value

(arrayish)
Has dtype and shape (end - start) %/% 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} ] 

# `end` 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} ]