Skip to content

Commit

Permalink
ivy-llc#15712-igamma
Browse files Browse the repository at this point in the history
  • Loading branch information
selvaraj-sembulingam committed May 20, 2023
1 parent c0d3e7e commit d925090
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 1 deletion.
40 changes: 40 additions & 0 deletions ivy/data_classes/array/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,43 @@ def bincount(
minlength=minlength,
out=out,
)

def igamma(
self: ivy.Array,
/,
*,
x: ivy.Array,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
ivy.Array instance method variant of ivy.igamma. This method simply wraps the
function, and so the docstring for ivy.igamma also applies to this method with
minimal changes.
Parameters
----------
self
Input array.
x
An additional input array.
`x` has the same type as `a`.
out
optional output array, for writing the result to.
Returns
-------
ret
The lower incomplete gamma function of the array elements.
Examples
--------
>>> a = ivy.array([2.5])
>>> x = ivy.array([1.7, 1.2])
>>> a.igamma(x)
ivy.array([0.3614, 0.2085])
"""
return ivy.igamma(
self._data,
x=x,
out=out,
)
86 changes: 86 additions & 0 deletions ivy/data_classes/container/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,3 +1011,89 @@ def bincount(
array([6.5, 2. , 2.5])
"""
return self.static_bincount(self, weights=weights, minlength=minlength, out=out)

@staticmethod
def static_igamma(
a: ivy.Container,
/,
*,
x: ivy.Container,
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.igamma. This method simply wraps the
function, and so the docstring for ivy.igamma also applies to this method with
minimal changes.
Parameters
----------
self
Input array.
x
An additional input array.
`x` has the same type as `a`.
out
optional output array, for writing the result to.
Returns
-------
ret
The lower incomplete gamma function of the array elements.
Examples
--------
>>> a = ivy.array([2.5])
>>> x = ivy.array([1.7, 1.2])
>>> a.igamma(x)
ivy.array([0.3614, 0.2085])
"""
return ContainerBase.cont_multi_map_in_function(
"igamma",
a,
x=x,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def igamma(
self: ivy.Container,
/,
*,
x: ivy.Container,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container instance method variant of ivy.igamma. This method simply wraps
the function, and so the docstring for ivy.igamma also applies to this method
with minimal changes.
Parameters
----------
self
Input array.
x
An additional input array.
`x` has the same type as `a`.
out
optional output array, for writing the result to.
Returns
-------
ret
The lower incomplete gamma function of the array elements.
Examples
--------
>>> a = ivy.array([2.5])
>>> x = ivy.array([1.7, 1.2])
>>> a.igamma(x)
ivy.array([0.3614, 0.2085])
"""
return self.static_igamma(self, x=x, out=out)
10 changes: 10 additions & 0 deletions ivy/functional/backends/jax/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,13 @@ def einsum(
equation: str, *operands: JaxArray, out: Optional[JaxArray] = None
) -> JaxArray:
return jnp.einsum(equation, *operands)


def igamma(
a: JaxArray,
/,
*,
x: JaxArray,
out: Optional[JaxArray] = None,
) -> JaxArray:
return jlax.igamma(a=a, x=x)
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,9 @@ def bincount(
ret = tf.math.bincount(x, minlength=minlength)
ret = tf.cast(ret, x.dtype)
return ret


def igamma(
a: tf.Tensor, /, *, x: tf.Tensor, out: Optional[tf.Tensor] = None
) -> tf.Tensor:
return tf.math.igamma(a, x)
13 changes: 13 additions & 0 deletions ivy/functional/backends/torch/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,16 @@ def bincount(


bincount.support_native_out = False


def igamma(
a: torch.Tensor,
/,
*,
x: torch.Tensor,
out: Optional[torch.Tensor] = None,
) -> torch.Tensor:
return torch.special.gammainc(a, x)


igamma.support_native_out = True
41 changes: 41 additions & 0 deletions ivy/functional/ivy/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,44 @@ def bincount(
return ivy.current_backend(x).bincount(
x, weights=weights, minlength=minlength, out=out
)


@handle_exceptions
@handle_nestable
@handle_out_argument
@to_native_arrays_and_back
def igamma(
a: ivy.Array,
/,
*,
x: ivy.Array,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
ivy.Array instance method variant of ivy.igamma. This method simply wraps the
function, and so the docstring for ivy.igamma also applies to this method with
minimal changes.
Parameters
----------
self
Input array.
x
An additional input array.
`x` has the same type as `a`.
out
optional output array, for writing the result to.
Returns
-------
ret
The lower incomplete gamma function of the array elements.
Examples
--------
>>> a = ivy.array([2.5])
>>> x = ivy.array([1.7, 1.2])
>>> a.igamma(x)
ivy.array([0.3614, 0.2085])
"""
return ivy.current_backend().igamma(a, x=x, out=out)
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_quantile(
@handle_test(
fn_tree="functional.ivy.experimental.corrcoef",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=["float32", "float64"],
available_dtypes=["int"],
num_arrays=2,
shared_dtype=True,
abs_smallest_val=1e-5,
Expand Down Expand Up @@ -437,3 +437,45 @@ def test_bincount(
weights=x[1],
minlength=min_length,
)


# igamma
@handle_test(
fn_tree="functional.ivy.experimental.igamma",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=["float32"],
num_arrays=2,
shared_dtype=True,
abs_smallest_val=1e-5,
min_num_dims=2,
max_num_dims=2,
min_dim_size=3,
max_dim_size=3,
min_value=1,
max_value=100,
allow_nan=False,
),
test_gradients=st.just(False),
test_with_out=st.just(False),
)
def test_igamma(
*,
dtype_and_x,
test_flags,
backend_fw,
fn_name,
on_device,
ground_truth_backend,
):
input_dtype, x = dtype_and_x
helpers.test_function(
ground_truth_backend=ground_truth_backend,
input_dtypes=input_dtype,
test_flags=test_flags,
on_device=on_device,
fw=backend_fw,
fn_name=fn_name,
rtol_=1e-04,
a=x[0],
x=x[1],
)

0 comments on commit d925090

Please sign in to comment.