2025-11-17 17:29:25 +01:00

193 lines
6.4 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdio.h>
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
if (rows == 0 || cols == 0) {
matrix.rows = 0;
matrix.cols = 0;
matrix.buffer = NULL;
}
else
{
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
for (int i = 0; i < matrix->rows; i++)
{ for (int j = 0; j < matrix->cols; j++)
{
matrix->cols = 0;
matrix->rows = 0;
matrix->buffer = NULL;
}
}
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Sicherheitsprüfung: Sicherstellen, dass der Matrix-Buffer zugewiesen ist und die Indizes innerhalb der Grenzen liegen
//matrix.buffer == NULL: Prüft, ob überhaupt Speicher für die Matrix zugewiesen wurde
//rowIdx >= matrix.rows: Prüft, ob der angegebene Zeilenindex nicht größer oder gleich der Gesamtzahl der Zeilen ist
//colIdx >= matrix.cols: Macht dasselbe für den Spaltenindex
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) {
return;
}
//value: Das ist der Wert, den Sie in die Matrix schreiben möchten (z.B. 5.0, 10, etc.)
//matrix: Das ist die Matrix-Struktur selbst, in die der Wert geschrieben werden soll
//rowIdx, colIdx: Das sind die Zeilen- und Spaltenindizes, an denen der Wert gespeichert werden soll.
//Wichtig ist, dass diese Indizes bei 0 beginnen (also 0 bis rows-1 und 0 bis cols-1)
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Sicherheitsprüfung: Sicherstellen, dass der Matrix-Buffer zugewiesen ist und die Indizes innerhalb der Grenzen liegen
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) {
// Wenn der Zugriff ungültig ist wird 0 zurückgegeben
return (MatrixType)0; // 0 in MatrixType umwandeln (z.B. 0.0 für float/double)
}
// Berechnung des 1D-Index: Ebenfalls identisch zur setMatrixAt-Funktion
//Wert zurückgeben: Sobald der korrekte oneD_index gefunden ist, wird der Wert an matrix.buffer[oneD_index] ausgelesen und als Ergebnis der Funktion zurückgegeben.
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2) {
Matrix matrix;
if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) { //Broadcasting für matrix1
matrix.cols = matrix2.cols;
matrix.rows = matrix2.rows;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++){
matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols];
}
}
return matrix;
}
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows)
{ //Broadcasting für matrix2
matrix.cols = matrix1.cols;
matrix.rows = matrix1.rows;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++){
matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols];
}
}
return matrix;
}
else if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
{
//Falls beide Matrizen die gleichen Dimensionen haben
matrix.cols = matrix1.cols;
matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix1.cols; j++) {
(matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix2.cols+j]);
}
}
return matrix;
}
else {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix matrix;
if(matrix1.cols == matrix2.rows)
{
matrix.cols = matrix2.cols;
matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i<matrix.rows; i++) {
for (int j = 0; j<matrix.cols; j++) {
matrix.buffer[i*matrix.cols+j] = 0; //Füllen des Arrays mit Nullen, da im nächsten Schritt mit += gearbeitet wird.
}
}
for(int i = 0; i < matrix.rows; i++)
{
for(int j = 0; j < matrix.cols; j++)
{
for(int k = 0; k < matrix1.cols; k++)
{
//Multiplikation der Matrizen
matrix.buffer[i*matrix.cols+j] += matrix1.buffer[i*matrix1.cols+k] * matrix2.buffer[k*matrix2.cols+j];
}
}
}
return matrix;
}
else
{
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
//Siehe I1-Pr Aufgabe
}