Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f1f3a90e82 | |||
| 174e356286 | |||
| f34adaac30 | |||
| 2ea8e29d28 | |||
| 44f9e3cf0e |
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
}
|
||||
Binary file not shown.
148
matrix.c
148
matrix.c
@ -3,33 +3,161 @@
|
||||
#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;
|
||||
|
||||
if (rows == 0 || cols == 0)
|
||||
{
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
}
|
||||
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
if (matrix != NULL)
|
||||
{
|
||||
if (matrix->buffer != NULL)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 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 || matrix.rows <= rowIdx || matrix.cols <= colIdx)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
||||
{
|
||||
Matrix addiert = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
MatrixType wert = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(wert, addiert, i, j);
|
||||
}
|
||||
}
|
||||
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++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType wert = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(wert, addiert, i, j);
|
||||
}
|
||||
}
|
||||
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++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/* 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)
|
||||
{
|
||||
|
||||
}
|
||||
if(matrix1.cols == matrix2.rows && matrix1.buffer != NULL && matrix2.buffer != NULL)
|
||||
{
|
||||
Matrix mult = createMatrix(matrix1.rows, matrix2.cols);
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols; 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
22
matrix.h
22
matrix.h
@ -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
|
||||
*/
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user