generated from freudenreichan/info2Praktikum-NeuronalesNetz
Clean up Matrix.c
This commit is contained in:
parent
98aa5f354a
commit
ce371e4228
127
matrix.c
127
matrix.c
@ -5,44 +5,6 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
/*
|
|
||||||
Alte Funktion
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
||||||
{
|
|
||||||
Matrix m;
|
|
||||||
m.rows = rows;
|
|
||||||
m.cols = cols;
|
|
||||||
m.data = NULL;
|
|
||||||
|
|
||||||
if(rows == 0 || cols == 0){
|
|
||||||
m.rows = m.cols = 0;
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
m.data = malloc(rows * sizeof *m.data);
|
|
||||||
|
|
||||||
if(!m.data){
|
|
||||||
m.rows = m.cols = 0;
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
for(unsigned int i = 0; i < rows; i++){
|
|
||||||
m.data[i] = malloc(cols * sizeof *m.data[i]);
|
|
||||||
|
|
||||||
if(!m.data[i]){
|
|
||||||
for(unsigned int j = 0; j < i; j++){
|
|
||||||
free(m.data[j]);
|
|
||||||
}
|
|
||||||
free(m.data);
|
|
||||||
m.data = NULL;
|
|
||||||
m.rows = m.cols = 0;
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Matrix createMatrix(size_t rows, size_t cols)
|
Matrix createMatrix(size_t rows, size_t cols)
|
||||||
{
|
{
|
||||||
Matrix m;
|
Matrix m;
|
||||||
@ -110,66 +72,6 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
//check if the matrices are able to be added (same size)
|
|
||||||
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
|
||||||
//size of the matrices should be the same, if the addition is supposed to happen
|
|
||||||
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
for (int i = 0; i < matrix1.rows;i++) {
|
|
||||||
for (int j = 0; j < matrix1.cols; j++) {
|
|
||||||
// how this should work in normal Matrix version:
|
|
||||||
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
|
||||||
outputMatrix.buffer[i + outputMatrix.rows* j] = matrix1.buffer[i + matrix1.rows* j] + matrix2.buffer[i + matrix2.rows * j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return outputMatrix;
|
|
||||||
} else {
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//Currently working version
|
|
||||||
/*
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
//check if the matrices are able to be added (same size)
|
|
||||||
if (matrix1.cols != matrix2.cols && matrix1.rows != matrix2.rows){
|
|
||||||
Matrix m = {NULL, 0, 0};
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//size of the matrices should be the same, if the addition is supposed to happen
|
|
||||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
|
|
||||||
if(!outputMatrix.buffer){
|
|
||||||
Matrix m = {NULL, 0, 0};
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < matrix1.rows;i++) {
|
|
||||||
for (int j = 0; j < matrix1.cols; j++) {
|
|
||||||
// how this should work in normal Matrix version:
|
|
||||||
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
|
||||||
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return outputMatrix;
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -219,35 +121,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
|
|
||||||
//check, if the matrices can be multiplied
|
|
||||||
if (matrix1.rows == matrix2.cols) {
|
|
||||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
|
||||||
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
|
||||||
for(int i = 0; i < matrix1.rows; i++) {
|
|
||||||
for (int j = 0; j < matrix2.cols; j++) {
|
|
||||||
for (int k = 0; k < matrix2.rows; k++) {
|
|
||||||
// how this should work in normal Matrix version:
|
|
||||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
|
|
||||||
outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return outputMatrix;
|
|
||||||
} else {
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user