matrix logic

This commit is contained in:
haugnerqu103436 committed 2026-11-05 11:16:39 +01:00
1 parent 447e77a7bf
commit 3ee900784e
2 files changed
+30 -5

No files matched your search

+25 -5
View File
@@ -6,27 +6,47 @@
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int));
const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage};
return matrix;
}
void clearMatrix(Matrix *matrix)
{
matrix->rows = 0;
matrix->cols = 0;
__builtin_free(matrix->storage);
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix.storage[rowIdx][colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
return matrix.storage[rowIdx][colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
return matrix1;
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++)
{
for (unsigned int j = 0; j < matrix1.cols; j++)
{
result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j];
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
+5
View File
@@ -6,7 +6,12 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType** storage;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);