generated from freudenreichan/info2Praktikum-NeuronalesNetz
75 lines
2.3 KiB
C
75 lines
2.3 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
MatrixTyp erstelleMatrix(unsigned int reihen, unsigned int spalten)
|
|
{
|
|
MatrixTyp m;
|
|
m.reihen = reihen;
|
|
m.spalten = spalten;
|
|
m.werte = (MatrixWert *)malloc(reihen * spalten * sizeof(MatrixWert));
|
|
for (unsigned int i = 0; i < reihen * spalten; ++i) {
|
|
m.werte[i] = UNDEFINIERTER_MATRIXWERT;
|
|
}
|
|
return m;
|
|
}
|
|
|
|
void loescheMatrix(MatrixTyp *matrix)
|
|
{
|
|
free(matrix->werte);
|
|
matrix->werte = NULL;
|
|
matrix->reihen = 0;
|
|
matrix->spalten = 0;
|
|
}
|
|
|
|
void setzeMatrixWert(MatrixWert wert, MatrixTyp matrix, unsigned int reiheIndex, unsigned int spalteIndex)
|
|
{
|
|
if (reiheIndex < matrix.reihen && spalteIndex < matrix.spalten) {
|
|
matrix.werte[reiheIndex * matrix.spalten + spalteIndex] = wert;
|
|
}
|
|
}
|
|
|
|
MatrixWert holeMatrixWert(const MatrixTyp matrix, unsigned int reiheIndex, unsigned int spalteIndex)
|
|
{
|
|
if (reiheIndex < matrix.reihen && spalteIndex < matrix.spalten) {
|
|
return matrix.werte[reiheIndex * matrix.spalten + spalteIndex];
|
|
}
|
|
return UNDEFINIERTER_MATRIXWERT;
|
|
}
|
|
|
|
MatrixTyp addiereMatrix(const MatrixTyp matrix1, const MatrixTyp matrix2)
|
|
{
|
|
if (matrix1.reihen != matrix2.reihen || matrix1.spalten != matrix2.spalten) {
|
|
return erstelleMatrix(0, 0);
|
|
}
|
|
|
|
MatrixTyp ergebnis = erstelleMatrix(matrix1.reihen, matrix1.spalten);
|
|
for (unsigned int i = 0; i < matrix1.reihen; ++i) {
|
|
for (unsigned int j = 0; j < matrix1.spalten; ++j) {
|
|
MatrixWert wert = holeMatrixWert(matrix1, i, j) + holeMatrixWert(matrix2, i, j);
|
|
setzeMatrixWert(wert, ergebnis, i, j);
|
|
}
|
|
}
|
|
return ergebnis;
|
|
}
|
|
|
|
MatrixTyp multipliziereMatrix(const MatrixTyp matrix1, const MatrixTyp matrix2)
|
|
{
|
|
if (matrix1.spalten != matrix2.reihen) {
|
|
return erstelleMatrix(0, 0);
|
|
}
|
|
|
|
MatrixTyp ergebnis = erstelleMatrix(matrix1.reihen, matrix2.spalten);
|
|
for (unsigned int i = 0; i < matrix1.reihen; ++i) {
|
|
for (unsigned int j = 0; j < matrix2.spalten; ++j) {
|
|
MatrixWert summe = 0;
|
|
for (unsigned int k = 0; k < matrix1.spalten; ++k) {
|
|
summe += holeMatrixWert(matrix1, i, k) * holeMatrixWert(matrix2, k, j);
|
|
}
|
|
setzeMatrixWert(summe, ergebnis, i, j);
|
|
}
|
|
}
|
|
return ergebnis;
|
|
} |