Find nth Root of a Number Using Numpy

In this article, we will learn about the numpy nth root of a number. We are going to learn how to solve the nth root of any number. In python, we use numpy.sqrt() to find the square root of a number. And we use numpy.cbrt() to find the third root, the cube of a number. But If we want the 5th root or 6th root of a number. What will we do? How to find this root? You will get the solutions for all these questions at the end of the article.

If B is equal to A multiplied n times, then B’s nth root would be equal to A, which is the nth root. The number in a smaller subscript tells which root it is so. Suppose we want the square root of 16. Then we can easily say two times of 4 will give 16. Likewise, if we want the cube root of 8, we say three times of 2 will give 27. Now we go for 32 roots of 5; then we have to find out what number multiplied by itself gives us 32. This is the common idea to get the nth root of any number.

How numpy.power is to get the root?

We are going to use numpy.power() to get the root of a number. It is useful to calculate the power of a number. Here we are using it to find the root. Many of you are confused about how it is possible? We know that 25 = 32. Now, if we want the root, we can interchange it as 321/5. Now we will get the root as 2. This is the change we will make in power() to get the root of a number.

What is numpy.power()?

An element in the second array is raised to the power of the element in the first array. The limit of both arrays must be the same and positive numbers.

Syntax

numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'power'>

Parameters

  • x1: base element
  • x2: power element
  • out: the result will be stored
  • where: If the value is True calculate the univerfunction. Otherwise, leave the value.
  • **kwargs: Allows to pass keyword variable length of the argument

Return

The bases in x1 are raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.

The change we are going to do in the power function

To calculate the power:

np.power(element1, element2)

To calculate the root:

np.power(element1, (1/element2))

Example 1

import numpy as np
def nthroot(a,n):
    return np.power(a,(1/n))
a=81
n=7
result=nthroot(a,n)
print(f'The {n}th root of {a} is:',result)

Explanation

  • Import numpy as np.
  • Create a function named nthroot.
  • The parameters of the function are a and n. a is the number. n is to represent which root it is for.
  • Inside a function using np.power() to calculate the root.
  • Giving the values of a and n.
  • Printing the result

Output

The 7th root of 81 is: 1.873444004574479

7 times of final result will give the value 81.

Example 2

import numpy as np
a = int(input("Enter a number:"))
n =int(input("Enter which root do you want:"))
result=(np.power(a,(1/n)))
print(f'The {n}th root of {a} is:',result)

Explanation

  • Import numpy as np
  • Get the values of a and n from the user.
  • Using np.power() to calculate the root.
  • Finally printing the result

Output

Enter a number:81
Enter which root do you want:6
The 6th root of 81 is: 2.080083823051904

6 times of final result will give the value 81.

Example 3

import numpy as np
a=90
n=4
print ("number: ",a)
print("The nth root you gave: ",n)
result=(np.power(a,(1/n)))
print(f'The {n}th root of {a} is:',result)

Explanation

  • Import numpy as np.
  • Giving the values of a and n.
  • Using np.power() to calculate the root.
  • Printing the result

Output

number:  90
The nth root you gave:  4
The 4th root of 90 is: 3.080070288241023

4 times the final result gives the value 90.

Until we saw how to get a root for a single element, now we will see how to get the root for the array of elements.

Code

import numpy as np
arr = np.arange(5)
n=4
print ("array is: ", arr)
result = np.power(arr,(1/n))
print(f'The {n}th root of {arr[0]} is:',result[0])
print(f'The {n}th root of {arr[1]} is:',result[1])
print(f'The {n}th root of {arr[2]} is:',result[2])
print(f'The {n}th root of {arr[3]} is:',result[3])
print(f'The {n}th root of {arr[4]} is:',result[4])

Explanation

  • Import numpy as np
  • Giving the n value as 4.
  • So we are going to calculate the 4th root of the every array element.
  • Array values are given from 0 to 4.Printing the 4th root of every array elements.

Output

array is:  [0 1 2 3 4]
The 4th root of 0 is: 0.0
The 4th root of 1 is: 1.0
The 4th root of 2 is: 1.189207115002721
The 4th root of 3 is: 1.3160740129524924
The 4th root of 4 is: 1.4142135623730951

Learn how to calculate the sqrt() and cbrt() using numpy

import numpy as np
square_root=np.sqrt(16)
print("The square root of 16 is:",int(square_root))
cube_root=np.cbrt(27)
print("The cube root of 27 is:",int(cube_root))

Explanation

  • Import numpy as np
  • Calculating square root using a built-in function np.sqrt()
  • Next calculating the cube root using a buil-in function np.cbrt()
  • Printing the result.

Output

The square root of 16 is: 4
The cube root of 27 is: 3

1. Which function in numpy is useful to calculate the nth root of a number?

numpy.power() is useful to calculate the nth root of a number.

2. How to calculate the square root of a number using numpy?

numpy.sqrt() is useful to calculate the square root of a number.

3. How to calculate the cube root of a number using numpy?

cbrt() is useful to calculate the square root of a number.

Conclusion

Here we learned about the numpy nth root of a number briefly. We have used numpy.power() to get the root of a number. We hope this article is beneficial for all. Try to solve all the python programs on your own. Learn python in a python pool.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments