diff --git a/Startcode/matrix.c b/Startcode/matrix.c index e7689c1..5a43288 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -6,6 +6,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { +<<<<<<< HEAD struct Matrix m; m.buffer = calloc(rows*cols, sizeof(MatrixType)); m.rows = rows; @@ -17,29 +18,133 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) m.cols = 0; } return m; +======= + Matrix m; + + if (rows == 0 || cols == 0) //Ueberprüfen, ob ungültige Werte für Matrix vorhanden + { + m.rows = 0; + m.cols = 0; + m.buffer = NULL; + return m; + } + + m.rows = rows; + m.cols = cols; + m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus + + return m; +>>>>>>> b8adf7f0d66d424098983e650064e8c25353316f } void clearMatrix(Matrix *matrix) { - + matrix->buffer = 0; + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols)) + { + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + MatrixType value = 0; + if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols)) + { + value = matrix.buffer[rowIdx * matrix.cols + colIdx]; + } + return value; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + typedef enum {elemt_add, broadcast} Addiermodi; + Addiermodi modus; + + if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows)) + { + modus = broadcast; + } + else if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols)) + { + modus = elemt_add; + } + else + { + Matrix failure = {NULL, 0, 0}; + return failure; + } + + Matrix m; + if(modus == elemt_add) + { + m = createMatrix(matrix1.rows, matrix1.cols); + + for(int i = 0; i< matrix1.rows; i++) + { + for(int j = 0; j < matrix1.cols ; j++) + { + m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix1.cols+j]; + } + } + return m; + } + else if(modus == broadcast) + { + + if(matrix1.cols == 1) + { + m = createMatrix(matrix2.rows, matrix2.cols); + for(int i = 0; i< matrix2.rows; i++) + { + for(int j = 0; j < matrix2.cols ; j++) + { + m.buffer[i*matrix2.cols+j] = matrix2.buffer[i*matrix2.cols+j] + matrix1.buffer[i*matrix1.cols]; + } + } + } + else + { + m = createMatrix(matrix1.rows, matrix1.cols); + for(int i = 0; i< matrix1.rows; i++) + { + for(int j = 0; j < matrix1.cols ; j++) + { + m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix2.cols]; + } + } + } + } + return m; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - -} \ No newline at end of file + if(matrix1.cols != matrix2.rows) + { + Matrix failure = {NULL, 0, 0}; + return failure; + } + + Matrix m = createMatrix(matrix1.rows, matrix2.cols); + + for(int i = 0; i < matrix1.rows; i++) + { + for(int j = 0; j < matrix2.cols ; j++) + { + int value = 0; + for(int k = 0; k < matrix1.cols; k++) + { + value += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k , j); + } + m.buffer[i*matrix2.cols+j] = value; + } + } + return m; +}