forked from freudenreichan/info2Praktikum-NeuronalesNetz
matrix logic
This commit is contained in:
1 parent
447e77a7bf
commit
3ee900784e
2 files changed
+28
-3
No files matched your search
+23
-3
@@ -6,27 +6,47 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int));
|
||||||
|
const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage};
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
__builtin_free(matrix->storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
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[rowIdx][colIdx] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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[rowIdx][colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||||
|
{
|
||||||
|
return matrix1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
|
|||||||
@@ -6,7 +6,12 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
typedef struct {
|
||||||
|
unsigned int rows;
|
||||||
|
unsigned int cols;
|
||||||
|
|
||||||
|
MatrixType** storage;
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
|
|||||||
Reference in new issue
Block a user