From 19910ab17f669f04f9134939d84b63fd32d28fc9 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Nov 2025 14:42:57 +0100 Subject: [PATCH 1/4] Added addition and multiplication to new version, still no functionality with accessing the matrix information --- matrix.c | 33 +++++++++++++++++++++++++++++++-- matrix.h | 4 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index c8eb6aa..ec22def 100644 --- a/matrix.c +++ b/matrix.c @@ -87,10 +87,39 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - + //check if the matrices are able to be added (same size) + if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){ + //size of the matrices should be the same, if the addition is supposed to happen + Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols); + for (int i = 0; i < matrix1.rows;i++) { + for (int j = 0; j < matrix1.cols; j++) { + // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math + // outputMatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j]; + } + } + } else { + //no matrix could be added, consequence should be defined + // return NULL; + } + } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + //check, if the matrices can be multiplied + if (matrix1.rows == matrix2.cols) { + Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols); + for(int i = 0; i < matrix1.rows; i++) { + for (int j = 0; j < matrix2.cols; j++) { + for (int k = 0; k < matrix2.rows; k++) { + // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math + // outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2[k][j]; + } + } + } + return outputMatrix; + } else { + //no matrix could be added, consequence should be defined + // return NULL; + } } \ No newline at end of file diff --git a/matrix.h b/matrix.h index e89fdc7..8f05f79 100644 --- a/matrix.h +++ b/matrix.h @@ -7,8 +7,8 @@ typedef float MatrixType; // TODO Matrixtyp definieren typedef struct Matrix { - size_t rows; - size_t cols; + size_t rows; //X-Element + size_t cols; //Y-Element MatrixType *buffer; } Matrix; From 4da0a796c1eb01a20d027a3e63d624b15759bfad Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Nov 2025 16:25:20 +0100 Subject: [PATCH 2/4] Added matrix funcionality methods --- matrix.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/matrix.c b/matrix.c index ec22def..41f1d40 100644 --- a/matrix.c +++ b/matrix.c @@ -72,17 +72,27 @@ Matrix createMatrix(size_t rows, size_t cols) void clearMatrix(Matrix *matrix) { + for (int i = 0; i < matrix->rows; i++) { + for (int j = 0; j < matrix->cols;j++) { + matrix->buffer[i-1 + matrix->rows*(j-1)] = UNDEFINED_MATRIX_VALUE; + } + } + free(matrix->buffer); + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + MatrixType returnVal; + returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)]; + return returnVal; } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -93,13 +103,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols); for (int i = 0; i < matrix1.rows;i++) { for (int j = 0; j < matrix1.cols; j++) { - // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math - // outputMatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j]; + // how this should work in normal Matrix version: + // outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j]; + outputMatrix.buffer[i-1+ outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(j-1)] + matrix2.buffer[i-1 + matrix2.rows *(j-1)]; } } } else { - //no matrix could be added, consequence should be defined - // return NULL; + //the matrix could not be added, since the matrix sizes are not set correct. + Matrix m; + m.rows = 0; + m.cols = 0; + m.buffer = NULL; + return m; } } @@ -112,14 +127,19 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) for(int i = 0; i < matrix1.rows; i++) { for (int j = 0; j < matrix2.cols; j++) { for (int k = 0; k < matrix2.rows; k++) { - // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math - // outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2[k][j]; + // how this should work in normal Matrix version: + // outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j]; + outputMatrix.buffer[i-1 + outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(k-1)] * matrix2.buffer[k-1 + matrix2.rows*(j-1)]; } } } return outputMatrix; } else { - //no matrix could be added, consequence should be defined - // return NULL; + //the matrix could not be added, since the matrix sizes are not set correct. + Matrix m; + m.rows = 0; + m.cols = 0; + m.buffer = NULL; + return m; } } \ No newline at end of file From e0f192f77493425c01dc78e1ca6a7ec66464d7cf Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Nov 2025 16:29:02 +0100 Subject: [PATCH 3/4] added return value to add function in matrix.c --- matrix.c | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix.c b/matrix.c index 41f1d40..f6c0d81 100644 --- a/matrix.c +++ b/matrix.c @@ -108,6 +108,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) outputMatrix.buffer[i-1+ outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(j-1)] + matrix2.buffer[i-1 + matrix2.rows *(j-1)]; } } + return outputMatrix; } else { //the matrix could not be added, since the matrix sizes are not set correct. Matrix m; From a3b3605ff9a4a6771ff64b9d3637d816db0ec543 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Nov 2025 16:47:37 +0100 Subject: [PATCH 4/4] fixed error with matrix buffer call --- matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix.c b/matrix.c index f6c0d81..35049a5 100644 --- a/matrix.c +++ b/matrix.c @@ -105,7 +105,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) for (int j = 0; j < matrix1.cols; j++) { // how this should work in normal Matrix version: // outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j]; - outputMatrix.buffer[i-1+ outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(j-1)] + matrix2.buffer[i-1 + matrix2.rows *(j-1)]; + outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + matrix2.rows * j]; } } return outputMatrix; @@ -130,7 +130,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) for (int k = 0; k < matrix2.rows; k++) { // how this should work in normal Matrix version: // outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j]; - outputMatrix.buffer[i-1 + outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(k-1)] * matrix2.buffer[k-1 + matrix2.rows*(j-1)]; + outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* k] * matrix2.buffer[k + matrix2.rows*j]; } } }