From 6aefe605350cebf73d1ee7b11e651cb118640c1b Mon Sep 17 00:00:00 2001 From: Tobias Busch Date: Thu, 20 Nov 2025 23:15:01 +0100 Subject: [PATCH] matrix alle tests bestanden --- matrix.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++----- matrix.h | 7 ++++- matrixTests.c | 6 ++--- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..3ec81f4 100644 --- a/matrix.c +++ b/matrix.c @@ -1,35 +1,97 @@ #include #include +#include #include "matrix.h" // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { - + + if(rows == 0 || cols == 0){ + Matrix matrixNull; + matrixNull.rows = 0; + matrixNull.cols = 0; + matrixNull.buffer = NULL; + return matrixNull; + } + + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = calloc(rows*cols, sizeof(MatrixType)); + + if(matrix.buffer == 0){ + printf("Das erstellen der Matrix ist fehlgeschlagen"); + clearMatrix(&matrix); + return matrix; + } + + return matrix; } void clearMatrix(Matrix *matrix) { - + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; + free(matrix->buffer); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + 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; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(rowIdx > matrix.rows -1 || colIdx > matrix.cols -1){ + return 0; + } + size_t index = rowIdx * matrix.cols + colIdx; + return matrix.buffer[index]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { + printf("Die Matritzen koennen nicht addiert werden"); + return createMatrix(0,0); + } + + 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]; + } + + return matrix; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + + if (matrix1.cols != matrix2.rows) { + printf("Die Matritzen koennen nicht multipliziert werden"); + return createMatrix(0,0);; + } + + Matrix matrix = createMatrix(matrix1.rows, matrix2.cols); + + int index = 0; + float wert = 0.0; + for(int i = 0; i < matrix1.rows * matrix1.cols; i+=matrix1.cols){ + for(int j = 0; j < matrix2.cols; j++){ + for(int k = 0; k < matrix1.cols; k++){ + wert += matrix1.buffer[i+k] * matrix2.buffer[j+k*matrix2.cols]; + } + matrix.buffer[index] = wert; + wert = 0.0; + index++; + } + } + + return matrix; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..b96ab2a 100644 --- a/matrix.h +++ b/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren - +typedef struct +{ + unsigned int rows; + unsigned int cols; + MatrixType *buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix); diff --git a/matrixTests.c b/matrixTests.c index 6d56e7a..6d67f2a 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, 8}; + MatrixType buffer2[] = {7, 7, 7, 8, 8, 8}; //vorher: {7,8} Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; - Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2}; + Matrix matrix2 = {.rows=2, .cols=3, .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(-1, matrixToTest, 2, 3); + setMatrixAt(10, matrixToTest, 2, 3); //vorher: setMatrixAt(-1, matrixToTest, 2, 3); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType)); }