gcd() and lcm() in PyTorch
Tags: pytorch, gcd, lcm, function
Published: 2024-05-01 03:14:02

*Memos:
- [My post](https://dev.to/hyperkai/abs-and-sqrt-in-pytorch-59mo) explains [abs()](https://pytorch.org/docs/stable/generated/torch.abs.html) and [sqrt()](https://pytorch.org/docs/stable/generated/torch.sqrt.html).

[gcd()](https://pytorch.org/docs/stable/generated/torch.gcd.html) can get the 0D or more D tensor of zero or more greatest common divisors from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `gcd()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`).
- The 2nd argument with `torch` or the 1st argument is `other`(Required-Type:`tensor` of `int`).
- 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.

```python
import torch

tensor1 = torch.tensor(16)
tensor2 = torch.tensor([-40, -30, -20, -10, 0, 10, 20, 30])

torch.gcd(input=tensor1, other=tensor2)
tensor1.gcd(other=tensor2)
torch.gcd(input=tensor2, other=tensor1)
# tensor([8, 2, 4, 2, 16, 2, 4, 2])

tensor1 = torch.tensor([16, -12, -15, 1, 9, -25, 0, -18])
tensor2 = torch.tensor([-40, -30, -20, -10, 0, 10, 20, 30])

torch.gcd(input=tensor1, other=tensor2)
torch.gcd(input=tensor2, other=tensor1)
# tensor([8, 6, 5, 1, 9, 5, 20, 6])

tensor1 = torch.tensor([[16, -12, -15, 1], [9, -25, 0, -18]])
tensor2 = torch.tensor([0, 10, 20, 30])

torch.gcd(input=tensor1, other=tensor2)
torch.gcd(input=tensor2, other=tensor1)
# tensor([[16, 2, 5, 1], [9, 5, 20, 6]])

tensor1 = torch.tensor([[[16, -12], [-15, 1]],
                        [[9, -25], [0, -18]]])
tensor2 = torch.tensor([0, 10])

torch.gcd(input=tensor1, other=tensor2)
torch.gcd(input=tensor2, other=tensor1)
# tensor([[[16, 2], [15, 1]],
#         [[9, 5], [0, 2]]])
```

[lcm()](https://pytorch.org/docs/stable/generated/torch.lcm.html) can get the 0D or more D tensor of zero or more least common multiples from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `lcm()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`).
- The 2nd argument with `torch` or the 1st argument is `other`(Required-Type:`tensor` of `int`).
- 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.

```python
import torch

tensor1 = torch.tensor(10)
tensor2 = torch.tensor([-4, -3, -2, -1, 0, 1, 2, 3])

torch.lcm(input=tensor1, other=tensor2)
tensor1.lcm(other=tensor2)
torch.lcm(input=tensor2, other=tensor1)
# tensor([20, 30, 10, 10, 0, 10, 10, 30])

tensor1 = torch.tensor([10, 1, -15, 4, 9, -6, 0, -5])
tensor2 = torch.tensor([-4, -3, -2, -1, 0, 1, 2, 3])

torch.lcm(input=tensor1, other=tensor2)
torch.lcm(input=tensor2, other=tensor1)
# tensor([20, 3, 30, 4, 0, 6, 0, 15])

tensor1 = torch.tensor([[10, 1, -15, 4], [9, -6, 0, -5]])
tensor2 = torch.tensor([0, 1, 2, 3])

torch.lcm(input=tensor1, other=tensor2)
torch.lcm(input=tensor2, other=tensor1)
# tensor([[0, 1, 30, 12], [0, 6, 0, 15]])

tensor1 = torch.tensor([[[10, 1], [-15, 4]], [[9, -6], [0, -5]]])
tensor2 = torch.tensor([0, 1])

torch.lcm(input=tensor1, other=tensor2)
torch.lcm(input=tensor2, other=tensor1)
# tensor([[[0, 1], [0, 4]],
#         [[0, 6], [0, 5]]])
```