127 lines
3.6 KiB
C
127 lines
3.6 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
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;
|
|
} |