diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 2d4b787..b2cbc33 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -3,7 +3,6 @@ #include "matrix.h" #include #include -// TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { @@ -50,7 +49,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } } else if(matrix2.cols == 1 && (matrix2.rows == matrix1.rows)) { //CAREFUL: ONLY VERTICAL VECTORS ARE IMPLEMENTED, TRANSPOSED ARE NOT YET INCLUDED, CAN LEAD TO ISSUES - //TODO: IMPLEMENT OPTION TO GIVE VECTOR AS FIRST FUNCTION ARGUMENT printf("Got broadcast\n"); newmatrix.cols = matrix1.cols; newmatrix.rows = matrix1.rows; @@ -75,7 +73,23 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return newmatrix; } -/*Matrix multiply(const Matrix matrix1, const Matrix matrix2) +Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - -}*/ \ No newline at end of file + Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0}; + if(matrix1.cols == matrix2.rows){ + newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType)); + newmatrix.rows = matrix1.rows; + newmatrix.cols = matrix2.cols; + for(int i = 0; i < newmatrix.rows; i++){ + for(int j = 0; j < newmatrix.cols; j++){ + //Set matrix entry to 0 + *(newmatrix.buffer+i*newmatrix.cols+j) = 0; + for(int k = 0; k < matrix1.cols; k++){ + //Increment matrix entry + *(newmatrix.buffer+i*newmatrix.cols+j) += *(matrix1.buffer+i*matrix1.cols+k) * *(matrix2.buffer+k*matrix2.cols+j); + } + } + } + } + return newmatrix; +} \ No newline at end of file diff --git a/Startcode/matrix.h b/Startcode/matrix.h index 94c5ad2..55dae77 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -16,7 +16,7 @@ void clearMatrix(Matrix *matrix); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); Matrix add(const Matrix matrix1, const Matrix matrix2); -//Matrix multiply(const Matrix matrix1, const Matrix matrix2); +Matrix multiply(const Matrix matrix1, const Matrix matrix2); #endif