NumPy – Using np.gcd() function (5 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, the fundamental package for scientific computing with Python, offers a vast array of functions for performing mathematical operations. Among these, the np.gcd() function is particularly useful for finding the greatest common divisor (GCD) of integers. This tutorial will explore the np.gcd() function through five practical examples, starting from the basics and progressing to more advanced applications.

What does np.gcd() Do?

The np.gcd() function calculates the greatest common divisor of two or more integers. The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder. Understanding how to use this function can be extremely useful in tasks related to number theory, algorithm development, and more.

Syntax:

numpy.gcd(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters explained:

  • x1, x2: array_like. Input values to find the GCD for. They can be scalars or arrays. If both are arrays, they should be broadcastable to the same shape.
  • out: ndarray, None, or tuple of ndarray and None, optional. A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to.
  • where: array_like, optional. This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.
  • casting, order, dtype, subok, signature, extobj: These are additional options for advanced usage, allowing more control over the operation’s execution.

Example 1: Basic Usage

import numpy as np

# Calculate the gcd of 12 and 15
result = np.gcd(12, 15)
print(f'The GCD of 12 and 15 is: {result}')

Output:

The GCD of 12 and 15 is: 3

Example 2: Finding GCD of an Array

import numpy as np

# Create an array of numbers
numbers = np.array([24, 108, 90])

# Calculate the gcd of numbers in the array
result = np.gcd.reduce(numbers)
print(f'The GCD of the numbers is: {result}')

Output:

The GCD of the numbers is: 6

Example 3: Pairwise GCD

import numpy as np

# Define two arrays of numbers
a = np.array([35, 52])
b = np.array([42, 39])

# Calculate the pairwise gcd
pairwise_gcd = np.gcd(a, b)
print(f'Pairwise GCDs are: {pairwise_gcd}')

Output:

Pairwise GCDs are: [7 13]

Example 4: GCD of Multidimensional Arrays

import numpy as np

# Define two 2D arrays
a = np.array([[15, 25], [40, 60]])
b = np.array([[5, 75], [25, 35]])

# Calculate the gcd of the arrays
result = np.gcd(a, b)
print(f'GCDs of arrays:
{result}')

Output:

GCDs of arrays:
[[ 5 25]
[40 5]]

Example 5: Using np.gcd() with Masking

import numpy as np

# Define an array of numbers
numbers = np.array([30, 45, 60, 90, 120, 150])

# Create a mask for numbers greater than 50
mask = numbers > 50

# Calculate the gcd of numbers greater than 50
filtered_gcd = np.gcd.reduce(numbers[mask])
print(f'GCD of numbers > 50: {filtered_gcd}')

Output:

GCD of numbers > 50: 30

Conclusion

Through these examples, we’ve seen how versatile the np.gcd() function is for computing the greatest common divisor in various scenarios. From simple pairwise comparisons to operations on arrays and using conditional masking, np.gcd() can handle a wide range of inputs. Mastering this function opens up numerous possibilities for number theory and algorithmic applications in Python.