prod#

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

Calculate the product of input array x elements.

Special Cases

Let N equal the number of elements over which to compute the product.

  • If N is 0, the product is 1 (i.e., the empty product).

For both both real-valued and complex floating-point operands, special cases must be handled as the operation is implemented by successive application of ivy.multiply():

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 products must be computed. By default, the product must be computed over the entire array. If a tuple of integers, products must be computed over multiple axes. Default: None.

  • keepdims (bool, default: False) – bool, 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.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – data type of the returned array. If None, if the default data type corresponding to the data type “kind” (integer or floating-point) of x has a smaller range of values than the data type of x (e.g., x has data type int64 and the default data type is int32, or x has data type uint64 and the default data type is int64), the returned array must have the same data type as x. if x has a floating-point data type, the returned array must have the default floating-point data type. if x has a signed integer data type (e.g., int16), the returned array must have the default integer data type. if x has an unsigned integer data type (e.g., uint16), the returned array must have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is int32, the returned array must have a uint32 data type). If the data type (either specified or resolved) differs from the data type of x, the input array should be cast to the specified data type before computing the product. Default: None.

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

Return type:

Array

Returns:

ret – array, if the product was computed over the entire array, a zero-dimensional array containing the product; otherwise, a non-zero-dimensional array containing the products. The returned array must have a data type as described by the dtype parameter above.

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.prod(x)
>>> print(z)
ivy.array(6)
>>> x = ivy.array([1, 0, 3])
>>> z = ivy.prod(x)
>>> print(z)
ivy.array(0)
>>> x = ivy.array([[3., 4., 5.]])
>>> y = ivy.prod(x, keepdims=True)
>>> print(y)
ivy.array([60.])
>>> x = ivy.array([2., 1.])
>>> y = ivy.array(0.)
>>> ivy.prod(x, out=y)
>>> print(y)
ivy.array(2.)
>>> x = ivy.array([[-1., -2.], [3., 3.]])
>>> y = ivy.prod(x, axis=1)
>>> print(y)
ivy.array([2., 9.])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1., 0., 1.]), b=ivy.array([1.1, 0.2, 1.4]))
>>> y = ivy.prod(x)
>>> print(y)
{
    a: ivy.array(-0.),
    b: ivy.array(0.30800003)
}
>>> x = ivy.Container(a=ivy.array([[1., 2.], [3., 4.]]),
...                   b=ivy.array([[ 4., 5.], [5., 6.]]))
>>> y = ivy.prod(x, axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[2.],
                  [12.]]),
    b: ivy.array([[20.],
                  [30.]])
}
Array.prod(self, /, *, axis=None, keepdims=False, dtype=None, out=None)[source]#

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

Parameters:
  • self (Array) – input array. Should have a floating-point data type.

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

  • keepdims (bool, default: False) – bool, 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.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – data type of the returned array.

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

Return type:

Array

Returns:

ret – container, if the product was computed over the entire array, a zero-dimensional array containing the product; otherwise, a non-zero-dimensional array containing the products. The returned array must have the same data type as self.

Examples

With: class: ivy.Array input:

>>> x = ivy.array([1, 2, 3])
>>> z = x.prod()
>>> print(z)
ivy.array(6)
>>> x = ivy.array([1, 0, 3])
>>> z = x.prod()
>>> print(z)
ivy.array(0)
>>> x = ivy.array([[3., 4., 5.]])
>>> y = x.prod(axis=1)
>>> print(y)
ivy.array([60.])
>>> x = ivy.array([2., 1.])
>>> y = ivy.array(0.)
>>> x.prod(out=y)
>>> print(y)
ivy.array(2.)
>>> x = ivy.array([[-1., -2.], [3., 3.]])
>>> y = x.prod(axis=1)
>>> print(y)
ivy.array([2., 9.])
Container.prod(self, /, *, axis=None, dtype=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.prod. This method simply wraps the function, and so the docstring for ivy.prod also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container. Should have a floating-point data type.

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

  • keepdims (Union[bool, Container], default: False) – bool, 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.

  • dtype (Optional[Union[Dtype, NativeDtype, Container]], default: None) – data type of the returned array.

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

  • 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 output, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – container, if the product was computed over the entire array, a zero-dimensional array containing the product; otherwise, a non-zero-dimensional array containing the products. The returned array must have the same data type as self.

Examples

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> y = x.prod()
>>> print(y)
{
    a: ivy.array(0.),
    b: ivy.array(60.)
}
>>> x = ivy.Container(a=ivy.array([0.1, 1.1]), b=ivy.array([0.1, 1.1, 2.1]))
>>> y = x.prod(keepdims=True)
>>> print(y)
{
    a: ivy.array([0.11000001]),
    b: ivy.array([0.23100001])
}
>>> x = ivy.Container(a=ivy.array([[2, 1]]), b=ivy.array([[2, 3]]))
>>> y = x.prod(axis=1, keepdims=True)
>>> print(y)
{
    a: ivy.array([[2]]),
    b: ivy.array([[6]])
}
>>> x = ivy.Container(a=ivy.array([-1, 0, 1]), b=ivy.array([1.1, 0.2, 1.4]))
>>> y = ivy.Container(a=ivy.array(0.), b=ivy.array(0.))
>>> x.prod(out=y)
>>> print(y)
{
    a: ivy.array(0),
    b: ivy.array(0.30800003)
}
>>> x = ivy.Container(a=ivy.array([0., -1., 1.]), b=ivy.array([1., 1., 1.]))
>>> y = ivy.Container(a=ivy.array(0.), b=ivy.array(0.))
>>> x.prod(out=y)
>>> print(y)
{
    a: ivy.array(-0.),
    b: ivy.array(1.)
}
>>> x = ivy.Container(a=ivy.array([[0., 1., 2.], [3., 4., 5.]]),
...                   b=ivy.array([[3., 4., 5.], [6., 7., 8.]]))
>>> y = ivy.Container(a=ivy.zeros(3), b=ivy.zeros(3))
>>> x.prod(axis=0, out=y)
>>> print(y)
{
    a: ivy.array([0., 4., 10.]),
    b: ivy.array([18., 28., 40.])
}
>>> x = ivy.Container(a=ivy.array([[1., 1., 1.], [2., 2., 2.]]),
...                   b=ivy.array([[3., 3., 3.], [4., 4., 4.]]))
>>> y = x.prod(axis=1)
>>> print(y)
{
    a: ivy.array([1., 8.]),
    b: ivy.array([27., 64.])
}