reshape#

ivy.reshape(x, /, shape, *, copy=None, order='C', allowzero=True, out=None)[source]#

Give a new shape to an array without changing its data.

Parameters:
  • x (Union[Array, NativeArray]) – Input array to be reshaped.

  • shape (Union[Shape, NativeShape, Sequence[int]]) – a new shape compatible with the original shape. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.

  • order (str, default: 'C') – Read the elements of x using this index order, and place the elements into the reshaped array using this index order. C means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. F means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the C and F options take no account of the memory layout of the underlying array, and only refer to the order of indexing. Default order is C

  • allowzero (bool, default: True) – When allowzero=True, any value in the shape argument that is equal to zero, the zero value is honored. When allowzero=False, any value in the shape argument that is equal to zero the corresponding dimension value is copied from the input tensor dynamically. Default value is True.

  • 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 the same data type and elements as x.

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 ivy.Array input:

>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.reshape(x,(3,2))
>>> print(y)
ivy.array([[0., 1.],
           [2., 3.],
           [4., 5.]])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.reshape(x,(3,2), order='F')
>>> print(y)
ivy.array([[0., 4.],
           [3., 2.],
           [1., 5.]])

With ivy.NativeArray input:

>>> x = ivy.native_array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.reshape(x,(2,3))
>>> print(y)
ivy.array([[0., 1., 2.],
           [3., 4., 5.]])

With ivy.Container input:

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

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[0., 1., 2.]]), b=ivy.array([[3., 4., 5.]]))
>>> y = ivy.reshape(x, (-1, 1))
>>> print(y)
{
    a: ivy.array([[0.],[1.],[2.]]),
    b: ivy.array([[3.],[4.],[5.]])
}
Array.reshape(self, /, shape, *, copy=None, order='C', allowzero=True, out=None)[source]#

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

Parameters:
  • self (Array) – input array.

  • shape (Union[Shape, NativeShape, Sequence[int]]) – The new shape should be compatible with the original shape. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • order (str, default: 'C') – Read the elements of the input array using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. Default order is ‘C’

  • 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 the same data type as self and elements as self.

Examples

>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = x.reshape((3,2))
>>> print(y)
ivy.array([[0., 1.],
           [2., 3.],
           [4., 5.]])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = x.reshape((3,2), order='F')
>>> print(y)
ivy.array([[0., 4.],
           [3., 2.],
           [1., 5.]])
Container.reshape(self, /, shape, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, copy=None, order='C', allowzero=True, out=None)[source]#

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

Parameters:
  • self (Container) – input container.

  • shape (Union[Shape, NativeShape, Sequence[int], Container]) – The new shape should be compatible with the original shape. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

  • copy (Optional[Union[bool, Container]], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default: None.

  • 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.

  • copy – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default: None.

  • order (Union[str, Container], default: 'C') – Read the elements of the input container using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. Default order is ‘C’

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

Return type:

Container

Returns:

ret – an output container having the same data type as self and elements as self.

Examples

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