00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <math.h>
00005 #include <assert.h>
00006 #include <unistd.h>
00007 #include <float.h>
00008 #include <limits.h>
00009
00010 #include "utils.h"
00011
00012 int *read_map(char *filename)
00013 {
00014 int n = 0;
00015 int *map = 0;
00016 char *str;
00017 FILE *file = fopen(filename, "r");
00018 if(!file) file_error(filename);
00019 while((str=fgetl(file))){
00020 ++n;
00021 map = realloc(map, n*sizeof(int));
00022 map[n-1] = atoi(str);
00023 }
00024 return map;
00025 }
00026
00027 void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections)
00028 {
00029 size_t i;
00030 for(i = 0; i < sections; ++i){
00031 size_t start = n*i/sections;
00032 size_t end = n*(i+1)/sections;
00033 size_t num = end-start;
00034 shuffle(arr+(start*size), num, size);
00035 }
00036 }
00037
00038 void shuffle(void *arr, size_t n, size_t size)
00039 {
00040 size_t i;
00041 void *swp = calloc(1, size);
00042 for(i = 0; i < n-1; ++i){
00043 size_t j = i + rand()/(RAND_MAX / (n-i)+1);
00044 memcpy(swp, arr+(j*size), size);
00045 memcpy(arr+(j*size), arr+(i*size), size);
00046 memcpy(arr+(i*size), swp, size);
00047 }
00048 }
00049
00050 void del_arg(int argc, char **argv, int index)
00051 {
00052 int i;
00053 for(i = index; i < argc-1; ++i) argv[i] = argv[i+1];
00054 argv[i] = 0;
00055 }
00056
00057 int find_arg(int argc, char* argv[], char *arg)
00058 {
00059 int i;
00060 for(i = 0; i < argc; ++i) {
00061 if(!argv[i]) continue;
00062 if(0==strcmp(argv[i], arg)) {
00063 del_arg(argc, argv, i);
00064 return 1;
00065 }
00066 }
00067 return 0;
00068 }
00069
00070 int find_int_arg(int argc, char **argv, char *arg, int def)
00071 {
00072 int i;
00073 for(i = 0; i < argc-1; ++i){
00074 if(!argv[i]) continue;
00075 if(0==strcmp(argv[i], arg)){
00076 def = atoi(argv[i+1]);
00077 del_arg(argc, argv, i);
00078 del_arg(argc, argv, i);
00079 break;
00080 }
00081 }
00082 return def;
00083 }
00084
00085 float find_float_arg(int argc, char **argv, char *arg, float def)
00086 {
00087 int i;
00088 for(i = 0; i < argc-1; ++i){
00089 if(!argv[i]) continue;
00090 if(0==strcmp(argv[i], arg)){
00091 def = atof(argv[i+1]);
00092 del_arg(argc, argv, i);
00093 del_arg(argc, argv, i);
00094 break;
00095 }
00096 }
00097 return def;
00098 }
00099
00100 char *find_char_arg(int argc, char **argv, char *arg, char *def)
00101 {
00102 int i;
00103 for(i = 0; i < argc-1; ++i){
00104 if(!argv[i]) continue;
00105 if(0==strcmp(argv[i], arg)){
00106 def = argv[i+1];
00107 del_arg(argc, argv, i);
00108 del_arg(argc, argv, i);
00109 break;
00110 }
00111 }
00112 return def;
00113 }
00114
00115
00116 char *basecfg(char *cfgfile)
00117 {
00118 char *c = cfgfile;
00119 char *next;
00120 while((next = strchr(c, '/')))
00121 {
00122 c = next+1;
00123 }
00124 c = copy_string(c);
00125 next = strchr(c, '.');
00126 if (next) *next = 0;
00127 return c;
00128 }
00129
00130 int alphanum_to_int(char c)
00131 {
00132 return (c < 58) ? c - 48 : c-87;
00133 }
00134 char int_to_alphanum(int i)
00135 {
00136 if (i == 36) return '.';
00137 return (i < 10) ? i + 48 : i + 87;
00138 }
00139
00140 void pm(int M, int N, float *A)
00141 {
00142 int i,j;
00143 for(i =0 ; i < M; ++i){
00144 printf("%d ", i+1);
00145 for(j = 0; j < N; ++j){
00146 printf("%2.4f, ", A[i*N+j]);
00147 }
00148 printf("\n");
00149 }
00150 printf("\n");
00151 }
00152
00153 void find_replace(char *str, char *orig, char *rep, char *output)
00154 {
00155 char buffer[4096] = {0};
00156 char *p;
00157
00158 sprintf(buffer, "%s", str);
00159 if(!(p = strstr(buffer, orig))){
00160 sprintf(output, "%s", str);
00161 return;
00162 }
00163
00164 *p = '\0';
00165
00166 sprintf(output, "%s%s%s", buffer, rep, p+strlen(orig));
00167 }
00168
00169 float sec(clock_t clocks)
00170 {
00171 return (float)clocks/CLOCKS_PER_SEC;
00172 }
00173
00174 void top_k(float *a, int n, int k, int *index)
00175 {
00176 int i,j;
00177 for(j = 0; j < k; ++j) index[j] = -1;
00178 for(i = 0; i < n; ++i){
00179 int curr = i;
00180 for(j = 0; j < k; ++j){
00181 if((index[j] < 0) || a[curr] > a[index[j]]){
00182 int swap = curr;
00183 curr = index[j];
00184 index[j] = swap;
00185 }
00186 }
00187 }
00188 }
00189
00190 void error(const char *s)
00191 {
00192 perror(s);
00193 assert(0);
00194 exit(-1);
00195 }
00196
00197 void malloc_error()
00198 {
00199 fprintf(stderr, "Malloc error\n");
00200 exit(-1);
00201 }
00202
00203 void file_error(char *s)
00204 {
00205 fprintf(stderr, "Couldn't open file: %s\n", s);
00206 exit(0);
00207 }
00208
00209 list *split_str(char *s, char delim)
00210 {
00211 size_t i;
00212 size_t len = strlen(s);
00213 list *l = make_list();
00214 list_insert(l, s);
00215 for(i = 0; i < len; ++i){
00216 if(s[i] == delim){
00217 s[i] = '\0';
00218 list_insert(l, &(s[i+1]));
00219 }
00220 }
00221 return l;
00222 }
00223
00224 void strip(char *s)
00225 {
00226 size_t i;
00227 size_t len = strlen(s);
00228 size_t offset = 0;
00229 for(i = 0; i < len; ++i){
00230 char c = s[i];
00231 if(c==' '||c=='\t'||c=='\n') ++offset;
00232 else s[i-offset] = c;
00233 }
00234 s[len-offset] = '\0';
00235 }
00236
00237 void strip_char(char *s, char bad)
00238 {
00239 size_t i;
00240 size_t len = strlen(s);
00241 size_t offset = 0;
00242 for(i = 0; i < len; ++i){
00243 char c = s[i];
00244 if(c==bad) ++offset;
00245 else s[i-offset] = c;
00246 }
00247 s[len-offset] = '\0';
00248 }
00249
00250 void free_ptrs(void **ptrs, int n)
00251 {
00252 int i;
00253 for(i = 0; i < n; ++i) free(ptrs[i]);
00254 free(ptrs);
00255 }
00256
00257 char *fgetl(FILE *fp)
00258 {
00259 if(feof(fp)) return 0;
00260 size_t size = 512;
00261 char *line = malloc(size*sizeof(char));
00262 if(!fgets(line, size, fp)){
00263 free(line);
00264 return 0;
00265 }
00266
00267 size_t curr = strlen(line);
00268
00269 while((line[curr-1] != '\n') && !feof(fp)){
00270 if(curr == size-1){
00271 size *= 2;
00272 line = realloc(line, size*sizeof(char));
00273 if(!line) {
00274 printf("%ld\n", size);
00275 malloc_error();
00276 }
00277 }
00278 size_t readsize = size-curr;
00279 if(readsize > INT_MAX) readsize = INT_MAX-1;
00280 fgets(&line[curr], readsize, fp);
00281 curr = strlen(line);
00282 }
00283 if(line[curr-1] == '\n') line[curr-1] = '\0';
00284
00285 return line;
00286 }
00287
00288 int read_int(int fd)
00289 {
00290 int n = 0;
00291 int next = read(fd, &n, sizeof(int));
00292 if(next <= 0) return -1;
00293 return n;
00294 }
00295
00296 void write_int(int fd, int n)
00297 {
00298 int next = write(fd, &n, sizeof(int));
00299 if(next <= 0) error("read failed");
00300 }
00301
00302 int read_all_fail(int fd, char *buffer, size_t bytes)
00303 {
00304 size_t n = 0;
00305 while(n < bytes){
00306 int next = read(fd, buffer + n, bytes-n);
00307 if(next <= 0) return 1;
00308 n += next;
00309 }
00310 return 0;
00311 }
00312
00313 int write_all_fail(int fd, char *buffer, size_t bytes)
00314 {
00315 size_t n = 0;
00316 while(n < bytes){
00317 size_t next = write(fd, buffer + n, bytes-n);
00318 if(next <= 0) return 1;
00319 n += next;
00320 }
00321 return 0;
00322 }
00323
00324 void read_all(int fd, char *buffer, size_t bytes)
00325 {
00326 size_t n = 0;
00327 while(n < bytes){
00328 int next = read(fd, buffer + n, bytes-n);
00329 if(next <= 0) error("read failed");
00330 n += next;
00331 }
00332 }
00333
00334 void write_all(int fd, char *buffer, size_t bytes)
00335 {
00336 size_t n = 0;
00337 while(n < bytes){
00338 size_t next = write(fd, buffer + n, bytes-n);
00339 if(next <= 0) error("write failed");
00340 n += next;
00341 }
00342 }
00343
00344
00345 char *copy_string(char *s)
00346 {
00347 char *copy = malloc(strlen(s)+1);
00348 strncpy(copy, s, strlen(s)+1);
00349 return copy;
00350 }
00351
00352 list *parse_csv_line(char *line)
00353 {
00354 list *l = make_list();
00355 char *c, *p;
00356 int in = 0;
00357 for(c = line, p = line; *c != '\0'; ++c){
00358 if(*c == '"') in = !in;
00359 else if(*c == ',' && !in){
00360 *c = '\0';
00361 list_insert(l, copy_string(p));
00362 p = c+1;
00363 }
00364 }
00365 list_insert(l, copy_string(p));
00366 return l;
00367 }
00368
00369 int count_fields(char *line)
00370 {
00371 int count = 0;
00372 int done = 0;
00373 char *c;
00374 for(c = line; !done; ++c){
00375 done = (*c == '\0');
00376 if(*c == ',' || done) ++count;
00377 }
00378 return count;
00379 }
00380
00381 float *parse_fields(char *line, int n)
00382 {
00383 float *field = calloc(n, sizeof(float));
00384 char *c, *p, *end;
00385 int count = 0;
00386 int done = 0;
00387 for(c = line, p = line; !done; ++c){
00388 done = (*c == '\0');
00389 if(*c == ',' || done){
00390 *c = '\0';
00391 field[count] = strtod(p, &end);
00392 if(p == c) field[count] = nan("");
00393 if(end != c && (end != c-1 || *end != '\r')) field[count] = nan("");
00394 p = c+1;
00395 ++count;
00396 }
00397 }
00398 return field;
00399 }
00400
00401 float sum_array(float *a, int n)
00402 {
00403 int i;
00404 float sum = 0;
00405 for(i = 0; i < n; ++i) sum += a[i];
00406 return sum;
00407 }
00408
00409 float mean_array(float *a, int n)
00410 {
00411 return sum_array(a,n)/n;
00412 }
00413
00414 void mean_arrays(float **a, int n, int els, float *avg)
00415 {
00416 int i;
00417 int j;
00418 memset(avg, 0, els*sizeof(float));
00419 for(j = 0; j < n; ++j){
00420 for(i = 0; i < els; ++i){
00421 avg[i] += a[j][i];
00422 }
00423 }
00424 for(i = 0; i < els; ++i){
00425 avg[i] /= n;
00426 }
00427 }
00428
00429 void print_statistics(float *a, int n)
00430 {
00431 float m = mean_array(a, n);
00432 float v = variance_array(a, n);
00433 printf("MSE: %.6f, Mean: %.6f, Variance: %.6f\n", mse_array(a, n), m, v);
00434 }
00435
00436 float variance_array(float *a, int n)
00437 {
00438 int i;
00439 float sum = 0;
00440 float mean = mean_array(a, n);
00441 for(i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i]-mean);
00442 float variance = sum/n;
00443 return variance;
00444 }
00445
00446 int constrain_int(int a, int min, int max)
00447 {
00448 if (a < min) return min;
00449 if (a > max) return max;
00450 return a;
00451 }
00452
00453 float constrain(float min, float max, float a)
00454 {
00455 if (a < min) return min;
00456 if (a > max) return max;
00457 return a;
00458 }
00459
00460 float dist_array(float *a, float *b, int n, int sub)
00461 {
00462 int i;
00463 float sum = 0;
00464 for(i = 0; i < n; i += sub) sum += pow(a[i]-b[i], 2);
00465 return sqrt(sum);
00466 }
00467
00468 float mse_array(float *a, int n)
00469 {
00470 int i;
00471 float sum = 0;
00472 for(i = 0; i < n; ++i) sum += a[i]*a[i];
00473 return sqrt(sum/n);
00474 }
00475
00476 void normalize_array(float *a, int n)
00477 {
00478 int i;
00479 float mu = mean_array(a,n);
00480 float sigma = sqrt(variance_array(a,n));
00481 for(i = 0; i < n; ++i){
00482 a[i] = (a[i] - mu)/sigma;
00483 }
00484 mu = mean_array(a,n);
00485 sigma = sqrt(variance_array(a,n));
00486 }
00487
00488 void translate_array(float *a, int n, float s)
00489 {
00490 int i;
00491 for(i = 0; i < n; ++i){
00492 a[i] += s;
00493 }
00494 }
00495
00496 float mag_array(float *a, int n)
00497 {
00498 int i;
00499 float sum = 0;
00500 for(i = 0; i < n; ++i){
00501 sum += a[i]*a[i];
00502 }
00503 return sqrt(sum);
00504 }
00505
00506 void scale_array(float *a, int n, float s)
00507 {
00508 int i;
00509 for(i = 0; i < n; ++i){
00510 a[i] *= s;
00511 }
00512 }
00513
00514 int sample_array(float *a, int n)
00515 {
00516 float sum = sum_array(a, n);
00517 scale_array(a, n, 1./sum);
00518 float r = rand_uniform(0, 1);
00519 int i;
00520 for(i = 0; i < n; ++i){
00521 r = r - a[i];
00522 if (r <= 0) return i;
00523 }
00524 return n-1;
00525 }
00526
00527 int max_index(float *a, int n)
00528 {
00529 if(n <= 0) return -1;
00530 int i, max_i = 0;
00531 float max = a[0];
00532 for(i = 1; i < n; ++i){
00533 if(a[i] > max){
00534 max = a[i];
00535 max_i = i;
00536 }
00537 }
00538 return max_i;
00539 }
00540
00541 int rand_int(int min, int max)
00542 {
00543 if (max < min){
00544 int s = min;
00545 min = max;
00546 max = s;
00547 }
00548 int r = (rand()%(max - min + 1)) + min;
00549 return r;
00550 }
00551
00552
00553 float rand_normal()
00554 {
00555 static int haveSpare = 0;
00556 static double rand1, rand2;
00557
00558 if(haveSpare)
00559 {
00560 haveSpare = 0;
00561 return sqrt(rand1) * sin(rand2);
00562 }
00563
00564 haveSpare = 1;
00565
00566 rand1 = rand() / ((double) RAND_MAX);
00567 if(rand1 < 1e-100) rand1 = 1e-100;
00568 rand1 = -2 * log(rand1);
00569 rand2 = (rand() / ((double) RAND_MAX)) * TWO_PI;
00570
00571 return sqrt(rand1) * cos(rand2);
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 size_t rand_size_t()
00586 {
00587 return ((size_t)(rand()&0xff) << 56) |
00588 ((size_t)(rand()&0xff) << 48) |
00589 ((size_t)(rand()&0xff) << 40) |
00590 ((size_t)(rand()&0xff) << 32) |
00591 ((size_t)(rand()&0xff) << 24) |
00592 ((size_t)(rand()&0xff) << 16) |
00593 ((size_t)(rand()&0xff) << 8) |
00594 ((size_t)(rand()&0xff) << 0);
00595 }
00596
00597 float rand_uniform(float min, float max)
00598 {
00599 if(max < min){
00600 float swap = min;
00601 min = max;
00602 max = swap;
00603 }
00604 return ((float)rand()/RAND_MAX * (max - min)) + min;
00605 }
00606
00607 float rand_scale(float s)
00608 {
00609 float scale = rand_uniform(1, s);
00610 if(rand()%2) return scale;
00611 return 1./scale;
00612 }
00613
00614 float **one_hot_encode(float *a, int n, int k)
00615 {
00616 int i;
00617 float **t = calloc(n, sizeof(float*));
00618 for(i = 0; i < n; ++i){
00619 t[i] = calloc(k, sizeof(float));
00620 int index = (int)a[i];
00621 t[i][index] = 1;
00622 }
00623 return t;
00624 }
00625