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; Matrix matrix;
if(matrix1.cols != matrix2.cols || matrix1.rows != matrix2.rows) if(matrix1.rows != matrix2.rows )
{ {
matrix.buffer=NULL; matrix.buffer=NULL;
matrix.cols = 0; matrix.cols = 0;
matrix.rows = 0; matrix.rows = 0;
return matrix; 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 else
{ {
@ -86,40 +114,42 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
for (int i = 0; i < matrix1.rows; i++) { for (int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix1.cols; j++) { 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; return matrix;
} }
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix matrix;
if(matrix1.cols == matrix2.rows)
{
matrix.cols = matrix2.cols; Matrix multiply(const Matrix matrix1, const Matrix matrix2)
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 matrix;
matrix.cols = 0; if(matrix1.cols == matrix2.rows)
matrix.rows = 0; {
return matrix;
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
}