Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conjugate transpose operator ".H" in numpy

It is very convenient in numpy to use the .T attribute to get a transposed version of an ndarray. However, there is no similar way to get the conjugate transpose. Numpy's matrix class has the .H operator, but not ndarray. Because I like readable code, and because I'm too lazy to always write .conj().T, I would like the .H property to always be available to me. How can I add this feature? Is it possible to add it so that it is brainlessly available every time numpy is imported?

(A similar question could by asked about the .I inverse operator.)

like image 831
benpro Avatar asked Nov 14 '14 14:11

benpro


People also ask

What is NumPy transpose in Python?

The numpy. transpose() function is one of the most important functions in matrix multiplication. This function permutes or reserves the dimension of the given array and returns the modified array. The numpy. transpose() function changes the row elements into column elements and the column elements into row elements.

How do you conjugate in NumPy?

The numpy. conj() function helps the user to conjugate any complex number. The conjugate of a complex number is obtained by changing the sign of its imaginary part. If the complex number is 2+5j then its conjugate is 2-5j.

What is difference between .T and transpose () in NumPy?

I guess what is meant is that . T and the transpose() call both return the transpose of the array. In fact, . T return the transpose of the array, while transpose is a more general method_ that can be given axes ( transpose(*axes) , with defaults that make the call transpose() equivalent to .

How do you find the conjugate of a matrix transpose in Python?

getH() method, we can make a conjugate Transpose of any complex matrix either having dimension one or more than more. Example #1 : In this example we can see that with the help of matrix. getH() we can get the conjugate transpose of a complex matrix having any dimension.


1 Answers

You can subclass the ndarray object like:

from numpy import ndarray  class myarray(ndarray):         @property     def H(self):         return self.conj().T 

such that:

a = np.random.rand(3, 3).view(myarray) a.H 

will give you the desired behavior.

like image 138
Saullo G. P. Castro Avatar answered Sep 18 '22 17:09

Saullo G. P. Castro