python - Square root of all values in numpy array, preserving sign -


i'd take square root of every value in numpy array, while preserving sign of value (and not returning complex numbers when negative) - signed square root.

the code below demonstrates desired functionality w/ lists, not taking advantage of numpy's optimized array manipulating superpowers.

def signed_sqrt(list):     new_list = []     v in arr:         sign = 1         if v < 0:             sign = -1         sqrt = cmath.sqrt(abs(v))         new_v = sqrt * sign         new_list.append(new_v)   list = [1., 81., -7., 4., -16.] list = signed_sqrt(list) # [1., 9., -2.6457, 2. -4.] 

for context, i'm computing hellinger kernel [thousands of] image comparisons.

any smooth way numpy? thanks.

you can try using numpy.sign function capture sign, , take square root of absolute value.

import numpy np x = np.array([-1, 1, 100, 16, -100, -16]) y = np.sqrt(np.abs(x)) * np.sign(x) # [-1, 1, 10, 4, -10, -4] 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -