Matrizen-Funktionen funktionieren wie in Matrix Tests gewünscht

This commit is contained in:
Manuel Nitsche 2025-11-19 10:38:05 +01:00
parent 8e67db0dd0
commit b2408e6f82
2 changed files with 51 additions and 25 deletions

View File

@ -6,11 +6,17 @@
// Matrix erstellen
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
// Falls eine Matrix eine Null Dimension hat leere Matrix ausgeben
if (rows ==0 || cols == 0){
Matrix empty = {0, 0, NULL};
return empty;
}
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
// Speicher erstellen und nullsetzen
matrix.buffer = (int *)calloc(rows * cols, sizeof(double));
matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
return matrix;
}
@ -34,30 +40,27 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Prüfen ob Indizes im gültigen Bereich sind
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
return 0;
}
// Prüfen ob was im Buffer ist
if (matrix.buffer == NULL) {
return 0;
}
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
// Neue Matrix mit passender Dimension erstellen
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
// Alle Elemente einzeln addieren
for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++) {
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) {
// Broadcasting Fall 1: matrix1 ist ein Spaltenvektor
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) {
// Neue Matrix mit passender Dimension erstellen
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
// Vektor zu jeder Spalte addieren
// Vektor zu jeder Spalte addieren
for (unsigned int i = 0; i < result.rows; i++) {
for (unsigned int j = 0; j < result.cols; j++) {
MatrixType vectorValue = getMatrixAt(matrix1, i, 0);
@ -68,22 +71,45 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return result;
}
else if (matrix2.cols == 1 && matrix2.rows == matrix1.rows){
// Broadcasting Fall 2: matrix2 ist ein Spaltenvektor
else if (matrix2.cols == 1 && matrix2.rows == matrix1.rows) {
// Neue Matrix mit passender Dimension erstellen
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
// Vektor zu jeder Spalte addieren
for (unsigned int i = 0; i < result.rows; i++) {
for (unsigned int j = 0; j < result.cols; j++) {
MatrixType vectorValue = getMatrixAt(matrix1, i, j);
MatrixType matrixValue = getMatrixAt(matrix2, i, 0);
setMatrixAt(vectorValue + matrixValue, result, i, j);
MatrixType matrixValue = getMatrixAt(matrix1, i, j);
MatrixType vectorValue = getMatrixAt(matrix2, i, 0);
setMatrixAt(matrixValue + vectorValue, result, i, j);
}
}
return result;
}
// Normale elementweise Addition: Dimensionen müssen übereinstimmen
else if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
// Neue Matrix mit passender Dimension erstellen
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
// Alle Elemente einzeln addieren
for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++) {
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
}
return result;
}
// Fehlerfall: Dimensionen passen nicht
else {
Matrix empty = {0, 0, NULL};
return empty;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
// Prüfen ob Matrizen multipiziert werden können
if (matrix1.cols != matrix2.rows) {
// Fehlerfall: Leere Matrix zurückgeben
@ -91,7 +117,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return empty;
}
// Neue Matrix mit passender Dimension erstellen
Matrix result = createMatrix(matrix1.cols, matrix2.rows);
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
// Matrix-Multiplikation durchführen
for (unsigned int i = 0; i < result.rows; i++) {
@ -109,5 +135,5 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
}
}
return result;
}
}

View File

@ -10,7 +10,7 @@ typedef struct
{
int rows;
int cols;
int *buffer;
float *buffer;
} Matrix;