-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (133 loc) · 4.38 KB
/
main.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
float pow(int power);
float *create_matrix(int dimension);
float *create_vector(int dimension);
float *transpose(int dimension, float *matrix);
float calculate_determinant(int dimension, float *matrix);
float *make_smaller_matrix(int n, int column, float *matrix);
float *matrix_vector_multiplication(int dimension, float *matrix, float *vector);
void show_matrix(int dimension, float *matrix);
void show_vector(int dimension, float *vector);
void first_task();
void second_task();
void third_task();
int main() {
third_task();
return 0;
}
void first_task() {
int dimension = 2;
float *matrix = create_matrix(dimension);
float determinant = calculate_determinant(dimension, matrix);
printf("%.2f", determinant);
}
void second_task() {
int dimension = 3;
float *matrix = create_matrix(dimension);
float determinant = calculate_determinant(dimension, matrix);
printf("%.2f", determinant);
}
void third_task() {
float *vectors = create_matrix(3);
float *basis_vectors = transpose(3, vectors);
free(vectors);
int amount_of_vectors;
scanf("%d", &amount_of_vectors);
float determinant = calculate_determinant(3, basis_vectors);
float *vector;
for (int i = 0; i < amount_of_vectors; i++) {
vector = create_vector(3);
if (determinant == 0) continue;
vector = matrix_vector_multiplication(3, basis_vectors, vector);
show_vector(3, vector);
free(vector);
}
free(basis_vectors);
}
float *matrix_vector_multiplication(int dimension, float *matrix, float *vector) {
float *resulting_vector = malloc(dimension * sizeof(float));
float value;
for (int i = 0; i < dimension; i++) {
value = 0;
for (int j = 0; j < dimension; j++) {
value += *(matrix + dimension * i + j) * *(vector + j);
}
*(resulting_vector + i) = value;
}
return resulting_vector;
}
float calculate_determinant(int dimension, float *matrix) {
if (dimension == 1) {
return *(matrix);
}
float determinant = 0;
for (int i = 0; i < dimension; i++) {
float *smaller_matrix = make_smaller_matrix(dimension, i, matrix);
float minor = calculate_determinant(dimension - 1, smaller_matrix);
determinant += *(matrix + i) * pow(i) * minor;
free(smaller_matrix);
}
return determinant;
}
float *create_matrix(int dimension) {
float *matrix = malloc(dimension * dimension * sizeof(float));
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
scanf("%f", matrix + i * dimension + j);
}
}
return matrix;
}
float *create_vector(int dimension) {
float *vector = malloc(dimension * sizeof(float));
for (int i = 0; i < dimension; i++) {
scanf("%f", vector + i);
}
return vector;
}
float *transpose(int dimension, float *matrix) {
float *transposed_matrix = malloc(dimension * dimension * sizeof(float));
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
*(transposed_matrix + dimension * i + j) = *(matrix + dimension * j + i);
}
}
return transposed_matrix;
}
float *make_smaller_matrix(int n, int column, float *matrix) {
int dimension = n - 1;
float *temporary_matrix = malloc(dimension * dimension * sizeof(float));
for (int i = 0; i < dimension; i++) {
for (int j = 0, columns_in_old_matrix = 0; j < dimension; j++, columns_in_old_matrix++) {
if (j == column) {
columns_in_old_matrix++;
}
*(temporary_matrix + i * dimension + j) = *(matrix + (i + 1) * n + columns_in_old_matrix);
}
}
return temporary_matrix;
}
void show_matrix(int dimension, float *matrix) {
printf("-----------------------------------\n");
printf("%dx%d matrix\n", dimension, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
printf("%.2f ", *(matrix + i * dimension + j));
}
printf("\n");
}
printf("-----------------------------------\n");
}
void show_vector(int dimension, float *vector) {
for (int i = 0; i < dimension; i++) {
printf("%f ", *(vector + i));
}
}
float pow(int power) {
float result = 1;
for (int i = 0; i < power; i++) {
result *= -1;
}
return result;
}