PSCF v1.5.0
Reduce.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 "Reduce.h"
9#include <util/containers/Array.h>
10
11namespace Pscf {
12namespace Reduce {
13
14 // Summation
15
16 /*
17 * Compute the sum of array elements (real).
18 */
19 double sum(Array<double> const & in)
20 {
21 int n = in.capacity();
22 UTIL_CHECK(n > 0);
23
24 // Kahan summation - to reduce accumulation of error
25 double sum = 0.0;
26 double err = 0.0;
27 double tempVal, tempSum;
28 for (int i = 0; i < n; ++i) {
29 tempVal = in[i] - err;
30 tempSum = sum + tempVal;
31 err = tempSum - sum - tempVal;
32 sum = tempSum;
33 }
34
35 #if 0
36 // Simple summation
37 double sum = 0.0;
38 for (int i = 0; i < n; i++) {
39 sum += in[i];
40 }
41 #endif
42
43 return sum;
44 }
45
46 /*
47 * Compute the sum of slice elements (real).
48 */
49 double sum(Array<double> const & in, int begin, int end)
50 {
51 int n = in.capacity();
52 UTIL_CHECK(n > 0);
53 UTIL_CHECK(begin >= 0);
54 UTIL_CHECK(end <= n);
55
56 // Kahan summation - to reduce accumulation of error
57 double sum = 0.0;
58 double err = 0.0;
59 double tempVal, tempSum;
60 for (int i = begin; i < end; ++i) {
61 tempVal = in[i] - err;
62 tempSum = sum + tempVal;
63 err = tempSum - sum - tempVal;
64 sum = tempSum;
65 }
66
67 #if 0
68 // Simple summation
69 double sum = 0.0;
70 for (int i = begin; i < end; i++) {
71 sum += in[i];
72 }
73 #endif
74
75 return sum;
76 }
77
78 /*
79 * Compute sum of array elements (complex).
80 */
81 std::complex<double> sum(Array<fftw_complex> const & a)
82 {
83 int n = a.capacity();
84 UTIL_CHECK(n > 0);
85 fftw_complex sum;
86 sum[0] = 0.0;
87 sum[1] = 0.0;
88 for (int i = 0; i < n; i++) {
89 sum[0] += a[i][0];
90 sum[1] += a[i][1];
91 }
92 return std::complex<double>(sum[0], sum[1]);
93 }
94
95 /*
96 * Compute sum of slice elements (complex).
97 */
98 std::complex<double> sum(Array<fftw_complex> const & a,
99 int begin, int end)
100 {
101 int n = a.capacity();
102 UTIL_CHECK(n > 0);
103 UTIL_CHECK(begin >= 0);
104 UTIL_CHECK(end <= n);
105 UTIL_CHECK(end > begin);
106 fftw_complex sum;
107 sum[0] = 0.0;
108 sum[1] = 0.0;
109 for (int i = begin; i < end; i++) {
110 sum[0] += a[i][0];
111 sum[1] += a[i][1];
112 }
113 return std::complex<double>(sum[0], sum[1]);
114 }
115
116 // Sum of Squares
117
118 /*
119 * Compute the sum of squares of all array elements (real).
120 */
121 double sumSq(Array<double> const & in)
122 {
123 int n = in.capacity();
124 UTIL_CHECK(n > 0);
125
126 // Kahan summation - to reduce accumulation of error
127 double sum = 0.0;
128 double err = 0.0;
129 double x, tempVal, tempSum;
130 for (int i = 0; i < n; ++i) {
131 x = in[i];
132 tempVal = x*x - err;
133 tempSum = sum + tempVal;
134 err = tempSum - sum - tempVal;
135 sum = tempSum;
136 }
137
138 #if 0
139 // Simple summation
140 double val;
141 double sum = 0.0;
142 for (int i = 0; i < n; i++) {
143 val = in[i];
144 sum += val*val;
145 }
146 #endif
147
148 return sum;
149 }
150
151 /*
152 * Compute sum of square of elements (complex).
153 */
154 std::complex<double> sumSq(Array<fftw_complex> const & a)
155 {
156 int n = a.capacity();
157 UTIL_CHECK(n > 0);
158 fftw_complex sum;
159 double ar, ai;
160 sum[0] = 0.0;
161 sum[1] = 0.0;
162 for (int i = 0; i < n; i++) {
163 ar = a[i][0];
164 ai = a[i][1];
165 sum[0] += ar*ar - ai*ai;
166 sum[1] += 2.0 * ar * ai;
167 }
168 return std::complex<double>(sum[0], sum[1]);
169 }
170
171 // Inner product
172
173 /*
174 * Compute Euclidean inner product of two arrays (real).
175 */
176 double innerProduct(Array<double> const & a,
177 Array<double> const & b)
178 {
179 int n = a.capacity();
180 UTIL_CHECK(n > 0);
181 UTIL_CHECK(b.capacity() == n);
182
183 // Kahan summation - to reduce accumulation of error
184 double sum = 0.0;
185 double err = 0.0;
186 double tempVal, tempSum;
187 for (int i = 0; i < n; ++i) {
188 tempVal = a[i]*b[i] - err;
189 tempSum = sum + tempVal;
190 err = tempSum - sum - tempVal;
191 sum = tempSum;
192 }
193
194 #if 0
195 // Simple summation
196 double sum = 0.0;
197 for (int i = 0; i < n; i++) {
198 sum += a[i]*b[i];
199 }
200 #endif
201
202 return sum;
203 }
204
205 /*
206 * Compute sum of element-wise product of two complex arrays.
207 */
208 std::complex<double> sumProduct(Array<fftw_complex> const & a,
209 Array<fftw_complex> const & b)
210 {
211 int n = a.capacity();
212 UTIL_CHECK(n > 0);
213 UTIL_CHECK(b.capacity() == n);
214 fftw_complex sum;
215 sum[0] = 0.0;
216 sum[1] = 0.0;
217 for (int i = 0; i < n; i++) {
218 sum[0] += a[i][0] * b[i][0] - a[i][1] * b[i][1];
219 sum[1] += a[i][0] * b[i][1] + a[i][1] * b[i][0];
220 }
221 return std::complex<double>(sum[0], sum[1]);
222 }
223
224 // Maxima and minima
225
226 /*
227 * Get maximum of array elements.
228 */
229 double max(Array<double> const & in)
230 {
231 int n = in.capacity();
232 UTIL_CHECK(n > 0);
233 double max = in[0];
234 for (int i = 1; i < n; i++) {
235 if (in[i] > max) max = in[i];
236 }
237 return max;
238 }
239
240 /*
241 * Get maximum element of array slice.
242 */
243 double max(Array<double> const & in, int begin, int end)
244 {
245 int n = in.capacity();
246 UTIL_CHECK(n > 0);
247 UTIL_CHECK(begin >= 0);
248 UTIL_CHECK(end <= n);
249 UTIL_CHECK(end > begin);
250 double max = in[begin];
251 for (int i = begin + 1; i < end; i++) {
252 if (in[i] > max) max = in[i];
253 }
254 return max;
255 }
256
257 /*
258 * Get maximum absolute magnitude of array elements.
259 */
260 double maxAbs(Array<double> const & in)
261 {
262 int n = in.capacity();
263 UTIL_CHECK(n > 0);
264 double val;
265 double max = std::abs(in[0]);
266 for (int i = 1; i < n; i++) {
267 val = std::abs(in[i]);
268 if (val > max) max = val;
269 }
270 return max;
271 }
272
273 /*
274 * Get minimum of array elements.
275 */
276 double min(Array<double> const & in)
277 {
278 int n = in.capacity();
279 UTIL_CHECK(n > 0);
280 double min = in[0];
281 for (int i = 1; i < n; i++) {
282 if (in[i] < min) min = in[i];
283 }
284 return min;
285 }
286
287 /*
288 * Get value of minimum element in an array slice.
289 */
290 double min(Array<double> const & in, int begin, int end)
291 {
292 int n = in.capacity();
293 UTIL_CHECK(n > 0);
294 UTIL_CHECK(begin >= 0);
295 UTIL_CHECK(end <= n);
296 UTIL_CHECK(end > begin);
297 double min = in[begin];
298 for (int i = begin + 1; i < end; i++) {
299 if (in[i] < min) min = in[i];
300 }
301 return min;
302 }
303
304 /*
305 * Get minimum absolute magnitude of array elements.
306 */
307 double minAbs(Array<double> const & in)
308 {
309 int n = in.capacity();
310 UTIL_CHECK(n > 0);
311 double val;
312 double min = std::abs(in[0]);
313 for (int i = 1; i < n; i++) {
314 val = std::abs(in[i]);
315 if (val < min) min = val;
316 }
317 return min;
318 }
319
320} // Reduce
321} // 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
double minAbs(Array< double > const &in)
Get minimum absolute magnitude of array elements .
Definition Reduce.cpp:307
double min(Array< double > const &in)
Get minimum of array elements .
Definition Reduce.cpp:276
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute Euclidean inner product of two real arrays .
Definition Reduce.cpp:176
double maxAbs(Array< double > const &in)
Get maximum absolute magnitude of array elements .
Definition Reduce.cpp:260
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 Reduce.cpp:208
double sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
Definition Reduce.cpp:121
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
double max(Array< double > const &in)
Get maximum of array elements (real).
Definition Reduce.cpp:229
Functions that perform array reductions.
Definition Reduce.cpp:12
PSCF package top-level namespace.