Skip to content

Einsum changes #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
7 commits merged into from
Feb 3, 2011
Merged
Show file tree
Hide file tree
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
51 changes: 42 additions & 9 deletions numpy/add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,10 @@

Evaluates the Einstein summation convention on the operands.

An alternative way to provide the subscripts and operands is as
einsum(op0, sublist0, op1, sublist1, ..., [sublistout]). The examples
below have corresponding einsum calls with the two parameter methods.

Using the Einstein summation convention, many common multi-dimensional
array operations can be represented in a simple fashion. This function
provides a way compute such summations.
Expand All @@ -1540,13 +1544,16 @@
``np.einsum('ji', a)`` takes its transpose.

The output can be controlled by specifying output subscript labels
as well. This specifies the label order, and allows summing to be
disallowed or forced when desired. The call ``np.einsum('i->', a)``
is equivalent to ``np.sum(a, axis=-1)``, and
``np.einsum('ii->i', a)`` is equivalent to ``np.diag(a)``.

It is also possible to control how broadcasting occurs using
an ellipsis. To take the trace along the first and last axes,
as well. This specifies the label order, and allows summing to
be disallowed or forced when desired. The call ``np.einsum('i->', a)``
is like ``np.sum(a, axis=-1)``, and ``np.einsum('ii->i', a)``
is like ``np.diag(a)``. The difference is that ``einsum`` does not
allow broadcasting by default.

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)``.
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, you can do
``np.einsum('ij...,jk...->ik...', a, b)``.
Expand Down Expand Up @@ -1602,20 +1609,30 @@

>>> np.einsum('ii', a)
60
>>> np.einsum(a, [0,0])
60
>>> np.trace(a)
60

>>> np.einsum('ii->i', a)
array([ 0, 6, 12, 18, 24])
>>> np.einsum(a, [0,0], [0])
array([ 0, 6, 12, 18, 24])
>>> np.diag(a)
array([ 0, 6, 12, 18, 24])

>>> np.einsum('ij,j', a, b)
array([ 30, 80, 130, 180, 230])
>>> np.einsum(a, [0,1], b, [1])
array([ 30, 80, 130, 180, 230])
>>> np.dot(a, b)
array([ 30, 80, 130, 180, 230])

>>> np.einsum('ji', c)
array([[0, 3],
[1, 4],
[2, 5]])
>>> np.einsum(c, [1,0])
array([[0, 3],
[1, 4],
[2, 5]])
Expand All @@ -1624,7 +1641,10 @@
[1, 4],
[2, 5]])

>>> np.einsum(',', 3, c)
>>> np.einsum('..., ...', 3, c)
array([[ 0, 3, 6],
[ 9, 12, 15]])
>>> np.einsum(3, [Ellipsis], c, [Ellipsis])
array([[ 0, 3, 6],
[ 9, 12, 15]])
>>> np.multiply(3, c)
Expand All @@ -1633,24 +1653,37 @@

>>> np.einsum('i,i', b, b)
30
>>> np.einsum(b, [0], b, [0])
30
>>> np.inner(b,b)
30

>>> np.einsum('i,j', np.arange(2)+1, b)
array([[0, 1, 2, 3, 4],
[0, 2, 4, 6, 8]])
>>> np.einsum(np.arange(2)+1, [0], b, [1])
array([[0, 1, 2, 3, 4],
[0, 2, 4, 6, 8]])
>>> np.outer(np.arange(2)+1, b)
array([[0, 1, 2, 3, 4],
[0, 2, 4, 6, 8]])

>>> np.einsum('i...->', a)
>>> np.einsum('i...->...', a)
array([50, 55, 60, 65, 70])
>>> np.einsum(a, [0,Ellipsis], [Ellipsis])
array([50, 55, 60, 65, 70])
>>> np.sum(a, axis=0)
array([50, 55, 60, 65, 70])

>>> a = np.arange(60.).reshape(3,4,5)
>>> b = np.arange(24.).reshape(4,3,2)
>>> np.einsum('ijk,jil->kl', a, b)
array([[ 4400., 4730.],
[ 4532., 4874.],
[ 4664., 5018.],
[ 4796., 5162.],
[ 4928., 5306.]])
>>> np.einsum(a, [0,1,2], b, [1,0,3], [2,3])
array([[ 4400., 4730.],
[ 4532., 4874.],
[ 4664., 5018.],
Expand Down
22 changes: 12 additions & 10 deletions numpy/core/code_generators/numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,19 @@
'NpyIter_GetInnerFixedStrideArray': 262,
'NpyIter_RemoveAxis': 263,
'NpyIter_GetAxisStrideArray': 264,
'NpyIter_RequiresBuffering': 265,
'NpyIter_GetInitialDataPtrArray': 266,
#
'PyArray_CastingConverter': 265,
'PyArray_CountNonzero': 266,
'PyArray_PromoteTypes': 267,
'PyArray_MinScalarType': 268,
'PyArray_ResultType': 269,
'PyArray_CanCastArrayTo': 270,
'PyArray_CanCastTypeTo': 271,
'PyArray_EinsteinSum': 272,
'PyArray_FillWithZero': 273,
'PyArray_NewLikeArray': 274,
'PyArray_CastingConverter': 267,
'PyArray_CountNonzero': 268,
'PyArray_PromoteTypes': 269,
'PyArray_MinScalarType': 270,
'PyArray_ResultType': 271,
'PyArray_CanCastArrayTo': 272,
'PyArray_CanCastTypeTo': 273,
'PyArray_EinsteinSum': 274,
'PyArray_FillWithZero': 275,
'PyArray_NewLikeArray': 276,
}

ufunc_types_api = {
Expand Down
Loading