Initializing a complex dynamic array (with real part from one array, and imaginary from other array)?

drug drug2004 at bk.ru
Wed Jul 21 09:17:03 UTC 2021


I wouldn't state it is the best way but you can try something like that:
```D
import std.complex;
import std.range : zip;
import std.algorithm : equal, map;
import std.array : array;

void main(){
     auto N=2;

     double[] x,y;
     x.length = N;
     y.length = N;

     x[0] = 1.1;
     x[1] = 2.2;
     y[0] = 3.3;
     y[1] = 4.4;

     auto z = zip(x, y)                      // concatenate two ranges
         .map!(a=>Complex!double(a[0],a[1])) // take the current first 
element of the first range as the real part and the current first 
element of the second range as the imaginary part
         .array;                             // convert the lazy range 
to a dynamic array, probably you can avoid but this depends on how you 
use it later

	assert(z.equal([
         Complex!double(1.1, 3.3),
         Complex!double(2.2, 4.4),
     ]));
}
```D



More information about the Digitalmars-d-learn mailing list