Pytorch Torch Igamma
## PyTorch torch.igamma
The `torch.igamma` function in PyTorch computes the element-wise regularized lower incomplete gamma function. It is widely used in statistics, probability theory, and various scientific computing applications.
---
## Introduction
The regularized lower incomplete gamma function is mathematically defined as:
$$P(a, x) = \frac{1}{\Gamma(a)} \int_{0}^{x} t^{a-1} e^{-t} dt$$
Where:
* $a$ is the shape parameter (must be positive).
* $x$ is the upper limit of integration (must be non-negative).
* $\Gamma(a)$ is the complete gamma function.
In PyTorch, `torch.igamma(input, other)` computes this function element-wise, where `input` corresponds to the parameter $a$, and `other` corresponds to the parameter $x$.
---
## Syntax and Parameters
### Syntax
```python
torch.igamma(input, other, *, out=None) -> Tensor
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **`input`** | Tensor | The first input tensor ($a$), representing the shape parameter. Must contain positive real numbers. |
| **`other`** | Tensor | The second input tensor ($x$), representing the upper integration limit. Must contain non-negative real numbers. |
| **`out`** | Tensor (optional) | The output tensor where the result will be written. |
### Return Value
* Returns a new tensor containing the element-wise evaluation of the regularized lower incomplete gamma function.
* The shape of the returned tensor is determined by broadcasting `input` and `other`.
---
## Code Examples
### Example 1: Basic Usage with 1D Tensors
This example demonstrates how to compute the regularized lower incomplete gamma function using two 1D tensors of the same shape.
```python
import torch
# Define the shape parameter 'a' (input) and evaluation point 'x' (other)
a = torch.tensor([1.0, 1.0, 1.0])
x = torch.tensor([1.0, 2.0, 3.0])
# Compute the regularized lower incomplete gamma function
result = torch.igamma(a, x)
print("Input (a): ", a)
print("Other (x): ", x)
print("Result: ", result)
```
**Output:**
```text
Input (a): tensor([1., 1., 1.])
Other (x): tensor([1., 2., 3.])
Result: tensor([0.6321, 0.8647, 0.9502])
```
*(Note: When $a = 1$, the function simplifies to $1 - e^{-x}$. For $x=1$, $1 - e^{-1} \approx 0.6321$.)*
### Example 2: Broadcasting Support
PyTorch supports broadcasting for `torch.igamma`. You can pass tensors of different shapes as long as they are broadcastable.
```python
import torch
# 'a' is a 1D tensor of shape (3,)
a = torch.tensor([1.0, 2.0, 3.0])
# 'x' is a 2D tensor of shape (2, 1)
x = torch.tensor([[1.0],
[2.0]])
# The resulting tensor will have a broadcasted shape of (2, 3)
result = torch.igamma(a, x)
print("Resulting Tensor:\n", result)
```
**Output:**
```text
Resulting Tensor:
tensor([[0.6321, 0.2642, 0.0803],
[0.8647, 0.5940, 0.3233]])
```
---
## Considerations and Rules
1. **Domain Restrictions**:
* The parameter $a$ (`input`) must be strictly positive ($a > 0$).
* The parameter $x$ (`other`) must be non-negative ($x \ge 0$).
* If these conditions are violated, the behavior is undefined or may return `NaN`.
2. **Data Types**: Both input tensors must have floating-point data types (e.g., `torch.float32` or `torch.float64`).
3. **Complementary Function**: If you need to compute the regularized *upper* incomplete gamma function ($Q(a, x) = 1 - P(a, x)$), use [`torch.igammac`](https://pytorch.org/docs/stable/generated/torch.igammac.html).
YouTip