Skip to content

Commit

Permalink
DOC: fix a small np.einsum example (numpy#25707)
Browse files Browse the repository at this point in the history
* DOC: fix a small np.einsum example

"..." is required if `a` is multi-dimensional, e.g.:
a = np.arange(25).reshape(5,5)

Otherwise, this error is raised:
ValueError: operand has more dimensions than subscripts given in
einstein sum, but no '...' ellipsis provided to broadcast the extra
dimensions.
  • Loading branch information
VladimirFokow authored Feb 3, 2024
1 parent b1c3f9b commit 4cba441
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 5 additions & 2 deletions numpy/_core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,8 +2009,9 @@
identifier '->' as well as the list of output subscript labels.
This feature increases the flexibility of the function since
summing can be disabled or forced when required. The call
``np.einsum('i->', a)`` is like :py:func:`np.sum(a, axis=-1) <numpy.sum>`,
and ``np.einsum('ii->i', a)`` is like :py:func:`np.diag(a) <numpy.diag>`.
``np.einsum('i->', a)`` is like :py:func:`np.sum(a) <numpy.sum>`
if ``a`` is a 1-D array, and ``np.einsum('ii->i', a)``
is like :py:func:`np.diag(a) <numpy.diag>` if ``a`` is a square 2-D array.
The difference is that `einsum` does not allow broadcasting by default.
Additionally ``np.einsum('ij,jh->ih', a, b)`` directly specifies the
order of the output subscript labels and therefore returns matrix
Expand All @@ -2019,6 +2020,8 @@
To enable and control broadcasting, use an ellipsis. Default
NumPy-style broadcasting is done by adding an ellipsis
to the left of each term, like ``np.einsum('...ii->...i', a)``.
``np.einsum('...i->...', a)`` is like
:py:func:`np.sum(a, axis=-1) <numpy.sum>` for array ``a`` of any shape.
To take the trace along the first and last axes,
you can do ``np.einsum('i...i', a)``, or to do a matrix-matrix
product with the left-most indices instead of rightmost, one can do
Expand Down
16 changes: 9 additions & 7 deletions numpy/_core/einsumfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,17 +1180,19 @@ def einsum(*operands, out=None, optimize=False, **kwargs):
identifier '->' as well as the list of output subscript labels.
This feature increases the flexibility of the function since
summing can be disabled or forced when required. The call
``np.einsum('i->', a)`` is like
:py:func:`np.sum(a, axis=-1) <numpy.sum>`, and ``np.einsum('ii->i', a)``
is like :py:func:`np.diag(a) <numpy.diag>`. The difference is that
`einsum` does not allow broadcasting by default. Additionally
``np.einsum('ij,jh->ih', a, b)`` directly specifies the order of
the output subscript labels and therefore returns matrix multiplication,
unlike the example above in implicit mode.
``np.einsum('i->', a)`` is like :py:func:`np.sum(a) <numpy.sum>`
if ``a`` is a 1-D array, and ``np.einsum('ii->i', a)``
is like :py:func:`np.diag(a) <numpy.diag>` if ``a`` is a square 2-D array.
The difference is that `einsum` does not allow broadcasting by default.
Additionally ``np.einsum('ij,jh->ih', a, b)`` directly specifies the
order of the output subscript labels and therefore returns matrix
multiplication, unlike the example above in implicit mode.
To enable and control broadcasting, use an ellipsis. Default
NumPy-style broadcasting is done by adding an ellipsis
to the left of each term, like ``np.einsum('...ii->...i', a)``.
``np.einsum('...i->...', a)`` is like
:py:func:`np.sum(a, axis=-1) <numpy.sum>` for array ``a`` of any shape.
To take the trace along the first and last axes,
you can do ``np.einsum('i...i', a)``, or to do a matrix-matrix
product with the left-most indices instead of rightmost, one can do
Expand Down

0 comments on commit 4cba441

Please sign in to comment.