J
J.M.
I have data in a double array of length 2N, which actually represents
complex numbers with real and imaginary parts interlaced. In other words,
elements in this array with even indices represents the real part of a
complex number, elements with odd indices represent the imaginary part. I
actually need an array of length N of type complex<double>. Obviously, I
could copy the data and destroy the first array, but this is an expensive
option. The following seems to work as well:
double* d;
complex<double>* C;
d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = -3.0;
d[3] = -4.0;
d[4] = 5.0;
d[5] = -6.0;
C = reinterpret_cast< complex<double>* > (d);
for(int i=0;i<3;i++){
cout<<" i= "<<i<<": "<<C<<std::endl;
}
delete [] C;
Is this conversion safe, reliable, somehow standard? If it is likely to
produce unpredictable results, I would prefer copying the data.... I need
to be on the safe side, but speed is an issue... And my results should not
be compiler dependent ;-) Thanks in advance for any comments.
Jan
complex numbers with real and imaginary parts interlaced. In other words,
elements in this array with even indices represents the real part of a
complex number, elements with odd indices represent the imaginary part. I
actually need an array of length N of type complex<double>. Obviously, I
could copy the data and destroy the first array, but this is an expensive
option. The following seems to work as well:
double* d;
complex<double>* C;
d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = -3.0;
d[3] = -4.0;
d[4] = 5.0;
d[5] = -6.0;
C = reinterpret_cast< complex<double>* > (d);
for(int i=0;i<3;i++){
cout<<" i= "<<i<<": "<<C<<std::endl;
}
delete [] C;
Is this conversion safe, reliable, somehow standard? If it is likely to
produce unpredictable results, I would prefer copying the data.... I need
to be on the safe side, but speed is an issue... And my results should not
be compiler dependent ;-) Thanks in advance for any comments.
Jan