Implemented matrix mutliplication

This commit is contained in:
lukas committed 2026-11-05 10:28:27 +01:00
1 parent f1556b18ba
commit 031341d455
2 files changed
+20 -6

No files matched your search

+19 -5
View File
@@ -3,7 +3,6 @@
#include "matrix.h"
#include <ctype.h>
#include <stdio.h>
// 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)
{
}*/
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;
}
+1 -1
View File
@@ -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