diff --git a/Start_Windows/matrix.c b/Start_Windows/matrix.c index d35d088..5390c55 100644 --- a/Start_Windows/matrix.c +++ b/Start_Windows/matrix.c @@ -12,64 +12,128 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix matrix; matrix.rows = rows; matrix.cols = cols; - matrix.data = EMPTY_CHAR; + matrix.buffer = EMPTY_CHAR; + + if(rows == 0 || cols == 0) + { + Matrix emptyMatix = { + .rows = 0, + .cols = 0, + .buffer = NULL}; + return emptyMatix; + + } if(rows > 0 && cols > 0) { - matrix.data = (MatrixType*) calloc(rows * cols, sizeof(MatrixType)); + matrix.buffer = (MatrixType*) calloc(rows * cols, sizeof(MatrixType)); } return matrix; } + + + void clearMatrix(Matrix *matrix) { - if(matrix && matrix->data) + if(matrix && matrix->buffer) { - free(matrix->data); - matrix->data = EMPTY_CHAR; + free(matrix->buffer); + matrix->buffer = EMPTY_CHAR; matrix->rows = 0; matrix->cols = 0; } } + + + void setMatrixAt(MatrixType value, Matrix *matrix, unsigned int rowIdx, unsigned int colIdx) { - if(matrix && matrix->data && rowIdx < matrix->rows && colIdx < matrix->cols) + if(matrix && matrix->buffer && rowIdx < matrix->rows && colIdx < matrix->cols) { - matrix->data[rowIdx * matrix->cols + colIdx] = value; + matrix->buffer[rowIdx * matrix->cols + colIdx] = value; } } + + MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - if(matrix.data && rowIdx < matrix.rows && colIdx < matrix.cols) + if(matrix.buffer && rowIdx < matrix.rows && colIdx < matrix.cols) { - return matrix.data[rowIdx * matrix.cols + colIdx]; + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } return UNDEFINED_MATRIX_VALUE; } + + + Matrix add(const Matrix matrix1, const Matrix matrix2) { - if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + unsigned int resRows = 0; + unsigned int resCols = 0; + + //Ergebniszeilenbestimmung + if(matrix1.rows == matrix2.rows) + { + resRows = matrix1.rows; + }else if(matrix1.rows == 1) + { + resRows = matrix2.rows; + }else if(matrix2.rows == 1) + { + resRows = matrix1.rows; + }else + { + return createMatrix(0, 0); + } + + //Ergebnisspaltenbestimmung + if(matrix1.cols == matrix2.cols) + { + resCols = matrix1.cols; + }else if(matrix1.cols == 1) + { + resCols = matrix2.cols; + }else if(matrix2.cols == 1) + { + resCols = matrix1.cols; + }else { return createMatrix(0,0); } - Matrix result = createMatrix(matrix1.rows, matrix1.cols); - for(unsigned int i = 0; i < result.rows; ++i) + + //Ergebnismatrix + Matrix result = createMatrix(resRows, resCols); + if(result.buffer == NULL && (resRows > 0 && resCols > 0)) { - for(unsigned int j = 0; j < result.cols; ++j) + return createMatrix(0, 0); + } + + for(unsigned int i = 0; i < resRows; ++i) + { + for(unsigned int j = 0; i < resCols; ++j) { - MatrixType val = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); + unsigned int i1 = (matrix1.rows == 1) ? 0 : i; + unsigned int j1 = (matrix1.cols == 1) ? 0 : j; + + unsigned int i2 = (matrix2.rows == 1) ? 0 : i; + unsigned int j2 = (matrix2.cols == 1) ? 0 : j; + + MatrixType val = getMatrixAt(matrix1, i1, j1) + getMatrixAt(matrix2, i2, j2); setMatrixAt(val, &result, i, j); } } return result; - } + + + Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if(matrix1.cols != matrix2.rows) @@ -92,15 +156,3 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) return result; } - - -//Vergleich der MatrixReihen/Zeilen - // if((sizeof(matrix1) / sizeof(matrix1[0])) == sizeof(matrix2) / sizeof(matrix2[0])) - // { - // if(sizeof(matrix1[0]) / sizeof(matrix1[0][0]) == sizeof(matrix2[0] / sizeof(matrix2[0][0]))) - // { - - - - // } - //} \ No newline at end of file diff --git a/Start_Windows/matrix.h b/Start_Windows/matrix.h index 44d4ef5..ffeb3ff 100644 --- a/Start_Windows/matrix.h +++ b/Start_Windows/matrix.h @@ -8,7 +8,7 @@ typedef float MatrixType; // TODO Matrixtyp definieren typedef struct { - MatrixType* data; + MatrixType* buffer; unsigned int cols; unsigned int rows; }Matrix; diff --git a/Start_Windows/matrixTests.c b/Start_Windows/matrixTests.c index 686db1e..33fd1c3 100644 --- a/Start_Windows/matrixTests.c +++ b/Start_Windows/matrixTests.c @@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void) TEST_ASSERT_EQUAL_UINT32(0, result.cols); } +void test_addSupportsBroadcasting(void) +{ + MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; + MatrixType buffer2[] = {7, 8}; + Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; + Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2}; + + Matrix result1 = add(matrix1, matrix2); + Matrix result2 = add(matrix2, matrix1); + + float expectedResults[] = {8, 9, 10, 12, 13, 14}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols); + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols); + + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows); + TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows); + + free(result1.buffer); + free(result2.buffer); +} + void test_multiplyReturnsCorrectResults(void) { MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; @@ -127,7 +153,7 @@ void test_setMatrixAtSetsCorrectValue(void) MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer}; - setMatrixAt(expectedResult, matrixUnderTest, 1, 2); + setMatrixAt(expectedResult, &matrixUnderTest, 1, 2); TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]); } @@ -137,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(-1, matrixToTest, 2, 3); + setMatrixAt(-1, &matrixToTest, 2, 3); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows); } @@ -159,6 +185,7 @@ int main() RUN_TEST(test_clearMatrixSetsMembersToNull); RUN_TEST(test_addReturnsCorrectResult); RUN_TEST(test_addFailsOnDifferentInputDimensions); + RUN_TEST(test_addSupportsBroadcasting); RUN_TEST(test_multiplyReturnsCorrectResults); RUN_TEST(test_multiplyFailsOnWrongInputDimensions); RUN_TEST(test_getMatrixAtReturnsCorrectResult);