max#

ivy.max(x, /, *, axis=None, keepdims=False, out=None)[source]#

Calculate the maximum value of the input array x.

Note

When the number of elements over which to compute the maximum value is zero, the maximum value is implementation-defined. Specification-compliant libraries may choose to raise an error, return a sentinel value (e.g., if x is a floating-point input array, return NaN), or return the minimum possible value for the input array x data type (e.g., if x is a floating-point array, return -infinity).

Special Cases

For floating-point operands,

  • If x_i is NaN, the maximum value is NaN (i.e., NaN values propagate).

Parameters:
  • x (Union[Array, NativeArray]) – input array. Should have a numeric data type.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis or axes along which maximum values must be computed. By default, the maximum value must be computed over the entire array. If a tuple of integers, maximum values must be computed over multiple axes. Default: None.

  • keepdims (bool, default: False) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default: False.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – if the maximum value was computed over the entire array, a zero-dimensional array containing the maximum value; otherwise, a non-zero-dimensional array containing the maximum values. The returned array must have the same data type as x.

This method 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([1, 2, 3])
>>> z = ivy.max(x)
>>> print(z)
ivy.array(3)
>>> x = ivy.array([0, 1, 2])
>>> z = ivy.array(0)
>>> y = ivy.max(x, out=z)
>>> print(z)
ivy.array(2)
>>> x = ivy.array([[0, 1, 2], [4, 6, 10]])
>>> y = ivy.max(x, axis=0, keepdims=True)
>>> print(y)
ivy.array([[4, 6, 10]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> y = ivy.max(x)
>>> print(y)
{
    a: ivy.array(2.),
    b: ivy.array(5.)
}
>>> x = ivy.Container(a=ivy.array([[1, 2, 3],[-1,0,2]]),
...                   b=ivy.array([[2, 3, 4], [0, 1, 2]]))
>>> z = ivy.max(x, axis=1)
>>> print(z)
{
    a: ivy.array([3, 2]),
    b: ivy.array([4, 2])
}
Array.max(self, /, *, axis=None, keepdims=False, out=None)[source]#

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

Parameters:
  • x – input array. Should have a numeric data type.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis or axes along which maximum values must be computed. By default, the maximum value must be computed over the entire array. If a tuple of integers, maximum values must be computed over multiple axes. Default: None.

  • keepdims (bool, default: False) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default: False.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – if the maximum value was computed over the entire array, a zero-dimensional array containing the maximum value; otherwise, a non-zero-dimensional array containing the maximum values. The returned array must have the same data type as x.

Examples

With ivy.Array input:

>>> x = ivy.array([1, 2, 3])
>>> z = x.max()
>>> print(z)
ivy.array(3)
>>> x = ivy.array([0, 1, 2])
>>> z = ivy.array(0)
>>> y = x.max(out=z)
>>> print(z)
ivy.array(2)
>>> x = ivy.array([[0, 1, 2], [4, 6, 10]])
>>> y = x.max(axis=0, keepdims=True)
>>> print(y)
ivy.array([[4, 6, 10]])
Container.max(self, /, *, axis=None, keepdims=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) – Input container. Should have a real-valued data type.

  • axis (Optional[Union[int, Sequence[int], Container]], default: None) – axis or axes along which max values must be computed. By default, the maximum value must be computed over the entire array. If a tuple of integers, maximum values must be computed over multiple axes. Default: None.

  • keepdims (Union[bool, Container], default: False) – optional boolean, if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default: False.

  • out (Optional[Container], default: None) – optional output array, for writing the result to.

Return type:

Container

Returns:

ret – if the maximum value was computed over the entire array, a zero-dimensional array containing the maximum value; otherwise, a non-zero-dimensional array containing the maximum values. The returned array must have the same data type as x.

Examples

With ivy.Container input:

>> > x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.array([2, 3, 4])) >> > z = x.max() >> > print(z) {

a: ivy.array(3), b: ivy.array(4)

} >>> x = ivy.Container(a=ivy.array([[1, 2, 3],[-1,0,2]]), … b=ivy.array([[2, 3, 4], [0, 1, 2]])) >>> z = x.max(axis=1) >>> print(z) {

a: ivy.array([3, 2]), b: ivy.array([4, 2])

}