forked from freudenreichan/info2Praktikum-NeuronalesNetz
multiply fix
This commit is contained in:
parent
c560fd5241
commit
14388a5637
55
matrix.c
55
matrix.c
@ -14,16 +14,17 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
|
||||
if (rows > 0 || cols > 0)
|
||||
{
|
||||
m.buffer = malloc (rows * cols * sizeof(int));
|
||||
m.buffer = malloc(rows * cols * sizeof(int));
|
||||
}
|
||||
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
if (matrix == NULL) {
|
||||
|
||||
if (matrix == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@ -38,21 +39,21 @@ void clearMatrix(Matrix *matrix)
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; //setzte Matrix auf den Wert value am Punkt (row col)
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col)
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
MatrixType value = 0;
|
||||
|
||||
return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; //hole Wert value am Punkt (row col)
|
||||
return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col)
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result = {0};
|
||||
|
||||
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
@ -61,21 +62,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
result.cols = matrix1.cols;
|
||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
|
||||
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.buffer == NULL)
|
||||
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if (result.buffer == NULL)
|
||||
{
|
||||
result.rows = result.cols = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Matritzenaddition
|
||||
for (unsigned int i = 0; i < result.rows; i++)
|
||||
// Matritzenaddition
|
||||
for (unsigned int i = 0; i < result.rows; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
{
|
||||
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@ -84,35 +84,38 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result = {0};
|
||||
|
||||
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
|
||||
if (matrix1.cols != matrix2.rows)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix1.cols;
|
||||
result.cols = matrix2.cols;
|
||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
|
||||
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.buffer == NULL)
|
||||
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if (result.buffer == NULL)
|
||||
{
|
||||
result.rows = result.cols = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Matritzenmultiplikation
|
||||
for (unsigned int i = 0; i < result.rows; i++)
|
||||
// Matritzenmultiplikation
|
||||
for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis
|
||||
{
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis
|
||||
{
|
||||
MatrixType sum = 0;
|
||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||
MatrixType sum = 0;
|
||||
|
||||
for (int n = 0; n < matrix1.cols; n++)
|
||||
{
|
||||
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||
sum += matrix1.buffer[r * matrix1.cols + n] *
|
||||
matrix2.buffer[n * matrix2.cols + m];
|
||||
}
|
||||
result.buffer[i * result.cols + j] = sum;
|
||||
|
||||
result.buffer[r * result.cols + m] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user