
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Compute the square root of negative input with emath in Python
To compute the square root of input, use the scimath.sqrt() method in Python Numpy. The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned. The parameter x is the input value. For negative input elements, a complex value is returned
Steps
At first, import the required libraries −
import numpy as np
Creating a numpy array using the array() method −
arr = np.array([1, -4, -9, 16, -25, 36])
Display the array −
print("Our Array...\n",arr)
Check the Dimensions −
print("\nDimensions of our Array...\n",arr.ndim)
Get the Datatype −
print("\nDatatype of our Array object...\n",arr.dtype)
Get the Shape −
print("\nShape of our Array object...\n",arr.shape)
To compute the square root of input, use the scimath.sqrt() method in Python Numpy. The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned −
print("\nResult...\n",np.emath.sqrt(arr))
Example
import numpy as np # Creating a numpy array using the array() method arr = np.array([1, -4, -9, 16, -25, 36]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute the square root of input, use the scimath.sqrt() method in Python Numpy print("\nResult...\n",np.emath.sqrt(arr))
Output
Our Array... [ 1 -4 -9 16 -25 36] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (6,) Result... [1.+0.j 0.+2.j 0.+3.j 4.+0.j 0.+5.j 6.+0.j]
- Related Articles
- Compute the square root of input with emath in Python
- Compute the square root of complex inputs with scimath in Python
- Return the non-negative square-root of an array element-wise in Numpy
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Guess Nearest Square Root in Python
- Return the square of the complex-value input in Python
- Compute the Natural logarithm for complex-valued input in Python
- Python Pandas - Compute the slice indexer for input labels
- Return the result of the negative power to which the input value is raised with scimath in Python
- Return the result of the power to which the negative input value is raised with scimath in Python
- Return the element-wise square of the array input in Python
- Replace Odd Numbers with Square root & Even Numbers with Square in Java
- Python Pandas - Compute slice locations for input labels
- How to calculate square root of a number in Python?
- How to find Square root of complex numbers in Python?

Advertisements