forked from freudenreichan/info2Praktikum-NeuronalesNetz
Implement matrix.c
This commit is contained in:
1 parent
447e77a7bf
commit
9b993719e8
1 file changed
+107
-12
+107
-12
@@ -2,34 +2,129 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// Matrix erstellen
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix matrix;
|
||||||
|
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
|
||||||
|
matrix.data = calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
if (matrix.data == NULL)
|
||||||
|
{
|
||||||
|
matrix.rows = 0;
|
||||||
|
matrix.cols = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
// Matrix-Speicher freigeben
|
||||||
|
void clearMatrix(Matrix* matrix)
|
||||||
{
|
{
|
||||||
|
if (matrix != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->data);
|
||||||
|
|
||||||
|
matrix->data = NULL;
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
// Wert an einer bestimmten Position setzen
|
||||||
|
void setMatrixAt(MatrixType value, Matrix matrix,
|
||||||
|
unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.data != NULL &&
|
||||||
|
rowIdx < matrix.rows &&
|
||||||
|
colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
matrix.data[rowIdx * matrix.cols + colIdx] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
// Wert an einer bestimmten Position auslesen
|
||||||
|
MatrixType getMatrixAt(const Matrix matrix,
|
||||||
|
unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (matrix.data != NULL &&
|
||||||
|
rowIdx < matrix.rows &&
|
||||||
|
colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
return matrix.data[rowIdx * matrix.cols + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zwei Matrizen addieren
|
||||||
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 ||
|
||||||
|
matrix1.data == NULL ||
|
||||||
|
matrix2.data == NULL)
|
||||||
|
{
|
||||||
|
return createMatrix(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
if (result.data == NULL)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType value =
|
||||||
|
getMatrixAt(matrix1, i, j) +
|
||||||
|
getMatrixAt(matrix2, i, j);
|
||||||
|
|
||||||
|
setMatrixAt(value, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zwei Matrizen multiplizieren
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if (matrix1.cols != matrix2.rows ||
|
||||||
}
|
matrix1.data == NULL ||
|
||||||
|
matrix2.data == NULL)
|
||||||
|
{
|
||||||
|
return createMatrix(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
if (result.data == NULL)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) // nur < weil bei 0 angefangen zu zählen
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
sum += getMatrixAt(matrix1, i, k) *
|
||||||
|
getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
setMatrixAt(sum, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user