diff --git a/matrix.c b/matrix.c index 55d3dd5..6a66a5e 100644 --- a/matrix.c +++ b/matrix.c @@ -12,7 +12,13 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) m.cols = cols; m.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols); - + // Prüfe auf ungültige Dimensionen + if (rows == 0 || cols == 0) { + m.rows = 0; + m.cols = 0; + m.buffer = NULL; + return m; + } if (m.buffer == NULL){ fprintf(stderr, "Error: Memory allocation failed in createMatrix!.\n"); m.rows = 0; @@ -28,14 +34,21 @@ return m; void clearMatrix(Matrix *matrix) { - if (matrix == NULL || matrix->buffer == NULL) { - return; - } - // Alle Elemente auf 0 setzen - for (unsigned int i = 0; i < matrix->rows * matrix->cols; i++) { - matrix->buffer[i] = 0.0f; - } + if (matrix == NULL) { + return; + } + + // Speicher freigeben falls vorhanden + if (matrix->buffer != NULL) { + free(matrix->buffer); + matrix->buffer = NULL; + } + + // Dimensionen zurücksetzen + matrix->rows = 0; + matrix->cols = 0; + } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) @@ -60,30 +73,77 @@ 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) { + Matrix result; + + // Fall 1: Normale elementweise Addition (gleiche Dimensionen) + if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) { + result = createMatrix(matrix1.rows, matrix1.cols); + if (result.buffer == NULL) { + return result; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) { + for (unsigned int j = 0; j < matrix1.cols; j++) { + setMatrixAt( + getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j), + result, + i, j + ); + } + } + return result; + } + + // Fall 2: Broadcasting - matrix2 ist Spaltenvektor (cols=1) + else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) { + result = createMatrix(matrix1.rows, matrix1.cols); + if (result.buffer == NULL) { + return result; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) { + for (unsigned int j = 0; j < matrix1.cols; j++) { + // matrix2 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt + setMatrixAt( + getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0), + result, + i, j + ); + } + } + return result; + } + + // Fall 3: Broadcasting - matrix1 ist Spaltenvektor (cols=1) + else if (matrix2.rows == matrix1.rows && matrix1.cols == 1) { + result = createMatrix(matrix2.rows, matrix2.cols); + if (result.buffer == NULL) { + return result; + } + + for (unsigned int i = 0; i < matrix2.rows; i++) { + for (unsigned int j = 0; j < matrix2.cols; j++) { + // matrix1 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt + setMatrixAt( + getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j), + result, + i, j + ); + } + } + return result; + } + + // Fall 4: Ungültige Dimensionen + else { fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n"); Matrix empty = {0, 0, NULL}; return empty; } - - Matrix result = createMatrix(matrix1.rows, matrix1.cols); - if (result.buffer == NULL) { - return result; - } - - for (unsigned int i = 0; i < matrix1.rows; i++) { - for (unsigned int j = 0; j < matrix1.cols; j++) { - setMatrixAt( - getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j), - result, - i, j - ); - } - } - - return result; } + + Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if (matrix1.cols != matrix2.rows) {