gsl_vector__minmax_source.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* vector/minmax_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, 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_vector,max) (const TYPE(gsl_vector) * v)
  21. {
  22. /* finds the largest element of a vector */
  23. const size_t N = v->size ;
  24. const size_t stride = v->stride ;
  25. BASE max = v->data[0 * stride];
  26. size_t i;
  27. for (i = 0; i < N; i++)
  28. {
  29. BASE x = v->data[i*stride];
  30. if (x > max)
  31. max = x;
  32. #ifdef FP
  33. if (isnan (x))
  34. return x;
  35. #endif
  36. }
  37. return max;
  38. }
  39. BASE
  40. FUNCTION(gsl_vector,min) (const TYPE(gsl_vector) * v)
  41. {
  42. /* finds the smallest element of a vector */
  43. const size_t N = v->size ;
  44. const size_t stride = v->stride ;
  45. BASE min = v->data[0 * stride];
  46. size_t i;
  47. for (i = 0; i < N; i++)
  48. {
  49. BASE x = v->data[i*stride];
  50. if (x < min)
  51. min = x;
  52. #ifdef FP
  53. if (isnan (x))
  54. return x;
  55. #endif
  56. }
  57. return min;
  58. }
  59. void
  60. FUNCTION(gsl_vector,minmax) (const TYPE(gsl_vector) * v,
  61. BASE * min_out,
  62. BASE * max_out)
  63. {
  64. /* finds the smallest and largest elements of a vector */
  65. const size_t N = v->size ;
  66. const size_t stride = v->stride ;
  67. BASE max = v->data[0 * stride];
  68. BASE min = v->data[0 * stride];
  69. size_t i;
  70. for (i = 0; i < N; i++)
  71. {
  72. BASE x = v->data[i*stride];
  73. if (x < min)
  74. {
  75. min = x;
  76. }
  77. if (x > max)
  78. {
  79. max = x;
  80. }
  81. #ifdef FP
  82. if (isnan (x))
  83. {
  84. min = x;
  85. max = x;
  86. break;
  87. }
  88. #endif
  89. }
  90. *min_out = min;
  91. *max_out = max;
  92. }
  93. size_t
  94. FUNCTION(gsl_vector,max_index) (const TYPE(gsl_vector) * v)
  95. {
  96. /* finds the largest element of a vector */
  97. const size_t N = v->size ;
  98. const size_t stride = v->stride ;
  99. BASE max = v->data[0 * stride];
  100. size_t imax = 0;
  101. size_t i;
  102. for (i = 0; i < N; i++)
  103. {
  104. BASE x = v->data[i*stride];
  105. if (x > max)
  106. {
  107. max = x;
  108. imax = i;
  109. }
  110. #ifdef FP
  111. if (isnan (x))
  112. {
  113. return i;
  114. }
  115. #endif
  116. }
  117. return imax;
  118. }
  119. size_t
  120. FUNCTION(gsl_vector,min_index) (const TYPE(gsl_vector) * v)
  121. {
  122. /* finds the smallest element of a vector */
  123. const size_t N = v->size ;
  124. const size_t stride = v->stride ;
  125. BASE min = v->data[0 * stride];
  126. size_t imin = 0;
  127. size_t i;
  128. for (i = 0; i < N; i++)
  129. {
  130. BASE x = v->data[i*stride];
  131. if (x < min)
  132. {
  133. min = x;
  134. imin = i;
  135. }
  136. #ifdef FP
  137. if (isnan (x))
  138. {
  139. return i;
  140. }
  141. #endif
  142. }
  143. return imin;
  144. }
  145. void
  146. FUNCTION(gsl_vector,minmax_index) (const TYPE(gsl_vector) * v,
  147. size_t * imin_out,
  148. size_t * imax_out)
  149. {
  150. /* finds the smallest and largest elements of a vector */
  151. const size_t N = v->size ;
  152. const size_t stride = v->stride ;
  153. size_t imin = 0, imax = 0;
  154. BASE max = v->data[0 * stride];
  155. BASE min = v->data[0 * stride];
  156. size_t i;
  157. for (i = 0; i < N; i++)
  158. {
  159. BASE x = v->data[i*stride];
  160. if (x < min)
  161. {
  162. min = x;
  163. imin = i;
  164. }
  165. if (x > max)
  166. {
  167. max = x;
  168. imax = i;
  169. }
  170. #ifdef FP
  171. if (isnan (x))
  172. {
  173. imin = i;
  174. imax = i;
  175. break;
  176. }
  177. #endif
  178. }
  179. *imin_out = imin;
  180. *imax_out = imax;
  181. }