diff --git a/matrix.c b/matrix.c index 5b6e070..1769f1f 100644 --- a/matrix.c +++ b/matrix.c @@ -68,16 +68,44 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co } -Matrix add(const Matrix matrix1, const Matrix matrix2) -{ +Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix matrix; - if(matrix1.cols != matrix2.cols || matrix1.rows != matrix2.rows) + if(matrix1.rows != matrix2.rows ) { matrix.buffer=NULL; matrix.cols = 0; matrix.rows = 0; return matrix; } + + if (matrix1.cols < matrix2.cols) { + matrix.cols = matrix2.cols; + matrix.rows = matrix2.rows; + matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); + for (int i = 0; i < matrix.rows; i++) { + for (int j = 0; j < matrix.cols; j++){ + matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols]; + + } + + } + + return matrix; + } + if (matrix1.cols > matrix2.cols) + { + matrix.cols = matrix1.cols; + matrix.rows = matrix1.rows; + matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); + for (int i = 0; i < matrix.rows; i++) { + for (int j = 0; j < matrix.cols; j++){ + matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols]; + } + + } + return matrix; + } + else { @@ -86,40 +114,42 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); for (int i = 0; i < matrix1.rows; i++) { for (int j = 0; j < matrix1.cols; j++) { - (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix.cols+j]); + (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix2.cols+j]); } } return matrix; } } -Matrix multiply(const Matrix matrix1, const Matrix matrix2) -{ - Matrix matrix; - if(matrix1.cols == matrix2.rows) - { - matrix.cols = matrix2.cols; - matrix.rows = matrix1.rows; - matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); - for(int i = 0; i < matrix1.rows; i++) - { - for(int j = 0; j < matrix2.cols; j++) - { - for(int k = 0; k < matrix1.cols; k++) - { - matrix.buffer[i*matrix.cols+j] = matrix1.buffer[i*matrix.cols+j] * matrix2.buffer[k*matrix.cols+k]; - } - } - } - return matrix; - } - else + Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - matrix.buffer=NULL; - matrix.cols = 0; - matrix.rows = 0; - return matrix; + Matrix matrix; + if(matrix1.cols == matrix2.rows) + { + + matrix.cols = matrix2.cols; + matrix.rows = matrix1.rows; + matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + for(int i = 0; i < matrix1.rows; i++) + { + for(int j = 0; j < matrix2.cols; j++) + { + for(int k = 0; k < matrix1.cols; k++) + { + matrix.buffer[i*matrix.cols+j] = matrix1.buffer[i*matrix.cols+j] * matrix2.buffer[k*matrix.cols+k]; + } + } + } + return matrix; + } + else + { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } + //Siehe I1-Pr Aufgabe + } - //Siehe I1-Pr Aufgabe -} \ No newline at end of file