Python

Factorial in Python NumPy

We’ll show you how to employ the NumPy factorial function, also known as np.math.factorial, in this article. We’ll also go through the syntax of the np.math.factorial function, how it works, and how to apply it. In addition, we’ll show you how to compute factorials element-wise on NumPy arrays using a different function, scipy.special.factorial. However, it is preferable if you read the entire tutorial to find all the guidelines. That way, everything will probably make more sense.

What Is a NumPy Factorial? 

Python’s Numpy.math.factorial() function computes the factorial of a given positive number. But first, let’s define what factorial means. The product of all positive non-zero numbers that are less than or equal to the given number is the factorial of that number. The following is the general formula for calculating the factorial of a number ‘n’:

# n! = n*(n-1)*(n-2)*(n-3)*(n-4)….3*2*1

The factorial of 7, for example, is 7*6*5*4*3*2*1, or 5040.

We have a good sense of what factorial is by now. Let’s have a look at how to use the factorial function in NumPy. The function may be found in the NumPy module’s maths library. It’s similar to other python library functions like scipy.math.factorial and math.factorial. We could even say that there are distinct names for math.factorial’s core function.

What Is the Syntax of NumPy Factorial?

The factorial() function in NumPy has the following basic syntax:

The input integer/number for which the factorial must be calculated is represented by the ‘n’ parameter in the above-mentioned syntax. The function gives you an integer as a result.

The definition, syntax, and arguments for working with factorial functions in Python have been discussed. Let’s look at a couple of examples that are based on it.

Example 1

In the first scenario, we’ll look for the factorial of 8. The code for this can be found in the section below. The NumPy module is used to find the factorial of 8 using the numpy.math.factorial function. The function is called with the number 8 as an argument. The result is recorded in the ‘factorial num’ variable, and the output is eventually displayed on the screen, as shown below.

import numpy
factorial_num = numpy.math.factorial(8)
print('Factorial of 8 is :', factorial_num)

We have calculated the factorial of number 8 here. The factorial for the number eight is (8*7*6*5*4*3*2*1), which is 40320. The output of the preceding code shows the numpy.math.factorial() function likewise produces the same result.

Example 2

We will try to find the factorial of 0 in the second example of this article. The code is the same as before, with the exception that in the second line of code, we have supplied 0 as an argument to the function. Finally, on the last line, the result is displayed.

import numpy
factorial_num = numpy.math.factorial(0)
print('Factorial of 0 is :', factorial_num)

We have computed the factorial of 0 in this case. The factorial of 0 in NumPy is similar to the factorial of 0 in mathematics. It is 1 in both cases. See the result below.

Example 3

We will show you how to compute the Python NumPy factorial of an array in this example. An array is a collection of data that is all the same. Using nump.math.factorial, we calculated and displayed the factorial of each integer in the array in this code.

You can see that we import the NumPy module and created two arrays in the code. The first array (arr one) has a variety of values. The numbers 3, 5, 2, and 4 are among them. The second array (arr two) is empty of any data. The factorial of each integer in the array was then calculated using the for loop, and the result was added to the new array. Finally, the before and after arrays have been presented on the screen. The whole code may be seen here.

import numpy
arr_one = [3, 5, 2, 4]
arr_two = []
for n in arr_one:
    outcome = numpy.math.factorial(n)
    arr_two.append(outcome)
print('Before:',arr_one)
print('After:',arr_two)

The first array in this output is the array we gave to the program, and the second array displays the Python NumPy factorial of the array.

Please note that the numpy.math library’s factorial function does not compute factorials for negative values. Negative inputs, on the other hand, result in an error. The factorial of only positive natural numbers can be found using math.factorial() function. It won’t work if you want to find the element-by-element factorial of an input array. In some circumstances, we may need to employ a different function, as seen in the example below.

Example 4

We’ll show you how to compute the element-wise factorial of a NumPy array in Python in this example. Let’s say we have a numerical value array and wish to calculate the factorial of each member in the array. In that instance, the factorial() method from the Python scipy module can be used. The scipy package is not included with the Python programming language and must be installed separately. The installation command for the scipy package is listed below.

# pip install scipy

The factorial() function takes an array as an argument, calculates element-by-element factorials, and returns an array with the results.

The factorial() method in the scipy.special package was used to calculate the element-wise factorial of the NumPy array in the code below. The numpy.array() function was used to generate the NumPy array. We then used the factorial() function to calculate the element-wise factorial and saved the result in another NumPy array called factorial_arr.

from scipy.special import factorial
import numpy
arr = numpy.array([[0,1,3],[2,4,6]])
factorial_arr = factorial(arr)
print(factorial_arr)

If you run the above piece of code, you’ll receive something like this (see below).

Conclusion

The NumPy library factorial() method is actually a function from Python’s math package. It’s similar to scipy.math.factorial() in terms of functionality. The factorial of positive numbers is computed by this function. For input arrays, it does not work. Consider using the scipy.special.factorial() function to calculate the factorial of an input array.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content