Files
Info2-Pr-NeuroNetz/Startcode/matrix.c
T
2026-10-30 12:54:23 +01:00

95 lines
2.7 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix = {NULL, 0, 0}; // NUll init
if(rows > 0 && cols > 0)
{
matrix.buffer = (MatrixType *)calloc((size_t)rows * cols, sizeof(MatrixType));
if(matrix.buffer != NULL) //sichergehn das Buffer holen geklappt hat, dann setzen
{
matrix.rows = rows;
matrix.cols = cols;
}
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
if(matrix != NULL) //Check ob die Matrix nicht eh schon leer ist
{
free(matrix->buffer); //memory freen
matrix->buffer = NULL; // buffer entfernen
matrix->rows = 0;
matrix->cols = 0;
}
}
// Helper Function, um zuschauen ob Index zugriff valide ist.
static int isValidIndex(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx){
return matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if(isValidIndex(matrix, rowIdx, colIdx))
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// Für Zugriff auf Row2, Col2, also m6, muss man im Buffer Die Positionen um 1 row +
MatrixType value;
if(isValidIndex(matrix, rowIdx, colIdx))
value = matrix.buffer[rowIdx * matrix.cols + colIdx];
return value;
}
static int haveSameDimensions(const Matrix matrix1, const Matrix matrix2)
{
return matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols;
}
Matrix addBroadcast(const Matrix matrix, const Matrix vector){
// Broadcasting Add von Matrix + Vector
Matrix result = createMatrix(0, 0);
}
static int isBroadcastable(const Matrix matrix1, const Matrix matrix2){
// Check Broadcastable
}
Matrix addElementwise(const Matrix matrix1, const Matrix matrix2){
// addieren der Matrizen
Matrix result = createMatrix(0, 0);
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {NULL, 0, 0};
if(matrix1.buffer != NULL && matrix2.buffer != NULL){
if(haveSameDimensions(matrix1, matrix2)){
// result = NULL; //addElementwise
} else if(isBroadcastable(matrix1, matrix2)) { // Check ob Matrix2 ein Vector ist
// result = NULL; //addBroadcast
} else if(isBroadcastable(matrix1, matrix2)) { // Check ob Matrix2 ein Vector ist
// result = NULL; //addBroadcast
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
//kann man Matrizen multiplizieren, größen Check
}