-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.c
More file actions
422 lines (361 loc) · 11.9 KB
/
matrix.c
File metadata and controls
422 lines (361 loc) · 11.9 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*------------------------------------------------------------------------------
* matrix.c : matrix functions
*
* Copyright (C) 2007-2018 by T.TAKASU, All rights reserved.
*
*
* version : $Revision: 1.2 $ $Date: 2008/07/14 00:05:05 $
* history : 2021/10/08 1.0 new
* 2008/05/09 1.1 fix bug lli flag outage
* 2008/06/16 1.2 separate common functions to rcvcmn.c
*-----------------------------------------------------------------------------*/
#include "matrix.h"
/* initialize a matrix --------------------------------------------------------
* Matrix is initialized to zero matrix
* args : int row I rows of the new matrix
* int col I columns of the new matrix
* return : matrix
*
*-----------------------------------------------------------------------------*/
matrix* MatInit(int row,int col)
{
matrix *m=(matrix*)malloc(sizeof (matrix));
double *p = ( double*)malloc(sizeof(double) * row * col);
m->p=p;
m->row=row;
m->col=col;
for(int i=0;i<row;i++)
for(int j=0;j<row;j++)
{
p[i+j*row]=0.0;
}
return m;
}
/* generate a matrix --------------------------------------------------------
* generate matrix from array
* args : int row I rows of the new matrix
* int col I columns of the new matrix
* return : matrix
*
*-----------------------------------------------------------------------------*/
matrix* MatGen(double ** a)
{
matrix* m = (matrix*)malloc(sizeof(matrix));
m->row = sizeof(a) / sizeof(a[0]);
m->col = sizeof(a[0]) / sizeof(double);
double* p = (double*)malloc(sizeof(double) * m->row * m->col);
m->p = p;
for (int i = 0; i < row; i++)
for (int j = 0; j < row; j++)
{
p[i + j * row] = a[i][j];
}
return m;
}
/* free a matrix --------------------------------------------------------
* Free up the space occupied by the matrix
* args : matrix *m IO matrix which will be deleted
* return : void
*
*-----------------------------------------------------------------------------*/
void MatFree(matrix *m)
{
free(m->p);
}
/* input a matrix --------------------------------------------------------
* Input the matrix from keyboard
* args : void
* return : matrix
*
*-----------------------------------------------------------------------------*/
matrix *MatInput()
{
int row,col;
printf("Row:");
scanf("%d",&row);
printf("Col:");
scanf("%d",&col);
matrix *m=MatInit(row,col);
printf("Matrix:");
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
scanf("%lf",&m->p[i*row+j]);
}
return m;
}
/* output a matrix --------------------------------------------------------
* Output the matrix to window
* args : matrix *m IO matrix which will be output
* return : void
*
*-----------------------------------------------------------------------------*/
void MatOutput(matrix *m)
{
for(int i=0;i<m->row;i++) {
for (int j = 0; j < m->col; j++) {
printf("%lf", m->p[i * m->row + j]);
}
printf("\n");
}
}
/* matrix addition--------------------------------------------------------
* Output the matrix to window
* args : matrix *a IO matrix which will be added
* matrix *b IO matrix which will be added
* return : matrix addition result
*
*-----------------------------------------------------------------------------*/
matrix *MatAdd(matrix *a, matrix *b)
{
if(a->row==b->row &&a->col==b->row)
{
matrix *m=MatInit(a->row,a->col);
for (int i = 0;i<a->row;i++)
for (int j = 0;j<a->col;j++)
{
m->p[i*a->row+j]=a->p[i*a->row+j]+b->p[i*a->row+j];
}
return m;
}
else printf("MatAdd Error: rows or cols are not equal\n");
}
/*matrix subtraction--------------------------------------------------------
* Output the matrix to window
* args : matrix *a IO matrix
* matrix *b IO matrix
* return : matrix subtraction result
*
*-----------------------------------------------------------------------------*/
matrix *MatSub(matrix *a, matrix *b)
{
if(a->row==b->row &&a->col==b->row)
{
matrix *m=MatInit(a->row,b->col);
for (int i = 0;i<a->row;i++)
for (int j = 0;j<a->col;j++)
{
m->p[i*a->row+j]=a->p[i*a->row+j]-b->p[i*a->row+j];
}
return m;
}
else printf("MatDel Error: rows or cols are not equal\n");
}
/* multiply a matrix --------------------------------------------------------
* Output the matrix to window
* args : matrix *a IO matrix
* matrix *b IO matrix
* return : matrix multiplication result
*
*-----------------------------------------------------------------------------*/
matrix *MatMal(matrix *a, matrix *b)
{
matrix *m=MatInit(a->row,b->col);
if(a->col==b->row)
{
for (int i = 0;i<m->row;i++)
for (int j = 0;j<m->col;j++)
{
double temp =0;
for (int k=0;k<a->col;k++)
temp=temp+a->p[i*a->row+k]*b->p[k*b->row+j];
m->p[i*m->row+j]=temp;
}
return m;
}
else printf("MatMul Error: a.row is not equal to b.col\n");
}
/* transpose a matrix --------------------------------------------------------
* transpose a matrix
* args : matrix *a I matrix
* return : matrix transpose result
*
*-----------------------------------------------------------------------------*/
matrix *MatTrans(matrix *a)
{
matrix *m=MatInit(a->col,a->row);
for (int i = 0;i<m->row;i++)
for (int j = 0;j<m->col;j++)
{
m->p[i*m->row+j]=a->p[j*a->row+i];
}
return m;
}
/* determination of a matrix --------------------------------------------------------
* determination of a matrix
* args : matrix *a I matrix
* return : double determination result
*
*-----------------------------------------------------------------------------*/
double MatDet(matrix *a)
{
if(a->row != a->col) printf("MatInv Error: row is not equal to col \n");
else {
int n = a->row;
double res=1.0;
matrix *copy=MatInit(n, n);
for(int x=0; x < a->row; x++)
for(int y=0; y < a->col; y++)
copy->p[x * a->row + y]=a->p[x * a->row + y];
matrix *output=MatInit(n, n);//output
matrix *L=MatInit(n,n);//L
matrix *U=MatInit(n,n);//U
matrix *r=MatInit(n,n);//r
int i,j,k;
double s,t;
for(j=0;j<n;j++)
copy->p[0 * n + j]=copy->p[0 * n + j]; //计算U矩阵的第一行
for(i=1;i<n;i++)
copy->p[i * n + 0]= copy->p[i * n + 0] / copy->p[0 * n + 0]; //计算L矩阵的第1列
for(k=1;k<n;k++)
{
for(j=k;j<n;j++)
{
s=0.0;
for (i=0;i<k;i++)
s= s + copy->p[k * n + i] * copy->p[i * n + j]; //累加
copy->p[k * n + j]= copy->p[k * n + j] - s; //计算U矩阵的其他元素
}
for(i=k+1;i<n;i++)
{
t=0.0;
for(j=0;j<k;j++)
t= t + copy->p[i * n + j] * copy->p[j * n + k]; //累加
copy->p[i * n + k]= (copy->p[i * n + k] - t) / copy->p[k * n + k]; //计算L矩阵的其他元素
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i>j)
{
L->p[i*n+j]=copy->p[i * n + j];
U->p[i*n+j]=0.0;
}//如果i>j,说明行大于列,计算矩阵的下三角部分,得出L的值,U的//为0
else
{
U->p[i*n+j]=copy->p[i * n + j];
if(i==j)
L->p[i*n+j]=1.0; //否则如果i<j,说明行小于列,计算矩阵的上三角部分,得出U的//值,L的为0
else
L->p[i*n+j]=0.0;
}
}
for (i=0;i<n;i++)
{res*=U->p[i*n+i]; }
return res;
}
}
/* Matrix inversion --------------------------------------------------------
* Matrix inversion
* args : matrix *a I matrix
* return : matrix transpose result
*
*-----------------------------------------------------------------------------*/
matrix *MatInv(matrix *a)
{
if(a->row != a->col) printf("MatInv Error: row is not equal to col \n");
else
{
int n=a->row;
matrix *copy=MatInit(n, n);
for(int x=0; x < a->row; x++)
for(int y=0; y < a->col; y++)
copy->p[x * a->row + y]=a->p[x * a->row + y];
matrix *output=MatInit(n, n);//output
matrix *L=MatInit(n,n);//L
matrix *U=MatInit(n,n);//U
matrix *r=MatInit(n,n);//r
matrix *u=MatInit(n,n);//u
matrix *test=MatInit(n,n);
int i,j,k;
double s,t;
double det=1.0;
for(j=0;j<n;j++)
copy->p[0 * n + j]=copy->p[0 * n + j]; //计算U矩阵的第一行
for(i=1;i<n;i++)
copy->p[i * n + 0]= copy->p[i * n + 0] / copy->p[0 * n + 0]; //计算L矩阵的第1列
for(k=1;k<n;k++)
{
for(j=k;j<n;j++)
{
s=0.0;
for (i=0;i<k;i++)
s= s + copy->p[k * n + i] * copy->p[i * n + j]; //累加
copy->p[k * n + j]= copy->p[k * n + j] - s; //计算U矩阵的其他元素
}
for(i=k+1;i<n;i++)
{
t=0.0;
for(j=0;j<k;j++)
t= t + copy->p[i * n + j] * copy->p[j * n + k]; //累加
copy->p[i * n + k]= (copy->p[i * n + k] - t) / copy->p[k * n + k]; //计算L矩阵的其他元素
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i>j)
{
L->p[i*n+j]=copy->p[i * n + j];
U->p[i*n+j]=0.0;
}//如果i>j,说明行大于列,计算矩阵的下三角部分,得出L的值,U的//为0
else
{
U->p[i*n+j]=copy->p[i * n + j];
if(i==j)
L->p[i*n+j]=1.0; //否则如果i<j,说明行小于列,计算矩阵的上三角部分,得出U的//值,L的为0
else
L->p[i*n+j]=0.0;
}
}
for (i=0;i<n;i++)
{det*=U->p[i*n+i]; }
if(abs(det)<1e-10)
{
printf("MatInv Error: matrix is not inversible \n");
}
/////////////////////求L和U矩阵的逆
for (i=0;i<n;i++) /*求矩阵U的逆 */
{
u->p[i*n+i]=1.0/(U->p[i*n+i]);//对角元素的值,直接取倒数
//printf("\n%lf\n",u->p[i*n+i]);
for (k=i-1;k>=0;k--)
{
s=0.0;
for (j=k+1;j<=i;j++)
s=s+(U->p[k*n+j])*(u->p[j*n+i]);
u->p[k*n+i]=-s/(U->p[k*n+k]);//迭代计算,按列倒序依次得到每一个值,
}
}
for (i=0;i<n;i++) //求矩阵L的逆
{
r->p[i*n+i]=1.0; //对角元素的值,直接取倒数,这里为1
for (k=i+1;k<n;k++)
{
for (j=i;j<=k-1;j++)
r->p[k*n+i]=(r->p[k*n+i])-(L->p[k*n+j])*(r->p[j*n+i]); //迭代计算,按列顺序依次得到每一个值
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ output->p[i * n + j]=0.0;}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
output->p[i * n + j]+= u->p[i * n + k] * r->p[k * n + j];
}
}
}
MatFree(L);
MatFree(U);
MatFree(r);
MatFree(u);
return output;
}
}