diff --git a/matrix.c b/matrix.c index 4d42efd..0aaaae3 100644 --- a/matrix.c +++ b/matrix.c @@ -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; } \ No newline at end of file