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

Robert Kern robert.kern at gmail.com
Sat Mar 15 12:47:48 CET 2014


On 2014-03-14 20:17, Serhiy Storchaka wrote:
> 14.03.14 03:59, Nathaniel Smith написав(ла):
>> PEP: XXXX
>> Title: Dedicated infix operators for matrix multiplication and matrix power
>
> This is about matrix multiplication and matrix power. But what about matrix
> division? It is needed to distinguish elementwise division from left and right
> matrix divisions.

In our experience, matrix division comes up rather more rarely. For numerical 
problems, these should typically be implemented with a linear solve of some 
type. The best kind of solve varies based on the problem, so that's a good 
reason to continue to implement it with a function call rather than an operator. 
For symbolic problems (a la Sympy), both forms can be spelled reasonably well 
given matrix multiplication and matrix power.

> And what about concatenation? Python lists and tuples use
> ``+`` for this, but NumPy arrayse use ``+`` for bitwise addition.

There are *lots* of ways to concatenate an n-dimensional array, and we have a 
whole complement of functions to do that, vstack(), hstack(), dstack(), 
concatenate(), r_[], c_[]. A single operator would not suffice.

> Matrix multiplication is only defined on 2d arrays (matrices). But why not on
> arbitrary tensors (except scalars)?

The PEP defines how numpy intends to interpret n-dimensional operands and the 
reasons why. For tensors qua tensors you tend to want to do arbitrary 
Einstein-summation, which is hard to do in a single operator. numpy.einsum() 
handles that for us.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the Python-ideas mailing list