[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power

Nathaniel Smith njs at pobox.com
Fri Mar 14 06:05:42 CET 2014


On Fri, Mar 14, 2014 at 4:03 AM, Andrew Barnert <abarnert at yahoo.com> wrote:
> This (or various related ideas) actually has come up somewhat regularly. I think the reason it hasn't gone anywhere is that there was never enough input from the numerical community; someone says, "I'll bet this would be useful for numerics," and someone else says "Then why haven't the numarray/numeric/numpy guys asked for it since 2000?", and nobody has an answer to that. This obviously is the answer to that.

The numeric community has many talents, but crossing the cultural
divide with upstream is not really a speciality...

> One thing that always comes up is a suggestion for using Unicode. There are obvious downsides--the Unicode multiplication character isn't easy to type; even if Python and major code editors are fully Unicode friendly, code often has to go through channels that may not be; etc. But it's worth asking whether the numeric community has considered this and rejected it for these reasons, or for other reasons, or if they'd be happy with it but just didn't think Python was ready for it, or whatever.

We don't really have a strong opinion on which character is used. It
is nice if it's easy to type, though -- esp. because in many ways it's
already beginners who suffer the brunt of the elmul/matmul issues, and
beginners are exactly the people for whom figuring out how to type
some stupid character is a prohibitive speed bump. (Scientific python
has a very large ongoing stream of newbie programmers.) And I don't
know that any of the Unicode characters are actually better. In real
math the two operations are distinguished by context, so there's no
existing character with the right meaning that we can steal. In some
ways having an unusual character for this is better, because it
indicates a special-case operation. If the two multiplication
characters are * and ×, then how do you remember which is which? But
if they're * and @, then it's easy to remember that * is the
general-use one, and @ is the special non-commutative matrix
multiplication one.

It's not like new operators are being added to Python every week and
we need to start scraping the bottom of the barrel for new characters.
ASCII gets the job done, has some minor upsides, and no real
downsides.

> Also, how do other general purpose programming languages solve this? Surely people do matrix math in Haskell and C++, at least? Do they just use separate types for matrices and arrays because they don't have to worry about duck typing (being statically-typed languages)? Do they just avoid mixing libraries together to avoid the problem? Or have people attempted to reuse % or other operators? Or (doesn't apply to C++, but does to Haskell) do they use spelled-out infix functions like `mmul` instead of trying to come up with symbolic operators? A lot of those answers wouldn't tell us anything more than "their experience isn't relevant to Python", but it would be nice to know that at least.

I'm not an expert on Haskell and C++ matrix libraries -- perhaps
someone else will speak up -- but Haskell's extensible infix system
and static typing do seem like they put them in a pretty different
design space. And I'm skeptical that there exist Haskell libraries
with anything like numpy's maturity, though I'll be delighted to be
proven wrong.

Eigen is maybe the most popular C++ matrix/array library right now,
and AFAICT from their docs they use a system where there's one "array"
type that *only* supports elementwise multiplication, and one "matrix"
type that *only* supports matrix multiplication, and if you want both
operations you have to cast back and forth. This seems a bit annoying
to me, but not as deadly as it is in Python -- in C++ the static type
system means they can at least fob off the work of keeping track of
which variables are which type, and doing conversions at function
boundaries, onto the compiler. In Python these conversions have to
happen by hand, and the two-type solution is just not workable.

In general, I doubt there's huge amounts of experience to steal from
other languages, because Python is in a unique place: AFAICT it's the
only general purpose language that has ever made serious inroads
against the specialized numeric languages (Matlab, R, GAUSS, IDL,
etc.) on their home territory. So we're breaking new ground here. (All
those languages do have separate infix operators for elementwise and
matrix multiplication; they all involve horrible names like ".*" or
"%*%" and all kinds of weird inconsistencies.)

-n

> On Mar 13, 2014, at 19:08, Nathaniel Smith <njs at pobox.com> wrote:
>
>> On Fri, Mar 14, 2014 at 1:59 AM, Nathaniel Smith <njs at pobox.com> wrote:
>>> [...]
>>
>> Hmm, not sure how that "Fwd:" snuck onto the subject line. (Or really,
>> I do know, but am embarrassed to say.) Oh well, sorry, hope it isn't
>> too distracting!
>>
>> -n
>>
>> --
>> Nathaniel J. Smith
>> Postdoctoral researcher - Informatics - University of Edinburgh
>> http://vorpus.org
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org


More information about the Python-ideas mailing list