Compare commits

..

7 Commits

Author SHA1 Message Date
c928a8433a matrix.h aktualisiert
matrix.h hinzugefügt
2025-11-16 17:16:42 +00:00
57857eb5d9 matrix.c aktualisiert 2025-11-14 08:11:34 +00:00
6fa4d9c084 matrixTests.c aktualisiert 2025-11-14 07:14:30 +00:00
e42dfc938d imageInputTests.c aktualisiert 2025-11-14 07:09:31 +00:00
ed35fdd9a4 matrixTests.c aktualisiert 2025-11-10 09:27:39 +00:00
40984fe981 matrixTests.c aktualisiert 2025-11-10 09:27:23 +00:00
3299cd29e3 matrix.c aktualisiert 2025-11-10 08:14:02 +00:00
2 changed files with 61 additions and 9 deletions

View File

@ -4,32 +4,80 @@
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) MatrixType createMatrix(unsigned int rows, unsigned int cols)
{ {
MatrixType m;
m.rows = rows;
m.cols = cols;
m.values = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (m.values == NULL) {
m.rows = 0;
m.cols = 0;
return m;
}
for (unsigned int i = 0; i < rows * cols; ++i) {
m.values[i] = 0.0; // Standardwert
}
return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->values != NULL) {
free(matrix->values);
}
matrix->values = NULL;
matrix->rows = 0;
matrix->cols = 0;
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (rowIdx < matrix.rows && colIdx < matrix.cols) {
matrix.values[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)
{ {
if (rowIdx < matrix.rows && colIdx < matrix.cols) {
return matrix.values[rowIdx * matrix.cols + colIdx];
}
return 0.0; // Fallback-Wert
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = createMatrix(0, 0);
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) {
return result;
}
result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; ++i) {
for (unsigned int j = 0; j < matrix1.cols; ++j) {
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(sum, result, i, j);
}
}
return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = createMatrix(0, 0);
} if (matrix1.cols != matrix2.rows) {
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix1.rows; ++i) {
for (unsigned int j = 0; j < matrix2.cols; ++j) {
MatrixType sum = 0.0;
for (unsigned int k = 0; k < matrix1.cols; ++k) {
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, result, i, j);
}
}
return result;

View File

@ -6,7 +6,11 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct {
MatrixType *buffer;
MatrixType rows;
MatrixType cols;
}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);