Compare commits

..
10 Commits
Author SHA1 Message Date
Your Name 5b295c52d1 complete code 2026-05-15 11:11:35 +02:00
Your Name 43edb7de81 Implementation progress on matrix functions 2026-05-08 13:41:54 +02:00
Your Name 4041322924 multiply 2026-05-07 20:44:09 +02:00
Your Name f2d35fb9d6 Implement add() function with broadcasting support 2026-05-07 19:42:05 +02:00
Your Name 7475cf1cc2 update 3 2026-05-07 19:39:02 +02:00
Your Name f5b255d0aa add funtion updated 2026-05-07 19:37:11 +02:00
Your Name f94ec127f9 add function 2026-05-07 19:24:29 +02:00
Your Name e72c8c454e Implement getMatrixAt and setMatrixAt functions 2026-05-06 06:34:44 +02:00
Your Name 4d6daf5b6f set and get 2026-05-06 06:26:14 +02:00
Your Name f3398f9ea0 Add Matrix struct definition and implement createMatrix function 2026-05-06 05:44:01 +02:00
8 changed files with 129 additions and 7 deletions

No files matched your search

+4 -1
View File
@@ -49,7 +49,10 @@ GrayScaleImageSeries *readImages(const char *path)
} }
unsigned short numberOfImages, width, height; unsigned short numberOfImages, width, height;
readFileHeader(file, &numberOfImages, &width, &height); // Hilfsfunktion von oben if (!readFileHeader(file, &numberOfImages, &width, &height)) {
fclose(file);
return NULL;
}
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); // Speicher in Serie-Struktur reservieren GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); // Speicher in Serie-Struktur reservieren
if (series == NULL) { // kann nicht angelegt werden if (series == NULL) { // kann nicht angelegt werden
+89 -5
View File
@@ -6,30 +6,114 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
if (rows ==0 || cols ==0){ // check if dimensions are valid
Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if invalid
return empty;
}
MatrixType *buffer = malloc(rows * cols* sizeof(MatrixType)); //allocate memory
if (buffer == NULL){ //check if memory allocation succeeded
Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if out of memory
return empty;
}
Matrix result ={.rows = rows, .cols = cols, .buffer = buffer}; // create and return matrix
return result;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ { free(matrix-> buffer); // free the buffer memory
matrix -> buffer = NULL; // set all members to NUll
matrix -> rows =0;
matrix -> cols =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)
{ { if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ // check if indices are valid
return; // do nothing if out of bounds
}
unsigned int index = rowIdx * matrix.cols + colIdx; // calculate index using row-major formula
matrix.buffer[index] = value ; // set value at that index
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ { if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ //check if indices are valid
return UNDEFINED_MATRIX_VALUE;
}
unsigned int index = rowIdx * matrix.cols + colIdx; //calculate index using row-major formula
return matrix.buffer [index]; // return the value at that index
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ { if (matrix1.rows != matrix2.rows){ // check row compatibility
Matrix empty = {.rows =0, .cols=0, .buffer = NULL};
return empty;
}
if (matrix1.cols!= matrix2.cols && matrix2.cols !=1 && matrix1.cols != 1){ // check column compatibility (allow broadcasting )
Matrix empty = {.rows =0, .cols=0, .buffer =NULL};
return empty;
}
unsigned int resultRows = matrix1.rows; //determine result dimensions
unsigned int resultCols = (matrix1.cols > matrix2.cols) ? matrix1.cols: matrix2.cols;
//Allocate memory for the result buffer
MatrixType *resultBuffer = malloc(resultRows * resultCols * sizeof(MatrixType));
if (resultBuffer == NULL){
Matrix empty = {.rows =0, .cols=0, .buffer=NULL};
return empty;
}
// add elements element-wise with broadcasting
for (unsigned int row =0; row < resultRows; ++row){
for (unsigned int col =0; col < resultCols; ++col){
//Handle broadcasting: if matrix2 has 1 column, use column 0
unsigned int col1 = (matrix1.cols ==1)? 0 : col;
MatrixType val1 = getMatrixAt (matrix1, row, col1);
unsigned int col2 = (matrix2.cols==1) ? 0 : col;
MatrixType val2 = getMatrixAt(matrix2, row, col2);
unsigned int resultIndex = row * resultCols + col;
resultBuffer[resultIndex] = val1 +val2;
}
}
Matrix result = {.rows = resultRows, .cols = resultCols, .buffer= resultBuffer};
return result; //create and return result
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ { if (matrix1.cols != matrix2.rows){ // check compatibility
Matrix empty = {.rows =0, .cols=0, .buffer =NULL};
return empty;
}
unsigned int resultRows = matrix1.rows; // determine result dimensions
unsigned int resultCols = matrix2.cols;
MatrixType *resultBuffer = malloc( resultRows * resultCols * sizeof(MatrixType)); // allocate memory
if (resultBuffer == NULL){
Matrix empty = {.rows =0, .cols=0, .buffer= NULL};
return empty;
}
for (unsigned int i =0; i < resultRows; ++i){
for (unsigned int j =0; j< resultCols; ++j){
// compute dot product of row i from matrix1 and column j from matrix2
MatrixType sum = 0.0;
for (unsigned int k =0; k< matrix1.cols; ++k){
MatrixType val1 =getMatrixAt(matrix1, i,k);
MatrixType val2 = getMatrixAt(matrix2, k,j);
sum += val1 * val2;
}
unsigned int resultIndex = i * resultCols + j; // store the result
resultBuffer [resultIndex] = sum;
}
}
Matrix result = {.rows = resultRows, .cols = resultCols, .buffer =resultBuffer};
return result;
} }
+5
View File
@@ -6,6 +6,11 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct{
unsigned int rows;
unsigned int cols;
MatrixType *buffer; //pointer to dynamically allocate array
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
+1 -1
View File
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count) static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
{ {
Matrix matrix = {NULL, 0, 0}; Matrix matrix = {0, 0, NULL};
if(count > 0 && images != NULL) if(count > 0 && images != NULL)
{ {
+30
View File
@@ -8,6 +8,36 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
FILE *file = fopen(path, "wb");
if (file != NULL){
const char *fileTag = "__info2_neural_network_file_format__"; // write the file header
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
for (unsigned int i =0; i< nn.numberOfLayers; i++){ //write each layer to the file
Layer layer = nn.layers[i];
int inputDimension = layer.weights.cols; // extract inputDimension from weights
int outputDimension = layer.weights.rows; // extract outputDimension from weights
// For the first layer, write both inputDimension and outputDimension
// For subsequent layers, only write outputDimension (inputDimension = previous outputDimension)
if (i == 0) {
fwrite(&inputDimension, sizeof(int), 1, file);
}
fwrite(&outputDimension, sizeof(int), 1, file);
// write weights matrix data
int weightsElementcount= layer.weights.rows *layer.weights.cols;
fwrite(layer.weights.buffer, sizeof(MatrixType), weightsElementcount, file);
//write biases matrix data
int biasesElementCount = layer.biases.rows *layer.biases.cols;
fwrite(layer.biases.buffer, sizeof (MatrixType), biasesElementCount, file);
}
int endMarker = 0; // write end marker (0) to signal no more layers
fwrite(&endMarker, sizeof(int), 1, file);
fclose(file); // close the file
}
// TODO // TODO
} }
BIN
View File
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.