2000-07-15 19:25:49 -04:00
|
|
|
PEP: 211
|
2001-06-05 12:50:09 -04:00
|
|
|
Title: Adding A New Outer Product Operator
|
2022-10-05 12:48:43 -04:00
|
|
|
Author: Greg Wilson <gvwilson@ddj.com>
|
2017-06-03 03:28:28 -04:00
|
|
|
Status: Rejected
|
2000-09-19 11:29:36 -04:00
|
|
|
Type: Standards Track
|
2017-01-24 15:47:22 -05:00
|
|
|
Content-Type: text/x-rst
|
2000-08-11 10:18:44 -04:00
|
|
|
Created: 15-Jul-2000
|
2007-06-19 00:20:07 -04:00
|
|
|
Python-Version: 2.1
|
2000-08-11 10:18:44 -04:00
|
|
|
Post-History:
|
2022-04-20 05:53:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The approach in the later :pep:`465` was eventually accepted
|
|
|
|
in lieu of this PEP. The :pep:`Rejected Ideas
|
|
|
|
<465#rejected-alternatives-to-adding-a-new-operator>`
|
|
|
|
of that PEP explains the rationale in more detail.
|
2000-07-15 19:25:49 -04:00
|
|
|
|
|
|
|
|
2000-08-11 10:18:44 -04:00
|
|
|
Introduction
|
2017-01-24 15:47:22 -05:00
|
|
|
============
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
This PEP describes a proposal to define ``@`` (pronounced "across")
|
|
|
|
as a new outer product operator in Python 2.2. When applied to
|
|
|
|
sequences (or other iterable objects), this operator will combine
|
|
|
|
their iterators, so that::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
for (i, j) in S @ T:
|
|
|
|
pass
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
will be equivalent to::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
for i in S:
|
|
|
|
for j in T:
|
|
|
|
pass
|
2001-06-05 12:50:09 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
Classes will be able to overload this operator using the special
|
|
|
|
methods ``__across__``, ``__racross__``, and ``__iacross__``. In
|
2022-01-21 06:03:51 -05:00
|
|
|
particular, the new Numeric module (:pep:`209`) will overload this
|
2017-01-24 15:47:22 -05:00
|
|
|
operator for multi-dimensional arrays to implement matrix
|
|
|
|
multiplication.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
Background
|
2017-01-24 15:47:22 -05:00
|
|
|
==========
|
|
|
|
|
|
|
|
Number-crunching is now just a small part of computing, but many
|
|
|
|
programmers --- including many Python users --- still need to
|
|
|
|
express complex mathematical operations in code. Most numerical
|
|
|
|
languages, such as APL, Fortran-90, MATLAB, IDL, and Mathematica,
|
|
|
|
therefore provide two forms of the common arithmetic operators.
|
|
|
|
One form works element-by-element, e.g. multiplies corresponding
|
|
|
|
elements of its matrix arguments. The other implements the
|
|
|
|
"mathematical" definition of that operation, e.g. performs
|
|
|
|
row-column matrix multiplication.
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
Zhu and Lielens have :pep:`proposed <225>` doubling up Python's operators in
|
|
|
|
this way. Their proposal would create six new binary infix
|
2017-01-24 15:47:22 -05:00
|
|
|
operators, and six new in-place operators.
|
|
|
|
|
|
|
|
The original version of this proposal was much more conservative.
|
2022-01-21 06:03:51 -05:00
|
|
|
The author consulted the developers of GNU Octave [1]_, an open
|
2017-01-24 15:47:22 -05:00
|
|
|
source clone of MATLAB. Its developers agreed that providing an
|
|
|
|
infix operator for matrix multiplication was important: numerical
|
|
|
|
programmers really do care whether they have to write ``mmul(A,B)``
|
|
|
|
instead of ``A op B``.
|
|
|
|
|
|
|
|
On the other hand, when asked how important it was to have infix
|
|
|
|
operators for matrix solution and other operations, Prof. James
|
2022-01-21 06:03:51 -05:00
|
|
|
Rawlings replied [2]_:
|
2017-01-24 15:47:22 -05:00
|
|
|
|
|
|
|
I DON'T think it's a must have, and I do a lot of matrix
|
2017-04-05 12:14:26 -04:00
|
|
|
inversion. I cannot remember if its ``A\b`` or ``b\A`` so I always
|
|
|
|
write ``inv(A)*b`` instead. I recommend dropping ``\``.
|
2017-01-24 15:47:22 -05:00
|
|
|
|
|
|
|
Based on this discussion, and feedback from students at the US
|
|
|
|
national laboratories and elsewhere, we recommended adding only
|
|
|
|
one new operator, for matrix multiplication, to Python.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
|
|
|
|
2001-06-05 12:50:09 -04:00
|
|
|
Iterators
|
2017-01-24 15:47:22 -05:00
|
|
|
=========
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
The planned addition of iterators to Python 2.2 opens up a broader
|
2022-01-21 06:03:51 -05:00
|
|
|
scope for this proposal. As part of the discussion of :pep:`201`,
|
|
|
|
Lockstep Iteration, the author of this proposal conducted an
|
|
|
|
informal usability experiment [3]_. The results showed that users
|
2017-01-24 15:47:22 -05:00
|
|
|
are psychologically receptive to "cross-product" loop syntax. For
|
|
|
|
example, most users expected::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
S = [10, 20, 30]
|
|
|
|
T = [1, 2, 3]
|
|
|
|
for x in S; y in T:
|
|
|
|
print x+y,
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
to print ``11 12 13 21 22 23 31 32 33``. We believe that users will
|
|
|
|
have the same reaction to::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
for (x, y) in S @ T:
|
|
|
|
print x+y
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
i.e. that they will naturally interpret this as a tidy way to
|
|
|
|
write loop nests.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
This is where iterators come in. Actually constructing the
|
|
|
|
cross-product of two (or more) sequences before executing the loop
|
|
|
|
would be very expensive. On the other hand, ``@`` could be defined
|
|
|
|
to get its arguments' iterators, and then create an outer iterator
|
|
|
|
which returns tuples of the values returned by the inner
|
|
|
|
iterators.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
|
|
|
|
2001-06-05 12:50:09 -04:00
|
|
|
Discussion
|
2017-01-24 15:47:22 -05:00
|
|
|
==========
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
1. Adding a named function "across" would have less impact on
|
|
|
|
Python than a new infix operator. However, this would not make
|
|
|
|
Python more appealing to numerical programmers, who really do
|
|
|
|
care whether they can write matrix multiplication using an
|
|
|
|
operator, or whether they have to write it as a function call.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
2. ``@`` would have be chainable in the same way as comparison
|
|
|
|
operators, i.e.::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
(1, 2) @ (3, 4) @ (5, 6)
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
would have to return ``(1, 3, 5) ... (2, 4, 6)``, and *not*
|
2021-07-14 14:01:22 -04:00
|
|
|
``((1, 3), 5) ... ((2, 4), 6)``. This should not require special
|
2017-01-24 15:47:22 -05:00
|
|
|
support from the parser, as the outer iterator created by the
|
|
|
|
first ``@`` could easily be taught how to combine itself with
|
|
|
|
ordinary iterators.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
3. There would have to be some way to distinguish restartable
|
|
|
|
iterators from ones that couldn't be restarted. For example,
|
|
|
|
if ``S`` is an input stream (e.g. a file), and ``L`` is a list, then ``S
|
|
|
|
@ L`` is straightforward, but ``L @ S`` is not, since iteration
|
|
|
|
through the stream cannot be repeated. This could be treated
|
|
|
|
as an error, or by having the outer iterator detect
|
|
|
|
non-restartable inner iterators and cache their values.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
4. Whiteboard testing of this proposal in front of three novice
|
|
|
|
Python users (all of them experienced programmers) indicates
|
|
|
|
that users will expect::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
"ab" @ "cd"
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
to return four strings, not four tuples of pairs of
|
|
|
|
characters. Opinion was divided on what::
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
("a", "b") @ "cd"
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
ought to return...
|
2000-08-11 10:18:44 -04:00
|
|
|
|
|
|
|
|
2000-11-22 17:01:47 -05:00
|
|
|
Alternatives
|
2017-01-24 15:47:22 -05:00
|
|
|
============
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
1. Do nothing --- keep Python simple.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
This is always the default choice.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
2. Add a named function instead of an operator.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
Python is not primarily a numerical language; it may not be worth
|
|
|
|
complexifying it for this special case. However, support for real
|
|
|
|
matrix multiplication *is* frequently requested, and the proposed
|
|
|
|
semantics for ``@`` for built-in sequence types would simplify
|
|
|
|
expression of a very common idiom (nested loops).
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
3. Introduce prefixed forms of all existing operators, such as
|
2022-01-21 06:03:51 -05:00
|
|
|
``~*`` and ``~+``, as proposed in :pep:`225`.
|
2000-11-22 17:01:47 -05:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
Our objections to this are that there isn't enough demand to
|
2022-01-21 06:03:51 -05:00
|
|
|
justify the additional complexity (see Rawlings' comments [2]_),
|
2017-01-24 15:47:22 -05:00
|
|
|
and that the proposed syntax fails the "low toner" readability
|
|
|
|
test.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2000-11-22 17:01:47 -05:00
|
|
|
|
|
|
|
Acknowledgments
|
2017-01-24 15:47:22 -05:00
|
|
|
===============
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2017-01-24 15:47:22 -05:00
|
|
|
I am grateful to Huaiyu Zhu for initiating this discussion, and to
|
|
|
|
James Rawlings and students in various Python courses for their
|
|
|
|
discussions of what numerical programmers really care about.
|
2000-08-11 10:18:44 -04:00
|
|
|
|
|
|
|
|
2000-11-22 17:01:47 -05:00
|
|
|
References
|
2017-01-24 15:47:22 -05:00
|
|
|
==========
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
.. [1] http://bevo.che.wisc.edu/octave/
|
2000-08-11 10:18:44 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
.. [2] http://www.egroups.com/message/python-numeric/4
|
2001-07-05 15:09:19 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
.. [3] https://mail.python.org/pipermail/python-dev/2000-July/006427.html
|