forked from freudenreichan/info2Praktikum-NeuronalesNetz
72 lines
1.8 KiB
C
72 lines
1.8 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
// Allocate memory for the matrix structure
|
|
Matrix matrix;
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
|
if (matrix.data == NULL)
|
|
{
|
|
// Handle memory allocation failure
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
}
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
// Free the allocated memory for the matrix data
|
|
if (matrix->data != NULL)
|
|
{
|
|
free(matrix->data);
|
|
matrix->data = NULL;
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
// Set the value at the specified row and column index
|
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
{
|
|
matrix.data[rowIdx * matrix.cols + colIdx] = value;
|
|
}
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
// Get the value at the specified row and column index
|
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
{
|
|
return matrix.data[rowIdx * matrix.cols + colIdx];
|
|
}
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
// Add two matrices and return the result
|
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
|
{
|
|
// Handle dimension mismatch
|
|
Matrix emptyMatrix = createMatrix(0, 0);
|
|
return emptyMatrix;
|
|
}
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
// Multiply two matrices and return the result
|
|
if (matrix1.cols != matrix2.rows)
|
|
{
|
|
// Handle dimension mismatch
|
|
Matrix emptyMatrix = createMatrix(0, 0);
|
|
return emptyMatrix;
|
|
}
|
|
} |