#include #include #include #include "matrix.h" // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { if (rows != 0 && cols != 0) { Matrix matrix; matrix.rows = rows; matrix.cols = cols; matrix.buffer = (float*) calloc(rows * cols, sizeof(float)); //belegt den speicherplatz mit calloc -> mit 0 return matrix; } else { //Bei einer "falschen" Matrix eine leere zurückgeben, ohne speicher zu belegen printf("Nullgroesse der Matrix!!!\n"); Matrix matrix; matrix.rows = 0; matrix.cols = 0; matrix.buffer = NULL; return matrix; } } void clearMatrix(Matrix *matrix) { free(matrix->buffer); //gibt den heap speicher frei matrix->buffer = NULL; //zeiger auf NULL setzen matrix->rows = 0; matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } 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]; //ACHTUNG! rowIdx und colIDX sind in Array position gedacht! matrix.cols ist normal gedacht! }else{ return 0; } } Matrix add(const Matrix matrix1, const Matrix matrix2) { //Überprüfen, ob die Matrizen die gleichen Dimensionen haben //wenn nicht muss die matrix "rows/cols=0 und buffer = NULL" leer zurückgegeben werden if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { Matrix result; result.rows = 0; result.cols = 0; result.buffer = NULL; return result; } else { //Matrix result ist die neue Matrix für das Ergebnis Matrix result; result.rows = matrix1.rows; result.cols = matrix1.cols; //TODO: Chat sagt hier noch buffer calloc rein //Addition der beiden Matrizen for (int i = 0; i < result.rows * result.cols; i++) { result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; } return result; } } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { //Spalten matrix 1 muss mit Reihen Matrix 2 übereinstimmen if (matrix1.cols != matrix2.rows) { Matrix result; result.rows = 0; result.cols = 0; result.buffer = NULL; return result; } else { Matrix result; result.rows = matrix1.rows; result.cols = matrix2.cols; //mit get matrix den 4 werte aus matrix1/2.buffer rausnehmen und verrechnen //mit set matrix in result.buffer reinladen //1. for: buffer um stelle weiter //2. for: 1. reihe von matrix1 geht von x10-x12 und x13-x15 das wird 2 mal gemacht //3. for: 2. reihe von matrix1 geht von x20-x23; x24-x27; x28-x211 das wird 2 mal gemacht } }