matrix.c
Go to the documentation of this file.
00001 #include "matrix.h"
00002 #include "utils.h"
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <assert.h>
00007 #include <math.h>
00008 
00009 void free_matrix(matrix m)
00010 {
00011     int i;
00012     for(i = 0; i < m.rows; ++i) free(m.vals[i]);
00013     free(m.vals);
00014 }
00015 
00016 float matrix_topk_accuracy(matrix truth, matrix guess, int k)
00017 {
00018     int *indexes = calloc(k, sizeof(int));
00019     int n = truth.cols;
00020     int i,j;
00021     int correct = 0;
00022     for(i = 0; i < truth.rows; ++i){
00023         top_k(guess.vals[i], n, k, indexes);
00024         for(j = 0; j < k; ++j){
00025             int class = indexes[j];
00026             if(truth.vals[i][class]){
00027                 ++correct;
00028                 break;
00029             }
00030         }
00031     }
00032     free(indexes);
00033     return (float)correct/truth.rows;
00034 }
00035 
00036 void scale_matrix(matrix m, float scale)
00037 {
00038     int i,j;
00039     for(i = 0; i < m.rows; ++i){
00040         for(j = 0; j < m.cols; ++j){
00041             m.vals[i][j] *= scale;
00042         }
00043     }
00044 }
00045 
00046 matrix resize_matrix(matrix m, int size)
00047 {
00048     int i;
00049     if (m.rows == size) return m;
00050     if (m.rows < size) {
00051         m.vals = realloc(m.vals, size*sizeof(float*));
00052         for (i = m.rows; i < size; ++i) {
00053             m.vals[i] = calloc(m.cols, sizeof(float));
00054         }
00055     } else if (m.rows > size) {
00056         for (i = size; i < m.rows; ++i) {
00057             free(m.vals[i]);
00058         }
00059         m.vals = realloc(m.vals, size*sizeof(float*));
00060     }
00061     m.rows = size;
00062     return m;
00063 }
00064 
00065 void matrix_add_matrix(matrix from, matrix to)
00066 {
00067     assert(from.rows == to.rows && from.cols == to.cols);
00068     int i,j;
00069     for(i = 0; i < from.rows; ++i){
00070         for(j = 0; j < from.cols; ++j){
00071             to.vals[i][j] += from.vals[i][j];
00072         }
00073     }
00074 }
00075 
00076 matrix make_matrix(int rows, int cols)
00077 {
00078     int i;
00079     matrix m;
00080     m.rows = rows;
00081     m.cols = cols;
00082     m.vals = calloc(m.rows, sizeof(float *));
00083     for(i = 0; i < m.rows; ++i){
00084         m.vals[i] = calloc(m.cols, sizeof(float));
00085     }
00086     return m;
00087 }
00088 
00089 matrix hold_out_matrix(matrix *m, int n)
00090 {
00091     int i;
00092     matrix h;
00093     h.rows = n;
00094     h.cols = m->cols;
00095     h.vals = calloc(h.rows, sizeof(float *));
00096     for(i = 0; i < n; ++i){
00097         int index = rand()%m->rows;
00098         h.vals[i] = m->vals[index];
00099         m->vals[index] = m->vals[--(m->rows)];
00100     }
00101     return h;
00102 }
00103 
00104 float *pop_column(matrix *m, int c)
00105 {
00106     float *col = calloc(m->rows, sizeof(float));
00107     int i, j;
00108     for(i = 0; i < m->rows; ++i){
00109         col[i] = m->vals[i][c];
00110         for(j = c; j < m->cols-1; ++j){
00111             m->vals[i][j] = m->vals[i][j+1];
00112         }
00113     }
00114     --m->cols;
00115     return col;
00116 }
00117 
00118 matrix csv_to_matrix(char *filename)
00119 {
00120     FILE *fp = fopen(filename, "r");
00121     if(!fp) file_error(filename);
00122 
00123     matrix m;
00124     m.cols = -1;
00125 
00126     char *line;
00127 
00128     int n = 0;
00129     int size = 1024;
00130     m.vals = calloc(size, sizeof(float*));
00131     while((line = fgetl(fp))){
00132         if(m.cols == -1) m.cols = count_fields(line);
00133         if(n == size){
00134             size *= 2;
00135             m.vals = realloc(m.vals, size*sizeof(float*));
00136         }
00137         m.vals[n] = parse_fields(line, m.cols);
00138         free(line);
00139         ++n;
00140     }
00141     m.vals = realloc(m.vals, n*sizeof(float*));
00142     m.rows = n;
00143     return m;
00144 }
00145 
00146 void matrix_to_csv(matrix m)
00147 {
00148     int i, j;
00149 
00150     for(i = 0; i < m.rows; ++i){
00151         for(j = 0; j < m.cols; ++j){
00152             if(j > 0) printf(",");
00153             printf("%.17g", m.vals[i][j]);
00154         }
00155         printf("\n");
00156     }
00157 }
00158 
00159 void print_matrix(matrix m)
00160 {
00161     int i, j;
00162     printf("%d X %d Matrix:\n",m.rows, m.cols);
00163     printf(" __");
00164     for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
00165     printf("__ \n");
00166 
00167     printf("|  ");
00168     for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
00169     printf("  |\n");
00170 
00171     for(i = 0; i < m.rows; ++i){
00172         printf("|  ");
00173         for(j = 0; j < m.cols; ++j){
00174             printf("%15.7f ", m.vals[i][j]);
00175         }
00176         printf(" |\n");
00177     }
00178     printf("|__");
00179     for(j = 0; j < 16*m.cols-1; ++j) printf(" ");
00180     printf("__|\n");
00181 }


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30