PSCF v1.4.0
ReduceCx.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "ReduceCx.h"
9#include <util/containers/Array.h>
10
11namespace Pscf {
12namespace Reduce {
13
14 /*
15 * Compute sum of complex array elements.
16 */
17 std::complex<double> sum(Array<fftw_complex> const & a)
18 {
19 int n = a.capacity();
20 UTIL_CHECK(n > 0);
21 fftw_complex sum;
22 sum[0] = 0.0;
23 sum[1] = 0.0;
24 for (int i = 0; i < n; i++) {
25 sum[0] += a[i][0];
26 sum[1] += a[i][1];
27 }
28 return std::complex<double>(sum[0], sum[1]);
29 }
30
31 /*
32 * Compute sum of elements in a complex array slice.
33 */
34 std::complex<double> sum(Array<fftw_complex> const & a,
35 int begin, int end)
36 {
37 int n = a.capacity();
38 UTIL_CHECK(n > 0);
39 UTIL_CHECK(begin >= 0);
40 UTIL_CHECK(end <= n);
41 UTIL_CHECK(end > begin);
42 fftw_complex sum;
43 sum[0] = 0.0;
44 sum[1] = 0.0;
45 for (int i = begin; i < end; i++) {
46 sum[0] += a[i][0];
47 sum[1] += a[i][1];
48 }
49 return std::complex<double>(sum[0], sum[1]);
50 }
51
52 /*
53 * Compute sum of square of elements of a complex array.
54 */
55 std::complex<double> sumSq(Array<fftw_complex> const & a)
56 {
57 int n = a.capacity();
58 UTIL_CHECK(n > 0);
59 fftw_complex sum;
60 double ar, ai;
61 sum[0] = 0.0;
62 sum[1] = 0.0;
63 for (int i = 0; i < n; i++) {
64 ar = a[i][0];
65 ai = a[i][1];
66 sum[0] += ar*ar - ai*ai;
67 sum[1] += 2.0 * ar * ai;
68 }
69 return std::complex<double>(sum[0], sum[1]);
70 }
71
72 /*
73 * Compute sum of element-wise product of two complex arrays.
74 */
75 std::complex<double> sumProduct(Array<fftw_complex> const & a,
76 Array<fftw_complex> const & b)
77 {
78 int n = a.capacity();
79 UTIL_CHECK(n > 0);
80 UTIL_CHECK(b.capacity() == n);
81 fftw_complex sum;
82 sum[0] = 0.0;
83 sum[1] = 0.0;
84 for (int i = 0; i < n; i++) {
85 sum[0] += a[i][0] * b[i][0] - a[i][1] * b[i][1];
86 sum[1] += a[i][0] * b[i][1] + a[i][1] * b[i][0];
87 }
88 return std::complex<double>(sum[0], sum[1]);
89 }
90
91} // Reduce
92} // Pscf
Array container class template.
Definition Array.h:40
int capacity() const
Return allocated size.
Definition Array.h:144
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
std::complex< double > sumProduct(Array< fftw_complex > const &a, Array< fftw_complex > const &b)
Compute sum of complex products of elements of two arrays (complex).
Definition ReduceCx.cpp:75
double sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
Definition Reduce.cpp:82
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:20
Reduction operations performed on a CPU or GPU.
Definition Reduce.cpp:13
PSCF package top-level namespace.