Apply an elementwise ufunc-like function blockwise across arguments. Like numpy ufuncs, broadcasting rules are respected. Parameters ---------- op : callable The function to apply. Should be numpy ufunc-like in the parameters that it accepts. *args : Any
(op, *args, out=None, where=True, dtype=None, name=None, **kwargs)
| 5087 | |
| 5088 | |
| 5089 | def elemwise(op, *args, out=None, where=True, dtype=None, name=None, **kwargs): |
| 5090 | """Apply an elementwise ufunc-like function blockwise across arguments. |
| 5091 | |
| 5092 | Like numpy ufuncs, broadcasting rules are respected. |
| 5093 | |
| 5094 | Parameters |
| 5095 | ---------- |
| 5096 | op : callable |
| 5097 | The function to apply. Should be numpy ufunc-like in the parameters |
| 5098 | that it accepts. |
| 5099 | *args : Any |
| 5100 | Arguments to pass to `op`. Non-dask array-like objects are first |
| 5101 | converted to dask arrays, then all arrays are broadcast together before |
| 5102 | applying the function blockwise across all arguments. Any scalar |
| 5103 | arguments are passed as-is following normal numpy ufunc behavior. |
| 5104 | out : dask array, optional |
| 5105 | If out is a dask.array then this overwrites the contents of that array |
| 5106 | with the result. |
| 5107 | where : array_like, optional |
| 5108 | An optional boolean mask marking locations where the ufunc should be |
| 5109 | applied. Can be a scalar, dask array, or any other array-like object. |
| 5110 | Mirrors the ``where`` argument to numpy ufuncs, see e.g. ``numpy.add`` |
| 5111 | for more information. |
| 5112 | dtype : dtype, optional |
| 5113 | If provided, overrides the output array dtype. |
| 5114 | name : str, optional |
| 5115 | A unique key name to use when building the backing dask graph. If not |
| 5116 | provided, one will be automatically generated based on the input |
| 5117 | arguments. |
| 5118 | |
| 5119 | Examples |
| 5120 | -------- |
| 5121 | >>> elemwise(add, x, y) # doctest: +SKIP |
| 5122 | >>> elemwise(sin, x) # doctest: +SKIP |
| 5123 | >>> elemwise(sin, x, out=dask_array) # doctest: +SKIP |
| 5124 | |
| 5125 | See Also |
| 5126 | -------- |
| 5127 | blockwise |
| 5128 | """ |
| 5129 | if kwargs: |
| 5130 | raise TypeError( |
| 5131 | f"{op.__name__} does not take the following keyword arguments " |
| 5132 | f"{sorted(kwargs)}" |
| 5133 | ) |
| 5134 | |
| 5135 | out = _elemwise_normalize_out(out) |
| 5136 | where = _elemwise_normalize_where(where) |
| 5137 | args = [np.asarray(a) if isinstance(a, (list, tuple)) else a for a in args] |
| 5138 | |
| 5139 | shapes = [] |
| 5140 | for arg in args: |
| 5141 | shape = getattr(arg, "shape", ()) |
| 5142 | if any(is_dask_collection(x) for x in shape): |
| 5143 | # Want to exclude Delayed shapes and dd.Scalar |
| 5144 | shape = () |
| 5145 | shapes.append(shape) |
| 5146 | if isinstance(where, Array): |
no test coverage detected
searching dependent graphs…