Numpy Factorial

What is Factorial?

A factorial is the reversed sequential multiplication from a given number and it ends on 1.

For example factorial of 4 is

4!

1x2x3x4 = 24

Hope you guys got the concept now, let’s move to our today’s topic the numpy factorial.

Introduction to Numpy Factorial

The numpy factorial is a library in python which is used to calculate the factorial of given positive integers or numbers. It cannot calculate the factorial of negative integers.

For better understanding check out the examples below.

Example 1

import numpy
factnum = numpy.math.factorial(7)
print('The Factorial of 7 is :', factnum)

Code Explanation

In the above code we imported the numpy library first then we declared a variable in which we stored the numpy.math.factorial function and passed 7 as a parameter. 7 is the number whose factorial we wanted to calculate.

In the end, we just printed the factorial, check out the results below.

Output

The Factorial of 7 is: 5040

As you can see from the above output it returned the factorial of 7 which is 5040.

Note: if the above code is not executing so first install the numpy library in your environment by running the “pip install numpy” command in the command prompt. After installing the numpy library execute the above code in the python environment.

Let’s try to calculate the factorial of 0 check out the example below.

Example 2

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

Output

The Factorial of 0 is: 1

We execute the same code in this example as we did above we just change the number to 0 and as you can see the output returned the factorial of 0 which is 1.

We can also use the for loop and if else conditions with numpy.math.factorial function, see the example below.

Example 3

number = 7
factorial = 1
if number < 0:
   print("can't calculate the Factorial of negative integer")
elif number == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,number + 1):
       factorial = factorial*i
   print("The factorial of",number,"=",factorial)

Code Explanation

You can see the above code, we declared 2 variables first one is the “number” variable and we assigned 7 to it. 7 is the number whose factorial we wanted to know then the second variable is “factorial” which is 1. Then we defined a condition that states that if the variable number is less than 0 means if the number is negative so print “can’t calculate the Factorial of negative integer”.

The next else if condition states that if the given number is 0 then print “The factorial of 0 is 1”

And in the else part we created a for loop which will calculate the factorial of a given number 7.

In the end, we printed the factorial, check out the results below.

Output

The factorial of 7 = 5040

We can calculate the factorial of integers in an array as well but for that, we have to install a module that is called “scipy”

So for installing scipy library run this command “pip install scipy” in the command prompt

Then after installing the library execute the following code in the python environment.

Example 4 – Scipy Library

from scipy.special import factorial
import numpy
sci_arr = numpy.array([4,0,2])
fact_arr = factorial(sci_arr)
print(fact_arr)

Code Explanation

In the above code we imported the libraries then we declared a variable “sci_arr” and assigned it the numpy.array() function and passed the array as a parameter.

Then we declared another variable”fact_arr” and assigned it the factorial() function and passed the “sci_arr” variable(in which the array is stored) as a parameter to it. After that we just printed the factorial array.

Check out the results below.

Output

[24.  1.  2.]

As you can see the output above it calculated the factorial of the array element-wise.

Hope you got the concept of spicy module. The best thing about using the spicy library is it can also calculate the factorials of multidimensional arrays sequentially. see the example below.

Example 5 – Multidimensional Array

import numpy
#importing scipy module
from scipy.special import factorial

#declaring multidimensional array
fact = numpy.array([[4,6,0],[2,5,-1]])
print('the given multidimensional array is:\n',fact)

#using function
fact_arr = factorial(fact)
print('Factorial of multidimensional array is:\n',fact_arr)

Code Explanation

You can see the above code, we imported the numpy library and then we imported the scipy module then in the numpy.array function we passed the multidimensional array as a parameter.

we printed our given array for showing the before and after factorial calculation of the array then we used the factorial function and passed that variable “fact” in which our numpy function is stored.

In the end, we just printed the factorial of the given multidimensional array.

Now let’s see the results below.

Output

[[ 24. 720.   1.]
 [  2. 120.  0.]]

As you can see from the output above the factorial of our multidimensional array has been printed.

Conclusion

In this tutorial, we talk about the numpy factorial in python.

We practiced a few examples as well using the numpy factorial in different scenarios. We also saw how to calculate the factorial of an array and multidimensional array by using the scipy module.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

Leave a Comment