File: math_utils.cpp

package info (click to toggle)
dpuser 4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,628 kB
  • sloc: cpp: 148,693; ansic: 18,648; fortran: 5,815; lex: 1,116; makefile: 760; yacc: 741; sh: 390; pascal: 98
file content (579 lines) | stat: -rw-r--r-- 18,079 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#ifdef WIN
#pragma warning (disable: 4786) // disable warning for STL maps
#endif /* WIN */

/*
 * file: utils/math_utils.cpp
 * Purpose: some useful math routines not available in C - implementation
 * Author: Thomas Ott
 *
 * History: 30.06.1999: file created
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fits.h"
#include "math_utils.h"

/* Determine nearest integer to specified double value */
int nint(double x)
{
	int k;
	
	k = (int)x;
	if (fabs(x - k) < 0.5 ) {
		return k;
	} else {
		if (x >= 0.0) return k + 1;
		else return k - 1;
	}
}

/* Fraction of a real number */
double frac(double x) {
	double rv;

	rv = x - (double)((long)x);
	if (rv < 0.0) rv++;

	return rv;
}

int floatcmp(const void *a, const void *b)
{
	if (*(float *)a < *(float *)b) return(-1);
	if (*(float *)a > *(float *)b) return(1);
	return(0);
}

/* Cosine for an argument in degrees */
double cosdeg(double x) {
	return(cos(x * M_PI / 180.0));
}

/* Sine for an argument in degrees */
double sindeg(double x) {
	return(sin(x * M_PI / 180.0));
}

/* Convert radians to degrees */
double rad2deg(double x) {
	return (x * 180.0 / M_PI);
}

/* Convert degrees to radians */
double deg2rad(double x) {
	return (x / 180.0 * M_PI);
}

/* Square of a real number */
double sqr(double x) {
	return(x*x);
}

#ifdef WIN
double asinh(double x) {
	return log(x + sqrt(x * x + 1.0));
}

double acosh(double x) {
	return log(x + sqrt(x * x - 1.0));
}

double atanh(double x) {
	return 0.5 * log((1.0 + x) / (1.0 - x));
}
#ifdef _WINDOWS // only for Visual C++ 6.0
extern "C" double rint(double x) {
	return (double)nint(x);
}
#endif
#endif /* WIN */

double trans_chi2(int ncoords, double *S, double *ref, double *xim, double *yim, double *error) {
	int i;
	double result = 0.0;
	double polynomial;
	
	for (i = 0; i < ncoords; i++) {
		polynomial = S[5] + S[0] * xim[i] + S[1] * yim[i] + S[2] * sqr(xim[i]) + S[3] * sqr(yim[i]) + S[4] * xim[i] * yim[i];
		result += sqr((ref[i] - polynomial) / error[i]);
	}
	return result;
}

/* PURPOSE: A least square fit is used in order to find 
;	   the parameters for linearly fitting a reference
;	   image to a any other image of the same star field.
;	   --> For further description and PASCAL source code
;	       see Montenbruck/Pfleger: "Astronomie mit dem 
;			Personal Computer", 1989, p. 198ff	 
;
; VARIABLES: n - Number of equations = number of reference sources
;	     m - Number of fitting parameters (3 for a linear fit, 6 for quadratic fit)
;	     A - coefficient matrix with n lines and m+1 columns
;		 structure of line k: x1 y1 1 X1
;			with (x1,y1) - coordinates of a reference star
;				       in the selected image 
;				   1 - cofficient for additive parameter
;			          X1 - x-coordinate of a reference 
;					star in the reference image 
;
; COMMENTS: Procedure has to be called two times in order to obtain all
;	    six plate constants. In the first call, the (m+1)st column
;	    of A contains the x-coordinates of the reference stars in 
; 	    the reference image; in the second call, these are replaced 
;	    by the y-coordinates.  */

double lsqfit(double **A, double *S, int n, int m) {
	double LEPS = 1.0e-10;
	double p = 0.0, q = 0.0, h = 0.0;
	double res = 0.0;
	long i = 0L, k = 0L, j = 0L;
/*	; i = 0...n (number of reference sources=number of equations) */
/*	; j = 0...m (number of parameters for linear fit) */

	for (j = 0; j < m; j++) { // Loop over columns 1..m of A
  
/* eliminate elements A[j,i] with i>j from columns j */

		for (i = (j+1); i < n; i++) {
			if ((A[j][i] > 0.0) || (A[j][i] < 0.0)) {
/* compute p, q, and new A[j,j] */
/* set A[j,i] = 0 */
				if (fabs(A[j][j]) < (LEPS * fabs(A[j][i]))) {
					p = 0.0; q = 1.0; A[j][j] = -A[j][i]; A[j][i] = 0.0;
				} else {
					h = sqrt(A[j][j] * A[j][j] + A[j][i] * A[j][i]);
					if (A[j][j] < 0.0) h = -h;
					p = A[j][j]/h; q = -A[j][i]/h; A[j][j] = h; A[j][i] = 0.0;
				}
/* Change rest of current line */
				for (k = (j+1); k <= m; k++) {
					h = p*A[k][j] - q*A[k][i];
					A[k][i] = q*A[k][j] + p*A[k][i];
					A[k][j] = h;
				}
			}
		}
	}
/* compute and print out residual error */
	for (i = m; i < n; i++) res += sqr(A[m][i]);
	res = sqrt(res);
//	printf("lsqfit: Residual error is %.10f\n", res);
	
/* compute elements of vector S */

	for (i = (m-1); i >= 0; i--) {
		h = A[m][i];
		for (k = (i+1); k < m; k++) {
			h = h + A[k][i]*S[k];
/*;	print, S[k], A[k,i], h */
		}
		S[i] = -h/A[i][i];
	}
	return res;
}

/*
 * Calculate transformation matrix between two lists of coordinates. The solution of
 * the fit is given as quadratic equations. If 1 reference coordinate is given, only
 * the shift is calculated. If 2 coordinates are given, the scale in both coordinates
 * is assumed to be the same, then this scale, the shift and rotation are calculated.
 * Given 3 to 5 coordinates, a linear equation will be calculated, using lsqfit to
 * solve the potential overdetermined set of equations. Starting with 6 coordinates,
 * a full quadratic solution will be determined.
 *
 * The transformation equations are defined as follows:
 *  x' = SX[0]*x+SX[1]*y+SX[2]*x*x+SX[3]*y*y+SX[4]*x*y+SX[5]
 *  y' = SY[0]*x+SY[1]*y+SY[2]*x*x+SY[3]*y*y+SY[4]*x*y+SY[5]
 *
 * The routine takes the following input parameters:
 *  ncoords : Number of coordinates
 *  xref    : Pointer to the reference coordinates, organised as [x0, x1, ..., x(n-1)]
 *  yref    : Pointer to the reference coordinates, organised as [y0, y1, ..., y(n-1)]
 *  xin     : Pointer to the coordinates to be fitted, organised as xref
 *  yin     : Pointer to the coordinates to be fitted, organised as yref
 *  SX      : Pointer to the solved equation for x, must be at least double[6];
 *  SY      : Pointer to the solved equation for y, must be at least double[6];
 */

int trans_matrix(int ncoords, int nused, double *xref, double *yref, double *xim, double *yim, double *SX, double *SY, double *xerror, double *yerror) {
	int i;//, j, k;
	double *A[11], angle, scale;
	int malloc_error = 0;

	for (i = 0; i < 10; i++) SX[i] = SY[i] = 0.0;
	for (i = 0; i <= 10; i++) if ((A[i] = (double *)malloc(ncoords * sizeof(double))) == NULL) malloc_error = 1;
	*xerror = *yerror = 0.0;

	if (malloc_error) {
		for (i = 0; i <= 10; i++) if (A[i] != NULL) free(A[i]);
		dp_output("trans_matrix: Memory allocation error\n");
		return 0;
	} else switch(nused) {
		case 1:
			SX[5] = xref[0] - xim[0];
			SY[5] = yref[0] - yim[0];
			SX[0] = SY[1] = 1.0;
			break;
		case 2:
/*			nenner = (xref[0] - xref[1]) * (xref[0] - xref[1]) + (yref[0] - yref[1]) * (yref[0] - yref[1]);
			if (nenner != 0.0) {
				SX[0] = ((xref[0] - xref[1]) * (xim[0] - xim[1]) + (yref[0] - yref[1]) * (yim[0] - yim[1])) / nenner;
				SY[1] = SX[0];
				SX[1] = ((yref[0] - yref[1]) * (xim[1] - xim[0]) + (xref[0] - xref[1]) * (yim[0] - yim[1])) / nenner;
				SY[0] = SX[1];
				SX[1] = -SX[1];
				SX[5] = (xref[0] * xref[0] * xim[1]
				      +  xim[0] * (xref[1] * xref[1] - yref[0] * yref[1] + yref[1] * yref[1])
				      +  yref[0] * (xim[1] * yref[0] - xref[1] * yim[0] - xim[1] * yref[1] + xref[1] * yim[1])
							-  xref[0] * (xim[0] * xref[1] + xref[1] * xim[1] - yim[0] * yref[1] + yref[1] * yim[1]))
							/ nenner;
				SY[5] = (xref[1] * xref[1] * yim[0]
				      +  xref[0] * xim[1] * yref[1]
							-  yref[0] * yim[0] * yref[1]
							+  yim[0] * yref[1] * yref[1]
							+  xim[0] * xim[0] * (xref[1] * yref[0] - xref[0] * yref[1])
							+  xref[0] * xref[0] * yim[1]
							+  yref[0] * yref[0] * yim[1]
							-  yref[0] * yref[1] * yim[1]
							-  xref[1] * (xim[1] * yref[0] + xref[0] * yim[0] + xref[0] * yim[1]))
							/  nenner;
			}*/

/* rainer */
/*     nenner = (xref[0] - xref[1]) * (xref[0] - xref[1]) + (yref[0] - yref[1]) * (yref[0] - yref[1]);
		 printf("%.10f\n", nenner);
			if (nenner != 0.0) {
					SY[0] = ((xref[0] - xref[1]) * (yim[0] - yim[1]) - (yref[0] - yref[1]) * (xim[0] - xim[1])) / nenner;
		      SX[1] = SY[0];
					SY[0] = -SY[0];
					SY[1] = (xim[0] - xim[1])/(xref[0] - xref[1]) + SY[1] * (yref[0] - yref[1])/(xref[0] - xref[1]);
			    SX[0] = SY[1];
          SX[5] = xim[0] - SX[0] * xref[0] - SX[1] * yref[0];
					SY[5] = yim[0] - SY[0] * xref[0] - SY[1] * yref[0];
					scale = sqrt(SX[0] * SX[0] + SY[0] * SY[0]);
					SX[5] *= fabs(SX[0]) / scale;
					SY[5] *= fabs(SX[0]) / scale;
      }*/
			
/* end rainer */
				angle = atan2((yim[0] - yim[1]), (xim[0] - xim[1]));
				angle -= atan2((yref[0] - yref[1]), (xref[0] - xref[1]));
				scale = sqr(yref[0] - yref[1]) + sqr(xref[0] - xref[1]);
				scale /= sqr(yim[0] - yim[1]) + sqr(xim[0] - xim[1]);
				scale = sqrt(scale);
				scale = 1. / scale;
				dp_debug("angle = %.10f\n", angle * 180. / M_PI);
				dp_debug("scale = %.10f\n", scale);
				SX[5] = xim[0] - scale * (xref[0] * cos(angle) - yref[0] * sin(angle));
				SY[5] = yim[0] - scale * (xref[0] * sin(angle) + yref[0] * cos(angle));
				SX[0] = SY[1] = scale * cos(angle);
				SY[0] = scale * sin(angle);
				SX[1] = -scale * sin(angle);
			break;
		case 3:
		case 4:
		case 5:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = 1.0;
				A[3][i] = -xim[i];
			}
			*xerror = lsqfit(A, SX, ncoords, 3);
			SX[5] = SX[2];
			SX[2] = 0.0;
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = 1.0;
				A[3][i] = -yim[i];
			}
			*yerror = lsqfit(A, SY, ncoords, 3);
			SY[5] = SY[2];
			SY[2] = 0.0;
			break;
		case 10:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = xref[i]*xref[i]*xref[i];
				A[6][i] = yref[i]*yref[i]*yref[i];
				A[7][i] = xref[i]*xref[i]*yref[i];
				A[8][i] = yref[i]*yref[i]*xref[i];
				A[9][i] = 1.0;
				A[10][i] = -xim[i];
			}
			*xerror = lsqfit(A, SX, ncoords, 10);
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = xref[i]*xref[i]*xref[i];
				A[6][i] = yref[i]*yref[i]*yref[i];
				A[7][i] = xref[i]*xref[i]*yref[i];
				A[8][i] = yref[i]*yref[i]*xref[i];
				A[9][i] = 1.0;
				A[10][i] = -yim[i];
			}
			*yerror = lsqfit(A, SY, ncoords, 10);
			break;
		default:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = 1.0;
				A[6][i] = -xim[i];
			}
			*xerror = lsqfit(A, SX, ncoords, 6);
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = 1.0;
				A[6][i] = -yim[i];
			}
			*yerror = lsqfit(A, SY, ncoords, 6);
			break;
		}
	
	for (i = 0; i <= 10; i++) if (A[i] != NULL) free(A[i]);
	return 1;
}

int trans_matrix_errors(int ncoords, int nused, double *xref, double *yref, double *xim, double *yim, double *SX, double *SY, double *xerror, double *yerror) {
	int i, j, k;
	double *A[11], angle, scale, error;
	int malloc_error = 0;

	for (i = 0; i < 10; i++) SX[i] = SY[i] = 0.0;
	for (i = 0; i <= 10; i++) if ((A[i] = (double *)malloc(ncoords * sizeof(double))) == NULL) malloc_error = 1;

	if (malloc_error) {
		for (i = 0; i <= 10; i++) if (A[i] != NULL) free(A[i]);
		dp_output("trans_matrix: Memory allocation error\n");
		return 0;
	} else switch(nused) {
		case 1:
			SX[5] = xref[0] - xim[0];
			SY[5] = yref[0] - yim[0];
			SX[0] = SY[1] = 1.0;
			break;
		case 2:
				angle = atan2((yim[0] - yim[1]), (xim[0] - xim[1]));
				angle -= atan2((yref[0] - yref[1]), (xref[0] - xref[1]));
				scale = sqr(yref[0] - yref[1]) + sqr(xref[0] - xref[1]);
				scale /= sqr(yim[0] - yim[1]) + sqr(xim[0] - xim[1]);
				scale = sqrt(scale);
				scale = 1. / scale;
				dp_debug("angle = %.10f\n", angle * 180. / M_PI);
				dp_debug("scale = %.10f\n", scale);
				SX[5] = xim[0] - scale * (xref[0] * cos(angle) - yref[0] * sin(angle));
				SY[5] = yim[0] - scale * (xref[0] * sin(angle) + yref[0] * cos(angle));
				SX[0] = SY[1] = scale * cos(angle);
				SY[0] = scale * sin(angle);
				SX[1] = -scale * sin(angle);
			break;
		case 3:
		case 4:
		case 5:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = 1.0;
				A[3][i] = -xim[i];
			}
			error = lsqfit(A, SX, ncoords, 3);
			SX[5] = SX[2];
			SX[2] = 0.0;
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = 1.0;
				A[3][i] = -yim[i];
			}
			error = lsqfit(A, SY, ncoords, 3);
			SY[5] = SY[2];
			SY[2] = 0.0;
			break;
		case 10:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = xref[i]*xref[i]*xref[i];
				A[6][i] = yref[i]*yref[i]*yref[i];
				A[7][i] = xref[i]*xref[i]*yref[i];
				A[8][i] = yref[i]*yref[i]*xref[i];
				A[9][i] = 1.0;
				A[10][i] = -xim[i];
			}
			error = lsqfit(A, SX, ncoords, 10);
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = xref[i]*xref[i]*xref[i];
				A[6][i] = yref[i]*yref[i]*yref[i];
				A[7][i] = xref[i]*xref[i]*yref[i];
				A[8][i] = yref[i]*yref[i]*xref[i];
				A[9][i] = 1.0;
				A[10][i] = -yim[i];
			}
			error = lsqfit(A, SY, ncoords, 10);
			break;
		default:
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = 1.0;
				A[6][i] = -xim[i];
			}
			error = lsqfit(A, SX, ncoords, 6);
			for (i = 0; i < ncoords; i++) {
				A[0][i] = xref[i];
				A[1][i] = yref[i];
				A[2][i] = xref[i]*xref[i];
				A[3][i] = yref[i]*yref[i];
				A[4][i] = xref[i]*yref[i];
				A[5][i] = 1.0;
				A[6][i] = -yim[i];
			}
			error = lsqfit(A, SY, ncoords, 6);
			break;
		}

	if (nused == 6) {
		double Sip[6], Sim[6], Sipjp[6], Sipjm[6], Simjp[6], Simjm[6];
		double eps = 0.001;
		double matrix[6][6];
		double chisq0;
		Fits imatrix;
		imatrix.create(6, 6, R8);

/* First, X coordinate */		
		chisq0 = trans_chi2(ncoords, SX, xref, xim, yim, xerror);
		for (i = 0; i < nused; i++) {
			for (k = 0; k < 6; k++) { Sip[k] = Sim[k] = SX[k]; }
			double epsi = eps * SX[i];
			Sip[i] += epsi;
			Sim[i] -= epsi;
			for (j = 0; j < nused; j++) {
				if (j < i) {
					matrix[i][j] = matrix[j][i];
				} else if (i == j) {
					matrix[i][i] = (trans_chi2(ncoords, Sip, xref, xim, yim, xerror)
								 + trans_chi2(ncoords, Sim, xref, xim, yim, xerror)
								 - 2. * chisq0) / sqr(epsi);
				} else {
					double epsj = eps * SX[j];
					for (k = 0; k < 6; k++) {
						Sipjp[k] = Sipjm[k] = Sip[k];
						Simjm[k] = Simjp[k] = Sim[k];
					}
					Sipjp[j] += epsj;
					Sipjm[j] -= epsj;
					Simjp[j] += epsj;
					Simjm[j] -= epsj;
					matrix[i][j] = (trans_chi2(ncoords, Sipjp, xref, xim, yim, xerror)
								 + trans_chi2(ncoords, Simjm, xref, xim, yim, xerror)
								 - trans_chi2(ncoords, Sipjm, xref, xim, yim, xerror)
								 - trans_chi2(ncoords, Simjp, xref, xim, yim, xerror)) / (4. * epsi * epsj);
								 
				}
			}
		}
		for (i = 0; i < 6; i++) {
			for (j = 0; j < 6; j++) {
				imatrix.r8data[imatrix.C_I(i, j)] = matrix[i][j];
			}
		}
		imatrix.div(2.);
		imatrix.ginvert();
		imatrix.Abs();
		imatrix.Sqrt();
		
		for (i = 0; i < 6; i++) xerror[i] = imatrix(i+1,i+1);

/* Second, Y coordinate */		
		chisq0 = trans_chi2(ncoords, SY, yref, xim, yim, yerror);
		for (i = 0; i < nused; i++) {
			for (k = 0; k < 6; k++) { Sip[k] = Sim[k] = SY[k]; }
			double epsi = eps * SY[i];
			Sip[i] += epsi;
			Sim[i] -= epsi;
			for (j = 0; j < nused; j++) {
				if (j < i) {
					matrix[i][j] = matrix[j][i];
				} else if (i == j) {
					matrix[i][i] = (trans_chi2(ncoords, Sip, yref, xim, yim, yerror)
								 + trans_chi2(ncoords, Sim, yref, xim, yim, yerror)
								 - 2. * chisq0) / sqr(epsi);
				} else {
					double epsj = eps * SY[j];
					for (k = 0; k < 6; k++) {
						Sipjp[k] = Sipjm[k] = Sip[k];
						Simjm[k] = Simjp[k] = Sim[k];
					}
					Sipjp[j] += epsj;
					Sipjm[j] -= epsj;
					Simjp[j] += epsj;
					Simjm[j] -= epsj;
					matrix[i][j] = (trans_chi2(ncoords, Sipjp, yref, xim, yim, yerror)
								 + trans_chi2(ncoords, Simjm, yref, xim, yim, yerror)
								 - trans_chi2(ncoords, Sipjm, yref, xim, yim, yerror)
								 - trans_chi2(ncoords, Simjp, yref, xim, yim, yerror)) / (4. * epsi * epsj);
								 
				}
			}
		}
		for (i = 0; i < 6; i++) {
			for (j = 0; j < 6; j++) {
				imatrix.r8data[imatrix.C_I(i, j)] = matrix[i][j];
			}
		}
		imatrix.div(2.);
		imatrix.ginvert();
		imatrix.Abs();
		imatrix.Sqrt();
		for (i = 0; i < 6; i++) yerror[i] = imatrix(i+1,i+1);
	}
				
				
	
	for (i = 0; i <= 10; i++) if (A[i] != NULL) free(A[i]);
	return 1;
}

// the sinc function: sinc(x) = sin(x) / x
// For arguments -0.1 < x < 0.1, return the taylor expansion
double sinc(double x) {
	if (fabs(x) > 0.1) return sin(x) / x;
	else return x*x*x*x / 120. - x*x / 6. + 1.;
}
// the cosc function: derivative of the sinc(x) = sin(x)/x
// For arguments -0.1 < x < 0.1, return the taylor expansion
double cosc(double x) {
	if (fabs(x) > 0.1) return cos(x) / x - sin(x) / x / x;
	else return -x*x*x*x*x / 840. + x*x*x / 30. - x / 3.;
}