add supports broadcasting

This commit is contained in:
Bannach 2025-11-11 23:06:39 +01:00
parent bcf9926076
commit cf19ece7a0

View File

@ -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,13 +114,14 @@ 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;
@ -122,4 +151,5 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return matrix;
}
//Siehe I1-Pr Aufgabe
}