The numpy.sqrt() function returns a non-negative square root of each element of the input array. Show
Syntaxnumpy.sqrt(x[, out]) = <ufunc ‘sqrt’> Parameters: x : [array_like] The input is array_like object. out: [ndarray][Optional] The output of the function can be copied to out variable. It should be of same shape as the result. Result: [ndarray] ndarray containing the positive square root of each element of input array. Examples,
Numpy square root of a matrixTo get the square root of matrix elements we can make use of the numpy.sqrt().
Numpy square root of 3D arraySimilar to matrices, the numpy.sqrt() function also works on multidimensional arrays. Below is an example,
Numpy square root of a numberWe can also obtain the square of scalar values using numpy.sqrt(). You can simply pass the number as the parameter.
Numpy square root of a list
Numpy square root of tuples
Numpy square root of complex numbersThe numpy.sqrt() function can also be used to find the square root of complex numbers. The square root of complex number is also a complex number. Every complex number has a square root. So, we can assume the equation z^2 = c Where c is a complex number. To determine the square root of a complex number, we can convert the value of z in the form of a + bj to equate, where a and b are real numbers. Let’s determine the square root of the complex number 21 – 20j. z^2 = 21 - 20j z = √(21 - 20j) a + bj = √(21 - 20j) (a + bj)^2 = 21 - 20j a^2 + b^2.j^2 + 2abj = 21 - 20j In complex numbers, we know j^2 = -1. Therefore, (a^2 - b^2) + 2abj = 21 - 20j If two complex numbers are equal then their real part and imaginary part are equal to each other. Hence, a^2 - b^2 = 21 and 2abj = -20j abj = -10j b = -10/a Substituting the value of b we can write the equation as a^2 - b^2 = 21 a^2 -(-10/a^2)^2 = 21 a^4 - 100/a^2 = 21 a^4 - 21a^2 - 100 = 0 (a^2 + 4)^2 . (a^2 - 25)^2 = 0 a^2 = - 4 or a^2 = 25 We know the value of a is real. Hence a = √-4 is not considered. a = 5 or a = -5 Since, numpy.sqrt returns a non-negative the value of a = 5 b = (-10/a) = (-10/5) = -2 √(21 – 20j) = a + bj √(21 – 20j) = 5 – 2j where a = 5 and b = -2 Let’s verify this using numpy.sqrt() function.
More examples,
NumPy square root of a negative numberThe numpy.sqrt() function returns a nan(not a number) for negative numbers. It also throws “RuntimeWarning: invalid value” warning.
To find the square root of negative numbers we need to consider the complex part. Let’s say we need the square root of -9, then we can evaluate the numbers as: √-9 = √-1 . √9 = 3√-1 In complex number, we know j^2 = -1 i.e, j = √-1, hence √-9 = 3j If you are interested in obtaining the above output in Numpy, simply let the sqrt function know that:
For Python use the “cmath” library.
More examples of numpy.sqrt()Numpy square root of sum of squares
numpy root mean squareTo calculate the root mean square we can make use of np.mean function along with np.sqrt function.
How do you declare a square root in Python?sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.
How do I square a value in NumPy?square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array. Parameters : arr : [array_like] Input array or object whose elements, we need to square.
|