|
|
#include "basicMatrix.h"
void free_matrix(cortexMATRIX m){ uint32_t i; for (i = 0; i < m.rows; ++i) free(m.values[i]); free(m.values);};
float matrix_topk_accuracy(cortexMATRIX truth, cortexMATRIX guess, uint32_t k){ int *indexes = (int*)calloc(k, sizeof(int)); uint32_t n = truth.cols; uint32_t i, j; uint32_t correct = 0; for (i = 0; i < truth.rows; ++i){ top_k(guess.values[i], n, k, indexes); for (j = 0; j < k; ++j){ int cls = indexes[j]; if (truth.values[i][cls]){ ++correct; break; } } } free(indexes); return (float)correct / truth.rows;}
void scale_matrix(cortexMATRIX m, float scale){ uint32_t i, j; for (i = 0; i < m.rows; ++i){ for (j = 0; j < m.cols; ++j){ m.values[i][j] *= scale; } }}
cortexMATRIX resize_matrix(cortexMATRIX m, uint32_t size){ uint32_t i; if (m.rows == size) return m; if (m.rows < size) { m.values = (float**)realloc(m.values, size*sizeof(float*)); for (i = m.rows; i < size; ++i) { m.values[i] = (float*)calloc(m.cols, sizeof(float)); } } else if (m.rows > size) { for (i = size; i < m.rows; ++i) { free(m.values[i]); } m.values = (float**)realloc(m.values, size*sizeof(float*)); } m.rows = size; return m;};
void matrix_add_matrix(cortexMATRIX from, cortexMATRIX to){ assert(from.rows == to.rows && from.cols == to.cols); uint32_t i, j; for (i = 0; i < from.rows; ++i){ for (j = 0; j < from.cols; ++j){ to.values[i][j] += from.values[i][j]; }; };};//void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
//{
// int i;
// for (i = 0; i < N; ++i) Y[i*INCY] = X[i*INCX];
//}
cortexMATRIX copy_matrix(cortexMATRIX m){ cortexMATRIX c = { 0 }; c.rows = m.rows; c.cols = m.cols; c.values = (float**)calloc(c.rows, sizeof(float *)); uint32_t i; for (i = 0; i < c.rows; ++i){ c.values[i] = (float*)calloc(c.cols, sizeof(float)); //copy_cpu(c.cols, m.values[i], 1, c.values[i], 1);
for (uint32_t j = 0; j < c.cols; ++j)c.values[i][j] = m.values[i][j]; } return c;}
cortexMATRIX make_matrix(uint32_t rows, uint32_t cols){ uint32_t i; cortexMATRIX m; m.rows = rows; m.cols = cols; m.values = (float**)calloc(m.rows, sizeof(float *)); for (i = 0; i < m.rows; ++i){ m.values[i] = (float*)calloc(m.cols, sizeof(float)); } return m;};
cortexMATRIX hold_out_matrix(cortexMATRIX *m, uint32_t n){ uint32_t i; cortexMATRIX h; h.rows = n; h.cols = m->cols; h.values = (float**)calloc(h.rows, sizeof(float *)); for (i = 0; i < n; ++i){ int index = rand() % m->rows; h.values[i] = m->values[index]; m->values[index] = m->values[--(m->rows)]; } return h;};
float *pop_column(cortexMATRIX *m, uint32_t c){ float *col = (float*)calloc(m->rows, sizeof(float)); uint32_t i, j; for (i = 0; i < m->rows; ++i){ col[i] = m->values[i][c]; for (j = c; j < m->cols - 1; ++j){ m->values[i][j] = m->values[i][j + 1]; }; }; --m->cols; return col;};
cortexMATRIX csv_to_matrix(char *filename){ FILE *fp = fopen(filename, "r"); if (!fp) file_error(filename);
cortexMATRIX m; m.cols = -1;
char *line;
uint32_t n = 0; uint32_t size = 1024; m.values = (float**)calloc(size, sizeof(float*)); while ((line = fgetl(fp))){ if (m.cols == -1) m.cols = count_fields(line); if (n == size){ size *= 2; m.values = (float**)realloc(m.values, size*sizeof(float*)); } m.values[n] = parse_fields(line, m.cols); free(line); ++n; } m.values = (float**)realloc(m.values, n*sizeof(float*)); m.rows = n; return m;};
void matrix_to_csv(cortexMATRIX m){ uint32_t i, j;
for (i = 0; i < m.rows; ++i){ for (j = 0; j < m.cols; ++j){ if (j > 0) printf(","); printf("%.17g", m.values[i][j]); }; printf("\n"); };};
void print_matrix(cortexMATRIX m){ uint32_t i, j; printf("%d X %d Matrix:\n", m.rows, m.cols); printf(" __"); for (j = 0; j < 16 * m.cols - 1; ++j) printf(" "); printf("__ \n");
printf("| "); for (j = 0; j < 16 * m.cols - 1; ++j) printf(" "); printf(" |\n");
for (i = 0; i < m.rows; ++i){ printf("| "); for (j = 0; j < m.cols; ++j){ printf("%15.7f ", m.values[i][j]); }; printf(" |\n"); }; printf("|__"); for (j = 0; j < 16 * m.cols - 1; ++j) printf(" "); printf("__|\n");};
|