diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 627e49a..b601a1c 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -6,7 +6,18 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix matrix; + //Prüfung ob die MAtrix ungültig ist, bei den Unit Tests gefordert das so zu machen + //sonst würde man auch für quasi nichts Speicher mit malloc reservieren und hätte unsichere Zustände + if(rows == 0 || cols == 0){ + + matrix.buffer = NULL; + matrix.rows = 0; + matrix.cols = 0; + + return matrix; +} matrix.rows = rows; matrix.cols = cols; @@ -22,9 +33,13 @@ void clearMatrix(Matrix *matrix) if(matrix == NULL){ return; } - //hier reicht das zum freigeben da der gesamte buffer nur ein Speicherblock sind, nicht wie beim ImageInput, somit reich - //einmal auf den buffer im Matrix struct zu zeigen und diesen freizugeben + //erstmal reservierten Speicher freigeben, danach ist der zwar theoretisch frei aber der Struct hat immernoch die + //vorherigen Werte, somit muss man die alle 0 setzten um eine nicht gültige Matrix wieder zu erschaffen + //sonst zeigt man z.b. auch auf eine nicht besetzte Speicheradresse was zu nicht definiertem Verhaltem etc führen kann free(matrix->buffer); + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; } @@ -62,43 +77,104 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { + // Normale Addition + if(matrix1.rows == matrix2.rows && + matrix1.cols == matrix2.cols) + { + Matrix result = createMatrix(matrix1.rows, matrix1.cols); - Matrix result = createMatrix(matrix1.rows, matrix1.cols); - - - if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){ - - for(int i = 0; i < matrix1.rows; i++){ - for(int j = 0; j < matrix1.cols; j++){ - - MatrixType value1 = getMatrixAt(matrix1, i, j); - MatrixType value2 = getMatrixAt(matrix2, i ,j); - - setMatrixAt(value1 + value2, result, i, j); + for(unsigned int i = 0; i < matrix1.rows; i++) + { + for(unsigned int j = 0; j < matrix1.cols; j++) + { + MatrixType value1 = getMatrixAt(matrix1, i, j); + MatrixType value2 = getMatrixAt(matrix2, i, j); + setMatrixAt(value1 + value2, result, i, j); } } + return result; + } + // Broadcasting: matrix2 ist Spaltenvektor + if(matrix1.rows == matrix2.rows && + matrix2.cols == 1) + { + Matrix result = createMatrix(matrix1.rows, matrix1.cols); - } + for(unsigned int i = 0; i < matrix1.rows; i++) + { + MatrixType vectorValue = getMatrixAt(matrix2, i, 0); + for(unsigned int j = 0; j < matrix1.cols; j++) + { + MatrixType value = getMatrixAt(matrix1, i, j); - - - + setMatrixAt(value + vectorValue, result, i, j); + } + } + + return result; + } + + // Broadcasting: matrix1 ist Spaltenvektor + if(matrix1.rows == matrix2.rows && + matrix1.cols == 1) + { + Matrix result = createMatrix(matrix2.rows, matrix2.cols); + + for(unsigned int i = 0; i < matrix2.rows; i++) + { + MatrixType vectorValue = getMatrixAt(matrix1, i, 0); + + for(unsigned int j = 0; j < matrix2.cols; j++) + { + MatrixType value = getMatrixAt(matrix2, i, j); + + setMatrixAt(value + vectorValue, result, i, j); + } + } + + return result; + } + + Matrix result; result.buffer = NULL; result.rows = 0; result.cols = 0; return result; - - - - } + + + //hier muss Spalte der ersten Matrix = Zeile der zweiten Matrix sein Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - -} \ No newline at end of file + + if (matrix1.cols != matrix2.rows) + { + return createMatrix(0, 0); // hier kann man auch noch eine Fehlerbehandlung einbauen + } + + 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 += getMatrixAt(matrix1, i, k) + * getMatrixAt(matrix2, k, j); + } + + setMatrixAt(sum, result, i ,j); + } + } + + return result; +} diff --git a/Startcode/matrix.h b/Startcode/matrix.h index daa0ef1..0c47405 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -9,7 +9,7 @@ typedef float MatrixType; // TODO Matrixtyp definieren typedef struct{ -MatrixType *values; +MatrixType *buffer; unsigned int rows; unsigned int cols;