Combinatorics
Calculate combinatorial quantities for integer inputs.
SciPy · Function
Computes the factorial of a number or array of numbers. It can use exact integer arithmetic or a faster floating-point approximation.
factorial computes the product of the positive integers through each non-negative input. With exact=False it uses the gamma-function relationship for a faster floating-point result; exact=True uses integer arithmetic and preserves exact values.
scipy.special.factorial(n, exact=False)| Name | Type | Description |
|---|---|---|
n | int or array_like of ints | Input value or array of values for which to compute the factorial. |
exact | bool, optional | If True, use exact long-integer arithmetic; otherwise use a floating-point approximation. |
float or int or ndarray — Factorial of n, as an integer or float depending on exact.
import numpy as np
from scipy.special import factorial
values = np.array([3, 4, 5])
print(factorial(values, exact=True))
print(factorial(5))Computes exact factorials for an integer array and the default floating-point result for a scalar.
Calculate combinatorial quantities for integer inputs.
Apply factorial elementwise to a NumPy array.
Choose exact integer results when rounding errors are unacceptable.
It accepts an integer or an array-like collection of integers and returns the corresponding factorial. For an array input, the result is an array with matching elementwise factorial values.
Follow-up: How can the result type change? With exact=True, results use integer arithmetic; with exact=False, results are floating-point approximations.
exact controls the calculation method. True requests exact long-integer arithmetic, while False uses a faster floating-point approximation based on the gamma function.
Follow-up: Why might exact=False be preferable? It is faster and suitable when floating-point output and approximation are acceptable.
The function returns a float, int, or ndarray depending on the input and exact setting. Negative n values return 0, according to the documented behavior.
Follow-up: What happens for an array containing negative values? Each negative element follows the same documented rule and contributes 0 to the output array.
scipy.special.gamma