[Numpy-discussion] [help needed] associativity and precedence of '@'
Nathaniel Smith
njs at pobox.com
Sat Mar 15 14:40:40 EDT 2014
On Sat, Mar 15, 2014 at 6:33 PM, Joe Kington <joferkington at gmail.com> wrote:
> On Sat, Mar 15, 2014 at 1:28 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> On Sat, Mar 15, 2014 at 3:41 AM, Nathaniel Smith <njs at pobox.com> wrote:
>> > Hi all,
>> >
>> > Here's the main blocker for adding a matrix multiply operator '@' to
>> > Python:
>> > we need to decide what we think its precedence and associativity should
>> > be.
>>
>> Another data point that might be useful:
>>
>> Matlab: same-left
>>
>>
>> R: tight-left
>
>
>
> I was going to ask this earlier, but I was worried I was missing something
> major.
>
> Why was "tight-left" not an option?
>
>
> This means that if you don't use parentheses, you get:
> a @ b @ c -> (a @ b) @ c
> a * b @ c -> a * (b @ c)
> a @ b * c -> (a @ b) * c
>
>
> In my (very inexperienced) opinion, it seems like the most intuitive option.
Because tight-left doesn't seem to have much to recommend it over
same-left, and all else being equal having fewer levels of precedence
is usually considered a good thing. Unless I'm missing something. If
we do decide that tight-left is best then we could certainly advocate
for it.
I wouldn't read too much into R's choice; they don't actually define a
separate precedence level for matrix multiplication specifically. They
have a single precedence level for all "special" (user-defined)
operators, and matrix multiplication happens to be one of these.
(Their versions of // and % are also "special", but I don't think
anyone would expect // to bind more tightly than / if one were
choosing precedences on a case-by-case basis.)
-n
--
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
More information about the NumPy-Discussion
mailing list