div(), remainder() and fmod() in PyTorch
Tags: pytorch, div, remainder, fmod
Published: 2024-05-09 00:39:58

*[My post](https://dev.to/hyperkai/add-sub-and-mul-in-pytorch-1m1f) explains [add()](https://pytorch.org/docs/stable/generated/torch.add.html), [sub()](https://pytorch.org/docs/stable/generated/torch.sub.html) and [mul()](https://pytorch.org/docs/stable/generated/torch.mul.html).

[div()](https://pytorch.org/docs/stable/generated/torch.div.html) can do division with two of the 0D or more D tensors of zero or more elements or scalars or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `div()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch`(`tensor` or `scalar` of `int`, `float`, `complex` or `bool`) or using a tensor(`tensor` of `int`, `float`, `complex` or `bool`) is `input`(Required).
- The 2nd argument with `torch` or the 1st argument with a tensor is `other`(Required-Type:`tensor` or `scalar` of `int`, `float`, `complex` or `bool`).
- There is `rounding_mode` argument with `torch` or a tensor(Optional-Default:`None`-Type:`str`):
  *Memos:
  - `None`, `"trunc"` or `"floor"` can be set.
  - `rounding_mode=` must be used.
  - `None` performs no rounding and, if both input and other are integer types, promotes the inputs to the default scalar type. *Equivalent to true division in Python (the `/` operator) and NumPy’s [np.true_divide()](https://numpy.org/doc/stable/reference/generated/numpy.true_divide.html).
  - `"trunc"` rounds the results of the division towards zero. *Equivalent to C-style integer division.
  - `"floor"` rounds the results of the division down. *Equivalent to floor division in Python (the `//` operator) and NumPy’s [np.floor_divide()](https://numpy.org/doc/stable/reference/generated/numpy.floor_divide.html).
  - The tensor or scalar of `complex` or `bool` cannot be used with `"trunc"` or `"floor"`.
  - Setting `0`(`int`) to `other` gets `ZeroDivisionError` with `"trunc"` or `"floor"`.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [divide()](https://pytorch.org/docs/stable/generated/torch.divide.html) is the alias of `div()`.
- [true_divide()](https://pytorch.org/docs/stable/generated/torch.true_divide.html) is the alias of `div()` with `rounding_mode=None`.
- [floor_divide()](https://pytorch.org/docs/stable/generated/torch.floor_divide.html) is the same as `div()` with `rounding_mode="trunc"` as long as I experimented.

```python
import torch

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])

torch.div(input=tensor1, other=tensor2)
tensor1.div(other=tensor2)
# tensor([[2.2887, -1.5595, 1.8576],　[-4.5139, 1.3039, -1.0468]])

torch.div(input=9.75, other=tensor2)
# tensor([[2.2887, -2.1476, 2.8932], [-4.5139, 1.7956, -1.6304]])

torch.div(input=tensor1, other=4.26)
# tensor([2.2887, 1.6620, 1.4695])

torch.div(input=9.75, other=4.26)
# tensor(2.2887)

torch.div(input=tensor1, other=tensor2, rounding_mode="trunc")
# tensor([[2., -1., 1.], [-4., 1., -1.]])

torch.div(input=9.75, other=tensor2, rounding_mode="trunc")
# tensor([[2., -2., 2.], [-4., 1., -1.]])

torch.div(input=tensor1, other=4.26, rounding_mode="trunc")
# tensor([2., 1., 1.])

torch.div(input=9.75, other=4.26, rounding_mode="trunc")
# tensor(2.)

torch.div(input=tensor1, other=tensor2, rounding_mode="floor")
# tensor([[2., -2., 1.], [-5., 1., -2.]])

torch.div(input=9.75, other=tensor2, rounding_mode="floor")
# tensor([[2., -3., 2.], [-5., 1., -2.]])

torch.div(input=tensor1, other=4.26, rounding_mode="floor")
# tensor([2., 1., 1.])

torch.div(input=9.75, other=4.26, rounding_mode="floor")
# tensor(2.)

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.div(input=tensor1, other=tensor2)
# tensor([[2.2500, -1.7500, 2.0000], [-4.5000, 1.4000, -1.2000]])

torch.div(input=9, other=tensor2)
# tensor([[2.2500, -2.2500, 3.0000], [-4.5000, 1.8000, -1.8000]])

torch.div(input=tensor1, other=4)
# tensor([2.2500, 1.7500, 1.5000])

torch.div(input=9, other=4)
# tensor(2.2500)

tensor1 = torch.tensor([9.+0.j, 7.+0.j, 6.+0.j])
tensor2 = torch.tensor([[4.+0.j, -4.+0.j, 3.+0.j],
                        [-2.+0.j, 5.+0.j, -5.+0.j]])
torch.div(input=tensor1, other=tensor2)
# tensor([[2.2500+0.j, -1.7500-0.j, 2.0000+0.j],
#         [-4.5000-0.j, 1.4000+0.j, -1.2000-0.j]])

torch.div(input=9.+0.j, other=tensor2)
# tensor([[2.2500+0.j, -2.2500-0.j, 3.0000+0.j],
#         [-4.5000-0.j, 1.8000+0.j, -1.8000-0.j]])

torch.div(input=tensor1, other=4.+0.j)
# tensor([2.2500+0.j, 1.7500+0.j, 1.5000+0.j])

torch.div(input=9.+0.j, other=4.+0.j)
# tensor(2.2500+0.j)

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[False, True, False], [True, False, True]])

torch.div(input=tensor1, other=tensor2)
# tensor([[inf, 0., inf], [1., nan, 1.]])

torch.div(input=True, other=tensor2)
# tensor([[inf, 1., inf], [1., inf, 1.]])

torch.div(input=tensor1, other=False)
# tensor([inf, nan, inf])

torch.div(input=True, other=False)
# tensor(inf)
```

[remainder()](https://pytorch.org/docs/stable/generated/torch.remainder.html) can do the modulo(mod) calculation of [Python’s modulus operation](https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations) with two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `remainder()` can be used with `torch` or a tensor.
- The 1st argument with `torch`(`tensor` or `scalar` of `int` or `float`) or using a tensor(`tensor` of `int` or `float`) is `input`(Required). *You must use a scalar without `input=`. 
- The 2nd argument with `torch` or the 1st argument with a tensor is `other`(Required-Type:`tensor` or `scalar` of `int` or `float`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Setting `0`(`int`) to `other` gets `ZeroDivisionError`.
- The combination of a scalar(`input`) and a scalar(`other`) cannot be used.
- The result has the same sign as `other`.

```python
import torch

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
tensor1.remainder(other=tensor2)
# tensor([[1, -1, 0], [-1, 2, -4]])

torch.remainder(9, other=tensor2)
# tensor([[1, -3, 0], [-1, 4, -1]])

torch.remainder(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([-9, -7, -6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
# tensor([[3, -3, 0],

torch.remainder(-9, other=tensor2)
# tensor([[3, -1, 0], [-1, 1, -4]])

torch.remainder(input=tensor1, other=4)
# tensor([3, 1, 2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])

torch.remainder(input=tensor1, other=tensor2)
# tensor([[1.2300, -2.0000, 2.8900],
#         [-1.0500, 1.6500, -5.7000]])

torch.remainder(9.75, other=tensor2)
# tensor([[1.2300, -3.8700, 3.0100], [-1.0500, 4.3200, -2.2100]])

torch.remainder(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])
```

[fmod()](https://pytorch.org/docs/stable/generated/torch.fmod.html) can do the modulo(mod) calculation of [C++’s std::fmod](https://en.cppreference.com/w/cpp/numeric/math/fmod) with two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `fmod()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int` or `float`). 
- The 2nd argument with `torch` or the 1st argument with a tensor is `other`(Required-Type:`tensor` or `scalar` of `int` or `float`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Setting `0`(`int`) to `other` gets `ZeroDivisionError`.
- The result has the same sign as `input`.

```python
import torch

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
tensor1.fmod(other=tensor2)
# tensor([[1, 3, 0], [1, 2, 1]])

torch.fmod(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([-9, -7, -6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
# tensor([[-1, -3, 0], [-1, -2, -1]])

torch.fmod(input=tensor1, other=4)
# tensor([-1, -3, -2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])

torch.fmod(input=tensor1, other=tensor2)
# tensor([[1.2300, 2.5400, 2.8900], [1.1100, 1.6500, 0.2800]])

torch.fmod(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])
```