Multiply Funktion geschrieben, add ueberarbeitet und rows und col Funktion geschrieben

This commit is contained in:
silvana884 2025-11-16 16:46:21 +01:00
parent 5de0e89bea
commit 062f6c541d
2 changed files with 59 additions and 15 deletions

View File

@ -15,6 +15,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
int rows = rows(*matrix);
int cols = cols(*matrix);
for(int i = 0; i < rows; i++) for(int i = 0; i < rows; i++)
{ {
for(int j = 0; j < cols; j++) for(int j = 0; j < cols; j++)
@ -36,10 +38,10 @@ 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)
{ {
rows1 = matrix1->rows; rows1 = rows(matrix1);
rows2 = matrix2->rows; rows2 = rows(matrix2);
cols1 = matrix1->cols; cols1 = cols(matrix1);
cols2 = matrix2->cols; cols2 = cols(matrix2);
if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting
{ {
if(rows1 == 1) //Wenn die erste Matrix der Vektor ist if(rows1 == 1) //Wenn die erste Matrix der Vektor ist
@ -56,14 +58,16 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix addition = createMatrix(rows1, cols2); //Matrix erstellt, in die die addierten Werte geschrieben werden Matrix addition = createMatrix(rows1, cols2); //Matrix erstellt, in die die addierten Werte geschrieben werden
MatrixType wert1; MatrixType wert1;
MatrixType wert2; MatrixType wert2;
int anzahl = rows1 * cols1; for(int i = 0; i < rows1; i++){ //soll fuer jedes Element durchlaufen, sodass alle Werte von beiden Matrizen aufaddiert werden.
for(int i = 0; i < anzahl; i++){ //soll fuer jedes Element durchlaufen, sodass alle Werte von beiden Matrizen aufaddiert werden for(int j = 0; j < cols1; j++){
wert1 = getMatrixAt(matrix1, rows1, cols1); wert1 = getMatrixAt(matrix1, i, j);
wert2 = getMatrixAt(matrix2, rows2, cols2); wert2 = getMatrixAt(matrix2, i, j);
MatrixType addierterWert = wert1 + wert2; MatrixType addierterWert = wert1 + wert2;
setMatrixAt(addierterWert, addition, rows1, cols1); setMatrixAt(addierterWert, addition, i, j);
} }
} }
return addition;
}
else{ else{
return 0; return 0;
} }
@ -71,19 +75,56 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix broadcasting(const Matrix vektor, const Matrix matrix) Matrix broadcasting(const Matrix vektor, const Matrix matrix)
{ {
int rows = matrix->rows; int rows = rows(matrix);
int cols = matrix->cols; int cols = cols(matrix);
Matrix result = createMatrix(rows, cols); Matrix result = createMatrix(rows, cols);
MatrixType wert; MatrixType wert;
MatrixType koordinate; MatrixType koordinate;
for(int i = 0; i < (rows*cols); i++) for(int i = 0; i < rows; i++)
{ {
wert = getMatrixAt(matrix, rows, cols); for(int j = 0; j < cols; j++){
koordinate = wert = getMatrixAt(matrix, i, j);
koordinate = getMatrixAt(vektor, j, 0);
setMatrixAt((koordinate + wert), result, i, j);
} }
} }
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
int rows1 = rows(matrix1);
int cols1 = cols(matrix1);
int rows2 = rows(matrix2);
int cols2 = cols(matrix2);
if(rows1 == cols2) //Bedingung fuer Multiplikation: Zeilen der ersten Matrix gleich Spalten 2. Matrix
{
Matrix result = createMatrix(rows1, cols2);
MatrixType wert1;
MatrixType wert2;
MatrixType addition;
for(int i = 0; i < rows1; i++)
{
for(int j = 0; j < cols2; j++)
{
wert1 = getMatrixAt(matrix1, i, j);
wert2 = getMatrixAt(matrix2, j, i);
addition += wert1 + wert2;
setMatrixAt(addition, result, i,j);
}
addition = 0;
}
return result;
}
return 0;
}
int rows(const Matrix matrix)
{
return matrix->rows;
}
int cols(const Matrix matrix)
{
return matrix->cols;
} }

View File

@ -18,7 +18,10 @@ void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix broadcasintg(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);
int rows(const Matrix matrix);
int cols(const Matrix matrix);
#endif #endif