From 25ad55cc270a894fe03216b6e3c601a36559b0ee Mon Sep 17 00:00:00 2001 From: poppni99154 Date: Thu, 20 Nov 2025 17:02:26 +0100 Subject: [PATCH] =?UTF-8?q?sowohl=20matrix.c=20als=20auch=20als=20auch=20m?= =?UTF-8?q?atrix.h=20ver=C3=A4ndert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- matrix.h | 6 ++++ 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..c5aaad3 100644 --- a/matrix.c +++ b/matrix.c @@ -6,30 +6,130 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix m= {NULL,0,0}; //erstellen eine leere Matrix + + if(rows ==0 || cols == 0) //wenn Dimensionen nicht passen -> leere Matrix zurückgeben + return m; + + m.buffer = calloc(rows * cols, sizeof(MatrixType)); //array erstellen, in Buffer laden + if(m.buffer == NULL) //buffer nicht geklappt-> leere Matrix + return m; + + m.rows = rows; //Dimensionen zuweisen + m.cols = cols: + return m; //passende Matrix übergeben } void clearMatrix(Matrix *matrix) { - + if(matrix == NULL) //leere Matrix -> abbruch + return; + free(matrix->buffer); //speicher freigeben + matrix->buffer = NULL; //pionter auf null setzen + matrix->rows = 0; //matrix auf null setzen + matrix->cols = 0; + } } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(matrix == NULL) //leere Matrix -> abbruch + return; + if(rowIdx >= matrix.rows|| colIdx >= matrix.cols) + return; + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + if(matrix == NULL) //leere Matrix -> abbruch + return UNDEFINED_MATRIX_VALUE; + if(rowIdx >= matrix.rows|| colIdx >= matrix.cols) + return UNDEFINED_MATRIX_VALUE; + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = {NULL,0,0}; + if(matrix1.buffer == NULL || matrix2.buffer == NULL) + return result; + if(matrix1.rows !=matrix2.rows && matrix1.rows != 1 && matrix2.rows != 1) // broadcasting check (Matrix Erweiterung) + return result; + if(matrix1.cols !=matrix2.cols && matrix1.cols != 1 && matrix2.cols != 1) // broadcasting check (Matrix Erweiterung) + return result; + + + unsigned int rows; + if (matrix1.rows > matrix2.rows) + rows = matrix1.rows; + else + rows = matrix2.rows; + + unsigned int cols; + if (matrix1.cols > matrix2.cols) + rows = matrix1.cols; + else + rows = matrix2.cols; + + result = createMatrix(rows, cols); + if(result.buffer == NULL) + return result; + + for(unsigned int i = 0; i < rows; i++) + { + unsigned int r1 = (matrix1.rows == 1) ? 0 : i; + unsigned int r2 = (matrix2.rows == 1) ? 0 : i; + + for(unsigned int j = 0; j < cols; j++) + { + unsigned int c1 = (matrix1.cols == 1) ? 0 : j; + unsigned int c2 = (matrix2.cols == 1) ? 0 : j; + + MatrixType v1 = matrix1.buffer[r1 * matrix1.cols + c1]; + MatrixType v2 = matrix2.buffer[r2 * matrix1.cols + c2]; + result.buffer[i*cols+j] = v1 + v2 + } +} +return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - +Matrix result = (Matrix){NULL,0,0}; +if(matrix1.buffer == NULL || matrix2.buffer == NULL) + return result; + +if (matrix1.cols != matrix2.rows) + return result; + +unsigned int rows = matrix1.rows; +unsigned int cols = matrix2.cols; +unsigned int inner = matrix1.cols; + +result = createMatrix(rows, cols); +if(result.buffer == NULL) + return result; + +for(unsigned int i = 0; i < rows; i++) + { + for(unsigned int j = 0; j < cols; j++) + { + MatrixType sum = 0; + + for(unsigned int k = 0; k < inner; k++) + { + MatrixType v1 = matrix1.buffer[i * matrix1.cols + k]; + MatrixType v2 = matrix2.buffer[k * matrix2.cols + j]; + sum += v1 * v2; + } + + result.buffer[i * cols + j] = sum; + } + } + return result; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..c285923 100644 --- a/matrix.h +++ b/matrix.h @@ -7,6 +7,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + MatrixType *buffer; + unsigned int rows; + unsigned int cols; +}Matrix; + Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);