Compilermeldungen versucht, zu beheben

This commit is contained in:
silvana884 2025-11-16 18:46:46 +01:00
parent a767bc6cd8
commit ad9a44673c
2 changed files with 37 additions and 30 deletions

View File

@ -6,42 +6,48 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
int size = rows * cols; Matrix matrix;
MatrixType *speicherbereich; matrix.rows = rows;
speicherbereich = (MatrixType *)calloc(size, sizeof(MatrixType) * size); matrix.cols = cols;
Matrix matrix = {rows, cols, &speicherbereich}; matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
return matrix;
for (int i = 0; i < (rows * cols); i++)
matrix.buffer[i] = 0;
return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
int rows = rows(*matrix); int rowsM = matrix->rows;
int cols = cols(*matrix); int colsM = matrix->cols;
for(int i = 0; i < rows; i++) for(int i = 0; i < rowsM; i++)
{ {
for(int j = 0; j < cols; j++) for(int j = 0; j < colsM; j++)
{ {
setMatrixAt(0, matrix, i, j); matrix->buffer[i * colsM + j] = 0;
} }
} }
} }
void setMatrixAt(Matrix matrix, unsigned int rowIdx, unsigned int colIdx, MatrixType value) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
matrix->data[rowIdx * matrix->cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
return matrix->data[rowIdx * matrix->cols + colIdx]; return matrix.buffer[rowIdx * matrix.cols + colIdx];
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
rows1 = rows(matrix1); int rows1 = rows(matrix1);
rows2 = rows(matrix2); int rows2 = rows(matrix2);
cols1 = cols(matrix1); int cols1 = cols(matrix1);
cols2 = cols(matrix2); int 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
@ -69,20 +75,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return addition; return addition;
} }
else{ else{
return 0; return createMatrix(0,0);
} }
} }
Matrix broadcasting(const Matrix vektor, const Matrix matrix) Matrix broadcasting(const Matrix vektor, const Matrix matrix)
{ {
int rows = rows(matrix); int rowsM = rows(matrix);
int cols = cols(matrix); int colsM = cols(matrix);
Matrix result = createMatrix(rows, cols); Matrix result = createMatrix(rowsM, colsM);
MatrixType wert; MatrixType wert;
MatrixType koordinate; MatrixType koordinate;
for(int i = 0; i < rows; i++) for(int i = 0; i < rowsM; i++)
{ {
for(int j = 0; j < cols; j++){ for(int j = 0; j < colsM; j++){
wert = getMatrixAt(matrix, i, j); wert = getMatrixAt(matrix, i, j);
koordinate = getMatrixAt(vektor, j, 0); koordinate = getMatrixAt(vektor, j, 0);
setMatrixAt((koordinate + wert), result, i, j); setMatrixAt((koordinate + wert), result, i, j);
@ -98,7 +104,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
int rows2 = rows(matrix2); int rows2 = rows(matrix2);
int cols2 = cols(matrix2); int cols2 = cols(matrix2);
if(cols1 == rows2) //Bedingung fuer Multiplikation: Spalten der ersten Matrix gleich Zeilen 2. Matrix if(cols1 == rows2) //Bedingung fuer Multiplikation: Spalten der ersten Matrix gleich Zeilen 2. Matrix
{ {
Matrix result = createMatrix(rows1, cols2); Matrix result = createMatrix(rows1, cols2);
MatrixType wert1; MatrixType wert1;
MatrixType wert2; MatrixType wert2;
@ -117,18 +123,19 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
} }
setMatrixAt(add, result, i, k); setMatrixAt(add, result, i, k);
add = 0; add = 0;
}
} }
return result; return result;
} }
return createMatrix(0,0); return createMatrix(0,0);
} }
int rows(const Matrix matrix) int rows(const Matrix matrix)
{ {
return matrix->rows; return matrix.rows;
} }
int cols(const Matrix matrix) int cols(const Matrix matrix)
{ {
return matrix->cols; return matrix.cols;
} }

View File

@ -9,7 +9,7 @@ typedef float MatrixType;
typedef struct{ typedef struct{
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType data[]; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder) MatrixType *buffer; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder)
} Matrix; } Matrix;
@ -18,7 +18,7 @@ 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 broadcasting(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 rows(const Matrix matrix);
int cols(const Matrix matrix); int cols(const Matrix matrix);