From 564aa364f20e10715bfb9aadd77d41c7cc335b55 Mon Sep 17 00:00:00 2001 From: Himanshu Agrawal Date: Thu, 3 Dec 2026 00:50:54 +0530 Subject: [PATCH 1/2] DOC: show two input core dimensions in apply_ufunc --- advanced/apply_ufunc/core-dimensions.ipynb | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/advanced/apply_ufunc/core-dimensions.ipynb b/advanced/apply_ufunc/core-dimensions.ipynb index 7a009782..37263d4b 100644 --- a/advanced/apply_ufunc/core-dimensions.ipynb +++ b/advanced/apply_ufunc/core-dimensions.ipynb @@ -328,6 +328,38 @@ ")" ] }, + { + "cell_type": "markdown", + "id": "two-input-core-dims", + "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": "two-input-core-dims-example", + "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": "21", From 748f9994c032db8efe18e509d74622d339c0b6f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:31:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- advanced/apply_ufunc/core-dimensions.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/advanced/apply_ufunc/core-dimensions.ipynb b/advanced/apply_ufunc/core-dimensions.ipynb index 37263d4b..758577f8 100644 --- a/advanced/apply_ufunc/core-dimensions.ipynb +++ b/advanced/apply_ufunc/core-dimensions.ipynb @@ -330,7 +330,7 @@ }, { "cell_type": "markdown", - "id": "two-input-core-dims", + "id": "21", "metadata": {}, "source": [ "## More than one core dimension on an input\n", @@ -343,7 +343,7 @@ { "cell_type": "code", "execution_count": null, - "id": "two-input-core-dims-example", + "id": "22", "metadata": {}, "outputs": [], "source": [ @@ -362,7 +362,7 @@ }, { "cell_type": "markdown", - "id": "21", + "id": "23", "metadata": { "tags": [] },