/***** * jims_matrix.c * * The implementation of the functions. * See ./readme.txt for the details. * * Jim Mahoney | cs.marlboro.edu | Jan 2016 | MIT License *****/ #include #include #include #include "jims_matrix.h" jmatrix new_jmatrix(int rows, int columns, float* values){ int n_bytes_for_values = rows * columns * sizeof(float); jmatrix m = malloc(sizeof(struct _jmatrix)); m->rows = rows; m->columns = columns; m->values = malloc(n_bytes_for_values); if (values != NULL){ memcpy(m->values, values, n_bytes_for_values); } return m; } float jm_get(jmatrix m, int row, int col){ return (m->values)[ row * m->columns + col ]; } void jm_set(jmatrix m, int row, int col, float v){ (m->values)[ row * m->columns + col ] = v; } void print_jmatrix(jmatrix m){ int row, col; for (row=0; row < m->rows; row++){ printf(" "); for (col=0; col < m->columns; col++){ printf("%5.2f ", jm_get(m, row, col)); } printf("\n"); } } void transpose_jmatrix(jmatrix m){ int row, col; float a, b; for (row=0; row < m->rows; row++){ /* loop over top half only, col > row */ for (col=row+1; col < m->columns; col++){ a = jm_get(m, row, col); b = jm_get(m, col, row); jm_set(m, row, col, b); jm_set(m, col, row, a); } } } void free_jmatrix(jmatrix m){ free(m->values); free(m); }