From b2408e6f82708b232ee7ba58823936680448ce01 Mon Sep 17 00:00:00 2001 From: manusmac Date: Wed, 19 Nov 2025 10:38:05 +0100 Subject: [PATCH] =?UTF-8?q?Matrizen-Funktionen=20funktionieren=20wie=20in?= =?UTF-8?q?=20Matrix=20Tests=20gew=C3=BCnscht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix.c | 74 ++++++++++++++++++++++++++++++++++++++------------------ matrix.h | 2 +- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/matrix.c b/matrix.c index 0fbaff8..547af1d 100644 --- a/matrix.c +++ b/matrix.c @@ -6,11 +6,17 @@ // Matrix erstellen Matrix createMatrix(unsigned int rows, unsigned int cols) { + // Falls eine Matrix eine Null Dimension hat leere Matrix ausgeben + if (rows ==0 || cols == 0){ + Matrix empty = {0, 0, NULL}; + return empty; + } + Matrix matrix; matrix.rows = rows; matrix.cols = cols; // Speicher erstellen und nullsetzen - matrix.buffer = (int *)calloc(rows * cols, sizeof(double)); + matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); return matrix; } @@ -34,30 +40,27 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + // Prüfen ob Indizes im gültigen Bereich sind + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { + return 0; + } + + // Prüfen ob was im Buffer ist + if (matrix.buffer == NULL) { + return 0; + } + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - // Neue Matrix mit passender Dimension erstellen - Matrix result = createMatrix(matrix1.rows, matrix1.cols); - - // Alle Elemente einzeln addieren - for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++) { - result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; - } - - return result; -} - - -Matrix multiply(const Matrix matrix1, const Matrix matrix2) -{ - if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) { + // Broadcasting Fall 1: matrix1 ist ein Spaltenvektor + if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) { // Neue Matrix mit passender Dimension erstellen Matrix result = createMatrix(matrix2.rows, matrix2.cols); - // Vektor zu jeder Spalte addieren + // Vektor zu jeder Spalte addieren for (unsigned int i = 0; i < result.rows; i++) { for (unsigned int j = 0; j < result.cols; j++) { MatrixType vectorValue = getMatrixAt(matrix1, i, 0); @@ -68,22 +71,45 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) return result; } - else if (matrix2.cols == 1 && matrix2.rows == matrix1.rows){ + // Broadcasting Fall 2: matrix2 ist ein Spaltenvektor + else if (matrix2.cols == 1 && matrix2.rows == matrix1.rows) { // Neue Matrix mit passender Dimension erstellen Matrix result = createMatrix(matrix1.rows, matrix1.cols); // Vektor zu jeder Spalte addieren for (unsigned int i = 0; i < result.rows; i++) { for (unsigned int j = 0; j < result.cols; j++) { - MatrixType vectorValue = getMatrixAt(matrix1, i, j); - MatrixType matrixValue = getMatrixAt(matrix2, i, 0); - setMatrixAt(vectorValue + matrixValue, result, i, j); + MatrixType matrixValue = getMatrixAt(matrix1, i, j); + MatrixType vectorValue = getMatrixAt(matrix2, i, 0); + setMatrixAt(matrixValue + vectorValue, result, i, j); } } return result; } - + + // Normale elementweise Addition: Dimensionen müssen übereinstimmen + else if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) { + // Neue Matrix mit passender Dimension erstellen + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + // Alle Elemente einzeln addieren + for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++) { + result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; + } + + return result; + } + + // Fehlerfall: Dimensionen passen nicht else { + Matrix empty = {0, 0, NULL}; + return empty; + } +} + + +Matrix multiply(const Matrix matrix1, const Matrix matrix2) +{ // Prüfen ob Matrizen multipiziert werden können if (matrix1.cols != matrix2.rows) { // Fehlerfall: Leere Matrix zurückgeben @@ -91,7 +117,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) return empty; } // Neue Matrix mit passender Dimension erstellen - Matrix result = createMatrix(matrix1.cols, matrix2.rows); + Matrix result = createMatrix(matrix1.rows, matrix2.cols); // Matrix-Multiplikation durchführen for (unsigned int i = 0; i < result.rows; i++) { @@ -109,5 +135,5 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) } } return result; - } + } \ No newline at end of file diff --git a/matrix.h b/matrix.h index ff1dd75..eec56bf 100644 --- a/matrix.h +++ b/matrix.h @@ -10,7 +10,7 @@ typedef struct { int rows; int cols; - int *buffer; + float *buffer; } Matrix;