Compare commits

..

1 Commits
main ... Krisp2

Author SHA1 Message Date
Kristin
3c49920613 matrix.c und matrix.h, makefile für windows 2025-11-13 20:28:48 +01:00
5 changed files with 46 additions and 70 deletions

Binary file not shown.

View File

@ -57,10 +57,11 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
# --------------------------
# Clean
# --------------------------
#clean:
#ifeq ($(OS),Windows_NT)
# del /f *.o *.exe
#else
# rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
#endif
clean:
ifeq ($(OS),Windows_NT)
del /f *.o *.exe
else
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
endif
rm -f *.o *.exe

View File

@ -1,36 +1,38 @@
#include "matrix.h"
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
MatrixType* data = malloc(rows * cols * sizeof(MatrixType));
Matrix newMatrix = {rows, cols, data};
return newMatrix;
/*typedef struct {
unsigned int rows; //Zeilen
unsigned int cols; //Spalten
MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
MatrixType *data =
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
// liefert Zeiger auf Speicher
Matrix newMatrix = {rows, cols, data}; // neue Matrix nach struct
return newMatrix;
}
void clearMatrix(Matrix *matrix)
{
void clearMatrix(Matrix *matrix) {
matrix->data = UNDEFINED_MATRIX_VALUE;
matrix->rows = UNDEFINED_MATRIX_VALUE;
matrix->cols = UNDEFINED_MATRIX_VALUE;
free((*matrix).data); // Speicher freigeben
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
void setMatrixAt(MatrixType value, Matrix matrix,
unsigned int rowIdx, // Kopie der Matrix wird übergeben
unsigned int colIdx) {
matrix.data[rowIdx * matrix.cols + colIdx] =
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
unsigned int colIdx) {
return 0;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix add(const Matrix matrix1, const Matrix matrix2) {
// broadcasting
return matrix1;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }

View File

@ -6,20 +6,20 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct
{
unsigned int rows;
unsigned int cols;
MatrixType *data;
} Matrix;
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *data;
} Matrix;
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);
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);
#endif

View File

@ -71,32 +71,6 @@ void test_addFailsOnDifferentInputDimensions(void)
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)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
@ -185,7 +159,6 @@ int main()
RUN_TEST(test_clearMatrixSetsMembersToNull);
RUN_TEST(test_addReturnsCorrectResult);
RUN_TEST(test_addFailsOnDifferentInputDimensions);
RUN_TEST(test_addSupportsBroadcasting);
RUN_TEST(test_multiplyReturnsCorrectResults);
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
RUN_TEST(test_getMatrixAtReturnsCorrectResult);