2013-11-13 22:50:38 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2016-03-01 00:54:12 +03:00
|
|
|
#include <assert.h>
|
2015-02-07 05:53:53 +03:00
|
|
|
#include <unistd.h>
|
2014-11-19 00:51:04 +03:00
|
|
|
#include <float.h>
|
2015-01-13 04:27:08 +03:00
|
|
|
#include <limits.h>
|
2017-06-02 06:31:13 +03:00
|
|
|
#include <time.h>
|
2018-04-11 02:25:25 +03:00
|
|
|
#include <sys/time.h>
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2017-06-02 06:31:13 +03:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-10-10 23:35:28 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
// old timing. is it better? who knows!!
|
|
|
|
double get_wall_time()
|
|
|
|
{
|
|
|
|
struct timeval time;
|
|
|
|
if (gettimeofday(&time,NULL)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (double)time.tv_sec + (double)time.tv_usec * .000001;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-06-02 06:31:13 +03:00
|
|
|
double what_time_is_it_now()
|
|
|
|
{
|
2018-04-11 02:25:25 +03:00
|
|
|
struct timeval time;
|
|
|
|
if (gettimeofday(&time,NULL)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (double)time.tv_sec + (double)time.tv_usec * .000001;
|
2017-06-02 06:31:13 +03:00
|
|
|
}
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2017-04-13 00:22:53 +03:00
|
|
|
int *read_intlist(char *gpu_list, int *ngpus, int d)
|
|
|
|
{
|
|
|
|
int *gpus = 0;
|
|
|
|
if(gpu_list){
|
|
|
|
int len = strlen(gpu_list);
|
|
|
|
*ngpus = 1;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < len; ++i){
|
|
|
|
if (gpu_list[i] == ',') ++*ngpus;
|
|
|
|
}
|
|
|
|
gpus = calloc(*ngpus, sizeof(int));
|
|
|
|
for(i = 0; i < *ngpus; ++i){
|
|
|
|
gpus[i] = atoi(gpu_list);
|
|
|
|
gpu_list = strchr(gpu_list, ',')+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gpus = calloc(1, sizeof(float));
|
|
|
|
*gpus = d;
|
|
|
|
*ngpus = 1;
|
|
|
|
}
|
|
|
|
return gpus;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:53:58 +03:00
|
|
|
int *read_map(char *filename)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int *map = 0;
|
|
|
|
char *str;
|
|
|
|
FILE *file = fopen(filename, "r");
|
|
|
|
if(!file) file_error(filename);
|
|
|
|
while((str=fgetl(file))){
|
|
|
|
++n;
|
|
|
|
map = realloc(map, n*sizeof(int));
|
|
|
|
map[n-1] = atoi(str);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-09-05 03:52:44 +03:00
|
|
|
void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < sections; ++i){
|
|
|
|
size_t start = n*i/sections;
|
|
|
|
size_t end = n*(i+1)/sections;
|
|
|
|
size_t num = end-start;
|
|
|
|
shuffle(arr+(start*size), num, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shuffle(void *arr, size_t n, size_t size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
void *swp = calloc(1, size);
|
|
|
|
for(i = 0; i < n-1; ++i){
|
|
|
|
size_t j = i + rand()/(RAND_MAX / (n-i)+1);
|
|
|
|
memcpy(swp, arr+(j*size), size);
|
|
|
|
memcpy(arr+(j*size), arr+(i*size), size);
|
|
|
|
memcpy(arr+(i*size), swp, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 03:10:33 +03:00
|
|
|
int *random_index_order(int min, int max)
|
|
|
|
{
|
|
|
|
int *inds = calloc(max-min, sizeof(int));
|
|
|
|
int i;
|
|
|
|
for(i = min; i < max; ++i){
|
|
|
|
inds[i] = i;
|
|
|
|
}
|
|
|
|
for(i = min; i < max-1; ++i){
|
|
|
|
int swap = inds[i];
|
|
|
|
int index = i + rand()%(max-i);
|
|
|
|
inds[i] = inds[index];
|
|
|
|
inds[index] = swap;
|
|
|
|
}
|
|
|
|
return inds;
|
|
|
|
}
|
|
|
|
|
2015-07-08 10:36:43 +03:00
|
|
|
void del_arg(int argc, char **argv, int index)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-10 01:22:14 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-24 05:52:05 +03:00
|
|
|
|
2015-03-06 21:49:03 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-24 05:52:05 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-23 03:38:24 +03:00
|
|
|
void pm(int M, int N, float *A)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
for(i =0 ; i < M; ++i){
|
2015-01-31 09:05:23 +03:00
|
|
|
printf("%d ", i+1);
|
2015-01-23 03:38:24 +03:00
|
|
|
for(j = 0; j < N; ++j){
|
2016-01-28 23:30:38 +03:00
|
|
|
printf("%2.4f, ", A[i*N+j]);
|
2015-01-23 03:38:24 +03:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
void find_replace(char *str, char *orig, char *rep, char *output)
|
2014-11-19 00:51:04 +03:00
|
|
|
{
|
2016-09-25 09:12:54 +03:00
|
|
|
char buffer[4096] = {0};
|
2014-11-19 00:51:04 +03:00
|
|
|
char *p;
|
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
sprintf(buffer, "%s", str);
|
|
|
|
if(!(p = strstr(buffer, orig))){ // Is 'orig' even in 'str'?
|
|
|
|
sprintf(output, "%s", str);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
*p = '\0';
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
sprintf(output, "%s%s%s", buffer, rep, p+strlen(orig));
|
2014-11-19 00:51:04 +03:00
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
|
2014-10-22 01:49:18 +04:00
|
|
|
float sec(clock_t clocks)
|
|
|
|
{
|
|
|
|
return (float)clocks/CLOCKS_PER_SEC;
|
|
|
|
}
|
|
|
|
|
2014-11-19 00:51:04 +03:00
|
|
|
void top_k(float *a, int n, int k, int *index)
|
|
|
|
{
|
|
|
|
int i,j;
|
2015-06-12 01:38:58 +03:00
|
|
|
for(j = 0; j < k; ++j) index[j] = -1;
|
2014-12-12 00:15:26 +03:00
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
int curr = i;
|
|
|
|
for(j = 0; j < k; ++j){
|
2015-06-12 01:38:58 +03:00
|
|
|
if((index[j] < 0) || a[curr] > a[index[j]]){
|
2014-12-12 00:15:26 +03:00
|
|
|
int swap = curr;
|
|
|
|
curr = index[j];
|
|
|
|
index[j] = swap;
|
2014-11-19 00:51:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 03:38:24 +03:00
|
|
|
void error(const char *s)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2014-12-07 11:41:26 +03:00
|
|
|
perror(s);
|
2016-03-01 00:54:12 +03:00
|
|
|
assert(0);
|
|
|
|
exit(-1);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2017-10-10 23:35:28 +03:00
|
|
|
unsigned char *read_file(char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp = fopen(filename, "rb");
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
size = ftell(fp);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
unsigned char *text = calloc(size+1, sizeof(char));
|
|
|
|
fread(text, 1, size, fp);
|
|
|
|
fclose(fp);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:50:38 +04:00
|
|
|
void malloc_error()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Malloc error\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_error(char *s)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Couldn't open file: %s\n", s);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
list *split_str(char *s, char delim)
|
|
|
|
{
|
2015-01-13 04:27:08 +03:00
|
|
|
size_t i;
|
|
|
|
size_t len = strlen(s);
|
2013-11-13 22:50:38 +04:00
|
|
|
list *l = make_list();
|
|
|
|
list_insert(l, s);
|
|
|
|
for(i = 0; i < len; ++i){
|
|
|
|
if(s[i] == delim){
|
|
|
|
s[i] = '\0';
|
|
|
|
list_insert(l, &(s[i+1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void strip(char *s)
|
|
|
|
{
|
2015-01-13 04:27:08 +03:00
|
|
|
size_t i;
|
|
|
|
size_t len = strlen(s);
|
|
|
|
size_t offset = 0;
|
2013-11-13 22:50:38 +04:00
|
|
|
for(i = 0; i < len; ++i){
|
|
|
|
char c = s[i];
|
|
|
|
if(c==' '||c=='\t'||c=='\n') ++offset;
|
|
|
|
else s[i-offset] = c;
|
|
|
|
}
|
|
|
|
s[len-offset] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void strip_char(char *s, char bad)
|
|
|
|
{
|
2015-01-13 04:27:08 +03:00
|
|
|
size_t i;
|
|
|
|
size_t len = strlen(s);
|
|
|
|
size_t offset = 0;
|
2013-11-13 22:50:38 +04:00
|
|
|
for(i = 0; i < len; ++i){
|
|
|
|
char c = s[i];
|
|
|
|
if(c==bad) ++offset;
|
|
|
|
else s[i-offset] = c;
|
|
|
|
}
|
|
|
|
s[len-offset] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-09-01 21:21:01 +03:00
|
|
|
void free_ptrs(void **ptrs, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i) free(ptrs[i]);
|
|
|
|
free(ptrs);
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:50:38 +04:00
|
|
|
char *fgetl(FILE *fp)
|
|
|
|
{
|
|
|
|
if(feof(fp)) return 0;
|
2014-12-28 20:42:35 +03:00
|
|
|
size_t size = 512;
|
2013-11-13 22:50:38 +04:00
|
|
|
char *line = malloc(size*sizeof(char));
|
|
|
|
if(!fgets(line, size, fp)){
|
|
|
|
free(line);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-28 20:42:35 +03:00
|
|
|
size_t curr = strlen(line);
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2014-12-28 20:42:35 +03:00
|
|
|
while((line[curr-1] != '\n') && !feof(fp)){
|
2015-01-13 04:27:08 +03:00
|
|
|
if(curr == size-1){
|
|
|
|
size *= 2;
|
|
|
|
line = realloc(line, size*sizeof(char));
|
|
|
|
if(!line) {
|
|
|
|
printf("%ld\n", size);
|
|
|
|
malloc_error();
|
|
|
|
}
|
2014-10-28 05:45:06 +03:00
|
|
|
}
|
2015-01-13 04:27:08 +03:00
|
|
|
size_t readsize = size-curr;
|
|
|
|
if(readsize > INT_MAX) readsize = INT_MAX-1;
|
|
|
|
fgets(&line[curr], readsize, fp);
|
2013-11-13 22:50:38 +04:00
|
|
|
curr = strlen(line);
|
|
|
|
}
|
2014-12-28 20:42:35 +03:00
|
|
|
if(line[curr-1] == '\n') line[curr-1] = '\0';
|
2013-11-13 22:50:38 +04:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2016-05-07 02:25:16 +03:00
|
|
|
int read_int(int fd)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int next = read(fd, &n, sizeof(int));
|
|
|
|
if(next <= 0) return -1;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_int(int fd, int n)
|
|
|
|
{
|
|
|
|
int next = write(fd, &n, sizeof(int));
|
|
|
|
if(next <= 0) error("read failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_all_fail(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) return 1;
|
|
|
|
n += next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_all_fail(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) return 1;
|
|
|
|
n += next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:53:53 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-13 22:50:38 +04:00
|
|
|
char *copy_string(char *s)
|
|
|
|
{
|
|
|
|
char *copy = malloc(strlen(s)+1);
|
|
|
|
strncpy(copy, s, strlen(s)+1);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
list *parse_csv_line(char *line)
|
|
|
|
{
|
|
|
|
list *l = make_list();
|
|
|
|
char *c, *p;
|
|
|
|
int in = 0;
|
|
|
|
for(c = line, p = line; *c != '\0'; ++c){
|
|
|
|
if(*c == '"') in = !in;
|
|
|
|
else if(*c == ',' && !in){
|
|
|
|
*c = '\0';
|
|
|
|
list_insert(l, copy_string(p));
|
|
|
|
p = c+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_insert(l, copy_string(p));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_fields(char *line)
|
|
|
|
{
|
2014-11-19 00:51:04 +03:00
|
|
|
int count = 0;
|
|
|
|
int done = 0;
|
2013-11-13 22:50:38 +04:00
|
|
|
char *c;
|
2014-11-19 00:51:04 +03:00
|
|
|
for(c = line; !done; ++c){
|
|
|
|
done = (*c == '\0');
|
|
|
|
if(*c == ',' || done) ++count;
|
|
|
|
}
|
|
|
|
return count;
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
float *parse_fields(char *line, int n)
|
2013-11-13 22:50:38 +04:00
|
|
|
{
|
2014-11-19 00:51:04 +03:00
|
|
|
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;
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
|
|
|
|
2014-08-11 23:52:07 +04:00
|
|
|
float sum_array(float *a, int n)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float sum = 0;
|
2013-12-03 04:41:40 +04:00
|
|
|
for(i = 0; i < n; ++i) sum += a[i];
|
2014-08-11 23:52:07 +04:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
float mean_array(float *a, int n)
|
|
|
|
{
|
|
|
|
return sum_array(a,n)/n;
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
|
2015-12-01 02:04:09 +03:00
|
|
|
void mean_arrays(float **a, int n, int els, float *avg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
memset(avg, 0, els*sizeof(float));
|
|
|
|
for(j = 0; j < n; ++j){
|
|
|
|
for(i = 0; i < els; ++i){
|
|
|
|
avg[i] += a[j][i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < els; ++i){
|
|
|
|
avg[i] /= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 08:27:56 +03:00
|
|
|
void print_statistics(float *a, int n)
|
|
|
|
{
|
|
|
|
float m = mean_array(a, n);
|
|
|
|
float v = variance_array(a, n);
|
|
|
|
printf("MSE: %.6f, Mean: %.6f, Variance: %.6f\n", mse_array(a, n), m, v);
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
float variance_array(float *a, int n)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float sum = 0;
|
|
|
|
float mean = mean_array(a, n);
|
2013-12-03 04:41:40 +04:00
|
|
|
for(i = 0; i < n; ++i) sum += (a[i] - mean)*(a[i]-mean);
|
2014-01-29 04:28:42 +04:00
|
|
|
float variance = sum/n;
|
2013-12-03 04:41:40 +04:00
|
|
|
return variance;
|
|
|
|
}
|
|
|
|
|
2016-06-03 01:25:24 +03:00
|
|
|
int constrain_int(int a, int min, int max)
|
|
|
|
{
|
|
|
|
if (a < min) return min;
|
|
|
|
if (a > max) return max;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:20:56 +03:00
|
|
|
float constrain(float min, float max, float a)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2015-03-24 23:20:56 +03:00
|
|
|
if (a < min) return min;
|
|
|
|
if (a > max) return max;
|
2013-12-03 04:41:40 +04:00
|
|
|
return a;
|
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
|
2016-06-03 01:25:24 +03:00
|
|
|
float dist_array(float *a, float *b, int n, int sub)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float sum = 0;
|
|
|
|
for(i = 0; i < n; i += sub) sum += pow(a[i]-b[i], 2);
|
|
|
|
return sqrt(sum);
|
|
|
|
}
|
|
|
|
|
2015-01-28 00:31:06 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
void normalize_array(float *a, int n)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float mu = mean_array(a,n);
|
|
|
|
float sigma = sqrt(variance_array(a,n));
|
2013-12-03 04:41:40 +04:00
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
a[i] = (a[i] - mu)/sigma;
|
|
|
|
}
|
|
|
|
mu = mean_array(a,n);
|
|
|
|
sigma = sqrt(variance_array(a,n));
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
void translate_array(float *a, int n, float s)
|
2013-12-06 01:17:16 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
a[i] += s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 23:41:20 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
void scale_array(float *a, int n, float s)
|
2013-12-06 01:17:16 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
a[i] *= s;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 22:28:42 +03:00
|
|
|
|
2016-05-07 02:25:16 +03:00
|
|
|
int sample_array(float *a, int n)
|
|
|
|
{
|
|
|
|
float sum = sum_array(a, n);
|
|
|
|
scale_array(a, n, 1./sum);
|
|
|
|
float r = rand_uniform(0, 1);
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
r = r - a[i];
|
|
|
|
if (r <= 0) return i;
|
|
|
|
}
|
|
|
|
return n-1;
|
|
|
|
}
|
|
|
|
|
2017-11-08 03:10:33 +03:00
|
|
|
int max_int_index(int *a, int n)
|
|
|
|
{
|
|
|
|
if(n <= 0) return -1;
|
|
|
|
int i, max_i = 0;
|
|
|
|
int max = a[0];
|
|
|
|
for(i = 1; i < n; ++i){
|
|
|
|
if(a[i] > max){
|
|
|
|
max = a[i];
|
|
|
|
max_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max_i;
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
int max_index(float *a, int n)
|
2013-12-06 01:17:16 +04:00
|
|
|
{
|
|
|
|
if(n <= 0) return -1;
|
|
|
|
int i, max_i = 0;
|
2014-01-29 04:28:42 +04:00
|
|
|
float max = a[0];
|
2013-12-06 01:17:16 +04:00
|
|
|
for(i = 1; i < n; ++i){
|
|
|
|
if(a[i] > max){
|
|
|
|
max = a[i];
|
|
|
|
max_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max_i;
|
|
|
|
}
|
|
|
|
|
2017-12-26 21:52:21 +03:00
|
|
|
int int_index(int *a, int val, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; ++i){
|
|
|
|
if(a[i] == val) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-01 00:54:12 +03:00
|
|
|
int rand_int(int min, int max)
|
|
|
|
{
|
2016-08-06 01:27:07 +03:00
|
|
|
if (max < min){
|
|
|
|
int s = min;
|
|
|
|
min = max;
|
|
|
|
max = s;
|
|
|
|
}
|
2016-03-01 00:54:12 +03:00
|
|
|
int r = (rand()%(max - min + 1)) + min;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-12 00:15:26 +03:00
|
|
|
// From http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
|
2014-01-29 04:28:42 +04:00
|
|
|
float rand_normal()
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2014-12-12 00:15:26 +03:00
|
|
|
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);
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
2014-12-12 00:15:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
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.;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-05-07 02:25:16 +03:00
|
|
|
size_t rand_size_t()
|
|
|
|
{
|
|
|
|
return ((size_t)(rand()&0xff) << 56) |
|
2017-06-02 06:31:13 +03:00
|
|
|
((size_t)(rand()&0xff) << 48) |
|
|
|
|
((size_t)(rand()&0xff) << 40) |
|
|
|
|
((size_t)(rand()&0xff) << 32) |
|
|
|
|
((size_t)(rand()&0xff) << 24) |
|
|
|
|
((size_t)(rand()&0xff) << 16) |
|
|
|
|
((size_t)(rand()&0xff) << 8) |
|
|
|
|
((size_t)(rand()&0xff) << 0);
|
2016-05-07 02:25:16 +03:00
|
|
|
}
|
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
float rand_uniform(float min, float max)
|
2014-02-14 22:26:31 +04:00
|
|
|
{
|
2016-08-11 21:54:24 +03:00
|
|
|
if(max < min){
|
|
|
|
float swap = min;
|
|
|
|
min = max;
|
|
|
|
max = swap;
|
|
|
|
}
|
2016-01-19 02:40:14 +03:00
|
|
|
return ((float)rand()/RAND_MAX * (max - min)) + min;
|
2014-02-14 22:26:31 +04:00
|
|
|
}
|
2013-12-07 01:26:09 +04:00
|
|
|
|
2016-09-02 02:48:41 +03:00
|
|
|
float rand_scale(float s)
|
|
|
|
{
|
|
|
|
float scale = rand_uniform(1, s);
|
|
|
|
if(rand()%2) return scale;
|
|
|
|
return 1./scale;
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
float **one_hot_encode(float *a, int n, int k)
|
2013-12-07 01:26:09 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float **t = calloc(n, sizeof(float*));
|
2013-12-07 01:26:09 +04:00
|
|
|
for(i = 0; i < n; ++i){
|
2014-01-29 04:28:42 +04:00
|
|
|
t[i] = calloc(k, sizeof(float));
|
2013-12-07 01:26:09 +04:00
|
|
|
int index = (int)a[i];
|
|
|
|
t[i][index] = 1;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|