matrix.c aktualisiert

This commit is contained in:
Simon Schuerer 2025-11-14 08:11:34 +00:00
parent 6fa4d9c084
commit 57857eb5d9

View File

@ -4,72 +4,80 @@
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
MatrixTyp erstelleMatrix(unsigned int reihen, unsigned int spalten) MatrixType createMatrix(unsigned int rows, unsigned int cols)
{ {
MatrixTyp m; MatrixType m;
m.reihen = reihen; m.rows = rows;
m.spalten = spalten; m.cols = cols;
m.werte = (MatrixWert *)malloc(reihen * spalten * sizeof(MatrixWert)); m.values = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
for (unsigned int i = 0; i < reihen * spalten; ++i) { if (m.values == NULL) {
m.werte[i] = UNDEFINIERTER_MATRIXWERT; m.rows = 0;
m.cols = 0;
return m;
}
for (unsigned int i = 0; i < rows * cols; ++i) {
m.values[i] = 0.0; // Standardwert
} }
return m; return m;
} }
void loescheMatrix(MatrixTyp *matrix) void clearMatrix(Matrix *matrix)
{ {
free(matrix->werte); if (matrix->values != NULL) {
matrix->werte = NULL; free(matrix->values);
matrix->reihen = 0; }
matrix->spalten = 0; matrix->values = NULL;
matrix->rows = 0;
matrix->cols = 0;
} }
void setzeMatrixWert(MatrixWert wert, MatrixTyp matrix, unsigned int reiheIndex, unsigned int spalteIndex) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (reiheIndex < matrix.reihen && spalteIndex < matrix.spalten) { if (rowIdx < matrix.rows && colIdx < matrix.cols) {
matrix.werte[reiheIndex * matrix.spalten + spalteIndex] = wert; matrix.values[rowIdx * matrix.cols + colIdx] = value;
} }
} }
MatrixWert holeMatrixWert(const MatrixTyp matrix, unsigned int reiheIndex, unsigned int spalteIndex) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (reiheIndex < matrix.reihen && spalteIndex < matrix.spalten) { if (rowIdx < matrix.rows && colIdx < matrix.cols) {
return matrix.werte[reiheIndex * matrix.spalten + spalteIndex]; return matrix.values[rowIdx * matrix.cols + colIdx];
} }
return UNDEFINIERTER_MATRIXWERT; return 0.0; // Fallback-Wert
} }
MatrixTyp addiereMatrix(const MatrixTyp matrix1, const MatrixTyp matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.reihen != matrix2.reihen || matrix1.spalten != matrix2.spalten) { Matrix result = createMatrix(0, 0);
return erstelleMatrix(0, 0); if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) {
return result;
} }
MatrixTyp ergebnis = erstelleMatrix(matrix1.reihen, matrix1.spalten); result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.reihen; ++i) { for (unsigned int i = 0; i < matrix1.rows; ++i) {
for (unsigned int j = 0; j < matrix1.spalten; ++j) { for (unsigned int j = 0; j < matrix1.cols; ++j) {
MatrixWert wert = holeMatrixWert(matrix1, i, j) + holeMatrixWert(matrix2, i, j); MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setzeMatrixWert(wert, ergebnis, i, j); setMatrixAt(sum, result, i, j);
} }
} }
return ergebnis; return result;
} }
MatrixTyp multipliziereMatrix(const MatrixTyp matrix1, const MatrixTyp matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.spalten != matrix2.reihen) { Matrix result = createMatrix(0, 0);
return erstelleMatrix(0, 0); if (matrix1.cols != matrix2.rows) {
return result;
} }
MatrixTyp ergebnis = erstelleMatrix(matrix1.reihen, matrix2.spalten); result = createMatrix(matrix1.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix1.reihen; ++i) { for (unsigned int i = 0; i < matrix1.rows; ++i) {
for (unsigned int j = 0; j < matrix2.spalten; ++j) { for (unsigned int j = 0; j < matrix2.cols; ++j) {
MatrixWert summe = 0; MatrixType sum = 0.0;
for (unsigned int k = 0; k < matrix1.spalten; ++k) { for (unsigned int k = 0; k < matrix1.cols; ++k) {
summe += holeMatrixWert(matrix1, i, k) * holeMatrixWert(matrix2, k, j); sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
} }
setzeMatrixWert(summe, ergebnis, i, j); setMatrixAt(sum, result, i, j);
} }
} }
return ergebnis; return result;
}