diff --git a/.idea/editor.xml b/.idea/editor.xml
index dc36d98..193436d 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -341,5 +341,245 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/info2praktikum-neuronalesnetz/matrix.c b/info2praktikum-neuronalesnetz/matrix.c
index 6e38675..cf6fe65 100644
--- a/info2praktikum-neuronalesnetz/matrix.c
+++ b/info2praktikum-neuronalesnetz/matrix.c
@@ -10,29 +10,31 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
- matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
- if (matrix.buffer != NULL) {
+ matrix.data = (float *)malloc(rows * cols * sizeof(MatrixType));
+ if (matrix.data != NULL) {
}
return matrix;
}
+
void clearMatrix(Matrix *matrix)
{
- if (matrix->buffer != NULL) {
- free(matrix->buffer);
- matrix->buffer = NULL;
-
+ if (matrix->data != NULL) {
+ free(matrix->data);
+ matrix->data = NULL;
}
+ matrix->rows = 0;
+ matrix->cols = 0;
+}
- }
-void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
+void setMatrixAt(MatrixType value, Matrix* matrix, unsigned int rowIdx, unsigned int colIdx)
{
- if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
+ if(rowIdx >= matrix->rows || colIdx >= matrix->cols){
return;
}
- matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
+ matrix->data[rowIdx * matrix->cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
@@ -41,20 +43,34 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
return 0;
}
MatrixType value;
- value = matrix.buffer[rowIdx * matrix.cols + colIdx];
+ value = matrix.data[rowIdx * matrix.cols + colIdx];
return value;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
-
-}
+ if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
+ Matrix errorMatrix = createMatrix(0, 0);
+ errorMatrix.data = NULL;
+ return errorMatrix;
+ }
+
+ Matrix result = createMatrix(matrix1.rows, matrix1.cols);
+ for (unsigned int i = 0; i < matrix1.rows; i++) {
+ for (unsigned int j = 0; j < matrix1.cols; j++) {
+ MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
+ setMatrixAt(sum, &result, i, j);
+ }
+ }
+ return result;
+ }
+
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows){
Matrix errorMatrix = createMatrix(0, 0);
- errorMatrix.buffer = NULL;
+ errorMatrix.data = NULL;
return errorMatrix;
}
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols);
@@ -65,7 +81,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
for(size_t k = 0; k < matrix1.cols; k++){
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
- setMatrixAt(sum, matrix3, i, j);
+ setMatrixAt(sum, &matrix3, i, j);
}
}