SDDSlib
Loading...
Searching...
No Matches
findMinMax.c
Go to the documentation of this file.
1/**
2 * @file findMinMax.c
3 * @brief Provides functions to find minimum and maximum values in arrays.
4 *
5 * This file contains functions to find the minimum and maximum values in one-dimensional and two-dimensional arrays,
6 * as well as functions to find the indices of these values and perform assignments based on comparisons.
7 *
8 * @copyright
9 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
10 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
11 *
12 * @license
13 * This file is distributed under the terms of the Software License Agreement
14 * found in the file LICENSE included with this distribution.
15 *
16 * @author M. Borland, C. Saunders, R. Soliday
17 */
18
19#include "mdb.h"
20
21/**
22 * @brief Finds the minimum and maximum values in a list of doubles.
23 *
24 * This function iterates through the provided list of doubles to determine the minimum and maximum values.
25 * The results are stored in the provided pointers if they are not NULL.
26 *
27 * @param min Pointer to store the minimum value found. Can be NULL if not needed.
28 * @param max Pointer to store the maximum value found. Can be NULL if not needed.
29 * @param list Pointer to the array of doubles to search.
30 * @param n Number of elements in the list.
31 * @return Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).
32 */
34 double *min, double *max, double *list,
35 int64_t n) {
36 register int64_t i;
37 register double lo, hi, val;
38
39 if (!n || !list)
40 return (0);
41 if (!min && !max)
42 return 0;
43
44 lo = DBL_MAX;
45 hi = -DBL_MAX;
46 for (i = 0; i < n; i++) {
47 if ((val = list[i]) < lo)
48 lo = val;
49 if (val > hi)
50 hi = val;
51 }
52 if (min)
53 *min = lo;
54 if (max)
55 *max = hi;
56 return (1);
57}
58
59/**
60 * @brief Updates the minimum and maximum values based on a list of doubles.
61 *
62 * This function iterates through the provided list of doubles to update the minimum and maximum values.
63 * If the 'reset' flag is non-zero, the function resets the current min and max before processing the list.
64 *
65 * @param min Pointer to the current minimum value. If 'reset' is non-zero, this will be set to the new minimum.
66 * @param max Pointer to the current maximum value. If 'reset' is non-zero, this will be set to the new maximum.
67 * @param list Pointer to the array of doubles to process.
68 * @param n Number of elements in the list.
69 * @param reset Flag indicating whether to reset the current min and max before updating. Non-zero to reset.
70 * @return Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).
71 */
73 double *min, double *max, double *list,
74 int64_t n, int32_t reset) {
75 register int64_t i;
76 register double lo, hi, val;
77
78 if (!n || !list)
79 return (0);
80 if (!min && !max)
81 return 0;
82
83 if (reset) {
84 lo = DBL_MAX;
85 hi = -DBL_MAX;
86 } else {
87 lo = *min;
88 hi = *max;
89 }
90
91 for (i = 0; i < n; i++) {
92 if ((val = list[i]) < lo)
93 lo = val;
94 if (val > hi)
95 hi = val;
96 }
97 if (min)
98 *min = lo;
99 if (max)
100 *max = hi;
101 return (1);
102}
103
104/**
105 * @brief Finds the indices of the minimum and maximum values in a list of doubles.
106 *
107 * This function iterates through the provided list of doubles to determine the indices of the minimum and maximum values.
108 * The indices are stored in the provided pointers if they are not NULL.
109 *
110 * @param imin Pointer to store the index of the minimum value. Can be NULL if not needed.
111 * @param imax Pointer to store the index of the maximum value. Can be NULL if not needed.
112 * @param list Pointer to the array of doubles to search.
113 * @param n Number of elements in the list.
114 * @return Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).
115 */
117 int64_t *imin, int64_t *imax, double *list, int64_t n) {
118 register int64_t i;
119 register double lo, hi, val;
120 int64_t iMin, iMax;
121
122 if (!n || !list)
123 return (0);
124 if (!imin && !imax)
125 return 0;
126
127 lo = DBL_MAX;
128 hi = -DBL_MAX;
129 iMin = iMax = 0;
130 for (i = 0; i < n; i++) {
131 if ((val = list[i]) < lo) {
132 iMin = i;
133 lo = val;
134 }
135 if (val > hi) {
136 hi = val;
137 iMax = i;
138 }
139 }
140 if (imin)
141 *imin = iMin;
142 if (imax)
143 *imax = iMax;
144
145 return (1);
146}
147
148/**
149 * @brief Finds the indices of the minimum and maximum values in a list of longs.
150 *
151 * This function iterates through the provided list of longs to determine the indices of the minimum and maximum values.
152 * The indices are stored in the provided pointers if they are not NULL.
153 *
154 * @param imin Pointer to store the index of the minimum value. Can be NULL if not needed.
155 * @param imax Pointer to store the index of the maximum value. Can be NULL if not needed.
156 * @param list Pointer to the array of longs to search.
157 * @param n Number of elements in the list.
158 * @return Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).
159 */
161 int64_t *imin, int64_t *imax, long *list, int64_t n) {
162 register int64_t i;
163 register long lo, hi, val;
164 int64_t iMin, iMax;
165
166 if (!n || !list)
167 return (0);
168 if (!imin && !imax)
169 return 0;
170
171 lo = LONG_MAX;
172 hi = -LONG_MAX;
173 iMin = iMax = 0;
174 for (i = 0; i < n; i++) {
175 if ((val = list[i]) < lo) {
176 iMin = i;
177 lo = val;
178 }
179 if (val > hi) {
180 hi = val;
181 iMax = i;
182 }
183 }
184 if (imin)
185 *imin = iMin;
186 if (imax)
187 *imax = iMax;
188
189 return (1);
190}
191
192/* routine: assign_min_max()
193 * purpose: compare a value to running minimum and maximum values,
194 * and assign these values accordingly.
195 */
196
197int assign_min_max(double *min, double *max, double val) {
198 int flag = 0;
199
200 if (!min || !max)
201 return (0);
202 flag |= 1;
203 if (*min > val) {
204 *min = val;
205 flag |= 2;
206 }
207 if (*max < val) {
208 *max = val;
209 flag |= 4;
210 }
211 return (flag);
212}
213
214/* routine: find_min_max_2d()
215 * purpose: find minimum and maximum values in a 2-d array (array
216 * of pointers).
217 */
218
219int find_min_max_2d(double *min, double *max, double **value,
220 long n1, long n2) {
221 double data, rmin, rmax, *value_i1;
222 long i1, i2;
223
224 if (!n1 || !n2 || !min || !max || !value)
225 return (0);
226
227 rmin = DBL_MAX;
228 rmax = -DBL_MAX;
229 for (i1 = 0; i1 < n1; i1++) {
230 if (!(value_i1 = value[i1]))
231 return (0);
232 for (i2 = 0; i2 < n2; i2++) {
233 if ((data = value_i1[i2]) > rmax)
234 rmax = data;
235 if (data < rmin)
236 rmin = data;
237 }
238 }
239 return (1);
240}
241
242/* routine: find_min_max_2d_float()
243 * purpose: find minimum and maximum values in a 2-d array (array
244 * of pointers) of floats.
245 */
246
247int find_min_max_2d_float(float *min, float *max, float **value,
248 long n1, long n2) {
249 float data, rmin, rmax, *value_i1;
250 long i1, i2;
251
252 if (!n1 || !n2 || !min || !max || !value)
253 return (0);
254
255 rmin = FLT_MAX;
256 rmax = -FLT_MAX;
257 for (i1 = 0; i1 < n1; i1++) {
258 if (!(value_i1 = value[i1]))
259 return (0);
260 for (i2 = 0; i2 < n2; i2++) {
261 if ((data = value_i1[i2]) > rmax)
262 rmax = data;
263 if (data < rmin)
264 rmin = data;
265 }
266 }
267 *min = rmin;
268 *max = rmax;
269 return (1);
270}
271
272int find_min(
273 double *min, double *loc, double *c1, double *c2,
274 long n) {
275 long i;
276 double val;
277
278 if (!n || !loc || !c1 || !c2)
279 return (0);
280
281 *min = DBL_MAX;
282 for (i = 0; i < n; i++) {
283 if ((val = c2[i]) < *min) {
284 *min = val;
285 *loc = c1[i];
286 }
287 }
288 return (1);
289}
290
291int find_max(
292 double *max, double *loc, double *c1, double *c2,
293 long n) {
294 long i;
295 double val;
296
297 if (!n || !c1 || !c2 || !loc || !max)
298 return (0);
299 *max = -DBL_MAX;
300 for (i = 0; i < n; i++) {
301 if ((val = c2[i]) > *max) {
302 *max = val;
303 *loc = c1[i];
304 }
305 }
306 return (1);
307}
308
309/**
310 * @brief Finds the maximum value in an array of doubles.
311 *
312 * This function iterates through the provided array of doubles to determine the maximum value.
313 *
314 * @param array Pointer to the array of doubles to search.
315 * @param n Number of elements in the array.
316 * @return Returns the maximum value found in the array. If the array is empty, returns -DBL_MAX.
317 */
318double max_in_array(double *array, long n) {
319 double max = -DBL_MAX;
320
321 while (n--)
322 if (array[n] > max)
323 max = array[n];
324 return (max);
325}
326
327/**
328 * @brief Finds the minimum value in an array of doubles.
329 *
330 * This function iterates through the provided array of doubles to determine the minimum value.
331 *
332 * @param array Pointer to the array of doubles to search.
333 * @param n Number of elements in the array.
334 * @return Returns the minimum value found in the array. If the array is empty, returns DBL_MAX.
335 */
336double min_in_array(double *array, long n) {
337 double min = DBL_MAX;
338
339 while (n--)
340 if (array[n] < min)
341 min = array[n];
342 return (min);
343}
int index_min_max(int64_t *imin, int64_t *imax, double *list, int64_t n)
Finds the indices of the minimum and maximum values in a list of doubles.
Definition findMinMax.c:116
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
Definition findMinMax.c:33
int update_min_max(double *min, double *max, double *list, int64_t n, int32_t reset)
Updates the minimum and maximum values based on a list of doubles.
Definition findMinMax.c:72
int index_min_max_long(int64_t *imin, int64_t *imax, long *list, int64_t n)
Finds the indices of the minimum and maximum values in a list of longs.
Definition findMinMax.c:160
double max_in_array(double *array, long n)
Finds the maximum value in an array of doubles.
Definition findMinMax.c:318
double min_in_array(double *array, long n)
Finds the minimum value in an array of doubles.
Definition findMinMax.c:336