Funktionen zur Matrixrechnung erstellt
This commit is contained in:
parent
c612d9cde1
commit
526ed2823f
42
matrix.c
42
matrix.c
@ -6,27 +6,59 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
int size = rows * cols;
|
||||
MatrixType *speicherbereich;
|
||||
speicherbereich = (MatrixType *)calloc(size, sizeof(MatrixType) * size);
|
||||
Matrix matrix = {rows, cols, &speicherbereich};
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
for(int i = 0; i < rows; i++)
|
||||
{
|
||||
for(int j = 0; j < cols; j++)
|
||||
{
|
||||
setMatrixAt(0, matrix, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
matrix -> data[rowIdx * rows + colIdx] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
return (matrix->data[rowIdx * rows + colIdx]);
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
rows1 = matrix1->rows;
|
||||
rows2 = matrix2->rows;
|
||||
cols1 = matrix1->cols;
|
||||
cols2 = matrix2->cols;
|
||||
if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting
|
||||
{
|
||||
//TODO: Broadcasting
|
||||
}
|
||||
else if(rows1 == rows2 && cols1 == cols2) //Addition nur moeglich, wenn beide AMtrizen gleiche ANzahl an Zeilen und Spalten haben
|
||||
{
|
||||
Matrix addition = createMatrix(rows1, cols2);
|
||||
MatrixType wert1;
|
||||
MatrixType wert2;
|
||||
for(int i = 0; i < rows; i++){
|
||||
wert1 = getMatrixAt(matrix1, rows1, cols1);
|
||||
wert2 = getMatrixAt(matrix2, rows2, cols2);
|
||||
MatrixType addierterWert = wert1 + wert2;
|
||||
setMatrixAt(addierterWert, addition, rows1, cols1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
3
matrix.h
3
matrix.h
@ -9,7 +9,8 @@ typedef float MatrixType;
|
||||
typedef struct{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
} Matrixtyp;
|
||||
MatrixType data[]; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder)
|
||||
} Matrix;
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user