matrixAdd check

This commit is contained in:
Nick Haller 2025-11-11 15:34:34 +01:00
parent 07f217f1f4
commit da8dc2a9ef
2 changed files with 34 additions and 4 deletions

View File

@ -73,14 +73,44 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return result; return result;
} }
// Matritzenaddition if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting
for (unsigned int i = 0; i < result.rows; i++)
{ {
for (unsigned int j = 0; j < result.cols; j++)
for (unsigned int i = 0; i < matrix1.rows; i++)
{ {
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows)
{
for (unsigned int i = 0; i < matrix2.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
}
} }
} }
else
{
// Elementweise Addition
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
return result; return result;
} }

Binary file not shown.