[SciPy-User] Matrix multiplication operator PEP

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Matrix multiplication operator PEP

Nathaniel Smith
Hey all,

I was feeling grotty and unproductive yesterday, so I decided to
procrastinate by writing a PEP, which I figure we might as well throw
at the wall and see if it sticks.  At least, once we do have consensus
on what it says -- I think we probably pretty much do, but let me
know. Any other comments, suggestions, pointings out of places where I
out a word, and arguments about how I've made some terrible mistakes,
are also very welcome; I guess we probably only get one more chance at
this.

Cheers,
-n

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

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

return-of-revenge-of-matmult-pep.rst (35K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Sturla Molden-3
It has to easily explain why subclassing is unsatisfactory. Otherwise it
will be thrown out with the argument that we can just subclass.

Sturla


Nathaniel Smith <[hidden email]> wrote:

> Hey all,
>
> I was feeling grotty and unproductive yesterday, so I decided to
> procrastinate by writing a PEP, which I figure we might as well throw at
> the wall and see if it sticks.  At least, once we do have consensus on
> what it says -- I think we probably pretty much do, but let me know. Any
> other comments, suggestions, pointings out of places where I out a word,
> and arguments about how I've made some terrible mistakes, are also very
> welcome; I guess we probably only get one more chance at this.
>
> Cheers,
> -n
>
> --
> Nathaniel J. Smith
> Postdoctoral researcher - Informatics - University of Edinburgh
> <a href="http://vorpus.org">http://vorpus.org</a>
>
> _______________________________________________ SciPy-User mailing list
> [hidden email] <a
> href="http://mail.scipy.org/mailman/listinfo/scipy-user">http://mail.scipy.org/mailman/listinfo/scipy-user</a>

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Nathaniel Smith

On 22 Feb 2014 05:21, "Sturla Molden" <[hidden email]> wrote:
>
> It has to easily explain why subclassing is unsatisfactory. Otherwise it
> will be thrown out with the argument that we can just subclass.

Do you mean the first "Alternative" (second half of "Rationale"), or something else?

-n

>
>
> Nathaniel Smith <[hidden email]> wrote:
> > Hey all,
> >
> > I was feeling grotty and unproductive yesterday, so I decided to
> > procrastinate by writing a PEP, which I figure we might as well throw at
> > the wall and see if it sticks.  At least, once we do have consensus on
> > what it says -- I think we probably pretty much do, but let me know. Any
> > other comments, suggestions, pointings out of places where I out a word,
> > and arguments about how I've made some terrible mistakes, are also very
> > welcome; I guess we probably only get one more chance at this.
> >
> > Cheers,
> > -n
> >
> > --
> > Nathaniel J. Smith
> > Postdoctoral researcher - Informatics - University of Edinburgh
> > <a href="http://vorpus.org">http://vorpus.org</a>
> >
> > _______________________________________________ SciPy-User mailing list
> > [hidden email] <a
> > href="http://mail.scipy.org/mailman/listinfo/scipy-user">http://mail.scipy.org/mailman/listinfo/scipy-user</a>
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Sturla Molden-3
On 22/02/14 12:36, Nathaniel Smith wrote:

> On 22 Feb 2014 05:21, "Sturla Molden" <[hidden email]
> <mailto:[hidden email]>> wrote:
>  >
>  > It has to easily explain why subclassing is unsatisfactory. Otherwise it
>  > will be thrown out with the argument that we can just subclass.
>
> Do you mean the first "Alternative" (second half of "Rationale"), or
> something else?
>
> -n


I dont't care as long as the reader will understand that subclassing is
not a viable option. Just make sure it does not drown in TL;DR text. Say
it early and preferably repeat it several times, so it does not get
missed. This is a literary strategy I've learned to use from writing
grant applications: Most of the prosaic text is never read, so make sure
the major message stand out as conspicuously as possible.

Subclassing is likely to be the major counter-argument, so make sure you
shoot it down thoroughly.


Sturla




_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Nathaniel Smith

On 22 Feb 2014 14:26, "Sturla Molden" <[hidden email]> wrote:
>
> On 22/02/14 12:36, Nathaniel Smith wrote:
> > On 22 Feb 2014 05:21, "Sturla Molden" <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >  >
> >  > It has to easily explain why subclassing is unsatisfactory. Otherwise it
> >  > will be thrown out with the argument that we can just subclass.
> >
> > Do you mean the first "Alternative" (second half of "Rationale"), or
> > something else?
> >
> > -n
>
>
> I dont't care as long as the reader will understand that subclassing is
> not a viable option. Just make sure it does not drown in TL;DR text. Say
> it early and preferably repeat it several times, so it does not get
> missed. This is a literary strategy I've learned to use from writing
> grant applications: Most of the prosaic text is never read, so make sure
> the major message stand out as conspicuously as possible.
>
> Subclassing is likely to be the major counter-argument, so make sure you
> shoot it down thoroughly.

My question is what counterargument are you referring to when you say "subclassing" :-). Is it the "you should just have both np.matrix and np.ndarray" argument?

-n


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Nathaniel Smith
In reply to this post by Nathaniel Smith
You know, better idea: let's move discussion to a PR:
    https://github.com/numpy/numpy/pull/4351

-n

On Sat, Feb 22, 2014 at 12:04 AM, Nathaniel Smith <[hidden email]> wrote:

> Hey all,
>
> I was feeling grotty and unproductive yesterday, so I decided to
> procrastinate by writing a PEP, which I figure we might as well throw
> at the wall and see if it sticks.  At least, once we do have consensus
> on what it says -- I think we probably pretty much do, but let me
> know. Any other comments, suggestions, pointings out of places where I
> out a word, and arguments about how I've made some terrible mistakes,
> are also very welcome; I guess we probably only get one more chance at
> this.
>
> Cheers,
> -n
>
> --
> Nathaniel J. Smith
> Postdoctoral researcher - Informatics - University of Edinburgh
> http://vorpus.org



--
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Geordie McBain-4
In reply to this post by Nathaniel Smith

I like this idea and draft PEP a lot.  Having this feature would ease my programming and greatly improve the readability of my SciPy programs.
   Having been trained in Matlab and then having used GNU Octave for many years, I thought about adapting their pair of infix multiplication operators, * and .*, but quickly realized that this wouldn't translate as they use the lone star for the linear algebraic product.
   A small suggestion is an additional mnemonic motivation for the at-sign: it slightly resembles the centred dot (LaTeX \cdot) often used in printed mathematics to represent an algebraic product.
   A bolder one that I haven't thought through yet: were there such an operator, besides making heavy use of its intended purpose, how might I overload it?  The first thing that springs to mind is functional composition and application.  The fundamental idea of a matrix is as a function on vectors; matrix-matrix multiplication is defined subsequently by associativity.  If I had arbitrary compatible functions f and g and an argument x, would I like to write 'f @ x' for f (x) and 'g @ f' for lambda x: g (f (x))?  Maybe not the former so much but the latter definitely.  Haskell uses a dot for this as in 'g . f'.

   http://www.haskell.org/haskellwiki/Function_composition

Of course the dot's unpalatable here as it's an accessor in Python.  The use of @ for composition might go some way to addressing the concern about adding an operator with no stdlib uses: I presume it does contain a few functional compositions.

Then turning that argument around: what about (should the PEP not get up, or even before it gets up) defining ndarray.__call__ as dot?  So we'd have A (x) instead of A.dot (x) or A @ x.  An advantage of this approach would be that it wouldn't require changing Python, only NumPy.  Matlab/Octave can't do that as they use parentheses for indexing as well as application; NumPy has left parentheses free by using brackets for the former.


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Sturla Molden-3
In reply to this post by Nathaniel Smith
Nathaniel Smith <[hidden email]> wrote:

> Subclassing is likely to be the major counter-argument, so make sure you
> shoot it down thoroughly.
>
> My question is what counterargument are you referring to when you say
> "subclassing" :-). Is it the "you should just have both np.matrix and np.ndarray" argument?
>

Yes, exactly that, but perhaps from a more a more principal point of view:
That subclassing and overloading the operator in a subclass is the more
"pythonic" way to change the behavior of an operator. Whereas adding more
syntactic bloat is unpythonic. So we have to explain why subclassing and
operator overloading is insufficient.

Sturla

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Matrix multiplication operator PEP

Matthew Brett
Hi,

On Sat, Feb 22, 2014 at 6:57 PM, Sturla Molden <[hidden email]> wrote:

> Nathaniel Smith <[hidden email]> wrote:
>
>> Subclassing is likely to be the major counter-argument, so make sure you
>> shoot it down thoroughly.
>>
>> My question is what counterargument are you referring to when you say
>> "subclassing" :-). Is it the "you should just have both np.matrix and np.ndarray" argument?
>>
>
> Yes, exactly that, but perhaps from a more a more principal point of view:
> That subclassing and overloading the operator in a subclass is the more
> "pythonic" way to change the behavior of an operator. Whereas adding more
> syntactic bloat is unpythonic. So we have to explain why subclassing and
> operator overloading is insufficient.

Yes, we do need to explain why we need two '*' operators, one for
element-wise multiplication and one for matrix multiplication.
Sub-classing can't do that for us - as we've know well - we just one
replace one mult by the other.

Cheers,

Matthew
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user