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)
| 4885 | |
| 4886 | |
| 4887 | def elemwise(op, *args, out=None, where=True, dtype=None, name=None, **kwargs): |
| 4888 | """Apply an elementwise ufunc-like function blockwise across arguments. |
| 4889 | |
| 4890 | Like numpy ufuncs, broadcasting rules are respected. |
| 4891 | |
| 4892 | Parameters |
| 4893 | ---------- |
| 4894 | op : callable |
| 4895 | The function to apply. Should be numpy ufunc-like in the parameters |
| 4896 | that it accepts. |
| 4897 | *args : Any |
| 4898 | Arguments to pass to `op`. Non-dask array-like objects are first |
| 4899 | converted to dask arrays, then all arrays are broadcast together before |
| 4900 | applying the function blockwise across all arguments. Any scalar |
| 4901 | arguments are passed as-is following normal numpy ufunc behavior. |
| 4902 | out : dask array, optional |
| 4903 | If out is a dask.array then this overwrites the contents of that array |
| 4904 | with the result. |
| 4905 | where : array_like, optional |
| 4906 | An optional boolean mask marking locations where the ufunc should be |
| 4907 | applied. Can be a scalar, dask array, or any other array-like object. |
| 4908 | Mirrors the ``where`` argument to numpy ufuncs, see e.g. ``numpy.add`` |
| 4909 | for more information. |
| 4910 | dtype : dtype, optional |
| 4911 | If provided, overrides the output array dtype. |
| 4912 | name : str, optional |
| 4913 | A unique key name to use when building the backing dask graph. If not |
| 4914 | provided, one will be automatically generated based on the input |
| 4915 | arguments. |
| 4916 | |
| 4917 | Examples |
| 4918 | -------- |
| 4919 | >>> elemwise(add, x, y) # doctest: +SKIP |
| 4920 | >>> elemwise(sin, x) # doctest: +SKIP |
| 4921 | >>> elemwise(sin, x, out=dask_array) # doctest: +SKIP |
| 4922 | |
| 4923 | See Also |
| 4924 | -------- |
| 4925 | blockwise |
| 4926 | """ |
| 4927 | if kwargs: |
| 4928 | raise TypeError( |
| 4929 | f"{op.__name__} does not take the following keyword arguments " |
| 4930 | f"{sorted(kwargs)}" |
| 4931 | ) |
| 4932 | |
| 4933 | out = _elemwise_normalize_out(out) |
| 4934 | where = _elemwise_normalize_where(where) |
| 4935 | args = [np.asarray(a) if isinstance(a, (list, tuple)) else a for a in args] |
| 4936 | |
| 4937 | shapes = [] |
| 4938 | for arg in args: |
| 4939 | shape = getattr(arg, "shape", ()) |
| 4940 | if any(is_dask_collection(x) for x in shape): |
| 4941 | # Want to exclude Delayed shapes and dd.Scalar |
| 4942 | shape = () |
| 4943 | shapes.append(shape) |
| 4944 | if isinstance(where, Array): |
no test coverage detected