Compilermeldungen versucht, zu beheben
This commit is contained in:
parent
a767bc6cd8
commit
ad9a44673c
55
matrix.c
55
matrix.c
@ -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));
|
||||||
|
|
||||||
|
for (int i = 0; i < (rows * cols); i++)
|
||||||
|
matrix.buffer[i] = 0;
|
||||||
|
|
||||||
return matrix;
|
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);
|
||||||
@ -118,6 +124,7 @@ 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);
|
||||||
@ -125,10 +132,10 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
4
matrix.h
4
matrix.h
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user