Restliche Funktionen

This commit is contained in:
Jochen Duernberger 2025-11-20 14:36:33 +01:00
parent ea24a8e34e
commit 502a73aa42
3 changed files with 142 additions and 6 deletions

28
.vscode/tasks.json vendored Normal file
View File

@ -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"
}

119
matrix.c
View File

@ -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;
}

View File

@ -6,6 +6,7 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
// *****************************************************
typedef struct {
unsigned int rows;
unsigned int cols;