12 Commits
Author SHA1 Message Date
= 15d74d972d added shellscript for matrixTests 2025-11-26 18:34:53 +01:00
= e7373fe73a fixed some spacing 2025-11-26 18:28:45 +01:00
= 9bd18d3681 Fixed editing of matrix values by defenition 2025-11-26 18:22:30 +01:00
D2A62006 04d9b35c36 Refractor Matrix add 2025-11-26 18:04:17 +01:00
D2A62006 57bf46bfc5 Fix matrix multiply 2025-11-26 17:43:41 +01:00
D2A62006 2783663ab9 fix minor issues 2025-11-26 11:10:28 +01:00
= a2f2be5592 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/bruennerda98937/info2NeuronalesNetzBruennnerKobNew 2025-11-25 15:01:53 +01:00
= 82c335ca89 Fixed UnitTest Errorrs in matrix.c 2025-11-25 15:01:21 +01:00
D2A62006 f8456a7d5e Update gitignore 2025-11-20 17:04:09 +01:00
= 05a29b50c6 fixed previously missed error 2025-11-20 16:52:31 +01:00
D2A62006 8ef0d7688f Merge branch 'dev_kob' 2025-11-20 16:50:33 +01:00
D2A62006 32cf681534 matrix createMatrix working 2025-11-20 16:47:34 +01:00
4 changed files with 88 additions and 8 deletions
+2
View File
@@ -1,5 +1,7 @@
mnist mnist
runTests runTests
runMatrixTests runMatrixTests
runImageInputTests
runNeuralNetworkTests
*.o *.o
*.exe *.exe
+82 -5
View File
@@ -70,36 +70,52 @@ Matrix createMatrix(size_t rows, size_t cols)
return m; return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
for (int i = 0; i < matrix->rows; i++) { for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) { for (int j = 0; j < matrix->cols;j++) {
matrix->buffer[i-1 + matrix->rows*(j-1)] = UNDEFINED_MATRIX_VALUE; // Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE;
} }
} }
free(matrix->buffer); free(matrix->buffer);
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 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)
{ {
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value; //checks, if the given incies are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
matrix.buffer[colIdx + matrix.cols * rowIdx] = 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; MatrixType returnVal;
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)]; // Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols];
return returnVal; 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) //check if the matrices are able to be added (same size)
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){ if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
//size of the matrices should be the same, if the addition is supposed to happen //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.rows, matrix1.cols); Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows;i++) { for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) { for (int j = 0; j < matrix1.cols; j++) {
@@ -119,18 +135,50 @@ 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){
Matrix m = {NULL, 0, 0};
return m;
}
//size of the matrices should be the same, if the addition is supposed to happen
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
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.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return outputMatrix;
}
/*
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
//check, if the matrices can be multiplied //check, if the matrices can be multiplied
if (matrix1.rows == matrix2.cols) { if (matrix1.rows == matrix2.cols) {
Matrix outputMatrix = createMatrix(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 i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) { for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix2.rows; k++) { for (int k = 0; k < matrix2.rows; k++) {
// how this should work in normal Matrix version: // how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j]; // 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]; outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
} }
} }
} }
@@ -144,3 +192,32 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return m; return m;
} }
} }
*/
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if(matrix1.cols != matrix2.rows){
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
//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 < matrix1.cols; 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];
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
}
}
}
return outputMatrix;
}
+1 -1
View File
@@ -7,9 +7,9 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct Matrix { typedef struct Matrix {
MatrixType *buffer;
size_t rows; //X-Element size_t rows; //X-Element
size_t cols; //Y-Element size_t cols; //Y-Element
MatrixType *buffer;
} Matrix; } Matrix;
+1
View File
@@ -0,0 +1 @@
make clean && make matrixTests && ./runMatrixTests