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)
| 185 | @set_module('numpy.lib.scimath') |
| 186 | @array_function_dispatch(_unary_dispatcher) |
| 187 | def sqrt(x): |
| 188 | """ |
| 189 | Compute the square root of x. |
| 190 | |
| 191 | For negative input elements, a complex value is returned |
| 192 | (unlike `numpy.sqrt` which returns NaN). |
| 193 | |
| 194 | Parameters |
| 195 | ---------- |
| 196 | x : array_like |
| 197 | The input value(s). |
| 198 | |
| 199 | Returns |
| 200 | ------- |
| 201 | out : ndarray or scalar |
| 202 | The square root of `x`. If `x` was a scalar, so is `out`, |
| 203 | otherwise an array is returned. |
| 204 | |
| 205 | See Also |
| 206 | -------- |
| 207 | numpy.sqrt |
| 208 | |
| 209 | Examples |
| 210 | -------- |
| 211 | For real, non-negative inputs this works just like `numpy.sqrt`: |
| 212 | |
| 213 | >>> import numpy as np |
| 214 | |
| 215 | >>> np.emath.sqrt(1) |
| 216 | 1.0 |
| 217 | >>> np.emath.sqrt([1, 4]) |
| 218 | array([1., 2.]) |
| 219 | |
| 220 | But it automatically handles negative inputs: |
| 221 | |
| 222 | >>> np.emath.sqrt(-1) |
| 223 | 1j |
| 224 | >>> np.emath.sqrt([-1,4]) |
| 225 | array([0.+1.j, 2.+0.j]) |
| 226 | |
| 227 | Different results are expected because: |
| 228 | floating point 0.0 and -0.0 are distinct. |
| 229 | |
| 230 | For more control, explicitly use complex() as follows: |
| 231 | |
| 232 | >>> np.emath.sqrt(complex(-4.0, 0.0)) |
| 233 | 2j |
| 234 | >>> np.emath.sqrt(complex(-4.0, -0.0)) |
| 235 | -2j |
| 236 | """ |
| 237 | x = _fix_real_lt_zero(x) |
| 238 | return nx.sqrt(x) |
| 239 | |
| 240 | |
| 241 | @set_module('numpy.lib.scimath') |
searching dependent graphs…