diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..dc4c5b1 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -1,35 +1,101 @@ +#include #include #include #include "matrix.h" -// TODO Matrix-Funktionen implementieren - -Matrix createMatrix(unsigned int rows, unsigned int cols) +int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx) { - + return rowIdx * cols + colIdx; } -void clearMatrix(Matrix *matrix) +Matrix createMatrix(const unsigned int rows, const unsigned int cols) { - + Matrix matrix = {.rows = rows, .cols = cols}; + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); + return matrix; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value; +} + +void clearMatrix(Matrix *matrix) +{ + for (unsigned int i = 0; i < matrix->rows; i++) + { + for (unsigned int j = 0; j < matrix->cols; j++) + { + setMatrixAt(UNDEFINED_MATRIX_VALUE, *matrix, i, j); + } + } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + return matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n", + matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols); + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; + } + + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + // check, ob genügend Speicher vorhanden + if (result.buffer == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) + { + for (unsigned int j = 0; j < matrix1.cols; j++) + { + unsigned int index = getMatrixIndex(matrix1.cols, i, j); + result.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index]; + } + } + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.cols != matrix2.rows) + { + printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n", + matrix1.cols, matrix2.rows); + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; + } + + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + + // check, ob genügend Speicher vorhanden + if (result.buffer == NULL) { + return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL }; + } + + 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++) + { + MatrixType val1 = matrix1.buffer[i * matrix1.cols + k]; + MatrixType val2 = matrix2.buffer[k * matrix2.cols + j]; + sum += val1 * val2; + } + + result.buffer[i * result.cols + j] = sum; + } + } + + return result; } \ No newline at end of file diff --git a/Startcode/matrix.h b/Startcode/matrix.h index cc640d1..104c35d 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -6,7 +6,13 @@ typedef float MatrixType; // TODO Matrixtyp definieren +// Flat Storage: Nur ein Pointer, sorgt für zusammenhängendes array, kein verstreuter Speicher -> bessere Performance +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType* buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);