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
6 changed files with 179 additions and 194 deletions
-3
View File
@@ -1,3 +0,0 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
-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 -163
View File
@@ -1,163 +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
/* Erstellt eine Matrix mit den gegebenen Zeilen und Spalten */
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m; Matrix m;
if (rows == 0 || cols == 0) if (rows == 0 || cols == 0)
{ {
m.rows = 0; m.rows = 0;
m.cols = 0; m.cols = 0;
m.buffer = NULL; m.buffer = NULL;
return m; return m;
} }
m.rows = rows; m.rows = rows;
m.cols = cols; m.cols = cols;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
return m; return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix != NULL) if (matrix != NULL)
{ {
if (matrix->buffer != NULL) if (matrix->buffer != NULL)
{ {
free(matrix->buffer); free(matrix->buffer);
matrix->buffer = NULL; matrix->buffer = NULL;
} }
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (matrix.buffer != NULL) if (matrix.buffer != NULL)
{ {
if (rowIdx < matrix.rows && colIdx < matrix.cols) if (rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
} }
} }
/* Ausgeben eines Wertes aus der Matrix
* matrix Die Matrix MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
* rowIdx Der Zeilenindex {
* colIdx Der Spaltenindex if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
* Der Wert an der Stelle (rowIdx, colIdx) wird zurückgegeben {
* Wenn der Index außerhalb der Matrix liegt ist es nicht möglich den Wert auszugeben return UNDEFINED_MATRIX_VALUE;
*/ }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ return matrix.buffer[rowIdx * matrix.cols + colIdx];
if(matrix.buffer == NULL || matrix.rows <= rowIdx || matrix.cols <= colIdx) }
{
return UNDEFINED_MATRIX_VALUE; Matrix add(const Matrix matrix1, const Matrix matrix2)
} {
return matrix.buffer[rowIdx * matrix.cols + colIdx]; Matrix result;
/*Da im Speicher eindimensional anneinandergereit [000111222333] (.rows = 4, .cols = 3);
Reihenindex (fängt bei null an) * Anzahl der Spalten der Matrix (2(3.Reihe)*3 = Stelle 6)[222333] -> überspringen bis zur gewünschten Zeile; if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
dann plus Spaltenindex = gewünschter Wert im Speicher*/ {
} result.rows = 0;
result.cols = 0;
/* Addition von zwei Matrizen result.buffer = NULL;
* matrix1 Die erste Matrix return result;
* matrix2 Die zweite Matrix }
* Die Addition der beiden Matrizen wird zurückgegeben
* Wenn die Matrizen nicht die gleiche Gräße haben if (matrix1.cols == matrix2.cols)
oder eine Matrix kein Spaltenvektor ist (Broadcasting), wird NULL zurückgegeben {
*/ result = createMatrix(matrix1.rows, matrix1.cols);
Matrix add(const Matrix matrix1, const Matrix matrix2) for (int i = 0; i < matrix1.rows; i++)
{ {
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) for (int j = 0; j < matrix1.cols; j++)
{ {
Matrix addiert = createMatrix(matrix1.rows, matrix1.cols); MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
for(int i = 0; i < matrix1.rows; i++) setMatrixAt(value, result, i, j);
{ }
for(int j = 0; j < matrix1.cols; j++) }
{ return result;
MatrixType wert = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); }
setMatrixAt(wert, addiert, i, j);
} if (matrix1.cols == 1 && matrix2.cols > 1)
} {
return addiert; result = createMatrix(matrix1.rows, matrix2.cols);
} for (int i = 0; i < matrix1.rows; i++)
else if(matrix1.rows == matrix2.rows && matrix1.cols == 1 && matrix2.cols > 0) {
{ for (int j = 0; j < matrix2.cols; j++)
Matrix addiert = createMatrix(matrix2.rows, matrix2.cols); {
for(int i = 0; i < matrix2.rows; i++) MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
{ setMatrixAt(value, result, i, j);
for(int j = 0; j < matrix2.cols; j++) }
{ }
MatrixType wert = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j); return result;
setMatrixAt(wert, addiert, i, j); }
} else if (matrix2.cols == 1 && matrix1.cols > 1)
} {
return addiert; result = createMatrix(matrix1.rows, matrix1.cols);
} for (int i = 0; i < matrix1.rows; i++)
else if(matrix1.rows == matrix2.rows && matrix2.cols == 1 && matrix1.cols > 0) {
{ for (int j = 0; j < matrix1.cols; j++)
Matrix addiert = createMatrix(matrix1.rows, matrix1.cols); {
for(int i = 0; i < matrix1.rows; i++) MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
{ setMatrixAt(value, result, i, j);
for(int j = 0; j < matrix1.cols; j++) }
{ }
MatrixType wert = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0); return result;
setMatrixAt(wert, addiert, i, j); }
}
} //Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
return addiert; result.rows = 0;
} result.cols = 0;
else result.buffer = NULL;
{ return result;
Matrix addiert = {0, 0, UNDEFINED_MATRIX_VALUE}; }
return addiert;
} Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
}
/* Multiplikation von zwei Matrizen if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
* matrix1 Die erste Matrix {
* matrix2 Die zweite Matrix result.rows = 0;
* Die Multiplikation der beiden Matrizen wird zurückgegeben result.cols = 0;
* l x m * m x n = l x n -> Spalte der ersten Matrix muss gleich der Zeile der zweiten Matrix sein result.buffer = NULL;
-> wenn das nicht der Fall ist, wird NULL zurückgegeben return result;
*/ }
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ result = createMatrix(matrix1.rows, matrix2.cols);
if(matrix1.cols == matrix2.rows && matrix1.buffer != NULL && matrix2.buffer != NULL)
{ for (int i = 0; i < matrix1.rows; i++)
Matrix mult = createMatrix(matrix1.rows, matrix2.cols); {
for(int i = 0; i < matrix1.rows; i++) for (int j = 0; j < matrix2.cols; j++)
{ {
for(int j = 0; j < matrix2.cols; j++) MatrixType sum = 0;
{
MatrixType wert = 0; for (int k = 0; k < matrix1.cols; k++)
MatrixType res = 0; {
for(int k = 0; k < matrix2.rows; k++) sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
{ }
wert = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); setMatrixAt(sum, result, i, j);
res = res + wert; }
wert = res; }
}
setMatrixAt(wert, mult, i, j); return result;
} }
}
return mult;
}
else
{
Matrix mult = {0, 0, UNDEFINED_MATRIX_VALUE};
return mult;
}
}
-22
View File
@@ -7,11 +7,6 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int 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);
@@ -22,20 +17,3 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif #endif
/*
struct -> Matrix liegt im Speicher direkt hintereinander
Array:
000
111
222
333
Speicher -> [000111222333] (Könnte auch andere anzahl an reihen oder spalten haben in speicher nicht sichtabar)
rows und cols Anzahl der Reihen und Zeilen (fängt bei 1 an)
buffer zeigt auf den Wert im Speicher
*/
+28 -1
View File
@@ -46,7 +46,7 @@ void test_addReturnsCorrectResult(void)
Matrix result = add(matrix1, matrix2); Matrix result = add(matrix1, matrix2);
float expectedResults[] = {8, 10, 12, 14, 16, 18 }; float expectedResults[] = {8, 10, 12, 14, 16, 18};
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows); TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows);
TEST_ASSERT_EQUAL_UINT32(matrix2.rows, result.rows); TEST_ASSERT_EQUAL_UINT32(matrix2.rows, result.rows);
@@ -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);