diff --git a/matrix.c b/matrix.c index 7512d1d..999f713 100644 --- a/matrix.c +++ b/matrix.c @@ -73,14 +73,44 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return result; } - // Matritzenaddition - for (unsigned int i = 0; i < result.rows; i++) + if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) // Broadcasting { - for (unsigned int j = 0; j < result.cols; j++) + + for (unsigned int i = 0; i < matrix1.rows; i++) { - result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; + for (unsigned int j = 0; j < result.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j]; + } + } + + + } + + else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows) + { + for (unsigned int i = 0; i < matrix2.rows; i++) + { + for (unsigned int j = 0; j < result.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i]; + } } } + + else + { + // Elementweise Addition + for (unsigned int i = 0; i < result.rows; i++) + { + for (unsigned int j = 0; j < result.cols; j++) + { + result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; + } + } + } + + return result; } diff --git a/runMatrixTests b/runMatrixTests index 4f7fb50..d05b8a8 100755 Binary files a/runMatrixTests and b/runMatrixTests differ