matrix c und h grundsätzlich gemacht, abbruchbedingungen unvollständig
This commit is contained in:
parent
4bf78b9893
commit
4893304ccd
51
matrix.c
51
matrix.c
@ -4,32 +4,81 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix matrix;
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
if(rows == 0 || cols == 0){
|
||||||
|
matrix.werte = NULL;
|
||||||
|
return matrix;
|
||||||
|
|
||||||
|
}
|
||||||
|
matrix.werte = malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
//speicher freimachen:
|
||||||
|
free(matrix->werte);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
matrix.werte[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.werte[rowIdx * matrix.cols + colIdx];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols){
|
||||||
|
//nicht sicher
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int reihe = 0; reihe < matrix1.rows; reihe++){
|
||||||
|
for(int spalte = 0; spalte < matrix1.cols; spalte++){
|
||||||
|
MatrixType wertM1 = getMatrixAt(matrix1, reihe, spalte);
|
||||||
|
MatrixType wertM2 = getMatrixAt(matrix2, reihe, spalte);
|
||||||
|
setMatrixAt(wertM1 + wertM2, ergebnisMatrix, reihe, spalte);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ergebnisMatrix;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
for(int i = 0; i < matrix1.rows; i++){
|
||||||
|
for(int j = 0; j < matrix2.cols; j++){
|
||||||
|
MatrixType summe = 0;
|
||||||
|
|
||||||
|
for(int l = 0; l < matrix1.cols; l++){
|
||||||
|
summe += getMatrixAt( matrix1, i, l) * getMatrixAt(matrix2, l, j);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
setMatrixAt(summe, ergebnisMatrix, i, j);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return ergebnisMatrix;
|
||||||
|
|
||||||
}
|
}
|
||||||
6
matrix.h
6
matrix.h
@ -6,8 +6,14 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
typedef struct {
|
||||||
|
unsigned int rows;
|
||||||
|
unsigned int cols;
|
||||||
|
float *werte;
|
||||||
|
|
||||||
|
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user