#include #include #include "matrix.h" // TODO Matrix-Funktionen implementieren //Matrix dimensionieren Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix m; //Struktur anlegen, Varibale m von Typ Matrix //Sonderfall aus Unit-Test, wenn rows == 0 oder cols == 0, darf kein Speicher allokiert werden if(rows==0 || cols==0){ m.rows = 0; m.cols = 0; m.buffer = NULL; //kein gültiger Speicher return m; } //Normalfall m.rows = rows; // strukurvariable.feldname --> Struktur-Zugriffsoperator m.cols = cols; m.buffer = malloc((rows * cols) * sizeof(MatrixType)); //Speicher reserviert für Elemente return m; } void clearMatrix(Matrix *matrix) //um Matrix wieder aufzuräumen { // falls Speicher existiert (buffer NICHT NULL ist): freigeben if(matrix->buffer != NULL) { free(matrix->buffer);// Speicher freigegeben aber zeigt irgendwo hin (dangling pointer) } //Matrix in definierten leeren Zustand setzen matrix->buffer = NULL; // dangling pointer zurücksetzen matrix->rows = 0; matrix->cols = 0; } //Wert in Matrix schreiben und wo genau void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { // Sicherheit: wenn buffer == NULL (kein gültiger Speicher) oder Index außerhalb der Matrix --> return if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) { return; } //index = Zeile * Anzahl_Spalten + Spalte unsigned int index = rowIdx * matrix.cols + colIdx; // Schreibt value direkt an die berechnete Position im Matrixspeicher matrix.buffer[index] = value; } 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) { return 0; //funktioniert nur wegen #define UNDEFINED_MATRIX_VALUE 0 in matrix.h } unsigned int index = rowIdx * matrix.cols + colIdx; return matrix.buffer[index]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix result; //Zeilen müssen gleich sein if (matrix1.rows != matrix2.rows) //Wenn nicht gleich, dann ungültige Matrix zurückgeben { result.rows = 0; result.cols = 0; result.buffer = NULL; return result; } //Spalten müssen gleich sein (mit broadcasting) //Fälle: gleiche Spalten ok, matrix1 hat 1 Spalte, matrix2 hat 1 Spalte //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) { //keine Bedingung erfüllt --> Addition nicht möglich result.rows = 0; result.cols = 0; result.buffer = NULL; return result; } //Ergebnis dimension bestimmen //Zeilen sind immer die gleichen wie bei den Inputs 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; //Speicher reservieren für Ergebnis-Matrix result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); //Addition mit Broadcasting for (unsigned int r = 0; r < result.rows; r++) //jede Zeile durchlaufen { for (unsigned int c = 0; c < result.cols; c++) //jede Spalte durchlaufen { // Bestimme Spalte für matrix1: // Wenn nur 1 Spalte -> immer Spalte 0 benutzen unsigned int c1 = (matrix1.cols == 1) ? 0 : c; // Bestimme Spalte für matrix2: unsigned int c2 = (matrix2.cols == 1) ? 0 : c; // Werte aus beiden Matrizen holen MatrixType v1 = getMatrixAt(matrix1, r, c1); MatrixType v2 = getMatrixAt(matrix2, r, c2); //Beide Werte addieren und in result speichern setMatrixAt(v1 + v2, result, r, c); } } return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix result; //Matrixmultiplikation: innere Dimensionen müssen übereinstimmen if(matrix1.cols != matrix2.rows) { result.rows = 0; result.cols = 0; result.buffer = NULL; return result; } //Größe der Ergebnismatrix bestimmen result.rows = matrix1.rows; result.cols = matrix2.cols; //Speicher für Ergebnis result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); //Matrixmultiplikation durchführen for (unsigned int r = 0; r < result.rows; r++) { for (unsigned int c = 0; c < result.cols; c++) { MatrixType sum = 0; // gemeinsame Dimension = matrix1.cols = matrix2.rows for (unsigned int i = 0; i < matrix1.cols; i++) { MatrixType a = getMatrixAt(matrix1, r, i); MatrixType b = getMatrixAt(matrix2, i, c); sum += a * b; } //Summe in Ergebniszelle schreiben setMatrixAt(sum, result, r, c); } } return result; }