142 lines
4.0 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "matrix.h"
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
if (rows != 0 && cols != 0)
{
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = (MatrixType*) calloc((size_t)rows * cols, sizeof(MatrixType));
return matrix;
}
else
{
Matrix matrix;
matrix.rows = 0;
matrix.cols = 0;
matrix.buffer = NULL;
return matrix;
}
}
void clearMatrix(Matrix *matrix)
{
if (matrix != NULL)
{
if (matrix->buffer != 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)
{
matrix.buffer[(size_t)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[(size_t)rowIdx * matrix.cols + colIdx];
}else{
return 0;
}
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
// Case A: same shape -> elementwise add
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) return result;
size_t n = (size_t)result.rows * result.cols;
for (size_t i = 0; i < n; i++)
{
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
}
return result;
}
// Case B: matrix1 has shape (rows x cols) and matrix2 is (rows x 1) -> broadcast second across columns
if (matrix1.rows == matrix2.rows && matrix2.cols == 1 && matrix1.cols > 1)
{
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < matrix1.rows; r++)
{
MatrixType b = matrix2.buffer[(size_t)r * matrix2.cols + 0];
for (unsigned int c = 0; c < matrix1.cols; c++)
{
result.buffer[(size_t)r * result.cols + c] = matrix1.buffer[(size_t)r * matrix1.cols + c] + b;
}
}
return result;
}
// Case C: matrix1 is (rows x 1) and matrix2 is (rows x cols) -> broadcast first across columns
if (matrix2.rows == matrix1.rows && matrix1.cols == 1 && matrix2.cols > 1)
{
Matrix result = createMatrix(matrix2.rows, matrix2.cols);
if (result.buffer == NULL) return result;
for (unsigned int r = 0; r < matrix2.rows; r++)
{
MatrixType b = matrix1.buffer[(size_t)r * matrix1.cols + 0];
for (unsigned int c = 0; c < matrix2.cols; c++)
{
result.buffer[(size_t)r * result.cols + c] = matrix2.buffer[(size_t)r * matrix2.cols + c] + b;
}
}
return result;
}
// unsupported shapes -> return empty matrix
Matrix result = {0};
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows)
{
Matrix result;
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
else
{
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
if (result.buffer == NULL) return result;
for (unsigned int i = 0; i < result.rows; i++)
{
for (unsigned int j = 0; j < result.cols; j++)
{
MatrixType summe = 0;
for (unsigned int k = 0; k < matrix1.cols; k++)
{
summe += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(summe, result, i, j);
}
}
return result;
}
}