| | |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <math.h> |
| | | #include <unistd.h> |
| | | #include <float.h> |
| | | #include <limits.h> |
| | | |
| | | #include "utils.h" |
| | | |
| | | |
| | | 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; |
| | | 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]; |
| | |
| | | void top_k(float *a, int n, int k, int *index) |
| | | { |
| | | int i,j; |
| | | for(j = 0; j < k; ++j) index[j] = 0; |
| | | 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(a[curr] > a[index[j]]){ |
| | | if((index[j] < 0) || a[curr] > a[index[j]]){ |
| | | int swap = curr; |
| | | curr = index[j]; |
| | | index[j] = swap; |
| | |
| | | } |
| | | } |
| | | |
| | | void error(char *s) |
| | | void error(const char *s) |
| | | { |
| | | perror(s); |
| | | //fprintf(stderr, "Error: %s\n", 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; |
| | |
| | | size_t curr = strlen(line); |
| | | |
| | | while((line[curr-1] != '\n') && !feof(fp)){ |
| | | printf("%ld %ld\n", curr, size); |
| | | size *= 2; |
| | | line = realloc(line, size*sizeof(char)); |
| | | if(!line) { |
| | | printf("%ld\n", size); |
| | | malloc_error(); |
| | | if(curr == size-1){ |
| | | size *= 2; |
| | | line = realloc(line, size*sizeof(char)); |
| | | if(!line) { |
| | | printf("%ld\n", size); |
| | | malloc_error(); |
| | | } |
| | | } |
| | | fgets(&line[curr], size-curr, fp); |
| | | size_t readsize = size-curr; |
| | | if(readsize > INT_MAX) readsize = INT_MAX-1; |
| | | fgets(&line[curr], readsize, fp); |
| | | curr = strlen(line); |
| | | } |
| | | 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); |
| | |
| | | return variance; |
| | | } |
| | | |
| | | float constrain(float a, float 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; |
| | | } |
| | | |
| | | float mse_array(float *a, int n) |
| | | { |
| | | int i; |
| | | 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 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; |