File: cov-lib.c

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (649 lines) | stat: -rw-r--r-- 11,923 bytes parent folder | download
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2020, All rights reserved
*         DTU Physics, Lyngby, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Library: share/cov-lib.c
*
* %Identification
* Written by: Tobias Weber <tweber@ill.fr>
* Date: Nov 20, 2020
* Origin: Institut Laue Langevin
* Release: McStas 3.0
* Version: 0.1
*
* This file is used for resolution calculations, it was taken from "tlibs2" and "matrix_calc":
*   https://code.ill.fr/scientific-software/takin/tlibs2/-/blob/master/libs/mathlib.c
*   https://github.com/t-weber/matrix_calc/blob/master/src/libs/runtime.c
*
* Usage: within SHARE
* %include "cov-lib"
*
*******************************************************************************/

#ifndef MCCODE_STRING
	#include "cov-lib.h"

	#include <math.h>
	#include <float.h>
	#include <stdlib.h>
#endif


/* ---------------------------------------------------------------------------- */
/* linked list */
/* ---------------------------------------------------------------------------- */

tl2_list_type* tl2_lst_create(void *elem)
{
	tl2_list_type* lst = (tl2_list_type*)malloc(sizeof(tl2_list_type));
	lst->elem = elem;
	lst->next = 0;

	return lst;
}


tl2_list_type* tl2_lst_append(tl2_list_type *lst, void *elem)
{
	while(lst->next)
		lst = lst->next;

	lst->next = (tl2_list_type*)malloc(sizeof(tl2_list_type));
	lst->next->elem = elem;
	lst->next->next = 0;

	return lst->next;
}


void tl2_lst_remove(tl2_list_type *lst, void *elem)
{
	tl2_list_type *lst_prev = 0;
	while(lst)
	{
		if(lst->elem == elem)
			break;
		lst_prev = lst;
		lst = lst->next;
	}

	if(lst)
	{
		/* remove element */
		if(lst->elem)
		{
			free(lst->elem);
			lst->elem = 0;
		}

		/* unlink */
		if(lst_prev)
		{
			lst_prev->next = lst->next;
			free(lst);
		}
	}
}


void tl2_lst_free(tl2_list_type *lst)
{
	if(lst && lst->next)
		tl2_lst_free(lst->next);
	if(lst && lst->elem)
		free(lst->elem);
	if(lst)
		free(lst);
}
/* ---------------------------------------------------------------------------- */



/* ---------------------------------------------------------------------------- */
/* linalg functions */
/* ---------------------------------------------------------------------------- */

static double g_tl2_eps = DBL_EPSILON;
/* #pragma acc declare create(g_tl2_eps) */


/**
 * set float epsilon
 */
void tl2_set_eps(double eps)
{
	g_tl2_eps = eps;
}


/**
 * get float epsilon
 */
double tl2_get_eps()
{
	return g_tl2_eps;
}


/**
 * tests equality of floating point numbers
 */
int tl2_flt_equals(double x, double y, double eps)
{
	double diff = x-y;
	if(diff < 0.)
		diff = -diff;
	return diff <= eps;
}


/**
 * set matrix elements to zero
 */
void tl2_mat_zero(double* M, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			M[i*J + j] = 0.;
}


/**
 * set vector elements to zero
 */
void tl2_vec_zero(double* vec, int N)
{
	for(int i=0; i<N; ++i)
		vec[i] = 0.;
}


/**
 * copy a vector
 */
void tl2_vec_cpy(double* dst, const double* src, int N)
{
	for(int i=0; i<N; ++i)
		dst[i] = src[i];
}


/**
 * copy a matrix
 */
void tl2_mat_cpy(double* DST, const double* SRC, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			DST[i*J + j] = SRC[i*J + j];
}


/**
 * removes a given row and column of a square matrix
 */
void tl2_submat(const double* M, int N, double* M_new, int iremove, int jremove)
{
	int row_new = 0;
	for(int row=0; row<N; ++row)
	{
		if(row == iremove)
			continue;

		int col_new = 0;
		for(int col=0; col<N; ++col)
		{
			if(col == jremove)
				continue;

			M_new[row_new*(N-1) + col_new] = M[row*N + col];
			++col_new;
		}
		++row_new;
	}
}


/**
 * calculates the determinant
 */
double tl2_determinant(const double* M, int N)
{
	/* special cases */
	if(N == 0)
		return 0;
	else if(N == 1)
		return M[0];
	else if(N == 2)
		return M[0*N+0]*M[1*N+1] - M[0*N+1]*M[1*N+0];


	/* get row with maximum number of zeros */
	int row = 0;
	int maxNumZeros = 0;
	for(int curRow=0; curRow<N; ++curRow)
	{
		int numZeros = 0;
		for(int curCol=0; curCol<N; ++curCol)
		{
			if(tl2_flt_equals(M[curRow*N + curCol], 0, g_tl2_eps))
				++numZeros;
		}

		if(numZeros > maxNumZeros)
		{
			row = curRow;
			maxNumZeros = numZeros;
		}
	}


	/* recursively expand determiant along a row */
	double fullDet = 0.;

	double *submat = (double*)malloc((N-1)*(N-1) * sizeof(double));
	for(int col=0; col<N; ++col)
	{
		const double elem = M[row*N + col];
		if(tl2_flt_equals(elem, 0, g_tl2_eps))
			continue;

		tl2_submat(M, N, submat, row, col);
		const double sgn = ((row+col) % 2) == 0 ? 1. : -1.;
		fullDet += elem * tl2_determinant(submat, N-1) * sgn;
	}
	free(submat);

	return fullDet;
}


/**
 * inverted matrix
 */
int tl2_inverse(const double* M, double* I, int N)
{
	double fullDet = tl2_determinant(M, N);

	/* fail if determinant is zero */
	if(tl2_flt_equals(fullDet, 0., g_tl2_eps))
		return 0;

	double *submat = (double*)malloc((N-1)*(N-1) * sizeof(double));
	for(int i=0; i<N; ++i)
	{
		for(int j=0; j<N; ++j)
		{
			tl2_submat(M, N, submat, i, j);
			const double sgn = ((i+j) % 2) == 0 ? 1. : -1.;
			I[j*N + i] = tl2_determinant(submat, N-1) * sgn / fullDet;
		}
	}
	free(submat);

	return 1;
}


/**
 * matrix-matrix product
 */
void tl2_matmat_mul(const double* M1, const double* M2, double *RES, int I, int J, int K)
{
	for(int i=0; i<I; ++i)
	{
		for(int j=0; j<J; ++j)
		{
			RES[i*J + j] = 0.;

			for(int k=0; k<K; ++k)
				RES[i*J + j] += M1[i*K + k]*M2[k*J + j];
		}
	}
}


/**
 * matrix-vector product
 */
void tl2_matvec_mul(const double* M, const double* v, double *res, int I, int J)
{
	for(int i=0; i<I; ++i)
	{
		res[i] = 0.;
		for(int j=0; j<J; ++j)
		{
			res[i] += M[i*J + j]*v[j];
		}
	}
}


/**
 * transposed matrix
 */
void tl2_transpose(const double* M, double* T, int rows, int cols)
{
	for(int ctr=0; ctr<rows*cols; ++ctr)
	{
		int i = ctr/cols;
		int j = ctr%cols;
		T[j*rows + i] = M[i*cols + j];
	}
}


/**
 * vector inner product
 */
double tl2_inner(const double* v0, const double* v1, int N)
{
	double res = 0.;
	for(int i=0; i<N; ++i)
		res += v0[i]*v1[i];
	return res;
}


/**
 * vector outer product
 */
void tl2_outer(const double* v0, const double* v1, double *M, int N)
{
	for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
			M[i*N + j] = v0[i] * v1[j];
}


/**
 * 3-vector cross product
 */
void tl2_cross(const double* v0, const double* v1, double *res)
{
	res[0] = v0[1]*v1[2] - v0[2]*v1[1];
	res[1] = v0[2]*v1[0] - v0[0]*v1[2];
	res[2] = v0[0]*v1[1] - v0[1]*v1[0];
}


/**
 * vector length
 */
double tl2_vec_len(const double* vec, int N)
{
	double len = tl2_inner(vec, vec, N);
	return sqrt(len);
}


/**
 * vector addition
 */
void tl2_vec_add(const double* v0, const double* v1, double *res, int N)
{
	for(int i=0; i<N; ++i)
		res[i] = v0[i] + v1[i];
}


/**
 * vector subtraction
 */
void tl2_vec_sub(const double* v0, const double* v1, double *res, int N)
{
	for(int i=0; i<N; ++i)
		res[i] = v0[i] - v1[i];
}


/**
 * negative vector
 */
void tl2_vec_neg(const double* vec, double *res, int N)
{
	for(int i=0; i<N; ++i)
		res[i] = -vec[i];
}


/**
 * vector-scalar multiplication
 */
void tl2_vec_mul(const double* vec, double s, double *res, int N)
{
	for(int i=0; i<N; ++i)
		res[i] = vec[i] * s;
}


/**
 * vector-scalar division
 */
void tl2_vec_div(const double* vec, double s, double *res, int N)
{
	tl2_vec_mul(vec, 1./s, res, N);
}


/**
 * matrix addition
 */
void tl2_mat_add(const double* M0, const double* M1, double *RES, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			RES[i*J + j] = M0[i*J + j] + M1[i*J + j];
}


/**
 * matrix subtraction
 */
void tl2_mat_sub(const double* M0, const double* M1, double *RES, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			RES[i*J + j] = M0[i*J + j] - M1[i*J + j];
}


/**
 * negative matrix
 */
void tl2_mat_neg(const double* M, double *RES, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			RES[i*J + j] = - M[i*J + j];
}


/**
 * matrix-scalar multiplication
 */
void tl2_mat_mul(const double* M, double s, double *RES, int I, int J)
{
	for(int i=0; i<I; ++i)
		for(int j=0; j<J; ++j)
			RES[i*J + j] = M[i*J + j] * s;
}


/**
 * matrix-scalar division
 */
void tl2_mat_div(const double* M, double s, double *RES, int I, int J)
{
	tl2_mat_mul(M, 1./s, RES, I, J);
}


/**
 * mean vector
 */
void tl2_vec_mean(const tl2_list_type* veclist, const tl2_list_type* problist,
	double* mean, int N)
{
	tl2_vec_zero(mean, N);
	double prob = 0.;
	double *vec = (double*)malloc(N * sizeof(double));

	while(veclist)
	{
		tl2_vec_cpy(vec, (double*)veclist->elem, N);
		double p = 1.;

		if(problist)
		{
			p = *(double*)problist->elem;
			tl2_vec_mul(vec, p, vec, N);
		}

		tl2_vec_add(mean, vec, mean, N);
		prob += p;

		veclist = veclist->next;
		if(problist) problist = problist->next;
	}

	tl2_vec_div(mean, prob, mean, N);

	free(vec);
}


/**
 * covariance matrix
 */
int tl2_covariance(const tl2_list_type* veclist, const tl2_list_type* problist,
	double* COV, double* mean, int N)
{
	tl2_mat_zero(COV, N, N);
	tl2_vec_mean(veclist, problist, mean, N);

	double *vec = (double*)malloc(N * sizeof(double));
	double *dev = (double*)malloc(N * sizeof(double));
	double *outer = (double*)malloc(N*N * sizeof(double));
	double prob = 0.;
	unsigned int num_events = 0;

	while(veclist)
	{
		tl2_vec_cpy(vec, (double*)veclist->elem, N);
		double p = 1.;

		tl2_vec_sub(vec, mean, dev, N);
		tl2_outer(dev, dev, outer, N);

		if(problist)
		{
			p = *(double*)problist->elem;
			tl2_mat_mul(outer, p, outer, N, N);
		}

		tl2_mat_add(COV, outer, COV, N, N);
		prob += p;

		veclist = veclist->next;
		if(problist) problist = problist->next;
		++num_events;
	}

	tl2_mat_div(COV, prob, COV, N, N);

	free(vec);
	free(dev);
	free(outer);

	return num_events > 0;
}


/**
 * matrix trafo
 */
void tl2_mat_trafo(const double* M, const double* T, double* RES, int N, int ortho)
{
	double *Tinv = (double*)malloc(N*N * sizeof(double));
	double *TMP = (double*)malloc(N*N * sizeof(double));

	if(ortho)
		tl2_transpose(T, Tinv, N, N);
	else
		tl2_inverse(T, Tinv, N);

	tl2_matmat_mul(Tinv, M, TMP, N, N, N);
	tl2_matmat_mul(TMP, T, RES, N, N, N);

	free(Tinv);
	free(TMP);
}


/**
 * resolution matrix
 */
int tl2_reso(const tl2_list_type* veclist, const tl2_list_type* problist,
	double* COV, double* RESO)
{
	const int N = 4;
	tl2_mat_zero(COV, N, N);
	tl2_mat_zero(RESO, N, N);

	double *Qmean = (double*)malloc(N * sizeof(double));
	if(!tl2_covariance(veclist, problist, COV, Qmean, N))
	{
		free(Qmean);
		return 0;
	}

	double *Qdir = (double*)malloc(N * sizeof(double));
	double Qlen = tl2_vec_len(Qmean, N-1);
	tl2_vec_div(Qmean, Qlen, Qdir, N-1);

	double *Qup = (double*)malloc(N * sizeof(double));
	tl2_vec_zero(Qup, N);
	Qup[1] = 1;

	double *Qside = (double*)malloc(N * sizeof(double));
	tl2_vec_zero(Qside, N);
	tl2_cross(Qup, Qdir, Qside);

	double *T = (double*)malloc(N*N * sizeof(double));
	tl2_mat_zero(T, N, N);
	for(int i=0; i<N; ++i)
	{
		T[i*N + 0] = Qdir[i];
		T[i*N + 1] = Qside[i];
		T[i*N + 2] = Qup[i];
	}
	T[3*N + 3] = 1;

	tl2_mat_trafo(COV, T, COV, N, 1);
	tl2_inverse(COV, RESO, N);

	free(Qside);
	free(Qup);
	free(T);
	free(Qdir);
	free(Qmean);

	return 1;
}
/* ---------------------------------------------------------------------------- */



/* ----------------------------------------------------------------------------- */
/* Helper functions */
/* ----------------------------------------------------------------------------- */
double tl2_k_to_E(double kix, double kiy, double kiz, double kfx, double kfy, double kfz)
{
	const double k2_to_E = 2.0721247;  /* from codata values*/
	return k2_to_E * (kix*kix + kiy*kiy + kiz*kiz - kfx*kfx - kfy*kfy - kfz*kfz);
}
/* ----------------------------------------------------------------------------- */