From e4a750c649e567ae7bf6b06d8f0ceabfce8ea952 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 12 Nov 2026 10:34:14 +0100 Subject: [PATCH] matrix: Typ definiert, Compilerfehler behoben, add/multiply korrigiert --- neuronalesNetz/matrix.c | 38 +++++++++++++++++++------------------- neuronalesNetz/matrix.h | 7 ++++++- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/neuronalesNetz/matrix.c b/neuronalesNetz/matrix.c index ff6ccdf..04bc73a 100644 --- a/neuronalesNetz/matrix.c +++ b/neuronalesNetz/matrix.c @@ -1,16 +1,13 @@ #include -#include -#include "matrix' -#include #include #include #include "matrix.h" - // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix matrix; if (rows == 0 || cols == 0) { //kein Speicher freigegeben, wenn die Matrix die 0. Dimension hat matrix.rows = 0; matrix.cols = 0; @@ -45,22 +42,22 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - if (matrix.buffer == NULL) || rowIdx >= matrix.rows || colIdx >= matrix.cols - return UNDEFINED_MATRIX_VALUE; + if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) + return UNDEFINED_MATRIX_VALUE; return matrix.buffer[rowIdx * matrix.cols + colIdx]; } -static int istSkalar(constMatrix m) { +static int istSkalar (const Matrix m) { return m.rows == 1 && m.cols == 1; } -static int addKompatibel (const Matrix a, const Matrix b) { - int gleicheZeilen = (a.rows == b.rows); - int gleicheSpalten = (a.cols == b.cols); +static int addKompatibel(const Matrix a, const Matrix b) { + int gleicheZeilen = (a.rows == b.rows); + int gleicheSpalten = (a.cols == b.cols); - return (gleicheZeilen && gleicheSpalten); - || istSkalar(a) || istSkalar(b); - ||(gleicheZeilen && (a.cols == b.cols)); - ||(gleicheSpalten && (a.rows == b.rows)); + return (gleicheZeilen && gleicheSpalten) + || istSkalar(a) || istSkalar(b) + || (gleicheZeilen && (a.cols == 1 || b.cols == 1)) + || (gleicheSpalten && (a.rows == 1 || b.rows == 1)); } static MatrixType wertErweitert(const Matrix m, unsigned int i, unsigned int j) { @@ -88,21 +85,24 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return result; } -Matrix multiply(const Matrix matrix1, const Matrix matrix2) + Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - if (matrix1.cols != matrix2.cols) - return createMatrix(0,0); + if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) + return createMatrix(0, 0); + Matrix result = createMatrix(matrix1.rows, matrix2.cols); if (result.buffer == NULL) - return createMatrix(0,0); + return createMatrix(0, 0); + for (unsigned int i = 0; i < matrix1.rows; i++) { for (unsigned int j = 0; j < matrix2.cols; j++) { MatrixType summe = 0; for (unsigned int k = 0; k < matrix1.cols; k++) { summe += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j]; - result.buffer[i * result.cols + j] = summe; } + result.buffer[i * result.cols + j] = summe; } } return result; + } \ No newline at end of file diff --git a/neuronalesNetz/matrix.h b/neuronalesNetz/matrix.h index cc640d1..db3c50c 100644 --- a/neuronalesNetz/matrix.h +++ b/neuronalesNetz/matrix.h @@ -5,7 +5,12 @@ typedef float MatrixType; -// TODO Matrixtyp definieren +typedef struct +{ + unsigned int rows; + unsigned int cols; + MatrixType *buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols);