質問編集履歴

2

アルゴリズムの修正。説明を追加。作り直したソースコードを張りました。

2017/02/13 11:00

投稿

inxsirencer
inxsirencer

スコア16

test CHANGED
File without changes
test CHANGED
@@ -6,17 +6,109 @@
6
6
 
7
7
  プログラムの流れは、
8
8
 
9
- 1.行列の行数、列数を入力
9
+ 1.行列の行数(lnum)、列数(cnum)を入力
10
10
 
11
11
  2.行列の各成分を入力、格納。
12
12
 
13
+ 3.行列の走査(matsearch)を行う。
14
+
15
+ 走査範囲は初めは1列目の全行であるとする。
16
+
17
+ 1列目から順に
18
+
19
+ 特定の列の走査範囲内の要素をを上から順に確認
20
+
21
+ →すべて0 ⇒ 走査範囲はそのままで次の列に
22
+
23
+
24
+
13
- 3.i行j列の成分0ならば、n[i]=0,非0ならばn[i]=1を格納。これをj列の成分すべてついて行う。
25
+ →上端が非0かつ上端以外はすべて0 ⇒ 走査範囲を一縮める(元の走査範囲から上端の行を抜たもの)。そし走査範囲は次の列へ
26
+
27
+
28
+
14
-
29
+ →上端以外に非0が存在
30
+
31
+ ⇒ その列が基本変形対象範囲最重要列J₀
32
+
33
+ かつ
34
+
15
- 4.i₀≦i≦[行数総数]とする。n[i]=1ならば、i行成分をすべて(i,j)成分で割り、n[i]=0ならば割らずに次列に移動を繰り返し、すべて列でこれが完了すると、i[i]=1であるiのみに対して、i行-i₀行の行基本変形う。それを終えると、j+1列に移って、i+1行以降の行について4.の操作を繰り返す
35
+ 走査範囲上端の行基本変形対象範囲開始I₀。
36
+
16
-
37
+ for行ループ → break
38
+
39
+ for列ループ → break
40
+
41
+ matsearch → 終了
42
+
43
+
44
+
45
+ 最終的に…matsearchの返却値は
46
+
47
+ ①J=cnumですべてその列の成分が0
48
+
49
+ → -2
50
+
51
+ ②(I₀,J₀)=0かつbreak
52
+
53
+ → I₀
54
+
55
+ ③(I₀,J₀)≠0かつbreak
56
+
57
+ → -1
58
+
59
+ 4.行基本変形
60
+
61
+ ①:
62
+
63
+ rank計算へ
64
+
65
+
66
+
67
+ ②:
68
+
69
+ (I, J₀)成分のうち、I₀≦I≦lnumの範囲で
70
+
71
+ 非0となっている成分について考える。
72
+
73
+ このような成分のうち、最小行番号をもつ成分の行番号
74
+
75
+ をI₁とする。(I₀<I₁≦lnum)
76
+
77
+
78
+
17
- 5.↑の操作を行うにあたって、i₀,j)成分が0あった場合にはi₀以降の(i,j)成分でn[i]=1の成分があれば、そのような行i₁行とし、i₀行+i₁行の行基本変形を行うそして、4.の操作をおこなう。
79
+ 2-2-1.1:I₁,J)成分でI₁行成分をすべ割る
80
+
18
-
81
+ 2-2-1.2:(I₀,J)成分に
82
+
83
+ 「(I₀,J)成分 + (I₁,J)成分」
84
+
85
+ の演算結果を代入する。
86
+
87
+ 2-2-1.3:
88
+
89
+ (I,J)成分に(I>I₀)
90
+
91
+ 「(I,J)成分 -{(I,J)成分*(I₀,J)}」
92
+
93
+ の演算結果を代入する。
94
+
95
+ そのまま③へ
96
+
97
+
98
+
99
+ ③:
100
+
101
+ 操作2-1
102
+
19
- 6.5.を満たすような成分がない(i₀行以降のj列成分すべて0の)場合は、j+1行に移って4.の操作を行うが、この時、その操作はi₀+1行から開始すのではなく、i₀行から開始する
103
+ 2-1.1:(I₀ , J₀)成分でI₀行成分すべてる。
104
+
105
+ 2-1.2:(I,J)成分に(I>I₀)
106
+
107
+ 「(I,J)成分 -{(I,J)成分*(I₀,J)}」
108
+
109
+ の演算結果を代入する。
110
+
111
+
20
112
 
21
113
 
22
114
 
@@ -28,25 +120,7 @@
28
120
 
29
121
  ###発生している問題・エラーメッセージ
30
122
 
31
- 例えば、計算過程で
32
-
33
- | 1.00 2.00 3.00 |
34
-
35
- | 0.00 1.00 2.00 |
36
-
37
- | 0.00 0.00 0.00 |
38
-
39
- こまで来ているので、もう何も余計なことをる必要はないのに、
123
+ 計算過程でループが終了せずどが間違っているのかがわからないです
40
-
41
- 次の手順後には
42
-
43
- | 1.00 2.00 3.00 |
44
-
45
- | 0.00 1.00 2.00 |
46
-
47
- | 0.00 0.00 1.00 |
48
-
49
- と訳の分からないことをしています。
50
124
 
51
125
 
52
126
 
@@ -54,105 +128,277 @@
54
128
 
55
129
  ###該当のソースコード
56
130
 
57
- ```ここに言語を入力
131
+ ```
58
132
 
59
133
  #include <stdio.h>
60
134
 
61
-
135
+ #define NA 7
62
-
136
+
63
- #define numa 3
137
+ #define NB 3
64
-
65
- #define numb 7
138
+
66
-
67
- #define plus 0
139
+ #define PLUS 0
68
-
140
+
69
- #define minus 1
141
+ #define MINUS 1
70
-
71
-
72
-
73
- /*四捨五入(10のn乗の位を処理)*/
142
+
74
-
75
- #include <math.h>
143
+ #define ERROR_R 1.000000e-15
76
-
77
-
78
-
144
+
145
+
146
+
147
+
148
+
79
- double around(double src, int n)
149
+ int zerojudgef(double a)
80
150
 
81
151
  {
82
152
 
83
- double dst;
84
-
85
-
86
-
87
- dst = src * pow(10, -n - 1); /*処理を行う桁を10-1 の位にする*/
88
-
89
- dst = (double)(int)(dst + 0.5);
90
-
91
-
92
-
93
- return dst * pow(10, n + 1); /*処理を行った桁を元に戻す*/
153
+ return ((a < ERROR_R) & (a > (-1)*ERROR_R) ? 0 : 1);
94
154
 
95
155
  }
96
156
 
97
157
 
98
158
 
99
- /**/
100
-
101
- int line, column = 0;
102
-
103
- /**/
104
-
105
-
106
-
107
- double mat[numa][numb][numb];
108
-
109
-
110
-
111
- int zerojudgef(double a)
159
+ int matsearchf(double mat[][NA][NA], int lnum, int cnum, int keylinenum, int keycolumnnum, int rank)
160
+
161
+ /*
162
+
163
+ keylinenum : the number of line that is the start point of elementary row operation. Exactly, in the column of key-column number, and below the line of the start line of elemntary row operation ,there may exists the element that is not zero which has the smallest number of line. The line number of such elements is defined as keylinenum.
164
+
165
+ That is, if the start line of elementary row operation is not the exactly defined keyline number, the start line of elementray row operation is linebreak.
166
+
167
+
168
+
169
+ keycolumnnum : the number of column that is the key-column of elementary row op
170
+
171
+ ertion.
172
+
173
+ */
112
174
 
113
175
  {
114
176
 
115
- return (a != 0 ? 1 : 0);
177
+ int i, j;
178
+
179
+ int linebreak = 0;
180
+
181
+ for(j=0; j<cnum; j++){
182
+
183
+ int k = 0;
184
+
185
+ int notzero = 0;
186
+
187
+ int keynum_for_count = 0;
188
+
189
+ for(i=linebreak; i<lnum; i++){
190
+
191
+
192
+
193
+ if(zerojudgef(mat[0][i][j])){
194
+
195
+ notzero++;
196
+
197
+ }/*for-for-if*/
198
+
199
+ }/*for-for*/
200
+
201
+ /*Finding out the key-line number
202
+
203
+ In the column of key-column number, and below the line of key-line number, there may exists the element that is not zero which has the smallest number of line. The line number of such elements is defined as keylinenum, and in this function, for the purpose of counting the key-line number, a variable 'keynum_for_count' is defined.
204
+
205
+ */
206
+
207
+ do{
208
+
209
+ keynum_for_count = (zerojudgef(mat[0][k][j]) != 0 ? k : -1);//ここは最悪k+linebreakでもok
210
+
211
+ k++;
212
+
213
+ }while( (keynum_for_count < 0) & (k != (NA-1) ) );//matのイランとこは全部0で初期化が条件
214
+
215
+
216
+
217
+ if(zerojudgef(notzero)){/*There exist at least one element that is not equal to zero.*/
218
+
219
+
220
+
221
+ if(zerojudgef(mat[0][linebreak][j])){/*The top elements in the scope of line-searching is not equal to zero.*/
222
+
223
+ if(notzero > 1){/*There exists not-zero elemnet at the top of the scope of line searching, and exists not-zero elements below the elements at the top. break roop*/
224
+
225
+ keylinenum = keynum_for_count;
226
+
227
+ keycolumnnum = j;
228
+
229
+ return -1;
230
+
231
+ break;
232
+
233
+ }else{
234
+
235
+ /*There exists not-zero element only at the top of the searching range, and elements below it is all equal to zero. continue roop*/
236
+
237
+ linebreak++;
238
+
239
+
240
+
241
+ }/*for-if-if-if*/
242
+
243
+ }else{/*The top elements in the scope of line-searching is equal to zero, but there exist at least one elements that is not equal to zero. break roop*/
244
+
245
+ keylinenum = keynum_for_count;
246
+
247
+ keycolumnnum = j;
248
+
249
+ return linebreak;
250
+
251
+ break;
252
+
253
+ }/*for-if-if*/
254
+
255
+ }else{/*There exist only elements that is equal to zero. continue roop*/
256
+
257
+
258
+
259
+ }/*for-if*/
260
+
261
+ }/*for*/
262
+
263
+
264
+
265
+ rank = linebreak;
266
+
267
+ return -2;
268
+
269
+
270
+
271
+ /*Finally, the result is classified into 3 types.
272
+
273
+ 1:All the elemnets in the last column of the scope is equal to zero.
274
+
275
+ output = -2
276
+
277
+ 2:The (keylinenum, keycolumnnum) elemnet is equal to zero.
278
+
279
+ output = linebreak
280
+
281
+ 3:The (keylinenum, keycolumnnum) elemnet is not equal to zero.
282
+
283
+ output = -1
284
+
285
+ */
116
286
 
117
287
  }
118
288
 
119
289
 
120
290
 
121
- void substif(int n[], double mat[0][numb][numb], int i, int lnum)
291
+ void divf(int divdo, int divdonel, int cnum, double mat[][NA][NA])
122
292
 
123
293
  {
124
294
 
295
+ /*
296
+
125
- /*i : 1 or 0 判定を行う列番号*/
297
+ divdo :割る数の要素の列番号
126
-
298
+
127
- /*lnum :数の総数*/
299
+ divdonel :割られる数の要素が存在する行番号
300
+
128
-
301
+ cnum :割り算実行される行の列数
302
+
303
+ */
304
+
305
+ double divisor = mat[0][divdonel][divdo];
306
+
307
+
308
+
129
- int h;
309
+ int i;
130
-
131
- double a;
310
+
132
-
133
- for(h=0; h<lnum; h++){
311
+ for(i=0; i<cnum; i++){
134
-
135
- a = mat[0][h][i];
312
+
136
-
137
- n[h] = zerojudgef(a);
313
+ mat[0][divdonel][i] /= divisor;
138
314
 
139
315
  }
140
316
 
141
317
 
142
318
 
319
+ /**/
320
+
321
+ int line, column;
322
+
323
+ puts("divf");
324
+
325
+ printf("\n");
326
+
327
+ for(line=0; line<NA; line++){
328
+
329
+ printf("|");
330
+
331
+ for(column=0; column<NA; column++){
332
+
333
+ printf("%5.2f", mat[0][line][column]);
334
+
335
+ }
336
+
337
+ printf(" |\n");
338
+
339
+ }
340
+
341
+ printf("\n\n");
342
+
343
+
344
+
345
+
346
+
143
347
  /**/
144
348
 
349
+
350
+
351
+ }
352
+
353
+
354
+
355
+ void subtraf(int p, int q, int r, int cnum, double mat[][NA][NA], double coeff)
356
+
357
+ {
358
+
359
+ /*
360
+
361
+ This function operates the line p ± the line q.
362
+
363
+ Judge by whether r = 0 or 1
364
+
365
+ -r=0→addition+
366
+
367
+ -r=1→subtractionー
368
+
369
+ cnum = The total number of line
370
+
371
+ coeff = the coefficient of the number that subtacts any numbers
372
+
373
+ */
374
+
375
+ int pm = (r == 1 ? (-1) : 1);
376
+
377
+ int i;
378
+
379
+ for(i=0; i<cnum; i++){
380
+
381
+ mat[0][p][i] += (coeff * pm * mat[0][q][i]);
382
+
383
+ }
384
+
385
+
386
+
387
+ /**/
388
+
389
+ int line, column;
390
+
145
- puts("substif");
391
+ puts("subtraf");
146
392
 
147
393
  printf("\n");
148
394
 
149
- for(line=0; line<numb; line++){
395
+ for(line=0; line<NA; line++){
150
396
 
151
397
  printf("|");
152
398
 
153
- for(column=0; column<numb; column++){
399
+ for(column=0; column<NA; column++){
154
-
400
+
155
- printf("%6.f", mat[0][line][column]);
401
+ printf("%5.2f", mat[0][line][column]);
156
402
 
157
403
  }
158
404
 
@@ -164,75 +410,177 @@
164
410
 
165
411
 
166
412
 
167
- for(line=0; line<numb; line++)
168
-
169
- printf("n[%d] %5d\n", line, n[line]);
170
-
171
413
 
172
414
 
173
415
  /**/
174
416
 
417
+
418
+
419
+
420
+
175
421
  }
176
422
 
177
423
 
178
424
 
179
- void divf(int divdo, int divdonel, int cnum, double mat[0][numb][numb])
425
+ void erowoperatef(double mat[][NA][NA], int lnum, int cnum, int keylinenum, int keycolumnnum)
180
426
 
181
427
  {
182
428
 
183
- /*
184
-
185
- divdo :割る数の要素の列番号
186
-
187
- divdonel :割られる数の要素が存在する行番号
188
-
189
- cnum :割り算実行される行の列数
190
-
191
- */
192
-
193
- double divisor = mat[0][divdonel][divdo];
194
-
195
-
196
-
197
- int i;
198
-
199
- for(i=0; i<cnum; i++){
200
-
201
- double divided = mat[0][divdonel][i];
202
-
203
- mat[0][divdonel][i] = around(divided / divisor, -5);
429
+ int rank = 0;
430
+
431
+ int i, j, k;
432
+
433
+ int breakjudge = 0;
434
+
435
+ for(i=0; i<NA; i++){
436
+
437
+ k = ( ( matsearchf(mat, lnum, cnum, keylinenum, keycolumnnum, rank) < 0 ) ? matsearchf(mat, lnum, cnum, keylinenum, keycolumnnum, rank) : 1 );
438
+
439
+ switch(k){
440
+
441
+ case 1:{
442
+
443
+ divf(keycolumnnum, keylinenum, cnum, mat);
444
+
445
+ subtraf( matsearchf(mat, lnum, cnum, keylinenum, keycolumnnum, rank), keylinenum, PLUS, cnum, mat, 1);
446
+
447
+ }/*for-switch-case0*/
448
+
449
+ case -1:{
450
+
451
+ divf(keycolumnnum, keylinenum, cnum, mat);
452
+
453
+ if(i = keylinenum){
454
+
455
+ }else{
456
+
457
+ for(j=0; j<lnum; j++){
458
+
459
+ subtraf( j, keylinenum, MINUS, cnum, mat , mat[0][j][keycolumnnum]);
460
+
461
+ }/*for-switch-case-1-if-for*/
462
+
463
+ }/*for-switch-case-1-if*/
464
+
465
+ break;
466
+
467
+ }/*for-switch-case1*/
468
+
469
+ case -2:{
470
+
471
+ breakjudge = 1;
472
+
473
+ break;
474
+
475
+ }/*for-switch-case-1*/
476
+
477
+ }/*switch*/
478
+
479
+ if(breakjudge == 1){
480
+
481
+ break;
482
+
483
+ }/*for-if*/
484
+
485
+ }/*for*/
486
+
487
+
488
+
489
+ }
490
+
491
+
492
+
493
+ int rankcaloperatef(double mat[][NA][NA], int lnum, int cnum)
494
+
495
+ {
496
+
497
+ int keylinenum = 0;
498
+
499
+ int keycolumnnum = 0;
500
+
501
+ erowoperatef(mat, lnum, cnum, keylinenum, keycolumnnum);
502
+
503
+ int rank = 0;
504
+
505
+ int empty = matsearchf(mat, lnum, cnum, keylinenum, keycolumnnum, rank);
506
+
507
+ return rank;
508
+
509
+ }
510
+
511
+
512
+
513
+ int main(void)
514
+
515
+ {
516
+
517
+ double mat[NB][NA][NA] = {};
518
+
519
+ int lnum, cnum;
520
+
521
+ /*mat[kind of matrix][line number][column number] */
522
+
523
+ puts("\nWe will calculate the rank of a matrix.\nPlease enter the scale of matrix.");
524
+
525
+ printf("The number of lines (under %d) = ", NA);
526
+
527
+ scanf("%d", &lnum);
528
+
529
+ printf("The number of columns (under %d) = ", NA);
530
+
531
+ scanf("%d", &cnum);
532
+
533
+ puts("Please enter the elements of the matrix.");
534
+
535
+ puts("'mat[i][j]' represents the (i,j) elements of the matrix.");
536
+
537
+ int i, j;
538
+
539
+ for(i=0; i<lnum; i++){
540
+
541
+ for(j=0; j<cnum; j++){
542
+
543
+ printf("mat[%d][%d] = ", i+1, j+1);
544
+
545
+ scanf("%lf", &mat[0][i][j]);
546
+
547
+ mat[1][i][j] = mat[0][i][j];
548
+
549
+ }
204
550
 
205
551
  }
206
552
 
207
-
208
-
209
- /**/
553
+ /*mat[1]にはもともと行列が保存される。*/
554
+
210
-
555
+ int Rank = rankcaloperatef(mat, lnum, cnum);
556
+
557
+ int r;
558
+
211
- puts("divf");
559
+ for(r=0; r<2; r++){
212
-
560
+
213
- printf("\n");
561
+ printf("\n");
214
-
562
+
215
- for(line=0; line<numb; line++){
563
+ for(i=0; i<lnum; i++){
216
-
564
+
217
- printf("|");
565
+ printf("|");
218
-
566
+
219
- for(column=0; column<numb; column++){
567
+ for(j=0; j<cnum; j++){
220
-
568
+
221
- printf("%5.2f", mat[0][line][column]);
569
+ printf("%5.f", mat[r][i][j]);
222
-
223
- }
224
-
225
- printf(" |\n");
226
570
 
227
571
  }
228
572
 
573
+ printf(" |\n");
574
+
575
+ }
576
+
229
- printf("\n\n");
577
+ printf("\n\n");
578
+
230
-
579
+ }
580
+
231
-
581
+ printf("\nThe rank of this matrix is %d.\n", Rank);
232
-
233
-
234
-
582
+
235
- /**/
583
+ return 0;
236
584
 
237
585
 
238
586
 
@@ -240,519 +588,7 @@
240
588
 
241
589
 
242
590
 
243
- void subtraf(int p, int q, int r, int cnum, double mat[0][numb][numb])
591
+
244
-
245
- {
246
-
247
- /*
248
-
249
- p行 ± q行 を行う。
250
-
251
- r = 0 or 1 で
252
-
253
- -r=0→足し算+
254
-
255
- -r=1→引き算ー
256
-
257
- cnum = 行列の総列数
258
-
259
- */
260
-
261
- int pm = (r == 1 ? (-1) : 1);
262
-
263
- int i;
264
-
265
- for(i=0; i<cnum; i++){
266
-
267
- mat[0][p][i] += (pm * mat[0][q][i]);
268
-
269
- }
270
-
271
-
272
-
273
- /**/
274
-
275
- puts("subtraf");
276
-
277
- printf("\n");
278
-
279
- for(line=0; line<numb; line++){
280
-
281
- printf("|");
282
-
283
- for(column=0; column<numb; column++){
284
-
285
- printf("%5.2f", mat[0][line][column]);
286
-
287
- }
288
-
289
- printf(" |\n");
290
-
291
- }
292
-
293
- printf("\n\n");
294
-
295
-
296
-
297
-
298
-
299
- /**/
300
-
301
-
302
-
303
-
304
-
305
- }
306
-
307
-
308
-
309
-
310
-
311
- void divjudgef(int e, int k, int cnum, int lnum, double mat[0][numb][numb], int n[])
312
-
313
- {
314
-
315
- /*
316
-
317
- e :行基本変形の対象範囲の左上角の成分の行番号
318
-
319
- k :行基本変形対象範囲の左上角の成分の列番号
320
-
321
- cnum :行列の総行数
322
-
323
- lnum :行列の総列数
324
-
325
- */
326
-
327
-
328
-
329
- int u = 0;/*e以降のeに対する相対的な行番号e+uはa_e_k以降初めての非0の成分の存在する業番号を表す。*/
330
-
331
-
332
-
333
- switch(n[e] == 0){
334
-
335
- /*1:The pattern of A_e_k = 0*/
336
-
337
- case 1:{
338
-
339
- while(n[e+u] == 0){
340
-
341
- u++;
342
-
343
- if((e+u) == lnum){
344
-
345
- break;
346
-
347
- }/*switch-case1-while-if*/
348
-
349
- break;
350
-
351
- }/*switch-case1-while*/
352
-
353
-
354
-
355
- switch((e+u) == lnum){
356
-
357
-
358
-
359
- /*The pattern of 1-1:A_e_* = 0(All zero)*/
360
-
361
- case 1:{
362
-
363
- e -= 1;
364
-
365
- printf("%d\n", e);
366
-
367
- break;
368
-
369
- }/*switch-case1-switch-case1*/
370
-
371
-
372
-
373
- /*The pattern of 1-2:There exists at least one elemnets such as A_e_* ≠ 0*/
374
-
375
- case 0:{
376
-
377
- divf(k, e+u, cnum, mat);
378
-
379
- subtraf(e, e+u, plus, cnum, mat);
380
-
381
- break;
382
-
383
- }/*switch-case1-switch-case0*/
384
-
385
- }/*switch-case1-switch*/
386
-
387
- break;
388
-
389
- }/*switch-case1*/
390
-
391
-
392
-
393
- /*The pattern of 2:A_e_k ≠ 0*/
394
-
395
- case 0:{
396
-
397
- for(u=0; u<lnum; u++){
398
-
399
- switch(n[e+u] == 1){
400
-
401
-
402
-
403
- /*The pattern of 2-2:A_e_p ≠ 0*/
404
-
405
- case 1:{
406
-
407
- divf(k, e+u, cnum, mat);
408
-
409
- break;
410
-
411
- }/*switch-case0-for-switch-case1*/
412
-
413
-
414
-
415
- /*The pattern of 2-1:A_e_p = 0*/
416
-
417
- case 0:{
418
-
419
- break;
420
-
421
- }/*switch-case0-for-switch-case0*/
422
-
423
- }/*switch-case0-for-switch*/
424
-
425
- }/*switch-case0-for*/
426
-
427
-
428
-
429
- break;
430
-
431
- }/*switch-case0*/
432
-
433
-
434
-
435
- }/*switch*/
436
-
437
-
438
-
439
- /**/
440
-
441
- puts("divjudgef");
442
-
443
- printf("\n");
444
-
445
- for(line=0; line<numb; line++){
446
-
447
- printf("|");
448
-
449
- for(column=0; column<numb; column++){
450
-
451
- printf("%5.2f", mat[0][line][column]);
452
-
453
- }
454
-
455
- printf(" |\n");
456
-
457
- }
458
-
459
- printf("\n\n");
460
-
461
-
462
-
463
-
464
-
465
- /**/
466
-
467
-
468
-
469
-
470
-
471
- }
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
- int rankcalf(double mat[0][numb][numb], int lnum, int cnum)
482
-
483
- {
484
-
485
-
486
-
487
- int rank = 0;
488
-
489
- int i,j;
490
-
491
- for(i=0; i<lnum; i++){
492
-
493
- for(j=0; j<numb; j++){
494
-
495
- if(mat[0][i][j] == 1){
496
-
497
- if(j != numb-1){
498
-
499
- rank++;
500
-
501
- break;
502
-
503
- }/*'j = numb-1' means the last column number of the matrix and its elements is a sentinel.*/
504
-
505
-
506
-
507
- }
508
-
509
- }
510
-
511
- }
512
-
513
-
514
-
515
- /**/
516
-
517
- puts("rankcalf");
518
-
519
- printf("\n");
520
-
521
- for(line=0; line<numb; line++){
522
-
523
- printf("|");
524
-
525
- for(column=0; column<numb; column++){
526
-
527
- printf("%5.2f", mat[0][line][column]);
528
-
529
- }
530
-
531
- printf(" |\n");
532
-
533
- }
534
-
535
- printf("\n\n");
536
-
537
-
538
-
539
-
540
-
541
- /**/
542
-
543
-
544
-
545
-
546
-
547
- return rank;
548
-
549
-
550
-
551
- }
552
-
553
-
554
-
555
- int rankopf(double mat[0][numb][numb], int lnum, int cnum, int n[])
556
-
557
- {
558
-
559
- /*
560
-
561
- We'll use substif and then use for sentence to apply dvijudgef to each column.
562
-
563
- In the for sentence, operation continues as follows.:
564
-
565
- After applying divjudgef to a particular column, the situation will be A or B.
566
-
567
- The element that is located in the upper-left side in the scope of elementary rows operations
568
-
569
- is
570
-
571
- A : 1
572
-
573
- B : 0.
574
-
575
- subtraf operation will be applied to the type A.
576
-
577
- nothing will be done to the type B.
578
-
579
- */
580
-
581
- int i = 0;/*for controling line row*/
582
-
583
- int j = 0;/*for controling column row*/
584
-
585
- for(i=0; i<lnum; i++){
586
-
587
- int detallzero = i;
588
-
589
- substif(n, mat, j, lnum);
590
-
591
- divjudgef(i, j, cnum, lnum, mat, n);
592
-
593
- if(detallzero == i){
594
-
595
- int k;
596
-
597
- for(k=i+1; k<lnum; k++){
598
-
599
- if(n[k] != 0){
600
-
601
- subtraf(k, i, minus, cnum, mat);
602
-
603
- }/*for-if-for-if*/
604
-
605
- }/*for-if-for*/
606
-
607
- }/*for-if*/
608
-
609
- j++;
610
-
611
- }/*for*/
612
-
613
-
614
-
615
- /*The operation above are all 'Elementary rows operations of matrix',
616
-
617
- and then, we will calculate the rank of the matrix.*/
618
-
619
-
620
-
621
- /**/
622
-
623
- puts("rankopf");
624
-
625
- printf("\n");
626
-
627
- for(line=0; line<numb; line++){
628
-
629
- printf("|");
630
-
631
- for(column=0; column<numb; column++){
632
-
633
- printf("%5.2f", mat[0][line][column]);
634
-
635
- }
636
-
637
- printf(" |\n");
638
-
639
- }
640
-
641
- printf("\n\n");
642
-
643
-
644
-
645
-
646
-
647
- /**/
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
- return rankcalf(mat, lnum, cnum);
656
-
657
- }
658
-
659
-
660
-
661
- int main(void)
662
-
663
- {
664
-
665
-
666
-
667
- int lnum, cnum;
668
-
669
- /*mat[kind of matrix][line number][column number] */
670
-
671
-
672
-
673
- puts("\nWe will calculate the rank of a matrix.\nPlease enter the scale of matrix.");
674
-
675
- printf("The number of lines (under %d) = ", numb);
676
-
677
- scanf("%d", &lnum);
678
-
679
- printf("The number of columns (under %d) = ", numb);
680
-
681
- scanf("%d", &cnum);
682
-
683
-
684
-
685
- puts("Please enter the elements of the matrix.");
686
-
687
- puts("'mat[i][j]' represents the (i,j) elements of the matrix.");
688
-
689
-
690
-
691
- int i, j;
692
-
693
- for(i=0; i<numb; i++){
694
-
695
- mat[0][i][numb-1] = 1;
696
-
697
- }/*Rendering mat[0][i][numb-1] sentinel for the rank calcularation.*/
698
-
699
-
700
-
701
- for(i=0; i<lnum; i++){
702
-
703
- for(j=0; j<cnum; j++){
704
-
705
- printf("mat[0][%d][%d] = ", i+1, j+1);
706
-
707
- scanf("%lf", &mat[0][i][j]);
708
-
709
- mat[1][i][j] = mat[0][i][j];
710
-
711
- }
712
-
713
- }
714
-
715
- /*mat[1]にはもともと行列が保存される。*/
716
-
717
- int n[numb];
718
-
719
- int rank = rankopf(mat, lnum, cnum, n);
720
-
721
-
722
-
723
- int r;
724
-
725
- for(r=0; r<2; r++){
726
-
727
- printf("\n");
728
-
729
- for(i=0; i<lnum; i++){
730
-
731
- printf("|");
732
-
733
- for(j=0; j<cnum; j++){
734
-
735
- printf("%5.f", mat[r][i][j]);
736
-
737
- }
738
-
739
- printf(" |\n");
740
-
741
- }
742
-
743
- printf("\n\n");
744
-
745
- }
746
-
747
-
748
-
749
- printf("\nThe rank of this matrix is %d.\n", rank);
750
-
751
-
752
-
753
- return 0;
754
-
755
- }
756
592
 
757
593
 
758
594
 
@@ -765,3 +601,7 @@
765
601
  ###試したこと
766
602
 
767
603
  上のソースコードのなかで、2つの/**/で挟まれたprintf …の部分は演算過程を見るために付け加えたものです。いくつかのデバッグは自己解決できたのですが、これに関してはわからないです。
604
+
605
+
606
+
607
+ 又、初めのアルゴリズムでは欠陥があったので、少し変えて作り直しました。

1

「試したこと」を記入しました。

2017/02/13 11:00

投稿

inxsirencer
inxsirencer

スコア16

test CHANGED
File without changes
test CHANGED
@@ -757,3 +757,11 @@
757
757
 
758
758
 
759
759
  ```
760
+
761
+
762
+
763
+
764
+
765
+ ###試したこと
766
+
767
+ 上のソースコードのなかで、2つの/**/で挟まれたprintf …の部分は演算過程を見るために付け加えたものです。いくつかのデバッグは自己解決できたのですが、これに関してはわからないです。