137 lines
3.1 KiB
C
137 lines
3.1 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
// typedef struct
|
|
// {
|
|
// unsigned int *buffer;
|
|
// unsigned int rows;
|
|
// unsigned int cols;
|
|
// } Matrix;
|
|
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
if(rows == 0 || cols == 0)
|
|
{
|
|
Matrix noMatrix = {NULL, 0, 0};
|
|
|
|
return noMatrix;
|
|
}
|
|
|
|
Matrix matrix = {(MatrixType *)calloc(rows * cols, sizeof(int)) ,rows, cols};
|
|
|
|
return matrix;
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
free(matrix->buffer);
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
matrix->buffer = NULL;
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if(rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
|
{
|
|
return;
|
|
}
|
|
|
|
matrix.buffer[matrix.cols * rowIdx + colIdx] = value;
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if(rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
|
{
|
|
return UNDEFINED_MATRIX_VALUE;
|
|
}
|
|
|
|
MatrixType matrixAt = matrix.buffer[matrix.cols * rowIdx + colIdx];
|
|
|
|
return matrixAt;
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
|
{
|
|
Matrix matrixSum = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
for(int i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix1.cols; j++)
|
|
{
|
|
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
|
|
|
setMatrixAt(value, matrixSum, i, j);
|
|
}
|
|
|
|
}
|
|
return matrixSum;
|
|
}
|
|
|
|
if((matrix1.cols == 1 || matrix2.cols == 1) && matrix1.rows == matrix2.rows)
|
|
{
|
|
if(matrix1.cols == 1)
|
|
{
|
|
return broadcasting(matrix2, matrix1);
|
|
}
|
|
else
|
|
{
|
|
return broadcasting(matrix1, matrix2);
|
|
}
|
|
}
|
|
|
|
Matrix failedMatrix = createMatrix(0, 0);
|
|
|
|
return failedMatrix;
|
|
}
|
|
|
|
Matrix broadcasting(const Matrix matrix, const Matrix vector)
|
|
{
|
|
Matrix matrixSum = createMatrix(matrix.rows, matrix.cols);
|
|
|
|
for(int i = 0; i < vector.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrix.cols; j++)
|
|
{
|
|
MatrixType value = getMatrixAt(matrix, i, j) + getMatrixAt(vector, i, 0);
|
|
|
|
setMatrixAt(value, matrixSum, i, j);
|
|
}
|
|
}
|
|
return matrixSum;
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if(matrix1.cols != matrix2.rows)
|
|
{
|
|
Matrix failedMatrix = createMatrix(0, 0);
|
|
|
|
return failedMatrix;
|
|
}
|
|
|
|
Matrix matrixRes = createMatrix(matrix1.rows, matrix2.cols);
|
|
|
|
for(int i = 0; i < matrixRes.rows; i++)
|
|
{
|
|
for(int j = 0; j < matrixRes.cols; j++)
|
|
{
|
|
MatrixType sum = 0;
|
|
|
|
for(int k = 0; k < matrix1.cols; k++)
|
|
{
|
|
MatrixType product = getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
|
|
|
sum += product;
|
|
}
|
|
setMatrixAt(sum, matrixRes, i, j);
|
|
}
|
|
}
|
|
return matrixRes;
|
|
} |