DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

gcd() and lcm() in PyTorch

*Memos:

gcd() can get the zero or more greatest common divisors of the zero or more values of two of 0D or more D tensors as shown below:

*Memos:

  • gcd() can be used with torch or a tensor.
  • The 1st argument(tensor of int) with torch or using a tensor(tensor of int) is input(Required).
  • The 2nd argument(tensor of int) with torch or the 1st argument(tensor of int) is other(Required).
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]]])

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

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

lcm() can get the zero or more least common multiples of the zero or more values of two of 0D or more D tensors as shown below:

*Memos:

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

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

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

Top comments (0)