forked from freudenreichan/info2Praktikum-NeuronalesNetz
sync2
This commit is contained in:
parent
e206895359
commit
c560fd5241
34
matrix.c
34
matrix.c
@ -8,11 +8,15 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
|
||||
m.buffer = NULL;
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
|
||||
m.data = malloc (rows * cols * sizeof(int));
|
||||
|
||||
if (rows > 0 || cols > 0)
|
||||
{
|
||||
m.buffer = malloc (rows * cols * sizeof(int));
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@ -24,8 +28,8 @@ void clearMatrix(Matrix *matrix)
|
||||
}
|
||||
|
||||
// Speicher freigeben, falls vorhanden
|
||||
free(matrix->data);
|
||||
matrix->data = NULL;
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
|
||||
// Metadaten zurücksetzen
|
||||
matrix->rows = 0;
|
||||
@ -34,14 +38,14 @@ void clearMatrix(Matrix *matrix)
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
matrix.data[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.data[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)
|
||||
@ -55,10 +59,10 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix1.cols;
|
||||
result.data = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
|
||||
//wenn Data nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.data == NULL)
|
||||
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.buffer == NULL)
|
||||
{
|
||||
result.rows = result.cols = 0;
|
||||
|
||||
@ -70,7 +74,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
for (unsigned int j = 0; j < result.cols; j++)
|
||||
{
|
||||
result.data[i * result.cols + j] = matrix1.data[i * matrix1.cols + j] + matrix2.data[i * matrix2.cols + j];
|
||||
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
|
||||
|
||||
}
|
||||
}
|
||||
@ -88,10 +92,10 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix1.cols;
|
||||
result.data = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
|
||||
|
||||
//wenn Data nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.data == NULL)
|
||||
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||
if(result.buffer == NULL)
|
||||
{
|
||||
result.rows = result.cols = 0;
|
||||
|
||||
@ -106,9 +110,9 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
MatrixType sum = 0;
|
||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
|
||||
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||
}
|
||||
result.data[i * result.cols + j] = sum;
|
||||
result.buffer[i * result.cols + j] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user