push
This commit is contained in:
1 parent
f9334f8b62
commit
f36a7c3948
1 file changed
+100
-8
+100
-8
@@ -2,34 +2,126 @@
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix matrix = {0, 0, NULL};
|
||||
|
||||
if (rows == 0 || cols == 0) {
|
||||
return matrix;
|
||||
}
|
||||
|
||||
matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
|
||||
if (matrix.buffer != NULL) {
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
if (matrix != NULL) {
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (matrix.buffer != NULL && 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 (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols) {
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix result = {0, 0, NULL};
|
||||
|
||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Elementweise Addition
|
||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (unsigned int r = 0; r < result.rows; r++) {
|
||||
for (unsigned int c = 0; c < result.cols; c++) {
|
||||
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c);
|
||||
setMatrixAt(sum, result, r, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Broadcasting: matrix2 ist ein Spaltenvektor
|
||||
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
|
||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (unsigned int r = 0; r < result.rows; r++) {
|
||||
MatrixType vecVal = getMatrixAt(matrix2, r, 0);
|
||||
for (unsigned int c = 0; c < result.cols; c++) {
|
||||
MatrixType sum = getMatrixAt(matrix1, r, c) + vecVal;
|
||||
setMatrixAt(sum, result, r, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Broadcasting: matrix1 ist ein Spaltenvektor
|
||||
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1) {
|
||||
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (unsigned int r = 0; r < result.rows; r++) {
|
||||
MatrixType vecVal = getMatrixAt(matrix1, r, 0);
|
||||
for (unsigned int c = 0; c < result.cols; c++) {
|
||||
MatrixType sum = vecVal + getMatrixAt(matrix2, r, c);
|
||||
setMatrixAt(sum, result, r, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix result = {0, 0, NULL};
|
||||
|
||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if (result.buffer == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (unsigned int r = 0; r < result.rows; r++) {
|
||||
for (unsigned int c = 0; c < result.cols; c++) {
|
||||
MatrixType sum = 0;
|
||||
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||
sum += getMatrixAt(matrix1, r, k) * getMatrixAt(matrix2, k, c);
|
||||
}
|
||||
setMatrixAt(sum, result, r, c);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user