Skip to content

Commit

Permalink
save matrix to file
Browse files Browse the repository at this point in the history
  • Loading branch information
QwEekYhyo committed Sep 30, 2024
1 parent 4c303cf commit 9204d69
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ void free_matrix(Matrix* matrix);

void print_matrix(Matrix* matrix);

int save_matrix(Matrix* matrix, const char* filename);

#endif // NCN_MATRIX_H
21 changes: 21 additions & 0 deletions src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@ void print_matrix(Matrix* matrix) {
printf("]\n");
}
}

int save_matrix(Matrix* matrix, const char* filename) {
FILE* file = fopen(filename, "a");
if (!file) {
printf("Could not open file %s for writting\n", filename);
return -1;
}

fprintf(file, "%c ", 'M');
fprintf(file, "%zu %zu\n", matrix->rows, matrix->columns);
for (size_t r = 0; r < matrix->rows; r++) {
for (size_t c = 0; c < matrix->columns; c++) {
fprintf(file, "%.15lf ", matrix->buffer[r][c]);
}
fprintf(file, "\n");
}
fprintf(file, "---\n");

fclose(file);
return 0;
}
96 changes: 96 additions & 0 deletions tests/test_save_matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* New Chad Neural - C library to train neural networks
* Copyright (C) 2024 Lucas Logan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#define _CRT_SECURE_NO_WARNINGS

#include <matrix.h>
#include <utils.h>

#include <stdio.h>

static const char* FILENAME = "matrix.ncn";

int main(void) {
Matrix* m = new_uninitialized_matrix(3, 4);

for (size_t i = 0; i < m->rows; i++) {
for (size_t j = 0; j < m->columns; j++) {
m->buffer[i][j] = (i * 10) + j / 15.0;
m->buffer[i][j] *= m->buffer[i][j];
}
}

print_matrix(m);

int error = save_matrix(m, FILENAME);
if (error != 0) {
printf("Error while saving vector\n");
return 1;
}

// Check if it was correctly saved
FILE* file = fopen(FILENAME, "r");
if (!file) {
printf("Error while opening file where vector is saved\n");
return 1;
}

char type;
fscanf(file, "%c", &type);
if (type != 'M') {
printf("Saved type is not Vector type, got: %c\n",
type
);
return 1;
}

size_t rows, columns;
fscanf(file, "%zu %zu", &rows, &columns);
if (rows != m->rows) {
printf("Saved size differs from real size, expected: %zu, got: %zu\n",
m->rows,
rows
);
return 1;
}
if (columns != m->columns) {
printf("Saved size differs from real size, expected: %zu, got: %zu\n",
m->columns,
columns
);
return 1;
}

double current;
for (size_t i = 0; i < m->rows; i++) {
for (size_t j = 0; j < m->columns; j++) {
fscanf(file, "%lf", &current);
if (!are_double_equals(current, m->buffer[i][j])) {
printf("Saved value differs from real value, expected: %.15lf, got: %.15lf\n",
m->buffer[i][j],
current
);
return 1;
}
}
}

fclose(file);
free_matrix(m);

return 0;
}

0 comments on commit 9204d69

Please sign in to comment.