1
/*===========================================================================
2
Copyright (C) 1991-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
29
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
.AUTHOR Cristian Levin - ESO La Silla
35
.PURPOSE Miscelanous mathematical algorithms:
36
- linear square fitting.
37
- fast polynomial evaluation.
38
- search of the median.
39
- polynomial interpolation.
40
.KEYWORDS interpolation, fitting, polynomial evaluation.
41
.VERSION 1.0 1-Apr-1991 Implementation
44
------------------------------------------------------------*/
46
#include "cpl_matrix.h"
52
/******************************************************
54
* lfit(): general linear least squares solution.
56
******************************************************/
57
void lfit( x, y, ndata, a, mfit, funcs )
58
int ndata, mfit; /* IN: no. of values, no. of coefficients */
59
double x[], y[], a[]; /* IN: (Xi,Yi) OUT: (Ai) */
62
lsqfit_nr(x, y, NULL, ndata, a, mfit, funcs);
65
void fit_poly (float inimage[], float outimage[], double start,
66
double step, int npix, int fit_deg)
70
double *ad, *xin, *yin;
73
xin = dvector (0, npix - 1);
74
yin = dvector (0, npix - 1);
76
for (i = 0; i < npix; i++)
78
xin[i] = start + i * step;
79
yin[i] = (double) inimage[i];
82
ad = dvector (1, fit_deg);
83
af = dvector (1, fit_deg);
85
lfit (xin, yin, npix, ad, fit_deg, dpoly);
86
for (i = 1; i <= fit_deg; i++)
91
for (i = 0; i < npix; i++)
93
xout = start + step * i;
94
outimage[i] = (float) eval_dpoly (xout, ad, fit_deg);
97
free_dvector(xin, 0, npix - 1), free_dvector(yin, 0, npix - 1);
98
free_dvector(ad, 1, fit_deg), free_dvector(af, 1, fit_deg);
104
void dpoly(double x, double p[], int np)
109
for (j = 2; j <= np; j++)
113
void dleg(double x, double pl[], int nl)
116
double twox, f2, f1, d;
125
for (j = 3; j <= nl; j++)
130
pl[j] = (f2 * pl[j - 1] - f1 * pl[j - 2]) / d;
135
void dcheb(double x, double pl[], int nl)
145
for (j = 3; j <= nl; j++)
146
pl[j] = twox * pl[j - 1] - pl[j - 2];
150
/************************************************************
152
* eval_fpoly(), eval_dpoly(): fast polynomial evaluation.
154
************************************************************/
156
float eval_fpoly(x, a, n)
158
float *a; /* coefficients (range: 1..n) */
164
for ( i = n; i >= 1; i-- )
170
double eval_dpoly(x, a, n)
172
double *a; /* coefficients (range: 1..n) */
178
for ( i = n; i >= 1; i-- )
185
double select_pos (unsigned long k, unsigned long n, double arr[])
187
double select_pos (k, n, arr)
192
/* select the kth smallest value in arr[1...n], NR, pg. 342 */
194
#define SWAPD(a, b) {double temp = (a); (a) = (b); (b) = temp; }
198
unsigned long i, ir, j, l, mid;
199
/* double a, temp; */
208
if (ir == l + 1 && arr[ir] < arr[l])
210
SWAPD (arr[l], arr[ir])
217
SWAPD (arr[mid], arr[l + 1])
218
if (arr[l + 1] > arr[ir])
220
SWAPD (arr[l + 1], arr[ir])
222
if (arr[l] > arr[ir])
224
SWAPD (arr[l], arr[ir])
226
if (arr[l + 1] > arr[l])
228
SWAPD (arr[l + 1], arr[l])
243
SWAPD (arr[i], arr[j])
258
void heap_copy(int n, float arr[], float out[])
260
void heap_copy (n, arr, out)
270
for (j=0; j<n; j++) out[j] = arr[j];
276
int heap_compare(int n, float arr1[], float arr2[])
278
int heap_compare (n, arr1, arr2)
283
/* sort distribution (heapsort) */
288
printf("Comparing arrays of size %d\n",n);
293
for (j = 0; j < 4; j++)
294
printf("HEAPSORT: Array elements [%d] = %f %f\n",j,arr1[j],arr2[j]);
295
for (j = n-4; j < n; j++)
296
printf("HEAPSORT: Array elements [%d] = %f %f\n",j,arr1[j],arr2[j]);
299
for (j = 0; j < n; j++)
301
// printf("Index : %d\n",j);
302
if (arr1[j] != arr2[j]) {
304
printf("HEAPSORT: Array difference at index %d (%f, %f)\n",j,arr1[j],arr2[j]);
307
printf ("Comparison flag = %d\n",check);
313
void heap_sort (int n, float arr[])
315
void heap_sort (n, arr)
319
/* sort distribution (heapsort) */
324
for (j = 1; j < n; j++)
328
while (i >= 0 && arr[i] > a)
340
float heap_median (int n, float arr[])
342
float heap_median (n, arr)
346
/* find median of distribution (heapsort) */
352
b = (float*) malloc ((size_t)((n)*sizeof (float)));
354
heap_copy(n, arr, b);
355
// printf("heap_sort: Comparing copies\n");
356
// heap_compare (n, arr, b);
363
// median = (n % 2 ? b[n2p] : 0.5*(b[n2]+b[n2p])); // b[(n-1)/2];
366
// printf("heap_sort: Median value %f\n",hmed);
371
/************************************************************
373
* median(): efficient search of the median.
375
************************************************************/
385
n2p = (n2 = n/2) + 1;
386
return((n % 2) ? x[n2p] : 0.5 * (x[n2] + x[n2p]));
389
/************************************************************
391
* piksrt(): sorting by straight insertion.
393
************************************************************/
405
while (i > 0 && arr[i] > a) {
414
float pik_median (int n, float arr[])
416
float pik_median ( n, arr )
420
/* find median of distribution (use piksort)
421
NR page 330 modified for mos by Sabine ??
422
input array arr[] is conserved by buffer b[] -- here we start
423
with arr[0,..,n-1] both in contrary to select_pos TS-0896
433
for (j = 0; j < n; j++)
436
for (j = 1; j < n; j++)
440
while (i >= 0 && b[i] > a)