145 lines
4.2 KiB
C
145 lines
4.2 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) //Values in matrix schreiben
|
|
{
|
|
matrix.buffer[(size_t)rowIdx * matrix.cols + colIdx] = value;
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) //aus matrix auslesen
|
|
{
|
|
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;
|
|
}
|
|
|
|
//Boradcasting Fall bei neuronlen Netzwerken
|
|
|
|
// Case B: matrix1 besteht aus (rows x cols) und matrix2 ist (rows x 1) -> einmal alle rows einzeln auf die andere Addieren
|
|
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;
|
|
}
|
|
|
|
//Broadcasting Fall
|
|
|
|
// Case C: matrix1 ist (rows x 1) und matrix2 ist (rows x cols) -> einmal alle cols einzeln auf die andere Addieren
|
|
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) //Spalten und Zeilen sind nicht gleich groß
|
|
{
|
|
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;
|
|
}
|
|
} |