Calculate element-wise roots
Transforming numeric arrays
NumPy · Function
Computes the non-negative square root element-wise for array-like input. Real negative values produce nan, while complex input produces complex square roots.
np.sqrt is a universal function that applies the non-negative square-root operation independently to each element of array-like input. It returns an array with the input shape, while its optional ufunc controls can direct storage, masking, casting, memory order, output type, and subclass behavior.
numpy.sqrt(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])| Name | Type | Description |
|---|---|---|
x | array_like | Values whose square-roots are required. |
out | ndarray, None, or tuple of ndarray and None, optional | Destination for the result; may be an ndarray, None, or a tuple of ndarray and None. |
where | array_like of bool, optional | Boolean or array-like mask selecting elements to compute. |
casting | {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional | Casting rule for the ufunc operation. |
order | {‘K’, ‘A’, ‘C’, ‘F’}, optional | Memory layout order of the result. |
dtype | dtype, optional | Output data type. |
subok | bool, optional | Whether to pass through subclasses of ndarray. |
signature | {None, str, tuple of ints}, optional | Generalized ufunc signature, when applicable. |
y : ndarray — An array with the same shape as x containing the positive square-root of each element. Complex input yields a complex array; real negative elements yield nan.
x as array-like input, so lists and arrays can be passed directly.out parameter lets callers specify destination storage for the result.nan.nan rather than complex roots; convert the input to a complex dtype when complex results are intended.import numpy as np
values = np.array([4.0, 9.0, 16.0])
roots = np.sqrt(values)
print(roots) # [2. 3. 4.]Computes the square root of each array element and prints the resulting array.
Transforming numeric arrays
Working with complex-valued data
Memory-conscious numerical pipelines
Conditional array computation
np.sqrt return for real and complex inputs?np.sqrt is a ufunc that returns the non-negative square root element-wise. For real input, negative values produce nan; complex input produces complex results.
Follow-up: What happens if an input array mixes real and complex values? The result is a complex array because the presence of any complex element causes complex output.
The result has the same shape as x. The out argument can provide an array or tuple of arrays where NumPy stores the computed values.
Follow-up: Why use out? It lets callers reuse storage and avoid allocating a separate result array when the destination is suitable.
where controls which elements are evaluated, while dtype, casting, order, and subok control aspects of ufunc execution and the returned or stored result. The documented signature exposes these as keyword-only options.
Follow-up: What must be handled when using where without out? Unselected positions in a newly allocated result may be uninitialized, so initialize the result if those values will be read.
numpy.emath.sqrtnumpy.squarenumpy.power