4 Commits
Author SHA1 Message Date
richardtfe99149 c7150d2250 Dateien nach "/" hochladen 2025-11-27 19:23:34 +00:00
richardtfe99149 8240e7bab2 matrix.c gelöscht 2025-11-27 19:23:07 +00:00
pvtrx b488c5bf55 added new files 2025-11-14 08:14:25 +01:00
pvtrx 2d8e53ed9c deleted another linux and mac file 2025-11-07 10:52:46 +01:00
5 changed files with 178 additions and 40 deletions
Binary file not shown.
-2
View File
@@ -1,2 +0,0 @@
LDFLAGS = -lGL -lX11 -lm
BINARIES = ./linux
-3
View File
@@ -1,3 +0,0 @@
LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -framework IOKit -framework Cocoa -framework CoreVideo
ARCH := $(shell uname -m)
BINARIES = ./macos-$(ARCH)
+151 -35
View File
@@ -1,35 +1,151 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m;
}
if (rows == 0 || cols == 0)
void clearMatrix(Matrix *matrix) {
{ m.rows = 0;
m.cols = 0;
} m.buffer = NULL;
return m;
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) }
{
m.rows = rows;
} m.cols = cols;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ return m;
}
}
void clearMatrix(Matrix *matrix)
Matrix add(const Matrix matrix1, const Matrix matrix2) {
{ if (matrix != NULL)
{
} if (matrix->buffer != NULL)
{
Matrix multiply(const Matrix matrix1, const Matrix matrix2) free(matrix->buffer);
{ matrix->buffer = NULL;
}
}
matrix->rows = 0;
matrix->cols = 0;
}
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (matrix.buffer != NULL)
{
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
return UNDEFINED_MATRIX_VALUE;
}
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
if (matrix1.cols == matrix2.cols)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
if (matrix1.cols == 1 && matrix2.cols > 1)
{
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
else if (matrix2.cols == 1 && matrix1.cols > 1)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
setMatrixAt(value, result, i, j);
}
}
return result;
}
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
for (int k = 0; k < matrix1.cols; k++)
{
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, result, i, j);
}
}
return result;
}
+27
View File
@@ -71,6 +71,32 @@ 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};
@@ -159,6 +185,7 @@ 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);