Home > OS >  Problem using numpy to obtain the complex conjugate of a matrix
Problem using numpy to obtain the complex conjugate of a matrix

Time:11-19

I have the following code:

import numpy as np
A=np.array([[2, 2-9j, -5j], [4-1j, 0, 9 6j], [4j, 6 7j, 6]])
print(A)
print(A.getH())

It doesn't work. I have checked different webs and followed this webpage (geeksforgeeks), and this other(official numpy documentation) but I still get an error and I don't know where. Can someone please help me?

The error is 'numpy.ndarray' object has no attribute 'getH'

CodePudding user response:

That's correct, a numpy array doesn't have a method getH. Your second link actually is the official documentation, and it shows that the method is not called getH. Read the documentation closely!

CodePudding user response:

You have to use numpy.conj() function.

import numpy as np
A=np.array([[2, 2-9j, -5j], [4-1j, 0, 9 6j], [4j, 6 7j, 6]])
print(A)
print(A.conj())

Output

[[ 2. 0.j  2.-9.j -0.-5.j]
 [ 4.-1.j  0. 0.j  9. 6.j]
 [ 0. 4.j  6. 7.j  6. 0.j]]
[[ 2.-0.j  2. 9.j -0. 5.j]
 [ 4. 1.j  0.-0.j  9.-6.j]
 [ 0.-4.j  6.-7.j  6.-0.j]]

CodePudding user response:

You're looking at pages about the numpy.matrix class, but the numpy.array function creates instances of numpy.ndarray, not numpy.matrix.

You could use numpy.matrix, but that's a bad idea. numpy.matrix has a lot of weird compatibility problems, and its use is discouraged in new code. Instead, use numpy.conj:

print(numpy.conj(A))
  • Related