10int rcomp(
const void* a,
const void* b);
12 double S,
double fa,
double fb,
double fc,
23 return x - y * floor( x / y );
50 xyz[0] = rpz[0] * c - rpz[4] * s;
51 xyz[4] = rpz[0] * s + rpz[4] * c;
55 temp[0] = rpz[1] * c - rpz[5] * s;
56 temp[1] = rpz[2] * c - rpz[6] * s;
57 temp[2] = rpz[3] * c - rpz[7] * s;
60 xyz[1] = temp[0] * c - temp[1] * s / r + (rpz[0]*s + rpz[4]*c) * s / r;
61 xyz[2] = temp[0] * s + temp[1] * c / r - (rpz[0]*s + rpz[4]*c) * c / r;
65 temp[0] = rpz[1] * s + rpz[5] * c;
66 temp[1] = rpz[2] * s + rpz[6] * c;
67 temp[2] = rpz[3] * s + rpz[7] * c;
70 xyz[5] = temp[0] * c - temp[1] * s / r + (rpz[0]*c - rpz[4]*s) * s / r;
71 xyz[6] = temp[0] * s + temp[1] * c / r - (rpz[0]*c - rpz[4]*s) * c / r;
80 xyz[9] = temp[0] * c - temp[1] * s / r;
81 xyz[10] = temp[0] * s + temp[1] * c / r;
109 rpz[0] = xyz[0] * c + xyz[4] * s;
110 rpz[4] = -xyz[0] * s + xyz[4] * c;
114 temp[0] = xyz[1] * c + xyz[5] * s;
115 temp[1] = xyz[2] * c + xyz[6] * s;
116 temp[2] = xyz[3] * c + xyz[7] * s;
119 rpz[1] = temp[0] * c + temp[1] * s;
120 rpz[2] = -temp[0] * s * r + temp[1] * c * r
121 - xyz[0] * s + xyz[4] * c;
125 temp[0] = -xyz[1] * s + xyz[5] * c;
126 temp[1] = -xyz[2] * s + xyz[6] * c;
127 temp[2] = -xyz[3] * s + xyz[7] * c;
130 rpz[5] = temp[0] * c + temp[1] * s;
131 rpz[6] = -temp[0] * s * r + temp[1] * c * r
132 - xyz[0] * c - xyz[4] * s;
141 rpz[9] = temp[0] * c + temp[1] * s;
142 rpz[10] = -temp[0] * s * r + temp[1] * c * r;
161 for (
int i = 0; i < d1; i=i+1) {
162 for (
int j = 0; j < d3; j=j+1) {
164 for (
int k = 0; k < d2; k=k+1){
165 sum = sum + matA[k * d1 + i]*matB[j * d2 + k];
167 matC[i * d3 + j] = sum;
192 v1 = drand48() * 2 - 1;
193 v2 = drand48() * 2 - 1;
197 X = v1 * sqrt(-2*log(s) / s);
212 for(
int i = 0; i < p; i++) {
232double math_simpson(
double (*f)(
double),
double a,
double b,
double eps) {
233 double c = (a + b)/2, h = b - a;
234 double fa = f(a), fb = f(b), fc = f(c);
235 double S = (h/6)*(fa + 4*fc + fb);
255 real d = (b-a)/(n-1);
257 for(i = 0; i < n; i++) {
278 for(
int i=0; i<n; i++) {
284 for(
int i=0; i<n; i++) {
288 for(
int j=0; j<n_unique; j++) {
289 if(test == unique[j]) {
296 unique[n_unique] = test;
325 real x = q[0], y = q[1], z = q[2];
326 real x1 = t1[0], y1 = t1[1], z1 = t1[2];
327 real x2 = t2[0], y2 = t2[1], z2 = t2[2];
328 real x3 = t3[0], y3 = t3[1], z3 = t3[2];
334 x3-x1, y3-y1, z3-z1) != 0.0){
340 x -x3, y -y3, z -z3) != 0.0){
381int rcomp(
const void* a,
const void* b) {
386 if (a_val >= b_val && a_val < c_val) {
389 return a_val > b_val ? 1 : -1;
406 return (
real*) bsearch(&key, base, num-1,
sizeof(
real),
rcomp);
431 for(i = 0; i < n - 1; i++) {
433 real z2 = zv[i+1] - z;
435 real r2 = rv[i+1] - r;
437 real ri = r1 + (z1*(r2-r1)) / (z1-z2);
450 double S,
double fa,
double fb,
double fc,
452 double c = (a + b)/2, h = b - a;
453 double d = (a + c)/2, e = (c + b)/2;
454 double fd = f(d), fe = f(e);
455 double Sleft = (h/12)*(fa + 4*fd + fc);
456 double Sright = (h/12)*(fc + 4*fe + fb);
457 double S2 = Sleft + Sright;
458 if (bottom <= 0 || fabs(S2 - S) <= eps*fabs(S)) {
459 return S2 + (S2 - S)/15;
Main header file for ASCOT5.
void math_uniquecount(int *in, int *unique, int *count, int n)
Find unique numbers and their frequency in given array.
double math_simpson(double(*f)(double), double a, double b, double eps)
Adaptive Simpsons rule for integral.
int math_ipow(int a, int p)
Calculate a^p where both a and p are integers (p >= 0)
int math_point_in_polygon(real r, real z, real *rv, real *zv, int n)
Check if coordinates are within polygon.
void math_matmul(real *matA, real *matB, int d1, int d2, int d3, real *matC)
Matrix multiplication.
real fmod(real x, real y)
Compute the modulus of two real numbers.
int rcomp(const void *a, const void *b)
Helper comparison routine for "math_rsearch".
void math_barycentric_coords_triangle(real AP[3], real AB[3], real AC[3], real n[3], real *s, real *t)
Find barycentric coordinates for a given point.
real * math_rsearch(const real key, const real *base, int num)
Search for array element preceding a key value.
double math_simpson_helper(double(*f)(double), double a, double b, double eps, double S, double fa, double fb, double fc, int bottom)
Helper routine for "math_simpson".
void math_linspace(real *vec, real a, real b, int n)
Generate linearly spaced vector.
int math_point_on_plane(real q[3], real t1[3], real t2[3], real t3[3])
Find if a point is on a given plane.
void math_jac_xyz2rpz(real *xyz, real *rpz, real r, real phi)
Convert a Jacobian from cartesian to cylindrical coordinates.
real math_normal_rand(void)
Generate normally distributed random numbers.
void math_jac_rpz2xyz(real *rpz, real *xyz, real r, real phi)
Convert a Jacobian from cylindrical to cartesian coordinates.
#define math_determinant3x3(x1, x2, x3, y1, y2, y3, z1, z2, z3)
Direct expansion of 3x3 matrix determinant.
#define math_unit(a, b)
Calculate unit vector b from a 3D vector a.
#define math_maxSimpsonDepth
Maximum recursion depth for the simpson integral rule.
#define math_scalar_triple_product(a, b, c)
the mixed or triple product of three vectors (a cross b) dot c mathematica: a={a0,...