stack#

ivy.stack(arrays, /, *, axis=0, out=None)[source]#

Join a sequence of arrays along a new axis.

Parameters:
  • arrays (Union[Tuple[Union[Array, NativeArray], ...], List[Union[Array, NativeArray]]]) – input arrays to join. Each array must have the same shape.

  • axis (int, default: 0) – axis along which the arrays will be joined. Providing an axis specifies the index of the new axis in the dimensions of the result. For example, if axis is 0, the new axis will be the first dimension and the output array will have shape (N, A, B, C); if axis is 1, the new axis will be the second dimension and the output array will have shape (A, N, B, C); and, if axis is -1, the new axis will be the last dimension and the output array will have shape (A, B, C, N). A valid axis must be on the interval [-N, N), where N is the rank (number of dimensions) of x. If provided an axis outside of the required interval, the function must raise an exception. Default: 0.

  • 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 – an output array having rank N+1, where N is the rank (number of dimensions) of x. If the input arrays have different data types, normal ref:type-promotion must apply. If the input arrays have the same data type, the output array must have the same data type as the input arrays. .. note:

This specification leaves type promotion between data type families (i.e.,
intxx and floatxx) unspecified.

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

With :code: ivy.Array input:

>>> x = ivy.array([0., 1., 2., 3., 4.])
>>> y = ivy.array([6.,7.,8.,9.,10.])
>>> ivy.stack((x,y))
ivy.array([[ 0.,  1.,  2.,  3.,  4.],
    [ 6.,  7.,  8.,  9., 10.]])

With :code: ivy.Array input and different axis :

>>> ivy.stack((x,y),axis=1)
ivy.array([[ 0.,  6.],
    [ 1.,  7.],
    [ 2.,  8.],
    [ 3.,  9.],
    [ 4., 10.]])
Array.stack(self, /, arrays, *, axis=0, out=None)[source]#

ivy.Array instance method variant of ivy.stack. This method simply wraps the function, and so the docstring for ivy.stack also applies to this method with minimal changes.

Parameters:
  • self (Array) – Array to join with other arrays.

  • arrays (Union[Tuple[Union[Array, NativeArray]], List[Union[Array, NativeArray]]]) – Other arrays to join with. Each array must have the same shape.

  • axis (int, default: 0) – axis along which the arrays will be joined. More details can be found in the ivy.stack documentation.

  • 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 – output array made by joining the input arrays along the specified axis.

Examples

>>> x = ivy.array([1, 2])
>>> y = ivy.array([5, 6])
>>> print(x.stack(y, axis=1))
ivy.array([[1, 5],
        [2, 6]])
>>> x.stack([y],axis=0)
ivy.array([[[1, 2]],
        [[5, 6]]])
Container.stack(self, /, xs, *, axis=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.stack. This method simply wraps the function, and so the docstring for ivy.stack also applies to this method with minimal changes.

Parameters:
  • self (Container) –

    Container with leaves to join with leaves of other arrays/containers.

    Each array leave must have the same shape.

  • xs (Union[Tuple[Union[Array, NativeArray, Container]], List[Union[Array, NativeArray, Container]]]) – Container with other leaves to join. Each array leave must have the same shape.

  • axis (Union[int, Container], default: 0) – axis along which the array leaves will be joined. More details can be found in the docstring for ivy.stack.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

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

Return type:

Container

Returns:

ret – an output container with the results.

Examples

>>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]]))
>>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]]))
>>> x.stack([y])
{
    a: ivy.array([[[0, 1],
                [2, 3]],
                [[3, 2],
                [1, 0]]]),
    b: ivy.array([[[4, 5]],
                [[1, 0]]])
}