generated from freudenreichan/info2Praktikum-NeuronalesNetz
137 lines
3.4 KiB
C
137 lines
3.4 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix m;
|
|
|
|
if (rows == 0 || cols == 0) //Ueberprüfen, ob ungültige Werte für Matrix vorhanden
|
|
{
|
|
m.rows = 0;
|
|
m.cols = 0;
|
|
m.buffer = NULL;
|
|
return m;
|
|
}
|
|
|
|
m.rows = rows;
|
|
m.cols = cols;
|
|
m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus
|
|
|
|
return m;
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
matrix->buffer = 0;
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
|
{
|
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
|
}
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
MatrixType value = 0;
|
|
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
|
{
|
|
value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
typedef enum {elemt_add, broadcast} Addiermodi;
|
|
Addiermodi modus;
|
|
|
|
if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows))
|
|
{
|
|
modus = broadcast;
|
|
}
|
|
else if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols))
|
|
{
|
|
modus = elemt_add;
|
|
}
|
|
else
|
|
{
|
|
Matrix failure = {NULL, 0, 0};
|
|
return failure;
|
|
}
|
|
|
|
Matrix m;
|
|
if(modus == elemt_add)
|
|
{
|
|
m = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
for(int i = 0; i< matrix1.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix1.cols ; j++)
|
|
{
|
|
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix1.cols+j];
|
|
}
|
|
}
|
|
return m;
|
|
}
|
|
else if(modus == broadcast)
|
|
{
|
|
|
|
if(matrix1.cols == 1)
|
|
{
|
|
m = createMatrix(matrix2.rows, matrix2.cols);
|
|
for(int i = 0; i< matrix2.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix2.cols ; j++)
|
|
{
|
|
m.buffer[i*matrix2.cols+j] = matrix2.buffer[i*matrix2.cols+j] + matrix1.buffer[i*matrix1.cols];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m = createMatrix(matrix1.rows, matrix1.cols);
|
|
for(int i = 0; i< matrix1.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix1.cols ; j++)
|
|
{
|
|
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix2.cols];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return m;
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if(matrix1.cols != matrix2.rows)
|
|
{
|
|
Matrix failure = {NULL, 0, 0};
|
|
return failure;
|
|
}
|
|
|
|
Matrix m = createMatrix(matrix1.rows, matrix2.cols);
|
|
|
|
for(int i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix2.cols ; j++)
|
|
{
|
|
int value = 0;
|
|
for(int k = 0; k < matrix1.cols; k++)
|
|
{
|
|
value += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k , j);
|
|
}
|
|
m.buffer[i*matrix2.cols+j] = value;
|
|
}
|
|
}
|
|
return m;
|
|
}
|