diff --git a/matrix.c b/matrix.c index 3ec81f4..6ceebc8 100644 --- a/matrix.c +++ b/matrix.c @@ -22,7 +22,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.buffer = calloc(rows*cols, sizeof(MatrixType)); if(matrix.buffer == 0){ - printf("Das erstellen der Matrix ist fehlgeschlagen"); + perror("Das erstellen der Matrix ist fehlgeschlagen"); clearMatrix(&matrix); return matrix; } @@ -40,6 +40,9 @@ void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { + return; + } size_t index = rowIdx * matrix.cols + colIdx; //spingt die zeilen * Anzahl der spalten (Eine Zeile = Anzahl der Spalten lang); + springt noch anzahl an spalten bis zur irhctigen Position matrix.buffer[index] = value; } @@ -56,16 +59,54 @@ 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) { - printf("Die Matritzen koennen nicht addiert werden"); + if (matrix1.rows == 0 || matrix1.cols == 0 || matrix2.rows == 0 || matrix2.cols == 0) { + perror("Die Matrizen sind leer"); + return createMatrix(0, 0); + } + + if (matrix1.rows * matrix1.cols < matrix2.rows * matrix2.cols) { + return add(matrix2, matrix1); + } + + if ((matrix1.rows != matrix2.rows && matrix2.rows != 1) || + (matrix1.cols != matrix2.cols && matrix2.cols != 1)) { + + perror("Die Matrizen koennen nicht addiert werden"); return createMatrix(0,0); } + Matrix matrix2Neu = matrix2; + int freigabe = 0; + + if (matrix1.cols != matrix2.cols) { //broadcasting + matrix2Neu = createMatrix(matrix1.rows, matrix1.cols); + freigabe = 1; + for (unsigned rowId = 0; rowId < matrix1.rows; rowId++) { + for (unsigned colId = 0; colId < matrix1.cols; colId++) { + setMatrixAt(matrix2.buffer[rowId], matrix2Neu, rowId, colId); + } + + } + + } else if (matrix1.rows != matrix2.rows) { //broadcasting + matrix2Neu = createMatrix(matrix1.rows, matrix1.cols); + freigabe = 1; + for (unsigned colId = 0; colId < matrix1.cols; colId++) { + for (unsigned rowId = 0; rowId < matrix1.rows; rowId++) { + setMatrixAt(matrix2.buffer[colId], matrix2Neu, rowId, colId); + } + + } + + } + Matrix matrix = createMatrix(matrix1.rows, matrix1.cols); for (int index = 0; index < matrix1.rows * matrix1.cols; index++){ - matrix.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index]; + matrix.buffer[index] = matrix1.buffer[index] + matrix2Neu.buffer[index]; } + if (freigabe == 1) + free(matrix2Neu.buffer); return matrix; } @@ -74,7 +115,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if (matrix1.cols != matrix2.rows) { - printf("Die Matritzen koennen nicht multipliziert werden"); + perror("Die Matritzen koennen nicht multipliziert werden"); return createMatrix(0,0);; } diff --git a/matrixTests.c b/matrixTests.c index 6d67f2a..6d56e7a 100644 --- a/matrixTests.c +++ b/matrixTests.c @@ -74,9 +74,9 @@ void test_addFailsOnDifferentInputDimensions(void) void test_addSupportsBroadcasting(void) { MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; - MatrixType buffer2[] = {7, 7, 7, 8, 8, 8}; //vorher: {7,8} + MatrixType buffer2[] = {7, 8}; Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; - Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2}; + Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2}; Matrix result1 = add(matrix1, matrix2); Matrix result2 = add(matrix2, matrix1); @@ -163,7 +163,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void) MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; - setMatrixAt(10, matrixToTest, 2, 3); //vorher: setMatrixAt(-1, matrixToTest, 2, 3); + setMatrixAt(-1, matrixToTest, 2, 3); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType)); }