You want to iterate over elements of a list and apply the sqrt function on it so. You can use the built-in function map which will apply the first argument to every elements of the second:
Copylst = [-1, 3, -8]
results = map(cmath.sqrt, lst)
Another way is with the classic list comprehension:
Copylst = [-1, 3, -8]
results = [cmath.sqrt(x) for x in lst]
Execution example:
Copy>>> lst = [-4, 3, -8, -9]
>>> map(cmath.sqrt, lst)
[2j, (1.7320508075688772+0j), 2.8284271247461903j, 3j]
>>> [cmath.sqrt(x) for x in lst]
[2j, (1.7320508075688772+0j), 2.8284271247461903j, 3j]
If you're using Python 3, you may have to apply list() on the result of map (or you'll have a ietrator object)
You want to iterate over elements of a list and apply the sqrt function on it so. You can use the built-in function map which will apply the first argument to every elements of the second:
Copylst = [-1, 3, -8]
results = map(cmath.sqrt, lst)
Another way is with the classic list comprehension:
Copylst = [-1, 3, -8]
results = [cmath.sqrt(x) for x in lst]
Execution example:
Copy>>> lst = [-4, 3, -8, -9]
>>> map(cmath.sqrt, lst)
[2j, (1.7320508075688772+0j), 2.8284271247461903j, 3j]
>>> [cmath.sqrt(x) for x in lst]
[2j, (1.7320508075688772+0j), 2.8284271247461903j, 3j]
If you're using Python 3, you may have to apply list() on the result of map (or you'll have a ietrator object)
Copyimport cmath, random
arr = [random.randint(-100, -1) for _ in range(10)]
sqrt_arr = [cmath.sqrt(i) for i in arr]
print(list(zip(arr, sqrt_arr)))
Result:
Copy[(-43, 6.557438524302j), (-80, 8.94427190999916j), (-15, 3.872983346207417j), (-1, 1j), (-60, 7.745966692414834j), (-29, 5.385164807134504j), (-2, 1.4142135623730951j), (-49, 7j), (-25, 5j), (-45, 6.708203932499369j)]
Videos
Does anyone know how to return the negative value of a square root? Normal operators only produce positive values. For example 25**(1/2) returns a value of 5. I’m looking for a calculation that would return a collection -5, 5. Is this possible?
Shifting this discussion from the comments...
In Python 3.0 the behaviour of the power operator changed. In earlier versions of python, raising a negative number to a fractional power raised a ValueError exception, but in Python 3 it yields a complex result.
An alternative for finding the square root is the python is the math.sqrt function. In Python 3 this raises a ValueError exception when used with a negative number:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.sqrt(-200)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
If python 3 returns a complex number and that is not what you want, you can always achieve the desired output with an if statement:
def sqRoot(x):
if not isinstance(x, (int, long, float)):
return "Please enter a number"
if x < 0:
return "This is a negative root"
return (x)**0.5
To avoid the invalid value warning/error, the argument to numpy's sqrt function must be complex:
In [8]: import numpy as np
In [9]: np.sqrt(-1+0j)
Out[9]: 1j
As @AshwiniChaudhary pointed out in a comment, you could also use the cmath standard library:
In [10]: cmath.sqrt(-1)
Out[10]: 1j
I just discovered the convenience function numpy.emath.sqrt explained in the sqrt documentation. I use it as follows:
>>> from numpy.emath import sqrt as csqrt
>>> csqrt(-1)
1j