Open In App

Compute the square root of negative input with emath in Python

Last Updated : 01 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy.

Example:

Input: [-3,-4]
Output: [0.+1.73205081j 0.+2.j        ]
Explanation: Square root of a negative input.

NumPy.emath.sqrt method:

The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex value is returned for negative input elements unlike numpy.sqrt. which returns NaN.

Syntax: np.emath.sqrt()

Parameters:

  • x: array like object.

Return: out: scalar or ndarray.

Example 1:

If the array contains negative input values, complex numbers are returned in the output, and the shape, datatype, and dimensions of the array can be found by .shape, .dtype, and .ndim attributes.

Python3




import numpy as np
  
# Creating an array
arr = np.array([-3, -4])
print(array)
  
# shape of the array is
print("Shape of the array is : ",arr.shape)
  
# dimension of the array
print("The dimension of the array is : ",arr.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",arr.dtype)
  
# computing the square root of negative inputs
print(np.emath.sqrt(arr))


Output:

[-3 -4]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  int64
[0.+1.73205081j 0.+2.j        ]

Example 2:

Python3




import numpy as np
  
# Creating an array
array = np.arr([complex(-2, -1),complex(-5, -3)])
print(array)
  
# shape of the array is
print("Shape of the array is : ",arr.shape)
  
# dimension of the array
print("The dimension of the array is : ",arr.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",arr.dtype)
  
# computing the square root of complex inputs
print(np.emath.sqrt(arr))


Output:

[-2.-1.j -5.-3.j]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  complex128
[0.34356075-1.45534669j 0.64457424-2.32711752j]


Similar Reads

Compute the square root of complex inputs with scimath in Python
In this article, we will cover how to compute the square root of complex inputs with scimath in Python using NumPy. ExampleInput: [-1 -2] Output: [0.+1.j 0.+1.41421356j] Explanation: Square root of complex input.NumPy.emath.sqrt method The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex value is
3 min read
How to compute numerical negative value for all elements in a given NumPy array?
In this article, we will see how to compute the negative value for all elements in a given NumPy array. So, The negative value is actually the number which when added to any number becomes 0. Example: If we take a number as 4 then -4 is its negative number because when we add -4 to 4 we get sum as 0. Now let us take another example, Suppose we take
2 min read
Compute the determinant of a given square array using NumPy in Python
In Python, the determinant of a square array can be easily calculated using the NumPy package. This package is used to perform mathematical calculations on single and multi-dimensional arrays. numpy.linalg is an important module of NumPy package which is used for linear algebra. We can use det() function of numpy.linalg module to find out the deter
2 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
How to compute the eigenvalues and right eigenvectors of a given square array using NumPY?
In this article, we will discuss how to compute the eigenvalues and right eigenvectors of a given square array using NumPy library. Example: Suppose we have a matrix as: [[1,2], [2,3]] Eigenvalue we get from this matrix or square array is: [-0.23606798 4.23606798] Eigenvectors of this matrix are: [[-0.85065081 -0.52573111], [ 0.52573111 -0.85065081
2 min read
How to compute the inverse of a square matrix in PyTorch
In this article, we are going to cover how to compute the inverse of a square matrix in PyTorch. torch.linalg.inv() method we can compute the inverse of the matrix by using torch.linalg.inv() method. It accepts a square matrix and a batch of the square matrices as input. If the input is a batch of the square matrices then the output will also have
2 min read
How to compute the eigenvalues and eigenvectors of a square matrix in PyTorch?
In this article, we are going to discuss how to compute the eigenvalues and eigenvectors of a square matrix in PyTorch. Compute the eigenvalues and eigenvectors of a square matrix in PyTorch torch.linalg.eig() method computes the eigenvalue decomposition of a square matrix or a batch of matrices. The decomposition exists if the input matrix is diag
2 min read
How to compute the element-wise angle of given input tensor in PyTorch?
In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angle() method to compute the element-wise angle, the
3 min read
How to compute element-wise entropy of an input tensor in PyTorch
In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input and returns a tensor with the element-wise entropy
2 min read
How to Compute the Heaviside Step Function for Each Element in Input in PyTorch?
In this article, we are going to cover how to compute the Heaviside step function for each element in input in PyTorch using Python. We can compute this with the help of torch.heaviside() method. torch.heaviside() method The torch.heaviside() method is used to compute the Heaviside step function for each element. This method accepts input and value
2 min read