forked from freudenreichan/info2Praktikum-NeuronalesNetz
This implements all functions in matrix.c except multiply(). All tests from matrixTests.c are passing for the implemented functions.
This commit is contained in:
parent
077c6def78
commit
8b76d014c1
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
mnist
|
mnist
|
||||||
runTests
|
runTests
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
|
runMatrixTests
|
||||||
68
matrix.c
68
matrix.c
@ -2,34 +2,84 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix mat = {.rows = rows, .cols = cols};
|
||||||
|
// If one dimension is 0, return both dimensions as 0 and don't init the array/buffer.
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
mat.rows = 0;
|
||||||
|
mat.cols = 0;
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate contiguous and 0 initialized memory
|
||||||
|
mat.buffer = calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
// check if calloc failed
|
||||||
|
if (mat.buffer == NULL)
|
||||||
|
{
|
||||||
|
clearMatrix(&mat);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reduce the dimensions to (0, 0) and free the memory
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->rows = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
// do nothing if idx is not in array or matrix buffer is NULL
|
||||||
|
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + 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 UNDEFINED_MATRIX_VALUE if idx is not in array or matrix buffer is NULL
|
||||||
}
|
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
};
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix resMat = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
// clear matrix and return if the dimensions of the input matrices differ from each other
|
||||||
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||||
|
{
|
||||||
|
clearMatrix(&resMat);
|
||||||
|
return resMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t m = 0; m < matrix1.rows; m++)
|
||||||
|
{
|
||||||
|
for (size_t n = 0; n < matrix1.cols; n++)
|
||||||
|
{
|
||||||
|
// this is unnecessarily complicated because at this point we already know that the matrices are compatible
|
||||||
|
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resMat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO implement
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
9
matrix.h
9
matrix.h
@ -5,8 +5,14 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// Matrixtyp
|
||||||
|
typedef struct Matrix
|
||||||
|
{
|
||||||
|
size_t rows;
|
||||||
|
size_t cols;
|
||||||
|
MatrixType *buffer;
|
||||||
|
|
||||||
|
} 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);
|
||||||
@ -15,5 +21,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user