error fix matrix
This commit is contained in:
parent
6c514d28ca
commit
d36d185214
89
matrix.c
89
matrix.c
@ -6,19 +6,17 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
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.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
|
return m;
|
||||||
if (m.buffer == NULL)
|
|
||||||
{
|
|
||||||
m.rows = 0;
|
|
||||||
m.cols = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,53 +50,53 @@ 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)
|
||||||
{
|
{
|
||||||
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
|
// check, equal rows
|
||||||
// check, equal rows
|
// "Elementweise Addition": test, if two matrix has exact size
|
||||||
if (matrix1.rows == matrix2.rows)
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
||||||
{
|
{
|
||||||
// "Elementweise Addition": test, if two matrix has exact size
|
Matrix result_add = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
for (int r = 0; r < matrix1.rows; r++)
|
||||||
{
|
{
|
||||||
for (int r = 0; r < matrix1.rows; r++)
|
for (int c = 0; c < matrix1.cols; c++)
|
||||||
{
|
{
|
||||||
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);
|
||||||
// first version: matrix_add[r][c] = matrix1[r][c] + matrix2[r][c]
|
setMatrixAt(sum, result_add, r, c);
|
||||||
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c);
|
|
||||||
setMatrixAt(sum, result_add, r, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// "Broadcasting": matrix1 has 1 collum
|
return result_add;
|
||||||
else if (matrix1.cols == 1)
|
}
|
||||||
|
// "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 r = 0; r < matrix1.rows; r++)
|
for (int c = 0; c < matrix2.cols; c++)
|
||||||
{
|
{
|
||||||
for (int c = 0; c < matrix2.cols; c++)
|
MatrixType sum = getMatrixAt(matrix2, r, c) + getMatrixAt(matrix1, r, 0);
|
||||||
{
|
setMatrixAt(sum, result_add, r, c);
|
||||||
MatrixType sum = getMatrixAt(matrix2, r, c) + getMatrixAt(matrix1, r, 0);
|
|
||||||
setMatrixAt(sum, result_add, r, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// "Broadcasting": matrix2 has 1 collum
|
return result_add;
|
||||||
else if (matrix2.cols == 1)
|
}
|
||||||
|
// "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 r = 0; r < matrix1.rows; r++)
|
for (int c = 0; c < matrix1.cols; c++)
|
||||||
{
|
{
|
||||||
for (int c = 0; c < matrix1.cols; c++)
|
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0);
|
||||||
{
|
setMatrixAt(sum, result_add, r, c);
|
||||||
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0);
|
|
||||||
setMatrixAt(sum, result_add, r, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result_add;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1)
|
//if (matrix1.rows != matrix2.rows || matrix1.cols != 1 || matrix2.cols != 1)
|
||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
|
|
||||||
return result_add;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
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); // ""
|
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;
|
buffer_add = 0;
|
||||||
for (int skalar = 0; skalar < matrix1.cols; skalar++)
|
for (unsigned int skalar = 0; skalar < matrix1.cols; skalar++)
|
||||||
// TODO: matrix_add initialisieren
|
|
||||||
{
|
{
|
||||||
// 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);
|
buffer_add += getMatrixAt(matrix1, index, skalar) * getMatrixAt(matrix2, skalar, shift);
|
||||||
}
|
}
|
||||||
// matrix_mul[index][shift] = buffer_add;
|
// matrix_mul[index][shift] = buffer_add;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user