From 502a73aa42954aae597b605d6ea5b2e6ff0c5fb1 Mon Sep 17 00:00:00 2001 From: duernbergerjo100488 Date: Thu, 20 Nov 2025 14:36:33 +0100 Subject: [PATCH] Restliche Funktionen --- .vscode/tasks.json | 28 +++++++++++ matrix.c | 119 ++++++++++++++++++++++++++++++++++++++++++--- matrix.h | 1 + 3 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..416883d --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\ProgramData\\mingw64\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "test", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/matrix.c b/matrix.c index ad00628..bc11915 100644 --- a/matrix.c +++ b/matrix.c @@ -4,32 +4,139 @@ // TODO Matrix-Funktionen implementieren +/* Matrix createMatrix(unsigned int rows, unsigned int cols) { - + + Matrix result; + + // Sonderfall: ungültige Dimensionen + if (rows == 0 || cols == 0) + { + result.rows = 0; + result.cols = 0; + result.buffer = NULL; + return result; + } + + // Speicher für rows * cols Elemente vom Typ MatrixType reservieren + result.buffer = malloc(rows * cols * sizeof(MatrixType)); + + // Wenn malloc fehlschlägt, Matrix als leer zurückgeben + if (result.buffer == NULL) + { + result.rows = 0; + result.cols = 0; + return result; + } + + // Erfolgreiche Initialisierung + result.rows = rows; + result.cols = cols; + return result; } +*/ + +// Erstellt eine neue Matrix mit gegebener Zeilen- und Spaltenanzahl +Matrix createMatrix(unsigned int rows, unsigned int cols) +{ + Matrix matrix = {0, 0, NULL}; // Initialisierung mit leeren Werten + if (rows == 0 || cols == 0) return matrix; // Ungültige Dimensionen → leere Matrix zurückgeben + + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); // Speicher für die Matrixdaten reservieren + + if (matrix.buffer == NULL) { // Falls Speicherreservierung fehlschlägt + matrix.rows = 0; + matrix.cols = 0; + } + + return matrix; +} + +// Gibt den Speicher einer Matrix frei und setzt die Werte auf Null void clearMatrix(Matrix *matrix) { - + if (matrix->buffer != NULL) { + free(matrix->buffer); // Speicher freigeben + matrix->buffer = NULL; + } + matrix->rows = 0; + matrix->cols = 0; } +// Setzt einen Wert an eine bestimmte Position in der Matrix void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx >= matrix.rows || colIdx >= matrix.cols || matrix.buffer == NULL) return; + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // Indexberechnung: Zeile * Spaltenanzahl + Spalte } +// Gibt den Wert an einer bestimmten Position zurück MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx >= matrix.rows || colIdx >= matrix.cols || matrix.buffer == NULL) + return UNDEFINED_MATRIX_VALUE; // Rückgabe eines Standardwerts bei ungültigem Zugriff + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } +// Addiert zwei Matrizen miteinander (entweder elementweise oder mit Broadcasting) Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = {0, 0, NULL}; + + // Elementweise Addition: gleiche Dimensionen + if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) { + result = createMatrix(matrix1.rows, matrix1.cols); + if (result.buffer == NULL) return result; + + for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; ++i) + result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; + + return result; + } + + // Broadcasting: eine Matrix hat nur eine Spalte (Spaltenvektor) + if (matrix1.rows == matrix2.rows && (matrix1.cols == 1 || matrix2.cols == 1)) { + Matrix matrix = matrix1.cols == 1 ? matrix1 : matrix2; // Vektor + Matrix other = matrix1.cols == 1 ? matrix2 : matrix1; // Matrix + + result = createMatrix(other.rows, other.cols); + if (result.buffer == NULL) return result; + + for (unsigned int r = 0; r < other.rows; ++r) { + MatrixType val = matrix.buffer[r]; // Wert aus dem Vektor + for (unsigned int c = 0; c < other.cols; ++c) + result.buffer[r * other.cols + c] = other.buffer[r * other.cols + c] + val; + } + return result; + } + + return result; // Rückgabe einer leeren Matrix bei inkompatiblen Dimensionen } +// Multipliziert zwei Matrizen miteinander (Matrixmultiplikation) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = {0, 0, NULL}; + + if (matrix1.cols != matrix2.rows) return result; // Dimensionen müssen passen: A.cols == B.rows + + result = createMatrix(matrix1.rows, matrix2.cols); + if (result.buffer == NULL) return result; + + // Matrixmultiplikation: Zeile von A mit Spalte von B multiplizieren + for (unsigned int i = 0; i < matrix1.rows; ++i) { + for (unsigned int j = 0; j < matrix2.cols; ++j) { + MatrixType sum = 0; + for (unsigned int k = 0; k < matrix1.cols; ++k) { + sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); + } + setMatrixAt(sum, result, i, j); + } + } + + return result; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index 132c143..00942ca 100644 --- a/matrix.h +++ b/matrix.h @@ -6,6 +6,7 @@ typedef float MatrixType; // TODO Matrixtyp definieren +// ***************************************************** typedef struct { unsigned int rows; unsigned int cols;