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;
|
Matrix m;
|
||||||
|
|
||||||
|
m.buffer = NULL;
|
||||||
m.rows = rows;
|
m.rows = rows;
|
||||||
m.cols = cols;
|
m.cols = cols;
|
||||||
|
|
||||||
m.data = malloc (rows * cols * sizeof(int));
|
if (rows > 0 || cols > 0)
|
||||||
|
{
|
||||||
|
m.buffer = malloc (rows * cols * sizeof(int));
|
||||||
|
}
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,8 +28,8 @@ void clearMatrix(Matrix *matrix)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Speicher freigeben, falls vorhanden
|
// Speicher freigeben, falls vorhanden
|
||||||
free(matrix->data);
|
free(matrix->buffer);
|
||||||
matrix->data = NULL;
|
matrix->buffer = NULL;
|
||||||
|
|
||||||
// Metadaten zurücksetzen
|
// Metadaten zurücksetzen
|
||||||
matrix->rows = 0;
|
matrix->rows = 0;
|
||||||
@ -34,14 +38,14 @@ void clearMatrix(Matrix *matrix)
|
|||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
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 getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
MatrixType value = 0;
|
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)
|
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.rows = matrix1.rows;
|
||||||
result.cols = matrix1.cols;
|
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
|
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||||
if(result.data == NULL)
|
if(result.buffer == NULL)
|
||||||
{
|
{
|
||||||
result.rows = result.cols = 0;
|
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++)
|
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.rows = matrix1.rows;
|
||||||
result.cols = matrix1.cols;
|
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
|
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
|
||||||
if(result.data == NULL)
|
if(result.buffer == NULL)
|
||||||
{
|
{
|
||||||
result.rows = result.cols = 0;
|
result.rows = result.cols = 0;
|
||||||
|
|
||||||
@ -106,9 +110,9 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
MatrixType sum = 0;
|
MatrixType sum = 0;
|
||||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
matrix.h
3
matrix.h
@ -11,7 +11,8 @@ typedef struct
|
|||||||
{
|
{
|
||||||
unsigned int rows; // Anzahl der Zeilen
|
unsigned int rows; // Anzahl der Zeilen
|
||||||
unsigned int cols; // Anzahl der Spalten
|
unsigned int cols; // Anzahl der Spalten
|
||||||
MatrixType *data; // Zeiger auf die Matrixdaten
|
//void *buffer;
|
||||||
|
MatrixType *buffer; // Zeiger auf die Matrixdaten
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user