| | |
| | | #include "utils.h" |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <math.h> |
| | | #include <float.h> |
| | | |
| | | #include "utils.h" |
| | | |
| | | 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; |
| | | float thresh = FLT_MAX; |
| | | for(i = 0; i < k; ++i){ |
| | | float max = -FLT_MAX; |
| | | int max_i = -1; |
| | | for(j = 0; j < n; ++j){ |
| | | float val = a[j]; |
| | | if(val > max && val < thresh){ |
| | | max = val; |
| | | max_i = j; |
| | | } |
| | | } |
| | | index[i] = max_i; |
| | | thresh = max; |
| | | } |
| | | } |
| | | |
| | | void error(char *s) |
| | | { |
| | | fprintf(stderr, "Error: %s\n", s); |
| | | perror(s); |
| | | //fprintf(stderr, "Error: %s\n", s); |
| | | exit(0); |
| | | } |
| | | |
| | |
| | | char *fgetl(FILE *fp) |
| | | { |
| | | if(feof(fp)) return 0; |
| | | int size = 512; |
| | | unsigned long size = 512; |
| | | char *line = malloc(size*sizeof(char)); |
| | | if(!fgets(line, size, fp)){ |
| | | free(line); |
| | |
| | | } |
| | | |
| | | int curr = strlen(line); |
| | | |
| | | |
| | | while(line[curr-1]!='\n'){ |
| | | size *= 2; |
| | | line = realloc(line, size*sizeof(char)); |
| | | if(!line) malloc_error(); |
| | | if(!line) { |
| | | printf("%ld\n", size); |
| | | malloc_error(); |
| | | } |
| | | fgets(&line[curr], size-curr, fp); |
| | | curr = strlen(line); |
| | | } |
| | |
| | | |
| | | 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 a, float max) |
| | | { |
| | | if(a > abs(max)) return abs(max); |
| | | if(a < -abs(max)) return -abs(max); |
| | | return a; |
| | | } |
| | | |
| | | void normalize_array(double *a, int n) |
| | | void normalize_array(float *a, int n) |
| | | { |
| | | int i; |
| | | double mu = mean_array(a,n); |
| | | double sigma = sqrt(variance_array(a,n)); |
| | | 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) |
| | | 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() |
| | | float rand_normal() |
| | | { |
| | | int n = 12; |
| | | int i; |
| | | double sum= 0; |
| | | for(i = 0; i < 12; ++i) sum += (double)rand()/RAND_MAX; |
| | | return sum-6.; |
| | | 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; |
| | | } |
| | | |
| | | double **one_hot_encode(double *a, int n, int k) |
| | | float **one_hot_encode(float *a, int n, int k) |
| | | { |
| | | int i; |
| | | double **t = calloc(n, sizeof(double*)); |
| | | float **t = calloc(n, sizeof(float*)); |
| | | for(i = 0; i < n; ++i){ |
| | | t[i] = calloc(k, sizeof(double)); |
| | | t[i] = calloc(k, sizeof(float)); |
| | | int index = (int)a[i]; |
| | | t[i][index] = 1; |
| | | } |