forked from freudenreichan/info2Praktikum-NeuronalesNetz
Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/haugnerqu103436/info2Praktikum-NeuronalesNetz
This commit is contained in:
commit
2d4d4b29e4
2 files changed
+82
-10
No files matched your search
+76
-10
@@ -1,35 +1,101 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
return rowIdx * cols + colIdx;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
Matrix createMatrix(const unsigned int rows, const unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix matrix = {.rows = rows, .cols = cols};
|
||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
for (unsigned int i = 0; i < matrix->rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < matrix->cols; j++)
|
||||
{
|
||||
setMatrixAt(UNDEFINED_MATRIX_VALUE, *matrix, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
return matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)];
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n",
|
||||
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
|
||||
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||
}
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
// check, ob genügend Speicher vorhanden
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
unsigned int index = getMatrixIndex(matrix1.cols, i, j);
|
||||
result.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
if (matrix1.cols != matrix2.rows)
|
||||
{
|
||||
printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n",
|
||||
matrix1.cols, matrix2.rows);
|
||||
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||
}
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
// check, ob genügend Speicher vorhanden
|
||||
if (result.buffer == NULL) {
|
||||
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType sum = 0;
|
||||
|
||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
MatrixType val1 = matrix1.buffer[i * matrix1.cols + k];
|
||||
MatrixType val2 = matrix2.buffer[k * matrix2.cols + j];
|
||||
sum += val1 * val2;
|
||||
}
|
||||
|
||||
result.buffer[i * result.cols + j] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -6,7 +6,13 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
// Flat Storage: Nur ein Pointer, sorgt für zusammenhängendes array, kein verstreuter Speicher -> bessere Performance
|
||||
typedef struct {
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
|
||||
MatrixType* buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
|
||||
Reference in new issue
Block a user