1 /* statistics/minmax_source.c
2  *
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Jim Davies, Brian Gough
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 
21 BASE
FUNCTION(gsl_stats,max)22 FUNCTION (gsl_stats,max) (const BASE data[], const size_t stride,
23                           const size_t n)
24 {
25   /* finds the largest member of a dataset */
26 
27   BASE max = data[0 * stride];
28   size_t i;
29 
30   for (i = 0; i < n; i++)
31     {
32       BASE xi = data[i * stride];
33 
34       if (xi > max)
35         max = xi;
36 #ifdef FP
37       if (isnan (xi))
38         return xi;
39 #endif
40     }
41 
42   return max;
43 }
44 
45 BASE
FUNCTION(gsl_stats,min)46 FUNCTION (gsl_stats,min) (const BASE data[], const size_t stride,
47                           const size_t n)
48 {
49   /* finds the smallest member of a dataset */
50 
51   BASE min = data[0 * stride];
52   size_t i;
53 
54   for (i = 0; i < n; i++)
55     {
56       BASE xi = data[i * stride];
57 
58       if (xi < min)
59         min = xi;
60 #ifdef FP
61       if (isnan (xi))
62         return xi;
63 #endif
64     }
65 
66   return min;
67 
68 }
69 
70 void
FUNCTION(gsl_stats,minmax)71 FUNCTION (gsl_stats,minmax) (BASE * min_out, BASE * max_out,
72                              const BASE data[], const size_t stride,
73                              const size_t n)
74 {
75   /* finds the smallest and largest members of a dataset */
76 
77   BASE min = data[0 * stride];
78   BASE max = data[0 * stride];
79   size_t i;
80 
81   for (i = 0; i < n; i++)
82     {
83       BASE xi = data[i * stride];
84 
85       if (xi < min)
86         min = xi;
87 
88       if (xi > max)
89         max = xi;
90 
91 #ifdef FP
92       if (isnan (xi))
93         {
94           min = xi;
95           max = xi;
96           break;
97         }
98 #endif
99 
100     }
101 
102   *min_out = min;
103   *max_out = max;
104 }
105 
106 size_t
FUNCTION(gsl_stats,max_index)107 FUNCTION (gsl_stats,max_index) (const BASE data[], const size_t stride,
108                                 const size_t n)
109 {
110   /* finds the index of the largest member of a dataset */
111   /* if there is more than one largest value then we choose the first */
112 
113   BASE max = data[0 * stride];
114   size_t i, max_index = 0;
115 
116   for (i = 0; i < n; i++)
117     {
118       BASE xi = data[i * stride];
119 
120       if (xi > max)
121         {
122           max = xi;
123           max_index = i;
124         }
125 
126 #ifdef FP
127       if (isnan (xi))
128         {
129           return i;
130         }
131 #endif
132     }
133 
134   return max_index;
135 }
136 
137 size_t
FUNCTION(gsl_stats,min_index)138 FUNCTION (gsl_stats,min_index) (const BASE data[], const size_t stride,
139                                 const size_t n)
140 {
141   /* finds the index of the smallest member of a dataset */
142   /* if there is more than one largest value then we choose the first  */
143 
144   BASE min = data[0 * stride];
145   size_t i, min_index = 0;
146 
147   for (i = 0; i < n; i++)
148     {
149       BASE xi = data[i * stride];
150 
151       if (xi < min)
152         {
153           min = xi;
154           min_index = i;
155         }
156 
157 #ifdef FP
158       if (isnan (xi))
159         {
160           return i;
161         }
162 #endif
163     }
164 
165   return min_index;
166 }
167 
168 void
FUNCTION(gsl_stats,minmax_index)169 FUNCTION (gsl_stats,minmax_index) (size_t * min_index_out,
170                                    size_t * max_index_out, const BASE data[],
171                                    const size_t stride, const size_t n)
172 {
173   /* finds the smallest and largest members of a dataset */
174 
175   BASE min = data[0 * stride];
176   BASE max = data[0 * stride];
177   size_t i, min_index = 0, max_index = 0;
178 
179   for (i = 0; i < n; i++)
180     {
181       BASE xi = data[i * stride];
182 
183       if (xi < min)
184         {
185           min = xi;
186           min_index = i;
187         }
188 
189       if (xi > max)
190         {
191           max = xi;
192           max_index = i;
193         }
194 
195 #ifdef FP
196       if (isnan (xi))
197         {
198           min_index = i;
199           max_index = i;
200           break;
201         }
202 #endif
203     }
204 
205   *min_index_out = min_index;
206   *max_index_out = max_index;
207 }
208