{"id":28093,"date":"2022-05-30T16:28:33","date_gmt":"2022-05-30T10:58:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=28093"},"modified":"2022-05-30T16:28:33","modified_gmt":"2022-05-30T10:58:33","slug":"how-to-calculate-the-square-root-of-negative-input-with-emath-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-calculate-the-square-root-of-negative-input-with-emath-in-python\/","title":{"rendered":"How to calculate the Square Root of Negative Input with emath in Python?"},"content":{"rendered":"
Let us see\u00a0how to use NumPy to compute the square root of negative inputs with emath in Python.<\/p>\n
Python numPy.emath.sqrt() Function:<\/strong><\/p>\n The numpy.emath.sqrt() function of the numpy module is used to\u00a0compute\u00a0the square root of complex inputs. In contrast to numpy.sqrt, which returns NaN for negative input elements, complex values are returned.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> It is an array-like object given as input.<\/p>\n Return Value:<\/strong> If the array contains negative input values, complex numbers are returned in the output, and the .shape, dtype, and .ndim attributes can be used to determine the array’s shape, datatype, and dimensions respectively.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Let us see\u00a0how to use NumPy to compute the square root of negative inputs with emath in Python. Python numPy.emath.sqrt() Function: The numpy.emath.sqrt() function of the numpy module is used to\u00a0compute\u00a0the square root of complex inputs. In contrast to numpy.sqrt, which returns NaN for negative input elements, complex values are returned. Syntax: numpy.emath.sqrt() Parameters x: …<\/p>\nnumpy.emath.sqrt()<\/pre>\n
\nA scalar value or an ndarray is returned by the numpy.emath.sqrt() function.<\/p>\nPython Program to Calculate the Square Root of Negative Input with emath<\/h2>\n
For Negative Array Values:<\/h3>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n\r\n# Pass some random list of negative numbers as an argument to the array() function\r\n# of the numpy module to create an array.\r\n# Store it in a variable\r\ngvn_arry = np.array([-2, -5, -3])\r\n\r\n# Print the given array\r\nprint(\"The given array is:\")\r\nprint(gvn_arry)\r\n\r\n# Apply shape attribute on the given array to get the shape of the given array and\r\n# print the result\r\nprint(\"The Shape of the given array \",gvn_arry,\" = \", gvn_arry.shape)\r\n\r\n# Apply ndim attribute on the given array to get the dimension of the given array and\r\n# print the result\r\nprint(\"The dimension of the given array \",gvn_arry,\" = \",gvn_arry.ndim)\r\n\r\n# Apply dtype attribute on the given array to get the datatype of all the elements of the\r\n# given array and print the result\r\nprint(\"The datatype of the given array \",gvn_arry,\" = \",gvn_arry.dtype)\r\n\r\n# Pass the given array as an argument to the sqrt() function of emath of the numpy module\r\n# to calculate the square root of the given negative input array and print it.\r\nprint(np.emath.sqrt(gvn_arry))<\/pre>\n
The given array is:\r\n[-2 -5 -3]\r\nThe Shape of the given array [-2 -5 -3] = (3,)\r\nThe dimension of the given array [-2 -5 -3] = 1\r\nThe datatype of the given array [-2 -5 -3] = int64\r\n[0.+1.41421356j 0.+2.23606798j 0.+1.73205081j]<\/pre>\n
For Complex numbers:<\/h3>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n\r\n# Pass some random list of complex numbers using the complex() function as an argument\r\n# to the array() function of the numpy module to create an array.\r\n# Store it in a variable\r\ngvn_arry = np.array([complex(-3, -1),complex(-2, -4)])\r\n\r\n# Print the given array\r\nprint(\"The given array is:\")\r\nprint(gvn_arry)\r\n\r\n# Apply shape attribute on the given array to get the shape of the given array and\r\n# print the result\r\nprint(\"The Shape of the given array \",gvn_arry,\" = \", gvn_arry.shape)\r\n\r\n# Apply ndim attribute on the given array to get the dimension of the given array and\r\n# print the result\r\nprint(\"The dimension of the given array \",gvn_arry,\" = \",gvn_arry.ndim)\r\n\r\n# Apply dtype attribute on the given array to get the datatype of all the elements of the\r\n# given array and print the result\r\nprint(\"The datatype of the given array \",gvn_arry,\" = \",gvn_arry.dtype)\r\n\r\n# Pass the given array as an argument to the sqrt() function of emath of the numpy module\r\n# to calculate the square root of the given array of complex values and print it.\r\nprint(np.emath.sqrt(gvn_arry))<\/pre>\n
The given array is:\r\n[-3.-1.j -2.-4.j]\r\nThe Shape of the given array [-3.-1.j -2.-4.j] = (2,)\r\nThe dimension of the given array [-3.-1.j -2.-4.j] = 1\r\nThe datatype of the given array [-3.-1.j -2.-4.j] = complex128\r\n[0.28484878-1.7553173j 1.11178594-1.79890744j]<\/pre>\n","protected":false},"excerpt":{"rendered":"