[Python-ideas] Fwd: [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power
Robert Kern
robert.kern at gmail.com
Tue Mar 18 11:47:02 CET 2014
On 2014-03-18 08:02, Nick Coghlan wrote:
>
> On 18 Mar 2014 09:08, "Nathaniel Smith"
> <njs at pobox.com
> <mailto:njs at pobox.com>> wrote:
> >
> > On Fri, Mar 14, 2014 at 8:15 PM, Nick Coghlan
> <ncoghlan at gmail.com
> <mailto:ncoghlan at gmail.com>> wrote:
> > > A few other miscellaneous comments:
> > >
> > > - nice work on the PEP Nathaniel!
> >
> > Thanks!
> >
> > > - as with others, "@" as the operator doesn't thrill me, but I also think it
> > > crosses the threshold of "good enough given the constraints"
> > > - the PEP should probably recommend adding an "operator.matmul" function, a
> > > "PyObject_MatrixMultiply" C API and consider whether or not the new special
> > > method should be given a C level type slot.
> >
> > operator.matmul and PyObject_MatrixMultiply are obvious enough, but
> > I'm afraid I'm not too clear on the tradeoffs about adding a C level
> > type slot, or even entirely sure what the alternative is. (I guess I
> > just assumed that all special methods used C level type slots and
> > there was nothing to think about.) Do you (or anyone) have any
> > thoughts?
>
> I suspect you're going to want one, as without it, the implementation method
> ends up in the class dict instead (the context management protocol works that way).
>
> I suspect the design we will want is a new struct for Py_Matrix slots (akin to
> those for numbers, etc). The alternative would be to just add more "Number"
> slots, but that isn't really accurate.
Would it be more palatable if the name were something like __altmul__ or
__auxmul__ rather than __matmul__? Really, it's just a second
multiplication-like operator. The leading use case for a second
multiplication-like operator happens to be matrix multiplication, but I strongly
suspect it will get used for other mathematical things like symbolic function
composition or operator application (as in "linear operator", not +-*/) and
maybe some secondary multiplication types in the weirder groups and fields (you
can bet I will resurrect my Clifford algebra module to use this operator for one
of the several types of multiplication they support). Granted, there is still
some awkwardness in that *none* of the builtin number types will support it.
And hey, look! It makes the @ux or @lt multiplication operator makes a sensible,
language-independent mnemonic! (not that "auxiliary" or "alternate" work in all
languages, but at least the 'a' is baked into the special method name)
--
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