gsl_statistics__minmax_source.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. BASE
  20. FUNCTION (gsl_stats,max) (const BASE data[], const size_t stride,
  21. const size_t n)
  22. {
  23. /* finds the largest member of a dataset */
  24. BASE max = data[0 * stride];
  25. size_t i;
  26. for (i = 0; i < n; i++)
  27. {
  28. BASE xi = data[i * stride];
  29. if (xi > max)
  30. max = xi;
  31. #ifdef FP
  32. if (isnan (xi))
  33. return xi;
  34. #endif
  35. }
  36. return max;
  37. }
  38. BASE
  39. FUNCTION (gsl_stats,min) (const BASE data[], const size_t stride,
  40. const size_t n)
  41. {
  42. /* finds the smallest member of a dataset */
  43. BASE min = data[0 * stride];
  44. size_t i;
  45. for (i = 0; i < n; i++)
  46. {
  47. BASE xi = data[i * stride];
  48. if (xi < min)
  49. min = xi;
  50. #ifdef FP
  51. if (isnan (xi))
  52. return xi;
  53. #endif
  54. }
  55. return min;
  56. }
  57. void
  58. FUNCTION (gsl_stats,minmax) (BASE * min_out, BASE * max_out,
  59. const BASE data[], const size_t stride,
  60. const size_t n)
  61. {
  62. /* finds the smallest and largest members of a dataset */
  63. BASE min = data[0 * stride];
  64. BASE max = data[0 * stride];
  65. size_t i;
  66. for (i = 0; i < n; i++)
  67. {
  68. BASE xi = data[i * stride];
  69. if (xi < min)
  70. min = xi;
  71. if (xi > max)
  72. max = xi;
  73. #ifdef FP
  74. if (isnan (xi))
  75. {
  76. min = xi;
  77. max = xi;
  78. break;
  79. }
  80. #endif
  81. }
  82. *min_out = min;
  83. *max_out = max;
  84. }
  85. size_t
  86. FUNCTION (gsl_stats,max_index) (const BASE data[], const size_t stride,
  87. const size_t n)
  88. {
  89. /* finds the index of the largest member of a dataset */
  90. /* if there is more than one largest value then we choose the first */
  91. BASE max = data[0 * stride];
  92. size_t i, max_index = 0;
  93. for (i = 0; i < n; i++)
  94. {
  95. BASE xi = data[i * stride];
  96. if (xi > max)
  97. {
  98. max = xi;
  99. max_index = i;
  100. }
  101. #ifdef FP
  102. if (isnan (xi))
  103. {
  104. return i;
  105. }
  106. #endif
  107. }
  108. return max_index;
  109. }
  110. size_t
  111. FUNCTION (gsl_stats,min_index) (const BASE data[], const size_t stride,
  112. const size_t n)
  113. {
  114. /* finds the index of the smallest member of a dataset */
  115. /* if there is more than one largest value then we choose the first */
  116. BASE min = data[0 * stride];
  117. size_t i, min_index = 0;
  118. for (i = 0; i < n; i++)
  119. {
  120. BASE xi = data[i * stride];
  121. if (xi < min)
  122. {
  123. min = xi;
  124. min_index = i;
  125. }
  126. #ifdef FP
  127. if (isnan (xi))
  128. {
  129. return i;
  130. }
  131. #endif
  132. }
  133. return min_index;
  134. }
  135. void
  136. FUNCTION (gsl_stats,minmax_index) (size_t * min_index_out,
  137. size_t * max_index_out, const BASE data[],
  138. const size_t stride, const size_t n)
  139. {
  140. /* finds the smallest and largest members of a dataset */
  141. BASE min = data[0 * stride];
  142. BASE max = data[0 * stride];
  143. size_t i, min_index = 0, max_index = 0;
  144. for (i = 0; i < n; i++)
  145. {
  146. BASE xi = data[i * stride];
  147. if (xi < min)
  148. {
  149. min = xi;
  150. min_index = i;
  151. }
  152. if (xi > max)
  153. {
  154. max = xi;
  155. max_index = i;
  156. }
  157. #ifdef FP
  158. if (isnan (xi))
  159. {
  160. min_index = i;
  161. max_index = i;
  162. break;
  163. }
  164. #endif
  165. }
  166. *min_index_out = min_index;
  167. *max_index_out = max_index;
  168. }