Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp2 extension #6197

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ivy/array/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,36 @@ def float_power(
ivy.array([1., 8., 27., 16., 5.])
"""
return ivy.float_power(self._data, x2, out=out)

def exp2(
self: Union[ivy.Array, float, list, tuple],
/,
*,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""ivy.Array instance method variant of ivy.exp2. This method simply
wraps the function, and so the docstring for ivy.exp2 also applies to
this method with minimal changes.

Parameters
----------
self
Array-like input.
out
optional output array, for writing the result to.

Returns
-------
ret
Element-wise 2 to the power x. This is a scalar if x is a scalar.

Examples
--------
>>> x = ivy.array([1, 2, 3])
>>> x.exp2()
ivy.array([2., 4., 8.])
>>> x = [5, 6, 7]
>>> x.exp2()
ivy.array([32., 64., 128.])
"""
return ivy.exp2(self._data, out=out)
84 changes: 84 additions & 0 deletions ivy/container/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,87 @@ def float_power(
}
"""
return self.static_float_power(self, x2, out=out)

@staticmethod
def static_exp2(
x: Union[ivy.Array, ivy.NativeArray, ivy.Container, float, list, tuple],
/,
*,
key_chains: Optional[Union[List[str], Dict[str, str]]] = None,
to_apply: bool = True,
prune_unapplied: bool = False,
map_sequences: bool = False,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.exp2. This method simply wraps
the function, and so the docstring for ivy.exp2 also applies to this
method with minimal changes.

Parameters
----------
x
container with the base input arrays.
out
optional output container, for writing the result to.

Returns
-------
ret
Container including arrays with element-wise 2 to the power
of input arrays elements.

Examples
--------
>>> x = ivy.Container(a=ivy.array([1, 2, 3]),\
b=[5, 6, 7])
>>> ivy.Container.static_exp2(x)
{
a: ivy.array([2., 4., 8.])
b: ivy.array([32., 64., 128.])
}
"""
return ContainerBase.multi_map_in_static_method(
"exp2",
x,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def exp2(
self: ivy.Container,
/,
*,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""ivy.Container instance method variant of ivy.exp2. This method simply
wraps the function, and so the docstring for ivy.exp2 also applies to
this method with minimal changes.

Parameters
----------
self
container with the base input arrays.
out
optional output container, for writing the result to.

Returns
-------
ret
Container including arrays with element-wise 2 to the power
of input array elements.

Examples
--------
>>> x = ivy.Container(a=ivy.array([1, 2, 3]),\
b=[5, 6, 7])
>>> x.exp2()
{
a: ivy.array([2., 4., 8.])
b: ivy.array([32., 64., 128.])
}
"""
return self.static_exp2(self, out=out)
9 changes: 9 additions & 0 deletions ivy/functional/backends/jax/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ def float_power(
out: Optional[JaxArray] = None,
) -> JaxArray:
return jnp.float_power(x1, x2)


def exp2(
x: Union[JaxArray, float, list, tuple],
/,
*,
out: Optional[JaxArray] = None,
) -> JaxArray:
return jnp.exp2(x)
12 changes: 12 additions & 0 deletions ivy/functional/backends/numpy/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,15 @@ def float_power(


float_power.support_native_out = True


def exp2(
x: Union[np.ndarray, float, list, tuple],
/,
*,
out: Optional[np.ndarray] = None,
) -> np.ndarray:
return np.exp2(x, out=out)


exp2.support_native_out = True
9 changes: 9 additions & 0 deletions ivy/functional/backends/tensorflow/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ def float_power(
out: Optional[Union[tf.Tensor, tf.Variable]] = None,
) -> Union[tf.Tensor, tf.Variable]:
return tf.experimental.numpy.float_power(x1, x2)


def exp2(
x: Union[tf.Tensor, tf.Variable, float, list, tuple],
/,
*,
out: Optional[Union[tf.Tensor, tf.Variable]] = None,
) -> Union[tf.Tensor, tf.Variable]:
return tf.math.pow(2, x, name=None)
12 changes: 12 additions & 0 deletions ivy/functional/backends/torch/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ def float_power(


float_power.support_native_out = True


def exp2(
x: Union[torch.Tensor, float, list, tuple],
/,
*,
out: Optional[torch.Tensor] = None,
) -> torch.Tensor:
return torch.exp2(x, out=out)


exp2.support_native_out = True
35 changes: 35 additions & 0 deletions ivy/functional/extensions/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,38 @@ def float_power(
ivy.array([1., 8., 27., 16., 5.])
"""
return ivy.current_backend().float_power(x1, x2, out=out)


@to_native_arrays_and_back
@handle_out_argument
@handle_nestable
def exp2(
x: Union[ivy.Array, float, list, tuple],
/,
*,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Calculate 2**p for all p in the input array.

Parameters
----------
x
Array-like input.
out
optional output array, for writing the result to.

Returns
-------
ret
Element-wise 2 to the power x. This is a scalar if x is a scalar.

Examples
--------
>>> x = ivy.array([1, 2, 3])
>>> ivy.exp2(x)
ivy.array([2., 4., 8.])
>>> x = [5, 6, 7]
>>> ivy.exp2(x)
ivy.array([32., 64., 128.])
"""
return ivy.current_backend().exp2(x, out=out)
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,42 @@ def test_float_power(
x1=np.asarray(x[0], dtype=input_dtype[0]),
x2=np.asarray(x[1], dtype=input_dtype[1])
)


# exp2
@handle_cmd_line_args
@given(
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=-10,
max_value=10,
min_num_dims=1,
max_num_dims=3,
min_dim_size=1,
max_dim_size=3,
),
num_positional_args=helpers.num_positional_args(fn_name="exp2"),
)
def test_exp2(
dtype_and_x,
with_out,
as_variable,
num_positional_args,
native_array,
container,
instance_method,
fw,
):
input_dtype, x = dtype_and_x
helpers.test_function(
input_dtypes=input_dtype,
as_variable_flags=as_variable,
with_out=with_out,
num_positional_args=num_positional_args,
native_array_flags=native_array,
container_flags=container,
instance_method=instance_method,
fw=fw,
fn_name="exp2",
x=np.asarray(x[0], dtype=input_dtype[0]),
)