From 1c5cd42acca8d1589e3ca2f15c8bee7ebd2014c1 Mon Sep 17 00:00:00 2001 From: jw Date: Thu, 20 Nov 2025 15:13:55 +0100 Subject: [PATCH] matrix programmiert --- matrix.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++------ matrix.h | 6 ++++- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..aeaa979 100644 --- a/matrix.c +++ b/matrix.c @@ -2,34 +2,96 @@ #include #include "matrix.h" -// TODO Matrix-Funktionen implementieren - +// Matrix erstellen Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix = {NULL, 0, 0}; + if (rows == 0 || cols == 0) { + return matrix; // Keine Speicherallokation + } + matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); + matrix.rows = rows; + matrix.cols = cols; + return matrix; } +// Speicher freigeben void clearMatrix(Matrix *matrix) { - + if (matrix->buffer != NULL) { + free(matrix->buffer); + matrix->buffer = NULL; + } + matrix->rows = 0; + matrix->cols = 0; } +// Wert setzen void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx < matrix.rows && colIdx < matrix.cols && matrix.buffer != NULL) { + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } } +// Wert auslesen MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx < matrix.rows && colIdx < matrix.cols && matrix.buffer != NULL) { + return matrix.buffer[rowIdx * matrix.cols + colIdx]; + } + return UNDEFINED_MATRIX_VALUE; // Fehlerbehandlung } +// Matrizen addieren (mit Broadcasting) Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.buffer == NULL || matrix2.buffer == NULL) { + return (Matrix){NULL, 0, 0}; + } + + // Prüfen auf Broadcasting-Kompatibilität + if (!((matrix1.rows == matrix2.rows) || (matrix1.rows == 1) || (matrix2.rows == 1))) { + return (Matrix){NULL, 0, 0}; + } + if (!((matrix1.cols == matrix2.cols) || (matrix1.cols == 1) || (matrix2.cols == 1))) { + return (Matrix){NULL, 0, 0}; + } + + unsigned int resultRows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows; + unsigned int resultCols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols; + + Matrix result = createMatrix(resultRows, resultCols); + + for (unsigned int i = 0; i < resultRows; i++) { + for (unsigned int j = 0; j < resultCols; j++) { + MatrixType val1 = matrix1.buffer[(matrix1.rows == 1 ? 0 : i) * matrix1.cols + + (matrix1.cols == 1 ? 0 : j)]; + MatrixType val2 = matrix2.buffer[(matrix2.rows == 1 ? 0 : i) * matrix2.cols + + (matrix2.cols == 1 ? 0 : j)]; + result.buffer[i * resultCols + j] = val1 + val2; + } + } + + return result; } +// Matrizen multiplizieren Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL) { + return (Matrix){NULL, 0, 0}; + } + + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + 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++) { + sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j]; + } + result.buffer[i * result.cols + j] = sum; + } + } + return result; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..3623fe3 100644 --- a/matrix.h +++ b/matrix.h @@ -6,7 +6,11 @@ typedef float MatrixType; // TODO Matrixtyp definieren - +typedef struct { + MatrixType *buffer; // zuerst der Speicherbereich + unsigned int rows; // dann die Anzahl Zeilen + unsigned int cols; // dann die Anzahl Spalten +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);