Kommentare hinzugefügt
This commit is contained in:
parent
09ada4e4d0
commit
b7de923066
40
matrix.c
40
matrix.c
@ -5,13 +5,17 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols) // Ergebnismatrix erstellen
|
||||||
{
|
{
|
||||||
|
//Matrix-Struktur initialisieren
|
||||||
|
|
||||||
Matrix m;
|
Matrix m;
|
||||||
m.rows = rows;
|
m.rows = rows;
|
||||||
m.cols = cols;
|
m.cols = cols;
|
||||||
m.buffer = NULL;
|
m.buffer = NULL;
|
||||||
|
|
||||||
|
//Prüfen auf 0-Zeilen oder Spalten -> wenn ja, Rückgabe leerer Matrix
|
||||||
|
|
||||||
if(rows == 0 || cols == 0)
|
if(rows == 0 || cols == 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -21,6 +25,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Speicher für Matrix reservieren -> wenn calloc fehlschlägt, wird wieder leere Matrix zurückgegeben
|
||||||
|
|
||||||
m.buffer = calloc(rows * cols, sizeof(MatrixType));
|
m.buffer = calloc(rows * cols, sizeof(MatrixType));
|
||||||
if (m.buffer == NULL)
|
if (m.buffer == NULL)
|
||||||
{
|
{
|
||||||
@ -34,7 +40,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix) //dynamisch angelegte Matrix wird freigegeben
|
||||||
{
|
{
|
||||||
free(matrix->buffer);
|
free(matrix->buffer);
|
||||||
|
|
||||||
@ -43,14 +49,16 @@ void clearMatrix(Matrix *matrix)
|
|||||||
matrix->buffer = NULL;
|
matrix->buffer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) //dient dazu einen Wert in der Matrix an bestimmte Position zu setzen
|
||||||
{
|
{
|
||||||
|
|
||||||
matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) //liest einen Wert an einer bestimmten Position aus der Matrix aus
|
||||||
{
|
{
|
||||||
|
//Prüfen, ob ein Buffer existiert & ob Zeilen- und Spaltenindex innerhalb der Grenzen ist
|
||||||
|
|
||||||
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
{
|
{
|
||||||
return UNDEFINED_MATRIX_VALUE;
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
@ -59,7 +67,11 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
static int can_add(Matrix matrix1, Matrix matrix2) {
|
|
||||||
|
|
||||||
|
|
||||||
|
static int can_add(Matrix matrix1, Matrix matrix2) //Prüft, ob und wie zwei Matrizen addiert werden können
|
||||||
|
{
|
||||||
|
|
||||||
int can_add;
|
int can_add;
|
||||||
|
|
||||||
@ -99,13 +111,22 @@ static int can_add(Matrix matrix1, Matrix matrix2) {
|
|||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2) //wir addieren nur wenn beide Matrizen gleich groß sind oder eine von beiden eine Zeile oder eine Spalte besitzt
|
Matrix add(const Matrix matrix1, const Matrix matrix2) //wir addieren nur wenn beide Matrizen gleich groß sind oder eine von beiden eine Zeile oder eine Spalte besitzt
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//Anlegen einer Variable für die Fallunterscheidung der Addition
|
||||||
|
|
||||||
int ok = can_add(matrix1,matrix2);
|
int ok = can_add(matrix1,matrix2);
|
||||||
|
|
||||||
|
//Gibt die Größe der Ergebnismatrix an
|
||||||
|
|
||||||
unsigned int erg_rows = (matrix1.rows == matrix2.rows) ? matrix1.rows : (matrix1.rows == 1 ? matrix2.rows : matrix1.rows);
|
unsigned int erg_rows = (matrix1.rows == matrix2.rows) ? matrix1.rows : (matrix1.rows == 1 ? matrix2.rows : matrix1.rows);
|
||||||
unsigned int erg_cols = (matrix1.cols == matrix2.cols) ? matrix1.cols : (matrix1.cols == 1 ? matrix2.cols : matrix1.cols);
|
unsigned int erg_cols = (matrix1.cols == matrix2.cols) ? matrix1.cols : (matrix1.cols == 1 ? matrix2.cols : matrix1.cols);
|
||||||
|
|
||||||
|
//Ergebnis-Matrix anlegen
|
||||||
|
|
||||||
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
switch(ok)
|
switch(ok)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
@ -167,7 +188,9 @@ return matrix_erg;
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
|
||||||
|
|
||||||
|
static int can_multiply (Matrix matrix1, Matrix matrix2) //Test, ob Matrixmultiplikation möglich
|
||||||
{
|
{
|
||||||
int can_multiply = 0;
|
int can_multiply = 0;
|
||||||
|
|
||||||
@ -180,7 +203,12 @@ static int can_multiply (Matrix matrix1, Matrix matrix2)
|
|||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
//Prüft, ob Matrixmultiplikation möglich -> Übergabe an Hilfsvariable
|
||||||
|
|
||||||
int ok = can_multiply(matrix1,matrix2);
|
int ok = can_multiply(matrix1,matrix2);
|
||||||
|
|
||||||
|
//Größe der Matrix festlegen und an Ergebnismatrix übergeben
|
||||||
|
|
||||||
unsigned int erg_rows = matrix1.rows;
|
unsigned int erg_rows = matrix1.rows;
|
||||||
unsigned int erg_cols = matrix2.cols;
|
unsigned int erg_cols = matrix2.cols;
|
||||||
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user