GIRAFFE Pipeline Reference Manual

giarray.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <string.h>
33 
34 #include <cxmemory.h>
35 #include <cxmessages.h>
36 
37 #include "giarray.h"
38 
39 
48 inline static void
49 _giraffe_swap(cxdouble* a, cxdouble* b)
50 {
51  register cxdouble tmp = *a;
52 
53  *a = *b;
54  *b = tmp;
55 
56  return;
57 
58 }
59 
60 
61 inline static cxint
62 _giraffe_array_heap_sort(cxdouble* array, cxsize size)
63 {
64 
65  register cxsize l = size >> 1;
66 
67  cxsize ir = size - 1;
68 
69 
70  while (1) {
71 
72  register cxsize i = 0;
73  register cxsize j = 0;
74 
75  register cxdouble rra = 0.;
76 
77  if ( l > 0) {
78  rra = array[--l]; /* Still in hiring phase. */
79  }
80  else { /* In retirement-and-promotion phase. */
81  rra = array[ir]; /* Clear a space at end of array. */
82  array[ir--] = array[0]; /* Retire the top of the heap into it. */
83  if (ir == 0) { /* Done with the last promotion. */
84  array[0] = rra; /* The least competent worker of all! */
85  return 0;
86  }
87  }
88 
89  i = l; /* Whether in the hiring phase or promotion */
90  j = (l << 1) + 1; /* phase, we here set up to sift down element */
91  while (j <= ir) { /* rra to its proper level. */
92 
93  /* Compare to the better underling. */
94  if (j < ir && (array[j+1] - array[j]) > DBL_EPSILON) {
95  ++j;
96  }
97 
98  if (array[j] - rra > DBL_EPSILON) { /* Demote rra. */
99  array[i] = array[j];
100  j += (i = j) + 1;
101  }
102  else {
103  j = ir + 1;
104  }
105  }
106  array[i] = rra; /* Put into its slot. */
107  }
108 
109 }
110 
111 
112 inline static cxdouble
113 _giraffe_array_get_sorted(cxdouble* a, cxint n, cxint k)
114 {
115 
116  register cxint l = 0;
117  register cxint m = n - 1;
118 
119 
120  while (l < m) {
121 
122  register cxint i = l;
123  register cxint j = m;
124 
125  register cxdouble x = a[k];
126 
127 
128  do {
129 
130  while (x - a[i] > DBL_EPSILON) {
131  ++i;
132  }
133 
134  while (a [j] - x > DBL_EPSILON) {
135  --j;
136  }
137 
138  if (i <= j) {
139  _giraffe_swap(&a[i], &a[j]) ;
140  ++i;
141  --j;
142  }
143 
144  } while (i <= j);
145 
146  if (j < k) {
147  l = i;
148  }
149 
150  if (k < i) {
151  m = j;
152  }
153 
154  }
155 
156  return a[k];
157 
158 }
159 
160 
176 cxint
177 giraffe_array_sort(cxdouble* array, cxsize size)
178 {
179 
180  return _giraffe_array_heap_sort(array, size);
181 
182 }
183 
184 
185 cxdouble
186 giraffe_array_mean(const cxdouble* array, cxsize size)
187 {
188  register cxsize i = 0;
189 
190  register cxdouble sum = 0.;
191 
192 
193  for (i = 0; i < size; i++) {
194  sum += array[i];
195  }
196 
197  return sum / size;
198 
199 }
200 
201 
202 cxdouble
203 giraffe_array_median(const cxdouble* array, cxsize size)
204 {
205 
206  register cxsize position = (size & 1) == 1 ? size / 2 : size / 2 - 1;
207 
208  cxdouble median = 0.;
209  cxdouble* a = NULL;
210 
211 
212  cx_assert(array != NULL);
213 
214  a = cx_calloc(size, sizeof(cxdouble));
215  memcpy(a, array, size * sizeof(cxdouble));
216 
217 
218  median = _giraffe_array_get_sorted(a, size, position);
219 
220  cx_free(a);
221  a = NULL;
222 
223  return median;
224 
225 }
226 

This file is part of the GIRAFFE Pipeline Reference Manual 2.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Mar 24 2014 11:43:51 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2004