generated from freudenreichan/info2Praktikum-NeuronalesNetz
121 lines
3.2 KiB
C
121 lines
3.2 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix matrix = {NULL,rows,cols};
|
|
if(rows != 0 && cols != 0){
|
|
matrix.buffer = calloc(rows * cols, sizeof(MatrixType));
|
|
if(matrix.buffer == NULL){
|
|
matrix.rows = 0;
|
|
matrix.cols = 0; // alternativ Programm beenden mit exit -> so einfach dimensionslos
|
|
}
|
|
}else{
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
free(matrix->buffer);
|
|
matrix->buffer = NULL;
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if(matrix.rows > rowIdx && matrix.cols > colIdx){
|
|
matrix.buffer[colIdx + rowIdx * matrix.cols] = value;
|
|
}
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if(matrix.rows > rowIdx && matrix.cols > colIdx){
|
|
return matrix.buffer[colIdx + rowIdx * matrix.cols];
|
|
}else{
|
|
return UNDEFINED_MATRIX_VALUE;
|
|
}
|
|
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
|
|
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
|
Matrix resultMatrix = createMatrix(matrix1.rows,matrix1.cols);
|
|
for(int i = 0; i < matrix1.rows * matrix1.cols; i++){
|
|
resultMatrix.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
|
}
|
|
return resultMatrix;
|
|
}else if(matrix1.rows == matrix2.rows && matrix1.cols == 1){ // Matrix1 ist Vektor
|
|
Matrix resultMatrix = createMatrix(matrix1.rows,matrix2.cols);
|
|
for(int i = 0; i < matrix2.cols; i++){
|
|
for(int j = 0; j < matrix1.rows; j++){
|
|
resultMatrix.buffer[i + j * matrix2.cols]= matrix2.buffer[i + j * matrix2.cols] + matrix1.buffer[j];
|
|
}
|
|
}
|
|
return resultMatrix;
|
|
}else if(matrix1.rows == matrix2.rows && matrix2.cols == 1){ // Matrix ist Vektor
|
|
Matrix resultMatrix = createMatrix(matrix1.rows,matrix1.cols);
|
|
for(int i = 0; i < matrix1.cols; i++){
|
|
for(int j = 0; j < matrix1.rows; j++){
|
|
resultMatrix.buffer[i + j * matrix1.cols]= matrix1.buffer[i + j * matrix1.cols] + matrix2.buffer[j];
|
|
}
|
|
}
|
|
return resultMatrix;
|
|
}else{
|
|
Matrix emptyMatrix = {NULL,0,0};
|
|
return emptyMatrix;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
Matrix resultMatrix = createMatrix(matrix1.rows,matrix2.cols);
|
|
|
|
if(matrix1.cols == matrix2.rows){
|
|
unsigned int commonDimension = matrix1.cols;
|
|
|
|
for(int i = 0; i < resultMatrix.rows; i++){
|
|
|
|
for(int j = 0; j < resultMatrix.cols; j++){
|
|
MatrixType entry = 0;
|
|
|
|
for(int k = 0; k < commonDimension; k++){
|
|
entry += (matrix1.buffer[k + i * matrix1.cols] * matrix2.buffer[j + k * matrix2.cols]);
|
|
}
|
|
|
|
resultMatrix.buffer[j + i * resultMatrix.cols] = entry;
|
|
}
|
|
}
|
|
|
|
return resultMatrix;
|
|
}else{
|
|
clearMatrix(&resultMatrix);
|
|
return resultMatrix;
|
|
}
|
|
|
|
|
|
|
|
|
|
} |