generated from freudenreichan/info2Praktikum-NeuronalesNetz
125 lines
3.1 KiB
C
125 lines
3.1 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
/*
|
|
Alte Funktion
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix m;
|
|
m.rows = rows;
|
|
m.cols = cols;
|
|
m.data = NULL;
|
|
|
|
if(rows == 0 || cols == 0){
|
|
m.rows = m.cols = 0;
|
|
return m;
|
|
}
|
|
|
|
m.data = malloc(rows * sizeof *m.data);
|
|
|
|
if(!m.data){
|
|
m.rows = m.cols = 0;
|
|
return m;
|
|
}
|
|
for(unsigned int i = 0; i < rows; i++){
|
|
m.data[i] = malloc(cols * sizeof *m.data[i]);
|
|
|
|
if(!m.data[i]){
|
|
for(unsigned int j = 0; j < i; j++){
|
|
free(m.data[j]);
|
|
}
|
|
free(m.data);
|
|
m.data = NULL;
|
|
m.rows = m.cols = 0;
|
|
return m;
|
|
}
|
|
|
|
}
|
|
return m;
|
|
}
|
|
*/
|
|
|
|
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)
|
|
{
|
|
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
//check if the matrices are able to be added (same size)
|
|
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
|
//size of the matrices should be the same, if the addition is supposed to happen
|
|
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
|
for (int i = 0; i < matrix1.rows;i++) {
|
|
for (int j = 0; j < matrix1.cols; j++) {
|
|
// I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math
|
|
// outputMatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
|
}
|
|
}
|
|
} else {
|
|
//no matrix could be added, consequence should be defined
|
|
// return NULL;
|
|
}
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
//check, if the matrices can be multiplied
|
|
if (matrix1.rows == matrix2.cols) {
|
|
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
|
for(int i = 0; i < matrix1.rows; i++) {
|
|
for (int j = 0; j < matrix2.cols; j++) {
|
|
for (int k = 0; k < matrix2.rows; k++) {
|
|
// I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math
|
|
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2[k][j];
|
|
}
|
|
}
|
|
}
|
|
return outputMatrix;
|
|
} else {
|
|
//no matrix could be added, consequence should be defined
|
|
// return NULL;
|
|
}
|
|
} |