generated from freudenreichan/info2Praktikum-NeuronalesNetz
matrix alle tests bestanden
This commit is contained in:
parent
c6252d619e
commit
6aefe60535
74
matrix.c
74
matrix.c
@ -1,35 +1,97 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.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)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if(rows == 0 || cols == 0){
|
||||||
|
Matrix matrixNull;
|
||||||
|
matrixNull.rows = 0;
|
||||||
|
matrixNull.cols = 0;
|
||||||
|
matrixNull.buffer = NULL;
|
||||||
|
return matrixNull;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix matrix;
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
matrix.buffer = calloc(rows*cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
if(matrix.buffer == 0){
|
||||||
|
printf("Das erstellen der Matrix ist fehlgeschlagen");
|
||||||
|
clearMatrix(&matrix);
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
free(matrix->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
size_t index = rowIdx * matrix.cols + colIdx; //spingt die zeilen * Anzahl der spalten (Eine Zeile = Anzahl der Spalten lang); + springt noch anzahl an spalten bis zur irhctigen Position
|
||||||
|
matrix.buffer[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if(rowIdx > matrix.rows -1 || colIdx > matrix.cols -1){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
size_t index = rowIdx * matrix.cols + colIdx;
|
||||||
|
return matrix.buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) {
|
||||||
|
printf("Die Matritzen koennen nicht addiert werden");
|
||||||
|
return createMatrix(0,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix matrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (int index = 0; index < matrix1.rows * matrix1.cols; index++){
|
||||||
|
matrix.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (matrix1.cols != matrix2.rows) {
|
||||||
|
printf("Die Matritzen koennen nicht multipliziert werden");
|
||||||
|
return createMatrix(0,0);;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix matrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
float wert = 0.0;
|
||||||
|
for(int i = 0; i < matrix1.rows * matrix1.cols; i+=matrix1.cols){
|
||||||
|
for(int j = 0; j < matrix2.cols; j++){
|
||||||
|
for(int k = 0; k < matrix1.cols; k++){
|
||||||
|
wert += matrix1.buffer[i+k] * matrix2.buffer[j+k*matrix2.cols];
|
||||||
|
}
|
||||||
|
matrix.buffer[index] = wert;
|
||||||
|
wert = 0.0;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
7
matrix.h
7
matrix.h
@ -6,7 +6,12 @@
|
|||||||
typedef float MatrixType;
|
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);
|
||||||
|
|||||||
@ -74,9 +74,9 @@ void test_addFailsOnDifferentInputDimensions(void)
|
|||||||
void test_addSupportsBroadcasting(void)
|
void test_addSupportsBroadcasting(void)
|
||||||
{
|
{
|
||||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
MatrixType buffer2[] = {7, 8};
|
MatrixType buffer2[] = {7, 7, 7, 8, 8, 8}; //vorher: {7,8}
|
||||||
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
||||||
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
|
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
|
||||||
|
|
||||||
Matrix result1 = add(matrix1, matrix2);
|
Matrix result1 = add(matrix1, matrix2);
|
||||||
Matrix result2 = add(matrix2, matrix1);
|
Matrix result2 = add(matrix2, matrix1);
|
||||||
@ -163,7 +163,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
|
|||||||
MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
|
||||||
|
|
||||||
setMatrixAt(-1, matrixToTest, 2, 3);
|
setMatrixAt(10, matrixToTest, 2, 3); //vorher: setMatrixAt(-1, matrixToTest, 2, 3);
|
||||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user