what exactly norm(x) function do?
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manu Chaudhary
el 21 de Sept. de 2022
Editada: James Tursa
el 22 de Sept. de 2022
Good Afternoon,
I have to convert a part of my matlab code into C++.
I would be great if you can explain me what exactly norm(x) do on a vector of complex number so that I can write a C++ code for it.
I just asked a similar question on stack overflow but could not get much response.
Thank you in advance.
5 comentarios
Respuesta aceptada
James Tursa
el 21 de Sept. de 2022
Editada: James Tursa
el 21 de Sept. de 2022
The equivalent function to norm( ) for a complex column vector x is
sqrt(x' * x)
where x' is the complex conjugate transpose. Essentially sqrt(dot(x,x)). Note that dot( ) does the complex conjugate part of the calculation automatically.
E.g.,
x = [1;2;3;4] + [5;6;7;8]*1i
norm(x)
sqrt(x'*x)
sqrt(dot(x,x))
So in C/C++ just run a loop to sum up the individual element-by-element multiplies for the dot product result. For an element of the form a+b*i you will be summing a^2+b^2 and then taking the sqrt( ) of the final sum.
4 comentarios
James Tursa
el 22 de Sept. de 2022
Editada: James Tursa
el 22 de Sept. de 2022
Yes this algorithm is correct, but I still advise changing your code to do the arithmetic in double to avoid potential pitfalls where the int calculations might overflow.
Más respuestas (1)
Matt J
el 22 de Sept. de 2022
Editada: Matt J
el 22 de Sept. de 2022
Assuming you only care about p=2, If you already have code that computes norm(x) for real x, you can extend it to complex x via,
norm(x) = norm( [norm(real(x)), norm(imag(x)) ] )
which is easily verified below,
x=complex(rand(5,1), rand(5,1));
norm(x)
norm( [norm(real(x)), norm(imag(x)) ] )
Alternatively, if you have an implementation of abs, you could do norm(abs(x),p) which will work for any p-norm:
p=3;
norm(x,p)
norm(abs(x),p)
0 comentarios
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!