-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.c
272 lines (229 loc) · 5.21 KB
/
matrix.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "matrix.h"
Matrix initMatrix(int rows, int cols)
{
Matrix result;
result.rows = rows;
result.cols = cols;
result.data = (double *)calloc(rows * cols, sizeof(double));
return result;
}
Matrix loadMatrix(int rows, int cols, double * elements)
{
Matrix result;
result.rows = rows;
result.cols = cols;
result.data = (double *)calloc(rows * cols, sizeof(double));
for(int i = 0; i < rows * cols; i++) {
result.data[i] = elements[i];
}
return result;
}
void printMatrix(Matrix m)
{
printf("\nRows: %d\tColumns: %d\n", m.rows, m.cols);
for(int i = 0; i < m.rows; i++) {
printf("| ");
for(int j = 0; j < m.cols; j++) {
printf("%4.2lf ", m.data[i*m.cols+j]);
}
printf(" |\n");
}
}
Matrix addMatrix(Matrix a, Matrix b)
{
if(a.rows != b.rows || a.cols != b.cols) {
printf("Can't add matrices of different dimensions");
return a;
}
else {
Matrix result = initMatrix(a.rows, a.cols);
for(int i = 0; i < a.rows * a.cols; i++) {
result.data[i] = a.data[i] + b.data[i];
}
return result;
}
}
Matrix multiMatrix(Matrix a, Matrix b)
{
if(a.cols != b.rows) {
printf("Can't multiply matrices");
return a;
}
else {
Matrix result = initMatrix(a.rows, b.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < b.cols; j++) {
double sum = 0;
for (int k = 0; k < a.cols; k++)
sum = sum + a.data[i * a.cols + k] * b.data[k * b.cols + j];
result.data[i * b.cols + j] = sum;
}
}
return result;
}
}
Matrix transMatrix(Matrix m)
{
Matrix result = initMatrix(m.cols, m.rows);
int k = 0;
for(int j = 0; j < m.rows; j++) {
for(int i = 0; i < m.cols; i++) {
result.data[i*m.rows + j] = m.data[k];
k++;
}
}
return result;
}
int squareMatrix(Matrix m, double square[25][25])
{
if(m.rows != m.cols || m.rows > 24) {
printf("Matrix isn't square!");
return 0;
}
else {
int k = 0;
for(int i = 0; i < m.rows; i++) {
for(int j = 0; j < m.rows; j++) {
square[i][j] = m.data[k];
k++;
}
}
return m.rows;
}
}
Matrix inverseMatrix(Matrix m)
{
double squareTemp [25][25];
memset(squareTemp, 0, 25 * 25 *sizeof(double));
Matrix result = initMatrix(m.rows,m.rows);
int n = squareMatrix(m, squareTemp);
double d = determinant(squareTemp, n);
//printf("\nThe determinant is: %.0f", d);
if (d == 0) {
printf("\nMATRIX IS NOT INVERSIBLE\n");
}
else {
cofactors(squareTemp, n);
}
int k = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
result.data[k] = squareTemp[i][j];
k++;
}
}
return result;
}
Matrix genCoefficients(Matrix x, Matrix y)
{
Matrix B = initMatrix(x.cols, x.cols);
B = multiMatrix((inverseMatrix(multiMatrix(transMatrix(x), x))),(multiMatrix(transMatrix(x),y)));
return B;
}
Matrix calcResiduals(Matrix x, Matrix y, Matrix B, double * errStdDev)
{
int i;
double sum = 0;
Matrix result = initMatrix(y.rows, 1);
Matrix Yhat = initMatrix(y.rows, 1);
Yhat = multiMatrix(x, B);
for(i = 0; i < y.rows; i++) {
result.data[i] = y.data[i] - Yhat.data[i];
}
for(i = 0; i < result.rows; i++) {
sum += result.data[i] * result.data[i];
}
*errStdDev = sqrt(sum / (result.rows - (x.cols - 1) - 1));
free(Yhat.data);
return result;
}
Matrix stdErr(Matrix x, double errStdDev)
{
int i;
Matrix result = initMatrix(x.cols, x.cols);
result = inverseMatrix(multiMatrix(transMatrix(x),x));
for(i = 0; i < x.cols * x.cols; i++) {
result.data[i] *= errStdDev * errStdDev;
}
return result;
}
//Inverse matrix functions from : http://scanftree.com/programs/c/c-program-to-find-the-inverse-of-the-matrix/
double determinant(double a[25][25], double k) {
double s = 1, det = 0, b[25][25];
int i, j, m, n, c;
if (k == 1) {
return (a[0][0]);
} else {
det = 0;
for (c = 0; c < k; c++) {
m = 0;
n = 0;
for (i = 0; i < k; i++) {
for (j = 0; j < k; j++) {
b[i][j] = 0;
if (i != 0 && j != c) {
b[m][n] = a[i][j];
if (n < (k - 2))
n++; else {
n = 0;
m++;
}
}
}
}
det = det + s * (a[0][c] * determinant(b, k - 1));
s = -1 * s;
}
}
return (det);
}
void cofactors(double num[25][25], double f) {
double b[25][25], fac[25][25];
int p, q, m, n, i, j;
for (q = 0; q < f; q++) {
for (p = 0; p < f; p++) {
m = 0;
n = 0;
for (i = 0; i < f; i++) {
for (j = 0; j < f; j++) {
b[i][j] = 0;
if (i != q && j != p) {
b[m][n] = num[i][j];
if (n < (f - 2))
n++; else {
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
}
}
trans(num, fac, f);
}
void trans(double num[25][25], double fac[25][25], double r) {
int i, j;
double b[25][25], inv[25][25], d;
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
b[i][j] = fac[j][i];
}
}
d = determinant(num, r);
inv[i][j] = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
inv[i][j] = b[i][j] / d;
}
}
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
num[i][j] = inv[i][j];
}
}
}