Matrix Funktion an Test angepasst

This commit is contained in:
Björn 2025-11-17 13:46:22 +01:00
parent e3349f7f68
commit 8ac27e20c5
2 changed files with 14 additions and 14 deletions

View File

@ -12,12 +12,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
matrix.cols = cols; matrix.cols = cols;
if (rows == 0 || cols == 0) { if (rows == 0 || cols == 0) {
matrix.data = NULL; matrix.buffer = NULL;
return matrix; return matrix;
} }
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data == NULL) if (matrix.buffer == NULL)
{ {
matrix.rows = 0; matrix.rows = 0;
matrix.cols = 0; matrix.cols = 0;
@ -28,10 +28,10 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
// Matrix Speicher freigeben // Matrix Speicher freigeben
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->data != NULL) if (matrix->buffer != 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;
@ -42,7 +42,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
{ {
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;
} }
} }
@ -51,7 +51,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
{ {
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 return 0; // Fallback
} }
@ -61,17 +61,17 @@ Matrix add(const Matrix m1, const Matrix m2)
{ {
if (m1.rows != m2.rows || m1.cols != m2.cols) if (m1.rows != m2.rows || m1.cols != m2.cols)
{ {
return createMatrix(0, 0); // Dimension passt nicht return createMatrix(0, 0); // Falls Matrix-Dimensionen nicht passen
} }
Matrix result = createMatrix(m1.rows, m1.cols); Matrix result = createMatrix(m1.rows, m1.cols);
if (result.data == NULL) return result; if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < m1.rows; r++) for (unsigned int r = 0; r < m1.rows; r++)
{ {
for (unsigned int c = 0; c < m1.cols; c++) for (unsigned int c = 0; c < m1.cols; c++)
{ {
result.data[r * m1.cols + c] = result.buffer[r * m1.cols + c] =
getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c); getMatrixAt(m1, r, c) + getMatrixAt(m2, r, c);
} }
} }
@ -87,7 +87,7 @@ Matrix multiply(const Matrix m1, const Matrix m2)
} }
Matrix result = createMatrix(m1.rows, m2.cols); Matrix result = createMatrix(m1.rows, m2.cols);
if (result.data == NULL) return result; if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < m1.rows; r++) for (unsigned int r = 0; r < m1.rows; r++)
{ {
@ -98,7 +98,7 @@ Matrix multiply(const Matrix m1, const Matrix m2)
{ {
sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c); sum += getMatrixAt(m1, r, k) * getMatrixAt(m2, k, c);
} }
result.data[r * m2.cols + c] = sum; result.buffer[r * m2.cols + c] = sum;
} }
} }

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;