How can I use this complex number in numpy matrix?

Viewed 193

Here's the Python code I'm working on:

def inver_hopf(x,y,z):
    return (1/np.sqrt(x**2+y**2+(1+z)**2))*np.matrix([[1+z],[x+y.j]],dtype=complex)

The problem happens at [x+y.j], where j means complex unit. It returns me the error message AttributeError: 'int' object has no attribute 'j'. If I remove the dot, then it returns NameError: name 'yj' is not defined. How can I correct that? Thanks!

3 Answers

j alone is a variable, you can have the complex number by typing 1j

Instead of trying to use x and y variables to write the complex number directly, try to pass then to the complex class. Example:

def inver_hopf(x, y):
    return np.matrix([complex(x, y)], dtype=complex)

It depends what you want to archive with your code.

In general writing y.j(object.attribute) you want to access the attribute of the said object. In your case the attribute j of the object y (an int in your example).

If i understand you correctly you want to have a matrix which first entry is pure real with value 1+z and a second entry purely imaginary x+y. To get this you need to multiply the pure real sum x+y with j.

Which would result in:

def inver_hopf(x,y,z):
    return (1/np.sqrt(x**2+y**2+(1+z)**2))*np.matrix([[1+z],[(x+y)*1j]],dtype=complex)
Related