diff --git a/matrix.c b/matrix.c index 29b5349..5ed4953 100644 --- a/matrix.c +++ b/matrix.c @@ -9,7 +9,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix matrix; - if(rows == 0 || cols == 0) + if (rows == 0 || cols == 0) { matrix.rows = 0; matrix.cols = 0; @@ -17,7 +17,6 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) return matrix; } - matrix.rows = rows; matrix.cols = cols; @@ -78,31 +77,69 @@ 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) + if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) // gleiche Dimension { - fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n", - matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols); + Matrix result = createMatrix(matrix1.rows, matrix1.cols); - Matrix empty = {0, 0, NULL}; - return empty; - } + if (result.buffer == NULL) + { + fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n"); + return result; + } - Matrix result = createMatrix(matrix1.rows, matrix1.cols); - - if (result.buffer == NULL) - { - fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n"); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix1.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; + } + } return result; } - - for (int i = 0; i < matrix1.rows; i++) - { - for (int j = 0; j < matrix1.cols; j++) + if (matrix1.rows == matrix2.rows && matrix2.cols == 1) // Matrix 2 hat eine Spalte { - result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + if(result.buffer == NULL) + { + fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n"); + return result; + } + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix1.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i]; + } + } + return result; } - } - return result; + + if (matrix1.rows == matrix2.rows && matrix1.cols == 1) // Matrix 1 hat eine Spalte + { + Matrix result = createMatrix(matrix2.rows, matrix2.cols); + + if(result.buffer == NULL) + { + fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n"); + return result; + } + for (int i = 0; i < matrix2.rows; i++) + { + for (int j = 0; j < matrix2.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j]; + } + } + return result; + } + + // passt nicht + fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n", + matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols); + + Matrix empty = {0, 0, NULL}; + return empty; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) @@ -134,8 +171,8 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j]; } - result.buffer[i * result.cols +j] = sum; + result.buffer[i * result.cols + j] = sum; } } return result; -} \ No newline at end of file +}