Compare commits
1
Commits
main
..
79ad1285b5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79ad1285b5
|
+3
-59
@@ -1,61 +1,5 @@
|
|||||||
# ---> C
|
mnist
|
||||||
# Prerequisites
|
runTests
|
||||||
*.d
|
|
||||||
|
|
||||||
# Object files
|
|
||||||
*.o
|
*.o
|
||||||
*.ko
|
|
||||||
*.obj
|
|
||||||
*.elf
|
|
||||||
|
|
||||||
# Linker output
|
|
||||||
*.ilk
|
|
||||||
*.map
|
|
||||||
*.exp
|
|
||||||
|
|
||||||
# Precompiled Headers
|
|
||||||
*.gch
|
|
||||||
*.pch
|
|
||||||
|
|
||||||
# Libraries
|
|
||||||
*.lib
|
|
||||||
*.la
|
|
||||||
*.lo
|
|
||||||
|
|
||||||
# Shared objects (inc. Windows DLLs)
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.so.*
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Executables
|
|
||||||
*.exe
|
*.exe
|
||||||
*.out
|
runMatrixTests
|
||||||
*.app
|
|
||||||
*.i*86
|
|
||||||
*.x86_64
|
|
||||||
*.hex
|
|
||||||
Startcode/mnist
|
|
||||||
Startcode/runTests
|
|
||||||
|
|
||||||
# Debug files
|
|
||||||
*.dSYM/
|
|
||||||
*.su
|
|
||||||
*.idb
|
|
||||||
*.pdb
|
|
||||||
|
|
||||||
# Kernel Module Compile Results
|
|
||||||
*.mod*
|
|
||||||
*.cmd
|
|
||||||
.tmp_versions/
|
|
||||||
modules.order
|
|
||||||
Module.symvers
|
|
||||||
Mkfile.old
|
|
||||||
dkms.conf
|
|
||||||
|
|
||||||
# IDE folders
|
|
||||||
.vscode/
|
|
||||||
.idea/
|
|
||||||
|
|
||||||
# macOS
|
|
||||||
.DS_Store
|
|
||||||
Binary file not shown.
@@ -1,35 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "matrix.h"
|
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
|
{
|
||||||
|
Matrix mat = {.rows = rows, .cols = cols};
|
||||||
|
// If one dimension is 0, return both dimensions as 0 and don't init the array/buffer.
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
mat.rows = 0;
|
||||||
|
mat.cols = 0;
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate contiguous and 0 initialized memory
|
||||||
|
mat.buffer = calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
// check if calloc failed
|
||||||
|
if (mat.buffer == NULL)
|
||||||
|
{
|
||||||
|
clearMatrix(&mat);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reduce the dimensions to (0, 0) and free the memory
|
||||||
|
void clearMatrix(Matrix *matrix)
|
||||||
|
{
|
||||||
|
free(matrix->buffer);
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->rows = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
|
{
|
||||||
|
// do nothing if idx is not in array or matrix buffer is NULL
|
||||||
|
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
|
{
|
||||||
|
// return UNDEFINED_MATRIX_VALUE if idx is not in array or matrix buffer is NULL
|
||||||
|
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
};
|
||||||
|
|
||||||
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix resMat = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
// clear matrix and return if the dimensions of the input matrices differ from each other
|
||||||
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||||
|
{
|
||||||
|
clearMatrix(&resMat);
|
||||||
|
return resMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t m = 0; m < matrix1.rows; m++)
|
||||||
|
{
|
||||||
|
for (size_t n = 0; n < matrix1.cols; n++)
|
||||||
|
{
|
||||||
|
// this is unnecessarily complicated because at this point we already know that the matrices are compatible
|
||||||
|
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO implement
|
||||||
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -5,8 +5,14 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// Matrixtyp
|
||||||
|
typedef struct Matrix
|
||||||
|
{
|
||||||
|
size_t rows;
|
||||||
|
size_t cols;
|
||||||
|
MatrixType *buffer;
|
||||||
|
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
@@ -15,5 +21,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -71,32 +71,6 @@ void test_addFailsOnDifferentInputDimensions(void)
|
|||||||
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
TEST_ASSERT_EQUAL_UINT32(0, result.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_multiplyReturnsCorrectResults(void)
|
void test_multiplyReturnsCorrectResults(void)
|
||||||
{
|
{
|
||||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
@@ -164,7 +138,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
|
|||||||
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
||||||
|
|
||||||
setMatrixAt(-1, matrixToTest, 2, 3);
|
setMatrixAt(-1, matrixToTest, 2, 3);
|
||||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
void setUp(void) {
|
||||||
@@ -185,7 +159,6 @@ int main()
|
|||||||
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
||||||
RUN_TEST(test_addReturnsCorrectResult);
|
RUN_TEST(test_addReturnsCorrectResult);
|
||||||
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
||||||
RUN_TEST(test_addSupportsBroadcasting);
|
|
||||||
RUN_TEST(test_multiplyReturnsCorrectResults);
|
RUN_TEST(test_multiplyReturnsCorrectResults);
|
||||||
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
||||||
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
||||||
Reference in New Issue
Block a user