GIRAFFE Pipeline Reference Manual

giarray.c

00001 /* $Id: giarray.c,v 1.9 2007/03/12 12:47:05 rpalsa Exp $
00002  *
00003  * This file is part of the GIRAFFE Pipeline
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: rpalsa $
00023  * $Date: 2007/03/12 12:47:05 $
00024  * $Revision: 1.9 $
00025  * $Name: giraffe-2_9 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <string.h>
00033 
00034 #include <cxmemory.h>
00035 #include <cxmessages.h>
00036 
00037 #include "giarray.h"
00038 
00039 
00048 inline static void
00049 _giraffe_swap(cxdouble* a, cxdouble* b)
00050 {
00051     register cxdouble tmp = *a;
00052 
00053     *a = *b;
00054     *b = tmp;
00055 
00056     return;
00057 
00058 }
00059 
00060 
00061 inline static cxint
00062 _giraffe_array_heap_sort(cxdouble* array, cxsize size)
00063 {
00064 
00065     register cxsize l = size >> 1;
00066 
00067     cxsize ir = size - 1;
00068 
00069 
00070     while (1) {
00071 
00072         register cxsize i = 0;
00073         register cxsize j = 0;
00074 
00075         register cxdouble rra = 0.;
00076 
00077         if ( l > 0) {
00078             rra = array[--l];        /* Still in hiring phase. */
00079         }
00080         else {                       /* In retirement-and-promotion phase. */
00081             rra = array[ir];         /* Clear a space at end of array.  */
00082             array[ir--] = array[0];  /* Retire the top of the heap into it. */
00083             if (ir == 0) {           /* Done with the last promotion. */
00084                 array[0] = rra;      /* The least competent worker of all! */
00085                 return 0;
00086             }
00087         }
00088 
00089         i = l;               /* Whether in the hiring phase or promotion */
00090         j = (l << 1) + 1;    /* phase, we here set up to sift down element */
00091         while (j <= ir) {    /* rra to its proper level. */
00092 
00093             /* Compare to the better underling. */
00094             if (j < ir && (array[j+1] - array[j]) > DBL_EPSILON) {
00095                 ++j;
00096             }
00097 
00098             if (array[j] - rra > DBL_EPSILON) {    /* Demote rra. */
00099                 array[i] = array[j];
00100                 j += (i = j) + 1;
00101             }
00102             else {
00103                 j = ir + 1;
00104             }
00105         }
00106         array[i] = rra;              /* Put into its slot. */
00107     }
00108 
00109 }
00110 
00111 
00112 inline static cxdouble
00113 _giraffe_array_get_sorted(cxdouble* a, cxint n, cxint k)
00114 {
00115 
00116     register cxint l = 0;
00117     register cxint m = n - 1;
00118 
00119 
00120     while (l < m) {
00121 
00122         register cxint i = l;
00123         register cxint j = m;
00124 
00125         register cxdouble x = a[k];
00126 
00127 
00128         do {
00129 
00130             while (x - a[i] > DBL_EPSILON) {
00131                 ++i;
00132             }
00133 
00134             while (a [j] - x > DBL_EPSILON) {
00135                 --j;
00136             }
00137 
00138             if (i <= j) {
00139                 _giraffe_swap(&a[i], &a[j]) ;
00140                 ++i;
00141                 --j;
00142             }
00143 
00144         } while (i <= j);
00145 
00146         if (j < k) {
00147             l = i;
00148         }
00149 
00150         if (k < i) {
00151             m = j;
00152         }
00153 
00154     }
00155 
00156     return a[k];
00157 
00158 }
00159 
00160 
00176 cxint
00177 giraffe_array_sort(cxdouble* array, cxsize size)
00178 {
00179 
00180     return _giraffe_array_heap_sort(array, size);
00181 
00182 }
00183 
00184 
00185 cxdouble
00186 giraffe_array_mean(const cxdouble* array, cxsize size)
00187 {
00188     register cxsize i = 0;
00189 
00190     register cxdouble sum = 0.;
00191 
00192 
00193     for (i = 0; i < size; i++) {
00194         sum += array[i];
00195     }
00196 
00197     return sum / size;
00198 
00199 }
00200 
00201 
00202 cxdouble
00203 giraffe_array_median(const cxdouble* array, cxsize size)
00204 {
00205 
00206     register cxsize position = (size & 1) == 1 ? size / 2 : size / 2 - 1;
00207 
00208     cxdouble median = 0.;
00209     cxdouble* a = NULL;
00210 
00211 
00212     cx_assert(array != NULL);
00213 
00214     a = cx_calloc(size, sizeof(cxdouble));
00215     memcpy(a, array, size * sizeof(cxdouble));
00216 
00217 
00218     median = _giraffe_array_get_sorted(a, size, position);
00219 
00220     cx_free(a);
00221     a = NULL;
00222 
00223     return median;
00224 
00225 }
00226 

This file is part of the GIRAFFE Pipeline Reference Manual 2.9.0.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Thu Jan 26 14:20:27 2012 by doxygen 1.6.3 written by Dimitri van Heesch, © 1997-2004