PSCF v1.5.0
VecOp.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "VecOp.h"
9#include <util/containers/Array.h>
10#include <cmath>
11
12#include <fftw3.h>
13
14namespace Pscf {
15namespace VecOp {
16
17 using namespace Util;
18
19 // Operations involving real array
20
21 // Assignment
22
23 /*
24 * Vector-vector assignment, a[i] = b[i] (real, slice).
25 */
26 void eqV(Array<double>& a, Array<double> const & b,
27 const int beginIdA, const int beginIdB, const int n)
28 {
29 UTIL_CHECK(beginIdA >= 0);
30 UTIL_CHECK(beginIdB >= 0);
31 UTIL_CHECK(n > 0);
32 UTIL_CHECK(a.capacity() >= beginIdA + n);
33 UTIL_CHECK(b.capacity() >= beginIdB + n);
34 for (int i = 0; i < n; ++i) {
35 a[i + beginIdA] = b[i + beginIdB];
36 }
37 }
38
39 /*
40 * Vector-vector assignment, a[i] = b[i] (real).
41 */
42 void eqV(Array<double>& a, Array<double> const & b)
43 {
44 const int n = a.capacity();
45 UTIL_CHECK(n > 0);
46 UTIL_CHECK(b.capacity() >= n);
47 for (int i = 0; i < n; ++i) {
48 a[i] = b[i];
49 }
50 }
51
52 /*
53 * Vector-scalar assignment, a[i] = b (real).
54 */
55 void eqS(Array<double>& a, double b)
56 {
57 const int n = a.capacity();
58 UTIL_CHECK(n > 0);
59 for (int i = 0; i < n; ++i) {
60 a[i] = b;
61 }
62 }
63
64 // Addition
65
66 /*
67 * Vector-vector addition, a[i] = b[i] + c[i] (real).
68 */
70 Array<double> const & b, Array<double> const & c)
71 {
72 const int n = a.capacity();
73 UTIL_CHECK(n > 0);
74 UTIL_CHECK(b.capacity() >= n);
75 UTIL_CHECK(c.capacity() >= n);
76 for (int i = 0; i < n; ++i) {
77 a[i] = b[i] + c[i];
78 }
79 }
80
81 /*
82 * Vector-scalar addition, a[i] = b[i] + c (real).
83 */
84 void addVS(Array<double>& a, Array<double> const & b, double c)
85 {
86 const int n = a.capacity();
87 UTIL_CHECK(n > 0);
88 UTIL_CHECK(b.capacity() >= n);
89
90 for (int i = 0; i < n; ++i) {
91 a[i] = b[i] + c;
92 }
93 }
94
95 // Subtraction
96
97 /*
98 * Vector-vector subtraction, a[i] = b[i] - c[i] (real).
99 */
101 Array<double> const & b, Array<double> const & c)
102 {
103 const int n = a.capacity();
104 UTIL_CHECK(n > 0);
105 UTIL_CHECK(b.capacity() >= n);
106 UTIL_CHECK(c.capacity() >= n);
107 for (int i = 0; i < n; ++i) {
108 a[i] = b[i] - c[i];
109 }
110 }
111
112 /*
113 * Vector-scalar subtraction, a[i] = b[i] - c (real).
114 */
115 void subVS(Array<double>& a, Array<double> const & b, double c)
116 {
117 const int n = a.capacity();
118 UTIL_CHECK(n > 0);
119 UTIL_CHECK(b.capacity() >= n);
120 for (int i = 0; i < n; ++i) {
121 a[i] = b[i] - c;
122 }
123 }
124
125 // Multiplication
126
127 /*
128 * Vector-vector multiplication, a[i] = b[i] * c[i] (real).
129 */
131 Array<double> const & b, Array<double> const & c)
132 {
133 const int n = a.capacity();
134 UTIL_CHECK(n > 0);
135 UTIL_CHECK(b.capacity() >= n);
136 UTIL_CHECK(c.capacity() >= n);
137 for (int i = 0; i < n; ++i) {
138 a[i] = b[i] * c[i];
139 }
140 }
141
142 /*
143 * Vector-scalar multiplication, a[i] = b[i] * c (real).
144 */
145 void mulVS(Array<double>& a, Array<double> const & b, double c)
146 {
147 const int n = a.capacity();
148 UTIL_CHECK(n > 0);
149 UTIL_CHECK(b.capacity() >= n);
150 for (int i = 0; i < n; ++i) {
151 a[i] = b[i] * c;
152 }
153 }
154
155 // Division
156
157 /*
158 * Vector-vector division, a[i] = b[i] / c[i] (real).
159 */
161 Array<double> const & b, Array<double> const & c)
162 {
163 const int n = a.capacity();
164 UTIL_CHECK(n > 0);
165 UTIL_CHECK(b.capacity() >= n);
166 UTIL_CHECK(c.capacity() >= n);
167 for (int i = 0; i < n; ++i) {
168 a[i] = b[i] / c[i];
169 }
170 }
171
172 /*
173 * Vector-scalar division, a[i] = b[i] / c (real).
174 */
175 void divVS(Array<double>& a, Array<double> const & b, double c)
176 {
177 const int n = a.capacity();
178 UTIL_CHECK(n > 0);
179 UTIL_CHECK(b.capacity() >= n);
180 for (int i = 0; i < n; ++i) {
181 a[i] = b[i] / c;
182 }
183 }
184
185 /*
186 * Scalar-vector division, a[i] = b / c[i] (real).
187 */
188 void divSV(Array<double>& a, double b, Array<double> const & c)
189 {
190 const int n = a.capacity();
191 UTIL_CHECK(n > 0);
192 UTIL_CHECK(c.capacity() >= n);
193 for (int i = 0; i < n; ++i) {
194 a[i] = b/c[i];
195 }
196 }
197
198 // In-place addition
199
200 /*
201 * Vector-vector in-place addition, a[i] += b[i] (real).
202 */
204 {
205 const int n = a.capacity();
206 UTIL_CHECK(n > 0);
207 UTIL_CHECK(b.capacity() >= n);
208 for (int i = 0; i < n; ++i) {
209 a[i] += b[i];
210 }
211 }
212
213 /*
214 * Vector-scalar in-place addition, a[i] += b (real).
215 */
216 void addEqS(Array<double>& a, double b)
217 {
218 const int n = a.capacity();
219 UTIL_CHECK(n > 0);
220 for (int i = 0; i < n; ++i) {
221 a[i] += b;
222 }
223 }
224
225 // In-place subtraction
226
227 /*
228 * Vector-vector in-place subtraction, a[i] -= b[i] (real).
229 */
231 {
232 const int n = a.capacity();
233 UTIL_CHECK(n > 0);
234 UTIL_CHECK(b.capacity() >= n);
235 for (int i = 0; i < n; ++i) {
236 a[i] -= b[i];
237 }
238 }
239
240 /*
241 * Vector-scalar in-place subtraction, a[i] -= b (real).
242 */
243 void subEqS(Array<double>& a, double b)
244 {
245 const int n = a.capacity();
246 UTIL_CHECK(n > 0);
247 for (int i = 0; i < n; ++i) {
248 a[i] -= b;
249 }
250 }
251
252 // In-place multiplication
253
254 /*
255 * Vector-vector in-place multiplication, a[i] *= b[i] (real).
256 */
258 {
259 const int n = a.capacity();
260 UTIL_CHECK(n > 0);
261 UTIL_CHECK(b.capacity() >= n);
262 for (int i = 0; i < n; ++i) {
263 a[i] *= b[i];
264 }
265 }
266
267 /*
268 * Vector-scalar in-place multiplication, a[i] *= b (real).
269 */
270 void mulEqS(Array<double>& a, double b)
271 {
272 const int n = a.capacity();
273 UTIL_CHECK(n > 0);
274 for (int i = 0; i < n; ++i) {
275 a[i] *= b;
276 }
277 }
278
279 // In-place division
280
281 /*
282 * Vector-vector in-place division, a[i] /= b[i] (real).
283 */
285 {
286 const int n = a.capacity();
287 UTIL_CHECK(n > 0);
288 UTIL_CHECK(b.capacity() >= n);
289 for (int i = 0; i < n; ++i) {
290 a[i] /= b[i];
291 }
292 }
293
294 /*
295 * Vector-scalar in-place division, a[i] /= b (real).
296 */
297 void divEqS(Array<double>& a, double b)
298 {
299 const int n = a.capacity();
300 UTIL_CHECK(n > 0);
301 for (int i = 0; i < n; ++i) {
302 a[i] /= b;
303 }
304 }
305
306 // Exponentiation
307
308 /*
309 * Vector exponentiation, a[i] = exp(b[i]) (real).
310 */
311 void expV(Array<double>& a, Array<double> const & b)
312 {
313 const int n = a.capacity();
314 UTIL_CHECK(n > 0);
315 UTIL_CHECK(b.capacity() >= n);
316 for (int i = 0; i < n; ++i) {
317 a[i] = exp(b[i]);
318 }
319 }
320
321 /*
322 * Exponentiate a scaled vector, a[i] = exp(b[i]*c) (real).
323 */
324 void expVc(Array<double>& a, Array<double> const & b, const double c)
325 {
326 const int n = a.capacity();
327 UTIL_CHECK(n > 0);
328 UTIL_CHECK(b.capacity() >= n);
329 for (int i = 0; i < n; ++i) {
330 a[i] = exp(b[i]*c);
331 }
332 }
333
334 // Square
335
336 /*
337 * Vector elementwise square, a[i] = b[i]*b[i] (real).
338 */
339 void sqV(Array<double>& a, Array<double> const & b)
340 {
341 const int n = a.capacity();
342 UTIL_CHECK(n > 0);
343 UTIL_CHECK(b.capacity() >= n);
344 for (int i = 0; i < n; ++i) {
345 a[i] = b[i]*b[i];
346 }
347 }
348
349 // Absolute magnitude
350
351 /*
352 * Vector elementwise square, a[i] = b[i]*b[i] (real).
353 */
354 void absV(Array<double>& a, Array<double> const & b)
355 {
356 const int n = a.capacity();
357 UTIL_CHECK(n > 0);
358 UTIL_CHECK(b.capacity() >= n);
359 for (int i = 0; i < n; ++i) {
360 a[i] = std::abs(b[i]);
361 }
362 }
363
364 // Linear combinations (separate result)
365
366 /*
367 * Add two scaled vectors, a[i] = b1[i]*c1 + b2[2]*c2 (real).
368 */
370 Array<double> const & b1, const double c1,
371 Array<double> const & b2, const double c2)
372 {
373 const int n = a.capacity();
374 UTIL_CHECK(n > 0);
375 UTIL_CHECK(b1.capacity() >= n);
376 UTIL_CHECK(b2.capacity() >= n);
377 for (int i = 0; i < n; ++i) {
378 a[i] = b1[i]*c1 + b2[i]*c2;
379 }
380 }
381
382 /*
383 * Add a scaled vector and a scalar, a[i] = b[i]*c + s (real).
384 */
386 Array<double> const & b, const double c,
387 const double s)
388 {
389 const int n = a.capacity();
390 UTIL_CHECK(n > 0);
391 UTIL_CHECK(b.capacity() >= n);
392 for (int i = 0; i < n; ++i) {
393 a[i] = b[i]*c + s;
394 }
395 }
396
397 /*
398 * In-place add one scaled vector, a[i] += b[i]*c (real).
399 */
401 Array<double> const & b, const double c)
402 {
403 const int n = a.capacity();
404 UTIL_CHECK(n > 0);
405 UTIL_CHECK(b.capacity() >= n);
406 for (int i = 0; i < n; ++i) {
407 a[i] += b[i]*c;
408 }
409 }
410
411 /*
412 * Add 2 scaled vectors & scalar, a[i] = b1[i]*c1 + b2[i]*c2 + s (real).
413 */
415 Array<double> const & b1, const double c1,
416 Array<double> const & b2, const double c2,
417 const double s)
418 {
419 const int n = a.capacity();
420 UTIL_CHECK(n > 0);
421 UTIL_CHECK(b1.capacity() >= n);
422 UTIL_CHECK(b2.capacity() >= n);
423 for (int i = 0; i < n; ++i) {
424 a[i] = b1[i]*c1 + b2[i]*c2 + s;
425 }
426 }
427
428 /*
429 * Add 3 scaled vectors, a[i] = b1[i]*c1 + b2[i]*c2 + b3[i]*c3 (real).
430 */
432 Array<double> const & b1, const double c1,
433 Array<double> const & b2, const double c2,
434 Array<double> const & b3, const double c3)
435 {
436 const int n = a.capacity();
437 UTIL_CHECK(n > 0);
438 UTIL_CHECK(b1.capacity() >= n);
439 UTIL_CHECK(b2.capacity() >= n);
440 UTIL_CHECK(b3.capacity() >= n);
441 for (int i = 0; i < n; ++i) {
442 a[i] = b1[i]*c1 + b2[i]*c2 + b3[i]*c3;
443 }
444 }
445
446 // Pair operations (two output arrays and a shared input)
447
448 /*
449 * Vector assignment in pairs, ax[i] = b[i], x = 1, 2 (real).
450 */
452 Array<double>& a2,
453 Array<double> const & b)
454 {
455 const int n = a1.capacity();
456 UTIL_CHECK(n > 0);
457 UTIL_CHECK(a2.capacity() == n);
458 UTIL_CHECK(b.capacity() >= n);
459 for (int i = 0; i < n; ++i) {
460 a1[i] = b[i];
461 a2[i] = b[i];
462 }
463 }
464
465 /*
466 * Vector multiplication in pairs, ax[i] = bx[i] * c[i], x=1,2 (real).
467 */
469 Array<double>& a2,
470 Array<double> const & b1,
471 Array<double> const & b2,
472 Array<double> const & c)
473 {
474 const int n = a1.capacity();
475 UTIL_CHECK(n > 0);
476 UTIL_CHECK(a2.capacity() == n);
477 UTIL_CHECK(b1.capacity() >= n);
478 UTIL_CHECK(b2.capacity() >= n);
479 UTIL_CHECK(c.capacity() >= n);
480 for (int i = 0; i < n; ++i) {
481 a1[i] = b1[i]*c[i];
482 a2[i] = b2[i]*c[i];
483 }
484 }
485
486 /*
487 * In-place vector multiplication in pairs, ax[i] *= b[i], x=1,2 (real).
488 */
490 Array<double>& a2,
491 Array<double> const & b)
492 {
493 const int n = a1.capacity();
494 UTIL_CHECK(n > 0);
495 UTIL_CHECK(a2.capacity() == n);
496 UTIL_CHECK(b.capacity() >= n);
497 for (int i = 0; i < n; ++i) {
498 a1[i] *= b[i];
499 a2[i] *= b[i];
500 }
501 }
502
503 // Real and imaginary parts
504
505 /*
506 * Copy real part of a complex array to a real array.
507 */
509 {
510 const int n = a.capacity();
511 UTIL_CHECK(n > 0);
512 UTIL_CHECK(b.capacity() == n);
513 for (int i = 0; i < n; ++i) {
514 a[i] = b[i][0];
515 }
516 }
517
518 /*
519 * Copy imaginary part of a complex array to a real array.
520 */
522 {
523 const int n = a.capacity();
524 UTIL_CHECK(n > 0);
525 UTIL_CHECK(b.capacity() == n);
526 for (int i = 0; i < n; ++i) {
527 a[i] = b[i][1];
528 }
529 }
530
531 // Assignment
532
533 /*
534 * Vector assignment, a[i] = b[i] (complex).
535 */
537 {
538 const int n = a.capacity();
539 UTIL_CHECK(n > 0);
540 UTIL_CHECK(b.capacity() == n);
541 for (int i = 0; i < n; ++i) {
542 a[i][0] = b[i][0];
543 a[i][1] = b[i][1];
544 }
545 }
546
547 /*
548 * Vector assignment, a[i] = (b[i], c[i]) (complex, real and imaginary).
549 */
551 Array<double> const & b,
552 Array<double> const & c)
553 {
554 const int n = a.capacity();
555 UTIL_CHECK(n > 0);
556 UTIL_CHECK(b.capacity() == n);
557 UTIL_CHECK(c.capacity() == n);
558 for (int i = 0; i < n; ++i) {
559 a[i][0] = b[i];
560 a[i][1] = c[i];
561 }
562 }
563
564 /*
565 * Vector assignment, a[i] = b[i] (mixed).
566 */
568 {
569 const int n = a.capacity();
570 UTIL_CHECK(n > 0);
571 UTIL_CHECK(b.capacity() == n);
572 for (int i = 0; i < n; ++i) {
573 a[i][0] = b[i];
574 a[i][1] = 0.0;
575 }
576 }
577
578 /*
579 * Vector-scalar assignment, a[i][0] = b (complex).
580 */
581 void eqS(Array<fftw_complex> & a, fftw_complex b)
582 {
583 const int n = a.capacity();
584 UTIL_CHECK(n > 0);
585 for (int i = 0; i < n; ++i) {
586 a[i][0] = b[0];
587 a[i][1] = b[1];
588 }
589 }
590
591 /*
592 * Vector-scalar assignment, a[i] = b (mixed).
593 */
594 void eqS(Array<fftw_complex> & a, double b)
595 {
596 const int n = a.capacity();
597 UTIL_CHECK(n > 0);
598 for (int i = 0; i < n; ++i) {
599 a[i][0] = b;
600 a[i][1] = 0.0;
601 }
602 }
603
604 // Addition
605
606 /*
607 * Vector addition, a[i] = b[i] + c[i] (complex).
608 */
610 Array<fftw_complex> const & b,
611 Array<fftw_complex> const & c)
612 {
613 const int n = a.capacity();
614 UTIL_CHECK(n > 0);
615 UTIL_CHECK(b.capacity() == n);
616 UTIL_CHECK(c.capacity() == n);
617 for (int i = 0; i < n; ++i) {
618 a[i][0] = b[i][0] + c[i][0];
619 a[i][1] = b[i][1] + c[i][1];
620 }
621 }
622
623 /*
624 * Vector addition, a[i] = b[i] + c[i] (mixed).
625 */
627 Array<fftw_complex> const & b,
628 Array<double> const & c)
629 {
630 const int n = a.capacity();
631 UTIL_CHECK(n > 0);
632 UTIL_CHECK(b.capacity() == n);
633 UTIL_CHECK(c.capacity() == n);
634 for (int i = 0; i < n; ++i) {
635 a[i][0] = b[i][0] + c[i];
636 a[i][1] = b[i][1];
637 }
638 }
639
640 /*
641 * Vector-scalar addition, a[i][0] = b[i][0] + c (complex).
642 */
644 Array<fftw_complex> const & b,
645 fftw_complex c)
646 {
647 const int n = a.capacity();
648 UTIL_CHECK(n > 0);
649 UTIL_CHECK(b.capacity() == n);
650
651 for (int i = 0; i < n; ++i) {
652 a[i][0] = b[i][0] + c[0];
653 a[i][1] = b[i][1] + c[1];
654 }
655 }
656
657 /*
658 * Vector-scalar addition, a[i][0] = b[i][0] + c (mixed).
659 */
661 Array<fftw_complex> const & b,
662 double c)
663 {
664 const int n = a.capacity();
665 UTIL_CHECK(n > 0);
666 UTIL_CHECK(b.capacity() == n);
667 for (int i = 0; i < n; ++i) {
668 a[i][0] = b[i][0] + c;
669 a[i][1] = b[i][1];
670 }
671 }
672
673 // Subtraction
674
675 /*
676 * Vector subtraction, a[i] = b[i] - c[i] (complex).
677 */
679 Array<fftw_complex> const & b,
680 Array<fftw_complex> const & c)
681 {
682 const int n = a.capacity();
683 UTIL_CHECK(n > 0);
684 UTIL_CHECK(b.capacity() == n);
685 UTIL_CHECK(c.capacity() == n);
686 for (int i = 0; i < n; ++i) {
687 a[i][0] = b[i][0] - c[i][0];
688 a[i][1] = b[i][1] - c[i][1];
689 }
690 }
691
692 /*
693 * Vector subtraction, a[i] = b[i] - c[i] (mixed).
694 */
696 Array<fftw_complex> const & b,
697 Array<double> const & c)
698 {
699 const int n = a.capacity();
700 UTIL_CHECK(n > 0);
701 UTIL_CHECK(b.capacity() == n);
702 UTIL_CHECK(c.capacity() == n);
703 for (int i = 0; i < n; ++i) {
704 a[i][0] = b[i][0] - c[i];
705 a[i][1] = b[i][1];
706 }
707 }
708
709 /*
710 * Vector-scalar subtraction, a[i] = b[i] - c (complex).
711 */
713 Array<fftw_complex> const & b,
714 fftw_complex c)
715 {
716 const int n = a.capacity();
717 UTIL_CHECK(n > 0);
718 UTIL_CHECK(b.capacity() == n);
719 for (int i = 0; i < n; ++i) {
720 a[i][0] = b[i][0] - c[0];
721 a[i][1] = b[i][1] - c[1];
722 }
723 }
724
725 /*
726 * Vector-scalar subtraction, a[i] = b[i] - c (mixed).
727 */
729 Array<fftw_complex> const & b,
730 double c)
731 {
732 const int n = a.capacity();
733 UTIL_CHECK(n > 0);
734 UTIL_CHECK(b.capacity() == n);
735 for (int i = 0; i < n; ++i) {
736 a[i][0] = b[i][0] - c;
737 a[i][1] = b[i][1];
738 }
739 }
740
741 // Multiplication
742
743 /*
744 * Vector multiplication, a[i] = b[i] * c[i] (complex).
745 */
747 Array<fftw_complex> const & b,
748 Array<fftw_complex> const & c)
749 {
750 const int n = a.capacity();
751 UTIL_CHECK(n > 0);
752 UTIL_CHECK(b.capacity() == n);
753 UTIL_CHECK(c.capacity() == n);
754 for (int i = 0; i < n; ++i) {
755 a[i][0] = b[i][0]*c[i][0] - b[i][1]*c[i][1];
756 a[i][1] = b[i][0]*c[i][1] + b[i][1]*c[i][0];
757 }
758 }
759
760 /*
761 * Vector multiplication, a[i] = b[i] * c[i] (mixed).
762 */
764 Array<fftw_complex> const & b,
765 Array<double> const & c)
766 {
767 const int n = a.capacity();
768 UTIL_CHECK(n > 0);
769 UTIL_CHECK(b.capacity() == n);
770 UTIL_CHECK(c.capacity() == n);
771 for (int i = 0; i < n; ++i) {
772 a[i][0] = b[i][0]*c[i];
773 a[i][1] = b[i][1]*c[i];
774 }
775 }
776
777 /*
778 * Vector-scalar multiplication, a[i] = b[i] * c (complex)
779 */
781 Array<fftw_complex> const & b,
782 fftw_complex c)
783 {
784 const int n = a.capacity();
785 UTIL_CHECK(n > 0);
786 UTIL_CHECK(b.capacity() == n);
787 for (int i = 0; i < n; ++i) {
788 a[i][0] = b[i][0]*c[0] - b[i][1]*c[1];
789 a[i][1] = b[i][0]*c[1] + b[i][1]*c[0];
790 }
791 }
792
793 /*
794 * Vector-scalar multiplication, a[i] = b[i] * c (mixed).
795 */
797 Array<fftw_complex> const & b,
798 double c)
799 {
800 const int n = a.capacity();
801 UTIL_CHECK(n > 0);
802 UTIL_CHECK(b.capacity() == n);
803 for (int i = 0; i < n; ++i) {
804 a[i][0] = b[i][0]*c;
805 a[i][1] = b[i][1]*c;
806 }
807 }
808
809 // Division
810
811 /*
812 * Vector division, a[i] = b[i] / c[i] (complex).
813 */
815 Array<fftw_complex> const & b,
816 Array<fftw_complex> const & c)
817 {
818 const int n = a.capacity();
819 UTIL_CHECK(n > 0);
820 UTIL_CHECK(b.capacity() == n);
821 UTIL_CHECK(c.capacity() == n);
822 double cr, ci, cSq;
823 for (int i = 0; i < n; ++i) {
824 cr = c[i][0];
825 ci = c[i][1];
826 cSq = cr*cr + ci*ci;
827 a[i][0] = (b[i][0]*cr + b[i][1]*ci)/cSq;
828 a[i][1] = (b[i][1]*cr - b[i][0]*ci)/cSq;
829 }
830 }
831
832 /*
833 * Vector division, a[i] = b[i] / c[i] (mixed).
834 */
836 Array<fftw_complex> const & b,
837 Array<double> const & c)
838 {
839 const int n = a.capacity();
840 UTIL_CHECK(n > 0);
841 UTIL_CHECK(b.capacity() == n);
842 UTIL_CHECK(c.capacity() == n);
843 for (int i = 0; i < n; ++i) {
844 a[i][0] = b[i][0] / c[i];
845 a[i][1] = b[i][1] / c[i];
846 }
847 }
848
849 /*
850 * Vector-scalar division, a[i][0] = b[i][0] / c (complex).
851 */
853 Array<fftw_complex> const & b,
854 fftw_complex c)
855 {
856 const int n = a.capacity();
857 UTIL_CHECK(n > 0);
858 UTIL_CHECK(b.capacity() == n);
859 double cr = c[0];
860 double ci = c[1];
861 double cSq = cr * cr + ci * ci;
862 for (int i = 0; i < n; ++i) {
863 a[i][0] = (b[i][0]*cr + b[i][1]*ci)/cSq;
864 a[i][1] = (b[i][1]*cr - b[i][0]*ci)/cSq;
865 }
866 }
867
868 /*
869 * Vector-scalar division, a[i] = b[i] / c (mixed).
870 */
872 Array<fftw_complex> const & b,
873 double c)
874 {
875 const int n = a.capacity();
876 UTIL_CHECK(n > 0);
877 UTIL_CHECK(b.capacity() == n);
878 for (int i = 0; i < n; ++i) {
879 a[i][0] = b[i][0] / c;
880 a[i][1] = b[i][1] / c;
881 }
882 }
883
884 // In-place addition
885
886 /*
887 * Vector in-place addition, a[i] += b[i] (complex).
888 */
890 Array<fftw_complex> const & b)
891 {
892 const int n = a.capacity();
893 UTIL_CHECK(n > 0);
894 UTIL_CHECK(b.capacity() == n);
895 for (int i = 0; i < n; ++i) {
896 a[i][0] += b[i][0];
897 a[i][1] += b[i][1];
898 }
899 }
900
901 /*
902 * Vector in-place addition, a[i] += (b[i], c[i]) (complex, real/imag).
903 */
905 Array<double> const & b,
906 Array<double> const & c)
907 {
908 const int n = a.capacity();
909 UTIL_CHECK(n > 0);
910 UTIL_CHECK(b.capacity() == n);
911 UTIL_CHECK(c.capacity() == n);
912 for (int i = 0; i < n; ++i) {
913 a[i][0] += b[i];
914 a[i][1] += c[i];
915 }
916 }
917
918 /*
919 * Vector in-place addition, a[i] += b[i] (mixed, b real).
920 */
921 void addEqV(Array<fftw_complex> & a, Array<double> const & b)
922 {
923 const int n = a.capacity();
924 UTIL_CHECK(n > 0);
925 UTIL_CHECK(b.capacity() == n);
926 for (int i = 0; i < n; ++i) {
927 a[i][0] += b[i];
928 }
929 }
930
931 /*
932 * Vector-scalar in-place addition, a[i] += b (complex).
933 */
934 void addEqS(Array<fftw_complex>& a, fftw_complex b)
935 {
936 const int n = a.capacity();
937 UTIL_CHECK(n > 0);
938 for (int i = 0; i < n; ++i) {
939 a[i][0] += b[0];
940 a[i][1] += b[1];
941 }
942 }
943
944 /*
945 * Vector-scalar in-place addition, a[i] += b (mixed, b real).
946 */
947 void addEqS(Array<fftw_complex>& a, double b)
948 {
949 const int n = a.capacity();
950 UTIL_CHECK(n > 0);
951 for (int i = 0; i < n; ++i) {
952 a[i][0] += b;
953 }
954 }
955
956 // In-place subtraction
957
958 /*
959 * Vector in-place subtraction, a[i] -= b[i] (complex).
960 */
962 {
963 const int n = a.capacity();
964 UTIL_CHECK(n > 0);
965 UTIL_CHECK(b.capacity() == n);
966 for (int i = 0; i < n; ++i) {
967 a[i][0] -= b[i][0];
968 a[i][1] -= b[i][1];
969 }
970 }
971
972 /*
973 * Vector in-place subtraction, a[i] -= b[i] (mixed).
974 */
976 {
977 const int n = a.capacity();
978 UTIL_CHECK(n > 0);
979 UTIL_CHECK(b.capacity() == n);
980 for (int i = 0; i < n; ++i) {
981 a[i][0] -= b[i];
982 }
983 }
984
985 /*
986 * Vector-scalar in-place subtraction, a[i] -= b (complex).
987 */
988 void subEqS(Array<fftw_complex>& a, fftw_complex b)
989 {
990 const int n = a.capacity();
991 UTIL_CHECK(n > 0);
992 for (int i = 0; i < n; ++i) {
993 a[i][0] -= b[0];
994 a[i][1] -= b[1];
995 }
996 }
997
998 /*
999 * Vector-scalar in-place subtraction, a[i][0] -= b (mixed).
1000 */
1001 void subEqS(Array<fftw_complex>& a, double b)
1002 {
1003 const int n = a.capacity();
1004 UTIL_CHECK(n > 0);
1005 for (int i = 0; i < n; ++i) {
1006 a[i][0] -= b;
1007 }
1008 }
1009
1010 // In-place multiplication
1011
1012 /*
1013 * Vector in-place multiplication, a[i] *= b[i] (complex).
1014 */
1016 {
1017 const int n = a.capacity();
1018 UTIL_CHECK(n > 0);
1019 UTIL_CHECK(b.capacity() == n);
1020 double x, y;
1021 for (int i = 0; i < n; ++i) {
1022 x = a[i][0] * b[i][0] - a[i][1] * b[i][1];
1023 y = a[i][0] * b[i][1] + a[i][1] * b[i][0];
1024 a[i][0] = x;
1025 a[i][1] = y;
1026 }
1027 }
1028
1029 /*
1030 * Vector in-place multiplication, a[i] *= b[i] (mixed, b real).
1031 */
1033 {
1034 const int n = a.capacity();
1035 UTIL_CHECK(n > 0);
1036 UTIL_CHECK(b.capacity() == n);
1037 for (int i = 0; i < n; ++i) {
1038 a[i][0] *= b[i];
1039 a[i][1] *= b[i];
1040 }
1041 }
1042
1043 /*
1044 * Vector-scalar in-place multiplication, a[i] *= b[i] (complex).
1045 */
1046 void mulEqS(Array<fftw_complex>& a, fftw_complex const & b)
1047 {
1048 const int n = a.capacity();
1049 UTIL_CHECK(n > 0);
1050 double x, y;
1051 for (int i = 0; i < n; ++i) {
1052 x = a[i][0] * b[0] - a[i][1] * b[1];
1053 y = a[i][0] * b[1] + a[i][1] * b[0];
1054 a[i][0] = x;
1055 a[i][1] = y;
1056 }
1057 }
1058
1059 /*
1060 * Vector-scalar in-place multiplication, a[i] *= b (mixed, b real)
1061 */
1062 void mulEqS(Array<fftw_complex>& a, double b)
1063 {
1064 const int n = a.capacity();
1065 UTIL_CHECK(n > 0);
1066 for (int i = 0; i < n; ++i) {
1067 a[i][0] *= b;
1068 a[i][1] *= b;
1069 }
1070 }
1071
1072 // In-place division
1073
1074 /*
1075 * Vector in-place division, a[i] /= b[i] (complex).
1076 */
1078 {
1079 const int n = a.capacity();
1080 UTIL_CHECK(n > 0);
1081 UTIL_CHECK(b.capacity() == n);
1082 double br, bi, bSq, x, y;
1083 for (int i = 0; i < n; ++i) {
1084 br = b[i][0];
1085 bi = b[i][1];
1086 bSq = br*br + bi*bi;
1087 x = (a[i][0] * br + a[i][1] * bi)/bSq;
1088 y = (a[i][1] * br - a[i][0] * bi)/bSq;
1089 a[i][0] = x;
1090 a[i][1] = y;
1091 }
1092 }
1093
1094 /*
1095 * Vector in-place division, a[i] /= b[i] (mixed, b real).
1096 */
1098 {
1099 const int n = a.capacity();
1100 UTIL_CHECK(n > 0);
1101 UTIL_CHECK(b.capacity() == n);
1102 for (int i = 0; i < n; ++i) {
1103 a[i][0] /= b[i];
1104 a[i][1] /= b[i];
1105 }
1106 }
1107
1108 /*
1109 * Vector-scalar in-place division, a[i] /= b (complex).
1110 */
1111 void divEqS(Array<fftw_complex>& a, fftw_complex b)
1112 {
1113 const int n = a.capacity();
1114 UTIL_CHECK(n > 0);
1115 double br = b[0];
1116 double bi = b[1];
1117 double bSq = br*br + bi*bi;
1118 double x, y;
1119 for (int i = 0; i < n; ++i) {
1120 x = (a[i][0] * br + a[i][1] * bi) / bSq;
1121 y = (a[i][1] * br - a[i][0] * bi) / bSq;
1122 a[i][0] = x;
1123 a[i][1] = y;
1124 }
1125 }
1126
1127 /*
1128 * Vector-scalar in-place division, a[i] /= b (mixed, b real).
1129 */
1130 void divEqS(Array<fftw_complex>& a, double b)
1131 {
1132 const int n = a.capacity();
1133 UTIL_CHECK(n > 0);
1134 for (int i = 0; i < n; ++i) {
1135 a[i][0] /= b;
1136 a[i][1] /= b;
1137 }
1138 }
1139
1140 // Exponentiation
1141
1142 /*
1143 * Vector exponentiation, a[i] = exp(b[i]) (complex).
1144 */
1146 {
1147 const int n = a.capacity();
1148 UTIL_CHECK(n > 0);
1149 UTIL_CHECK(b.capacity() == n);
1150 double rex;
1151 for (int i = 0; i < n; ++i) {
1152 rex = std::exp(b[i][0]);
1153 a[i][0] = rex * cos(b[i][1]);
1154 a[i][1] = rex * sin(b[i][1]);
1155 }
1156 }
1157
1158 // Complex square
1159
1160 /*
1161 * Elementwise complex square, a[i] = b[i] * b[i] (complex).
1162 */
1164 {
1165 const int n = a.capacity();
1166 UTIL_CHECK(n > 0);
1167 UTIL_CHECK(b.capacity() == n);
1168 double br, bi;
1169 for (int i = 0; i < n; ++i) {
1170 br = b[i][0];
1171 bi = b[i][1];
1172 a[i][0] = br * br - bi * bi;
1173 a[i][1] = 2.0 * br * bi;
1174 }
1175 }
1176
1177 // Even powers of complex absolute magnitude
1178
1179 /*
1180 * Square absolute magnitude, a[i] = |b[i]|^2 (complex).
1181 */
1183 {
1184 const int n = a.capacity();
1185 UTIL_CHECK(n > 0);
1186 UTIL_CHECK(b.capacity() == n);
1187 double br, bi;
1188 for (int i = 0; i < n; ++i) {
1189 br = b[i][0];
1190 bi = b[i][1];
1191 a[i] = br * br + bi * bi;
1192 }
1193 }
1194
1195 /*
1196 * Fourth power of absolute magnitude, a[i] = |b[i]|^4 (complex).
1197 */
1199 {
1200 const int n = a.capacity();
1201 UTIL_CHECK(n > 0);
1202 UTIL_CHECK(b.capacity() == n);
1203 double br, bi, sq;
1204 for (int i = 0; i < n; ++i) {
1205 br = b[i][0];
1206 bi = b[i][1];
1207 sq = br * br + bi * bi;
1208 a[i] = sq*sq;
1209 }
1210 }
1211
1212 // Combined operations
1213
1214 void divEqVc(Array<fftw_complex>& a, Array<double> const & b, double c)
1215 {
1216 const int n = a.capacity();
1217 UTIL_CHECK(n > 0);
1218 UTIL_CHECK(b.capacity() == n);
1219 for (int i = 0; i < n; ++i) {
1220 a[i][0] /= (b[i]*c);
1221 a[i][1] /= (b[i]*c);
1222 }
1223 }
1224
1225} // namespace VecOp
1226} // namespace Pscf
Array container class template.
Definition Array.h:41
int capacity() const
Return allocated size.
Definition Array.h:250
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void divEqS(Array< double > &a, double b)
Vector-scalar in-place division, a[i] /= b.
Definition VecOp.cpp:297
void mulVVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b1, Array< double > const &b2, Array< double > const &c)
Vector multiplication in pairs, ax[i] = bx[i] * s[i], x=1,2.
Definition VecOp.cpp:468
void addEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place addition, a[i] += b[i] (real).
Definition VecOp.cpp:203
void divEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place division, a[i] /= b[i].
Definition VecOp.cpp:284
void addEqS(Array< double > &a, double b)
Vector-scalar in-place addition, a[i] += b (real).
Definition VecOp.cpp:216
void sqV(Array< double > &a, Array< double > const &b)
Vector element-wise square, a[i] = b[i]*b[i] (real).
Definition VecOp.cpp:339
void mulEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place multiplication, a[i] *= b[i] (real).
Definition VecOp.cpp:257
void eqV(Array< double > &a, Array< double > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i] (real, slice).
Definition VecOp.cpp:26
void sqAbsV(Array< double > &a, Array< fftw_complex > const &b)
Square of absolute magnitude, a[i] = |b[i]|^2 (complex).
Definition VecOp.cpp:1182
void imag(Array< double > &a, Array< fftw_complex > const &b)
Copy imaginary part of a complex array to a real array.
Definition VecOp.cpp:521
void eqVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b)
Vector assignment in pairs, ax[i] = b[i], x = 1, 2.
Definition VecOp.cpp:451
void sqSqAbsV(Array< double > &a, Array< fftw_complex > const &b)
Fourth power of absolute magnitude, a[i] = |b[i]|^4 (complex).
Definition VecOp.cpp:1198
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
Definition VecOp.cpp:270
void subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector subtraction, a[i] = b[i] - c[i] (real).
Definition VecOp.cpp:100
void absV(Array< double > &a, Array< double > const &b)
Element-wise absolute magnitude, a[i] = abs(b[i]) (real).
Definition VecOp.cpp:354
void expV(Array< double > &a, Array< double > const &b)
Vector exponentiation, a[i] = exp(b[i]) (real).
Definition VecOp.cpp:311
void divVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar division, a[i] = b[i] / c (real).
Definition VecOp.cpp:175
void expVc(Array< double > &a, Array< double > const &b, const double c)
Exponentiation a scaled vector, a[i] = exp(b[i]*c) (real).
Definition VecOp.cpp:324
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
void mulVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector multiplication, a[i] = b[i] * c[i] (real).
Definition VecOp.cpp:130
void mulEqVPair(Array< double > &a1, Array< double > &a2, Array< double > const &b)
In-place vector multiplication in pairs, ax[i] *= b[i], x=1,2.
Definition VecOp.cpp:489
void subVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar subtraction, a[i] = b[i] - c (real).
Definition VecOp.cpp:115
void subEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place subtraction, a[i] -= b[i] (real).
Definition VecOp.cpp:230
void addVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector addition, a[i] = b[i] + c[i] (real).
Definition VecOp.cpp:69
void real(Array< double > &a, Array< fftw_complex > const &b)
Copy real part of a complex array to a real array.
Definition VecOp.cpp:508
void divSV(Array< double > &a, double b, Array< double > const &c)
Vector division, a[i] = b / c[i].
Definition VecOp.cpp:188
void addVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar addition, a[i] = b[i] + c (real).
Definition VecOp.cpp:84
void divEqVc(Array< fftw_complex > &a, Array< double > const &b, double c)
Vector division in-place w/ coeff., a[i] /= (b[i] * c).
Definition VecOp.cpp:1214
void divVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector division, a[i] = b[i] / c[i] (real).
Definition VecOp.cpp:160
void subEqS(Array< double > &a, double b)
Vector-scalar subtraction in-place, a[i] -= b (real).
Definition VecOp.cpp:243
void addEqVc(Array< double > &a, Array< double > const &b, const double c)
Add scaled vector in-place, a[i] += b[i]*c (real).
Definition VecOp.cpp:400
void mulVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar multiplication, a[i] = b[i] * c (real).
Definition VecOp.cpp:145
Elementwise vector operations.
Definition VecOp.cpp:15
void addVcVcS(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2, const double s)
Add scaled vectors + scalar, a[i] = b1[i]*c1 + b2[2]*c2 + s (real).
Definition VecOp.cpp:414
void addVcS(Array< double > &a, Array< double > const &b, const double c, const double s)
Add a scaled vector and a scalar, a[i] = b[i]*c + s (real).
Definition VecOp.cpp:385
void addVcVc(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2)
Add two scaled vectors, a[i] = b1[i]*c1 + b2[2]*c2 (real).
Definition VecOp.cpp:369
void addVcVcVc(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2, Array< double > const &b3, const double c3)
Add scaled vectors, a[i] = b1[i]*c1 + b2[i]*c2 + b3[i]*c3 (real).
Definition VecOp.cpp:431
PSCF package top-level namespace.
Utility classes for scientific computation.