initial draft of matrix.c
This commit is contained in:
1 parent
447e77a7bf
commit
bc87e13966
2 files changed
+51
-12
No files matched your search
+45
-9
@@ -1,35 +1,71 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
|
||||
if(rows && cols){
|
||||
newmatrix.cols = cols;
|
||||
newmatrix.rows = rows;
|
||||
newmatrix.buffer = malloc(rows*cols*sizeof(MatrixType));
|
||||
}
|
||||
return newmatrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(rowIdx < matrix.rows && colIdx < matrix.cols) *(matrix.buffer+rowIdx*matrix.cols+colIdx) = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(rowIdx < matrix.rows && colIdx < matrix.cols) return *(matrix.buffer+rowIdx*matrix.cols+colIdx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix newmatrix = {.cols = 0, .rows = 0, .buffer = NULL};
|
||||
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||
newmatrix.cols = matrix1.cols;
|
||||
newmatrix.rows = matrix1.rows;
|
||||
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
|
||||
//element addition
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix1.cols; j++){
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols+j);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(matrix2.cols == 1 && (matrix2.rows == matrix1.rows)) { //CAREFUL: ONLY VERTICAL VECTORS ARE IMPLEMENTED, TRANSPOSED ARE NOT YET INCLUDED, CAN LEAD TO ISSUES
|
||||
//TODO: IMPLEMENT OPTION TO GIVE VECTOR AS FIRST FUNCTION ARGUMENT
|
||||
printf("Got broadcast\n");
|
||||
newmatrix.cols = matrix1.cols;
|
||||
newmatrix.rows = matrix1.rows;
|
||||
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix1.cols; j++){
|
||||
//printf("Added %f to get %f\n", *(matrix2.buffer+i*matrix2.cols), *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols));
|
||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newmatrix;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
/*Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}*/
|
||||
+6
-3
@@ -5,15 +5,18 @@
|
||||
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct {
|
||||
u_int32_t rows;
|
||||
u_int32_t cols;
|
||||
MatrixType* buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
//Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user