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

Andrew Barnert abarnert at yahoo.com
Tue Mar 18 20:59:30 CET 2014


From: M.-A. Lemburg <mal at egenix.com>

Sent: Tuesday, March 18, 2014 5:18 AM
> On 18.03.2014 12:27, Nick Coghlan wrote:

>>  The other way to do it would be to just add four new slots to
>> PyNumberMethods:
>> 
>>          binaryfunc nb_matrix_multiply;
>>          binaryfunc nb_matrix_power;
>>          binaryfunc nb_inplace_matrix_multiply;
>>          binaryfunc nb_inplace_matrix_power;
>> 
>>  This approach increases the size of all type objects that define one
>>  or more of the numeric functions by four pointers, and doesn't really
>>  make sense at a conceptual level. The latter is the main reason I
>>  prefer the separate PyMatrixMethods struct.
> 
> I don't think that it's a good idea to make all type objects
> larger just to address matrix multiplications which none of the
> builtin types will actually use, so +1 on adding to the number
> methods, even if a matrix isn't a number (they still use numbers,
> so it's not that far off :-)), but -1 on adding another slot
> struct.

I don't see how putting them in PyNumberMethods doesn't make sense at a conceptual level. (Matrix, +, @) is a ring, and they form an assocative algebra. Sure, matrices are collections of simpler numbers, and their operations are defined in terms of the operations of those simpler numbers, but the same thing is true for complex numbers, or even rationals. From a mathematical point of view, there's no principled definition of "number" (or, rather, if there is, it's only going to include the naturals or maybe the ordinals). If anything, the float type has a lot more problems as a "number" than a matrix type does. So if there is a good practical reason to call matrix multiplication a numeric operation, why not?



More information about the Python-ideas mailing list