forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae2582cfbc | ||
|
|
7f980922d0 | ||
|
|
f807f8cc1a |
No files matched your search
+114
-3
@@ -2,34 +2,145 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
// ungueltiges ergebniss definieren
|
||||||
|
Matrix matrix = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// fehlerfall wenn zeile oder spaltenanzahl = 0
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
// anfangsadresse fuer speicher ermittlen
|
||||||
|
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); // malloc liefert freie adresse oder NULL wenn kein speicher frei
|
||||||
|
|
||||||
|
// wenn speicher frei -> zeilen und spalten weitergeben
|
||||||
|
if (matrix.buffer != NULL)
|
||||||
|
{
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
// pruefen ob gueltige adresse übergeben wurde
|
||||||
|
if (matrix != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->buffer); // speicher freigeben (*matrix).buffer = matrix->buffer
|
||||||
|
matrix->buffer = NULL; // zeiger auf NULL setzen
|
||||||
|
matrix->rows = 0; // zeilen und spalten auf 0 setzen (sonst absturz in neuralNetwork.c z. 45)
|
||||||
|
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 (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = 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 && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
// laut matrixTests muessen 3 versch faelle funktionieren:
|
||||||
|
// 1. beide matrizen gleich groß
|
||||||
|
// 2. matrix1 ist spaltenvektor (durch broadcasting wird spaltenvektor verbreitert durch kopieren der spalte)
|
||||||
|
// 3. matirx2 ist spaltenvektor ''
|
||||||
|
|
||||||
|
// ungueltiges ergebnis definieren
|
||||||
|
Matrix result = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// 1. beide matrizen gleich groß
|
||||||
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 2. matrix1 ist spaltenvektor
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 3. matirx2 ist spaltenvektor
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// #spalten matrix1 muss = #zeilen matirx2 sein
|
||||||
|
if (matrix1.cols != matrix2.rows)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) // je unsigned weil rows und cols unsigned
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = 0;
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
summe += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
+6
-2
@@ -5,8 +5,12 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
typedef struct
|
||||||
|
{
|
||||||
|
MatrixType *buffer; // zeiger auf inhalt
|
||||||
|
unsigned int rows; // anzahl zeilen
|
||||||
|
unsigned int cols; // anzahl spalten
|
||||||
|
} 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);
|
||||||
|
|||||||
Reference in new issue
Block a user