Compare commits

3 Commits
Author SHA1 Message Date
Giorgi Kesidis 9dd4eff0d7 Bug-Fix create matrix 2025-11-10 20:07:53 +01:00
Giorgi Kesidis f0cd9abe2b Matrix fertig 2025-11-10 19:54:47 +01:00
Giorgi Kesidis 8722f104a6 matrix fertig 2025-11-10 19:54:24 +01:00
38 changed files with 155 additions and 125 deletions
+2 -59
View File
@@ -1,61 +1,4 @@
# ---> C
# Prerequisites
*.d
# Object files
mnist
runTests
*.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
*.out
*.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.
-35
View File
@@ -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)
{
}
@@ -54,7 +54,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10;
const char *path = "testFile.info2";
prepareImageFile(path, expectedWidth, 8, 2, 1);
prepareImageFile(path, 8, expectedWidth, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
@@ -70,7 +70,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10;
const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedHeight, 2, 1);
prepareImageFile(path, expectedHeight, 8, 2, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images);
View File
View File
+141
View File
@@ -0,0 +1,141 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdio.h>
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
if(rows == 0 || cols == 0)
{
matrix.rows = 0;
matrix.cols = 0;
matrix.data = NULL;
return matrix;
}
matrix.rows = rows;
matrix.cols = cols;
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data == NULL)
{
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix.data[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
}
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
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)
{
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
return; // abbruch falls fehler
}
matrix.data[rowIdx * matrix.cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
return UNDEFINED_MATRIX_VALUE;
}
return matrix.data[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n",
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
Matrix empty = {0, 0, NULL};
return empty;
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.data == NULL)
{
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
return result;
}
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
result.data[i * result.cols + j] = matrix1.data[i * matrix1.cols + j] + matrix2.data[i * matrix2.cols + j];
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows)
{
fprintf(stderr, "Fehler: Matrizen der Dimension (%u x %u) und (%u x %u) koennen nicht multipliziert werden\n",
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
Matrix empty = {0, 0, NULL};
return empty;
}
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
if (result.data == NULL)
{
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
return result;
}
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0.0;
for (int k = 0; k < matrix1.cols; k++)
{
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
}
result.data[i * result.cols +j] = sum;
}
}
return result;
}
+8
View File
@@ -7,6 +7,14 @@ typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct Matrix {
unsigned int rows;
unsigned int cols;
MatrixType *data;
#define buffer data
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
+1 -28
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};
@@ -164,7 +138,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
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) {
@@ -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);