Skip to content

Commit

Permalink
feat: reformatted log2 function (ivy-llc#26318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Array22 authored Sep 30, 2023
1 parent 5064877 commit 3b53c67
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ivy/data_classes/array/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,27 @@ def log2(self: ivy.Array, *, out: Optional[ivy.Array] = None) -> ivy.Array:
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 :ref:`type-promotion`.
Examples
--------
Using :code:`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]])
"""
return ivy.log2(self._data, out=out)

Expand Down
30 changes: 30 additions & 0 deletions ivy/data_classes/container/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -5167,6 +5167,21 @@ def _static_log2(
a container containing the evaluated base ``2`` logarithm for
each element in ``x``. The returned array must have a real-valued
floating-point data type determined by :ref:`type-promotion`.
Examples
--------
Using :code:`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.Container.static_log2(x)
>>> print(y)
{
a: ivy.array([-inf, nan]),
b: ivy.array([-inf, nan, inf]),
c: ivy.array([3.15, 1.07, 0.])
}
"""
return ContainerBase.cont_multi_map_in_function(
"log2",
Expand Down Expand Up @@ -5217,6 +5232,21 @@ def log2(
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 :ref:`type-promotion`.
Examples
--------
Using :code:`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.])
}
"""
return self._static_log2(
self,
Expand Down
31 changes: 31 additions & 0 deletions ivy/functional/ivy/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -4246,6 +4246,37 @@ def log2(
Both the description and the type hints above assumes an array input for simplicity,
but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
instances in place of any of the arguments.
Examples
--------
With :class:`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 :class:`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.])
}
"""
return ivy.current_backend(x).log2(x, out=out)

Expand Down

0 comments on commit 3b53c67

Please sign in to comment.