Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions advanced/apply_ufunc/core-dimensions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
},
Expand Down