Tutorial

NumPy sqrt() - Square Root of Matrix Elements

Published on August 4, 2022
author

Pankaj

NumPy sqrt() - Square Root of Matrix Elements

Python NumPy module is used to work with multidimensional arrays and matrix manipulations. We can use NumPy sqrt() function to get the square root of the matrix elements.

Python NumPy sqrt() Example

import numpy

array_2d = numpy.array([[1, 4], [9, 16]], dtype=numpy.float)

print(array_2d)

array_2d_sqrt = numpy.sqrt(array_2d)

print(array_2d_sqrt)

Output:

[[ 1.  4.]
 [ 9. 16.]]
[[1. 2.]
 [3. 4.]]
Python Numpy Sqrt Example
Python Numpy sqrt() Example

Let’s look at another example where the matrix elements are not square of integers. This time we will use the Python interpreter.

>>> import numpy
>>> 
>>> array = numpy.array([[1, 3], [5, 7]], dtype=numpy.float)
>>> 
>>> print(array)
[[1. 3.]
 [5. 7.]]
>>> 
>>> array_sqrt = numpy.sqrt(array)
>>> 
>>> print(array_sqrt)
[[1.         1.73205081]
 [2.23606798 2.64575131]]
>>> 

NumPy sqrt() Infinity Example

Let’s see what happens when we have infinity as the matrix element.

>>> array = numpy.array([1, numpy.inf])
>>> 
>>> numpy.sqrt(array)
array([ 1., inf])
>>> 

Complex Numbers

>>> array = numpy.array([1 + 2j, -3 + 4j], dtype=numpy.complex)
>>> 
>>> numpy.sqrt(array)
array([1.27201965+0.78615138j, 1.        +2.j        ])
>>> 
Numpy Sqrt Complex Numbers
Numpy Sqrt Complex Numbers

Negative Numbers

>>> array = numpy.array([4, -4])
>>> 
>>> numpy.sqrt(array)
__main__:1: RuntimeWarning: invalid value encountered in sqrt
array([ 2., nan])
>>> 

The square root of a matrix with negative numbers will throw RuntimeWarning and the square root of the element is returned as nan. Reference: NumPy Docs

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.