From eefb9d362edc9c4e246527298f6060360f3ea7bc Mon Sep 17 00:00:00 2001 From: regis37 Date: Tue, 18 Nov 2025 17:07:34 +0100 Subject: [PATCH] matrix.c is done and matrix.h too. End of Project --- imageInputTests.c | 2 +- matrix.c | 99 +++++++++++++++++++++++++++++++++++++------- matrix.h | 11 +++++ matrixTests.c | 2 +- mnistVisualization.c | 2 +- neuralNetwork.c | 2 +- neuralNetworkTests.c | 2 +- 7 files changed, 99 insertions(+), 21 deletions(-) diff --git a/imageInputTests.c b/imageInputTests.c index 03240ab..b901dd8 100644 --- a/imageInputTests.c +++ b/imageInputTests.c @@ -2,7 +2,7 @@ #include #include #include -#include "unity.h" +#include "C:\Git\Versuch 2\unity\unity.h" #include "imageInput.h" diff --git a/matrix.c b/matrix.c index ad00628..d2a9ffe 100644 --- a/matrix.c +++ b/matrix.c @@ -1,35 +1,102 @@ -#include -#include #include "matrix.h" - -// TODO Matrix-Funktionen implementieren +#include Matrix createMatrix(unsigned int rows, unsigned int cols) { - + if (rows == 0 || cols == 0) { + Matrix m = {0, 0, NULL}; + return m; + } + + Matrix m; + m.rows = rows; + m.cols = cols; + m.buffer = (MatrixType*)malloc(rows * cols * sizeof(MatrixType)); + + if (!m.buffer) { + m.rows = m.cols = 0; + } + + return m; } void clearMatrix(Matrix *matrix) { - -} + if (!matrix || !matrix->buffer) + return; -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - + free(matrix->buffer); + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) + return UNDEFINED_MATRIX_VALUE; + + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } -Matrix add(const Matrix matrix1, const Matrix matrix2) +void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) + return; + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } -Matrix multiply(const Matrix matrix1, const Matrix matrix2) +Matrix add(const Matrix m1, const Matrix m2) { - -} \ No newline at end of file + // Broadcasting unterstützt + if (m1.rows != m2.rows || (m1.cols != m2.cols && m1.cols != 1 && m2.cols != 1)) { + Matrix error = {0, 0, NULL}; + return error; + } + + unsigned int rows = m1.rows; + unsigned int cols = (m1.cols > m2.cols) ? m1.cols : m2.cols; + + Matrix result = createMatrix(rows, cols); + if (!result.buffer) + return result; + + for (unsigned int i = 0; i < rows; i++) { + for (unsigned int j = 0; j < cols; j++) { + + MatrixType a = (m1.cols == 1) ? m1.buffer[i] : m1.buffer[i * m1.cols + j]; + MatrixType b = (m2.cols == 1) ? m2.buffer[i] : m2.buffer[i * m2.cols + j]; + + result.buffer[i * cols + j] = a + b; + } + } + + return result; +} + +Matrix multiply(const Matrix m1, const Matrix m2) +{ + if (m1.cols != m2.rows) { + Matrix error = {0, 0, NULL}; + return error; + } + + Matrix result = createMatrix(m1.rows, m2.cols); + if (!result.buffer) + return result; + + for (unsigned int i = 0; i < m1.rows; i++) { + for (unsigned int j = 0; j < m2.cols; j++) { + + MatrixType sum = 0; + + for (unsigned int k = 0; k < m1.cols; k++) + sum += m1.buffer[i * m1.cols + k] * m2.buffer[k * m2.cols + j]; + + result.buffer[i * m2.cols + j] = sum; + } + } + + return result; +} diff --git a/matrix.h b/matrix.h index cc640d1..5804051 100644 --- a/matrix.h +++ b/matrix.h @@ -1,18 +1,29 @@ #ifndef MATRIX_H #define MATRIX_H + #define UNDEFINED_MATRIX_VALUE 0 typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType *buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); + void clearMatrix(Matrix *matrix); + void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); + MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); + Matrix add(const Matrix matrix1, const Matrix matrix2); + Matrix multiply(const Matrix matrix1, const Matrix matrix2); diff --git a/matrixTests.c b/matrixTests.c index 80b1daa..bd693ce 100644 --- a/matrixTests.c +++ b/matrixTests.c @@ -2,7 +2,7 @@ #include #include #include "matrix.h" -#include "unity.h" +#include "C:\Git\Versuch 2\unity\unity.h" void test_createMatrixFailsOnZeroDimensions(void) diff --git a/mnistVisualization.c b/mnistVisualization.c index b0a29f5..c957bde 100644 --- a/mnistVisualization.c +++ b/mnistVisualization.c @@ -2,7 +2,7 @@ #include #include #include "mnistVisualization.h" -#include "raylib.h" +#include "C:\Git\Versuch 2\raylib\raylib.h" #define MAX_TEXT_LEN 100 diff --git a/neuralNetwork.c b/neuralNetwork.c index bd8f164..30e6e4f 100644 --- a/neuralNetwork.c +++ b/neuralNetwork.c @@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path) static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) { - Matrix matrix = {NULL, 0, 0}; + Matrix matrix = {0, 0, NULL}; if(count > 0 && images != NULL) { diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index 21ab370..e73bf21 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -2,7 +2,7 @@ #include #include #include -#include "unity.h" +#include "C:\Git\Versuch 2\unity\unity.h" #include "neuralNetwork.h"