Skip to content

Commit

Permalink
load matrix from file
Browse files Browse the repository at this point in the history
  • Loading branch information
QwEekYhyo committed Sep 30, 2024
1 parent 9204d69 commit d461e98
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
2 changes: 2 additions & 0 deletions include/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define NCN_MATRIX_H

#include <stddef.h>
#include <stdio.h>

typedef struct {
size_t rows;
Expand All @@ -34,5 +35,6 @@ void free_matrix(Matrix* matrix);
void print_matrix(Matrix* matrix);

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

#endif // NCN_MATRIX_H
26 changes: 26 additions & 0 deletions src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#define _CRT_SECURE_NO_WARNINGS

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

Expand Down Expand Up @@ -101,3 +103,27 @@ int save_matrix(Matrix* matrix, const char* filename) {
fclose(file);
return 0;
}

Matrix* new_matrix_from_file(FILE* file) {
char type;
fscanf(file, "%c", &type);
if (type != 'M') {
printf("Type \"%c\" is not Vector type\n", type);
return NULL;
}

size_t rows, columns;
fscanf(file, "%zu %zu", &rows, &columns);

Matrix* new_matrix = new_uninitialized_matrix(rows, columns);
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < columns; c++) {
fscanf(file, "%lf", &new_matrix->buffer[r][c]);
}
}

char delimiter[5];
fscanf(file, "%5c", delimiter);

return new_matrix;
}
27 changes: 19 additions & 8 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ enable_testing()

file(GLOB_RECURSE SOURCES *.c)

set(SOURCE_FILE ${CMAKE_SOURCE_DIR}/tests/test_vector.ncn)
set(DEST_FILE ${CMAKE_BINARY_DIR}/tests/test_vector.ncn)
set(SOURCE_FILE1 ${CMAKE_SOURCE_DIR}/tests/test_vector.ncn)
set(DEST_FILE1 ${CMAKE_BINARY_DIR}/tests/test_vector.ncn)

set(SOURCE_FILE2 ${CMAKE_SOURCE_DIR}/tests/test_matrix.ncn)
set(DEST_FILE2 ${CMAKE_BINARY_DIR}/tests/test_matrix.ncn)

add_custom_command(
OUTPUT ${DEST_FILE1}
COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE1} ${DEST_FILE1}
DEPENDS ${SOURCE_FILE1}
COMMENT "Copying Vector data files needed by the tests"
)
add_custom_command(
OUTPUT ${DEST_FILE}
COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE} ${DEST_FILE}
DEPENDS ${SOURCE_FILE}
COMMENT "Copying data files needed by the tests to build directory"
OUTPUT ${DEST_FILE2}
COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE2} ${DEST_FILE2}
DEPENDS ${SOURCE_FILE2}
COMMENT "Copying Matrix data files needed by the tests"
)

add_custom_target(copy_file ALL DEPENDS ${DEST_FILE})
add_custom_target(copy_vector_file ALL DEPENDS ${DEST_FILE1})
add_custom_target(copy_matrix_file ALL DEPENDS ${DEST_FILE2})

foreach(SOURCE ${SOURCES})
get_filename_component(FILE_NAME ${SOURCE} NAME_WE)
Expand All @@ -23,4 +33,5 @@ foreach(SOURCE ${SOURCES})
add_test(NAME "Run${FILE_NAME}" COMMAND ${FILE_NAME})
endforeach()

add_dependencies(test_load_vector copy_file)
add_dependencies(test_load_vector copy_vector_file)
add_dependencies(test_load_matrix copy_matrix_file)
88 changes: 88 additions & 0 deletions tests/test_load_matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* 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/>.
*/

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

#include <stdio.h>

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

int main(void) {
FILE* file = fopen(FILENAME, "r");
if (!file) {
printf("Error while opening file where matrix is saved\n");
return 1;
}

Matrix* m = new_matrix_from_file(file);
if (!m) {
printf("Error while loading matrix from file\n");
return 1;
}

if (m->rows != 4) {
printf("Loaded matrix size is wrong: %zu\n", m->rows);
return 1;
}
if (m->columns != 3) {
printf("Loaded matrix size is wrong: %zu\n", m->columns);
return 1;
}

for (size_t r = 0; r < 4; r++) {
for (size_t c = 0; c < 3; c++) {
if (!are_double_equals(m->buffer[r][c], 6.9)) {
printf("Loaded matrix doesn't have the right value: %lf\n", m->buffer[r][c]);
return 1;
}
}
}

print_matrix(m);
free_matrix(m);

m = new_matrix_from_file(file);
if (!m) {
printf("Error while loading matrix from file\n");
return 1;
}

if (m->rows != 2) {
printf("Loaded matrix size is wrong: %zu\n", m->rows);
return 1;
}
if (m->columns != 4) {
printf("Loaded matrix size is wrong: %zu\n", m->columns);
return 1;
}

for (size_t r = 0; r < 2; r++) {
for (size_t c = 0; c < 4; c++) {
if (!are_double_equals(m->buffer[r][c], 1.0)) {
printf("Loaded matrix doesn't have the right value: %lf\n", m->buffer[r][c]);
return 1;
}
}
}
print_matrix(m);

free_matrix(m);
fclose(file);

return 0;
}

0 comments on commit d461e98

Please sign in to comment.