Numpy.reciprocal(): Return Element-wise Reciprocal using Numpy?

Numpy Reciprocal

Numbers are interesting entities to be dealt with. They have many peculiar properties that can be exercised as and when needed. They exist in many forms such as decimals, integers, and whole numbers, and can be bent to the desire of the person using them.

They even exist in the paradigms of both real and imaginary. One such form which these numbers take is the fraction, which seems to exist with all the integers.  

Reciprocating is a property by which the numerator and the denominator are swapped in position to determine the resulting number. In this article, we will be looking into the different ways of reciprocating numbers and arrays using the functions from the numpy library in Python with the aid of the following sections.

  • Mind the Decimal!
  • Using the reciprocal( ) function
  • Reciprocating with formula

Mind the Decimal!

Let us start things off by importing the numpy library within Python using the following code.

import numpy as np

We shall now demonstrate the crucial role that the decimal point plays while exercising the reciprocal( ) function from the numpy library in Python. A random number is assigned to a variable which is then put through the reciprocal( ) function as shown in the below code.

a = 3
r1 = np.reciprocal (a)

Now let us have a look at the result by using the print( ) function.

print (r1)
Incorrect Result For Reciprocating ‘3
Incorrect Result For Reciprocating ‘3′

It is strange to notice that the reciprocal of ‘3’ is given as zero rather than 0.33! This can be explained by the missing decimal point. Let us repeat the same but this time using a number with decimals as given below.

a = 3.14
Correct Result Of Reciprocating
Correct Result Of Reciprocating

Thusly, it is imperative that one bears in mind the usage of the decimal point while seeking to reciprocate numbers using the numpy functions in Python. The result of the above demonstration holds good even for numbers with only zeros in their decimals such as ‘3.0’.

a = 3.                  
Result Of Reciprocating ‘3
Result Of Reciprocating ‘3.’

Using Numpy.reciprocal( ) function

Let us try to extend the reciprocation to a bunch of numbers in an array this time. So, we get started by constructing an array and assigning it to a variable just as shown in the following code.

ar = np.array([[2.5, 6.4, 3],
              [1, 5, 0.9]])
Constructing An Array
Constructing An Array

Once done, we can move ahead to determine the reciprocal of the declared array by using the below code.

r2 = np.reciprocal (ar)
print (r2)
Reciprocal Of An Array
Reciprocal Of An Array

It could be very well observed in the input array that certain numerical entries within it do not possess the decimal point whilst others do. This indicates that if one specifies at least one decimal point within a collection of numbers in an array, Python recognizes all the other entities too as a floating-type integer!

Reciprocating with the formula

This is a pretty straightforward approach where one shall topple a given set of arrays by constructing a formula to send all the numbers to the denominator dividing ‘1’. Better done than said! So here’s the code which demonstrates the aforementioned.

ar = np.array([[2.5, 6.4, 3],
              [1, 5, 0.9]])              
r3 = 1/ar                  
print (r3)
Result Of Reciprocating Using Formula
Result Of Reciprocating Using Formula


Now that we have reached the end of this article, hope it has elaborated on how to return the reciprocal of the elements within an array using the numpy.reciprocal( ) function in Python. Here’s another article that details how to round integers near zero using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, adios!