5 Commits
Author SHA1 Message Date
hofmannsv99200 f1f3a90e82 fixed problems in matrix.c 2025-11-28 09:15:45 +01:00
hofmannsv99200 174e356286 little fixes 2025-11-28 08:43:46 +01:00
hofmannsv99200 f34adaac30 matrix.c fertiggestellt 2025-11-28 08:37:59 +01:00
hofmannsv99200 2ea8e29d28 matrix.c get, add und multiply programmiert 2025-11-27 17:24:13 +01:00
hofmannsv99200 44f9e3cf0e neuste pdf 2025-11-21 08:10:41 +01:00
6 changed files with 194 additions and 179 deletions
+3
View File
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
+2
View File
@@ -0,0 +1,2 @@
LDFLAGS = -lGL -lX11 -lm
BINARIES = ./linux
+3
View File
@@ -0,0 +1,3 @@
LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -framework IOKit -framework Cocoa -framework CoreVideo
ARCH := $(shell uname -m)
BINARIES = ./macos-$(ARCH)
+163 -151
View File
@@ -1,151 +1,163 @@
#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
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) * matrix Die Matrix
{ * rowIdx Der Zeilenindex
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) * colIdx Der Spaltenindex
{ * Der Wert an der Stelle (rowIdx, colIdx) wird zurückgegeben
return UNDEFINED_MATRIX_VALUE; * Wenn der Index außerhalb der Matrix liegt ist es nicht möglich den Wert auszugeben
} */
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)
{
Matrix add(const Matrix matrix1, const Matrix matrix2) return UNDEFINED_MATRIX_VALUE;
{ }
Matrix result; return matrix.buffer[rowIdx * matrix.cols + colIdx];
/*Da im Speicher eindimensional anneinandergereit [000111222333] (.rows = 4, .cols = 3);
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows) Reihenindex (fängt bei null an) * Anzahl der Spalten der Matrix (2(3.Reihe)*3 = Stelle 6)[222333] -> überspringen bis zur gewünschten Zeile;
{ dann plus Spaltenindex = gewünschter Wert im Speicher*/
result.rows = 0; }
result.cols = 0;
result.buffer = NULL; /* Addition von zwei Matrizen
return result; * matrix1 Die erste Matrix
} * matrix2 Die zweite Matrix
* Die Addition der beiden Matrizen wird zurückgegeben
if (matrix1.cols == matrix2.cols) * Wenn die Matrizen nicht die gleiche Gräße haben
{ oder eine Matrix kein Spaltenvektor ist (Broadcasting), wird NULL zurückgegeben
result = createMatrix(matrix1.rows, matrix1.cols); */
for (int i = 0; i < matrix1.rows; i++) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
for (int j = 0; j < matrix1.cols; j++) if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
{ {
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); Matrix addiert = createMatrix(matrix1.rows, matrix1.cols);
setMatrixAt(value, result, i, j); for(int i = 0; i < matrix1.rows; i++)
} {
} 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) }
{ }
result = createMatrix(matrix1.rows, matrix2.cols); return addiert;
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);
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j); for(int i = 0; i < matrix2.rows; i++)
setMatrixAt(value, result, i, j); {
} for(int j = 0; j < matrix2.cols; j++)
} {
return result; MatrixType wert = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
} setMatrixAt(wert, addiert, i, j);
else if (matrix2.cols == 1 && matrix1.cols > 1) }
{ }
result = createMatrix(matrix1.rows, matrix1.cols); return addiert;
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);
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0); 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, 0);
} setMatrixAt(wert, addiert, i, j);
}
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1 }
result.rows = 0; return addiert;
result.cols = 0; }
result.buffer = NULL; else
return result; {
} Matrix addiert = {0, 0, UNDEFINED_MATRIX_VALUE};
return addiert;
Matrix multiply(const Matrix matrix1, const Matrix matrix2) }
{
Matrix result;
}
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) /* Multiplikation von zwei Matrizen
{ * matrix1 Die erste Matrix
result.rows = 0; * matrix2 Die zweite Matrix
result.cols = 0; * Die Multiplikation der beiden Matrizen wird zurückgegeben
result.buffer = NULL; * l x m * m x n = l x n -> Spalte der ersten Matrix muss gleich der Zeile der zweiten Matrix sein
return result; -> wenn das nicht der Fall ist, wird NULL zurückgegeben
} */
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 j = 0; j < matrix2.cols; j++) for(int i = 0; i < matrix1.rows; i++)
{ {
MatrixType sum = 0; for(int j = 0; j < matrix2.cols; j++)
{
for (int k = 0; k < matrix1.cols; k++) MatrixType wert = 0;
{ MatrixType res = 0;
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); for(int k = 0; k < matrix2.rows; k++)
} {
setMatrixAt(sum, result, i, j); wert = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
} res = res + wert;
} wert = res;
}
return result; setMatrixAt(wert, mult, i, j);
} }
}
return mult;
}
else
{
Matrix mult = {0, 0, UNDEFINED_MATRIX_VALUE};
return mult;
}
}
+22
View File
@@ -7,6 +7,11 @@ 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);
@@ -17,3 +22,20 @@ 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
*/
+1 -28
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,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};
@@ -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);