From d36d185214220ac107e80699cd8c314717e05df1 Mon Sep 17 00:00:00 2001 From: maxgrf Date: Sun, 16 Nov 2025 21:29:56 +0100 Subject: [PATCH] error fix matrix --- matrix.c | 89 +++++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/matrix.c b/matrix.c index 86f3a30..b46c57a 100644 --- a/matrix.c +++ b/matrix.c @@ -6,19 +6,17 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix m = {NULL, 0, 0}; - Matrix m = {NULL, rows, cols}; // Wahrscheinlich in Programm bereits enthalten; + if (rows == 0 || cols == 0) + return m; - if (rows > 0 && cols > 0) - { - m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); - if (m.buffer == NULL) - { - m.rows = 0; - m.cols = 0; - } - } + m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); + if (m.buffer == NULL) + return m; + m.rows = rows; + m.cols = cols; return m; } @@ -52,53 +50,53 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - Matrix result_add = createMatrix(matrix1.rows, matrix1.cols); - // check, equal rows - if (matrix1.rows == matrix2.rows) + // check, equal rows + // "Elementweise Addition": test, if two matrix has exact size + if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) { - // "Elementweise Addition": test, if two matrix has exact size - if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) + Matrix result_add = createMatrix(matrix1.rows, matrix1.cols); + for (int r = 0; r < matrix1.rows; r++) { - for (int r = 0; r < matrix1.rows; r++) + for (int c = 0; c < matrix1.cols; c++) { - for (int c = 0; c < matrix1.cols; c++) - { - // first version: matrix_add[r][c] = matrix1[r][c] + matrix2[r][c] - MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c); - setMatrixAt(sum, result_add, r, c); - } + // first version: matrix_add[r][c] = matrix1[r][c] + matrix2[r][c] + MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c); + setMatrixAt(sum, result_add, r, c); } } - // "Broadcasting": matrix1 has 1 collum - else if (matrix1.cols == 1) + return result_add; + } + // "Broadcasting": matrix1 has 1 collum + if (matrix1.rows == matrix2.rows && matrix1.cols == 1) + { + Matrix result_add = createMatrix(matrix1.rows, matrix2.cols); + for (int r = 0; r < matrix1.rows; r++) { - for (int r = 0; r < matrix1.rows; r++) + for (int c = 0; c < matrix2.cols; c++) { - for (int c = 0; c < matrix2.cols; c++) - { - MatrixType sum = getMatrixAt(matrix2, r, c) + getMatrixAt(matrix1, r, 0); - setMatrixAt(sum, result_add, r, c); - } + MatrixType sum = getMatrixAt(matrix2, r, c) + getMatrixAt(matrix1, r, 0); + setMatrixAt(sum, result_add, r, c); } } - // "Broadcasting": matrix2 has 1 collum - else if (matrix2.cols == 1) + return result_add; + } + // "Broadcasting": matrix2 has 1 collum + if (matrix1.rows == matrix2.rows && matrix2.cols == 1) + { + Matrix result_add = createMatrix(matrix1.rows, matrix1.cols); + for (int r = 0; r < matrix1.rows; r++) { - for (int r = 0; r < matrix1.rows; r++) + for (int c = 0; c < matrix1.cols; c++) { - for (int c = 0; c < matrix1.cols; c++) - { - MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0); - setMatrixAt(sum, result_add, r, c); - } + MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0); + setMatrixAt(sum, result_add, r, c); } } + return result_add; } - else if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1) + //if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1) return createMatrix(0, 0); - - return result_add; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) @@ -111,15 +109,14 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix result_mul = createMatrix(matrix1.rows, matrix2.cols); // "" - for (int index = 0; index < matrix1.rows; index++) + for (unsigned int index = 0; index < matrix1.rows; index++) { - for (int shift = 0; shift < matrix2.cols; shift++) + for (unsigned int shift = 0; shift < matrix2.cols; shift++) { buffer_add = 0; - for (int skalar = 0; skalar < matrix1.cols; skalar++) - // TODO: matrix_add initialisieren + for (unsigned int skalar = 0; skalar < matrix1.cols; skalar++) { - // buffer_add += matrix1[index][skalar]*matrix2[skalar][index]; + // buffer_add += matrix1[index][skalar]*matrix2[skalar][shift]; buffer_add += getMatrixAt(matrix1, index, skalar) * getMatrixAt(matrix2, skalar, shift); } // matrix_mul[index][shift] = buffer_add;