Files
Team_3/neuronalesNetz/matrix.c
T

108 lines
3.4 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
if (rows == 0 || cols == 0) { //kein Speicher freigegeben, wenn die Matrix die 0. Dimension hat
matrix.rows = 0;
matrix.cols = 0;
matrix.buffer = NULL;
}
else { //buffer, zeigend auf einen Speicher -> rows*cows
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = (float*)calloc(rows * cols, sizeof(float));
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
if (matrix->buffer != NULL) {
for (int i = 0; i < matrix->rows * matrix->cols; i++)
matrix->buffer[i] = 0;
free(matrix->buffer);
matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
}
}
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)
{
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
return UNDEFINED_MATRIX_VALUE;
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
static int istSkalar (const Matrix m) {
return m.rows == 1 && m.cols == 1;
}
static int addKompatibel(const Matrix a, const Matrix b) {
int gleicheZeilen = (a.rows == b.rows);
int gleicheSpalten = (a.cols == b.cols);
return (gleicheZeilen && gleicheSpalten)
|| istSkalar(a) || istSkalar(b)
|| (gleicheZeilen && (a.cols == 1 || b.cols == 1))
|| (gleicheSpalten && (a.rows == 1 || b.rows == 1));
}
static MatrixType wertErweitert(const Matrix m, unsigned int i, unsigned int j) {
unsigned int spalte = (m.cols ==1) ? 0:i;
unsigned int zeile = (m.rows ==1) ? 0:j;
return m.buffer[zeile * m.cols + spalte];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.buffer == NULL || matrix2.buffer == NULL || !addKompatibel(matrix1, matrix2))
return createMatrix(0,0);
unsigned int spalten = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
unsigned int zeilen = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
Matrix result = createMatrix(zeilen, spalten);
if (result.buffer == NULL)
return createMatrix(0,0);
for (unsigned int j = 0 ; j < zeilen; j++) {
for (unsigned int i = 0 ; i < spalten; i++)
result.buffer[j * spalten + i] = wertErweitert(matrix1, i, j) + wertErweitert(matrix2, i, j);
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
return createMatrix(0, 0);
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
if (result.buffer == NULL)
return createMatrix(0, 0);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix2.cols; j++) {
MatrixType summe = 0;
for (unsigned int k = 0; k < matrix1.cols; k++) {
summe += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
}
result.buffer[i * result.cols + j] = summe;
}
}
return result;
}