Compare commits

7 Commits
Author SHA1 Message Date
hallerni98888 42e92f278f Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/hallerni98888/info2Praktikum-NeuronalesNetz 2025-11-08 15:28:39 +01:00
hallerni98888 14388a5637 multiply fix 2025-11-08 15:23:56 +01:00
Simon ebff958c4e 2nd fail fixed 2025-11-08 15:05:29 +01:00
Simon 5b5e1182bd first fail fixed 2025-11-08 14:59:27 +01:00
Simon c560fd5241 sync2 2025-11-08 14:46:39 +01:00
hallerni98888 e206895359 sync 2025-11-08 14:26:00 +01:00
hallerni98888 ddf70dbefd matrix.c prototype 2025-11-08 14:12:40 +01:00
2 changed files with 104 additions and 6 deletions
+92 -1
View File
@@ -6,30 +6,121 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m = {0, 0, NULL};
if (rows > 0 && cols > 0)
{
m.rows = rows;
m.cols = cols;
m.buffer = malloc(rows * cols * sizeof(int));
}
return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix == NULL)
{
return;
}
// Speicher freigeben, falls vorhanden
free(matrix->buffer);
matrix->buffer = NULL;
// Metadaten zurücksetzen
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)
{ {
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col)
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
MatrixType value = 0;
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col)
}
return value;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
return result;
}
result.rows = matrix1.rows;
result.cols = matrix1.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if (result.buffer == NULL)
{
result.rows = result.cols = 0;
return result;
}
// Matritzenaddition
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
return result;
}
result.rows = matrix1.rows;
result.cols = matrix2.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if (result.buffer == NULL)
{
result.rows = result.cols = 0;
return result;
}
// Matritzenmultiplikation
for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis
{
for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis
{
MatrixType sum = 0;
for (int n = 0; n < matrix1.cols; n++)
{
sum += matrix1.buffer[r * matrix1.cols + n] *
matrix2.buffer[n * matrix2.cols + m];
}
result.buffer[r * result.cols + m] = sum;
}
}
return result;
} }
+7
View File
@@ -7,6 +7,13 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct
{
unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten
MatrixType *buffer; // Zeiger auf die Matrixdaten
} 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);