#include #include #include "matrix.h" // TODO Matrix-Funktionen implementieren 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; }