Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1f3a90e82 | ||
|
|
174e356286 | ||
|
|
f34adaac30 | ||
|
|
2ea8e29d28 | ||
|
|
44f9e3cf0e |
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
LDFLAGS = -lGL -lX11 -lm
|
||||
BINARIES = ./linux
|
||||
@@ -0,0 +1,3 @@
|
||||
LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -framework IOKit -framework Cocoa -framework CoreVideo
|
||||
ARCH := $(shell uname -m)
|
||||
BINARIES = ./macos-$(ARCH)
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
/* Erstellt eine Matrix mit den gegebenen Zeilen und Spalten */
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
@@ -48,104 +48,116 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Ausgeben eines Wertes aus der Matrix
|
||||
* matrix Die Matrix
|
||||
* rowIdx Der Zeilenindex
|
||||
* colIdx Der Spaltenindex
|
||||
* 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
|
||||
*/
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||
if(matrix.buffer == NULL || matrix.rows <= rowIdx || matrix.cols <= colIdx)
|
||||
{
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
/*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;
|
||||
dann plus Spaltenindex = gewünschter Wert im Speicher*/
|
||||
}
|
||||
|
||||
/* Addition von zwei Matrizen
|
||||
* matrix1 Die erste Matrix
|
||||
* matrix2 Die zweite Matrix
|
||||
* Die Addition der beiden Matrizen wird zurückgegeben
|
||||
* Wenn die Matrizen nicht die gleiche Gräße haben
|
||||
oder eine Matrix kein Spaltenvektor ist (Broadcasting), wird NULL zurückgegeben
|
||||
*/
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
|
||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
||||
{
|
||||
Matrix addiert = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
result.rows = 0;
|
||||
result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
return result;
|
||||
for(int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
MatrixType wert = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(wert, addiert, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
if (matrix1.cols == matrix2.cols)
|
||||
return addiert;
|
||||
}
|
||||
else if(matrix1.rows == matrix2.rows && matrix1.cols == 1 && matrix2.cols > 0)
|
||||
{
|
||||
Matrix addiert = createMatrix(matrix2.rows, matrix2.cols);
|
||||
for(int i = 0; i < matrix2.rows; i++)
|
||||
{
|
||||
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;
|
||||
for(int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType wert = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(wert, addiert, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
if (matrix1.cols == 1 && matrix2.cols > 1)
|
||||
return addiert;
|
||||
}
|
||||
else if(matrix1.rows == matrix2.rows && matrix2.cols == 1 && matrix1.cols > 0)
|
||||
{
|
||||
Matrix addiert = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
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;
|
||||
for(int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
MatrixType wert = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
|
||||
setMatrixAt(wert, addiert, i, j);
|
||||
}
|
||||
}
|
||||
return addiert;
|
||||
}
|
||||
else
|
||||
{
|
||||
Matrix addiert = {0, 0, UNDEFINED_MATRIX_VALUE};
|
||||
return addiert;
|
||||
}
|
||||
|
||||
|
||||
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
|
||||
result.rows = 0;
|
||||
result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Multiplikation von zwei Matrizen
|
||||
* matrix1 Die erste Matrix
|
||||
* matrix2 Die zweite Matrix
|
||||
* Die Multiplikation der beiden Matrizen wird zurückgegeben
|
||||
* l x m * m x n = l x n -> Spalte der ersten Matrix muss gleich der Zeile der zweiten Matrix sein
|
||||
-> wenn das nicht der Fall ist, wird NULL zurückgegeben
|
||||
*/
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result;
|
||||
|
||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
||||
if(matrix1.cols == matrix2.rows && matrix1.buffer != NULL && matrix2.buffer != NULL)
|
||||
{
|
||||
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++)
|
||||
Matrix mult = 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);
|
||||
MatrixType wert = 0;
|
||||
MatrixType res = 0;
|
||||
for(int k = 0; k < matrix2.rows; k++)
|
||||
{
|
||||
wert = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||
res = res + wert;
|
||||
wert = res;
|
||||
}
|
||||
setMatrixAt(wert, mult, i, j);
|
||||
}
|
||||
}
|
||||
return mult;
|
||||
}
|
||||
else
|
||||
{
|
||||
Matrix mult = {0, 0, UNDEFINED_MATRIX_VALUE};
|
||||
return mult;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,11 @@ typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct {
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
@@ -17,3 +22,20 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
#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
@@ -46,7 +46,7 @@ void test_addReturnsCorrectResult(void)
|
||||
|
||||
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(matrix2.rows, result.rows);
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user