Compare commits

...
19 Commits
Author SHA1 Message Date
wolfni101342 c1086b6be5 bugfixes und Formatierung (in neuem Zweig) 2025-11-13 23:10:26 +01:00
wolfni101342 542d94eac8 Fehler aus Vorlesung verbessert (in Uni) 2025-11-13 22:04:44 +01:00
AD005\z004z3ez d5466ae1fd Verbesserungen nach Informatik-Stunde 2025-11-13 11:13:40 +01:00
AD005\z004z3ez b7de923066 Kommentare hinzugefügt 2025-11-12 20:06:57 +01:00
AD005\z0046beb 09ada4e4d0 unittest fail gefixt ( Matrix fails out of range bei getMatrixAt ) 2025-11-12 11:51:42 +01:00
AD005\z0046beb 4aee809756 bug fix in create matrix 2025-11-12 11:44:26 +01:00
AD005\z0046beb b661866a16 bug fixes (data in buffer unbenannt) 2025-11-12 11:36:28 +01:00
AD005\z0046beb 5b32e1475c ein #include und exit failiure bei matrixMultiply hinzugefügt 2025-11-12 11:15:13 +01:00
AD005\z0046beb c96311347e matrixMultiply erstellt 2025-11-12 11:09:41 +01:00
AD005\z004z3ez f3b964f17e add_matrix Funktion erstellt 2025-11-11 18:12:20 +01:00
AD005\z004z3ez 782bcdc350 Merge branch 'get_matrix' 2025-11-11 16:11:10 +01:00
AD005\z004z3ez a1c582fcff Merge branch 'set_matrix' 2025-11-11 16:10:03 +01:00
AD005\z004z3ez bf27920a61 get_matrix Funktion erstellt 2025-11-11 16:09:42 +01:00
wolfni101342 6885e104eb Merge branch 'create-matrix' 2025-11-10 15:51:18 +01:00
wolfni101342 d15f3d1b9c Merge branch 'clear-matrix' 2025-11-10 15:50:30 +01:00
wolfni101342 84fb7219fb clear matrix 2025-11-10 15:47:48 +01:00
wolfni101342 e2dbb0952f clear matrix func wip 2025-11-10 15:43:00 +01:00
wolfni101342 3a72794684 create matrix update wip 2025-11-10 15:33:39 +01:00
wolfni101342 f0bc0ce06a create matrix func wip 2025-11-10 15:13:32 +01:00
2 changed files with 239 additions and 10 deletions
+238 -9
View File
@@ -1,36 +1,265 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "matrix.h" #include "matrix.h"
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
enum addModes{SAMEDIMENSIONS, COLVEC, ROWVEC};
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m;
m.rows = rows;
m.cols = cols;
m.buffer = NULL;
if(rows == 0 || cols == 0)
{
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
m.buffer = calloc(rows * cols, sizeof(MatrixType));
if (m.buffer == NULL)
{
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->buffer == NULL)
{
matrix->rows = 0;
matrix->cols = 0;
matrix->buffer = NULL;
return;
}
else
{
free(matrix->buffer);
matrix->rows = 0;
matrix->cols = 0;
matrix->buffer = NULL;
}
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
matrix.data[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value {
return;
}
else
{
matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
}
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
return UNDEFINED_MATRIX_VALUE;
}
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
} }
static int get_add_mode(Matrix matrix1, Matrix matrix2)
{
int get_add_mode = -1;
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
{
get_add_mode = SAMEDIMENSIONS;
}
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows)
{
get_add_mode = COLVEC;
}
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows)
{
get_add_mode = COLVEC;
}
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols)
{
get_add_mode = ROWVEC;
}
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols)
{
get_add_mode = ROWVEC;
}
return get_add_mode;
}
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
{
Matrix matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
{
matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
}
return matrix_erg;
}
Matrix addColVec(Matrix matrix1, Matrix matrix2)
{
Matrix matrix_erg;
if(matrix1.cols == 1)
{
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
for(int i = 0; i < matrix2.rows; i++)
{
for(int j = 0; j < matrix2.cols; j++)
{
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
if(matrix2.cols == 1)
{
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
for(int i = 0; i < matrix1.rows; i++)
{
for(int j = 0; j < matrix1.cols; j++)
{
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
}
}
}
return matrix_erg;
}
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
{
Matrix matrix_erg;
if(matrix1.rows == 1)
{
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
for(int i = 0; i < matrix2.rows; i++)
{
for(int j = 0; j < matrix2.cols; j++)
{
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[j] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
if(matrix2.rows == 1)
{
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
for(int i = 0; i < matrix1.rows; i++)
{
for(int j = 0; j < matrix1.cols; j++)
{
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[j];
}
}
}
return matrix_erg;
}
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
int ok = get_add_mode(matrix1,matrix2);
Matrix matrix_erg = createMatrix(0, 0);
switch(ok)
{
case SAMEDIMENSIONS:
matrix_erg = addSameDim(matrix1, matrix2);
break;
case COLVEC:
matrix_erg = addColVec(matrix1, matrix2);
break;
case ROWVEC:
matrix_erg = addRowVec(matrix1, matrix2);
break;
}
return matrix_erg;
} }
static int can_multiply (Matrix matrix1, Matrix matrix2)
{
int can_multiply = 0;
if(matrix1.cols == matrix2.rows)
{
can_multiply = 1;
}
return can_multiply;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
int ok = can_multiply(matrix1,matrix2);
} unsigned int erg_rows = matrix1.rows;
unsigned int erg_cols = matrix2.cols;
Matrix matrix_erg = createMatrix(0, 0);
if (ok == 1)
{
matrix_erg = createMatrix(erg_rows, erg_cols);
for (int i = 0; i < erg_rows; i++)
{
for (int j = 0; j < erg_cols; j++)
{
MatrixType sum = 0;
for (int k = 0; k < matrix1.cols; k++)
{
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, matrix_erg, i, j);
}
}
}
return matrix_erg;
}
+1 -1
View File
@@ -11,7 +11,7 @@ typedef struct
{ {
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType *data; MatrixType *buffer;
}Matrix; }Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);