arange#

ivy.arange(start, /, stop=None, step=1, *, dtype=None, device=None, out=None)[source]#

Return evenly spaced values within a given interval, with the spacing being specified.

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an array in the chosen ml_framework rather than a list.

See \(linspace\) for a certain number of evenly spaced values in an interval.

Parameters:
  • start (Number) – if stop is specified, the start of interval (inclusive); otherwise, the end of the interval (exclusive). If stop is not specified, the default starting value is 0.

  • stop (Optional[Number], default: None) – the end of the interval. Default: None.

  • step (Number, default: 1) – the distance between two adjacent elements (out[i+1] - out[i]). Must not be 0; may be negative, this results in an empty array if stop >= start. Default: 1.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type must be inferred from start, stop and step. If those are all integers, the output array dtype must be the default integer dtype; if one or more have type float, then the output array dtype must be the default floating-point data type. Default: None.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to place the created array. Default: None.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – a one-dimensional array containing evenly spaced values. The length of the output array must be ceil((stop-start)/step) if stop - start and step have the same sign, and length 0 otherwise.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

>>> stop = 5
>>> x = ivy.arange(stop)
>>> print(x)
ivy.array([0, 1, 2, 3, 4])
>>> start = 1
>>> stop = 5
>>> x = ivy.arange(start, stop)
>>> print(x)
ivy.array([1, 2, 3, 4])
>>> start = 1
>>> stop = 10
>>> step = 2
>>> x = ivy.arange(start, stop, step)
>>> print(x)
ivy.array([1, 3, 5, 7, 9])
>>> start = 1
>>> stop = 10
>>> step = 2
>>> dtype = "float64"
>>> device = "cpu"
>>> x = ivy.arange(start, stop, step, dtype=dtype, device=device)
>>> print(x, x.dtype, x.device)
ivy.array([1., 3., 5., 7., 9.]) float64 cpu