DEV Community

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

Posted on • Edited on

gcd and lcm in PyTorch

Buy Me a Coffee

*My post explains abs() and sqrt().

gcd() 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 or a tensor.
  • The 1st argument(input) with torch or using a tensor(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-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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]]])
Enter fullscreen mode Exit fullscreen mode

lcm() 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(input) with torch or using a tensor(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-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)