generated from freudenreichan/info2Praktikum-NeuronalesNetz
Added matrix funcionality methods
This commit is contained in:
parent
19910ab17f
commit
4da0a796c1
40
matrix.c
40
matrix.c
@ -72,17 +72,27 @@ Matrix createMatrix(size_t rows, size_t cols)
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
for (int i = 0; i < matrix->rows; i++) {
|
||||
for (int j = 0; j < matrix->cols;j++) {
|
||||
matrix->buffer[i-1 + matrix->rows*(j-1)] = UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
}
|
||||
free(matrix->buffer);
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
MatrixType returnVal;
|
||||
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)];
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
@ -93,13 +103,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for (int i = 0; i < matrix1.rows;i++) {
|
||||
for (int j = 0; j < matrix1.cols; j++) {
|
||||
// I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math
|
||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
||||
// how this should work in normal Matrix version:
|
||||
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
||||
outputMatrix.buffer[i-1+ outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(j-1)] + matrix2.buffer[i-1 + matrix2.rows *(j-1)];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//no matrix could be added, consequence should be defined
|
||||
// return NULL;
|
||||
//the matrix could not be added, since the matrix sizes are not set correct.
|
||||
Matrix m;
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
||||
@ -112,14 +127,19 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
for(int i = 0; i < matrix1.rows; i++) {
|
||||
for (int j = 0; j < matrix2.cols; j++) {
|
||||
for (int k = 0; k < matrix2.rows; k++) {
|
||||
// I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math
|
||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2[k][j];
|
||||
// how this should work in normal Matrix version:
|
||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
|
||||
outputMatrix.buffer[i-1 + outputMatrix.rows*(j-1)] = matrix1.buffer[i-1 + matrix1.rows*(k-1)] * matrix2.buffer[k-1 + matrix2.rows*(j-1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
return outputMatrix;
|
||||
} else {
|
||||
//no matrix could be added, consequence should be defined
|
||||
// return NULL;
|
||||
//the matrix could not be added, since the matrix sizes are not set correct.
|
||||
Matrix m;
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user