127 lines
3.3 KiB
C
127 lines
3.3 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)
|
|
{
|
|
if (rows != 0 && cols != 0)
|
|
{
|
|
Matrix matrix;
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
matrix.buffer = (float*) calloc(rows * cols, sizeof(float)); //belegt den speicherplatz mit calloc -> mit 0
|
|
return matrix;
|
|
}
|
|
else
|
|
{ //Bei einer "falschen" Matrix eine leere zurückgeben, ohne speicher zu belegen
|
|
printf("Nullgroesse der Matrix!!!\n");
|
|
Matrix matrix;
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
matrix.buffer = NULL;
|
|
return matrix;
|
|
}
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
// Sicherheits-Check für den übergebenen Zeiger
|
|
if (matrix != NULL)
|
|
{
|
|
// **WICHTIGE KORREKTUR:** Puffer-Check vor free()
|
|
if (matrix->buffer != NULL) {
|
|
free(matrix->buffer);
|
|
}
|
|
|
|
matrix->buffer = NULL; // Zeiger auf NULL setzen
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if(rowIdx < matrix.rows && colIdx < matrix.cols){
|
|
return matrix.buffer[rowIdx * matrix.cols + colIdx]; //ACHTUNG! rowIdx und colIDX sind in Array position gedacht! matrix.cols ist normal gedacht!
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
//Überprüfen, ob die Matrizen die gleichen Dimensionen haben
|
|
//wenn nicht muss die matrix "rows/cols=0 und buffer = NULL" leer zurückgegeben werden
|
|
|
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
|
{
|
|
Matrix result = {0}; // Struktur auf 0/NULL initialisieren
|
|
result.rows = 0;
|
|
result.cols = 0;
|
|
result.buffer = NULL;
|
|
return result;
|
|
}
|
|
|
|
else
|
|
{
|
|
// **WICHTIGE KORREKTUR:** Speicher für das Ergebnis reservieren
|
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
// Prüfen, ob Speicherreservierung erfolgreich war
|
|
if (result.buffer == NULL) {
|
|
return result; // Gibt Null-Matrix zurück, falls malloc fehlschlug
|
|
}
|
|
|
|
// Addition der beiden Matrizen
|
|
for (unsigned int i = 0; i < result.rows * result.cols; i++)
|
|
{
|
|
// Achtung: Wenn Sie die Matrizen nicht per const Pointer übergeben,
|
|
// müssen Sie wissen, dass die Daten nicht temporär sind (hier ok, da lokale Kopien).
|
|
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
//Spalten matrix 1 muss mit Reihen Matrix 2 übereinstimmen
|
|
if (matrix1.cols != matrix2.rows)
|
|
{
|
|
Matrix result;
|
|
result.rows = 0;
|
|
result.cols = 0;
|
|
result.buffer = NULL;
|
|
return result;
|
|
}
|
|
|
|
else
|
|
{
|
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
|
for (unsigned int i = 0; i < result.rows; i++)
|
|
{
|
|
for (unsigned int j = 0; j < result.cols; j++)
|
|
{
|
|
MatrixType summe = 0;
|
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
|
{
|
|
summe += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
|
}
|
|
setMatrixAt(summe, result, i, j);
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
} |