Files
neuronalesNetz/Startcode/matrix.c
T
2026-11-17 14:03:04 +01:00

181 lines
4.8 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
//Prüfung ob die MAtrix ungültig ist, bei den Unit Tests gefordert das so zu machen
//sonst würde man auch für quasi nichts Speicher mit malloc reservieren und hätte unsichere Zustände
if(rows == 0 || cols == 0){
matrix.buffer = NULL;
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
return matrix;
}
//gibt den vorher belegten Speicher wieder frei
void clearMatrix(Matrix *matrix)
{
if(matrix == NULL){
return;
}
//erstmal reservierten Speicher freigeben, danach ist der zwar theoretisch frei aber der Struct hat immernoch die
//vorherigen Werte, somit muss man die alle 0 setzten um eine nicht gültige Matrix wieder zu erschaffen
//sonst zeigt man z.b. auch auf eine nicht besetzte Speicheradresse was zu nicht definiertem Verhaltem etc führen kann
free(matrix->buffer);
matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
}
//schreibt einen Wert an eine bestimmte Stelle in der Matrix
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
//diese Prüfung muss man machen, da nicht vorhandene Werte nicht gesetzt werden können
//bei einem Row-Index von 2 bräuchte die Matrix 3 Zeilen, somit müssen die Zeilen immer eins größer sein als der Index
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
return;
}
//Formel um die richtige Position im Speicherblock zu berechnen: Index = (Zeile * Anzahl_Spalten) + Spalte
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
//Liest einen Wert aus der Matrix
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
return UNDEFINED_MATRIX_VALUE;
}
//selbe Formel wie bei setMatrix nur dass man hier den jeweiligen Wert zurückgeben soll
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
// Normale Addition
if(matrix1.rows == matrix2.rows &&
matrix1.cols == matrix2.cols)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for(unsigned int i = 0; i < matrix1.rows; i++)
{
for(unsigned int j = 0; j < matrix1.cols; j++)
{
MatrixType value1 = getMatrixAt(matrix1, i, j);
MatrixType value2 = getMatrixAt(matrix2, i, j);
setMatrixAt(value1 + value2, result, i, j);
}
}
return result;
}
// Broadcasting: matrix2 ist Spaltenvektor
if(matrix1.rows == matrix2.rows &&
matrix2.cols == 1)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for(unsigned int i = 0; i < matrix1.rows; i++)
{
MatrixType vectorValue = getMatrixAt(matrix2, i, 0);
for(unsigned int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j);
setMatrixAt(value + vectorValue, result, i, j);
}
}
return result;
}
// Broadcasting: matrix1 ist Spaltenvektor
if(matrix1.rows == matrix2.rows &&
matrix1.cols == 1)
{
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
for(unsigned int i = 0; i < matrix2.rows; i++)
{
MatrixType vectorValue = getMatrixAt(matrix1, i, 0);
for(unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType value = getMatrixAt(matrix2, i, j);
setMatrixAt(value + vectorValue, result, i, j);
}
}
return result;
}
Matrix result;
result.buffer = NULL;
result.rows = 0;
result.cols = 0;
return result;
}
//hier muss Spalte der ersten Matrix = Zeile der zweiten Matrix sein
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows)
{
return createMatrix(0, 0); // hier kann man auch noch eine Fehlerbehandlung einbauen
}
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix1.rows; i++)
{
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;
}