generated from freudenreichan/info2Praktikum-NeuronalesNetz
Funktion add in Matrix.c verbessert und verfollständigt
This commit is contained in:
1 parent
1cc99ac04b
commit
b9e2658f52
1 file changed
+26
-26
+26
-26
@@ -5,28 +5,28 @@
|
|||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols){
|
Matrix createMatrix(unsigned int rows, unsigned int cols){
|
||||||
/*
|
/*
|
||||||
unsigned int memory_depth = 0;
|
unsigned int memory_depth = 0;
|
||||||
memory_depth = rows * cols; //Meine Speichertiefe ist die Anzahl meiner Spalten mal die Reihen
|
memory_depth = rows * cols; //Meine Speichertiefe ist die Anzahl meiner Spalten mal die Reihen
|
||||||
|
|
||||||
unsigned int memory_reservation = 0;
|
unsigned int memory_reservation = 0;
|
||||||
memory_reservation = memory_depth * sizeof(MatrixType); //Nimmt die Speichertiefe mal die größe der einzelnen Speicherinhalte (float)
|
memory_reservation = memory_depth * sizeof(MatrixType); //Nimmt die Speichertiefe mal die größe der einzelnen Speicherinhalte (float)
|
||||||
|
|
||||||
Matrix.space = malloc(memory_reservation); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger space (malloc "merkt" sich die Speichergröße an der adresse space)
|
Matrix.space = malloc(memory_reservation); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger space (malloc "merkt" sich die Speichergröße an der adresse space)
|
||||||
*/
|
*/
|
||||||
Matrix matrix; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix.
|
Matrix matrix; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix.
|
||||||
|
|
||||||
matrix.rows = rows;
|
matrix.rows = rows;
|
||||||
matrix.cols = cols;
|
matrix.cols = cols;
|
||||||
matrix.space = malloc(rows * cols * sizeof(MatrixType));
|
matrix.space = malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
|
||||||
if (matrix.space == NULL) {
|
if (matrix.space == NULL) {
|
||||||
fprintf(stderr, "Error, matrix could not be created...\n");
|
fprintf(stderr, "Error, matrix could not be created...\n");
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,21 +84,21 @@ Matrix add(const Matrix matrix1, const Matrix matrix2){
|
|||||||
// Leere Ergebnismatrix für Fehlerfälle erstellen
|
// Leere Ergebnismatrix für Fehlerfälle erstellen
|
||||||
Matrix summatrix = {0, 0, NULL};
|
Matrix summatrix = {0, 0, NULL};
|
||||||
|
|
||||||
|
|
||||||
// Prüfen, ob beide Matrizen Speicher besitzen
|
// Prüfen, ob beide Matrizen Speicher besitzen
|
||||||
if(matrix1.space == NULL || matrix2.space == NULL){
|
if(matrix1.space == NULL || matrix2.space == NULL){
|
||||||
fprintf(stderr, "Error, one or more matrices without memory...\n");
|
fprintf(stderr, "Error, one or more matrices without memory...\n");
|
||||||
return summatrix;
|
return summatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Prüfen, ob die Zeilenanzahl übereinstimmt, weil nur zwei Matrizen mit den exakt gleichen Dimmensionen addiert werden dürfen
|
// Prüfen, ob die Zeilenanzahl übereinstimmt, weil nur zwei Matrizen mit den exakt gleichen Dimmensionen addiert werden dürfen
|
||||||
if(matrix1.rows != matrix2.rows){
|
if(matrix1.rows != matrix2.rows){
|
||||||
fprintf(stderr, "Error, incompatible matrix dimensions...\n");
|
fprintf(stderr, "Error, incompatible matrix dimensions...\n");
|
||||||
return summatrix;
|
return summatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Spaltenanzahl der Ergebnismatrix bestimmen
|
// Spaltenanzahl der Ergebnismatrix bestimmen
|
||||||
unsigned int resultcols = 0;
|
unsigned int resultcols = 0;
|
||||||
|
|
||||||
@@ -116,16 +116,16 @@ Matrix add(const Matrix matrix1, const Matrix matrix2){
|
|||||||
return summatrix;
|
return summatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Ergebnismatrix mit der richtigen Dimmension erstellen
|
//Ergebnismatrix mit der richtigen Dimmension erstellen
|
||||||
summatrix = createMatrix(matrix1.rows, resultcols);
|
summatrix = createMatrix(matrix1.rows, resultcols);
|
||||||
|
|
||||||
if(summatrix.space == NULL){ //Prüfen ob Matrix erfolgreich erstellt wurde
|
if(summatrix.space == NULL){ //Prüfen ob Matrix erfolgreich erstellt wurde
|
||||||
fprintf(stderr, "Error, memory allocation failed...\n");
|
fprintf(stderr, "Error, memory allocation failed...\n");
|
||||||
return summatrix;
|
return summatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Addition durchführen
|
// Addition durchführen
|
||||||
for(unsigned int i = 0; i < summatrix.rows; i++){ //Zeilenanzahl erhöhen
|
for(unsigned int i = 0; i < summatrix.rows; i++){ //Zeilenanzahl erhöhen
|
||||||
for(unsigned int j = 0; j < summatrix.cols; j++){ //Spaltenanzahl erhöhen
|
for(unsigned int j = 0; j < summatrix.cols; j++){ //Spaltenanzahl erhöhen
|
||||||
@@ -153,8 +153,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2){
|
|||||||
summatrix.space[(i * summatrix.cols) + j] = value1 + value2; //Wert in neue matrix eintragen/!Erklärung von [(i* summatrix.cols) + j] in "getMatrixAt"!
|
summatrix.space[(i * summatrix.cols) + j] = value1 + value2; //Wert in neue matrix eintragen/!Erklärung von [(i* summatrix.cols) + j] in "getMatrixAt"!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return summatrix;
|
return summatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -164,4 +164,4 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2){
|
|||||||
//!wenn ich eine 3x4 mit einer 4x3 matrix multipliziere kommt eine 3x3 raus!
|
//!wenn ich eine 3x4 mit einer 4x3 matrix multipliziere kommt eine 3x3 raus!
|
||||||
// 3x4 mit 5x3 ist mathematisc hnicht möglich!
|
// 3x4 mit 5x3 ist mathematisc hnicht möglich!
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user