2025-11-26 18:37:26 +01:00

150 lines
4.6 KiB
C

#include <stdlib.h>
#include <string.h>
#include "matrix.h"
#include <stdbool.h>
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(size_t rows, size_t cols)
{
Matrix m;
m.rows = rows;
m.cols = cols;
m.buffer = NULL;
if(rows == 0 || cols == 0){
m.rows = m.cols = 0;
return m;
}
// Single allocation for entire matrix
m.buffer = malloc(rows * cols * sizeof(MatrixType));
if(!m.buffer){
m.rows = m.cols = 0;
return m;
}
// Initialize (optional)
for(unsigned int i = 0; i < rows * cols; i++){
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
return m;
}
void clearMatrix(Matrix *matrix)
{
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols;j++) {
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE;
}
}
free(matrix->buffer);
matrix->rows = 0;
matrix->cols = 0;
matrix->buffer = NULL;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
//checks, if the given incies are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
matrix.buffer[colIdx + matrix.cols * rowIdx] = value;
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
//checks, if the given indices are allowed
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
MatrixType returnVal;
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
// returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols];
return returnVal;
}
else return 0;
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
bool doBroadcast = false;
Matrix larger, smaller;
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
larger = matrix1;
smaller = matrix2;
}
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
{
larger = matrix1;
smaller = matrix2;
doBroadcast = true;
}
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
larger = matrix2;
smaller = matrix1;
doBroadcast = true;
}
else{
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
if(doBroadcast){
for(int i = 0; i < outputMatrix.rows; i++){
MatrixType broadcastValue = smaller.buffer[i];
for(int j = 0; j < outputMatrix.cols; j++){
outputMatrix.buffer[i * outputMatrix.cols + j] = larger.buffer[i * larger.cols + j] + broadcastValue;
}
}
} else{
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
}
return outputMatrix;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if(matrix1.cols != matrix2.rows){
Matrix m = {NULL, 0, 0};
return m;
}
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
for(int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.cols; j++) {
for (int k = 0; k < matrix1.cols; k++) {
// how this should work in normal Matrix version:
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
//outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
}
}
}
return outputMatrix;
}