HOWTO · NumPy

NumPy Factorial

Learn how to compute the factorial of the elements of a NumPy array using SciPy's special.factorial() function in Python, with code examples and explanations of the results, including why the values are returned as floating-point numbers and how to request exact integer results instead.

On this page

Calculating the factorial of numbers is a common task in mathematics and programming, especially in fields like statistics and combinatorics. In Python, we can compute the factorial of elements in a NumPy array using the special.factorial() function from the SciPy library.

This function performs the calculation element-wise over an array. Whether you’re working with a small set of numbers or a large array, the same call applies the factorial to every element at once.

In this article, we’ll explore how to use the special.factorial() function, providing clear examples and explanations to help you grasp this essential concept.

What is a Factorial?

Before diving into the code, it’s crucial to understand what a factorial is. The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. For example, 5! equals 5 × 4 × 3 × 2 × 1, which equals 120. Factorials are widely used in permutations, combinations, and other mathematical computations.

In programming, factorials are often needed for every element of an array at once. This is where NumPy and SciPy come into play: special.factorial() applies the operation element-wise over an array.

Using SciPy’s special.factorial() Function

The special.factorial() function from the SciPy library is designed to compute the factorial of each element in an array. It performs the operation element-wise, so one call handles an array of any size.

To use this function, you first need to install the SciPy library if you haven’t already. You can do this using pip:

pip install scipy

Once you have SciPy installed, you can start using the special.factorial() function. Below is a simple example to demonstrate how to compute the factorial of an array of integers.

import numpy as np
from scipy import special

arr = np.array([0, 1, 2, 3, 4, 5])
factorials = special.factorial(arr)

print(factorials)

Output:

[  1.   1.   2.   6.  24. 120.]

In this example, we first import the necessary libraries: NumPy and SciPy’s special module. We create a NumPy array containing integers from 0 to 5. By passing this array to the special.factorial() function, we obtain the factorials of these numbers. The output shows the respective factorials as floating-point numbers, which is the default behavior of the function.

Computing Factorials for Large Arrays

The same call works unchanged for larger arrays.

Let’s look at an example where we compute the factorial for a larger range of numbers.

large_arr = np.arange(10, 21)  # Array from 10 to 20
large_factorials = special.factorial(large_arr)

print(large_factorials)

Output:

[3.62880000e+06 3.99168000e+07 4.79001600e+08 6.22702080e+09
 8.71782912e+10 1.30767437e+12 2.09227899e+13 3.55687428e+14
 6.40237371e+15 1.21645100e+17 2.43290201e+18]

In this case, we create an array containing integers from 10 to 20 using np.arange(). The special.factorial() function computes the factorial for each element in this array, without the need to write a loop. The output is displayed in scientific notation due to the large size of the numbers.

Why the Results Are Floating-Point Numbers

Look again at the outputs above: the factorials come back as floating-point numbers — 120. rather than 120, and the larger results in scientific notation. That is the default behavior of special.factorial(): it returns its results as floating-point values, even when every input is an integer.

Floating-point numbers have limited precision, so for very large factorials the returned value is the nearest representable floating-point number rather than the exact integer. If you need exact integer results, pass exact=True:

arr = np.array([0, 1, 2, 3, 4, 5])
exact_factorials = special.factorial(arr, exact=True)

print(exact_factorials)

Output:

[  1   1   2   6  24 120]

With exact=True, the same inputs return integers with no decimal point, as the output shows. Choose the default form when floating-point results are acceptable for your computation, and the exact form when the precise integer value matters.

Conclusion

In summary, the special.factorial() function from the SciPy library calculates factorials element-wise over a NumPy array in Python. Whether you’re working with a small array or a large one, the same call applies, and the results come back as floating-point numbers by default, with exact integer results available by passing exact=True. Understanding how to compute factorials not only aids in mathematical computations but also opens doors to more advanced topics in statistics and data science.

FAQ

  1. What is the factorial of a number?
    The factorial of a non-negative integer n is the product of all positive integers less than or equal to n, denoted as n!.

  2. How do I install SciPy?
    You can install SciPy using pip with the command pip install scipy.

  3. Why are the results shown as floating-point numbers?
    By default, the special.factorial() function returns floating-point values, even for integer inputs, so large results print in scientific notation.

  4. How can I get exact integer factorials instead?
    Pass exact=True to the special.factorial() function, as shown in the floating-point section above.

  5. What output format does special.factorial() return?
    The output is returned as floating-point numbers by default.