generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
No commits in common. "5ce0982e17ca1ff13e1321c57f489971978ae619" and "9bd18d3681be7febd1f9d8028fa8f7339a1cf777" have entirely different histories.
5ce0982e17
...
9bd18d3681
54
matrix.c
54
matrix.c
@ -1,7 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
@ -138,8 +137,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Currently working version
|
|
||||||
/*
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
//check if the matrices are able to be added (same size)
|
//check if the matrices are able to be added (same size)
|
||||||
@ -167,57 +164,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
return outputMatrix;
|
return outputMatrix;
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
|
|
||||||
bool doBroadcast = false;
|
|
||||||
Matrix larger, smaller;
|
|
||||||
|
|
||||||
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
|
||||||
larger = matrix1;
|
|
||||||
smaller = matrix2;
|
|
||||||
}
|
|
||||||
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
|
|
||||||
{
|
|
||||||
larger = matrix1;
|
|
||||||
smaller = matrix2;
|
|
||||||
doBroadcast = true;
|
|
||||||
}
|
|
||||||
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
|
||||||
{
|
|
||||||
larger = matrix2;
|
|
||||||
smaller = matrix1;
|
|
||||||
doBroadcast = true;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Matrix m = {NULL, 0, 0};
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
|
|
||||||
if(doBroadcast){
|
|
||||||
for(int i = 0; i < outputMatrix.rows; i++){
|
|
||||||
MatrixType broadcastValue = smaller.buffer[i];
|
|
||||||
for(int j = 0; j < outputMatrix.cols; j++){
|
|
||||||
outputMatrix.buffer[i * outputMatrix.cols + j] = larger.buffer[i * larger.cols + j] + broadcastValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else{
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user