fixed problems in matrix.c
This commit is contained in:
parent
174e356286
commit
f1f3a90e82
107
matrix.c
107
matrix.c
@ -132,7 +132,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
*/
|
*/
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if(matrix1.cols == matrix2.rows)
|
if(matrix1.cols == matrix2.rows && matrix1.buffer != NULL && matrix2.buffer != NULL)
|
||||||
{
|
{
|
||||||
Matrix mult = createMatrix(matrix1.rows, matrix2.cols);
|
Matrix mult = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
for(int i = 0; i < matrix1.rows; i++)
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
@ -140,11 +140,12 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
for(int j = 0; j < matrix2.cols; j++)
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
{
|
{
|
||||||
MatrixType wert = 0;
|
MatrixType wert = 0;
|
||||||
|
MatrixType res = 0;
|
||||||
for(int k = 0; k < matrix2.rows; k++)
|
for(int k = 0; k < matrix2.rows; k++)
|
||||||
{
|
{
|
||||||
wert = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
wert = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
MatrixType res = wert;
|
res = res + wert;
|
||||||
wert = res + wert;
|
wert = res;
|
||||||
}
|
}
|
||||||
setMatrixAt(wert, mult, i, j);
|
setMatrixAt(wert, mult, i, j);
|
||||||
}
|
}
|
||||||
@ -160,103 +161,3 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
||||||
{
|
|
||||||
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
|
||||||
{
|
|
||||||
return UNDEFINED_MATRIX_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
Matrix result;
|
|
||||||
|
|
||||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
|
||||||
{
|
|
||||||
result.rows = 0;
|
|
||||||
result.cols = 0;
|
|
||||||
result.buffer = NULL;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matrix1.cols == matrix2.cols)
|
|
||||||
{
|
|
||||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
for (int i = 0; i < matrix1.rows; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < matrix1.cols; j++)
|
|
||||||
{
|
|
||||||
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
|
||||||
setMatrixAt(value, result, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matrix1.cols == 1 && matrix2.cols > 1)
|
|
||||||
{
|
|
||||||
result = createMatrix(matrix1.rows, matrix2.cols);
|
|
||||||
for (int i = 0; i < matrix1.rows; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < matrix2.cols; j++)
|
|
||||||
{
|
|
||||||
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
|
|
||||||
setMatrixAt(value, result, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
else if (matrix2.cols == 1 && matrix1.cols > 1)
|
|
||||||
{
|
|
||||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
for (int i = 0; i < matrix1.rows; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < matrix1.cols; j++)
|
|
||||||
{
|
|
||||||
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
|
|
||||||
setMatrixAt(value, result, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
|
|
||||||
result.rows = 0;
|
|
||||||
result.cols = 0;
|
|
||||||
result.buffer = NULL;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
Matrix result;
|
|
||||||
|
|
||||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
|
||||||
{
|
|
||||||
result.rows = 0;
|
|
||||||
result.cols = 0;
|
|
||||||
result.buffer = NULL;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = createMatrix(matrix1.rows, matrix2.cols);
|
|
||||||
|
|
||||||
for (int i = 0; i < matrix1.rows; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < matrix2.cols; j++)
|
|
||||||
{
|
|
||||||
MatrixType sum = 0;
|
|
||||||
|
|
||||||
for (int k = 0; k < matrix1.cols; k++)
|
|
||||||
{
|
|
||||||
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
|
||||||
}
|
|
||||||
setMatrixAt(sum, result, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}*/
|
|
||||||
Loading…
x
Reference in New Issue
Block a user