84 lines
1.9 KiB
C
84 lines
1.9 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix matrix;
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
if(rows == 0 || cols == 0){
|
|
matrix.werte = NULL;
|
|
return matrix;
|
|
|
|
}
|
|
matrix.werte = malloc(rows * cols * sizeof(MatrixType));
|
|
return matrix;
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
//speicher freimachen:
|
|
free(matrix->werte);
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
matrix.werte[rowIdx * matrix.cols + colIdx] = value;
|
|
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
|
|
return matrix.werte[rowIdx * matrix.cols + colIdx];
|
|
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols){
|
|
//nicht sicher
|
|
|
|
|
|
}
|
|
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
for(int reihe = 0; reihe < matrix1.rows; reihe++){
|
|
for(int spalte = 0; spalte < matrix1.cols; spalte++){
|
|
MatrixType wertM1 = getMatrixAt(matrix1, reihe, spalte);
|
|
MatrixType wertM2 = getMatrixAt(matrix2, reihe, spalte);
|
|
setMatrixAt(wertM1 + wertM2, ergebnisMatrix, reihe, spalte);
|
|
|
|
}
|
|
}
|
|
return ergebnisMatrix;
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
|
for(int i = 0; i < matrix1.rows; i++){
|
|
for(int j = 0; j < matrix2.cols; j++){
|
|
MatrixType summe = 0;
|
|
|
|
for(int l = 0; l < matrix1.cols; l++){
|
|
summe += getMatrixAt( matrix1, i, l) * getMatrixAt(matrix2, l, j);
|
|
|
|
|
|
|
|
}
|
|
setMatrixAt(summe, ergebnisMatrix, i, j);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
return ergebnisMatrix;
|
|
|
|
} |