what exactly norm(x) function do?
8 ビュー (過去 30 日間)
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.
James Tursa 2022 年 9 月 21 日
編集済み: James Tursa 2022 年 9 月 21 日
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.
x = [1;2;3;4] + [5;6;7;8]*1i
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.
その他の回答 (1 件)
Matt J 2022 年 9 月 22 日
編集済み: Matt J 2022 年 9 月 22 日
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,
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: