Broadcasting 1xn Vektor Funktion hinzugefuegt

This commit is contained in:
Giorgi Kesidis 2025-11-11 14:44:32 +01:00
parent ed1325cfba
commit bd909b2c42

View File

@ -9,7 +9,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix matrix; Matrix matrix;
if(rows == 0 || cols == 0) if (rows == 0 || cols == 0)
{ {
matrix.rows = 0; matrix.rows = 0;
matrix.cols = 0; matrix.cols = 0;
@ -17,7 +17,6 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
return matrix; return matrix;
} }
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
@ -78,15 +77,8 @@ 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) if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) // gleiche Dimension
{ {
fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n",
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
Matrix empty = {0, 0, NULL};
return empty;
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols); Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) if (result.buffer == NULL)
@ -103,6 +95,51 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
} }
} }
return result; return result;
}
if (matrix1.rows == matrix2.rows && matrix2.cols == 1) // Matrix 2 hat eine Spalte
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if(result.buffer == NULL)
{
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
return result;
}
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
}
}
return result;
}
if (matrix1.rows == matrix2.rows && matrix1.cols == 1) // Matrix 1 hat eine Spalte
{
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
if(result.buffer == NULL)
{
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
return result;
}
for (int i = 0; i < matrix2.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
result.buffer[i * result.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
}
}
return result;
}
// passt nicht
fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n",
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
Matrix empty = {0, 0, NULL};
return empty;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
@ -134,7 +171,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
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];
} }
result.buffer[i * result.cols +j] = sum; result.buffer[i * result.cols + j] = sum;
} }
} }
return result; return result;