3 changed files with 66 additions and 7 deletions

No files matched your search

+2
View File
@@ -1,5 +1,7 @@
mnist mnist
runTests runTests
runMatrixTests runMatrixTests
runImageInputTests
runNeuralNetworkTests
*.o *.o
*.exe *.exe
+62 -5
View File
@@ -73,25 +73,82 @@ Matrix createMatrix(size_t cols, size_t rows)
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) {
matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
}
}
free(matrix->buffer);
matrix->rows = 0;
matrix->cols = 0;
matrix->buffer = NULL;
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
//checks, if the given incies are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
//checks, if the given indices are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
MatrixType returnVal;
returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
return returnVal;
}
else return 0;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
//check if the matrices are able to be added (same size)
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
//size of the matrices should be the same, if the addition is supposed to happen
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
Matrix outputMatrix = createMatrix(matrix1.cols, matrix1.rows);
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + matrix2.rows * j];
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
//check, if the matrices can be multiplied
if (matrix1.rows == matrix2.cols) {
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
for(int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix2.rows; k++) {
// how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
outputMatrix.buffer[i + outputMatrix.rows * j] = matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
}
}
}
return outputMatrix;
} else {
//the matrix could not be added, since the matrix sizes are not set correct.
Matrix m;
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
} }
+2 -2
View File
@@ -7,8 +7,8 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct Matrix { typedef struct Matrix {
size_t rows; size_t rows; //X-Element
size_t cols; size_t cols; //Y-Element
MatrixType *buffer; MatrixType *buffer;
} Matrix; } Matrix;