Matrix Funktion an Test angepasst
This commit is contained in:
parent
e3349f7f68
commit
8ac27e20c5
26
matrix.c
26
matrix.c
@ -12,12 +12,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
matrix.cols = cols;
|
||||
|
||||
if (rows == 0 || cols == 0) {
|
||||
matrix.data = NULL;
|
||||
matrix.buffer = NULL;
|
||||
return matrix;
|
||||
}
|
||||
|
||||
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||
if (matrix.data == NULL)
|
||||
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||
if (matrix.buffer == NULL)
|
||||
{
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
@ -28,10 +28,10 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
// Matrix Speicher freigeben
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
if (matrix->data != NULL)
|
||||
if (matrix->buffer != NULL)
|
||||
{
|
||||
free(matrix->data);
|
||||
matrix->data = NULL;
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
}
|
||||
matrix->rows = 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return matrix.data[rowIdx * matrix.cols + colIdx];
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
return 0; // Fallback
|
||||
}
|
||||
@ -61,17 +61,17 @@ Matrix add(const Matrix m1, const Matrix m2)
|
||||
{
|
||||
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);
|
||||
if (result.data == NULL) return result;
|
||||
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.data[r * m1.cols + c] =
|
||||
result.buffer[r * m1.cols + 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);
|
||||
if (result.data == NULL) return result;
|
||||
if (result.buffer == NULL) return result;
|
||||
|
||||
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);
|
||||
}
|
||||
result.data[r * m2.cols + c] = sum;
|
||||
result.buffer[r * m2.cols + c] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user