89 lines
2.0 KiB
C
89 lines
2.0 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
if (rows != 0 && cols != 0)
|
|
{
|
|
Matrix matrix;
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
matrix.buffer = (float*) calloc(rows * cols, sizeof(float)); //belegt den speicherplatz mit calloc -> mit 0
|
|
return matrix;
|
|
}
|
|
else
|
|
{ //Bei einer "falschen" Matrix eine leere zurückgeben, ohne speicher zu belegen
|
|
printf("Nullgroesse der Matrix!!!\n");
|
|
Matrix matrix;
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
matrix.buffer = NULL;
|
|
return matrix;
|
|
}
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
free(matrix->buffer); //gibt den heap speicher frei
|
|
matrix->buffer = NULL; //zeiger auf NULL setzen
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
matrix.buffer[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[rowIdx * matrix.cols + colIdx];
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
//Überprüfen, ob die Matrizen die gleichen Dimensionen haben
|
|
//wenn nicht muss die matrix "rows/cols=0 und buffer = NULL" leer zurückgegeben werden
|
|
|
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
|
{
|
|
Matrix result;
|
|
result.rows = 0;
|
|
result.cols = 0;
|
|
result.buffer = NULL;
|
|
return result;
|
|
}
|
|
|
|
else
|
|
{
|
|
//Matrix result ist die neue Matrix für das Ergebnis
|
|
Matrix result;
|
|
result.rows = matrix1.rows;
|
|
result.cols = matrix1.cols;
|
|
|
|
|
|
//Addition der beiden Matrizen
|
|
for (int i = 0; i < result.rows * result.cols; i++)
|
|
{
|
|
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
|
|
} |