diff --git a/.idea/editor.xml b/.idea/editor.xml
index 963c96f..ba605af 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -340,5 +340,245 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/matrix.c b/matrix.c
index 8847000..5b6e070 100644
--- a/matrix.c
+++ b/matrix.c
@@ -70,13 +70,17 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
- if(matrtix1.cols != matrix2.cols || matrix1.rows != matrix2.rows)
+ Matrix matrix;
+ if(matrix1.cols != matrix2.cols || matrix1.rows != matrix2.rows)
{
- return NULL;
+ matrix.buffer=NULL;
+ matrix.cols = 0;
+ matrix.rows = 0;
+ return matrix;
}
else
{
- Matrix matrix;
+
matrix.cols = matrix1.cols;
matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
@@ -91,12 +95,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
-
+ Matrix matrix;
if(matrix1.cols == matrix2.rows)
{
- Matrix matrix;
- matrix.cols = matrix1.cols;
- matrix.rows = matrix2.rows
+
+ matrix.cols = matrix2.cols;
+ matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
for(int i = 0; i < matrix1.rows; i++)
{
@@ -104,14 +108,18 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
for(int k = 0; k < matrix1.cols; k++)
{
- matrix.buffer[i*matrix.cols+j] += matrix1.buffer[i*matrix.cols+k] * matrix2.buffer[k*matrix.cols+j];
+ matrix.buffer[i*matrix.cols+j] = matrix1.buffer[i*matrix.cols+j] * matrix2.buffer[k*matrix.cols+k];
}
}
}
+ return matrix;
}
else
{
- return NULL;
+ matrix.buffer=NULL;
+ matrix.cols = 0;
+ matrix.rows = 0;
+ return matrix;
}
//Siehe I1-Pr Aufgabe
}
\ No newline at end of file
diff --git a/matrixTests.c b/matrixTests.c
index 686db1e..5319282 100644
--- a/matrixTests.c
+++ b/matrixTests.c
@@ -36,7 +36,31 @@ void test_clearMatrixSetsMembersToNull(void)
TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.rows);
TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.cols);
}
+void test_addSupportsBroadcasting(void)
+{
+ MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
+ MatrixType buffer2[] = {7, 8};
+ Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
+ Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
+ Matrix result1 = add(matrix1, matrix2);
+ Matrix result2 = add(matrix2, matrix1);
+
+ float expectedResults[] = {8, 9, 10, 12, 13, 14};
+
+ TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows);
+ TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols);
+ TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
+ TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
+
+ TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
+ TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
+ TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
+ TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
+
+ free(result1.buffer);
+ free(result2.buffer);
+}
void test_addReturnsCorrectResult(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
@@ -165,6 +189,7 @@ int main()
RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange);
RUN_TEST(test_setMatrixAtSetsCorrectValue);
RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange);
+ RUN_TEST(test_addSupportsBroadcasting);
return UNITY_END();
}
\ No newline at end of file
diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c
index 477e342..c970a29 100644
--- a/neuralNetworkTests.c
+++ b/neuralNetworkTests.c
@@ -8,6 +8,21 @@
#define IDENTIFICATION_TAG "__info2_neural_network_file_format__"
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{
+ FILE *file = fopen(path, "wb");
+ fwrite(IDENTIFICATION_TAG, 1, sizeof(IDENTIFICATION_TAG), file);
+ for(int i = 0; i < nn.numberOfLayers; i++)
+ {
+ unsigned int inputDimensions = nn.layers[i].weights.cols;
+ unsigned int outputDimensions = nn.layers[i].weights.rows;
+ fwrite(&inputDimensions, sizeof(int), 1, file);
+ fwrite(&outputDimensions, sizeof(int), 1, file);
+ fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), inputDimensions*outputDimensions, file);
+ fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), inputDimensions*outputDimensions, file);
+ }
+ int dimension = 0;
+ fwrite(&dimension, sizeof(int), 1, file);
+ fwrite(&dimension, sizeof(int), 1, file);
+ fclose(file);
}
diff --git a/testFile.info2 b/testFile.info2
deleted file mode 100644
index 1ea3b4c..0000000
--- a/testFile.info2
+++ /dev/null
@@ -1 +0,0 @@
-some_tag
\ No newline at end of file