简体   繁体   中英

Finding the magnitude of a 3X1 complex vector in Python?

I was wondering if someone would help me with this problem. I have complex 3x1 matrix that looks like,

matrix = np.matrix([[(0.0009118819655545739+0.0009118819655545738j)], [(0.0009118819655544588-0.0009118819655544589j)], [(-0.0009118819655545161-5.421010862427522e-20j)]])

As I am not interested in the last element I will delete as follows, so I get:

vector = np.delete(matrix, 2, 0)

This gives me,

vector = [[(0.0009118819655545739+0.0009118819655545738j)]]
         [[(0.0009118819655544588-0.0009118819655544589j)]]

I am interested in finding the magnitude of the above vector (2X1). This is what I have done:

for element in vector:
      mag = np.absolute(element)
    
return mag

What I get is the,

mag = 0.0012895958429705512

which is the magnitude of the first element.

How do I get the magnitude of the new 2x1 vector that incorporates the complex values of all elements in the vector?

Is the math correct: if I consider,

vector = [[(x1+x2j)]]
         [[(y1-y2j)]]

As mag = sqrt((x1)**2 + (x2)**2 + (y1)**2 + (y2)**2)) which would instead give me a mag of 0.00182376392 rather than 0.0012895958429705512.

I would really appreciate the help with this problem.

Many thanks

numpy comes with numpy.linalg.norm for calculating the norm of a vector. c.f here

In your case that would simply be:

np.linalg.norm(vector)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM