Kommentare für besseres Verständnis
This commit is contained in:
parent
46601b3020
commit
ad32be997b
16
matrix.c
16
matrix.c
@ -46,12 +46,12 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addition
|
// Addition (mit Broadcasting-Unterstützung für Bias)
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix result;
|
Matrix result;
|
||||||
|
|
||||||
// Case 1: Exact same dimensions
|
// Fall 1: Exakte Dimensionen (Element-weise Addition)
|
||||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++)
|
for (unsigned int i = 0; i < matrix1.rows * matrix1.cols; i++)
|
||||||
@ -59,7 +59,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2: matrix1 is (rows x 1) column vector, matrix2 is (rows x cols) - broadcast bias
|
// Fall 2: matrix1 ist (zeilen x 1) Spaltenvektor, matrix2 ist (zeilen x spalten)
|
||||||
|
// Broadcasting: matrix1's Spalte wird zu jeder Spalte von matrix2 addiert
|
||||||
if (matrix1.rows == matrix2.rows && matrix1.cols == 1) {
|
if (matrix1.rows == matrix2.rows && matrix1.cols == 1) {
|
||||||
result = createMatrix(matrix2.rows, matrix2.cols);
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
for (unsigned int col = 0; col < matrix2.cols; col++) {
|
for (unsigned int col = 0; col < matrix2.cols; col++) {
|
||||||
@ -72,7 +73,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 3: matrix2 is (rows x 1) column vector, matrix1 is (rows x cols) - broadcast bias
|
// Fall 3: matrix2 ist (zeilen x 1) Spaltenvektor, matrix1 ist (zeilen x spalten)
|
||||||
|
// Broadcasting: matrix2's Spalte wird zu jeder Spalte von matrix1 addiert
|
||||||
if (matrix2.rows == matrix1.rows && matrix2.cols == 1) {
|
if (matrix2.rows == matrix1.rows && matrix2.cols == 1) {
|
||||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
for (unsigned int col = 0; col < matrix1.cols; col++) {
|
for (unsigned int col = 0; col < matrix1.cols; col++) {
|
||||||
@ -85,7 +87,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No valid case - return empty matrix
|
// Ungültige Dimensionen - leere Matrix zurückgeben
|
||||||
result.rows = 0;
|
result.rows = 0;
|
||||||
result.cols = 0;
|
result.cols = 0;
|
||||||
result.buffer = NULL;
|
result.buffer = NULL;
|
||||||
@ -96,6 +98,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix result;
|
Matrix result;
|
||||||
|
|
||||||
|
// Überprüfe ob Multiplikation möglich ist (Spalten matrix1 == Zeilen matrix2)
|
||||||
if (matrix1.cols != matrix2.rows) {
|
if (matrix1.cols != matrix2.rows) {
|
||||||
result.rows = 0;
|
result.rows = 0;
|
||||||
result.cols = 0;
|
result.cols = 0;
|
||||||
@ -105,10 +109,12 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
result = createMatrix(matrix1.rows, matrix2.cols);
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
// Berechne alle Elemente des Ergebnisses
|
||||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
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++)
|
||||||
{
|
{
|
||||||
|
// Skalarprodukt: Reihe i von matrix1 × Spalte j von matrix2
|
||||||
MatrixType sum = 0;
|
MatrixType sum = 0;
|
||||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||||
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user