3 Commits
Author SHA1 Message Date
lukas 031341d455 Implemented matrix mutliplication 2026-11-05 10:28:27 +01:00
lukas f1556b18ba Implemented broadcasting 2026-11-05 10:03:51 +01:00
lukas bc87e13966 initial draft of matrix.c 2026-10-29 11:13:21 +01:00
3 changed files with 76 additions and 86 deletions

No files matched your search

+3 -76
View File
@@ -6,90 +6,17 @@
#define BUFFER_SIZE 100 #define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__" #define FILE_HEADER_STRING "__info2_image_file_format__"
static int checkHeader(FILE *file) //Headerprüfung
{
char buffer[BUFFER_SIZE];
fread(buffer, //liest Header ein
sizeof(char),
strlen(FILE_HEADER_STRING),
file);
buffer[strlen(FILE_HEADER_STRING)] = '\0';
return strcmp(buffer, FILE_HEADER_STRING) == 0;
}
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
static GrayScaleImage readImage(FILE *file,unsigned int width,unsigned int height)
{
GrayScaleImage image;
image.width = width;
image.height = height;
image.buffer = malloc(width * height * sizeof(GrayScalePixelType));
fread(image.buffer,sizeof(GrayScalePixelType),width * height,file);
return image;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) //Bilder einlesen GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL;
FILE *file = fopen(path, "rb");
if (file == NULL)
{
return NULL;
}
if (!checkHeader(file)) //Prüft Header
{
fclose(file);
return NULL;
}
unsigned short count, width, height; //Anzahl der Bilder, Höhe und Breite
fread(&count,sizeof(unsigned short),1,file);
fread(&width,sizeof(unsigned short),1,file);
fread(&height,sizeof(unsigned short),1,file);
GrayScaleImageSeries *series =malloc(sizeof(GrayScaleImageSeries));
series->count = count;
series->images = malloc(count * sizeof(GrayScaleImage)); //Reservierung der Speicher für alle Bilder
series->labels = malloc(count * sizeof(unsigned char));
for (unsigned short i = 0; i < count; i++)
{
series->images[i] = readImage(file, width, height);
fread(&series->labels[i],sizeof(unsigned char),1,file);
}
fclose(file); /
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) //Gibt den reservierten Speicher wieder frei void clearSeries(GrayScaleImageSeries *series)
{ {
if (series == NULL) //Falls keine Serie existiert -> Beenden
{
return;
}
for (unsigned short i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
}
free(series->images);
free(series->labels);
free(series);
} }
+68 -8
View File
@@ -1,35 +1,95 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
#include <ctype.h>
// TODO Matrix-Funktionen implementieren #include <stdio.h>
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
if(rows && cols){
newmatrix.cols = cols;
newmatrix.rows = rows;
newmatrix.buffer = malloc(rows*cols*sizeof(MatrixType));
}
return newmatrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
matrix->rows = 0;
matrix->cols = 0;
free(matrix->buffer);
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)
{ {
if(rowIdx < matrix.rows && colIdx < matrix.cols) *(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)
{ {
if(rowIdx < matrix.rows && colIdx < matrix.cols) return *(matrix.buffer+rowIdx*matrix.cols+colIdx);
return 0;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
newmatrix.cols = matrix1.cols;
newmatrix.rows = matrix1.rows;
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
//element addition
for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix1.cols; j++){
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols+j);
}
}
}
else if(matrix2.cols == 1 && (matrix2.rows == matrix1.rows)) { //CAREFUL: ONLY VERTICAL VECTORS ARE IMPLEMENTED, TRANSPOSED ARE NOT YET INCLUDED, CAN LEAD TO ISSUES
printf("Got broadcast\n");
newmatrix.cols = matrix1.cols;
newmatrix.rows = matrix1.rows;
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix1.cols; j++){
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
}
}
}
else if(matrix1.cols == 1 && (matrix2.rows == matrix1.rows)){
printf("Got broadcast\n");
newmatrix.cols = matrix2.cols;
newmatrix.rows = matrix2.rows;
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix2.cols; j++){
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols);
}
}
}
return newmatrix;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0};
if(matrix1.cols == matrix2.rows){
newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType));
newmatrix.rows = matrix1.rows;
newmatrix.cols = matrix2.cols;
for(int i = 0; i < newmatrix.rows; i++){
for(int j = 0; j < newmatrix.cols; j++){
//Set matrix entry to 0
*(newmatrix.buffer+i*newmatrix.cols+j) = 0;
for(int k = 0; k < matrix1.cols; k++){
//Increment matrix entry
*(newmatrix.buffer+i*newmatrix.cols+j) += *(matrix1.buffer+i*matrix1.cols+k) * *(matrix2.buffer+k*matrix2.cols+j);
}
}
}
}
return newmatrix;
} }
+5 -2
View File
@@ -5,8 +5,11 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren typedef struct {
u_int32_t rows;
u_int32_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);