From 04d9b35c363be2450b0ea44914e49b49668dff57 Mon Sep 17 00:00:00 2001 From: D2A62006 Date: Wed, 26 Nov 2025 18:04:17 +0100 Subject: [PATCH] Refractor Matrix add --- matrix.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/matrix.c b/matrix.c index aca5f9f..477d480 100644 --- a/matrix.c +++ b/matrix.c @@ -101,6 +101,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co else return 0; } +/* Matrix add(const Matrix matrix1, const Matrix matrix2) { //check if the matrices are able to be added (same size) @@ -126,7 +127,36 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } } +*/ +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){ + Matrix m = {NULL, 0, 0}; + return m; + } + + + //size of the matrices should be the same, if the addition is supposed to happen + // Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols); + Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols); + + if(!outputMatrix.buffer){ + Matrix m = {NULL, 0, 0}; + return m; + } + + for (int i = 0; i < matrix1.rows;i++) { + 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 * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; + } + } + return outputMatrix; + +} /* Matrix multiply(const Matrix matrix1, const Matrix matrix2)