| | |
| | | #include "utils.h" |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <math.h> |
| | | #include <unistd.h> |
| | | #include <float.h> |
| | | #include <limits.h> |
| | | |
| | | void error(char *s) |
| | | #include "utils.h" |
| | | |
| | | void del_arg(int argc, char **argv, int index) |
| | | { |
| | | fprintf(stderr, "Error: %s\n", s); |
| | | int i; |
| | | for(i = index; i < argc-1; ++i) argv[i] = argv[i+1]; |
| | | argv[i] = 0; |
| | | } |
| | | |
| | | int find_arg(int argc, char* argv[], char *arg) |
| | | { |
| | | int i; |
| | | for(i = 0; i < argc; ++i) { |
| | | if(!argv[i]) continue; |
| | | if(0==strcmp(argv[i], arg)) { |
| | | del_arg(argc, argv, i); |
| | | return 1; |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | int find_int_arg(int argc, char **argv, char *arg, int def) |
| | | { |
| | | int i; |
| | | for(i = 0; i < argc-1; ++i){ |
| | | if(!argv[i]) continue; |
| | | if(0==strcmp(argv[i], arg)){ |
| | | def = atoi(argv[i+1]); |
| | | del_arg(argc, argv, i); |
| | | del_arg(argc, argv, i); |
| | | break; |
| | | } |
| | | } |
| | | return def; |
| | | } |
| | | |
| | | float find_float_arg(int argc, char **argv, char *arg, float def) |
| | | { |
| | | int i; |
| | | for(i = 0; i < argc-1; ++i){ |
| | | if(!argv[i]) continue; |
| | | if(0==strcmp(argv[i], arg)){ |
| | | def = atof(argv[i+1]); |
| | | del_arg(argc, argv, i); |
| | | del_arg(argc, argv, i); |
| | | break; |
| | | } |
| | | } |
| | | return def; |
| | | } |
| | | |
| | | char *find_char_arg(int argc, char **argv, char *arg, char *def) |
| | | { |
| | | int i; |
| | | for(i = 0; i < argc-1; ++i){ |
| | | if(!argv[i]) continue; |
| | | if(0==strcmp(argv[i], arg)){ |
| | | def = argv[i+1]; |
| | | del_arg(argc, argv, i); |
| | | del_arg(argc, argv, i); |
| | | break; |
| | | } |
| | | } |
| | | return def; |
| | | } |
| | | |
| | | |
| | | char *basecfg(char *cfgfile) |
| | | { |
| | | char *c = cfgfile; |
| | | char *next; |
| | | while((next = strchr(c, '/'))) |
| | | { |
| | | c = next+1; |
| | | } |
| | | c = copy_string(c); |
| | | next = strchr(c, '.'); |
| | | if (next) *next = 0; |
| | | return c; |
| | | } |
| | | |
| | | int alphanum_to_int(char c) |
| | | { |
| | | return (c < 58) ? c - 48 : c-87; |
| | | } |
| | | char int_to_alphanum(int i) |
| | | { |
| | | if (i == 36) return '.'; |
| | | return (i < 10) ? i + 48 : i + 87; |
| | | } |
| | | |
| | | void pm(int M, int N, float *A) |
| | | { |
| | | int i,j; |
| | | for(i =0 ; i < M; ++i){ |
| | | printf("%d ", i+1); |
| | | for(j = 0; j < N; ++j){ |
| | | printf("%10.6f, ", A[i*N+j]); |
| | | } |
| | | printf("\n"); |
| | | } |
| | | printf("\n"); |
| | | } |
| | | |
| | | |
| | | char *find_replace(char *str, char *orig, char *rep) |
| | | { |
| | | static char buffer[4096]; |
| | | char *p; |
| | | |
| | | if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'? |
| | | return str; |
| | | |
| | | strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$ |
| | | buffer[p-str] = '\0'; |
| | | |
| | | sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig)); |
| | | |
| | | return buffer; |
| | | } |
| | | |
| | | float sec(clock_t clocks) |
| | | { |
| | | return (float)clocks/CLOCKS_PER_SEC; |
| | | } |
| | | |
| | | void top_k(float *a, int n, int k, int *index) |
| | | { |
| | | int i,j; |
| | | for(j = 0; j < k; ++j) index[j] = -1; |
| | | for(i = 0; i < n; ++i){ |
| | | int curr = i; |
| | | for(j = 0; j < k; ++j){ |
| | | if((index[j] < 0) || a[curr] > a[index[j]]){ |
| | | int swap = curr; |
| | | curr = index[j]; |
| | | index[j] = swap; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void error(const char *s) |
| | | { |
| | | perror(s); |
| | | exit(0); |
| | | } |
| | | |
| | |
| | | |
| | | list *split_str(char *s, char delim) |
| | | { |
| | | int i; |
| | | int len = strlen(s); |
| | | size_t i; |
| | | size_t len = strlen(s); |
| | | list *l = make_list(); |
| | | list_insert(l, s); |
| | | for(i = 0; i < len; ++i){ |
| | |
| | | |
| | | void strip(char *s) |
| | | { |
| | | int i; |
| | | int len = strlen(s); |
| | | int offset = 0; |
| | | size_t i; |
| | | size_t len = strlen(s); |
| | | size_t offset = 0; |
| | | for(i = 0; i < len; ++i){ |
| | | char c = s[i]; |
| | | if(c==' '||c=='\t'||c=='\n') ++offset; |
| | |
| | | |
| | | void strip_char(char *s, char bad) |
| | | { |
| | | int i; |
| | | int len = strlen(s); |
| | | int offset = 0; |
| | | size_t i; |
| | | size_t len = strlen(s); |
| | | size_t offset = 0; |
| | | for(i = 0; i < len; ++i){ |
| | | char c = s[i]; |
| | | if(c==bad) ++offset; |
| | |
| | | char *fgetl(FILE *fp) |
| | | { |
| | | if(feof(fp)) return 0; |
| | | int size = 512; |
| | | size_t size = 512; |
| | | char *line = malloc(size*sizeof(char)); |
| | | if(!fgets(line, size, fp)){ |
| | | free(line); |
| | | return 0; |
| | | } |
| | | |
| | | int curr = strlen(line); |
| | | |
| | | while(line[curr-1]!='\n'){ |
| | | size *= 2; |
| | | line = realloc(line, size*sizeof(char)); |
| | | if(!line) malloc_error(); |
| | | fgets(&line[curr], size-curr, fp); |
| | | size_t curr = strlen(line); |
| | | |
| | | while((line[curr-1] != '\n') && !feof(fp)){ |
| | | if(curr == size-1){ |
| | | size *= 2; |
| | | line = realloc(line, size*sizeof(char)); |
| | | if(!line) { |
| | | printf("%ld\n", size); |
| | | malloc_error(); |
| | | } |
| | | } |
| | | size_t readsize = size-curr; |
| | | if(readsize > INT_MAX) readsize = INT_MAX-1; |
| | | fgets(&line[curr], readsize, fp); |
| | | curr = strlen(line); |
| | | } |
| | | line[curr-1] = '\0'; |
| | | if(line[curr-1] == '\n') line[curr-1] = '\0'; |
| | | |
| | | return line; |
| | | } |
| | | |
| | | void read_all(int fd, char *buffer, size_t bytes) |
| | | { |
| | | size_t n = 0; |
| | | while(n < bytes){ |
| | | int next = read(fd, buffer + n, bytes-n); |
| | | if(next <= 0) error("read failed"); |
| | | n += next; |
| | | } |
| | | } |
| | | |
| | | void write_all(int fd, char *buffer, size_t bytes) |
| | | { |
| | | size_t n = 0; |
| | | while(n < bytes){ |
| | | size_t next = write(fd, buffer + n, bytes-n); |
| | | if(next <= 0) error("write failed"); |
| | | n += next; |
| | | } |
| | | } |
| | | |
| | | |
| | | char *copy_string(char *s) |
| | | { |
| | | char *copy = malloc(strlen(s)+1); |
| | |
| | | |
| | | int count_fields(char *line) |
| | | { |
| | | int count = 0; |
| | | int done = 0; |
| | | int count = 0; |
| | | int done = 0; |
| | | char *c; |
| | | for(c = line; !done; ++c){ |
| | | done = (*c == '\0'); |
| | | if(*c == ',' || done) ++count; |
| | | } |
| | | return count; |
| | | for(c = line; !done; ++c){ |
| | | done = (*c == '\0'); |
| | | if(*c == ',' || done) ++count; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | double *parse_fields(char *line, int n) |
| | | float *parse_fields(char *line, int n) |
| | | { |
| | | double *field = calloc(n, sizeof(double)); |
| | | char *c, *p, *end; |
| | | int count = 0; |
| | | int done = 0; |
| | | for(c = line, p = line; !done; ++c){ |
| | | done = (*c == '\0'); |
| | | if(*c == ',' || done){ |
| | | *c = '\0'; |
| | | field[count] = strtod(p, &end); |
| | | if(p == c) field[count] = nan(""); |
| | | if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats! |
| | | p = c+1; |
| | | ++count; |
| | | } |
| | | } |
| | | return field; |
| | | float *field = calloc(n, sizeof(float)); |
| | | char *c, *p, *end; |
| | | int count = 0; |
| | | int done = 0; |
| | | for(c = line, p = line; !done; ++c){ |
| | | done = (*c == '\0'); |
| | | if(*c == ',' || done){ |
| | | *c = '\0'; |
| | | field[count] = strtod(p, &end); |
| | | if(p == c) field[count] = nan(""); |
| | | if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats! |
| | | p = c+1; |
| | | ++count; |
| | | } |
| | | } |
| | | return field; |
| | | } |
| | | |
| | | double mean_array(double *a, int n) |
| | | float sum_array(float *a, int n) |
| | | { |
| | | int i; |
| | | double sum = 0; |
| | | float sum = 0; |
| | | for(i = 0; i < n; ++i) sum += a[i]; |
| | | return sum/n; |
| | | return sum; |
| | | } |
| | | |
| | | double variance_array(double *a, int n) |
| | | float mean_array(float *a, int n) |
| | | { |
| | | return sum_array(a,n)/n; |
| | | } |
| | | |
| | | float variance_array(float *a, int n) |
| | | { |
| | | int i; |
| | | double sum = 0; |
| | | double mean = mean_array(a, n); |
| | | float sum = 0; |
| | | float mean = mean_array(a, n); |
| | | for(i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i]-mean); |
| | | double variance = sum/n; |
| | | float variance = sum/n; |
| | | return variance; |
| | | } |
| | | |
| | | double constrain(double a, double max) |
| | | float constrain(float min, float max, float a) |
| | | { |
| | | if(a > abs(max)) return abs(max); |
| | | if(a < -abs(max)) return -abs(max); |
| | | if (a < min) return min; |
| | | if (a > max) return max; |
| | | return a; |
| | | } |
| | | |
| | | void normalize_array(double *a, int n) |
| | | float mse_array(float *a, int n) |
| | | { |
| | | int i; |
| | | double mu = mean_array(a,n); |
| | | double sigma = sqrt(variance_array(a,n)); |
| | | float sum = 0; |
| | | for(i = 0; i < n; ++i) sum += a[i]*a[i]; |
| | | return sqrt(sum/n); |
| | | } |
| | | |
| | | void normalize_array(float *a, int n) |
| | | { |
| | | int i; |
| | | float mu = mean_array(a,n); |
| | | float sigma = sqrt(variance_array(a,n)); |
| | | for(i = 0; i < n; ++i){ |
| | | a[i] = (a[i] - mu)/sigma; |
| | | } |
| | |
| | | sigma = sqrt(variance_array(a,n)); |
| | | } |
| | | |
| | | void translate_array(double *a, int n, double s) |
| | | void translate_array(float *a, int n, float s) |
| | | { |
| | | int i; |
| | | for(i = 0; i < n; ++i){ |
| | |
| | | } |
| | | } |
| | | |
| | | void scale_array(double *a, int n, double s) |
| | | float mag_array(float *a, int n) |
| | | { |
| | | int i; |
| | | float sum = 0; |
| | | for(i = 0; i < n; ++i){ |
| | | sum += a[i]*a[i]; |
| | | } |
| | | return sqrt(sum); |
| | | } |
| | | |
| | | void scale_array(float *a, int n, float s) |
| | | { |
| | | int i; |
| | | for(i = 0; i < n; ++i){ |
| | | a[i] *= s; |
| | | } |
| | | } |
| | | int max_index(double *a, int n) |
| | | |
| | | int max_index(float *a, int n) |
| | | { |
| | | if(n <= 0) return -1; |
| | | int i, max_i = 0; |
| | | double max = a[0]; |
| | | float max = a[0]; |
| | | for(i = 1; i < n; ++i){ |
| | | if(a[i] > max){ |
| | | max = a[i]; |
| | |
| | | return max_i; |
| | | } |
| | | |
| | | double rand_normal() |
| | | // From http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform |
| | | #define TWO_PI 6.2831853071795864769252866 |
| | | float rand_normal() |
| | | { |
| | | static int haveSpare = 0; |
| | | static double rand1, rand2; |
| | | |
| | | if(haveSpare) |
| | | { |
| | | haveSpare = 0; |
| | | return sqrt(rand1) * sin(rand2); |
| | | } |
| | | |
| | | haveSpare = 1; |
| | | |
| | | rand1 = rand() / ((double) RAND_MAX); |
| | | if(rand1 < 1e-100) rand1 = 1e-100; |
| | | rand1 = -2 * log(rand1); |
| | | rand2 = (rand() / ((double) RAND_MAX)) * TWO_PI; |
| | | |
| | | return sqrt(rand1) * cos(rand2); |
| | | } |
| | | |
| | | /* |
| | | float rand_normal() |
| | | { |
| | | int n = 12; |
| | | int i; |
| | | float sum= 0; |
| | | for(i = 0; i < n; ++i) sum += (float)rand()/RAND_MAX; |
| | | return sum-n/2.; |
| | | } |
| | | */ |
| | | |
| | | float rand_uniform() |
| | | { |
| | | return (float)rand()/RAND_MAX; |
| | | } |
| | | |
| | | float **one_hot_encode(float *a, int n, int k) |
| | | { |
| | | int i; |
| | | double sum= 0; |
| | | for(i = 0; i < 12; ++i) sum += (double)rand()/RAND_MAX; |
| | | return sum-6.; |
| | | float **t = calloc(n, sizeof(float*)); |
| | | for(i = 0; i < n; ++i){ |
| | | t[i] = calloc(k, sizeof(float)); |
| | | int index = (int)a[i]; |
| | | t[i][index] = 1; |
| | | } |
| | | return t; |
| | | } |
| | | |