-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_operations.c
More file actions
141 lines (119 loc) · 4.29 KB
/
matrix_operations.c
File metadata and controls
141 lines (119 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#include "matrix_operations.h"
void create_matrix(Matrix* matrix, int dimension) {
matrix->dimension = dimension;
matrix->matrix = malloc(dimension * dimension * sizeof(int));
}
void populate_matrix(Matrix* matrix) {
int value;
for (int row = 0; row < matrix->dimension; row++) {
for (int col = 0; col < matrix->dimension; col++) {
printf("Please input value for row %d, col %d\n", row, col);
scanf("%d", &value);
int index = row * matrix->dimension + col;
matrix->matrix[index] = value;
}
}
}
void find_max_width(const Matrix* matrix, int* max_width) {
for (int i = 0; i < matrix->dimension * matrix->dimension; i++) {
int num = matrix->matrix[i];
int width = 0;
while (num != 0) {
num /= 10;
width++;
}
if (width > *max_width) {
*max_width = width;
}
}
}
void print_matrix_row(const Matrix* matrix, int row, int max_width) {
for (int col = 0; col < matrix->dimension; col++) {
int index = row * matrix->dimension + col;
printf("%*d ", max_width, matrix->matrix[index]);
}
printf("\n");
}
void display_matrix(const Matrix* matrix) {
int max_width = 0;
find_max_width(matrix, &max_width);
for (int row = 0; row < matrix->dimension; row++) {
print_matrix_row(matrix, row, max_width);
}
}
void matrix_addition(const Matrix* matrix_one, const Matrix* matrix_two) {
Matrix result_matrix;
create_matrix(&result_matrix, matrix_one->dimension);
for (int i = 0; i < matrix_one->dimension * matrix_one->dimension; i++) {
result_matrix.matrix[i] = matrix_one->matrix[i] + matrix_two->matrix[i];
}
display_matrix(&result_matrix);
}
void matrix_transpose(const Matrix* matrix, Matrix* result_matrix) {
create_matrix(result_matrix, matrix->dimension);
for (int row = 0; row < matrix->dimension; row++) {
for (int col = 0; col < matrix->dimension; col++) {
int original_index = row * matrix->dimension + col;
int transpose_index = col * matrix->dimension + row;
result_matrix->matrix[transpose_index] = matrix->matrix[original_index];
}
}
}
void matrix_multiplication(const Matrix* matrix_one, const Matrix* matrix_two) {
Matrix result_matrix;
create_matrix(&result_matrix, matrix_one->dimension);
Matrix matrix_two_T;
matrix_transpose(matrix_two, &matrix_two_T);
for (int row = 0; row < matrix_one->dimension; row++) {
for (int col = 0; col < matrix_two_T.dimension; col++) {
int value = 0;
for (int k = 0; k < matrix_one->dimension; k++) {
int index1 = row * matrix_one->dimension + k;
int index2 = col * matrix_two_T.dimension + k;
value += matrix_one->matrix[index1] * matrix_two_T.matrix[index2];
}
int result_index = row * result_matrix.dimension + col;
result_matrix.matrix[result_index] = value;
}
}
display_matrix(&result_matrix);
}
int find_determinant(const Matrix* matrix) {
if (matrix->dimension == 1) {
return matrix->matrix[0];
}
int det = 0;
int sign = 1;
for (int j = 0; j < matrix->dimension; j++) {
int cofactor = sign * matrix->matrix[j] * determinant_of_minor(matrix, 0, j);
det += cofactor;
sign *= -1;
}
return det;
}
int determinant_of_minor(const Matrix* matrix, int row, int col) {
Matrix minor_matrix;
create_matrix(&minor_matrix, matrix->dimension - 1);
int minor_row = 0;
for (int i = 0; i < matrix->dimension; i++) {
if (i != row) {
int minor_col = 0;
for (int j = 0; j < matrix->dimension; j++) {
if (j != col) {
int minor_index = minor_row * (matrix->dimension - 1) + minor_col;
int original_index = i * matrix->dimension + j;
minor_matrix.matrix[minor_index] = matrix->matrix[original_index];
minor_col++;
}
}
minor_row++;
}
}
int minor_determinant = find_determinant(&minor_matrix);
return minor_determinant;
}
void free_matrix(Matrix* matrix) {
free(matrix->matrix);
}