From 6c266527440fe3812d5b36fedab9800af07e4af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Tue, 18 Nov 2025 01:52:43 +0100 Subject: [PATCH] matrix fehler behoben --- main.c | 2 +- matrix.c | 121 ++++++++++++++++++++++++++++-------------------- neuralNetwork.c | 4 +- 3 files changed, 74 insertions(+), 53 deletions(-) diff --git a/main.c b/main.c index 7e4127d..67316a0 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) unsigned char *predictions = NULL; printf("Processing %u images ...\n", series->count); - + predictions = predict(model, series->images, series->count); if(predictions != NULL) diff --git a/matrix.c b/matrix.c index 253d848..8ae5294 100644 --- a/matrix.c +++ b/matrix.c @@ -1,8 +1,8 @@ #include #include #include "matrix.h" +#include -// TODO Matrix-Funktionen implementieren // Matrix erzeugen Matrix createMatrix(unsigned int rows, unsigned int cols) @@ -11,29 +11,33 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.buffer = NULL; matrix.rows = 0; matrix.cols = 0; - - // Wenn die Dimensionen gültig sind, Speicher reservieren - if (rows > 0 && cols > 0) - { - matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); - if (matrix.buffer != NULL) - { - matrix.rows = rows; - matrix.cols = cols; - } - } + if (rows == 0 || cols == 0) + return matrix; // leere Matrix + + matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); + if (!matrix.buffer) + return matrix; // Speicher konnte nicht reserviert werden + + matrix.rows = rows; + matrix.cols = cols; + + // Initialisiere alle Werte auf UNDEFINED_MATRIX_VALUE + for (unsigned int i = 0; i < rows * cols; i++) + matrix.buffer[i] = UNDEFINED_MATRIX_VALUE; + return matrix; } // Matrix Speicher freigeben void clearMatrix(Matrix *matrix) { - if (matrix->buffer != NULL) - { + if (!matrix) return; + + if (matrix->buffer) free(matrix->buffer); - matrix->buffer = NULL; - } + + matrix->buffer = NULL; matrix->rows = 0; matrix->cols = 0; } @@ -41,67 +45,84 @@ void clearMatrix(Matrix *matrix) // Wert setzen void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - if (rowIdx < matrix.rows && colIdx < matrix.cols) - { - matrix.buffer[rowIdx * matrix.cols + colIdx] = value; - } + if (!matrix.buffer) return; + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return; + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } // Wert auslesen MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - if (rowIdx < matrix.rows && colIdx < matrix.cols) - { - return matrix.buffer[rowIdx * matrix.cols + colIdx]; - } - return 0; // Fallback -} + if (!matrix.buffer) return UNDEFINED_MATRIX_VALUE; + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return UNDEFINED_MATRIX_VALUE; + return matrix.buffer[rowIdx * matrix.cols + colIdx]; +} // Matrizen addieren Matrix add(const Matrix m1, const Matrix m2) { - if (m1.rows != m2.rows || m1.cols != m2.cols) + if (!m1.buffer || !m2.buffer) return createMatrix(0,0); + + // gleiche Dimension + if (m1.rows == m2.rows && m1.cols == m2.cols) { - return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen + Matrix result = createMatrix(m1.rows, m1.cols); + if (!result.buffer) return result; + + for (unsigned int r = 0; r < m1.rows; r++) + for (unsigned int c = 0; c < m1.cols; c++) + result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r * m2.cols + c]; + + return result; } - Matrix result = createMatrix(m1.rows, m1.cols); - if (result.buffer == NULL) return result; - - for (unsigned int r = 0; r < m1.rows; r++) + // Matrix2 ist ein Spaltenvektor + if (m1.rows == m2.rows && m2.cols == 1) { - for (unsigned int c = 0; c < m1.cols; c++) - { - result.buffer[r * m1.cols + c] = - getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c); - } + Matrix result = createMatrix(m1.rows, m1.cols); + if (!result.buffer) return result; + + for (unsigned int r = 0; r < m1.rows; r++) + for (unsigned int c = 0; c < m1.cols; c++) + result.buffer[r * result.cols + c] = m1.buffer[r * m1.cols + c] + m2.buffer[r]; + + return result; } - return result; + + // Matrix1 ist ein Spaltenvektor + if (m1.rows == m2.rows && m1.cols == 1) + { + Matrix result = createMatrix(m2.rows, m2.cols); + if (!result.buffer) return result; + + for (unsigned int r = 0; r < m2.rows; r++) + for (unsigned int c = 0; c < m2.cols; c++) + result.buffer[r * result.cols + c] = m1.buffer[r] + m2.buffer[r * m2.cols + c]; + + return result; + } + + // passt nicht + return createMatrix(0,0); } - // Matrizen multiplizieren Matrix multiply(const Matrix m1, const Matrix m2) { - if (m1.cols != m2.rows) - { - return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen - } + if (!m1.buffer || !m2.buffer) return createMatrix(0,0); + if (m1.cols != m2.rows) return createMatrix(0,0); Matrix result = createMatrix(m1.rows, m2.cols); - if (result.buffer == NULL) return result; + if (!result.buffer) return result; for (unsigned int r = 0; r < m1.rows; r++) - { for (unsigned int c = 0; c < m2.cols; c++) { MatrixType sum = 0; for (unsigned int k = 0; k < m1.cols; k++) - { - sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c); - } - result.buffer[r * m2.cols + c] = sum; + sum += m1.buffer[r * m1.cols + k] * m2.buffer[k * m2.cols + c]; + result.buffer[r * result.cols + c] = sum; } - } return result; } \ No newline at end of file diff --git a/neuralNetwork.c b/neuralNetwork.c index bd8f164..fede51d 100644 --- a/neuralNetwork.c +++ b/neuralNetwork.c @@ -197,7 +197,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch) if(result.buffer != NULL) { - for(int i = 0; i < model.numberOfLayers; i++) + for(int i = 0; i < model.numberOfLayers; i++) { Matrix biasResult; Matrix weightResult; @@ -246,7 +246,7 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[], { Matrix inputBatch = imageBatchToMatrixOfImageVectors(images, numberOfImages); Matrix outputBatch = forward(model, inputBatch); - + unsigned char *result = argmax(outputBatch); clearMatrix(&outputBatch);