Compare commits
No commits in common. "43edb7de8180560b64931045c8c9550a9a0c8700" and "fa52898db62ff8aaec834fa8a0c551d5c74da8b7" have entirely different histories.
43edb7de81
...
fa52898db6
93
matrix.c
93
matrix.c
@ -6,113 +6,30 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
if (rows ==0 || cols ==0){ // check if dimensions are valid
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if invalid
|
||||
return empty;
|
||||
}
|
||||
MatrixType *buffer = malloc(rows * cols* sizeof(MatrixType)); //allocate memory
|
||||
if (buffer == NULL){ //check if memory allocation succeeded
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if out of memory
|
||||
return empty;
|
||||
}
|
||||
Matrix result ={.rows = rows, .cols = cols, .buffer = buffer}; // create and return matrix
|
||||
return result;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{ free(matrix-> buffer); // free the buffer memory
|
||||
matrix -> buffer = NULL; // set all members to NUll
|
||||
matrix -> rows =0;
|
||||
matrix -> cols =0;
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{ if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ // check if indices are valid
|
||||
return; // do nothing if out of bounds
|
||||
}
|
||||
unsigned int index = rowIdx * matrix.cols + colIdx; // calculate index using row-major formula
|
||||
matrix.buffer[index] = value ; // set value at that index
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{ if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ //check if indices are valid
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
unsigned int index = rowIdx * matrix.cols + colIdx; //calculate index using row-major formula
|
||||
return matrix.buffer [index]; // return the value at that index
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{ if (matrix1.rows != matrix2.rows){ // check row compatibility
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer = NULL};
|
||||
return empty;
|
||||
}
|
||||
if (matrix1.cols!= matrix2.cols && matrix2.cols !=1 && matrix1.cols != 1){ // check column compatibility (allow broadcasting )
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer =NULL};
|
||||
return empty;
|
||||
}
|
||||
unsigned int resultRows = matrix1.rows; //determine result dimensions
|
||||
unsigned int resultCols = (matrix1.cols > matrix2.cols) ? matrix1.cols: matrix2.cols;
|
||||
//Allocate memory for the result buffer
|
||||
MatrixType *resultBuffer = malloc(resultRows * resultCols * sizeof(MatrixType));
|
||||
if (resultBuffer == NULL){
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer=NULL};
|
||||
return empty;
|
||||
}
|
||||
// add elements element-wise with broadcasting
|
||||
for (unsigned int row =0; row < resultRows; ++row){
|
||||
for (unsigned int col =0; col < resultCols; ++col){
|
||||
//Handle broadcasting: if matrix2 has 1 column, use column 0
|
||||
unsigned int col1 = (matrix1.cols ==1)? 0 : col;
|
||||
MatrixType val1 = getMatrixAt (matrix1, row, col1);
|
||||
unsigned int col2 = (matrix2.cols==1) ? 0 : col;
|
||||
MatrixType val2 = getMatrixAt(matrix2, row, col2);
|
||||
|
||||
unsigned int resultIndex = row * resultCols + col;
|
||||
resultBuffer[resultIndex] = val1 +val2;
|
||||
}
|
||||
}
|
||||
Matrix result = {.rows = resultRows, .cols = resultCols, .buffer= resultBuffer};
|
||||
return result; //create and return result
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{ if (matrix1.cols != matrix2.rows){ // check compatibility
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer =NULL};
|
||||
return empty;
|
||||
}
|
||||
unsigned int resultRows = matrix1.rows; // determine result dimensions
|
||||
unsigned int resultCols = matrix2.cols;
|
||||
|
||||
MatrixType *resultBuffer = malloc( resultRows * resultCols * sizeof(MatrixType)); // allocate memory
|
||||
if (resultBuffer == NULL){
|
||||
Matrix empty = {.rows =0, .cols=0, .buffer= NULL};
|
||||
return empty;
|
||||
}
|
||||
for (unsigned int i =0; i < resultRows; ++i){
|
||||
for (unsigned int j =0; j< resultCols; ++j){
|
||||
MatrixType sum = 0.0;
|
||||
for (unsigned int k =0; k< matrix1.cols; ++k){
|
||||
MatrixType val1 =getMatrixAt(matrix1, i,k);
|
||||
MatrixType val2 = getMatrixAt(matrix2, k,j);
|
||||
sum += val1 * val2;
|
||||
}
|
||||
|
||||
|
||||
unsigned int resultIndex = i * resultCols + j;
|
||||
resultBuffer [resultIndex] = sum;
|
||||
}
|
||||
}
|
||||
Matrix result = {.rows = resultRows, .cols = resultCols, .buffer =resultBuffer};
|
||||
return result;
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
5
matrix.h
5
matrix.h
@ -6,11 +6,6 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
typedef struct{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer; //pointer to dynamically allocate array
|
||||
} Matrix;
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
|
||||
BIN
runMatrixTests
BIN
runMatrixTests
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user