log2#

ivy.log2(x, /, *, out=None)[source]#

Calculate an implementation-dependent approximation to the base 2 logarithm, having domain [0, +infinity] and codomain [-infinity, +infinity], for each element x_i of the input array x.

Special cases

For floating-point operands,

  • If x_i is NaN, the result is NaN.

  • If x_i is less than 0, the result is NaN.

  • If x_i is either +0 or -0, the result is -infinity.

  • If x_i is 1, the result is +0.

  • If x_i is +infinity, the result is +infinity.

For complex floating-point operands, special cases must be handled as if the operation is implemented using the standard change of base formula

\[\log_{2} x = \frac{\log_{e} x}{\log_{e} 2}\]

where \(\log_{e}\) is the natural logarithm.

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

  • 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 array containing the evaluated base 2 logarithm for each element in x. The returned array must have a floating-point data type determined by type-promotion.

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([5.0, 1, -0.0, -6.0]) >>> y = ivy.log2(x) >>> print(y) ivy.array([2.32, 0., -inf, nan]) >>> x = ivy.array([[float(‘nan’), 1, 6.0, float(‘+inf’)], … [+0, -2.0, -7, float(‘-inf’)]]) >>> y = ivy.empty_like(x) >>> ivy.log2(x, out=y) >>> print(y) ivy.array([[nan, 0., 2.58, inf],[-inf, nan, nan, nan]]) >>> x = ivy.array([[float(‘nan’), 1, 7.0, float(‘+inf’)], … [+0, -3.0, -8, float(‘-inf’)]]) >>> ivy.log2(x, out=x) >>> print(x) ivy.array([[nan, 0., 2.81, inf],[-inf, nan, nan, nan]])

With ivy.Container input: >>> x = ivy.Container(a=ivy.array([0.0, float(‘nan’)]), … b=ivy.array([-0., -4.9, float(‘+inf’)]), … c=ivy.array([8.9, 2.1, 1.])) >>> y = ivy.log2(x) >>> print(y) {

a: ivy.array([-inf, nan]), b: ivy.array([-inf, nan, inf]), c: ivy.array([3.15, 1.07, 0.])

}

Array.log2(self, *, out=None)[source]#

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

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

  • 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 array containing the evaluated base 2 logarithm for each element in self. The returned array must have a real-valued floating-point data type determined by type-promotion.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([5.0, 1, -0.0, -6.0])
>>> y = ivy.log2(x)
>>> print(y)
ivy.array([2.32, 0., -inf, nan])
>>> x = ivy.array([float('nan'), -5.0, -0.0, 1.0, 5.0, float('+inf')])
>>> y = x.log2()
>>> print(y)
ivy.array([nan, nan, -inf, 0., 2.32, inf])
>>> x = ivy.array([[float('nan'), 1, 5.0, float('+inf')],                            [+0, -2.0, -5, float('-inf')]])
>>> y = x.log2()
>>> print(y)
ivy.array([[nan, 0., 2.32, inf],
           [-inf, nan, nan, nan]])
Container.log2(self, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

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

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

Return type:

Container

Returns:

ret – a container containing the evaluated base 2 logarithm for each element in self. The returned array must have a real-valued floating-point data type determined by type-promotion.

Examples

Using ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([0.0, float('nan')]),
...                   b=ivy.array([-0., -5.9, float('+inf')]),
...                   c=ivy.array([8.9, 2.1, 1.]))
>>> y = ivy.log2(x)
>>> print(y)
{
    a: ivy.array([-inf, nan]),
    b: ivy.array([-inf, nan, inf]),
    c: ivy.array([3.15, 1.07, 0.])
}