generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
2
Commits
devKob
..
5ce0982e17
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ce0982e17 | ||
|
|
cfac9ae60e |
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
@@ -137,6 +138,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
}
|
||||
*/
|
||||
|
||||
//Currently working version
|
||||
/*
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
//check if the matrices are able to be added (same size)
|
||||
@@ -147,6 +150,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
|
||||
//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);
|
||||
|
||||
if(!outputMatrix.buffer){
|
||||
@@ -163,6 +167,57 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
}
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -211,12 +266,16 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
||||
for(int i = 0; i < matrix1.rows; i++) {
|
||||
for (int j = 0; j < matrix2.cols; j++) {
|
||||
MatrixType sum = 0;
|
||||
|
||||
for (int k = 0; k < matrix1.cols; 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];
|
||||
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
|
||||
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
|
||||
}
|
||||
outputMatrix.buffer[i * outputMatrix.cols + j] = sum;
|
||||
|
||||
}
|
||||
}
|
||||
return outputMatrix;
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
make clean && make matrixTests && ./runMatrixTests
|
||||
Reference in new issue
Block a user