Home » 
        Python » 
        Python Programs
    
    
    Factorial in numpy and scipy
    
    
    
    
	    Learn, how to calculate factorial of a number in numpy and scipy?
	    
		    Submitted by Pranit Sharma, on January 20, 2023
	    
    
    
    NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.
    SciPy is a free and open-source Python library used for scientific computing and technical computing. We can use SciPy modules for optimization, interpolation, special functions, linear algebra, integration FFT, signal and image processing, ODE solvers, and other tasks common in science and engineering.
    Problem statement
    Suppose that we are given a NumPy array and we need to import numpy and scipy factorial functions to check which one works faster.
    Finding the factorial in numpy and scipy
    Basically, scipy.math.factorial(), numpy.math.factorial(), and math.factorial() are same when it comes to their processing. However, scipy.special.factorial is different as it can take a ndarray as an input while others can't.
    Let us understand with the help of an example,
    Python code to find the factorial in numpy and scipy
# Import numpy
import numpy as np
# Import scipy special
import scipy.special
# Creating a numpy array
arr = np.array([3, 4, 5])
# Display original array
print("Original array:\n",arr,"\n")
res = scipy.special.factorial(arr)
# Display the result
print("Result:\n",res)
    Output
    
    In this example, we have used the following Python basic topics that you should learn:
    
    Python NumPy Programs »
    
    
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement