Compare commits

..

No commits in common. "75dc5fc631a9787a9e25ed1ddd1023a709b81526" and "e25875fac6b61cc77956e8588acfa89d2b6cf02a" have entirely different histories.

View File

@ -6,17 +6,19 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m = {NULL, 0, 0};
if (rows == 0 || cols == 0) Matrix m = {NULL, rows, cols}; // Wahrscheinlich in Programm bereits enthalten;
return m;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); if (rows > 0 && cols > 0)
if (m.buffer == NULL) {
return m; m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
if (m.buffer == NULL)
{
m.rows = 0;
m.cols = 0;
}
}
m.rows = rows;
m.cols = cols;
return m; return m;
} }
@ -50,52 +52,25 @@ 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)
{ {
// check, equal rows // test, if two matrix has exact size
// "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++)
{
// first version: matrix_add[r][c] = matrix1[r][c] + matrix2[r][c]
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c);
setMatrixAt(sum, result_add, r, c);
}
}
return result_add;
}
// "Broadcasting": matrix1 has 1 collum
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++)
{
MatrixType sum = getMatrixAt(matrix2, r, c) + getMatrixAt(matrix1, r, 0);
setMatrixAt(sum, result_add, r, c);
}
}
return result_add;
}
// "Broadcasting": matrix2 has 1 collum
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++)
{
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0);
setMatrixAt(sum, result_add, r, c);
}
}
return result_add;
}
return createMatrix(0, 0); if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
return createMatrix(0, 0);
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
for (int r = 0; r < matrix1.rows; r++)
{
for (int c = 0; c < matrix1.cols; c++)
{
// TODO: matrix_add initialisieren
// matrix_add[r][c] = matrix1[r][c] + matrix2[r][c]
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c);
setMatrixAt(sum, result_add, r, c); // evtl re
}
}
return result_add;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
@ -108,14 +83,15 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix result_mul = createMatrix(matrix1.rows, matrix2.cols); // "" Matrix result_mul = createMatrix(matrix1.rows, matrix2.cols); // ""
for (unsigned int index = 0; index < matrix1.rows; index++) for (int index = 0; index < matrix1.rows; index++)
{ {
for (unsigned int shift = 0; shift < matrix2.cols; shift++) for (int shift = 0; shift < matrix2.cols; shift++)
{ {
buffer_add = 0; buffer_add = 0;
for (unsigned int skalar = 0; skalar < matrix1.cols; skalar++) for (int skalar = 0; skalar < matrix1.cols; skalar++)
// TODO: matrix_add initialisieren
{ {
// buffer_add += matrix1[index][skalar]*matrix2[skalar][shift]; // buffer_add += matrix1[index][skalar]*matrix2[skalar][index];
buffer_add += getMatrixAt(matrix1, index, skalar) * getMatrixAt(matrix2, skalar, shift); buffer_add += getMatrixAt(matrix1, index, skalar) * getMatrixAt(matrix2, skalar, shift);
} }
// matrix_mul[index][shift] = buffer_add; // matrix_mul[index][shift] = buffer_add;