diff --git a/advanced/apply_ufunc/core-dimensions.ipynb b/advanced/apply_ufunc/core-dimensions.ipynb index 7a009782..758577f8 100644 --- a/advanced/apply_ufunc/core-dimensions.ipynb +++ b/advanced/apply_ufunc/core-dimensions.ipynb @@ -331,6 +331,38 @@ { "cell_type": "markdown", "id": "21", + "metadata": {}, + "source": [ + "## More than one core dimension on an input\n", + "\n", + "The inner list in `input_core_dims` names all the core dimensions for one input. To compute a matrix norm separately for each sample, pass `[[\"y\", \"x\"]]`. Xarray moves `y` and `x` to the last two axes in that order, even though `sample` is between them in the original array.\n", + "\n", + "`axis=(-2, -1)` tells NumPy to reduce both of those axes. Neither core dimension remains in the output, so only `sample` is left." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22", + "metadata": {}, + "outputs": [], + "source": [ + "field = xr.DataArray(\n", + " [[[3, 0], [0, 0]], [[0, 4], [0, 12]]],\n", + " dims=(\"y\", \"sample\", \"x\"),\n", + " coords={\"sample\": [\"first\", \"second\"]},\n", + ")\n", + "xr.apply_ufunc(\n", + " np.linalg.norm,\n", + " field,\n", + " input_core_dims=[[\"y\", \"x\"]],\n", + " kwargs={\"axis\": (-2, -1)},\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "23", "metadata": { "tags": [] },