neueste Version mit Kommentaren

This commit is contained in:
Harun Faizi 2025-11-27 18:47:15 +01:00
parent 22588d9b0e
commit fffb6dca39
5 changed files with 31 additions and 13 deletions

5
.idea/misc.xml generated
View File

@ -5,6 +5,11 @@
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<MakefileProjectSettings> <MakefileProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
<option name="version" value="2" /> <option name="version" value="2" />
</MakefileProjectSettings> </MakefileProjectSettings>
</option> </option>

View File

@ -13,7 +13,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
if(rows==0 || cols==0){ if(rows==0 || cols==0){
m.rows = 0; m.rows = 0;
m.cols = 0; m.cols = 0;
m.buffer = NULL; m.buffer = NULL; //kein gültiger Speicher
return m; return m;
} }
@ -25,7 +25,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
return m; return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix) //um Matrix wieder aufzuräumen
{ {
// falls Speicher existiert (buffer NICHT NULL ist): freigeben // falls Speicher existiert (buffer NICHT NULL ist): freigeben
if(matrix->buffer != NULL) if(matrix->buffer != NULL)
@ -54,11 +54,11 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
matrix.buffer[index] = value; matrix.buffer[index] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) //liest wert an bestimmter Pos.
{ {
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
{ {
return 0; return 0; //funktioniert nur wegen #define UNDEFINED_MATRIX_VALUE 0 in matrix.h
} }
unsigned int index = rowIdx * matrix.cols + colIdx; unsigned int index = rowIdx * matrix.cols + colIdx;
@ -71,7 +71,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix result; Matrix result;
//Zeilen müssen gleich sein //Zeilen müssen gleich sein
if (matrix1.rows != matrix2.rows) if (matrix1.rows != matrix2.rows) //Wenn nicht gleich, dann ungültige Matrix zurückgeben
{ {
result.rows = 0; result.rows = 0;
result.cols = 0; result.cols = 0;
@ -83,22 +83,28 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
//Spalten müssen gleich sein (mit broadcasting) //Spalten müssen gleich sein (mit broadcasting)
//Fälle: gleiche Spalten ok, matrix1 hat 1 Spalte, matrix2 hat 1 Spalte //Fälle: gleiche Spalten ok, matrix1 hat 1 Spalte, matrix2 hat 1 Spalte
//sonst inkompatibel //sonst inkompatibel
//Broadcasting --> 3x1 + 3x4 ist möglich, da 3x1 zu 3x4 wird und die erste Spalte einfach kopiert wird in andere Spalten
if (matrix1.cols != matrix2.cols && matrix1.cols != 1 && matrix2.cols != 1) if (matrix1.cols != matrix2.cols && matrix1.cols != 1 && matrix2.cols != 1)
{ { //keine Bedingung erfüllt --> Addition nicht möglich
result.rows = 0; result.rows = 0;
result.cols = 0; result.cols = 0;
result.buffer = NULL; result.buffer = NULL;
return result; return result;
} }
//Ergebnis dimension bestimmen
//Zeilen sind immer die gleichen wie bei den Inputs
result.rows = matrix1.rows; result.rows = matrix1.rows;
//Broadcasting Überprüft --> nimmt immer die grössere Spalte der beiden Matrizen
result.cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols; result.cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
//Speicher reservieren für Ergebnis-Matrix
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
for (unsigned int r = 0; r < result.rows; r++)
//Addition mit Broadcasting
for (unsigned int r = 0; r < result.rows; r++) //jede Zeile durchlaufen
{ {
for (unsigned int c = 0; c < result.cols; c++) for (unsigned int c = 0; c < result.cols; c++) //jede Spalte durchlaufen
{ {
// Bestimme Spalte für matrix1: // Bestimme Spalte für matrix1:
// Wenn nur 1 Spalte -> immer Spalte 0 benutzen // Wenn nur 1 Spalte -> immer Spalte 0 benutzen
@ -107,9 +113,11 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
// Bestimme Spalte für matrix2: // Bestimme Spalte für matrix2:
unsigned int c2 = (matrix2.cols == 1) ? 0 : c; unsigned int c2 = (matrix2.cols == 1) ? 0 : c;
// Werte aus beiden Matrizen holen
MatrixType v1 = getMatrixAt(matrix1, r, c1); MatrixType v1 = getMatrixAt(matrix1, r, c1);
MatrixType v2 = getMatrixAt(matrix2, r, c2); MatrixType v2 = getMatrixAt(matrix2, r, c2);
//Beide Werte addieren und in result speichern
setMatrixAt(v1 + v2, result, r, c); setMatrixAt(v1 + v2, result, r, c);
} }
} }
@ -122,6 +130,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result; Matrix result;
//Matrixmultiplikation: innere Dimensionen müssen übereinstimmen
if(matrix1.cols != matrix2.rows) if(matrix1.cols != matrix2.rows)
{ {
result.rows = 0; result.rows = 0;
@ -130,12 +139,14 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return result; return result;
} }
//Größe der Ergebnismatrix bestimmen
result.rows = matrix1.rows; result.rows = matrix1.rows;
result.cols = matrix2.cols; result.cols = matrix2.cols;
//Speicher für Ergebnis
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
//Matrixmultiplikation durchführen
for (unsigned int r = 0; r < result.rows; r++) for (unsigned int r = 0; r < result.rows; r++)
{ {
for (unsigned int c = 0; c < result.cols; c++) for (unsigned int c = 0; c < result.cols; c++)
@ -150,7 +161,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
sum += a * b; sum += a * b;
} }
//Summe in Ergebniszelle schreiben
setMatrixAt(sum, result, r, c); setMatrixAt(sum, result, r, c);
} }
} }

View File

@ -6,10 +6,12 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct { typedef struct {
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType* buffer; //buffer Pointer zeigt auf Heap, mit malloc dort dann Speicher reservieren MatrixType* buffer; //Zeiger auf dynamisch reservierten Speicher, damit Matrix beliebig groß zur Laufzeit
} Matrix; } Matrix;

BIN
matrix.o

Binary file not shown.

Binary file not shown.