real and imaginary part of a complex number
I have 2 pointers which points to two 20 member arrays. My arrays contains complex numbers. I want to make element by element division for that complex numbers that is why I need to separate numbers to real and imaginary parts. I tried the following code but it gives error.
#include <complex>
complex *a;
complex *b;
complex array1[20];
complex array2[20];
a = &array1;
b = &array2;
int i=0;
for (i=0;i<=19;i++)
{
real_part_array1[i] = real(*a[i]);
imag_part_array1[i] = imag(*a[i]);
real_part_array2[i] = real(*b[i]);
imag_part_array2[i] = imag(*b[i]);
}
First error I got was; I tried to write it as
#include <complex.h>
the error message was "cannot open source file complex.h". Then i deleted h and error was gone. The second error I have is for real() and imag(). The error message is "identifier real is undefined".
For division I have to seperate them to real and imaginary parts but I dont know how to solve that problem. I hope you guys can help me.
complex
is not a type, it's a type template. You need to specify the type of the real and imaginary components as a template parameter, eg complex<double>
.
The type template complex
and the functions real
and imag
are in the std
namespace.
Regarding complex<...>
, you can either write std::complex<...>
or put using std::complex; below your includes. (You could also write
using std::complex; below your includes. (You could also write
using std::complex; below your includes. (You could also write
using namespace std;` but that might be dangerous to get used to it.)
Regarding real
and imag
, they can use ADL ( a rgument d ependent l ookup: when their argument is in the std
namespace, the function name is automatically looked up in std
too), so you don't need to specify the namespace for these functions.
In the line a = &array1;
(and the other one analogous), you point to the whole array array1
, which is a pointer to array. What you probably want is either &array[1]
or just array1
, as arrays can be converted implicitly to the pointer to their first element.
In *a[i]
you access the i-th element in the array a
points to ( a
itself is not a pointer but the array subscript operator works on pointers as if they were arrays). Then you dereference that complex type, which is invalid. Simply drop the *
.
You can see the final code here.
You probably want to use it like :
#include <complex>
int main()
{
std::complex<float> *a;
std::complex<float> *b;
std::complex<float> array1[20];
std::complex<float> array2[20];
int real_part_array1[20];
int real_part_array2[20];
int imag_part_array1[20];
int imag_part_array2[20];
a = array1;
b = array2;
int i=0;
for (i=0;i<=19;i++)
{
real_part_array1[i] = std::real(a[i]);
imag_part_array1[i] = std::imag(a[i]);
real_part_array2[i] = std::real(b[i]);
imag_part_array2[i] = std::imag(b[i]);
}
return 0;
}
complex is not a type but a type template. Hence you may want to use something like complex< int > or complex< double >.
Also real and imag are in the std namespace, hence std::real and std::imag.
Now when you say a = &array1, array1 is already a pointer, you are assigning a pointer to a pointer to the LHS, which is a pointer, this is a type error
链接地址: http://www.djcxy.com/p/24380.html上一篇: 与MATLAB中的复杂数字操作混淆
下一篇: 复数的实部和虚部