Files
info2Praktikum-NeuronalesNetz/Startcode/matrix.c
T
2026-11-12 09:51:40 +01:00

163 lines
4.6 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
if (rows == 0 || cols == 0) {
return (Matrix){0}; // Setzt rows, cols und den Puffer auf 0/NULL
}
Matrix m;
m.rows = rows;
m.cols = cols;
// Speicher reservieren und mit 0 initialisieren
m.buffer = (MatrixType *)calloc((size_t)rows * cols, sizeof(MatrixType));
// Falls Arbeitsspeicher voll ist
if (m.buffer == NULL) {
m.rows = 0;
m.cols = 0;
}
return m;
}
void clearMatrix(Matrix *matrix)
{
//prüfen ob pointer exisitert und ob es einen buffer gibt
if (matrix != NULL && matrix->buffer != NULL)
{
// Speicher freigeben
free(matrix->buffer);
// Werte zurücksetzen
matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
}
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Sind Idx innerhalb der Matrix?
if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols)
{
// Umrechnung von 2D auf 1D
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Sicherheitsprüfung wie beim setzen
if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols)
{
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
// Fehlerwert, falls außerhalb der grenzen
return UNDEFINED_MATRIX_VALUE;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
// Prüfen ob Zeilenanzahl gleich ist und ob buffer existieren
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
{
return (Matrix){0};
}
// Wenn beide Matrixen gleich groß sind:
if (matrix1.cols == matrix2.cols)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) return (Matrix){0};
unsigned int totalElements = matrix1.rows * matrix1.cols;
for (unsigned int i = 0; i < totalElements; i++)
{
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
}
return result;
}
//Wenn matrix2 ein Spaltenvektor ist
else if (matrix2.cols == 1)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) return (Matrix){0};
for (unsigned int r = 0; r < matrix1.rows; r++)
{
for (unsigned int c = 0; c < matrix1.cols; c++)
{
// Wert mit r und c aus m1 + Wert mit r aus m2
result.buffer[r * matrix1.cols + c] = matrix1.buffer[r * matrix1.cols + c] + matrix2.buffer[r];
}
}
return result;
}
//Wenn matrix1 ein Spaltenvektor ist
else if (matrix1.cols == 1)
{
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
if (result.buffer == NULL) return (Matrix){0};
for (unsigned int r = 0; r < matrix2.rows; r++)
{
for (unsigned int c = 0; c < matrix2.cols; c++)
{
//gleich wie davor nur m1 und m2 getauscht
result.buffer[r * matrix2.cols + c] = matrix2.buffer[r * matrix2.cols + c] + matrix1.buffer[r];
}
}
return result;
}
return (Matrix){0};
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
// Überprüfen, ob die Matrizen gültig sind und ob ihre Dimensionen eine Multiplikation erlauben
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
return (Matrix){0};
}
// Ergebnismatrix erstellen
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
// Überprüfen, ob die Speicherreservierung erfolgreich war
if (result.buffer == NULL)
{
return (Matrix){0};
}
// Jede Zeile der ersten Matrix durchlaufen
for (unsigned int i = 0; i < matrix1.rows; i++)
{
// Jede Spalte der zweiten Matrix durchlaufen
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
// Summe der Produkte berechnen
for (unsigned int k = 0; k < matrix1.cols; k++)
{
sum += matrix1.buffer[i * matrix1.cols + k] *
matrix2.buffer[k * matrix2.cols + j];
}
// Berechneten Wert in der Ergebnismatrix speichern
result.buffer[i * result.cols + j] = sum;
}
}
return result;
}