[Numpy-discussion] add .H attribute?

Toder, Evgeny evgeny.toder at jpmorgan.com
Mon Jul 22 15:39:47 EDT 2013

What if .H is not an attribute, but a method? Is this enough of a warning about copying?


-----Original Message-----
From: numpy-discussion-bounces at scipy.org [mailto:numpy-discussion-bounces at scipy.org] On Behalf Of Nathaniel Smith
Sent: Monday, July 22, 2013 3:11 PM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] add .H attribute?

On Thu, Jul 18, 2013 at 3:18 PM, Stéfan van der Walt <stefan at sun.ac.za> wrote:
> On Sat, Jul 13, 2013 at 7:46 PM, Nathaniel Smith <njs at pobox.com> wrote:
>> Why not just write
>> def H(a):
>>     return a.conj().T
> It's hard to convince students that this is the Best Way of doing
> things in NumPy.  Why, they ask, can you do it using a' in MATLAB,
> then?

I guess I'd try to treat it as a teachable moment... the answer points
to a basic difference in numpy versus MATLAB. Numpy operates at a
slightly lower level of abstraction. In MATLAB you're encouraged to
think of arrays as just mathematical matrices and let MATLAB worry
about how to actually represent those inside the computer. Sometimes
it does a good job, sometimes not. In numpy you need to think of
arrays as structured representations of a chunk of memory. There
disadvantages to this -- e.g. keeping track of which arrays return
view and which return copies can be tricky -- but it also gives a lot
of power: views are awesome, you get better interoperability with C
libraries/Cython, better ability to predict which operations are
expensive or cheap, more opportunities to use clever tricks when you
need to, etc.

And one example of this is that transpose and conjugate transpose
really are very different at this level, because one is a cheap stride
manipulation that returns a view, and the other is a (relatively)
expensive data copying operation. The convention in Python is that
attribute access is supposed to be cheap, while function calls serve
as a warning that something expensive might be going on. So in short:
MATLAB is optimized for doing linear algebra and not thinking too hard
about programming; numpy is optimized for writing good programs.
Having .T but not .H is an example of this split.

Also it's a good opportunity to demonstrate the value of making little
helper functions, which is a powerful technique that students
generally need to be taught ;-).

NumPy-Discussion mailing list
NumPy-Discussion at scipy.org

This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  

More information about the NumPy-Discussion mailing list