forked from freudenreichan/info2Praktikum-NeuronalesNetz
refactor: store matrix struc on stack
This commit is contained in:
1 parent
07522b7d3e
commit
dba7bb6624
1 file changed
+12
-15
+12
-15
@@ -1,21 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
return rowIdx * cols + colIdx;
|
||||
}
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
Matrix createMatrix(const unsigned int rows, const unsigned int cols)
|
||||
{
|
||||
Matrix* m = malloc(sizeof(Matrix));
|
||||
m->rows = rows;
|
||||
m->cols = cols;
|
||||
m->storage = malloc(rows * cols * sizeof(MatrixType));
|
||||
return m;
|
||||
Matrix matrix = {.rows = rows, .cols = cols};
|
||||
matrix.storage = malloc(rows * cols * sizeof(MatrixType));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
@@ -24,16 +26,11 @@ void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
for (unsigned int j = 0; j < matrix->cols; j++)
|
||||
{
|
||||
setMatrixElement(matrix, i, j, UNDEFINED_MATRIX_VALUE);
|
||||
setMatrixAt(UNDEFINED_MATRIX_VALUE, *matrix, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
return matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)];
|
||||
@@ -71,7 +68,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if (matrix1.cols != matrix2.rows)
|
||||
{
|
||||
fprintf(stderr, "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);
|
||||
return (Matrix){ .rows = 0, .cols = 0, .storage = NULL };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user