Annotation of OpenXM/src/asir-contrib/packages/src/mtest.rr, Revision 1.4
1.4 ! takayama 1: /* $OpenXM: OpenXM/src/asir-contrib/packages/src/mtest.rr,v 1.3 2003/05/17 06:29:51 takayama Exp $ */
1.1 takayama 2:
3: /* A reasonable size program to test the new module structure. */
1.3 takayama 4: ctrl("show_crossref",1)$
1.2 takayama 5: /*
1.4 ! takayama 6: if (!Loaded_noro_print) load(FileName); else ;
1.2 takayama 7: */
8: /*
1.4 ! takayama 9: extern Loaded_noro_print$
1.2 takayama 10: */
11: /* The next two lines cannot be in a module */
1.3 takayama 12: /*
1.4 ! takayama 13: ox_loadfile(Loaded_noro_print,"noro_print.rr")$
1.2 takayama 14: ox_loadfile(Loaded_Matrix,"Matrix")$
1.3 takayama 15: */
1.2 takayama 16:
17: module lasg;
1.1 takayama 18:
1.3 takayama 19: localf matrix_to_dp, v, dp_to_matrix,
20: lasg_1, lasg_2, lt, gen_1, should_zero,
21: gen_1b, gen_rank_prob, print_matrix_list,
22: gen_eq_prob, gen_eq_nosol_prob,
23: make_dense, copy_matrix, reduced_form,
24: reduced_form_old, build_solution,
25: zero_row, kernel, gen_lineq_quiz,
26: test, gen_ans, gen_reduced_matrix;
27:
1.1 takayama 28: #define min(a,b) (a>b? b: a)
29: #define MaxN 3
30: #define Step 2
31: #define MaxC 1
1.2 takayama 32: print("Type in test(); Problems will be saved in hoge.tex")$
33: static Seed0 $
34: static Dense $
35: static Lasg_test_data_1 $
1.1 takayama 36:
1.2 takayama 37: Seed0 = 1 $
38: Dense = 1 $
1.1 takayama 39:
40: def matrix_to_dp(M) {
41: S = size(M);
42: Ans = [];
43: for (I=0; I<S[0]; I++) {
44: F = 0;
45: for (J=0; J<S[1]; J++) {
46: F = F+M[I][J]*v(J,S[1]);
47: }
48: Ans = append(Ans,[F]);
49: }
50: return(Ans);
51: }
52:
53: def v(I,N) {
54: V = newvect(N);
55: V[I] = 1;
56: return(dp_vtoe(V));
57: }
58:
59: def dp_to_matrix(FF,N) {
60: M = length(FF);
61: A = newmat(M,N);
62: for (I=0; I<M; I++) {
63: F = FF[I];
64: while (F != 0) {
65: HH = dp_hm(F);
66: H = dp_etov(HH);
67: F = F-HH;
68: for (K=0; K<N; K++) {
69: if (H[K] != 0) {
70: A[I][K] = dp_hc(HH);
71: }
72: }
73: }
74: }
75: return A;
76: }
77:
78: def lasg_1(F,I,J) {
79: G = newvect(length(F),F);
80: T = G[I];
81: G[I] = G[J];
82: G[J] = T;
83: return vtol(G);
84: }
85:
86: def lasg_2(F,I,C) {
87: G = newvect(length(F),F);
88: G[I] = G[I]*C;
89: return vtol(G);
90: }
91:
92: def lasg_3(F,I,J,C) {
93: G = newvect(length(F),F);
94: G[I] = G[I] + G[J]*C;
95: return vtol(G);
96: }
97:
98: def lt(A,B) {
99: N=size(A)[0];
100: for (I=0; I<N; I++) {
101: if (A[I] < B[I]) return(1);
102: if (A[I] > B[I]) return(0);
103: }
104: return(2);
105: }
106:
107: def gen_1(M,N,Rank,Seed) {
108: /**/ print([M,N,Rank,Seed]);
109: random(Seed);
110: if (Rank > min(M,N)) Rank = min(M,N);
111: A = newmat(M,N);
112: S = newvect(N);
113:
114: /* Determine the support */
115: for (I=0; I<Rank; I++) {
116: while (1) {
117: K = random()%N;
118: print(K);
119: if (I == 0) K = 0;
120: if (S[K] == 0) { S[K] =1 ; break; }
121: }
122: }
123:
124: for (I=0,K=0; (K<N && I<M) ; I++) {
125: while(1) {
126: if (S[K] != 0) {A[I][K] = 1; K++; break;}
127: else { K++; if (K >= N) break;}
128: }
129: }
130: return A;
131: }
132:
133: def should_zero(A,I0,J) {
134: M = size(A)[0];
135: for (I = I0+1; I<M; I++) {
136: if (A[I][J]) return(1);
137: }
138: return(0);
139: }
140:
141: def gen_1b(M,N,Rank,Seed) {
1.3 takayama 142: /* static Dense; */
1.1 takayama 143: A = gen_1(M,N,Rank,Seed);
144: for (I=0; I<Rank; I++) {
145: J=0;
146: while (J < N ) {
147: if ( A[I][J] ) {
148: J++;
149: while (J<N) {
150: if (!should_zero(A,I,J)) {
151: if (random() % 2 == 0 || Dense) {
152: A[I][J] = (random()%MaxN +1)*(-1)^(random() % N);
153: }
154: }
155: J++;
156: }
157: }
158: J++;
159: }
160: }
161: return A;
162: }
163:
164: /*&usage begin: lasg.gen_rank_prob(M,N,Rank,P)
165: It returns problems to evaluate ranks of matrices.
166: description:
167: It returns {P}
168: {M} X {N} matrix of the rank {Rank}.
169: The symbol {Step} is used to control the difficulty
170: and the global variable {seed0} is a seed for random numbers.
171: example: lasg.gen_rank_prob(3,4,2,2);
172: end: */
173: def gen_rank_prob(M,N,Rank,P) {
1.3 takayama 174: /* static Seed0; */
1.1 takayama 175: print(Seed0);
176: Ans = [ ];
177: for (K=0; K<P; K++) {
178: A = gen_1b(M,N,Rank,Seed0);
179: B = [A];
180: A = make_dense(A);
181: B = append(B,[A]);
182: F = matrix_to_dp(A);
183: for (R=0; R<Step; R++) {
184: if (random()% (Step-1) == 0) {
185: I = random()%M; J = random()%M;
186: if (I != J) {
187: F = lasg_1(F,I,J);
188: B = append(B,[[I,J],[dp_to_matrix(F,N)]]);
189: }
190: }
191: I = J = 0;
192: while (I==J) {
193: I = random()%M; J = random()%M; C = (random()%MaxN)*(-1)^(random()%3);
194: if (C == 0) C = 1;
195: if (I != J) {
196: F = lasg_3(F,I,J,C);
197: B = append(B,[[I,J,C],dp_to_matrix(F,N)]);
198: break;
199: }
200: }
201: }
202: print_matrix_list(B);
203: print("---------------------------------");
204: Ans = append(Ans,[B]);
205: }
206: return Ans;
207: }
208:
209: def print_matrix_list(B) {
210: print("_____________________________________");
211: N = length(B);
212: for (I=0; I<N; I++) {
213: print(B[I]);
214: print(" ");
215: }
216: print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
217: }
218:
219: /*&usage begin: lasg.gen_eq_prob(M,Rank,N)
220: It returns problems to solve systems of linear equations.
221: description:
222: It returns {N}
223: {M} X {M} matrix of the rank {Rank}.
224: The symbol {Step} is used to control the difficulty
225: and the global variable {seed0} is a seed for random numbers.
226: example: lasg.gen_eq_prob(4,3,2);
227: ref: lasg.gen_lineq_quiz.
228: end: */
229: def gen_eq_prob(M,Rank,P) {
1.3 takayama 230: /* extern Seed0; */
1.1 takayama 231: Ans = [ ];
232: N = M+1;
233: for (K=0; K<P; K++) {
234: A = gen_1b(M,M,Rank,Seed0);
235: AA = newmat(M,N);
236: for (I=0; I<M; I++) {
237: for (J=0; J<N-1; J++) {
238: AA[I][J] = A[I][J];
239: }
240: if (I < Rank) {
241: AA[I][N-1] = random()%MaxN + 1;
242: }
243: }
244: A = AA;
245: B = [A];
246: A = make_dense(A);
247: B = append(B,[A]);
248: F = matrix_to_dp(A);
249: for (R=0; R<Step; R++) {
250: if (random()% (Step-1) == 0) {
251: I = random()%M; J = random()%M;
252: if (I != J) {
253: F = lasg_1(F,I,J);
254: B = append(B,[[I,J],[dp_to_matrix(F,N)]]);
255: }
256: }
257: I = J = 0;
258: while (I==J) {
259: I = random()%M; J = random()%M; C = (random()%MaxN)*(-1)^(random()%3);
260: if (C == 0) C = 1;
261: if (I != J) {
262: F = lasg_3(F,I,J,C);
263: B = append(B,[[I,J,C],dp_to_matrix(F,N)]);
264: break;
265: }
266: }
267: }
268: print_matrix_list(B);
269: print("---------------------------------");
270: Ans = append(Ans,[B]);
271: }
272: return Ans;
273: }
274:
275: def gen_eq_nosol_prob(M,Rank,P) {
1.3 takayama 276: /* extern Seed0; */
1.1 takayama 277: Ans = [ ];
278: if (Rank >= M) error();
279: N = M+1;
280: for (K=0; K<P; K++) {
281: A = gen_1b(M,M,Rank,Seed0);
282: AA = newmat(M,N);
283: for (I=0; I<M; I++) {
284: for (J=0; J<N-1; J++) {
285: AA[I][J] = A[I][J];
286: }
287: if (I <= Rank) {
288: AA[I][N-1] = random()%MaxN + 1;
289: }
290: }
291: A = AA;
292: B = [A];
293: A = make_dense(A);
294: B = append(B,[A]);
295: F = matrix_to_dp(A);
296: for (R=0; R<Step; R++) {
297: if (random()% (Step-1) == 0) {
298: I = random()%M; J = random()%M;
299: if (I != J) {
300: F = lasg_1(F,I,J);
301: B = append(B,[[I,J],[dp_to_matrix(F,N)]]);
302: }
303: }
304: I = J = 0;
305: while (I==J) {
306: I = random()%M; J = random()%M; C = (random()% MaxC)*(-1)^(random()%3);
307: if (C == 0) C = 1;
308: if (I != J) {
309: F = lasg_3(F,I,J,C);
310: B = append(B,[[I,J,C],dp_to_matrix(F,N)]);
311: break;
312: }
313: }
314: }
315: print_matrix_list(B);
316: print("---------------------------------");
317: Ans = append(Ans,[B]);
318: }
319: return Ans;
320: }
321:
322: def make_dense(A) {
323: M = size(A)[0];
324: N = size(A)[1];
325: AA = newmat(M,N);
326: for (I=0; I<M; I++) {
327: for (J=0; J<N; J++) {
328: if (I != 0) {
329: AA[I][J] = A[I][J] + A[0][J];
330: }else{
331: AA[I][J] = A[I][J];
332: }
333: }
334: }
335: return AA;
336: }
337:
338: /* A = newmat(2,3,[[1,2,3],[0,1,2]])$ */
339:
340: def copy_matrix(A) {
341: M = size(A)[0];
342: N = size(A)[1];
343: AA = newmat(M,N);
344: for (I=0; I<M; I++) {
345: for (J=0; J<N; J++) {
346: AA[I][J] = A[I][J];
347: }
348: }
349: return AA;
350: }
351:
352: /*&usage begin: lasg.reduced_form(A)
353: It translates the matrix {A} into the reduced normal form
354: by elementary transformations.
355: The sequence of elementary transformations is also returned.
356: example: lasg.reduced_form(newmat(2,3,[[2,3,8],[1,2,5]]));
357: end:
358: */
359: Lasg_test_data_1=newmat(3,5,[[0,0,0,1,1],[0,0,1,0,-2],[1,3,0,0,2]]) $
360: def reduced_form(A) {
361: Ans = [ ];
362: M = size(A)[0];
363: N = size(A)[1];
364: AA = A;
365: Pivot = 0;
366: J = 0;
367: while (J < N) {
368: I = Pivot;
369: AllZero = 1;
370: while (I < M) {
371: if (AA[I][J] != 0) {
372: AllZero = 0;
373: if (AA[I][J] != 1) {
374: Msg = "Multiplying the "+rtostr(I+1)+"th row by "+
375: rtostr(1/AA[I][J]);
376: B = copy_matrix(AA);
377: for (JJ=0; JJ<N; JJ++) {
378: B[I][JJ] = AA[I][JJ]/AA[I][J];
379: }
380: Ans = append(Ans,[[Msg,B]]);
381: AA = B;
382: }
383: if (I != Pivot) {
384: Msg = "Exchanging the "+rtostr(I+1)+"th row and "+rtostr(Pivot+1)+
385: "th row";
386: B = copy_matrix(AA);
387: for (JJ=0; JJ<N; JJ++) {
388: B[I][JJ] = AA[Pivot][JJ];
389: B[Pivot][JJ] = AA[I][JJ];
390: }
391: Ans = append(Ans,[[Msg,B]]);
392: AA = B;
393: }
394: Msg = "Eliminating the "+rtostr(J+1)+"th column by "+
395: "("+rtostr(I+1)+","+rtostr(J+1)+")th entry" ;
396: B = copy_matrix(AA);
397: print(B); print("----------------");
398: Changed = 0;
399: for (II = 0; II<M; II++) {
400: if (II != Pivot && B[II][J] != 0) {
401: C = B[II][J]; Changed = 1;
402: for (JJ=0; JJ<N; JJ++) {
403: B[II][JJ] = B[II][JJ] - B[Pivot][JJ]*C;
404: }
405: }
406: }
407: if (Changed) {
408: Ans = append(Ans,[[Msg,B]]);
409: print(B); print("---------------");
410: }
411: AA = B;
412: break; /* break J loop */
413: }
414: I++;
415: }
416: if (!AllZero) Pivot++;
417: J++;
418: }
419: return(Ans);
420: }
421:
422: def reduced_form_old(A) {
423: Ans = [ ];
424: M = size(A)[0];
425: N = size(A)[1];
426: AA = A;
427: I = 0;
428: while (I < M) {
429: J = 0;
430: while (J < N) {
431: if (AA[I][J] != 0) {
432: if (AA[I][J] != 1) {
433: Msg = "Multiplying the "+rtostr(I+1)+"th row by "+
434: rtostr(1/AA[I][J]);
435: B = copy_matrix(AA);
436: for (JJ=0; JJ<N; JJ++) {
437: B[I][JJ] = AA[I][JJ]/AA[I][J];
438: }
439: Ans = append(Ans,[[Msg,B]]);
440: AA = B;
441: }
442: Msg = "Eliminating the "+rtostr(J+1)+"th column by "+
443: "("+rtostr(I+1)+","+rtostr(J+1)+")th entry" ;
444: B = copy_matrix(AA);
445: print(B); print("----------------");
446: Changed = 0;
447: for (II = 0; II<M; II++) {
448: if (II != I && B[II][J] != 0) {
449: C = B[II][J]; Changed = 1;
450: for (JJ=0; JJ<N; JJ++) {
451: B[II][JJ] = B[II][JJ] - B[I][JJ]*C;
452: }
453: }
454: }
455: if (Changed) {
456: Ans = append(Ans,[[Msg,B]]);
457: print(B); print("---------------");
458: }
459: AA = B;
460: break; /* break J loop */
461: }
462: J++;
463: }
464: I++;
465: }
466: return(Ans);
467: }
468:
469: /* Construct solutions from a matrix of the normal form.
470: [ x1 x2 ... xn b ]
471: */
472: /* Test data for build_solution */
473: /* A = newmat(3,5,[[1,1,0,1,1],
474: [0,0,1,1,2],
475: [0,0,0,0,0]])$ */
476: def build_solution(A) {
477: N =size(A);
478: M = N[0]; N=N[1];
479: B = newvect(M);
480: for (I=0; I<M; I++) {
481: B[I] = A[I][N-1];
482: }
483: /* Check if there is a solution or not. */
484: for (I=M-1; I>=0; I--) {
485: if (B[I] != 0 && zero_row(A,I)) {
486: return([]); /* No solution */
487: }
488: }
489: /* S is a solution vector for non-homogeneous problem. */
490: S = newvect(N); S[N-1] = -1;
491: for (I=M-1; I>=0; I--) {
492: V = A*S;
493: if (V[I] != 0) {
494: for (J=0; J<N-1; J++) {
495: if (A[I][J] != 0) {
496: S[J] = -V[I]/A[I][J];
497: break;
498: }
499: }
500: }
501: }
502: AA = newmat(M,N-1);
503: for (I=0; I<M; I++) {
504: for (J=0; J<N-1;J++) {
505: AA[I][J] = A[I][J];
506: }
507: }
508: K = kernel(AA);
509: SS = newvect(N-1);
510: for (I=0; I<N-1; I++) {
511: SS[I] = S[I];
512: }
513: return([SS,K]);
514: }
515:
516: def zero_row(A,I) {
517: N =size(A);
518: M = N[0]; N=N[1];
519: for (J=0; J<N-1; J++) {
520: if (A[I][J] != 0) return 0;
521: }
522: return 1;
523: }
524:
525: def kernel(A) {
526: K = omatrix_kernel(A);
527: N = K[0];
528: if (N == 0) { return [ ]; }
529: M = size(A)[1];
530: Ans = [ ];
531: for (I=0; I<N; I++) {
532: Ans = append(Ans,[ newvect(M,K[1][I]) ]);
533: }
534: return Ans;
535: }
536:
537: /*&usage begin: lasg.gen_lineq_quiz()
538: It returns a set of problems and answers
539: to solve systems of linear equations
540: in the LaTeX format.
541: description:
542: Change parameters in the program
543: P = lasg.gen_eq_prob(M,R,N);
544: where {M} X {M} is the size of the matrix, {R} is the rank, and
545: {N} is the number of problems.
546: The symbol {Step} is used to control the difficulty
547: and the global variable {seed0} is a seed for random numbers.
548: Ref: lasg.gen_rank_prob, lasg.gen_eq_prob, lasg.build_solution, lasg.test.
549: end: */
550: def gen_lineq_quiz() {
551: S = "\\documentclass{article} \\begin{document}\n";
552: N = 2; /* Number of questions */
553: P = gen_eq_prob(5,3,N); /* size of matrix and rank */
554: for (I=0; I<N; I++) {
555: PP = P[I];
556: PP = PP[length(PP)-1];
557: S += "\\noindent $\\bullet$ Make elementary transformations for \n"+
558: "$$ (A|b) = "+taka_tex_form(PP) + "$$ ";
559: S += "to solve a system of linear equations of the form $(A|b)$.\n \\medbreak \\noindent Answer:\n";
560: A = reduced_form(PP);
561: M = length(A);
562: for (J=0; J<M; J++) {
563: S += A[J][0]+", we get \\\\ \n";
564: S += "$$ "+taka_tex_form(A[J][1])+" $$\n";
565: }
566: K = build_solution(A[M-1][1]);
567: if (length(K) == 0) {
568: S += "There is no solution.\n";
569: }else{
570: S += "The solutions of the system of linear equations are \n";
571: S += "$$ "+taka_tex_form(K[0]);
572: for (J=0; J < length(K[1]); J++) {
573: S += "+ c_{"+rtostr(J+1)+"}" + taka_tex_form(K[1][J]);
574: }
575: S += " $$\n";
576: if (length(K[1])>0) {
577: S += "where $c_i$ are arbitrary constants. ";
578: }
579: }
580: S += "\\bigbreak\n";
581: }
582: S += "\\end{document}\n";
583: return(S);
584: }
585:
586: def test() {
587: S = gen_lineq_quiz();
588: shell("rm hoge.tex");
589: output("hoge.tex");
590: print(S);
591: output();
592: }
593:
594:
595: /*&uasge lasg.gen_ans(P)
596: It generates an answer of the problem of solving linear equations
597: given by {P} (extended matrix).
598: The answer is returned as a string in LaTeX format.
599: example: lasg.gen_ans([[2,3,8],[1,2,5]]);
600: end:
601: */
602: def gen_ans(PP) {
603: if (type(PP) == 4) {
604: PP = matrix_list_to_matrix(PP);
605: }
606: S = "% problem. \n";
607: S += "\\noindent $\\bullet$ Make elementary transformations for \n"+
608: "$$ (A|b) = "+taka_tex_form(PP) + "$$\n";
609: S += "to solve a system of linear equations of the form $(A|b)$.\n \\medbreak \\noindent Answer:\n";
610: A = reduced_form(PP);
611: M = length(A);
612: for (J=0; J<M; J++) {
613: S += A[J][0]+", we get \\\\ \n";
614: S += "$$ "+taka_tex_form(A[J][1])+" $$\n";
615: }
616: K = build_solution(A[M-1][1]);
617: if (length(K) == 0) {
618: S += "There is no solution.\n";
619: }else{
620: S += "The solutions of the system of linear equations are \n";
621: S += "$$ "+taka_tex_form(K[0]);
622: for (J=0; J < length(K[1]); J++) {
623: S += "+ c_{"+rtostr(J+1)+"}" + taka_tex_form(K[1][J]);
624: }
625: S += " $$\n";
626: if (length(K[1])>0) {
627: S += "where $c_i$ are arbitrary constants. ";
628: }
629: }
630: S += "\n\n";
631: return S;
632: }
633:
634: /*&uasge lasg.gen_reduced_matrix(P)
635: It generates reduced_matrix for a given matrix {P}.
636: The answer is returned as a string in LaTeX format.
637: example: lasg.gen_reduced_matrix([[0,0,0,1,1],[0,0,1,0,-2],[1,3,0,0,2]]);
638: end:
639: */
640: def gen_reduced_matrix(PP) {
641: if (type(PP) == 4) {
642: PP = matrix_list_to_matrix(PP);
643: }
644: S = "% problem. \n";
645: S += "\\noindent $\\bullet$ Make elementary transformations for \n"+
646: "$$ A = "+taka_tex_form(PP) + "$$\n";
647: S += "to generate the reduced expression.\n \\medbreak \\noindent Answer:\n";
648: A = reduced_form(PP);
649: M = length(A);
650: for (J=0; J<M; J++) {
651: S += A[J][0]+", we get \\\\ \n";
652: S += "$$ "+taka_tex_form(A[J][1])+" $$\n";
653: }
654: S += "\n\n";
655: return S;
656: }
657:
1.2 takayama 658: endmodule;
1.1 takayama 659:
660:
1.2 takayama 661: module afo;
662: static Seed$
663: Seed = 1$
1.1 takayama 664: def foo() {
1.2 takayama 665: /* extern Seed; Do not add this line. */
666: afo.foo2();
1.1 takayama 667: random(Seed);
668: while(1) {
669: A = random()%5;
670: print(A);
671: if (A == 1) return;
672: }
673: }
1.2 takayama 674: def foo2() {
675: print("hello world!");
676: }
1.1 takayama 677:
1.2 takayama 678: endmodule;
1.1 takayama 679:
1.2 takayama 680: extern Seed2 $
681: Seed2 = 1 $
1.1 takayama 682: def foo() {
683: random(Seed2);
684: while(1) {
685: A = random()%5;
686: print(A);
687: if (A == 1) return;
688: }
689: }
690:
691:
692: /*
693: example: lasg.gen_rank_prob(3,4,2,2);
694: example: lasg.gen_eq_prob(4,3,2);
695: example: lasg.reduced_form(newmat(2,3,[[2,3,8],[1,2,5]]));
696: example: lasg.gen_ans([[2,3,8],[1,2,5]]);
697: example: lasg.gen_reduced_matrix([[0,0,0,1,1],[0,0,1,0,-2],[1,3,0,0,2]]);
698: */
1.2 takayama 699:
700: /*----------------------------------------------------*/
701:
1.1 takayama 702:
703: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>