Author SHA1 Message Date
Mihaela Creciun e30fc66453 Multiply Funktion in matrix.c 2026-11-12 09:55:43 +01:00
Mihaela Creciun 5583ecd1ba Matrix-Struktur definieren 2026-11-12 01:46:53 +01:00
Mihaela Creciun e4c7ec1a59 Multiply Funktion in matrix.c 2026-11-11 23:38:06 +01:00
2 changed files with 40 additions and 1 deletions

No files matched your search

+39
View File
@@ -31,5 +31,44 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
// Überprüfen, ob die Matrizen gültig sind und ob ihre Dimensionen eine Multiplikation erlauben
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
return (Matrix){0};
}
// Ergebnismatrix erstellen
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
// Überprüfen, ob die Speicherreservierung erfolgreich war
if (result.buffer == NULL)
{
return (Matrix){0};
}
// Jede Zeile der ersten Matrix durchlaufen
for (unsigned int i = 0; i < matrix1.rows; i++)
{
// Jede Spalte der zweiten Matrix durchlaufen
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
// Summe der Produkte berechnen
for (unsigned int k = 0; k < matrix1.cols; k++)
{
sum += matrix1.buffer[i * matrix1.cols + k] *
matrix2.buffer[k * matrix2.cols + j];
}
// Berechneten Wert in der Ergebnismatrix speichern
result.buffer[i * result.cols + j] = sum;
}
}
return result;
}
+1 -1
View File
@@ -9,7 +9,7 @@ typedef float MatrixType;
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *buffer;
MatrixType *buffer; // Speichert die Matrixwerte in einem Array, die Speicheradresse des ersten Matrixwerts
} Matrix;