error fix matrix

This commit is contained in:
maxgrf 2025-11-16 21:29:56 +01:00
parent 6c514d28ca
commit d36d185214

View File

@ -6,19 +6,17 @@
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m = {NULL, 0, 0};
Matrix m = {NULL, rows, cols}; // Wahrscheinlich in Programm bereits enthalten;
if (rows == 0 || cols == 0)
return m;
if (rows > 0 && cols > 0)
{
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
if (m.buffer == NULL)
{
m.rows = 0;
m.cols = 0;
}
}
return m;
m.rows = rows;
m.cols = cols;
return m;
}
@ -52,13 +50,11 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
// check, equal rows
if (matrix1.rows == matrix2.rows)
{
// "Elementweise Addition": test, if two matrix has exact size
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
{
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
for (int r = 0; r < matrix1.rows; r++)
{
for (int c = 0; c < matrix1.cols; c++)
@ -68,10 +64,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
setMatrixAt(sum, result_add, r, c);
}
}
return result_add;
}
// "Broadcasting": matrix1 has 1 collum
else if (matrix1.cols == 1)
if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
Matrix result_add = createMatrix(matrix1.rows, matrix2.cols);
for (int r = 0; r < matrix1.rows; r++)
{
for (int c = 0; c < matrix2.cols; c++)
@ -80,10 +78,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
setMatrixAt(sum, result_add, r, c);
}
}
return result_add;
}
// "Broadcasting": matrix2 has 1 collum
else if (matrix2.cols == 1)
if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
{
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
for (int r = 0; r < matrix1.rows; r++)
{
for (int c = 0; c < matrix1.cols; c++)
@ -92,13 +92,11 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
setMatrixAt(sum, result_add, r, c);
}
}
}
}
else if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1)
return createMatrix(0, 0);
return result_add;
}
//if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1)
return createMatrix(0, 0);
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
@ -111,15 +109,14 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix result_mul = createMatrix(matrix1.rows, matrix2.cols); // ""
for (int index = 0; index < matrix1.rows; index++)
for (unsigned int index = 0; index < matrix1.rows; index++)
{
for (int shift = 0; shift < matrix2.cols; shift++)
for (unsigned int shift = 0; shift < matrix2.cols; shift++)
{
buffer_add = 0;
for (int skalar = 0; skalar < matrix1.cols; skalar++)
// TODO: matrix_add initialisieren
for (unsigned int skalar = 0; skalar < matrix1.cols; skalar++)
{
// buffer_add += matrix1[index][skalar]*matrix2[skalar][index];
// buffer_add += matrix1[index][skalar]*matrix2[skalar][shift];
buffer_add += getMatrixAt(matrix1, index, skalar) * getMatrixAt(matrix2, skalar, shift);
}
// matrix_mul[index][shift] = buffer_add;