#include #include #include #include "matrixOp.h" Matrix* CreateMatrixZero(int rows, int cols) { Matrix *m = malloc(sizeof(Matrix)); m->rows = rows; m->cols = cols; m->data = malloc(rows * sizeof(double*)); for (int i = 0; i < rows; i++) { m->data[i] = calloc(cols, sizeof(double)); } return m; } Matrix* CreateMatrixRand(int rows, int cols) { srand(time(NULL)); Matrix *m = CreateMatrixZero(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { m->data[i][j] = (rand() % 200 - 100) / 1.0; // -100 bis 99 } } return m; } Matrix* CreateMatrix(int rows, int cols, double startValue) { Matrix *m = CreateMatrixZero(rows, cols); double val = startValue; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { m->data[i][j] = val++; } } return m; } void FreeMatrix(Matrix *m) { for (int i = 0; i < m->rows; i++) { free(m->data[i]); } free(m->data); free(m); } void PrintMatrix(Matrix *m) { for (int i = 0; i < m->rows; i++) { for (int j = 0; j < m->cols; j++) { printf("%6.2f ", m->data[i][j]); } printf("\n"); } } Matrix* AddMatrix(Matrix *a, Matrix *b) { if (a->rows != b->rows || a->cols != b->cols) return NULL; Matrix *m = CreateMatrixZero(a->rows, a->cols); for (int i = 0; i < m->rows; i++) { for (int j = 0; j < m->cols; j++) { m->data[i][j] = a->data[i][j] + b->data[i][j]; } } return m; } Matrix* SubMatrix(Matrix *a, Matrix *b) { if (a->rows != b->rows || a->cols != b->cols) return NULL; Matrix *m = CreateMatrixZero(a->rows, a->cols); for (int i = 0; i < m->rows; i++) { for (int j = 0; j < m->cols; j++) { m->data[i][j] = a->data[i][j] - b->data[i][j]; } } return m; } Matrix* TransposeMatrix(Matrix *a) { Matrix *m = CreateMatrixZero(a->cols, a->rows); for (int i = 0; i < a->rows; i++) { for (int j = 0; j < a->cols; j++) { m->data[j][i] = a->data[i][j]; } } return m; } Matrix* MulMatrix(Matrix *a, Matrix *b) { if (a->cols != b->rows) return NULL; Matrix *m = CreateMatrixZero(a->rows, b->cols); for (int i = 0; i < a->rows; i++) { for (int j = 0; j < b->cols; j++) { for (int k = 0; k < a->cols; k++) { m->data[i][j] += a->data[i][k] * b->data[k][j]; } } } return m; } double DetMatrix(Matrix *m) { if (m->rows != m->cols) return 0; if (m->rows == 2) { return m->data[0][0] * m->data[1][1] - m->data[0][1] * m->data[1][0]; } // Optional: Erweiterung für n > 2 return 0; }