From 3a9d8275a8ca069433bd55f9ca9c0048214fe23a Mon Sep 17 00:00:00 2001 From: Simon Wiesend <117300309+smnws@users.noreply.github.com> Date: Fri, 28 Nov 2025 08:14:41 +0100 Subject: [PATCH] fix bug --- matrix.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/matrix.c b/matrix.c index cf75a94..feca5cc 100644 --- a/matrix.c +++ b/matrix.c @@ -67,14 +67,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return createMatrix(0, 0); } + // matrices not compatible + if (matrix1.rows != matrix2.rows) + { + clearMatrix(&resMat); + return resMat; + } + + // check if broadcasting is possible if (matrix1.cols != matrix2.cols) { - if (matrix1.rows != matrix2.rows) - { - clearMatrix(&resMat); - return resMat; - } - else if (matrix1.cols == 1) + // matrix1 is a vector + if (matrix1.cols == 1) { // broadcast vector for (size_t m = 0; m < matrix2.rows; m++) @@ -86,6 +90,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } return resMat; } + // matrix2 is a vector else if (matrix2.cols == 1) { // broadcast vector @@ -98,6 +103,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } return resMat; } + // addition not possible else { clearMatrix(&resMat);