File: complexvec.C

package info (click to toggle)
tela 1.28-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,596 kB
  • ctags: 5,519
  • sloc: ansic: 14,013; cpp: 13,376; lex: 1,651; fortran: 1,048; yacc: 834; sh: 715; makefile: 464
file content (544 lines) | stat: -rw-r--r-- 13,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 * This file is part of tela the Tensor Language.
 * Copyright (c) 1994-1996 Pekka Janhunen
 */

/*
  Basic arithmetic (Add,Sub,Mul,Div) operations for complex vectors
  and other data types (scalars and int, real, complex vectors).
  Also some logical etc. operations are included in this file.

  This file is included by objarithm.C, not compiled as a separate unit.

  This file is currently included only if VECTOR_MACHINE is defined.
*/

#define FOR1(i) for (Tint i=0; i<2*N; i+=2)
#define FOR2(i,j) for (Tint i=0,j=0; j<N; i+=2,j++)

/* Notation:
   AddCC - add two complex vectors, producing complex vector
   AddCR - add complex and real vector, and so on.
   AddCCs - add complex vector and complex scalar etc.

   First argument is always output argument.
   */

/*------------------------------------------------------------------------------
  1. Add/Sub functions producing complex vectors.

  CV + CV -> CV				AddCC
  CV + RV -> CV				AddCR
  CV + IV -> CV				AddCI
  CV + C  -> CV				AddCCs
  CV + R  -> CV				 -"-
  RV + C  -> CV				AddRCs
  IV + C  -> CV				AddICs

  We get 6 static functions and 26 inline functions.
  -------------------------------------------------------------------------------*/

static void AddCC(Treal c[], const Treal a[], const Treal b[], Tint N, Treal bsign=1)
{
	VECTORIZED FOR1(i) {
		c[i] = a[i] + bsign*b[i];
		c[i+1] = a[i+1] + bsign*b[i+1];
	}
}

inline void Add(Tcomplex c[], const Tcomplex a[], const Tcomplex b[], Tint N) {
	AddCC((Treal*)c,(const Treal*)a,(const Treal*)b,N);
}

inline void Sub(Tcomplex c[], const Tcomplex a[], const Tcomplex b[], Tint N) {
	AddCC((Treal*)c,(const Treal*)a,(const Treal*)b,N,-1);
}


static void AddCR(Treal c[], const Treal a[], const Treal b[], Tint N, Treal asign=1, Treal bsign=1)
{
	VECTORIZED FOR2(i,j) {
		c[i] = asign*a[i] + bsign*b[j];
		c[i+1] = asign*a[i+1];
	}
}

inline void Add(Tcomplex c[], const Tcomplex a[], const Treal b[], Tint N) {
	AddCR((Treal*)c,(const Treal*)a,b,N);
}

inline void Add(Tcomplex c[], const Treal a[], const Tcomplex b[], Tint N) {
	AddCR((Treal*)c,(const Treal*)b,a,N);
}

inline void Sub(Tcomplex c[], const Tcomplex a[], const Treal b[], Tint N) {
	AddCR((Treal*)c,(const Treal*)a,b,N, 1,-1);
}

inline void Sub(Tcomplex c[], const Treal a[], const Tcomplex b[], Tint N) {
	AddCR((Treal*)c,(const Treal*)b,a,N, -1,1);
}


static void AddCI(Treal c[], const Treal a[], const Tint b[], Tint N, Treal asign=1, Treal bsign=1) {
	VECTORIZED FOR2(i,j) {
		c[i] = asign*a[i] + bsign*b[j];
		c[i+1] = asign*a[i+1];
	}
}

inline void Add(Tcomplex c[], const Tcomplex a[], const Tint b[], Tint N) {
	AddCI((Treal*)c,(const Treal*)a,b,N);
}

inline void Add(Tcomplex c[], const Tint a[], const Tcomplex b[], Tint N) {
	AddCI((Treal*)c,(const Treal*)b,a,N);
}

inline void Sub(Tcomplex c[], const Tcomplex a[], const Tint b[], Tint N) {
	AddCI((Treal*)c,(const Treal*)a,b,N,1,-1);
}

inline void Sub(Tcomplex c[], const Tint a[], const Tcomplex b[], Tint N) {
	AddCI((Treal*)c,(const Treal*)b,a,N,-1,1);
}

inline void Neg(Tcomplex c[], Tint N) {
	Treal *cr = (Treal*)c;
	VECTORIZED FOR1(i) {
		cr[i] = -cr[i];
		cr[i+1] = -cr[i+1];
	}
}

static void AddCCs(Treal c[], const Treal a[], Tcomplex b, Tint N, Treal bsign=1) {
	const Treal br = bsign*b.real();
	const Treal bi = bsign*b.imag();
	VECTORIZED FOR1(i) {
		c[i] = a[i] + br;
		c[i+1] = a[i+1] + bi;
	}
}

inline void Add(Tcomplex c[], const Tcomplex a[], Tcomplex b, Tint N) {
	AddCCs((Treal*)c,(const Treal*)a,b,N);
}

inline void Add(Tcomplex c[], Tcomplex a, const Tcomplex b[], Tint N) {
	AddCCs((Treal*)c,(const Treal*)b,a,N);
}

inline void Add(Tcomplex c[], const Tcomplex a[], Treal b, Tint N) {Add(c,a,Tcomplex(b),N);}
inline void Add(Tcomplex c[], Treal a, const Tcomplex b[], Tint N) {Add(c,Tcomplex(a),b,N);}

inline void Sub(Tcomplex c[], const Tcomplex a[], Tcomplex b, Tint N) {
	AddCCs((Treal*)c,(const Treal*)a,b,N,-1);
}

inline void Sub(Tcomplex c[], Tcomplex a, const Tcomplex b[], Tint N) {
	Sub(c,b,a,N);
	Neg(c,N);
}

inline void Sub(Tcomplex c[], const Tcomplex a[], Treal b, Tint N) {Sub(c,a,Tcomplex(b),N);}
inline void Sub(Tcomplex c[], Treal a, const Tcomplex b[], Tint N) {Sub(c,Tcomplex(a),b,N);}


static void AddRCs(Treal c[], const Treal a[], Tcomplex b, Tint N, Treal asign=1)
{
	const Treal br = b.real();
	const Treal bi = b.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = asign*a[j] + br;
		c[i+1] = bi;
	}
}

inline void Add(Tcomplex c[], const Treal a[], Tcomplex b, Tint N) {
	AddRCs((Treal*)c,a,b,N);
}

inline void Add(Tcomplex c[], Tcomplex a, const Treal b[], Tint N) {Add(c,b,a,N);}

inline void Sub(Tcomplex c[], const Treal a[], Tcomplex b, Tint N) {
	AddRCs((Treal*)c,a,-b,N);
}

inline void Sub(Tcomplex c[], Tcomplex a, const Treal b[], Tint N) {
	AddRCs((Treal*)c,b,a,N,-1);
}


static void AddICs(Treal c[], const Tint a[], Tcomplex b, Tint N, Treal asign=1)
{
	const Treal br = b.real();
	const Treal bi = b.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = asign*a[j] + br;
		c[i+1] = bi;
	}
}

inline void Add(Tcomplex c[], const Tint a[], Tcomplex b, Tint N) {
	AddICs((Treal*)c,a,b,N);
}

inline void Add(Tcomplex c[], Tcomplex a, const Tint b[], Tint N) {Add(c,b,a,N);}

inline void Sub(Tcomplex c[], const Tint a[], Tcomplex b, Tint N) {
	AddICs((Treal*)c,a,-b,N);
}

inline void Sub(Tcomplex c[], Tcomplex a, const Tint b[], Tint N) {
	AddICs((Treal*)c,b,a,N,-1);
}







/*------------------------------------------------------------------------------
  2. Mul functions producing complex vectors.

  CV * CV -> CV				MulCC
  CV * RV -> CV				MulCR
  CV * IV -> CV				MulCI
  CV * C  -> CV				MulCCs
  CV * R  -> CV				AddCR(asign=b,bsign=0)
  RV * C  -> CV				MulRCs
  IV * C  -> CV				MulICs

  We get 6 new static functions and 13 new inline functions.
  -------------------------------------------------------------------------------*/

static void MulCC(Treal c[], const Treal a[], const Treal b[], Tint N)
{
	Treal ci;			// c may be the same vector as a or b !
	VECTORIZED FOR1(i) {
		ci = a[i]*b[i] - a[i+1]*b[i+1];
		c[i+1] = a[i]*b[i+1] + a[i+1]*b[i];
		c[i] = ci;
	}
}

inline void Mul(Tcomplex c[], const Tcomplex a[], const Tcomplex b[], Tint N) {
	MulCC((Treal*)c,(const Treal*)a,(const Treal*)b,N);
}

static void MulCR(Treal c[], const Treal a[], const Treal b[], Tint N)
{
	VECTORIZED FOR2(i,j) {
		c[i] = a[i]*b[j];
		c[i+1] = a[i+1]*b[j];
	}
}

inline void Mul(Tcomplex c[], const Tcomplex a[], const Treal b[], Tint N) {
	MulCR((Treal*)c,(const Treal*)a,b,N);
}

inline void Mul(Tcomplex c[], const Treal a[], const Tcomplex b[], Tint N) {Mul(c,b,a,N);}


static void MulCI(Treal c[], const Treal a[], const Tint b[], Tint N)
{
	VECTORIZED FOR2(i,j) {
		c[i] = a[i]*b[j];
		c[i+1] = a[i+1]*b[j];
	}
}

inline void Mul(Tcomplex c[], const Tcomplex a[], const Tint b[], Tint N) {
	MulCI((Treal*)c,(const Treal*)a,b,N);
}

inline void Mul(Tcomplex c[], const Tint a[], const Tcomplex b[], Tint N) {Mul(c,b,a,N);}


static void MulCCs(Treal c[], const Treal a[], Tcomplex b, Tint N)
{
	Treal ci;			// c and a may be the same vector!
	const Treal br = b.real();
	const Treal bi = b.imag();
	VECTORIZED FOR1(i) {
		ci = a[i]*br - a[i+1]*bi;
		c[i+1] = a[i]*bi + a[i+1]*br;
		c[i] = ci;
	}
}

inline void Mul(Tcomplex c[], const Tcomplex a[], Tcomplex b, Tint N) {
	MulCCs((Treal*)c,(const Treal*)a,b,N);
}

inline void Mul(Tcomplex c[], Tcomplex a, const Tcomplex b[], Tint N) {Mul(c,b,a,N);}

inline void Mul(Tcomplex c[], const Tcomplex a[], Treal b, Tint N) {
	AddCR((Treal*)c,(const Treal*)a,(const Treal*)a,N,b,0);
}

inline void Mul(Tcomplex c[], Treal a, const Tcomplex b[], Tint N) {Mul(c,b,a,N);}


static void MulRCs(Treal c[], const Treal a[], Tcomplex b, Tint N)
{
	const Treal br = b.real();
	const Treal bi = b.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = a[j]*br;
		c[i+1] = a[j]*bi;
	}
}

inline void Mul(Tcomplex c[], const Treal a[], Tcomplex b, Tint N) {
	MulRCs((Treal*)c,a,b,N);
}

inline void Mul(Tcomplex c[], Tcomplex a, const Treal b[], Tint N) {Mul(c,b,a,N);}


static void MulICs(Treal c[], const Tint a[], Tcomplex b, Tint N)
{
	const Treal br = b.real();
	const Treal bi = b.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = a[j]*br;
		c[i+1] = a[j]*bi;
	}
}

inline void Mul(Tcomplex c[], const Tint a[], Tcomplex b, Tint N) {
	MulICs((Treal*)c,a,b,N);
}

inline void Mul(Tcomplex c[], Tcomplex a, const Tint b[], Tint N) {Mul(c,b,a,N);}









/*------------------------------------------------------------------------------
  3. Inv and Div functions producing complex vectors.

  CV / CV -> CV
  CV / RV -> CV
  RV / CV -> CV
  CV / IV -> CV
  IV / CV -> CV
  CV / C  -> CV
  C / CV  -> CV
  CV / R  -> CV
  R / CV  -> CV
  RV / C  -> CV
  C / RV  -> CV
  IV / C  -> CV
  C / IV  -> CV

  We get 5 new static functions and 13 new inline functions.
  -------------------------------------------------------------------------------*/

static void InvC(Treal c[], const Treal a[], Tint N)
{
	Treal C;
	VECTORIZED FOR1(i) {
		C = 1/(a[i]*a[i] + a[i+1]*a[i+1]);
		c[i] = C*a[i];
		c[i+1]= -C*a[i+1];
	}
}

inline void Inv(Tcomplex c[], const Tcomplex a[], int N) {InvC((Treal*)c,(const Treal*)a,N);}

inline void Div(Tcomplex c[], const Tcomplex a[], const Tcomplex b[], Tint N) {
	Inv(c,b,N);
	Mul(c,c,a,N);
}

static void DivCR(Treal c[], const Treal a[], const Treal b[], Tint N)
{
	VECTORIZED FOR2(i,j) {
		c[i] = a[i]/b[j];
		c[i+1] = a[i+1]/b[j];
	}
}

inline void Div(Tcomplex c[], const Tcomplex a[], const Treal b[], Tint N) {
	DivCR((Treal*)c,(const Treal*)a,b,N);
}

inline void Div(Tcomplex c[], const Treal a[], const Tcomplex b[], Tint N) {
	Inv(c,b,N);
	Mul(c,c,a,N);
}

static void DivCI(Treal c[], const Treal a[], const Tint b[], Tint N)
{
	VECTORIZED FOR2(i,j) {
		c[i] = a[i]/b[j];
		c[i+1] = a[i+1]/b[j];
	}
}

inline void Div(Tcomplex c[], const Tcomplex a[], const Tint b[], Tint N) {
	DivCI((Treal*)c,(const Treal*)a,b,N);
}

inline void Div(Tcomplex c[], const Tint a[], const Tcomplex b[], Tint N) {
	Inv(c,b,N);
	Mul(c,c,a,N);
}

inline void Div(Tcomplex c[], const Tcomplex a[], Tcomplex b, Tint N) {
	const Tcomplex invb = 1/b;
	Mul(c,a,invb,N);
}

inline void Div(Tcomplex c[], Tcomplex a, const Tcomplex b[], Tint N) {
	Inv(c,b,N);
	Mul(c,c,a,N);
}

inline void Div(Tcomplex c[], const Tcomplex a[], Treal b, Tint N) {Mul(c,a,1/b,N);}

inline void Div(Tcomplex c[], Treal a, const Tcomplex b[], Tint N) {
	Inv(c,b,N);
	Mul(c,c,a,N);
}

inline void Div(Tcomplex c[], const Treal a[], Tcomplex b, Tint N) {Mul(c,a,1/b,N);}

static void DivCsR(Treal c[], Tcomplex a, const Treal b[], Tint N)
{
	const Treal ar = a.real();
	const Treal ai = a.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = ar/b[j];
		c[i+1] = ai/b[j];
	}
}

inline void Div(Tcomplex c[], Tcomplex a, const Treal b[], Tint N) {
	DivCsR((Treal*)c,a,b,N);
}

inline void Div(Tcomplex c[], const Tint a[], Tcomplex b, Tint N) {Mul(c,a,1/b,N);}

static void DivCsI(Treal c[], Tcomplex a, const Tint b[], Tint N)
{
	const Treal ar = a.real();
	const Treal ai = a.imag();
	VECTORIZED FOR2(i,j) {
		c[i] = ar/b[j];
		c[i+1] = ai/b[j];
	}
}

inline void Div(Tcomplex c[], Tcomplex a, const Tint b[], Tint N) {
	DivCsI((Treal*)c,a,b,N);
}






/*------------------------------------------------------------------------------
  4. Some Eq, Ne stuff
  ------------------------------------------------------------------------------*/

inline void Eq(Tint c[], const Tint a[], const Tcomplex b[], Tint N) {
	const Treal *br = (const Treal*)b;
	VECTORIZED FOR2(i,j)
		c[j] = (a[j] == br[i]) && (0 == br[i+1]);
}
	
inline void Ne(Tint c[], const Tint a[], const Tcomplex b[], Tint N) {
	const Treal *br = (const Treal*)b;
	VECTORIZED FOR2(i,j)
		c[j] = (a[j] != br[i]) || (0 != br[i+1]);
}

static void Eq(Tint c[], const Treal a[], Tcomplex b, Tint N)
{
	if (b.imag()!=0) {
		VECTORIZED FOR2(i,j) c[j] = 0;
	} else {
		const Treal br = b.real();
		VECTORIZED FOR2(i,j)
			c[j] = (a[j] == br);
	}
}

static void Ne(Tint c[], const Treal a[], Tcomplex b, Tint N)
{
	if (b.imag()!=0) {
		VECTORIZED FOR2(i,j) c[j] = 1;
	} else {
		const Treal br = b.real();
		VECTORIZED FOR2(i,j)
			c[j] = (a[j] != br);
	}
}




/*------------------------------------------------------------------------------
  5. Neg for complex vectors
  ------------------------------------------------------------------------------*/

static void Neg(Tcomplex c[], const Tcomplex a[], Tint N)
{
	Treal *cr = (Treal *)c;
	const Treal *ar = (const Treal *)a;
	VECTORIZED for (Tint i=0; i<2*N; i++)
		c[i] = -a[i];
}




/*------------------------------------------------------------------------------
  6. Inc,Dec
  ------------------------------------------------------------------------------*/

inline void Inc(Tint a[], Tint N)
{
	VECTORIZED for (Tint i=0; i<N; i++) a[i]++;
}

inline void Dec(Tint a[], Tint N)
{
	VECTORIZED for (Tint i=0; i<N; i++) a[i]--;
}

inline void Inc(Treal a[], Tint N)
{
	VECTORIZED for (Tint i=0; i<N; i++) a[i]+= 1.0;
}

inline void Dec(Treal a[], Tint N)
{
	VECTORIZED for (Tint i=0; i<N; i++) a[i]-= 1.0;
}

inline void Inc(Tcomplex a[], Tint N)
{
	Treal *ar = (Treal *)a;
	VECTORIZED FOR1(i) a[i]+= 1.0;
}

inline void Dec(Tcomplex a[], Tint N)
{
	Treal *ar = (Treal *)a;
	VECTORIZED FOR1(i) a[i]-= 1.0;
}

#undef FOR1
#undef FOR2