From 80481f037de6d413660276692fd4a56a7c3a0a92 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Thu, 13 Nov 2025 15:54:04 +0100 Subject: [PATCH 1/7] Matrix Strct erstellt/Erste Matrixfunktionen geschrieben --- matrix.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- matrix.h | 7 ++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..86fd5ab 100644 --- a/matrix.c +++ b/matrix.c @@ -6,22 +6,63 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = malloc(rows * cols * sizeof(float)); + + if(matrix.buffer == NULL) + { + printf("Fehler bei der Speicherreservierung! Keine Matrix erstellt!"); + matrix.rows = 0; + matrix.cols = 0; + } + + return matrix; } void clearMatrix(Matrix *matrix) { - + free(matrix.buffer); + + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(matrix.buffer == NULL) + { + printf("Fehler beim Setzen! Matrix nicht initialisiert"); + return; + } + + if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + { + printf("Ungueltige Indizes beim Setzen!\n"); + return; + } + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(matrix.buffer == NULL) + { + printf("Fehler beim Lesen! Matrix nicht initialisiert"); + return 0; + } + + if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + { + printf("Ungueltige Indizes beim Lesen!\n"); + return 0; + } + + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) diff --git a/matrix.h b/matrix.h index cc640d1..42879d5 100644 --- a/matrix.h +++ b/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren - +typedef struct +{ + int rows; + int cols; + float *buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix); From 6594829227e5724c69359d3d8c6acf5bdd7507ea Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Sun, 16 Nov 2025 14:46:33 +0100 Subject: [PATCH 2/7] =?UTF-8?q?Addition=20und=20Multiplikation=20hinzugef?= =?UTF-8?q?=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/matrix.c b/matrix.c index 86fd5ab..02f2473 100644 --- a/matrix.c +++ b/matrix.c @@ -31,21 +31,21 @@ void clearMatrix(Matrix *matrix) matrix->cols = 0; } -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +void setMatrixAt(MatrixType value, Matrix *matrix, unsigned int rowIdx, unsigned int colIdx) { - if(matrix.buffer == NULL) + if(matrix->buffer == NULL) { printf("Fehler beim Setzen! Matrix nicht initialisiert"); return; } - if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + if(rowIdx >= matrix->rows || colIdx >= matrix->cols) { printf("Ungueltige Indizes beim Setzen!\n"); return; } - matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + matrix->buffer[rowIdx * matrix->cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) @@ -67,10 +67,61 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) + { + printf("Fehler bei Addition: Matrix Dimensionen passen nicht ueberein!\n"); + Matrix empty = {0, 0, NULL}; + return empty; + } + + float matrix1Wert = 0; + float matrix2Wert = 0; + float summe = 0; + + Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); + + for(int row = 0; row < ergebnisMatrix.rows; row++) + { + for(int col = 0; col < ergebnisMatrix.cols; col++) + { + matrix1Wert = getMatrixAt(matrix1, row, col); + matrix2Wert = getMatrixAt(matrix2, row, col); + summe = matrix1Wert + matrix2Wert; + + setMatrixAt(summe, &ergebnisMatrix, row, col); + } + } + + return ergebnisMatrix; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - -} \ No newline at end of file + if(matrix1.cols != matrix2.rows) + { + printf("Fehler bei Multiplikation: Matrix Dimensionen passen nicht ueberein!\n"); + Matrix empty = {0, 0, NULL}; + return empty; + } + + float erg = 0; + + Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix2.cols); + + for(int row = 0; row < ergebnisMatrix.rows; row++) + { + for(int col = 0; col < ergebnisMatrix.cols; col++) + { + erg = 0; + + for(int k = 0; k < matrix1.cols; k++) + { + erg += getMatrixAt(matrix1, row, k) * getMatrixAt(matrix2, k, col); + } + + setMatrixAt(erg, &ergebnisMatrix, row, col); + } + } + + return ergebnisMatrix; +} From 78579ded18508728a78a605f0208dd5635828802 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Tue, 18 Nov 2025 10:27:39 +0100 Subject: [PATCH 3/7] Kleiner Fix 2 --- matrix.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/matrix.c b/matrix.c index 8552d10..8286a04 100644 --- a/matrix.c +++ b/matrix.c @@ -8,6 +8,13 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix matrix; + Matrix empty = {0, 0, NULL}; + + if(rows == 0 || cols == 0) + { + //print("Fehler: Dimensionen muessen >= 1 sein!"); + return empty; + } matrix.rows = rows; matrix.cols = cols; @@ -15,7 +22,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) if(matrix.buffer == NULL) { - printf("Fehler bei der Speicherreservierung! Keine Matrix erstellt!"); + //printf("Fehler bei der Speicherreservierung! Keine Matrix erstellt!"); matrix.rows = 0; matrix.cols = 0; } @@ -36,13 +43,13 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned { if(matrix.buffer == NULL) { - printf("Fehler beim Setzen! Matrix nicht initialisiert"); + //printf("Fehler beim Setzen! Matrix nicht initialisiert"); return; } if(rowIdx >= matrix.rows || colIdx >= matrix.cols) { - printf("Ungueltige Indizes beim Setzen!\n"); + //printf("Ungueltige Indizes beim Setzen!\n"); return; } @@ -53,13 +60,13 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co { if(matrix.buffer == NULL) { - printf("Fehler beim Lesen! Matrix nicht initialisiert"); + //printf("Fehler beim Lesen! Matrix nicht initialisiert"); return 0; } if(rowIdx >= matrix.rows || colIdx >= matrix.cols) { - printf("Ungueltige Indizes beim Lesen!\n"); + //printf("Ungueltige Indizes beim Lesen!\n"); return 0; } @@ -70,7 +77,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { if((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) { - printf("Fehler bei Addition: Matrix Dimensionen passen nicht ueberein!\n"); + //printf("Fehler bei Addition: Matrix Dimensionen passen nicht ueberein!\n"); Matrix empty = {0, 0, NULL}; return empty; } @@ -100,7 +107,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if(matrix1.cols != matrix2.rows) { - printf("Fehler bei Multiplikation: Matrix Dimensionen passen nicht ueberein!\n"); + //printf("Fehler bei Multiplikation: Matrix Dimensionen passen nicht ueberein!\n"); Matrix empty = {0, 0, NULL}; return empty; } From 9d7ec2dc0d853b05a068f27b0de23bd1e4595c71 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Tue, 18 Nov 2025 11:00:22 +0100 Subject: [PATCH 4/7] Bis auf Broadcasting alles fertig --- matrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix.c b/matrix.c index 8286a04..14456e3 100644 --- a/matrix.c +++ b/matrix.c @@ -77,7 +77,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { if((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) { - //printf("Fehler bei Addition: Matrix Dimensionen passen nicht ueberein!\n"); + //printf("Fehler bei Addition: Matrix Dimensionen stimmen nicht ueberein!\n"); Matrix empty = {0, 0, NULL}; return empty; } From 72dab86dd961e6306687406030defbc5c70ea04b Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Tue, 18 Nov 2025 11:12:43 +0100 Subject: [PATCH 5/7] =?UTF-8?q?abschlie=C3=9Fender=20Commit=20Termin=2018.?= =?UTF-8?q?11.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matrix.c b/matrix.c index 14456e3..36ed5ef 100644 --- a/matrix.c +++ b/matrix.c @@ -82,6 +82,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return empty; } + //Hier broadcasting noch einfuegen + float matrix1Wert = 0; float matrix2Wert = 0; float summe = 0; From 18c917193c207b3e7346b1fd84fd23fe1cbb2d72 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Thu, 20 Nov 2025 18:56:31 +0100 Subject: [PATCH 6/7] broadcasting mal versucht --- matrix.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 115 insertions(+), 13 deletions(-) diff --git a/matrix.c b/matrix.c index 36ed5ef..71723cc 100644 --- a/matrix.c +++ b/matrix.c @@ -75,33 +75,135 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - if((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) - { - //printf("Fehler bei Addition: Matrix Dimensionen stimmen nicht ueberein!\n"); - Matrix empty = {0, 0, NULL}; - return empty; - } - - //Hier broadcasting noch einfuegen - float matrix1Wert = 0; float matrix2Wert = 0; float summe = 0; + int broadcasting = 0; - Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); + Matrix broadcastedmatrix1 = {0,0,NULL}; + Matrix broadcastedmatrix2 = {0,0,NULL}; + + if((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) + { + if((matrix1.rows != matrix2.rows) && (matrix1.cols == matrix2.cols)) + { + if(matrix1.rows == 1) + { + broadcastedmatrix1 = createMatrix(matrix2.rows, matrix2.cols); + + for(int row = 0; row Date: Sat, 22 Nov 2025 12:59:54 +0100 Subject: [PATCH 7/7] Optisch leicht verbessert --- matrix.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/matrix.c b/matrix.c index 71723cc..950d052 100644 --- a/matrix.c +++ b/matrix.c @@ -161,17 +161,16 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix ergebnisMatrix; - if(broadcasting == 0) + switch (broadcasting) { - ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); - } - else if(broadcasting == 1) - { - ergebnisMatrix = createMatrix(matrix2.rows, matrix2.cols); - } - else if(broadcasting == 2) - { - ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); + case 0: + case 2: + ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); + break; + + case 1: + ergebnisMatrix = createMatrix(matrix2.rows, matrix2.cols); + break; } for(int row = 0; row < ergebnisMatrix.rows; row++)