Grundkonzept Matrix

This commit is contained in:
Björn 2025-11-14 19:25:16 +01:00
parent 96c6871ea3
commit 8368439db1
2 changed files with 49 additions and 6 deletions

View File

@ -6,30 +6,67 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
// Allocate memory for the matrix structure
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data == NULL)
{
// Handle memory allocation failure
matrix.rows = 0;
matrix.cols = 0;
}
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
// Free the allocated memory for the matrix data
if (matrix->data != NULL)
{
free(matrix->data);
matrix->data = NULL;
matrix->rows = 0;
matrix->cols = 0;
}
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
// Set the value at the specified row and column index
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.data[rowIdx * matrix.cols + colIdx] = value;
}
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
// Get the value at the specified row and column index
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
return matrix.data[rowIdx * matrix.cols + colIdx];
}
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
// Add two matrices and return the result
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
// Handle dimension mismatch
Matrix emptyMatrix = createMatrix(0, 0);
return emptyMatrix;
}
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
// Multiply two matrices and return the result
if (matrix1.cols != matrix2.rows)
{
// Handle dimension mismatch
Matrix emptyMatrix = createMatrix(0, 0);
return emptyMatrix;
}
} }

View File

@ -6,6 +6,12 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct
{
unsigned int rows;
unsigned int cols;
MatrixType *data;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);