#include #include #include "matrix.h" #include // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix matrix = {NULL,rows,cols}; if(rows != 0 && cols != 0){ matrix.buffer = calloc(rows * cols, sizeof(MatrixType)); if(matrix.buffer == NULL){ matrix.rows = 0; matrix.cols = 0; // alternativ Programm beenden mit exit -> so einfach dimensionslos } }else{ matrix.rows = 0; matrix.cols = 0; } return matrix; } void clearMatrix(Matrix *matrix) { free(matrix->buffer); matrix->buffer = NULL; matrix->rows = 0; matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { if(matrix.rows > rowIdx && matrix.cols > colIdx){ matrix.buffer[colIdx + rowIdx * matrix.cols] = value; } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { if(matrix.rows > rowIdx && matrix.cols > colIdx){ return matrix.buffer[colIdx + rowIdx * matrix.cols]; }else{ return UNDEFINED_MATRIX_VALUE; } } Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix testHelp = {NULL, 0,0}; return testHelp; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix resultMatrix = createMatrix(matrix1.rows,matrix2.cols); if(matrix1.cols == matrix2.rows){ unsigned int commonDimension = matrix1.cols; for(int i = 0; i < resultMatrix.rows; i++){ for(int j = 0; j < resultMatrix.cols; j++){ MatrixType entry = 0; for(int k = 0; k < commonDimension; k++){ entry += (matrix1.buffer[k + i * matrix1.cols] * matrix2.buffer[j + k * matrix2.cols]); } resultMatrix.buffer[j + i * resultMatrix.cols] = entry; } } return resultMatrix; }else{ resultMatrix.buffer = NULL; resultMatrix.rows = 0; resultMatrix.cols = 0; return resultMatrix; } }