NeuronalesNetz/matrix.c

130 lines
3.5 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
int size = rows * cols;
MatrixType *speicherbereich;
speicherbereich = (MatrixType *)calloc(size, sizeof(MatrixType) * size);
Matrix matrix = {rows, cols, &speicherbereich};
return matrix;
}
void clearMatrix(Matrix *matrix)
{
int rows = rows(*matrix);
int cols = cols(*matrix);
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
setMatrixAt(0, matrix, i, j);
}
}
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix -> data[rowIdx * rows + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
return (matrix->data[rowIdx * rows + colIdx]);
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
rows1 = rows(matrix1);
rows2 = rows(matrix2);
cols1 = cols(matrix1);
cols2 = cols(matrix2);
if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting
{
if(rows1 == 1) //Wenn die erste Matrix der Vektor ist
{
return broadcasting(matrix1, matrix2);
}
else
{
return broadcasting(matrix2, matrix1);
}
}
else if(rows1 == rows2 && cols1 == cols2) //Addition nur moeglich, wenn beide Matrizen gleiche ANzahl an Zeilen und Spalten haben
{
Matrix addition = createMatrix(rows1, cols2); //Matrix erstellt, in die die addierten Werte geschrieben werden
MatrixType wert1;
MatrixType wert2;
for(int i = 0; i < rows1; i++){ //soll fuer jedes Element durchlaufen, sodass alle Werte von beiden Matrizen aufaddiert werden.
for(int j = 0; j < cols1; j++){
wert1 = getMatrixAt(matrix1, i, j);
wert2 = getMatrixAt(matrix2, i, j);
MatrixType addierterWert = wert1 + wert2;
setMatrixAt(addierterWert, addition, i, j);
}
}
return addition;
}
else{
return 0;
}
}
Matrix broadcasting(const Matrix vektor, const Matrix matrix)
{
int rows = rows(matrix);
int cols = cols(matrix);
Matrix result = createMatrix(rows, cols);
MatrixType wert;
MatrixType koordinate;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++){
wert = getMatrixAt(matrix, i, j);
koordinate = getMatrixAt(vektor, j, 0);
setMatrixAt((koordinate + wert), result, i, j);
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
int rows1 = rows(matrix1);
int cols1 = cols(matrix1);
int rows2 = rows(matrix2);
int cols2 = cols(matrix2);
if(rows1 == cols2) //Bedingung fuer Multiplikation: Zeilen der ersten Matrix gleich Spalten 2. Matrix
{
Matrix result = createMatrix(rows1, cols2);
MatrixType wert1;
MatrixType wert2;
MatrixType addition;
for(int i = 0; i < rows1; i++)
{
for(int j = 0; j < cols2; j++)
{
wert1 = getMatrixAt(matrix1, i, j);
wert2 = getMatrixAt(matrix2, j, i);
addition += wert1 + wert2;
setMatrixAt(addition, result, i,j);
}
addition = 0;
}
return result;
}
return 0;
}
int rows(const Matrix matrix)
{
return matrix->rows;
}
int cols(const Matrix matrix)
{
return matrix->cols;
}