From 3c49920613c589218775af6e52cd596f3314b3e4 Mon Sep 17 00:00:00 2001 From: Kristin Date: Thu, 13 Nov 2025 20:28:48 +0100 Subject: [PATCH 1/3] =?UTF-8?q?matrix.c=20und=20matrix.h,=20makefile=20f?= =?UTF-8?q?=C3=BCr=20windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- makefile | 13 +++++++------ matrix.c | 58 +++++++++++++++++++++++++++++--------------------------- matrix.h | 18 +++++++++--------- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/makefile b/makefile index f27b756..67a3c53 100644 --- a/makefile +++ b/makefile @@ -57,10 +57,11 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c # -------------------------- # Clean # -------------------------- +#clean: +#ifeq ($(OS),Windows_NT) +# del /f *.o *.exe +#else +# rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests +#endif clean: -ifeq ($(OS),Windows_NT) - del /f *.o *.exe -else - rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests -endif - + rm -f *.o *.exe \ No newline at end of file diff --git a/matrix.c b/matrix.c index f1a06f3..6cd833e 100644 --- a/matrix.c +++ b/matrix.c @@ -1,36 +1,38 @@ +#include "matrix.h" #include #include -#include "matrix.h" - // TODO Matrix-Funktionen implementieren - -Matrix createMatrix(unsigned int rows, unsigned int cols) -{ - MatrixType* data = malloc(rows * cols * sizeof(MatrixType)); - Matrix newMatrix = {rows, cols, data}; - return newMatrix; +/*typedef struct { + unsigned int rows; //Zeilen + unsigned int cols; //Spalten + MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten +} Matrix;*/ +Matrix createMatrix(unsigned int rows, unsigned int cols) { + MatrixType *data = + malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc + // liefert Zeiger auf Speicher + Matrix newMatrix = {rows, cols, data}; // neue Matrix nach struct + return newMatrix; } - -void clearMatrix(Matrix *matrix) -{ +void clearMatrix(Matrix *matrix) { + matrix->data = UNDEFINED_MATRIX_VALUE; + matrix->rows = UNDEFINED_MATRIX_VALUE; + matrix->cols = UNDEFINED_MATRIX_VALUE; + free((*matrix).data); // Speicher freigeben } - -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - +void setMatrixAt(MatrixType value, Matrix matrix, + unsigned int rowIdx, // Kopie der Matrix wird übergeben + unsigned int colIdx) { + matrix.data[rowIdx * matrix.cols + colIdx] = + value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte + // innerhalb der Zeile } - -MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - +MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, + unsigned int colIdx) { + return 0; } - -Matrix add(const Matrix matrix1, const Matrix matrix2) -{ - +Matrix add(const Matrix matrix1, const Matrix matrix2) { + // broadcasting + return matrix1; } - -Matrix multiply(const Matrix matrix1, const Matrix matrix2) -{ - -} \ No newline at end of file +Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; } diff --git a/matrix.h b/matrix.h index 736e6e1..1d37962 100644 --- a/matrix.h +++ b/matrix.h @@ -6,20 +6,20 @@ typedef float MatrixType; // TODO Matrixtyp definieren -typedef struct -{ - unsigned int rows; - unsigned int cols; - MatrixType *data; -} Matrix; +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType *data; +} 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); +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); - #endif From 0081e8f89e804b5b0f2943e860ade50a3d285439 Mon Sep 17 00:00:00 2001 From: Kristin Date: Tue, 18 Nov 2025 10:11:38 +0100 Subject: [PATCH 2/3] data in buffer --- matrix.c | 25 +++++++++++++++++-------- matrix.h | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/matrix.c b/matrix.c index 6cd833e..3049010 100644 --- a/matrix.c +++ b/matrix.c @@ -5,31 +5,40 @@ /*typedef struct { unsigned int rows; //Zeilen unsigned int cols; //Spalten - MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten + MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten } Matrix;*/ Matrix createMatrix(unsigned int rows, unsigned int cols) { - MatrixType *data = + MatrixType *buffer = malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc // liefert Zeiger auf Speicher - Matrix newMatrix = {rows, cols, data}; // neue Matrix nach struct + Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct return newMatrix; } void clearMatrix(Matrix *matrix) { - matrix->data = UNDEFINED_MATRIX_VALUE; + matrix->buffer = UNDEFINED_MATRIX_VALUE; matrix->rows = UNDEFINED_MATRIX_VALUE; matrix->cols = UNDEFINED_MATRIX_VALUE; - free((*matrix).data); // Speicher freigeben + free((*matrix).buffer); // Speicher freigeben } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, // Kopie der Matrix wird übergeben unsigned int colIdx) { - matrix.data[rowIdx * matrix.cols + colIdx] = + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte // innerhalb der Zeile } -MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, +MatrixType getMatrixAt(const Matrix matrix, + unsigned int rowIdx, // Kopie der Matrix wird übergeben unsigned int colIdx) { - return 0; + if (rowIdx >= matrix.rows || + colIdx >= matrix.cols) { // Speichergröße nicht überschreiten + return 0; + } + + MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx]; + + return value; } Matrix add(const Matrix matrix1, const Matrix matrix2) { // broadcasting diff --git a/matrix.h b/matrix.h index 1d37962..ca871ae 100644 --- a/matrix.h +++ b/matrix.h @@ -9,7 +9,7 @@ typedef float MatrixType; typedef struct { unsigned int rows; unsigned int cols; - MatrixType *data; + MatrixType *buffer; } Matrix; From 858673bdca9a62a32a1cf5ceef28b6dd88c0662d Mon Sep 17 00:00:00 2001 From: Kristin Date: Thu, 20 Nov 2025 14:46:14 +0100 Subject: [PATCH 3/3] =?UTF-8?q?makefile=20f=C3=BCr=20alle=20Betriebssystem?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- makefile | 3 ++- matrix.c | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/makefile b/makefile index 67a3c53..5d94684 100644 --- a/makefile +++ b/makefile @@ -63,5 +63,6 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c #else # rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests #endif +# clean für windows clean: - rm -f *.o *.exe \ No newline at end of file + rm -f *.o *.exe \ No newline at end of file diff --git a/matrix.c b/matrix.c index 3049010..7a8dcac 100644 --- a/matrix.c +++ b/matrix.c @@ -20,9 +20,9 @@ void clearMatrix(Matrix *matrix) { matrix->cols = UNDEFINED_MATRIX_VALUE; free((*matrix).buffer); // Speicher freigeben } -void setMatrixAt(MatrixType value, Matrix matrix, - unsigned int rowIdx, // Kopie der Matrix wird übergeben - unsigned int colIdx) { +void setMatrixAt(const MatrixType value, Matrix matrix, + const unsigned int rowIdx, // Kopie der Matrix wird übergeben + const unsigned int colIdx) { matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte @@ -41,7 +41,25 @@ MatrixType getMatrixAt(const Matrix matrix, return value; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - // broadcasting - return matrix1; + + // Ergebnismatrix + Matrix result; + + // Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender + // Matrix + if (matrix1.rows != matrix2.rows) { + + // check, which one is smaller + // realloc + } + + if (matrix1.cols != matrix2.cols) { + } + + // Speicher reservieren + + // Matrix addieren + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }