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)
| 197 | |
| 198 | @array_function_dispatch(_unary_dispatcher) |
| 199 | def 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) |