Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn ufunc LCM

NumPy does not have a built-in universal function (ufunc) for computing the least common multiple (LCM) of elements in an array. However, you can still compute the LCM of array elements using NumPy's capabilities along with the numpy.gcd() function, which calculates the greatest common divisor (GCD).


Here's how you can compute the LCM of elements in a NumPy array:


import numpy as np
# Define a custom function to compute the LCM of two numbers
def lcm(a, b):
return abs(a * b) // np.gcd(a, b) if a and b else 0
# Define a vectorized version of the LCM function
lcm_vec = np.vectorize(lcm)
# Example array
arr = np.array([6, 8, 10])
# Compute the LCM of elements in the array
lcm_array = np.reduce(lcm_vec, arr)
print("LCM of array elements:", lcm_array)

This approach allows you to compute the LCM of array elements efficiently using NumPy's vectorized operations.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java