[Numpy-discussion] Syntax Improvement for Array Transpose

Hameer Abbasi einstein.edison at gmail.com
Mon Jun 24 06:24:54 EDT 2019


Given that np.dot and np.matmul do the right thing for 1-D arrays, I would be opposed to introducing an error as well.

 

From: NumPy-Discussion <numpy-discussion-bounces+einstein.edison=gmail.com at python.org> on behalf of Ilhan Polat <ilhanpolat at gmail.com>
Reply-To: Discussion of Numerical Python <numpy-discussion at python.org>
Date: Monday, 24. June 2019 at 11:58
To: Discussion of Numerical Python <numpy-discussion at python.org>
Subject: Re: [Numpy-discussion] Syntax Improvement for Array Transpose

 

Please don't introduce more errors for 1D arrays. They are already very counter-intuitive for transposition and for other details not relevant to this issue. Emitting errors for such a basic operation is very bad for user experience. This already is the case with wildly changing slicing syntax. It would have made sense if 2D arrays were the default objects and 1D required extra effort to create. But it is the other way around. Hence a transpose operation is "expected" from it. This would kind of force all NumPy users to shift their code one tab further to accomodate for the extra try, catch blocks for "Oh wait, what if a 1D array comes in?" checks for the existence of transposability everytime I write down `.T` in the code. 

 

Code example; I am continuously writing code involving lots of matrix products with inverses and transposes/hermitians (say, the 2nd eq., https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_continuous_are.html )

That means I have to check at least 4-6 matrices if any of them are transposable to make that equation go through.

 

The dot-H solution is actually my ideal choice but I get the point that the base namespace is already crowded. I am even OK with having `x.conj(T=True)` having a keyword for extra transposition so that I can get away with `x.conj(1)`; it doesn't solve the fundamental issue but at least gives some convenience.

 

Best,

ilhan

 

 

 

 

 

 

 

 

 

On Mon, Jun 24, 2019 at 3:11 AM Marten van Kerkwijk <m.h.vankerkwijk at gmail.com> wrote:

I had not looked at any implementation (only remembered the nice idea of "importing from the future"), and looking at the links Eric shared, it seems that the only way this would work is, effectively, pre-compilation doing a `<codetext>.replace('.T', '._T_from_the_future')`, where you'd be hoping that there never is any other meaning for a `.T` attribute, for any class, since it is impossible to be sure a given variable is an ndarray. (Actually, a lot less implausible than for the case of numpy indexing discussed in the link...)

 

Anyway, what I had in mind was something along the lines of inside the `.T` code there being be a check on whether a particular future item was present in the environment. But thinking more, I can see that it is not trivial to get to know something about the environment in which the code that called you was written....

 

So, it seems there is no (simple) way to tell numpy that inside a given module you want `.T` to have the new behaviour, but still to warn if outside the module it is used in the old way (when risky)?

 

-- Marten

 

p.s. I'm somewhat loath to add new properties to ndarray, but `.T` and `.H` have such obvious and clear meaning to anyone dealing with (complex) matrices that I think it is worth it. See https://mail.python.org/pipermail/numpy-discussion/2019-June/079584.html for a list of options of attributes that we might deprecate "in exchange"...

 

All the best,

 

Marten

 

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion at python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion at python.org https://mail.python.org/mailman/listinfo/numpy-discussion 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190624/ed1e02b9/attachment-0001.html>


More information about the NumPy-Discussion mailing list