Skip to contents

Creates a 1-D array with steps evenly spaced values from start to end (both inclusive), like R's seq(start, end, length.out = steps).

The spacing (end - start) / (steps - 1) is generally not a whole number, so the result is a float.

nv_linspace_like() is a variant where dtype and device default to those of like.

Usage

nv_linspace(start, end, steps, dtype = NULL, device = NULL)

nv_linspace_like(like, start, end, steps, dtype = NULL, device = NULL)

Arguments

start, end

(numeric(1))
First and last value of the sequence. end may lie below start, in which case the values decrease.

steps

(integer(1))
Number of values to generate. Must be at least 1; for steps = 1 the result is start.

dtype

(NULL | character(1) | DataType)
Data type of the result. Must be a float data type; NULL (default) uses the default float data type (see default_dtypes()), since the spacing is fractional. For nv_linspace_like(), NULL uses dtype(like), which must then be a float too.

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_linspace_like()).

Value

(arrayish)
Has dtype and shape steps.

See also

nv_seq() for consecutive integers, nv_iota() for values increasing along an axis of any shape, dtypes for the data type categories.

Examples

nv_linspace(0, 1, steps = 5L)
#> AnvlArray
#>  0.0000
#>  0.2500
#>  0.5000
#>  0.7500
#>  1.0000
#> [ CPUf32{5} ] 

# end below start counts down
nv_linspace(1, 0, steps = 3L)
#> AnvlArray
#>  1.0000
#>  0.5000
#>  0.0000
#> [ CPUf32{3} ] 

# steps = 1 gives start alone
nv_linspace(2.5, 10, steps = 1L)
#> AnvlArray
#>  2.5000
#> [ CPUf32{1} ] 

# the data type must be a float; convert afterwards for integers
nv_convert(nv_linspace(0, 10, steps = 5L), "i32")
#> AnvlArray
#>   0
#>   2
#>   5
#>   7
#>  10
#> [ CPUi32{5} ] 

# nv_linspace_like() takes the data type and device from an existing array
x <- nv_array(c(1, 2, 3), dtype = "f64")
nv_linspace_like(x, 0, 1, steps = 3L)
#> AnvlArray
#>  0.0000
#>  0.5000
#>  1.0000
#> [ CPUf64{3} ]