Matrixfunktionen set, get, multiply und add geschrieben
This commit is contained in:
parent
2897a9f2a4
commit
ecbb95a6c0
75
matrix.c
75
matrix.c
@ -9,7 +9,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
// Speicher allokieren
|
||||
// Speicher erstellen und nullsetzen
|
||||
matrix.data = (double *)calloc(rows * cols, sizeof(double));
|
||||
|
||||
return matrix;
|
||||
@ -25,22 +25,89 @@ void clearMatrix(Matrix *matrix)
|
||||
}
|
||||
}
|
||||
|
||||
// Matrix muss von 2D in 1D umgewandelt werden deswegen (Zeile* Anzahl Spalten + Spalte)
|
||||
// um an entsprechende Speicheradresse zu kommen
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
matrix.data[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
return matrix.data[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
// Neue Matrix mit passender Dimension erstellen
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
}
|
||||
// Alle Elemente einzeln addieren
|
||||
for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++) {
|
||||
result.data[i] = matrix1.data[i] + matrix2.data[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) {
|
||||
// Neue Matrix mit passender Dimension erstellen
|
||||
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
|
||||
|
||||
// Vektor zu jeder Spalte addieren
|
||||
for (unsigned int i = 0; i < result.rows; i++) {
|
||||
for (unsigned int j = 0; j < result.cols; j++) {
|
||||
MatrixType vectorValue = getMatrixAt(matrix1, i, 0);
|
||||
MatrixType matrixValue = getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(vectorValue + matrixValue, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
else if (matrix2.cols == 1 && matrix2.rows == matrix1.rows){
|
||||
// Neue Matrix mit passender Dimension erstellen
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
// Vektor zu jeder Spalte addieren
|
||||
for (unsigned int i = 0; i < result.rows; i++) {
|
||||
for (unsigned int j = 0; j < result.cols; j++) {
|
||||
MatrixType vectorValue = getMatrixAt(matrix1, i, j);
|
||||
MatrixType matrixValue = getMatrixAt(matrix2, i, 0);
|
||||
setMatrixAt(vectorValue + matrixValue, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
else {
|
||||
// Prüfen ob Matrizen multipiziert werden können
|
||||
if (matrix1.cols != matrix2.rows) {
|
||||
// Fehlerfall: Leere Matrix zurückgeben
|
||||
Matrix empty = {0, 0, NULL};
|
||||
return empty;
|
||||
}
|
||||
// Neue Matrix mit passender Dimension erstellen
|
||||
Matrix result = createMatrix(matrix1.cols, matrix2.rows);
|
||||
|
||||
// Matrix-Multiplikation durchführen
|
||||
for (unsigned int i = 0; i < result.rows; i++) {
|
||||
for (unsigned int j = 0; j < result.cols; j++) {
|
||||
MatrixType sum = 0.0;
|
||||
|
||||
// Skalarprodukt der i-ten Zeile mit j-ter Spalte
|
||||
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||
MatrixType a = getMatrixAt(matrix1, i, k);
|
||||
MatrixType b = getMatrixAt(matrix2, k, j);
|
||||
sum += a * b;
|
||||
}
|
||||
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user