8000 merge master into 1.9 branch by juliantaylor · Pull Request #4849 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

merge master into 1.9 branch #4849

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
merged 96 commits into from
Jul 7, 2014
Merged
Changes from 1 commit
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
6447357
initial commit of new infix matrix multiply PEP
njsmith Feb 22, 2014
703fcc6
Many updates to draft PEP incorporating feedback
njsmith Feb 24, 2014
1199a31
add mention of ellipsis
njsmith Feb 24, 2014
8041598
bold the 'dot' row of table
njsmith Feb 24, 2014
5bb44e4
more table format tweaking. too bad .rst sucks at this!
njsmith Feb 24, 2014
76b1f2c
switch to just use text for the table, it's easier to read!
njsmith Feb 24, 2014
9a7a9f0
more text tweaks
njsmith Feb 24, 2014
dd8fa82
tweak tweak
njsmith Feb 24, 2014
c13e2eb
more edits
njsmith Feb 25, 2014
3b94837
move intended usage section after motivation
njsmith Feb 25, 2014
07ef370
add negative power support to @@
njsmith Feb 25, 2014
3203a05
edit edit
njsmith Mar 8, 2014
d99195e
More edits; pretty clean at this point.
njsmith Mar 9, 2014
d369c1a
add some more fields to the Big List o' Fields
njsmith Mar 9, 2014
81982f8
make the initial section even more overwritten
njsmith Mar 9, 2014
42354ba
much expanded background section, to start addressing feedback from s…
njsmith Mar 10, 2014
976b50c
lots better text, more polishing yes
njsmith Mar 11, 2014
ebca136
phrasing tweaks
njsmith Mar 11, 2014
2f0e55b
fix ReST typoes that were breaking footnotes
njsmith Mar 11, 2014
09df639
more ReST fixes
njsmith Mar 11, 2014
3151e78
another ReST tweak
njsmith Mar 11, 2014
05dfbee
add copyright notice
njsmith Mar 11, 2014
9472a8d
clean up in prep for python-ideas
njsmith Mar 12, 2014
3ede17e
more updates based on people's latest suggestions
njsmith Mar 13, 2014
ba9da00
phrasing tweaks
njsmith Mar 13, 2014
83f5127
more phrasing tweaks
njsmith Mar 13, 2014
03564ae
and yet even more phrasing tweaks
njsmith Mar 13, 2014
32de06e
add 'unresolved issues' section; + wording tweaks and a little more e…
njsmith Mar 13, 2014
c86a38c
realized I wasn't so sure of my claim about in-place matrix multiplic…
njsmith Mar 14, 2014
d319c24
yet more word tweaks
njsmith Mar 14, 2014
9d24bc1
add more comprehensive discussion of alternative symbols
njsmith Mar 14, 2014
0e933e6
New version just submitted to PEP editors.
njsmith Mar 18, 2014
1d884dd
fix PEP headers to placate PEP-0000 builder, mention Julia in notatio…
njsmith Mar 18, 2014
aadcf11
add associativity/precedence rationale (+ a few misc tweaks)
njsmith Apr 6, 2014
fb35105
BLD: workaround msvc being stupid
juliantaylor Jun 9, 2014
89d9add
Merge pull request #4796 from juliantaylor/msvc-fix
charris Jun 9, 2014
59b5606
MAINT: const correctness and minor fixes to C code
larsmans Jun 10, 2014
6ec8a1d
DOC: document broadcastable lam parameter of poisson
juliantaylor Jun 11, 2014
118dc9e
DOC: fix signature of PyArray_NewShape in C-API docs
juliantaylor Jun 11, 2014
6e2a69b
DOC: remove wrong mention of .gz in np.load
juliantaylor Jun 11, 2014
5a52edd
DOC: fix a couple mistakes in the indexing documentation
juliantaylor Jun 11, 2014
75bb95f
DOC: update numpydoc to tag v0.5
juliantaylor Jun 11, 2014
d70431d
DOC: add full/full_like
juliantaylor Jun 14, 2014
480432b
WIP: Fix matplotlib, etc. errors
seberg Jun 11, 2014
71fc802
Fix typemap for Fortran ordered array input
adamreeve Jun 15, 2014
ef4806a
Merge pull request #4803 from juliantaylor/doc-updates
charris Jun 17, 2014
133d4f4
Merge pull request #4809 from adamreeve/swig_typemap_fix
charris Jun 17, 2014
621c0a6
MAINT: move star imports to end of numeric.py
juliantaylor Jun 19, 2014
fe3410f
Merge pull request #4816 from juliantaylor/star-import
njsmith Jun 19, 2014
e20d4b9
BUG: handle rounding issue with histogram edges on float32 data
juliantaylor Jun 23, 2014
ad902ff
ENH: use copy to move the masked values into the result
juliantaylor Jun 23, 2014
305b26b
MAINT: replace two step errstate change with a direct change
juliantaylor Jun 23, 2014
c613147
DOC Polynomial example import statement updated. #3615
ilam Jun 25, 2014
59892ad
DOC Polynomial example import statement corrected. #3615
ilam Jun 25, 2014
7f0f6c8
Merge pull request #4825 from ilam/poly
charris Jun 25, 2014
578ce46
BUG: fix some memory leaks found by cpychecker
juliantaylor Jun 25, 2014
92fbc04
MAINT: enable external api use when running cpychecker
juliantaylor Jun 25, 2014
2421f2d
BLD: fix random API hash due to memory references of annotations
juliantaylor Jun 25, 2014
2aafae5
Merge pull request #4826 from juliantaylor/umath-static-fixes
charris Jun 26, 2014
ad48eaa
Merge pull request #4823 from juliantaylor/hist-rounding
charris Jun 30, 2014
efb203c
Merge pull request #4822 from juliantaylor/masked-improv
charris Jun 30, 2014
7298d36
MAINT: Simplify some uses of errstate context manager.
charris Jun 30, 2014
cdfbc69
DOC: Update 1.9.0-notes to mention pairwise summation.
charris May 10, 2014
290f192
Move tempdir context manager to numpy.testing.utils
ogrisel Jun 30, 2014
6efd849
FIX isfileobj accepts write-mode files under PY3
ogrisel Jun 26, 2014
e2254e4
MAINT: Use an unqualified nomask variable in ma.core.
abalkin Jun 30, 2014
49c30fd
Merge pull request #4832 from abalkin/nomask
charris Jun 30, 2014
4e3a24b
Merge pull request #4828 from ogrisel/fix-isfileobj-py3
juliantaylor Jun 30, 2014
2762f54
Merge pull request #4831 from charris/simplify-with-errstate
juliantaylor Jun 30, 2014
c62b14a
Merge pull request #4801 from larsmans/c-fixes
juliantaylor Jun 30, 2014
0136d65
BUG: pickling ufuncs defined in nested modules
ogrisel Jun 30, 2014
b25cdd6
Merge pull request #4800 from ogrisel/fix-ufunc-pickling
juliantaylor Jun 30, 2014
fc5d7cf
BUG: limit type alignment to the largest alignment needed by numpy
juliantaylor Jun 17, 2014
83524f6
BUG: fix arrays wrapping foreign data losing their alignment flag
juliantaylor Jun 17, 2014
9bdd5d4
BUG: fix transpose not updating alignment flag
juliantaylor Jun 19, 2014
1d96a95
TST: disable tests that fail due to bad alignment on sparc
juliantaylor Jun 30, 2014
e8d1374
Merge pull request #4812 from juliantaylor/align-bloat
charris Jul 3, 2014
d6c7a16
BUG: wrong selection for orders falling into equal ranges
juliantaylor Jul 4, 2014
7a2b14a
Merge pull request #4837 from juliantaylor/select-bug
charris Jul 4, 2014
4b8c9fd
BUG: fix buffer overflow in data array of ldexp and frexp
juliantaylor Jul 5, 2014
7434481
DEP: Allow high dim boolean assignment, but deprecate it
seberg Jul 5, 2014
ed88fa9
BUG: boolean assignment; allow wrong number of elements
seberg Jul 5, 2014
9c4d48c
BUG: Add missing error incref in 1d indexing fallback
seberg Jul 5, 2014
c9c53ea
BUG: disable garbage collector during memory allocation hook
juliantaylor Jul 5, 2014
81242f6
Merge pull request #4841 from juliantaylor/alloc-hook-gc
charris Jul 5, 2014
8dba040
Merge pull request #4804 from seberg/fancy-ass-1d
charris Jul 6, 2014
77d62bb
BUG: retain writeable flag when indexing subclasses
juliantaylor Jul 6, 2014
94417e4
Merge pull request #4843 from juliantaylor/subclass-writeable
charris Jul 6, 2014
d4a4e99
MAINT: Spellcheck some files.
charris Jul 4, 2014
ca397d8
Merge pull request #4839 from juliantaylor/buffer-overflow
juliantaylor Jul 6, 2014
35626e7
Merge pull request #4844 from charris/spellcheck-some-files
juliantaylor Jul 6, 2014
1c71d46
Merge pull request #4351 from njsmith/matmul-pep
charris Jul 6, 2014
0d744c4
MAINT: better fix for subclass writeable flag preservation
juliantaylor Jul 6, 2014
4352957
MAINT: fix a compiler warning
juliantaylor Jul 6, 2014
d244ec7
Merge pull request #4847 from juliantaylor/writeable2
charris Jul 6, 2014
594b0de
Merge pull request #4697 from charris/update-1.9.0-notes
charris Jul 7, 2014
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
Prev Previous commit
Next Next commit
more updates based on people's latest suggestions
  • Loading branch information
njsmith committed Mar 13, 2014
commit 3ede17e917d7fe0af24d88e3675ba1ab6fecd7c2
132 changes: 76 additions & 56 deletions doc/neps/return-of-revenge-of-matmul-pep.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,33 @@ Executive summary

In numerical code, there are two important operations which compete
for use of Python's ``*`` operator: elementwise multiplication, and
matrix multiplication. And in the nearly twenty years since the
Numeric library was first released, no-one has found any really
satisfactory way to resolve this competition. Currently, most
numerical Python code uses ``*`` for elementwise multiplication, and
function/method syntax for matrix multiplication; however, this leads
to ugly and unreadable code in common circumstances. The problem is
bad enough that significant amounts of code continue to use the
opposite convention (which has the virtue of producing ugly and
unreadable code in *different* circumstances), despite the problems
this API fragmentation causes. There does not seem to be any *good*
solution to the problem of designing a numerical API within current
Python syntax -- only a landscape of options that are bad in different
ways. This makes it intrisically difficult to reach consensus, and
thus fragmentation continues. The minimal change to Python syntax
which is sufficient to resolve these problems is the addition of a
single new infix operator for matrix multiplication.
matrix multiplication. In the nearly twenty years since the Numeric
library was first proposed, there have been many attempts to resolve
this tension [#hugunin]_; none have been really satisfactory.
Currently, most numerical Python code uses ``*`` for elementwise
multiplication, and function/method syntax for matrix multiplication;
however, this leads to ugly and unreadable code in common
circumstances. The problem is bad enough that significant amounts of
code continue to use the opposite convention (which has the virtue of
producing ugly and unreadable code in *different* circumstances), and
this API fragmentation across codebases then creates yet more
problems. There does not seem to be any *good* solution to the
problem of designing a numerical API within current Python syntax --
only a landscape of options that are bad in different ways. The
minimal change to Python syntax which is sufficient to resolve these
problems is the addition of a single new infix operator for matrix
multiplication.

Matrix multiplication has a singular combination of features which
distinguish it from other binary operations. Together they mean that
such an operator will fit comfortably into Python's existing style,
and provide a uniquely compelling case for its addition:
distinguish it from other binary operations, which together provide a
uniquely compelling case for the addition of a dedicated infix
operator:

* Just as for the existing numerical operators, there exists a vast
body of prior art supporting the use of infix notation for matrix
multiplication across all fields of mathematics, science, and
engineering.
engineering; ``@`` fills a hole in Python's existing operator
system.

* ``@`` greatly clarifies real-world code.

Expand Down Expand Up @@ -138,7 +139,7 @@ at hand.
Matrix multiplication is more of a special case. It's only defined on
2d arrays (also known as "matrices"), and multiplication is the only
operation that has a meaningful "matrix" version -- "matrix addition"
is the same as elementwise addition; there is no such thing "matrix
is the same as elementwise addition; there is no such thing as "matrix
bitwise-or" or "matrix floordiv"; "matrix division" can be defined but
is not very useful, etc. However, matrix multiplication is still used
very heavily across all numerical application areas; mathematically,
Expand Down Expand Up @@ -604,21 +605,19 @@ Semantics

The recommended semantics for ``@`` are:

* 0d (scalar) inputs raise an error. Scalar * matrix multiplication
is a mathematically and algorithmically distinct operation from
matrix @ matrix multiplication; scalar * matrix multiplication
should go through ``*`` instead of ``@``.

* 2d inputs are conventional matrices, and treated in the obvious way.
If we write ``arr(2, 3)`` to represent an arbitrary 2x3 array, then
``arr(3, 4) @ arr(4, 5)`` returns an array with shape (3, 5).
* 2d inputs are conventional matrices, and so the semantics are
obvious: we apply conventional matrix multiplication. If we write
``arr(2, 3)`` to represent an arbitrary 2x3 array, then ``arr(3, 4)
@ arr(4, 5)`` returns an array with shape (3, 5).

* 1d vector inputs are promoted to 2d by prepending or appending a '1'
to the shape on the side 'away' from the operator, the operation is
performed, and then the added dimension is removed from the output.
The result is that matrix @ vector and vector @ matrix are both
legal (assuming compatible shapes), and both return 1d vectors;
vector @ vector returns a scalar. This is clearer with examples.
to the shape, the operation is performed, and then the added
dimension is removed from the output. The 1 is always added on the
"outside" of the shape: prepended for left arguments, and appended
for right arguments. The result is that matrix @ vector and vector
@ matrix are both legal (assuming compatible shapes), and both
return 1d vectors; vector @ vector returns a scalar. This is
clearer with examples.

* ``arr(2, 3) @ arr(3, 1)`` is a regular matrix product, and returns
an array with shape (2, 1), i.e., a column vector.
Expand Down Expand Up @@ -662,7 +661,10 @@ The recommended semantics for ``@`` are:
@ b[:, np.newaxis])[0, 0]`` instead of ``a @ b`` every time they
compute an inner product, or ``(a[np.newaxis, :] @ Mat @ b[:,
np.newaxis])[0, 0]`` for general quadratic forms instead of ``a @
Mat @ b``.
Mat @ b``. In addition, sage and sympy (see below) use these
non-associative semantics with an infix matrix multiplication
operator (they use ``*``), and they report that they haven't
experienced any problems caused by it.

* For inputs with more than 2 dimensions, we treat the last two
dimensions as being the dimensions of the matrices to multiply, and
Expand All @@ -671,23 +673,34 @@ The recommended semantics for ``@`` are:
For example, ``arr(10, 2, 3) @ arr(10, 3, 4)`` performs 10 separate
matrix multiplies, each of which multiplies a 2x3 and a 3x4 matrix
to produce a 2x4 matrix, and then returns the 10 resulting matrices
together in an array with shape (10, 2, 4). Note that in more
complicated cases, broadcasting allows several simple but powerful
tricks for controlling how arrays are aligned with each other; see
together in an array with shape (10, 2, 4). The intuition here is
that we treat these 3d arrays of numbers as if they were 1d arrays
*of matrices*, and then apply matrix multiplication in an
elementwise manner, where now the elements are whole matrices. Note
that broadcasting is not limited to perfectly aligned arrays; in
more complicated cases, it allows several simple but powerful tricks
for controlling how arrays are aligned with each other; see
[#broadcasting]_ for details. (In particular, it turns out that
elementwise multiplication with broadcasting includes the standard
scalar * matrix product as a special case, further motivating the
use of ``*`` for this case.)
when broadcasting is taken into account, the standard scalar *
matrix product is a special case of the elementwise multiplication
operator ``*``.)

If one operand is >2d, and another operand is 1d, then the above
rules apply unchanged, with 1d->2d promotion performed before
broadcasting. E.g., ``arr(10, 2, 3) @ arr(3)`` first promotes to
``arr(10, 2, 3) @ arr(3, 1)``, then broadcasts to ``arr(10, 2, 3) @
arr(10, 3, 1)``, multiplies to get an array with shape (10, 2, 1),
and finally removes the added dimension, returning an array with
shape (10, 2). Similarly, ``arr(2) @ arr(10, 2, 3)`` produces an
intermediate array with shape (10, 1, 3), and a final array with
shape (10, 3).
``arr(10, 2, 3) @ arr(3, 1)``, then broadcasts the right argument to
create the aligned operation ``arr(10, 2, 3) @ arr(10, 3, 1)``,
multiplies to get an array with shape (10, 2, 1), and finally
removes the added dimension, returning an array with shape (10, 2).
Similarly, ``arr(2) @ arr(10, 2, 3)`` produces an intermediate array
with shape (10, 1, 3), and a final array with shape (10, 3).

* 0d (scalar) inputs raise an error. Scalar * matrix multiplication
is a mathematically and algorithmically distinct operation from
matrix @ matrix multiplication, and is already covered as a special
case of the elementwise ``*`` operator. Allowing scalar @ matrix
would thus both require an unnecessary special case, and violate
TOOWTDI.

The recommended semantics for ``@@`` are::

Expand Down Expand Up @@ -773,14 +786,14 @@ above. These projects focus on computational methods for analyzing
matrices in the sense of abstract mathematical objects (i.e., linear
maps over free modules over rings), rather than as big bags full of
numbers that need crunching. And it turns out that for these
purposes, they don't actually have any use for most elementwise
operations; as discussed in the Background section above, elementwise
operations are motivated by the bag-of-numbers approach. So, these
projects don't encounter the basic problem that this PEP exists to
address, making it mostly irrelevant to them. They use ``*`` for
matrix multiplication, and if this PEP is accepted, their expressed
intention is to continue doing so, while probably adding ``@`` and
``@@`` as aliases for ``*`` and ``**``:
purposes, they don't actually have much use for elementwise operations
in the first place; as discussed in the Background section above,
elementwise operations are motivated by the bag-of-numbers approach.
So, these projects don't encounter the basic problem that this PEP
exists to address, making it mostly irrelevant to them. They use ``*``
for matrix multiplication (and for group actions, etc.), and if this
PEP is accepted, their expressed intention is to continue doing so,
while perhaps adding ``@`` and ``@@`` as aliases for ``*`` and ``**``:

* sympy
* sage
Expand Down Expand Up @@ -815,7 +828,8 @@ about how this operator should be named [#matmul-other-langs]_, but
a nice bonus.

* The swirly shape is reminiscent of the simultaneous sweeps over rows
and columns that define matrix multiplication.
and columns that define matrix multiplication; its asymmetry is
evocative of its non-commutative nature.


(Non)-Definitions for built-in types
Expand Down Expand Up @@ -1159,6 +1173,12 @@ References
files that are choosing to directly interact with that API, which
is sort of like what we get by looking at import statements.

.. [#hugunin] The first such proposal occurs in Jim Hugunin's very
first email to the matrix SIG in 1995, which lays out the first
draft of what became Numeric. He suggests using ``*`` for
elementwise multiplication, and ``%`` for matrix multiplication:
https://mail.python.org/pipermail/matrix-sig/1995-August/000002.html


Copyright
=========
Expand Down
0