Bastian 2025-11-17 17:37:43 +01:00
commit 807bbbd375
2 changed files with 64 additions and 30 deletions

100
matrix.c
View File

@ -4,69 +4,103 @@
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
// Matrix erzeugen
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
// Allocate memory for the matrix structure
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data == NULL) if (rows == 0 || cols == 0) {
matrix.buffer = NULL;
return matrix;
}
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.buffer == NULL)
{ {
// Handle memory allocation failure
matrix.rows = 0; matrix.rows = 0;
matrix.cols = 0; matrix.cols = 0;
} }
return matrix;
} }
// Matrix Speicher freigeben
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
// Free the allocated memory for the matrix data if (matrix->buffer != NULL)
if (matrix->data != NULL)
{ {
free(matrix->data); free(matrix->buffer);
matrix->data = NULL; matrix->buffer = NULL;
}
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
}
// Wert setzen
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
// Set the value at the specified row and column index
if (rowIdx < matrix.rows && colIdx < matrix.cols) if (rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
matrix.data[rowIdx * matrix.cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
} }
// Wert auslesen
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
// Get the value at the specified row and column index
if (rowIdx < matrix.rows && colIdx < matrix.cols) if (rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
return matrix.data[rowIdx * matrix.cols + colIdx]; return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
return 0; // Fallback
}
// Matrizen addieren
Matrix add(const Matrix m1, const Matrix m2)
{
if (m1.rows != m2.rows || m1.cols != m2.cols)
{
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
}
Matrix result = createMatrix(m1.rows, m1.cols);
if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < m1.rows; r++)
{
for (unsigned int c = 0; c < m1.cols; c++)
{
result.buffer[r * m1.cols + c] =
getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c);
}
}
return result;
}
// Matrizen multiplizieren
Matrix multiply(const Matrix m1, const Matrix m2)
{
if (m1.cols != m2.rows)
{
return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
}
Matrix result = createMatrix(m1.rows, m2.cols);
if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < m1.rows; r++)
{
for (unsigned int c = 0; c < m2.cols; c++)
{
MatrixType sum = 0;
for (unsigned int k = 0; k < m1.cols; k++)
{
sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c);
}
result.buffer[r * m2.cols + c] = sum;
} }
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) return result;
{
// Add two matrices and return the result
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
// Handle dimension mismatch
Matrix emptyMatrix = createMatrix(0, 0);
return emptyMatrix;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
// Multiply two matrices and return the result
if (matrix1.cols != matrix2.rows)
{
// Handle dimension mismatch
Matrix emptyMatrix = createMatrix(0, 0);
return emptyMatrix;
}
} }

View File

@ -10,7 +10,7 @@ typedef struct
{ {
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType *data; MatrixType *buffer;
} Matrix; } Matrix;