/
Matrix.cpp
241 lines (205 loc) · 5.66 KB
/
Matrix.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <assert.h>
#include "Matrix.h"
#define idx(i, cols, j) ((i) ? (i*cols + j) : j)
typedef struct {
TYPE* data;
int cols;
int rows;
} matrix_t;
void ShowMatrix(MatHandler mat){
matrix_t *m = (matrix_t *)mat;
if (!m){
printf("Matrix is NULL\n\n");
return;
}
printf("Matrix:\nCols: %d; Rows: %d;\n", m->cols, m->rows);
for (int i = 0; i < m->rows; ++i){
for (int j = 0; j < m->cols; ++j){
printf("[%d]%lf\t", idx(j, m->cols, i), m->data[idx(j, m->cols, i)]);
}
printf("\n");
}
printf("\n");
}
MatHandler CreateMat(int cols, int rows, TYPE *nums){
matrix_t *m = (matrix_t *)calloc(1, sizeof(matrix_t));
if (m){
assert(cols > 0 && rows > 0);
m->cols = cols; m->rows = rows;
m->data = (TYPE *)calloc(cols*rows, sizeof(TYPE));
if (m->data){
if (nums){
for (int i = 0; i < cols; ++i){
for (int j = 0; j < rows; ++j){
m->data[idx(i, cols, j)] = *(nums + j*cols + i);
}
}
}
return (MatHandler *)m;
}
}
return NULL;
}
void DestroyMat(MatHandler mat){
matrix_t *m = (matrix_t *)mat;
free(m->data);
free(m);
}
int AddMat(MatHandler mat1, MatHandler mat2, MatHandler *ans){
matrix_t *m1, *m2, *a;
m1 = (matrix_t *)mat1; m2 = (matrix_t *)mat2;
if (!m1 || !m2) return M_ERR_PTRISNULL;
if (m1->cols != m2->cols || m1->rows != m2->rows) return M_ERR_COLSROWS;
a = (matrix_t *)(*ans) = (matrix_t *)CreateMat(m1->cols, m1->rows, NULL);
if (!a) return M_ERR_ALLOC;
for (int i = 0; i < a->cols; ++i){
for (int j = 0; j < a->rows; ++j){
int t = idx(i, a->cols, j);
a->data[t] = m1->data[t] + m2->data[t];
}
}
return M_OK;
}
int SubMat(MatHandler mat1, MatHandler mat2, MatHandler *ans){
matrix_t m = *(matrix_t *)mat2;
if (!mat2) return M_ERR_PTRISNULL;
for (int i = 0; i < m.cols; ++i){
for (int j = 0; j < m.rows; ++j){
int t = idx(i, m.cols, j);
m.data[t] *= -1;
}
}
return AddMat(mat1, &m, ans);
}
int NumberMulMat(MatHandler mat, double k, MatHandler *ans){
matrix_t *m1, *a;
m1 = (matrix_t *)mat;
if (!m1) return M_ERR_PTRISNULL;
a = (matrix_t *)(*ans) = (matrix_t *)CreateMat(m1->cols, m1->rows, NULL);
if (!a) return M_ERR_ALLOC;
for (int i = 0; i < a->cols; ++i){
for (int j = 0; j < a->rows; ++j){
int t = idx(i, a->cols, j);
a->data[t] = m1->data[t] * k;
}
}
return M_OK;
}
int ColCount(MatHandler mat){
return ((matrix_t *)mat) ? ((matrix_t *)mat)->cols : 0;
}
int RowCount(MatHandler mat){
return ((matrix_t *)mat) ? ((matrix_t *)mat)->rows : 0;
}
int MatrixMulMatrix(MatHandler mat1, MatHandler mat2, MatHandler *ans){
matrix_t *m1, *m2, *a;
m1 = (matrix_t *)mat1; m2 = (matrix_t *)mat2;
if (m1->cols != m2->rows) return M_ERR_COLSROWS;
a = (matrix_t *)(*ans) = CreateMat(m2->cols, m1->rows, NULL);
if (!a) return M_ERR_ALLOC;
for (int i = 0; i < a->cols; ++i){
for (int j = 0; j < a->rows; ++j){
for (int k = 0; k < m1->cols; ++k){
a->data[idx(i, a->cols, j)] +=
m1->data[idx(k, m1->cols, j)] * m2->data[idx(i, m2->cols, k)];
}
}
}
return M_OK;
}
int DetMat(MatHandler mat, double *ans){
matrix_t *m = (matrix_t *)mat;
if (m->cols != m->rows) return M_ERR_COLSROWS;
if (m->cols == 1){
*ans = m->data[0];
return M_OK;
}
if (m->cols == 2){
*ans = m->data[0] * m->data[3] - m->data[1] * m->data[2];
return M_OK;
}
matrix_t *t = (matrix_t *)CreateMat(m->cols - 1, m->rows - 1, NULL);
/* Ðàçëîæåíèå ïî ïåðâîé ñòðîêå */
int id = 0;
for (int i = 0; i < m->cols; ++i){ /* Áåæèò ïî âñåì îïðåäåëèòåëÿì, äîïîëíèòåëüíûì ìèíîðàì */
id = 0;
for (int j = 0; j < m->cols; ++j){ /* Áåæèò ïî âñåì ñòîëáöàì */
if (j == i) continue;
for (int k = 1; k < m->rows; ++k){
t->data[id] = m->data[idx(j, m->cols, k)];
++id;
}
}
//ShowMatrix(t); printf("\n");
double res = 0;
int r = DetMat(t, &res);
if (r == M_OK){
if (i % 2)
*ans -= m->data[idx(i, m->cols, 0)] * res;
else
*ans += m->data[idx(i, m->cols, 0)] * res;
}
else return r;
}
DestroyMat(t);
return M_OK;
}
int TransposeMat(MatHandler mat, MatHandler *ans){
matrix_t *m, *a;
m = (matrix_t *)mat;
a = (matrix_t *)(*ans) = CreateMat(m->rows, m->cols, NULL);
if (!a) return M_ERR_ALLOC;
for (int i = 0; i < m->cols; ++i){
for (int j = 0; j < m->rows; ++j){
a->data[idx(j, m->rows, i)] = m->data[idx(i, m->cols, j)];
}
}
return M_OK;
}
int CreateMinorMat(MatHandler mat, int icol, int jrow, MatHandler *ans){ /* Ñîçäàåò ìèíîð òîëüêî n-1-îãî ïîðÿäêà*/
matrix_t *a, *m = (matrix_t *)mat;
assert(m->cols > 1 && m->rows > 1);
a = (matrix_t *)(*ans) = (matrix_t *)CreateMat(m->cols - 1, m->rows - 1, NULL);
if (!a) return M_ERR_ALLOC;
a->cols = m->cols - 1;
a->rows = m->rows - 1;
int id = 0;
for (int i = 0; i < m->cols; ++i){
if (i == icol) continue;
for (int j = 0; j < m->rows; ++j){
if (j == jrow) continue;
int r = idx(i, m->cols, j);
a->data[id] = m->data[r];
++id;
}
}
return M_OK;
}
int InvertMat(MatHandler mat, MatHandler *ans){
matrix_t *m, *alg;
double det = 0.0;
int k = DetMat(mat, &det);
if (k != M_OK || !det) return M_ERR;
/* Íàéäåì ìàòðèöó àëãåáðàè÷åñêèõ äîïîëíåíèé */
m = (matrix_t *)mat;
if (!m) return M_ERR_PTRISNULL;
alg = (matrix_t *)CreateMat(m->cols, m->rows, NULL);
if (!alg) return M_ERR_ALLOC;
for (int i = 0; i < m->cols; ++i){
for (int j = 0; j < m->rows; ++j){
MatHandler t = NULL;
if (CreateMinorMat(mat, i, j, &t) != M_OK) return M_ERR;
double res = 0.0;
if (DetMat(t, &res) != M_OK) return M_ERR;
if ((i + j) % 2)
alg->data[idx(i, m->cols, j)] = -res;
else
alg->data[idx(i, m->cols, j)] = res;
}
}
matrix_t *talg = NULL;
if (TransposeMat((MatHandler)alg, &talg) != M_OK) return M_ERR;
if (NumberMulMat((MatHandler)talg, 1/det, ans) != M_OK) return M_ERR;
return M_OK;
}