arrays.c (4563B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <math.h> 4 5 #define DEG2RAD(x) (x*M_PI/180.0) 6 7 /* Translate a i,j,k index in grid with dimensions nx, ny, nz into a linear 8 * index */ 9 unsigned int 10 idx3(const unsigned int i, 11 const unsigned int j, 12 const unsigned int k, 13 const unsigned int nx, 14 const unsigned int ny) 15 { 16 return i + nx*j + nx*ny*k; 17 } 18 19 /* Translate a i,j,k index in grid with dimensions nx, ny, nz and a padding of 20 * single ghost nodes into a linear index */ 21 unsigned int 22 idx3g(const unsigned int i, 23 const unsigned int j, 24 const unsigned int k, 25 const unsigned int nx, 26 const unsigned int ny) 27 { 28 return i+1 + (nx+2)*(j+1) + (nx+2)*(ny+2)*(k+1); 29 } 30 31 /* Translate a i,j index in grid with dimensions nx, ny into a linear index */ 32 unsigned int 33 idx2(const unsigned int i, const unsigned int j, const unsigned int nx) 34 { 35 return i + nx*j; 36 } 37 38 /* Translate a i,j index in grid with dimensions nx, ny and a padding of single 39 * ghost nodes into a linear index */ 40 unsigned int 41 idx2g(const unsigned int i, const unsigned int j, const unsigned int nx) 42 { 43 return i+1 + (nx+2)*(j+1); 44 } 45 46 /* Translate a i index in grid with a padding of single into a linear index */ 47 unsigned int 48 idx1g(const unsigned int i) 49 { 50 return i+1; 51 } 52 53 /* Return an array of `n` linearly spaced values in the range [lower; upper] */ 54 double* 55 linspace(const double lower, const double upper, const int n) 56 { 57 int i; 58 double *x; 59 double dx; 60 61 x = malloc(n*sizeof(double)); 62 dx = (upper - lower)/(double)(n-1); 63 for (i=0; i<n; ++i) 64 x[i] = lower + dx*i; 65 return x; 66 } 67 68 /* Return an array of `n-1` values with the intervals between `x` values */ 69 double* 70 spacing(const double* x, const int n) 71 { 72 int i; 73 double *dx; 74 75 dx = malloc((n-1)*sizeof(double)); 76 for (i=0; i<n-1; ++i) 77 dx[i] = x[i+1] - x[i]; 78 return dx; 79 } 80 81 /* Return an array of `n` values with the value 0.0 */ 82 double* 83 zeros(const int n) 84 { 85 int i; 86 double *x; 87 88 x = malloc(n*sizeof(double)); 89 for (i=0; i<n; ++i) 90 x[i] = 0.0; 91 return x; 92 } 93 94 /* Return an array of `n` values with the value 1.0 */ 95 double* 96 ones(const int n) 97 { 98 int i; 99 double *x; 100 101 x = malloc(n*sizeof(double)); 102 for (i=0; i<n; ++i) 103 x[i] = 1.0; 104 return x; 105 } 106 107 /* Return an array of `n` values with a specified value */ 108 double* 109 initval(const double value, const int n) 110 { 111 int i; 112 double *x; 113 114 x = malloc(n*sizeof(double)); 115 for (i=0; i<n; ++i) 116 x[i] = value; 117 return x; 118 } 119 120 /* Return an array of `n` uninitialized values */ 121 double* 122 empty(const int n) 123 { 124 return malloc(n*sizeof(double)); 125 } 126 127 /* Return largest value in array `a` with size `n` */ 128 double 129 max(const double* a, const int n) 130 { 131 int i; 132 double maxval; 133 134 maxval = -INFINITY; 135 for (i=0; i<n; ++i) 136 if (a[i] > maxval) 137 maxval = a[i]; 138 return maxval; 139 } 140 141 /* Return smallest value in array `a` with size `n` */ 142 double 143 min(const double* a, const int n) 144 { 145 int i; 146 double minval; 147 148 minval = +INFINITY; 149 for (i=0; i<n; ++i) 150 if (a[i] < minval) 151 minval = a[i]; 152 return minval; 153 } 154 155 void 156 print_array(const double* a, const int n) 157 { 158 int i; 159 for (i=0; i<n; ++i) 160 printf("%.17g\n", a[i]); 161 } 162 163 void 164 print_arrays(const double* a, const double* b, const int n) 165 { 166 int i; 167 for (i=0; i<n; ++i) 168 printf("%.17g\t%.17g\n", a[i], b[i]); 169 } 170 171 void 172 print_arrays_2nd_normalized(const double* a, const double* b, const int n) 173 { 174 int i; 175 double max_b; 176 177 max_b = max(b, n); 178 for (i=0; i<n; ++i) 179 printf("%.17g\t%.17g\n", a[i], b[i]/max_b); 180 } 181 182 void 183 print_three_arrays(const double* a, 184 const double* b, 185 const double* c, 186 const int n) 187 { 188 int i; 189 for (i=0; i<n; ++i) 190 printf("%.17g\t%.17g\t%.17g\n", a[i], b[i], c[i]); 191 } 192 193 void 194 fprint_arrays(FILE* fp, const double* a, const double* b, const int n) 195 { 196 int i; 197 for (i=0; i<n; ++i) 198 fprintf(fp, "%.17g\t%.17g\n", a[i], b[i]); 199 } 200 201 void 202 fprint_three_arrays(FILE* fp, 203 const double* a, 204 const double* b, 205 const double* c, 206 const int n) 207 { 208 int i; 209 for (i=0; i<n; ++i) 210 fprintf(fp, "%.17g\t%.17g\t%.17g\n", a[i], b[i], c[i]); 211 } 212 213 void 214 copy_values(const double* in, double* out, const int n) 215 { 216 int i; 217 for (i=0; i<n; ++i) 218 out[i] = in[i]; 219 } 220 221 double* 222 copy(const double* in, const int n) 223 { 224 double *out; 225 out = empty(n); 226 copy_values(in, out, n); 227 return out; 228 } 229 230 double* 231 normalize(const double* in, const int n) 232 { 233 int i; 234 double max_val; 235 double *out; 236 237 out = malloc(n*sizeof(double)); 238 copy_values(in, out, n); 239 max_val = max(out, n); 240 241 if (max_val == 0.0) 242 max_val = 1.0; 243 244 for (i=0; i<n; ++i) 245 out[i] /= max_val; 246 return out; 247 }