forked from freudenreichan/info2Praktikum-NeuronalesNetz
refactor: rename matrix storage to buffer
This commit is contained in:
1 parent
dba7bb6624
commit
b67beaa2ab
2 files changed
+14
-14
No files matched your search
+13
-13
@@ -11,13 +11,13 @@ int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx)
|
|||||||
Matrix createMatrix(const unsigned int rows, const unsigned int cols)
|
Matrix createMatrix(const unsigned int rows, const unsigned int cols)
|
||||||
{
|
{
|
||||||
Matrix matrix = {.rows = rows, .cols = cols};
|
Matrix matrix = {.rows = rows, .cols = cols};
|
||||||
matrix.storage = malloc(rows * cols * sizeof(MatrixType));
|
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
@@ -33,7 +33,7 @@ void clearMatrix(Matrix *matrix)
|
|||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
return matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)];
|
return matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
@@ -42,14 +42,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n",
|
fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n",
|
||||||
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
|
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
|
||||||
return (Matrix){ .rows = 0, .cols = 0, .storage = NULL };
|
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
// check, ob genügend Speicher vorhanden
|
// check, ob genügend Speicher vorhanden
|
||||||
if (result.storage == NULL) {
|
if (result.buffer == NULL) {
|
||||||
return (Matrix){ .rows = 0, .cols = 0, .storage = NULL };
|
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
@@ -57,7 +57,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
for (unsigned int j = 0; j < matrix1.cols; j++)
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
{
|
{
|
||||||
unsigned int index = getMatrixIndex(matrix1.cols, i, j);
|
unsigned int index = getMatrixIndex(matrix1.cols, i, j);
|
||||||
result.storage[index] = matrix1.storage[index] + matrix2.storage[index];
|
result.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,14 +70,14 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n",
|
printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n",
|
||||||
matrix1.cols, matrix2.rows);
|
matrix1.cols, matrix2.rows);
|
||||||
return (Matrix){ .rows = 0, .cols = 0, .storage = NULL };
|
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
// check, ob genügend Speicher vorhanden
|
// check, ob genügend Speicher vorhanden
|
||||||
if (result.storage == NULL) {
|
if (result.buffer == NULL) {
|
||||||
return (Matrix){ .rows = 0, .cols = 0, .storage = NULL };
|
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
@@ -88,12 +88,12 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||||
{
|
{
|
||||||
MatrixType val1 = matrix1.storage[i * matrix1.cols + k];
|
MatrixType val1 = matrix1.buffer[i * matrix1.cols + k];
|
||||||
MatrixType val2 = matrix2.storage[k * matrix2.cols + j];
|
MatrixType val2 = matrix2.buffer[k * matrix2.cols + j];
|
||||||
sum += val1 * val2;
|
sum += val1 * val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.storage[i * result.cols + j] = sum;
|
result.buffer[i * result.cols + j] = sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ typedef struct {
|
|||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
|
|
||||||
MatrixType* storage;
|
MatrixType* buffer;
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
Reference in new issue
Block a user