[math] Sparse matrix from dense

This commit is contained in:
Al
2016-01-08 00:48:57 -05:00
parent ba8fc716df
commit 6b164d263e
2 changed files with 30 additions and 0 deletions

19
src/sparse_matrix_utils.c Normal file
View File

@@ -0,0 +1,19 @@
#include "sparse_matrix_utils.h"
#include "float_utils.h"
#include "matrix.h"
sparse_matrix_t *sparse_matrix_new_from_matrix(matrix_t *matrix) {
sparse_matrix_t *sparse = sparse_matrix_new_shape(matrix->m, matrix->n);
for (size_t i = 0; i < matrix->m; i++) {
for (size_t j = 0; j < matrix->n; j++) {
double value = matrix_get(matrix, i, j);
if (!double_equals(value, 0.0)) {
sparse_matrix_append(sparse, j, value);
}
}
sparse_matrix_finalize_row(sparse);
}
return sparse;
}