Python

# PyTorch – gcd()

PyTorch is an open-source framework for the Python programming language.

A tensor is a multidimensional array that is used to store data. So to use a tensor, we have to import the torch module.

To create a tensor the method used is tensor().

Syntax:
torch.tensor(data)

Where data is a multi-dimensional array.

## torch.gcd()

gcd() in PyTorch is used to return the greatest common divisors from both the elements in two tensor objects.

Syntax:
torch.gcd(tensor_object1,tensor_object2)

Where

1. tensor_object1 is the first tensor.
2. tensor_object2 is the second tensor.

Return:
It will also return the greatest common divisors from two tensors.

Example 1:
In this example, we will create two tensors with one dimension that have five elements each and perform gcd() operation on them.

#import torch module
import torch

#create two 1D tensors
data1 = torch.tensor([1,2,3,4,5])
data2 = torch.tensor([34,45,3,40,10])

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([1, 2, 3, 4, 5])
tensor([34, 45,  3, 40, 10])
GCD
tensor([1, 1, 3, 4, 5])

Working:

1. gcd(1,34) – 1
2. gcd(2,45) – 1
3. gcd(3,3) – 3
4. gcd(4,40) – 4
5. gcd(5,10) – 5

It is also possible to return the GCD with one element that computes each element.

Example 2:
In this example, we will create two tensors with one dimension that have five elements in the first tensor and one element in the second tensor and perform gcd() operation on them.

#import torch module
import torch

#create two 1D tensors
data1 = torch.tensor([1,2,3,4,5])
data2 = torch.tensor([10])

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([1, 2, 3, 4, 5])
tensor([10])
GCD
tensor([1, 2, 1, 2, 5])

Working:

1. gcd(1,10) -1
2. gcd(2,10) – 2
3. gcd(3,10) – 1
4. gcd(4,10) – 2
5. gcd(5,10) – 5

Example 3:
In this example, we will create two tensors with two dimensions that have five elements each and perform gcd() operation on them.

#import torch module
import torch

#create two 2D tensors
data1 = torch.tensor([[1,2,3,4,5],[45,67,89,87,78]])
data2 = torch.tensor([[134,54,67,65,56],[45,67,89,87,78]])

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([[ 1,  2,  3,  4,  5],
[45, 67, 89, 87, 78]])
tensor([[134,  54,  67,  65,  56],
[ 45,  67,  89,  87,  78]])
GCD
tensor([[ 1,  2,  1,  1,  1],
[45, 67, 89, 87, 78]])

Working:

1. gcd(1,134) -1,gcd(45,45) -45
2. gcd(2,54) – 2,gcd(67,67) -67
3. gcd(3,67) – 1,gcd(89,89) -89
4. gcd(4,65) – 1,gcd(87,87) -87
5. gcd(5,56) – 1,gcd(78,78) -78

## Work with CPU

If you want to run a gcd() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

At this time, when we are creating a tensor, we can use the cpu() function.

Syntax:
torch.tensor(data).cpu()

Example 1:
In this example, we will create two tensors with one dimension that have five elements each and perform gcd() operation on them.

#import torch module
import torch

#create two 1D tensors
data1 = torch.tensor([1,2,3,4,5]).cpu()
data2 = torch.tensor([34,45,3,40,10]).cpu()

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([1, 2, 3, 4, 5])
tensor([34, 45,  3, 40, 10])
GCD
tensor([1, 1, 3, 4, 5])

Working:

1. gcd(1,34) – 1
2. gcd(2,45) – 1
3. gcd(3,3) – 3
4. gcd(4,40) – 4
5. gcd(5,10) – 5

It is also possible to return the GCD with one element that computes each element.

Example 2:
In this example, we will create two tensors with one dimension that have five elements in the first tensor and one element in the second tensor and perform gcd() operation on them.

#import torch module
import torch

#create two 1D tensors
data1 = torch.tensor([1,2,3,4,5]).cpu()
data2 = torch.tensor([10]).cpu()

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([1, 2, 3, 4, 5])
tensor([10])
GCD
tensor([1, 2, 1, 2, 5])

Working:

1. gcd(1,10) -1
2. gcd(2,10) – 2
3. gcd(3,10) – 1
4. gcd(4,10) – 2
5. gcd(5,10) – 5

Example 3:
In this example, we will create two tensors with two dimensions that have elements each and perform gcd() operation on them.

#import torch module
import torch

#create two 2D tensors
data1 = torch.tensor([[1,2,3,4,5],[45,67,89,87,78]]).cpu()
data2 = torch.tensor([[134,54,67,65,56],[45,67,89,87,78]]).cpu()

#display
print("Actual Tensors: ")
print(data1)
print(data2)

print("GCD")
#return greatest common divisors
print(torch.gcd(data1,data2))

Output:

Actual Tensors:
tensor([[ 1,  2,  3,  4,  5],
[45, 67, 89, 87, 78]])
tensor([[134,  54,  67,  65,  56],
[ 45,  67,  89,  87,  78]])
GCD
tensor([[ 1,  2,  1,  1,  1],
[45, 67, 89, 87, 78]])

Working:

1. gcd(1,134) -1,gcd(45,45) -45
2. gcd(2,54) – 2,gcd(67,67) -67
3. gcd(3,67) – 1,gcd(89,89) -89
4. gcd(4,65) – 1,gcd(87,87) -87
5. gcd(5,56) – 1,gcd(78,78) -78

## Conclusion

In this PyTorch lesson, we saw gcd() and how to apply gcd() on a tensor to return the greatest common divisor. We also created a tensor with cpu() function and returned GCD.