forked from freudenreichan/info2Praktikum-NeuronalesNetz
matrix.c fix
This commit is contained in:
parent
7d9b4bc6bf
commit
8d4ee4cc4e
84
matrix.c
84
matrix.c
@ -12,7 +12,13 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
m.cols = cols;
|
m.cols = cols;
|
||||||
|
|
||||||
m.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
|
m.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
|
||||||
|
// Prüfe auf ungültige Dimensionen
|
||||||
|
if (rows == 0 || cols == 0) {
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
if (m.buffer == NULL){
|
if (m.buffer == NULL){
|
||||||
fprintf(stderr, "Error: Memory allocation failed in createMatrix!.\n");
|
fprintf(stderr, "Error: Memory allocation failed in createMatrix!.\n");
|
||||||
m.rows = 0;
|
m.rows = 0;
|
||||||
@ -28,14 +34,21 @@ return m;
|
|||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
if (matrix == NULL || matrix->buffer == NULL) {
|
|
||||||
|
if (matrix == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alle Elemente auf 0 setzen
|
// Speicher freigeben falls vorhanden
|
||||||
for (unsigned int i = 0; i < matrix->rows * matrix->cols; i++) {
|
if (matrix->buffer != NULL) {
|
||||||
matrix->buffer[i] = 0.0f;
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dimensionen 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)
|
||||||
@ -60,13 +73,11 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) {
|
Matrix result;
|
||||||
fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n");
|
|
||||||
Matrix empty = {0, 0, NULL};
|
|
||||||
return empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
// Fall 1: Normale elementweise Addition (gleiche Dimensionen)
|
||||||
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
if (result.buffer == NULL) {
|
if (result.buffer == NULL) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -80,10 +91,59 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 2: Broadcasting - matrix2 ist Spaltenvektor (cols=1)
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
||||||
|
// matrix2 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
||||||
|
setMatrixAt(
|
||||||
|
getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0),
|
||||||
|
result,
|
||||||
|
i, j
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 3: Broadcasting - matrix1 ist Spaltenvektor (cols=1)
|
||||||
|
else if (matrix2.rows == matrix1.rows && matrix1.cols == 1) {
|
||||||
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||||
|
// matrix1 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
||||||
|
setMatrixAt(
|
||||||
|
getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j),
|
||||||
|
result,
|
||||||
|
i, j
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall 4: Ungültige Dimensionen
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n");
|
||||||
|
Matrix empty = {0, 0, NULL};
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.cols != matrix2.rows) {
|
if (matrix1.cols != matrix2.rows) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user