generated from freudenreichan/info2Praktikum-NeuronalesNetz
matrix.c alle Funktionen eingefügt
This commit is contained in:
1 parent
b9e2658f52
commit
b22aa5c3bc
1 file changed
+46
@@ -163,5 +163,51 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2){
|
||||
|
||||
//!wenn ich eine 3x4 mit einer 4x3 matrix multipliziere kommt eine 3x3 raus!
|
||||
// 3x4 mit 5x3 ist mathematisch nicht möglich!
|
||||
// A ⋅ B ist im Allgemeinen nicht dasselbe wie B ⋅ A -> Kein Tausch von A & B nötig wenn m nicht gleich groß ist (n x m) * (m x k)
|
||||
|
||||
// Leere Ergebnismatrix für Fehlerfälle erstellen
|
||||
Matrix multmatrix = {0, 0, NULL};
|
||||
|
||||
// Prüfen, ob beide Matrizen Speicher besitzen
|
||||
if(matrix1.space == NULL || matrix2.space == NULL){
|
||||
fprintf(stderr, "Error, one or more matrices without memory...\n");
|
||||
return multmatrix;
|
||||
}
|
||||
|
||||
|
||||
// Prüfen, ob Matrizen multipliziert werden dürfen
|
||||
// (n x m) * (m x k) = (n x k) / Spalten der ersten Matrix == Zeilen der zweitne Matrix
|
||||
if(matrix1.cols != matrix2.rows){ //Sind die beiden Ungleich, darf nicht gerechnet werden
|
||||
fprintf(stderr, "Error, incompatible matrix dimensions...\n");
|
||||
return multmatrix;
|
||||
}
|
||||
|
||||
|
||||
//Ergebnismatrix mit der richtigen Dimmension erstellen
|
||||
multmatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
if(multmatrix.space == NULL){ //Prüfen ob Matrix erfolgreich erstellt wurde
|
||||
fprintf(stderr, "Error, memory allocation failed...\n");
|
||||
return multmatrix;
|
||||
}
|
||||
|
||||
|
||||
//Multiplikation durchführen
|
||||
//Es ist immer: Ergebnismatrix [row,col] = Summe aller {matrix1[row1]*matrix2[col1]}
|
||||
|
||||
for(unsigned int i = 0; i < multmatrix.rows; i++){ //Zeilenanzahl erhöhen
|
||||
for(unsigned int j = 0; j < multmatrix.cols; j++){ //Spaltenanzahl erhöhen
|
||||
|
||||
MatrixType summe = 0;
|
||||
|
||||
// Skalarprodukt aus Zeile und Spalte berechnen
|
||||
for(unsigned int k = 0; k < matrix1.cols; k++){
|
||||
summe = summe + (getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j));
|
||||
}
|
||||
|
||||
multmatrix.space[(i * multmatrix.cols) + j] = summe;
|
||||
}
|
||||
}
|
||||
|
||||
return multmatrix;
|
||||
}
|
||||
Reference in new issue
Block a user