Updated matrix.c

This commit is contained in:
Henrik Batz committed 2026-11-12 10:16:28 +01:00
1 parent 5a82e2c1d6
commit 21c9176403
1 file changed
+63 -22
+63 -22
View File
@@ -1,3 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
@@ -7,20 +8,30 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = 0;
matrix.cols = cols; matrix.cols = 0;
matrix.buffer = NULL;
// Ungültige Dimensionen überprüfen damit calloc wie gewünscht arbeitet
if (rows == 0 || cols == 0)
{
return matrix;
}
matrix.buffer = calloc(rows * cols, sizeof(MatrixType)); matrix.buffer = calloc(rows * cols, sizeof(MatrixType));
if (matrix.buffer == NULL) if (matrix.buffer == NULL)
{ {
matrix.rows = 0; return matrix;
matrix.cols = 0;
} }
matrix.rows = rows;
matrix.cols = cols;
return matrix; return matrix;
} }
// Matrix-Speicher freigeben // Matrix-Speicher freigeben
void clearMatrix(Matrix* matrix) void clearMatrix(Matrix* matrix)
{ {
@@ -34,6 +45,7 @@ void clearMatrix(Matrix* matrix)
} }
} }
// Wert an einer bestimmten Position setzen // Wert an einer bestimmten Position setzen
void setMatrixAt(MatrixType value, Matrix matrix, void setMatrixAt(MatrixType value, Matrix matrix,
unsigned int rowIdx, unsigned int colIdx) unsigned int rowIdx, unsigned int colIdx)
@@ -46,6 +58,7 @@ void setMatrixAt(MatrixType value, Matrix matrix,
} }
} }
// Wert an einer bestimmten Position auslesen // Wert an einer bestimmten Position auslesen
MatrixType getMatrixAt(const Matrix matrix, MatrixType getMatrixAt(const Matrix matrix,
unsigned int rowIdx, unsigned int colIdx) unsigned int rowIdx, unsigned int colIdx)
@@ -60,34 +73,58 @@ MatrixType getMatrixAt(const Matrix matrix,
return UNDEFINED_MATRIX_VALUE; return UNDEFINED_MATRIX_VALUE;
} }
// Zwei Matrizen addieren
// Zwei Matrizen addieren (mit Broadcasting)
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
// Dimensionen und Buffer überprüfen // Buffer überprüfen
if (matrix1.rows != matrix2.rows || if (matrix1.buffer == NULL ||
matrix1.cols != matrix2.cols ||
matrix1.rows == 0 ||
matrix1.cols == 0 ||
matrix1.buffer == NULL ||
matrix2.buffer == NULL) matrix2.buffer == NULL)
{ {
return createMatrix(0, 0); return (Matrix){0};
} }
// Überprüfen, ob Broadcasting möglich ist
if ((matrix1.rows != matrix2.rows &&
matrix1.rows != 1 && matrix2.rows != 1) ||
(matrix1.cols != matrix2.cols &&
matrix1.cols != 1 && matrix2.cols != 1))
{
return (Matrix){0};
}
// Ergebnisdimensionen bestimmen
unsigned int rows = matrix1.rows > matrix2.rows
? matrix1.rows : matrix2.rows;
unsigned int cols = matrix1.cols > matrix2.cols
? matrix1.cols : matrix2.cols;
// Ergebnismatrix erstellen // Ergebnismatrix erstellen
Matrix result = createMatrix(matrix1.rows, matrix1.cols); Matrix result = createMatrix(rows, cols);
if (result.buffer == NULL) if (result.buffer == NULL)
{ {
return result; return result;
} }
// Matrizen elementweise addieren // Elementweise Addition mit Broadcasting
unsigned int size = matrix1.rows * matrix1.cols; for (unsigned int i = 0; i < rows; i++)
for (unsigned int i = 0; i < size; i++)
{ {
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i]; for (unsigned int j = 0; j < cols; j++)
{
unsigned int r1 = matrix1.rows == 1 ? 0 : i;
unsigned int c1 = matrix1.cols == 1 ? 0 : j;
unsigned int r2 = matrix2.rows == 1 ? 0 : i;
unsigned int c2 = matrix2.cols == 1 ? 0 : j;
MatrixType value =
getMatrixAt(matrix1, r1, c1) +
getMatrixAt(matrix2, r2, c2);
setMatrixAt(value, result, i, j);
}
} }
return result; return result;
@@ -97,13 +134,17 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
// Zwei Matrizen multiplizieren // Zwei Matrizen multiplizieren
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
// Dimensionen und Buffer überprüfen
if (matrix1.cols != matrix2.rows || if (matrix1.cols != matrix2.rows ||
matrix1.rows == 0 ||
matrix2.cols == 0 ||
matrix1.buffer == NULL || matrix1.buffer == NULL ||
matrix2.buffer == NULL) matrix2.buffer == NULL)
{ {
return createMatrix(0, 0); return (Matrix){0};
} }
// Ergebnismatrix erstellen
Matrix result = createMatrix(matrix1.rows, matrix2.cols); Matrix result = createMatrix(matrix1.rows, matrix2.cols);
if (result.buffer == NULL) if (result.buffer == NULL)
@@ -111,7 +152,8 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return result; return result;
} }
for (unsigned int i = 0; i < matrix1.rows; i++) // nur < weil bei 0 angefangen zu zählen // Matrizenmultiplikation
for (unsigned int i = 0; i < matrix1.rows; i++)
{ {
for (unsigned int j = 0; j < matrix2.cols; j++) for (unsigned int j = 0; j < matrix2.cols; j++)
{ {
@@ -120,11 +162,10 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
for (unsigned int k = 0; k < matrix1.cols; k++) for (unsigned int k = 0; k < matrix1.cols; k++)
{ {
sum += getMatrixAt(matrix1, i, k) * sum += getMatrixAt(matrix1, i, k) *
getMatrixAt(matrix2, k, j); getMatrixAt(matrix2, k, j);
} }
setMatrixAt(sum, result, i, j); setMatrixAt(sum, result, i, j);
} }
} }