MCPcopy Create free account
hub / github.com/numpy/numpy / sqrt

Function sqrt

numpy/lib/scimath.py:199–248  ·  view source on GitHub ↗

Compute the square root of x. For negative input elements, a complex value is returned (unlike `numpy.sqrt` which returns NaN). Parameters ---------- x : array_like The input value(s). Returns ------- out : ndarray or scalar The square root of `x

(x)

Source from the content-addressed store, hash-verified

197
198@array_function_dispatch(_unary_dispatcher)
199def sqrt(x):
200 """
201 Compute the square root of x.
202
203 For negative input elements, a complex value is returned
204 (unlike `numpy.sqrt` which returns NaN).
205
206 Parameters
207 ----------
208 x : array_like
209 The input value(s).
210
211 Returns
212 -------
213 out : ndarray or scalar
214 The square root of `x`. If `x` was a scalar, so is `out`,
215 otherwise an array is returned.
216
217 See Also
218 --------
219 numpy.sqrt
220
221 Examples
222 --------
223 For real, non-negative inputs this works just like `numpy.sqrt`:
224
225 >>> np.emath.sqrt(1)
226 1.0
227 >>> np.emath.sqrt([1, 4])
228 array([1., 2.])
229
230 But it automatically handles negative inputs:
231
232 >>> np.emath.sqrt(-1)
233 1j
234 >>> np.emath.sqrt([-1,4])
235 array([0.+1.j, 2.+0.j])
236
237 Different results are expected because:
238 floating point 0.0 and -0.0 are distinct.
239
240 For more control, explicitly use complex() as follows:
241
242 >>> np.emath.sqrt(complex(-4.0, 0.0))
243 2j
244 >>> np.emath.sqrt(complex(-4.0, -0.0))
245 -2j
246 """
247 x = _fix_real_lt_zero(x)
248 return nx.sqrt(x)
249
250
251@array_function_dispatch(_unary_dispatcher)

Callers 15

__init__Method · 0.90
test_complexMethod · 0.90
test_testUfuncs1Method · 0.90
corrcoefFunction · 0.70
_i0_2Function · 0.70
kaiserFunction · 0.70
user_array.pyFile · 0.70
legacy_gaussFunction · 0.50
legacy_standard_gammaFunction · 0.50
legacy_rayleighFunction · 0.50
legacy_waldFunction · 0.50

Calls 1

_fix_real_lt_zeroFunction · 0.85

Tested by 5

__init__Method · 0.72
test_complexMethod · 0.72
test_testUfuncs1Method · 0.72
test_basic_ufuncsMethod · 0.40