<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Apr 6, 2016 at 5:20 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>On Wed, Apr 6, 2016 at 10:43 AM, Todd <<a href="mailto:toddrjen@gmail.com" target="_blank">toddrjen@gmail.com</a>> wrote:<br>
><br>
> My intention was to make linear algebra operations easier in numpy.  With<br>
> the @ operator available, it is now very easy to do basic linear algebra on<br>
> arrays without needing the matrix class.  But getting an array into a state<br>
> where you can use the @ operator effectively is currently pretty verbose and<br>
> confusing.  I was trying to find a way to make the @ operator more useful.<br>
<br>
</div></div>Can you elaborate on what you're doing that you find verbose and<br>
confusing, maybe paste an example? I've never had any trouble like<br>
this doing linear algebra with @ or dot (which have similar semantics<br>
for 1d arrays), which is probably just because I've had different use<br>
cases, but it's much easier to talk about these things with a concrete<br>
example in front of us to put everyone on the same page.<br>
<div><div><br></div></div></blockquote><div><br></div><div>Let's say you want to do a simple matrix multiplication example.  You create two example arrays like so:<br><br></div><div>   a = np.arange(20)<br></div><div>   b = np.arange(10, 50, 10)<br><br></div><div>Now you want to do <br><br>    a.T @ b <br><br></div><div>First you need to turn a into a 2D array.  I can think of 10 ways to do this off the top of my head, and there may be more:<br><br></div><div>    1a) a[:, None]<br></div><div>    1b) a[None]<br>    1c) a[None, :]</div><div>    2a) a.shape = (1, -1)<br>    2b) a.shape = (-1, 1)<br>    3a) a.reshape(1, -1)<br>    3b) a.reshape(-1, 1)<br>    4a) np.reshape(a, (1, -1))<br>    4b) np.reshape(a, (-1, 1))</div><div>    5) np.atleast_2d(a)<br><br></div><div>5 is pretty clear, and will work fine with any number of dimensions, but is also long to type out when trying to do a simple example.  The different variants of 1, 2, 3, and 4, however, will only work with 1D arrays (making them less useful for functions), are not immediately obvious to me what the result will be (I always need to try it to make sure the result is what I expect), and are easy to get mixed up in my opinion.  They also require people keep a mental list of lots of ways to do what should be a very simple task.<br><br></div><div>Basically, my argument here is the same as the argument from pep465 for the inclusion of the @ operator:<br><a href="https://www.python.org/dev/peps/pep-0465/#transparent-syntax-is-especially-crucial-for-non-expert-programmers">https://www.python.org/dev/peps/pep-0465/#transparent-syntax-is-especially-crucial-for-non-expert-programmers</a><br><p>"A large proportion of scientific code is written by people who are
experts in their domain, but are not experts in programming.  And
there are many university courses run each year with titles like "Data
analysis for social scientists" which assume no programming
background, and teach some combination of mathematical techniques,
introduction to programming, and the use of programming to implement
these mathematical techniques, all within a 10-15 week period.  These
courses are more and more often being taught in Python rather than
special-purpose languages like R or Matlab.
    </p>
    
     For these kinds of users, whose programming knowledge is fragile, the
existence of a transparent mapping between formulas and code often
means the difference between succeeding and failing to write that code
at all."</div></div></div></div>