implement matmul

This commit is contained in:
Simon Wiesend 2025-11-14 09:17:31 +01:00
parent 645d471860
commit 0d7f380d87
Signed by: wiesendsi102436
GPG Key ID: C18A833054142CF0

View File

@ -113,4 +113,25 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
// TODO implement
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
{
return createMatrix(0, 0);
}
int rows = matrix1.rows, cols = matrix2.cols;
Matrix resMat = createMatrix(rows, cols);
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
{
for (size_t colIdx = 0; colIdx < cols; colIdx++)
{
int curCellVal = 0;
for (size_t k = 0; k < matrix1.cols; k++)
{
curCellVal += getMatrixAt(matrix1, rowIdx, k) * getMatrixAt(matrix2, k, colIdx);
}
setMatrixAt(curCellVal, resMat, rowIdx, colIdx);
}
}
return resMat;
}