[Numpy-discussion] add .H attribute?

Dave Hirschfeld dave.hirschfeld at gmail.com
Wed Jul 24 04:23:09 EDT 2013

 <josef.pktd <at> gmail.com> writes:

> I think a H is feature creep and too specialized
> What's .H of a int a str a bool ?
> It's just .T and a view, so you cannot rely that conj() makes a copy
> if you don't work with complex.
> .T is just a reshape function and has **nothing** to do with matrix 

It seems to me that that ship has already sailed - i.e. conj doesn't make 
much sense for str arrays, but it still works in the sense that it's a nop

In [16]: A = asarray(list('abcdefghi')).reshape(3,3)
    ...: np.all(A.T == A.conj().T)
Out[16]: True

If we're voting my vote goes to add the .H attribute for all the reasons 
Alan has specified. Document that it returns a copy but that it may in 
future return a view so it it not future proof to operate on the result 

I'm -1 on .H() as it will require code changes if it ever changes to a 
property and it will simply result in questions about why .T is a property 
and .H is a function (and why it's a property for (sparse) matrices)

Regarding Dag's example:

xh = x.H
x *= 2
assert np.all(2 * xh == x.H)

I'm sceptical that there's much code out there actually relying on the fact 
that a transpose is a view with the specified intention of altering the 
original array inplace.

I work with a lot of beginners and whenever I've seen them operate inplace 
on a transpose it has been a bug in the code, leading to a discussion of 
how, for performance reasons, numpy will return a view where possible, 
leading to yet further discussion of when it is and isn't possible to return 
a view.

The third option of .H returning a view would probably be agreeable to 
everyone but I don't think we should punt on this decision for something 
that if it does happen is likely years away. It seems that work on this 
front is happening in different projects to numpy. Even if for example 
sometime in the future numpy's internals were replaced with libdynd or other 
expression graph engine surely this would result in more breaking changes 
than .H returning a view rather than a copy?!

IANAD so I'm happy with whatever the consensus is I just thought I'd put 
forward the view from a (specific type of) user perspective.


More information about the NumPy-Discussion mailing list