Funktion add in Matrix.c hinzugefügt

This commit is contained in:
KohlerMaxi committed 2026-11-08 17:58:55 +01:00
1 parent d56dfd6622
commit 1cc99ac04b
1 file changed
+68 -26
+68 -26
View File
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -80,46 +81,87 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2){
//Matrix erstellen, in der die additionsmatrix gespeichert wird
unsigned int maxrows = 0;
unsigned int maxcols = 0;
if(matrix1.rows >= matrix2.rows){maxrows = matrix1.rows;}
//Maximale Anzahlen der Reihen bestimmen
else {maxrows = matrix2.rows;}
if(matrix1.cols >= matrix2.cols){maxcols = matrix1.cols;}
//Maximale Anzahl der Zeilen bestimmen
else {maxcols = matrix2.cols;}
Matrix summatrix = createMatrix(maxrows, maxcols); //Neue Matrix erstellen
// Leere Ergebnismatrix für Fehlerfälle erstellen
Matrix summatrix = {0, 0, NULL};
//Checken ob Matrizenen werte haben
if(matrix1.space == 0 || matrix2.space ==0){
fprintf(stderr, "Error, addition of matrices was not possible...\n");
summatrix.rows = 0;
summatrix.cols = 0;
// 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 summatrix;
}
//Prüfen welches Additionsverfahren genurtz werden soll
if((matrix1.cols == 1 || matrix2.cols == 1) && matrix1.cols == matrix2.cols){ //Erkennung ob eine der Matrizen ein Vektor ist -> Broadcasting addition
// Prüfen, ob die Zeilenanzahl übereinstimmt, weil nur zwei Matrizen mit den exakt gleichen Dimmensionen addiert werden dürfen
if(matrix1.rows != matrix2.rows){
fprintf(stderr, "Error, incompatible matrix dimensions...\n");
return summatrix;
}
else{
for(int i = 0; i > summatrix.rows; i++){
summatrix.space[i]=
// Spaltenanzahl der Ergebnismatrix bestimmen
unsigned int resultcols = 0;
if(matrix1.cols == matrix2.cols){ //Kein Spaltenvektor, beide gleich groß
resultcols = matrix1.cols;
}
else if(matrix1.cols == 1){ //Matrix 1 ist Spaltenvektor -> Matrix 2 gibt Spaltengröße vor
resultcols = matrix2.cols;
}
else if(matrix2.cols == 1){ //Matrix 2 ist Spaltenvektor -> Matrix 1 gibt Spaltengröße vor
resultcols = matrix1.cols;
}
else{ //Kein Spaltenvektor, beide unterschiedlich groß -> Fehler
fprintf(stderr, "Error, incompatible matrix dimensions...\n");
return summatrix;
}
//Ergebnismatrix mit der richtigen Dimmension erstellen
summatrix = createMatrix(matrix1.rows, resultcols);
if(summatrix.space == NULL){ //Prüfen ob Matrix erfolgreich erstellt wurde
fprintf(stderr, "Error, memory allocation failed...\n");
return summatrix;
}
// Addition durchführen
for(unsigned int i = 0; i < summatrix.rows; i++){ //Zeilenanzahl erhöhen
for(unsigned int j = 0; j < summatrix.cols; j++){ //Spaltenanzahl erhöhen
MatrixType value1 = 0; //Platzhalterwerte bis bestimmt ist ob es ein Spaltenvektor ist oder nicht
MatrixType value2 = 0; //Platzhalterwerte bis bestimmt ist ob es ein Spaltenvektor ist oder nicht
// Wert aus Matrix 1 holen
if(matrix1.cols == 1){ //Für Spaltenvektor
value1 = getMatrixAt(matrix1, i, 0);
}
else{ //Für normale Matrix
value1 = getMatrixAt(matrix1, i, j);
}
// Wert aus Matrix 2 holen
if(matrix2.cols == 1){ //Für Spaltenvektor
value2 = getMatrixAt(matrix2, i, 0);
}
else{ //Für normale Matrix
value2 = getMatrixAt(matrix2, i, j);
}
// Werte addieren und speichern
summatrix.space[(i * summatrix.cols) + j] = value1 + value2; //Wert in neue matrix eintragen/!Erklärung von [(i* summatrix.cols) + j] in "getMatrixAt"!
}
}
return summatrix;
}
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 mathematisc hnicht möglich!
}