PSCF v1.4.0
Reduce.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 "Reduce.h"
9#include <util/containers/Array.h>
10#include <cmath>
11
12namespace Pscf {
13namespace Reduce {
14
15 // Summation
16
17 /*
18 * Compute the sum of array elements (real).
19 */
20 double sum(Array<double> const & in)
21 {
22 int n = in.capacity();
23 UTIL_CHECK(n > 0);
24
25 // Kahan summation - to reduce accumulation of error
26 double sum = 0.0;
27 double err = 0.0;
28 double tempVal, tempSum;
29 for (int i = 0; i < n; ++i) {
30 tempVal = in[i] - err;
31 tempSum = sum + tempVal;
32 err = tempSum - sum - tempVal;
33 sum = tempSum;
34 }
35
36 #if 0
37 // Simple summation
38 double sum = 0.0;
39 for (int i = 0; i < n; i++) {
40 sum += in[i];
41 }
42 #endif
43
44 return sum;
45 }
46
47 /*
48 * Compute the sum of array elements (real).
49 */
50 double sum(Array<double> const & in, int begin, int end)
51 {
52 int n = in.capacity();
53 UTIL_CHECK(n > 0);
54 UTIL_CHECK(begin >= 0);
55 UTIL_CHECK(end <= n);
56
57 // Kahan summation - to reduce accumulation of error
58 double sum = 0.0;
59 double err = 0.0;
60 double tempVal, tempSum;
61 for (int i = begin; i < end; ++i) {
62 tempVal = in[i] - err;
63 tempSum = sum + tempVal;
64 err = tempSum - sum - tempVal;
65 sum = tempSum;
66 }
67
68 #if 0
69 // Simple summation
70 double sum = 0.0;
71 for (int i = begin; i < end; i++) {
72 sum += in[i];
73 }
74 #endif
75
76 return sum;
77 }
78
79 /*
80 * Compute the sum of squares of all array elements (real).
81 */
82 double sumSq(Array<double> const & in)
83 {
84 int n = in.capacity();
85 UTIL_CHECK(n > 0);
86
87 // Kahan summation - to reduce accumulation of error
88 double sum = 0.0;
89 double err = 0.0;
90 double x, tempVal, tempSum;
91 for (int i = 0; i < n; ++i) {
92 x = in[i];
93 tempVal = x*x - err;
94 tempSum = sum + tempVal;
95 err = tempSum - sum - tempVal;
96 sum = tempSum;
97 }
98
99 #if 0
100 // Simple summation
101 double val;
102 double sum = 0.0;
103 for (int i = 0; i < n; i++) {
104 val = in[i];
105 sum += val*val;
106 }
107 #endif
108
109 return sum;
110 }
111
112 // Inner product
113
114 /*
115 * Compute Euclidean inner product of two arrays (real).
116 */
117 double innerProduct(Array<double> const & a,
118 Array<double> const & b)
119 {
120 int n = a.capacity();
121 UTIL_CHECK(n > 0);
122 UTIL_CHECK(b.capacity() == n);
123
124 // Kahan summation - to reduce accumulation of error
125 double sum = 0.0;
126 double err = 0.0;
127 double tempVal, tempSum;
128 for (int i = 0; i < n; ++i) {
129 tempVal = a[i]*b[i] - err;
130 tempSum = sum + tempVal;
131 err = tempSum - sum - tempVal;
132 sum = tempSum;
133 }
134
135 #if 0
136 // Simple summation
137 double sum = 0.0;
138 for (int i = 0; i < n; i++) {
139 sum += a[i]*b[i];
140 }
141 #endif
142
143 return sum;
144 }
145
146 // Maxima and minima
147
148 /*
149 * Get maximum of array elements.
150 */
151 double max(Array<double> const & in)
152 {
153 int n = in.capacity();
154 UTIL_CHECK(n > 0);
155 double max = in[0];
156 for (int i = 1; i < n; i++) {
157 if (in[i] > max) max = in[i];
158 }
159 return max;
160 }
161
162 /*
163 * Get maximum element of array slice.
164 */
165 double max(Array<double> const & in, int begin, int end)
166 {
167 int n = in.capacity();
168 UTIL_CHECK(n > 0);
169 UTIL_CHECK(begin >= 0);
170 UTIL_CHECK(end <= n);
171 UTIL_CHECK(end > begin);
172 double max = in[begin];
173 for (int i = begin + 1; i < end; i++) {
174 if (in[i] > max) max = in[i];
175 }
176 return max;
177 }
178
179 /*
180 * Get maximum absolute magnitude of array elements.
181 */
182 double maxAbs(Array<double> const & in)
183 {
184 int n = in.capacity();
185 UTIL_CHECK(n > 0);
186 double val;
187 double max = std::abs(in[0]);
188 for (int i = 1; i < n; i++) {
189 val = std::abs(in[i]);
190 if (val > max) max = val;
191 }
192 return max;
193 }
194
195 /*
196 * Get minimum of array elements.
197 */
198 double min(Array<double> const & in)
199 {
200 int n = in.capacity();
201 UTIL_CHECK(n > 0);
202 double min = in[0];
203 for (int i = 1; i < n; i++) {
204 if (in[i] < min) min = in[i];
205 }
206 return min;
207 }
208
209 /*
210 * Get value of minimum element in an array slice.
211 */
212 double min(Array<double> const & in, int begin, int end)
213 {
214 int n = in.capacity();
215 UTIL_CHECK(n > 0);
216 UTIL_CHECK(begin >= 0);
217 UTIL_CHECK(end <= n);
218 UTIL_CHECK(end > begin);
219 double min = in[begin];
220 for (int i = begin + 1; i < end; i++) {
221 if (in[i] < min) min = in[i];
222 }
223 return min;
224 }
225
226 /*
227 * Get minimum absolute magnitude of array elements.
228 */
229 double minAbs(Array<double> const & in)
230 {
231 int n = in.capacity();
232 UTIL_CHECK(n > 0);
233 double val;
234 double min = std::abs(in[0]);
235 for (int i = 1; i < n; i++) {
236 val = std::abs(in[i]);
237 if (val < min) min = val;
238 }
239 return min;
240 }
241
242} // Reduce
243} // 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
double minAbs(Array< double > const &in)
Get minimum absolute magnitude of array elements .
Definition Reduce.cpp:229
double min(Array< double > const &in)
Get minimum of array elements .
Definition Reduce.cpp:198
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute Euclidean inner product of two real arrays .
Definition Reduce.cpp:117
double maxAbs(Array< double > const &in)
Get maximum absolute magnitude of array elements .
Definition Reduce.cpp:182
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
double max(Array< double > const &in)
Get maximum of array elements (real).
Definition Reduce.cpp:151
Reduction operations performed on a CPU or GPU.
Definition Reduce.cpp:13
PSCF package top-level namespace.