Conjugate transpose of self using numpy syntax


Conjugate transpose of self using numpy syntax



I am trying to translate this MATLAB code into Python.



The following is the code:


Y=C*Up(:,1:p-1)'*Y;



And this is my translation thus far:


Y = C * Up[:, 1:p-1] * Y



I am having trouble with the syntax for the conjugate transpose of self that is used in the MATLAb code. I am not certain that my first idea:


Y = C * Up[:, 1:p-1].getH() * Y



would be correct.



Does anyone have any ideas?





Can you provide sample inputs for the matrix (matrices)?
– rahlf23
Jun 29 at 17:47





1:p-1 - remember, python indexing is 0 based, MATLAB 1 based.
– hpaulj
Jun 29 at 18:32


1:p-1





Is Up (possibly) complex? If not then plain transpose is enough. I was going to warn about transpose not doing anything to 1d arrays, but Up is evidently 2d, Also what kind of multiplication do you want? Matrix or element wise? MATLAB and numpy have different operators for those.
– hpaulj
Jun 29 at 18:35


Up


transpose


Up





.getH is a method for np.matrix subclass. np.matrix is MATLAB like (including its definition of *), it's use in numpy is discouraged. Up.T.conjugte() will do the same for ndarray objects. Of course the conjugate part is needed only if Up is complex.
– hpaulj
Jun 29 at 18:51


.getH


np.matrix


np.matrix


*


numpy


Up.T.conjugte()


ndarray


conjugate


Up




1 Answer
1



I am not very experienced with numpy, but based on the comments of @hpaulj I can suggest the following:



If you don't want to be subject to the limitations of numpy.matrix objects (see warning here), you can define your own function for doing a conjugate transpose. All you need to do is transpose your array, then subtract the imaginary part of the result, times 2, from the result. I am not sure how computationally efficient this is, but it should definitely give the correct result.


numpy.matrix



I'd expect something like this to work:


Y = C * ctranspose(Up[:, 0:p-1]) * Y

...

def ctranspose(arr: np.ndarray) -> np.ndarray:
# Explanation of the math involved:
# x == Real(X) + j*Imag(X)
# conj_x == Real(X) - j*Imag(X)
# conj_x == Real(X) + j*Imag(X) - 2j*Imag(X) == x - 2j*Imag(X)
tmp = arr.transpose()
return tmp - 2j*tmp.imag



(Solution is for Python 3)



A more elegant solution based on the comment by @AndrasDeak:


Y = C * Up[:, 0:p-1].conj().T * Y



Note also, two differences related to indexing between python and MATLAB:


0


1


inclusive:exclusive


inclusive:inclusive



Therefore, when we want to access the first 3 elements of a vector in MATLAB we'd write:


res = vec(1:3);



In Python we'd write:


res = vec[0:3] # or [:3]



(Again, credits to @Andras for this explanation)





Why not arr.conj().T?
– Andras Deak
Jun 30 at 10:55


arr.conj().T





Elementary my dear @Andras - because I didn't know that such a function exists :)
– Dev-iL
Jun 30 at 10:57






Also * only does matmul if OP has np.matrices, otherwise they need @.
– Andras Deak
2 days ago



*


np.matri


@






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift