aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 35f4b8868ab2c48505aad33b2cf23bc34f010093 (plain)
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
/*
 * utils.c
 *
 * Utility stuff
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *	    Gordon Ball <gfb21@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#include <math.h>
#include <gsl/gsl_matrix.h>

/* Return the MOST POSITIVE of two numbers */
unsigned int biggest(signed int a, signed int b) {
	if ( a>b ) {
		return a;
	}
	return b;
}

/* Return the LEAST POSITIVE of two numbers */
unsigned int smallest(signed int a, signed int b) {
	if ( a<b ) {
		return a;
	}
	return b;
}

double distance(double x1, double y1, double x2, double y2) {
	return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

double modulus(double x, double y, double z) {
	return sqrt(x*x + y*y + z*z);
}

/* Angle between two vectors.  Answer in degrees */
double angle_between(double x1, double y1, double z1, double x2, double y2, double z2) {

	double mod1 = modulus(x1, y1, z1);
	double mod2 = modulus(x2, y2, z2);
	return ((acos((x1*x2 + y1*y2 + z1*z2) / (mod1*mod2)))/M_PI) * 180;

}

/* Wavelength of an electron (in m) given accelerating potential (in V) */
double lambda(double V) {

	double m = 9.110E-31;
	double h = 6.625E-34;
	double e = 1.60E-19;
	double c = 2.998E8;
	
	return h / sqrt(2*m*e*V*(1+((e*V) / (2*m*c*c))));

}

/* Renormalise a gsl_matrix to 0->1
 * Re-written to use gsl_matrix native functions */
void matrix_renormalise(gsl_matrix *m) {
	
	double max,min;
	
	gsl_matrix_minmax(m,&min,&max);
	gsl_matrix_add_constant(m,0.-min);
	gsl_matrix_scale(m,1./(max-min));

}