def safe_inverse_root(d): if (d < 0).any(): raise ValueError return np.piecewise(d, [d>0, d==0], [lambda x: x**(-0.5), lambda x: x])