Updated April 3, 2023
Definition of NumPy Factorial
Numpy.math.factorial() is a mathematical function in python that is used to compute the factorial of a given positive number. But before we start, what exactly is factorial? The factorial of a number is the product of all the positive non-zero numbers less than or equal to the given number. The generic formula for computing factorial of number ‘n’ is given by :
n! = n*(n-1)*(n-2)*(n-3)*(n-4)….3*2*1
For example,
factorial of 6 would be 6*5*4*3*2*1, i.e. 720
factorial of 0 and 1 would be 1
factorial of 9 would be 9*8*7*6*5*4*3*2*1, i.e. 362880
By now, we have a fair idea of what factorial is. Let’s go ahead and understand the factorial function in NumPy. The function is present in the maths library of the NumPy module. It is very similar to other functions in python libraries such as scipy.math.factorial and math.factorial. We can even say that they are different names for the main function of math.factorial.
Syntax and Parameters
The basic syntax for using the Numpy factorial() function is as follows :
numpy.math.factorial(n)
The parameters used in the above-mentioned syntax are as follows :
n: This is the input integer/number for which the factorial has to be calculated.
Return: The function returns an integer.
Having discussed the syntax and arguments used for working with factorial functions in python. Let’s try a few examples based on it.
Examples of NumPy Factorial
Different examples are mentioned below:
Example #1
Find the factorial of 5.
Code:
#import module
import numpy as np
#function
factorial = np.math.factorial(5)
print('Factorial of 5 is :', factorial)
Output:
Here, we have computed the factorial of number 5. The factorial of 5 is given as (5X4X3X2X1), which is equivalent to 120. We can observe from the output of the given code that numpy.math.factorial() function also returns the same output.
Example #2
Find the factorial of 0.
Code:
import numpy as np
factorial = np.math.factorial(0)
print('Factorial of 0 is :', factorial)
Output:
In this example, we have computed the factorial of 0. Similar to mathematics, the factorial of 0 in numpy.math is also 1.
Example #3
Find the factorial of 760.
Code:
import numpy as np
factorial = np.math.factorial(760)
print('Factorial of 760 is :', factorial)
Output:
In this example, we have calculated the factorial of 760. The factorial of this number is a large integer value, as can be seen from the output.
Example #4
Find the factorial of -8.
Code:
import numpy as np
factorial = np.math.factorial(-8)
print('Factorial of -8 is :', factorial)
Output:
In this example, we have tried to illustrate that the factorial function from numpy.math library does not compute factorials for negative numbers. Instead, it throws an error for negative inputs. But as you read ahead, compare this example with the example 6.
The above-discussed math.factorial() function will find the factorial of only positive natural numbers. It will not work for finding the element-wise factorial of an input array. In those cases, we might have to use a different function. The function which is frequently used for finding the factorial of a NumPy array is given as follows :
scipy.special.factorial(input array)
This factorial function is taken from scipy.special for performing element-wise factorial computation on a numpy ndarray. The input argument of the function is self-explanatory. The function returns a numpy array with computed values.
Here are a few examples to illustrate this further.
Example #5
Compute the element-wise factorial of the given matrix.
Code:
import numpy as np
#import scipy module
from scipy.special import factorial
#input
a = np.array([[1,2,3],[4,1,2]])
print('Input array is:\n', a)
#function
fact_arr = factorial(a)
print('Factorial of the input array is:\n',fact_arr)
Output:
Here we have illustrated the use of the factorial() function from scipy.special module to compute the element-wise factorial of an input numpy ndarray.
Example #6
Compute the element-wise factorial of the given multidimensional array.
Code:
import numpy as np
#import scipy module
from scipy.special import factorial
#input
a = np.array([[1,-2,1],[1,1,3]])
print('Input array is:\n', a)
#function
fact_arr = factorial(a)
print('Factorial of the input array is:\n',fact_arr)
Output:
In this example, we tried to illustrate the special.factorial() function does not throw an error when a few input array elements are negative. It just returns ‘0’ corresponding to that particular element and factorials for the rest of the elements. Compare this with example 4 where the numpy.math.factorial() function is used for negative numbers.
Conclusion
factorial() function from the NumPy library is actually a function from the maths library of python. It is similar to scipy.math.factorial(). The function computes the factorial of positive numbers. It does not work for input arrays. In order to calculate the factorial of an input array, consider using scipy.special.factorial() function.
Recommended Articles
This is a guide to NumPy Factorial. Here we discuss the definition, syntax of the NumPy Factorial function in Python, along with programming examples and their outputs. You may also have a look at the following articles to learn more –