niklaskegelmann 396ad0c4ca Multiplikation versucht
Prüfung passt fürs erste
Multiplikation an sich safe falsch aber is ne erste Überlegung
2025-11-12 19:47:43 +01:00

120 lines
2.8 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)
{
free(matrix->buffer); //gibt den heap speicher frei
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;
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
else
{
//Matrix result ist die neue Matrix für das Ergebnis
Matrix result;
result.rows = matrix1.rows;
result.cols = matrix1.cols;
//TODO: Chat sagt hier noch buffer calloc rein
//Addition der beiden Matrizen
for (int i = 0; i < result.rows * result.cols; i++)
{
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;
result.rows = matrix1.rows;
result.cols = matrix2.cols;
//TODO: Chat sagt hier noch buffer calloc rein
//Multipliaktion der beiden Matrizen
for(int i = 0; ... ; i++)
{
result.buffer[i] = matrix1.buffer[i] * matrix2.buffer[i];
for(int j = 1; ... ; j++)
{
result.buffer[i] += matrix1.buffer[j] * matrix2.buffer[i*j]
}
}
//FIXME: hier steht glaube viel bullshit lol
}
}