This commit is contained in:
Simon Wiesend 2025-11-28 08:14:41 +01:00
parent bbb0ea1cf5
commit 3a9d8275a8
Signed by: wiesendsi102436
GPG Key ID: C18A833054142CF0

View File

@ -67,14 +67,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return createMatrix(0, 0); 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.cols != matrix2.cols)
{ {
if (matrix1.rows != matrix2.rows) // matrix1 is a vector
{ if (matrix1.cols == 1)
clearMatrix(&resMat);
return resMat;
}
else if (matrix1.cols == 1)
{ {
// broadcast vector // broadcast vector
for (size_t m = 0; m < matrix2.rows; m++) for (size_t m = 0; m < matrix2.rows; m++)
@ -86,6 +90,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
} }
return resMat; return resMat;
} }
// matrix2 is a vector
else if (matrix2.cols == 1) else if (matrix2.cols == 1)
{ {
// broadcast vector // broadcast vector
@ -98,6 +103,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
} }
return resMat; return resMat;
} }
// addition not possible
else else
{ {
clearMatrix(&resMat); clearMatrix(&resMat);