From 3ee900784e6f6a739cc79079f0c56bf7a706fc95 Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 5 Nov 2026 11:16:39 +0100 Subject: [PATCH 1/6] matrix logic --- Startcode/matrix.c | 30 +++++++++++++++++++++++++----- Startcode/matrix.h | 5 +++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..9f1c1cf 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -6,27 +6,47 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - + MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int)); + const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage}; + + return matrix; } void clearMatrix(Matrix *matrix) { - + matrix->rows = 0; + matrix->cols = 0; + __builtin_free(matrix->storage); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.storage[rowIdx][colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + return matrix.storage[rowIdx][colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + return matrix1; + } + + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + for (unsigned int i = 0; i < matrix1.rows; i++) + { + for (unsigned int j = 0; j < matrix1.cols; j++) + { + result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j]; + } + } + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) diff --git a/Startcode/matrix.h b/Startcode/matrix.h index cc640d1..ed29aae 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType** storage; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix); From 5e1a1fdf72f39768a149e4dc6473638f088fd04a Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 5 Nov 2026 14:26:57 +0100 Subject: [PATCH 2/6] refactor: change storage type of matrix to flat storage --- Startcode/matrix.c | 31 +++++++++++++++++++++---------- Startcode/matrix.h | 3 ++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 9f1c1cf..b238b81 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -4,29 +4,39 @@ // TODO Matrix-Funktionen implementieren +int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx) +{ + return rowIdx * cols + colIdx; +} + Matrix createMatrix(unsigned int rows, unsigned int cols) { - MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int)); - const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage}; - - return matrix; + Matrix* m = malloc(sizeof(Matrix)); + m->rows = rows; + m->cols = cols; + m->storage = malloc(rows * cols * sizeof(MatrixType)); + return m; } void clearMatrix(Matrix *matrix) { - matrix->rows = 0; - matrix->cols = 0; - __builtin_free(matrix->storage); + for (int i = 0; i < matrix->rows; i++) + { + for (int j = 0; j < matrix->cols; j++) + { + setMatrixElement(matrix, i, j, UNDEFINED_MATRIX_VALUE); + } + } } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.storage[rowIdx][colIdx] = value; + matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - return matrix.storage[rowIdx][colIdx]; + return matrix.storage[getMatrixIndex(matrix->cols, rowIdx, colIdx)]; } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -42,7 +52,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { for (unsigned int j = 0; j < matrix1.cols; j++) { - result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j]; + unsigned int index = getMatrixIndex(matrix1.cols, i, j); + result.storage[index] = matrix1.storage[index] + matrix2.storage[index]; } } diff --git a/Startcode/matrix.h b/Startcode/matrix.h index ed29aae..8300fcb 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -6,11 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren +// Flat Storage: Nur ein Pointer, sorgt für zusammenhängendes array, kein verstreuter Speicher -> bessere Performance typedef struct { unsigned int rows; unsigned int cols; - MatrixType** storage; + MatrixType* storage; } Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); From 3519fc9af601729748e1fb0d30ae05b60158c21c Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 5 Nov 2026 14:43:52 +0100 Subject: [PATCH 3/6] feat: multiply method --- Startcode/matrix.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index b238b81..07a2309 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -20,9 +20,9 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) void clearMatrix(Matrix *matrix) { - for (int i = 0; i < matrix->rows; i++) + for (unsigned int i = 0; i < matrix->rows; i++) { - for (int j = 0; j < matrix->cols; j++) + for (unsigned int j = 0; j < matrix->cols; j++) { setMatrixElement(matrix, i, j, UNDEFINED_MATRIX_VALUE); } @@ -43,11 +43,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { - return matrix1; + fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n", + matrix1->rows, matrix1->cols, matrix2->rows, matrix2->cols); + return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; } Matrix result = createMatrix(matrix1.rows, matrix1.cols); + // check, ob genügend Speicher vorhanden + if (result.storage == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + } + for (unsigned int i = 0; i < matrix1.rows; i++) { for (unsigned int j = 0; j < matrix1.cols; j++) @@ -62,5 +69,36 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.cols != matrix2.rows) + { + fprintf(stderr, "Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n", + matrix1.cols, matrix2.rows); + return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + } + + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + + // check, ob genügend Speicher vorhanden + if (result.storage == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) + { + for (unsigned int j = 0; j < matrix2.cols; j++) + { + MatrixType sum = 0; + + for (unsigned int k = 0; k < matrix1.cols; k++) + { + MatrixType val1 = matrix1.storage[i * matrix1.cols + k]; + MatrixType val2 = matrix2.storage[k * matrix2.cols + j]; + sum += val1 * val2; + } + + result.storage[i * result.cols + j] = sum; + } + } + + return result; } \ No newline at end of file From 07522b7d3eef74dd91a19e46937b4d16f3571663 Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Mon, 9 Nov 2026 14:08:41 +0100 Subject: [PATCH 4/6] fix: syntax --- Startcode/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 07a2309..d9186c7 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -36,7 +36,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - return matrix.storage[getMatrixIndex(matrix->cols, rowIdx, colIdx)]; + return matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)]; } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -44,7 +44,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n", - matrix1->rows, matrix1->cols, matrix2->rows, matrix2->cols); + matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols); return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; } From dba7bb662425a317bc7d8e73575da24ed3aafb8c Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 12 Nov 2026 10:49:37 +0100 Subject: [PATCH 5/6] refactor: store matrix struc on stack --- Startcode/matrix.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index d9186c7..20bf4e6 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -1,21 +1,23 @@ +#include #include #include #include "matrix.h" -// TODO Matrix-Funktionen implementieren - int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx) { return rowIdx * cols + colIdx; } -Matrix createMatrix(unsigned int rows, unsigned int cols) +Matrix createMatrix(const unsigned int rows, const unsigned int cols) { - Matrix* m = malloc(sizeof(Matrix)); - m->rows = rows; - m->cols = cols; - m->storage = malloc(rows * cols * sizeof(MatrixType)); - return m; + Matrix matrix = {.rows = rows, .cols = cols}; + matrix.storage = malloc(rows * cols * sizeof(MatrixType)); + return matrix; +} + +void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +{ + matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; } void clearMatrix(Matrix *matrix) @@ -24,16 +26,11 @@ void clearMatrix(Matrix *matrix) { for (unsigned int j = 0; j < matrix->cols; j++) { - setMatrixElement(matrix, i, j, UNDEFINED_MATRIX_VALUE); + setMatrixAt(UNDEFINED_MATRIX_VALUE, *matrix, i, j); } } } -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; -} - MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { return matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)]; @@ -71,7 +68,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if (matrix1.cols != matrix2.rows) { - fprintf(stderr, "Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n", + printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n", matrix1.cols, matrix2.rows); return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; } From b67beaa2ab9c055ad312b592e69a9f27891ea2ef Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 12 Nov 2026 11:09:34 +0100 Subject: [PATCH 6/6] refactor: rename matrix storage to buffer --- Startcode/matrix.c | 26 +++++++++++++------------- Startcode/matrix.h | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 20bf4e6..dc4c5b1 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -11,13 +11,13 @@ int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx) Matrix createMatrix(const unsigned int rows, const unsigned int cols) { Matrix matrix = {.rows = rows, .cols = cols}; - matrix.storage = malloc(rows * cols * sizeof(MatrixType)); + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); return matrix; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; + matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; } void clearMatrix(Matrix *matrix) @@ -33,7 +33,7 @@ void clearMatrix(Matrix *matrix) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - return matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)]; + return matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)]; } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -42,14 +42,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n", matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols); - return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; } Matrix result = createMatrix(matrix1.rows, matrix1.cols); // check, ob genügend Speicher vorhanden - if (result.storage == NULL) { - return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + if (result.buffer == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; } for (unsigned int i = 0; i < matrix1.rows; i++) @@ -57,7 +57,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) for (unsigned int j = 0; j < matrix1.cols; j++) { unsigned int index = getMatrixIndex(matrix1.cols, i, j); - result.storage[index] = matrix1.storage[index] + matrix2.storage[index]; + result.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index]; } } @@ -70,14 +70,14 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n", matrix1.cols, matrix2.rows); - return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; } Matrix result = createMatrix(matrix1.rows, matrix2.cols); // check, ob genügend Speicher vorhanden - if (result.storage == NULL) { - return (Matrix){ .rows = 0, .cols = 0, .storage = NULL }; + if (result.buffer == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; } for (unsigned int i = 0; i < matrix1.rows; i++) @@ -88,12 +88,12 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) for (unsigned int k = 0; k < matrix1.cols; k++) { - MatrixType val1 = matrix1.storage[i * matrix1.cols + k]; - MatrixType val2 = matrix2.storage[k * matrix2.cols + j]; + MatrixType val1 = matrix1.buffer[i * matrix1.cols + k]; + MatrixType val2 = matrix2.buffer[k * matrix2.cols + j]; sum += val1 * val2; } - result.storage[i * result.cols + j] = sum; + result.buffer[i * result.cols + j] = sum; } } diff --git a/Startcode/matrix.h b/Startcode/matrix.h index 8300fcb..104c35d 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -11,7 +11,7 @@ typedef struct { unsigned int rows; unsigned int cols; - MatrixType* storage; + MatrixType* buffer; } Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols);