Annotation of OpenXM_contrib2/asir2000/engine/mat.c, Revision 1.12
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.12 ! saito 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/mat.c,v 1.11 2004/07/26 07:15:46 saito Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
1.4 saito 51: #include "../parse/parse.h"
52:
53: extern int StrassenSize;
1.12 ! saito 54: void mulmatmat_miser();
1.1 noro 55:
56: void addmat(vl,a,b,c)
57: VL vl;
58: MAT a,b,*c;
59: {
60: int row,col,i,j;
1.4 saito 61: MAT t;
62: pointer *ab,*bb,*tb;
63:
64: if ( !a )
65: *c = b;
66: else if ( !b )
67: *c = a;
68: else if ( (a->row != b->row) || (a->col != b->col) ) {
69: *c = 0; error("addmat : size mismatch add");
70: } else {
71: row = a->row; col = a->col;
72: MKMAT(t,row,col);
73: for ( i = 0; i < row; i++ )
74: for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i];
75: j < col; j++ )
1.9 noro 76: arf_add(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]);
1.4 saito 77: *c = t;
78: }
1.1 noro 79: }
80:
81: void submat(vl,a,b,c)
82: VL vl;
83: MAT a,b,*c;
84: {
85: int row,col,i,j;
1.4 saito 86: MAT t;
87: pointer *ab,*bb,*tb;
1.1 noro 88:
1.4 saito 89: if ( !a )
90: chsgnmat(b,c);
91: else if ( !b )
92: *c = a;
93: else if ( (a->row != b->row) || (a->col != b->col) ) {
94: *c = 0; error("submat : size mismatch sub");
95: } else {
96: row = a->row; col = a->col;
97: MKMAT(t,row,col);
98: for ( i = 0; i < row; i++ )
99: for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i];
100: j < col; j++ )
1.9 noro 101: arf_sub(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]);
1.4 saito 102: *c = t;
103: }
1.1 noro 104: }
105:
1.11 saito 106: void addmat_miser(vl,a,b,c,ar0,ac0,ar1,ac1,br0,bc0,br1,bc1)
107: VL vl;
108: MAT a,b,*c;
109: int ar0,ac0,ar1,ac1,br0,bc0,br1,bc1;
110: {
111: int row,col,i,j;
112: MAT t;
113: pointer *ab,*bb,*tb;
114: row = ar1 - ar0 + 1; col = ac1 - ac0 + 1;
115:
116: if ( !a )
117: *c = b;
118: else if ( !b )
119: *c = a;
120: else if ( (row != br1 - br0 + 1) || (col != bc1 - bc0 + 1) ) {
121: *c = 0; error("addmat : size mismatch add");
122: } else {
123: MKMAT(t,row,col);
124: for ( i = 0; i < row; i++ ) {
125: if (i+ar0 > a->row-1) {
126: ab = NULL;
127: } else {
128: ab = BDY(a)[i+ar0];
129: }
130: if (i+br0 > b->row-1) {
131: bb = NULL;
132: } else {
133: bb = BDY(b)[i+br0];
134: }
135: tb = BDY(t)[i];
136: for ( j =0; j < col; j++ ) {
137: if ((ab == NULL || j+ac0 > a->col-1) && (bb == NULL || j+bc0 > b->col-1)) {
138: arf_add(vl,NULL,NULL,(Obj *)&tb[j]);
139: } else if ((ab != NULL && j+ac0 <= a->col-1) && (bb == NULL || j+bc0 > b->col-1)){
140: arf_add(vl,(Obj)ab[j+ac0],NULL,(Obj *)&tb[j]);
141: } else if ((ab == NULL || j+ac0 > a->col-1) && (bb != NULL && j+bc0 <= b->col-1)) {
142: arf_add(vl,NULL, (Obj)bb[j+bc0],(Obj *)&tb[j]);
143: } else {
144: arf_add(vl,(Obj)ab[j+ac0],(Obj)bb[j+bc0],(Obj *)&tb[j]);
145: }
146:
147: }
148: }
149: *c = t;
150: }
151: }
152:
153: void submat_miser(vl,a,b,c,ar0,ac0,ar1,ac1,br0,bc0,br1,bc1)
154: VL vl;
155: MAT a,b,*c;
156: int ar0,ac0,ar1,ac1,br0,bc0,br1,bc1;
157: {
158: int row,col,i,j;
159: MAT t;
160: pointer *ab,*bb,*tb;
161:
162: row = ar1 - ar0 + 1; col = ac1 - ac0 + 1;
163:
164: if ( !a )
165: chsgnmat(b,c);
166: else if ( !b )
167: *c = a;
168: else if ( (row != br1 - br0 + 1) || (col != bc1 - bc0 + 1) ) {
169: *c = 0; error("submat : size mismatch sub");
170: } else {
171: MKMAT(t,row,col);
172: for ( i = 0; i < row; i++ ) {
173: if (i+ar0 > a->row-1) {
174: ab = NULL;
175: } else {
176: ab = BDY(a)[i+ar0];
177: }
178: if (i+br0 > b->row-1) {
179: bb = NULL;
180: } else {
181: bb = BDY(b)[i+br0];
182: }
183: tb = BDY(t)[i];
184: for ( j =0; j < col; j++ ) {
185: if ((ab == NULL || j+ac0 > a->col-1) && (bb == NULL || j+bc0 > b->col-1)) {
186: arf_sub(vl,NULL,NULL,(Obj *)&tb[j]);
187: } else if ((ab != NULL && j+ac0 <= a->col-1) && (bb == NULL || j+bc0 > b->col-1)){
188: arf_sub(vl,(Obj)ab[j+ac0],NULL,(Obj *)&tb[j]);
189: } else if ((ab == NULL || j+ac0 > a->col-1) && (bb != NULL && j+bc0 <= b->col-1)) {
190: arf_sub(vl,NULL, (Obj)bb[j+bc0],(Obj *)&tb[j]);
191: } else {
192: arf_sub(vl,(Obj)ab[j+ac0],(Obj)bb[j+bc0],(Obj *)&tb[j]);
193: }
194:
195: }
196: }
197: *c = t;
198: }
199: }
200:
1.1 noro 201: void mulmat(vl,a,b,c)
202: VL vl;
203: Obj a,b,*c;
204: {
1.8 noro 205: VECT vect;
206: MAT mat;
207:
208: if ( !a && !b )
1.1 noro 209: *c = 0;
1.8 noro 210: else if ( !a || !b ) {
211: if ( !a )
212: a = b;
213: switch ( OID(a) ) {
214: case O_VECT:
215: MKVECT(vect,((VECT)a)->len);
216: *c = (Obj)vect;
217: break;
218: case O_MAT:
219: MKMAT(mat,((MAT)a)->row,((MAT)a)->col);
220: *c = (Obj)mat;
221: break;
222: default:
223: *c = 0;
224: break;
225: }
1.10 noro 226: } else if ( OID(a) <= O_R || OID(a) == O_DP )
1.1 noro 227: mulrmat(vl,(Obj)a,(MAT)b,(MAT *)c);
1.10 noro 228: else if ( OID(b) <= O_R || OID(b) == O_DP )
1.1 noro 229: mulrmat(vl,(Obj)b,(MAT)a,(MAT *)c);
230: else
231: switch ( OID(a) ) {
232: case O_VECT:
233: switch ( OID(b) ) {
234: case O_MAT:
235: mulvectmat(vl,(VECT)a,(MAT)b,(VECT *)c); break;
236: case O_VECT: default:
237: notdef(vl,a,b,c); break;
238: }
239: break;
240: case O_MAT:
241: switch ( OID(b) ) {
242: case O_VECT:
243: mulmatvect(vl,(MAT)a,(VECT)b,(VECT *)c); break;
244: case O_MAT:
1.11 saito 245: mulmatmat_miser(vl,(MAT)a,(MAT)b,(MAT *)c, 0,0, ((MAT)a)->row-1, ((MAT)a)->col-1, 0,0,((MAT)b)->row-1, ((MAT)b)->col-1); break;
1.1 noro 246: default:
247: notdef(vl,a,b,c); break;
248: }
249: break;
250: default:
251: notdef(vl,a,b,c); break;
252: }
253: }
254:
255: void divmat(vl,a,b,c)
256: VL vl;
257: Obj a,b,*c;
258: {
259: Obj t;
260:
261: if ( !b )
262: error("divmat : division by 0");
263: else if ( !a )
264: *c = 0;
265: else if ( OID(b) > O_R )
266: notdef(vl,a,b,c);
267: else {
1.9 noro 268: arf_div(vl,(Obj)ONE,b,&t); mulrmat(vl,t,(MAT)a,(MAT *)c);
1.1 noro 269: }
270: }
271:
272: void chsgnmat(a,b)
273: MAT a,*b;
274: {
275: MAT t;
276: int row,col,i,j;
277: pointer *ab,*tb;
278:
279: if ( !a )
280: *b = 0;
281: else {
282: row = a->row; col = a->col;
283: MKMAT(t,row,col);
284: for ( i = 0; i < row; i++ )
285: for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i];
286: j < col; j++ )
1.9 noro 287: arf_chsgn((Obj)ab[j],(Obj *)&tb[j]);
1.1 noro 288: *b = t;
289: }
290: }
291:
292: void pwrmat(vl,a,r,c)
293: VL vl;
294: MAT a;
295: Obj r;
296: MAT *c;
297: {
1.8 noro 298: int n,i;
299: MAT t;
300:
1.1 noro 301: if ( !a )
302: *c = 0;
1.8 noro 303: else if ( !r ) {
304: if ( a->row != a->col ) {
305: *c = 0; error("pwrmat : non square matrix");
306: } else {
307: n = a->row;
308: MKMAT(t,n,n);
309: for ( i = 0; i < n; i++ )
310: t->body[i][i] = ONE;
311: *c = t;
312: }
313: } else if ( !NUM(r) || !RATN(r) ||
1.1 noro 314: !INT(r) || (SGN((Q)r)<0) || (PL(NM((Q)r))>1) ) {
315: *c = 0; error("pwrmat : invalid exponent");
316: } else if ( a->row != a->col ) {
317: *c = 0; error("pwrmat : non square matrix");
318: } else
319: pwrmatmain(vl,a,QTOS((Q)r),c);
320: }
321:
322: void pwrmatmain(vl,a,e,c)
323: VL vl;
324: MAT a;
325: int e;
326: MAT *c;
327: {
328: MAT t,s;
329:
330: if ( e == 1 ) {
331: *c = a;
332: return;
333: }
334:
335: pwrmatmain(vl,a,e/2,&t);
336: mulmat(vl,(Obj)t,(Obj)t,(Obj *)&s);
337: if ( e % 2 )
338: mulmat(vl,(Obj)s,(Obj)a,(Obj *)c);
339: else
340: *c = s;
341: }
342:
343: void mulrmat(vl,a,b,c)
344: VL vl;
345: Obj a;
346: MAT b,*c;
347: {
348: int row,col,i,j;
349: MAT t;
350: pointer *bb,*tb;
351:
352: if ( !a || !b )
353: *c = 0;
354: else {
355: row = b->row; col = b->col;
356: MKMAT(t,row,col);
357: for ( i = 0; i < row; i++ )
358: for ( j = 0, bb = BDY(b)[i], tb = BDY(t)[i];
359: j < col; j++ )
1.9 noro 360: arf_mul(vl,(Obj)a,(Obj)bb[j],(Obj *)&tb[j]);
1.1 noro 361: *c = t;
362: }
363: }
364:
365: void mulmatmat(vl,a,b,c)
366: VL vl;
367: MAT a,b,*c;
368: {
1.4 saito 369: #if 0
1.1 noro 370: int arow,bcol,i,j,k,m;
371: MAT t;
372: pointer s,u,v;
373: pointer *ab,*tb;
374:
1.5 saito 375: /* Mismach col and row */
1.1 noro 376: if ( a->col != b->row ) {
377: *c = 0; error("mulmat : size mismatch");
378: } else {
379: arow = a->row; m = a->col; bcol = b->col;
1.4 saito 380: MKMAt(t,arow,bcol);
1.1 noro 381: for ( i = 0; i < arow; i++ )
382: for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < bcol; j++ ) {
383: for ( k = 0, s = 0; k < m; k++ ) {
1.9 noro 384: arf_mul(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u);
385: arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v);
1.4 saito 386: s = v;
1.1 noro 387: }
388: tb[j] = s;
389: }
390: *c = t;
391: }
392: }
1.4 saito 393:
394: void Strassen(arg, c)
395: NODE arg;
396: Obj *c;
397: {
1.11 saito 398: AT a,b;
1.4 saito 399: VL vl;
400:
401: /* tomo */
402: a = (MAT)ARG0(arg);
403: b = (MAT)ARG1(arg);
404: vl = CO;
405: strassen(CO, a, b, c);
406: }
407:
408: void strassen(vl,a,b,c)
409: VL vl;
410: MAT a,b,*c;
411: {
412: #endif
413: int arow,bcol,i,j,k,m, h, arowh, bcolh;
414: MAT t, a11, a12, a21, a22;
415: MAT p, b11, b12, b21, b22;
416: MAT ans1, ans2, ans3, c11, c12, c21, c22;
417: MAT s1, s2, t1, t2, u1, v1, w1, aa, bb;
418: pointer s,u,v;
419: pointer *ab,*tb;
420: int a1row,a2row, a3row,a4row, a1col, a2col, a3col, a4col;
421: int b1row,b2row, b3row,b4row, b1col, b2col, b3col, b4col;
422: int pflag1, pflag2;
1.5 saito 423: /* mismach col and row */
1.4 saito 424: if ( a->col != b->row ) {
425: *c = 0; error("mulmat : size mismatch");
426: }
427: else {
428: pflag1 = 0; pflag2 = 0;
429: arow = a->row; m = a->col; bcol = b->col;
430: MKMAT(t,arow,bcol);
431: /* StrassenSize == 0 or matrix size less then StrassenSize,
1.5 saito 432: then calc cannonical algorithm. */
433: if((StrassenSize == 0)||(a->row<=StrassenSize || a->col <= StrassenSize) || (b->row<=StrassenSize || b->col <= StrassenSize)) {
1.4 saito 434: for ( i = 0; i < arow; i++ )
435: for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < bcol; j++ ) {
436: for ( k = 0, s = 0; k < m; k++ ) {
1.9 noro 437: arf_mul(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u);
438: arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v);
1.4 saito 439: s = v;
440: }
441: tb[j] = s;
442: }
443: *c = t;
444: return;
445: }
1.5 saito 446: /* padding odd col and row to even number for zero */
1.4 saito 447: i = arow/2;
448: j = arow - i;
449: if (i != j) {
450: arow++;
451: pflag1 = 1;
452: }
453: i = m/2;
454: j = m - i;
455: if (i != j) {
456: m++;
457: pflag2 = 1;
458: }
1.11 saito 459: /*
1.4 saito 460: MKMAT(aa, arow, m);
461: for (i = 0; i < a->row; i++) {
462: for (j = 0; j < a->col; j++) {
463: aa->body[i][j] = a->body[i][j];
464: }
465: }
466: i = bcol/2;
467: j = bcol - i;
468: if (i != j) {
469: bcol++;
470: }
471: MKMAT(bb, m, bcol);
472: for (i = 0; i < b->row; i++) {
473: for ( j = 0; j < b->col; j++) {
474: bb->body[i][j] = b->body[i][j];
475: }
476: }
1.11 saito 477: */
1.4 saito 478:
1.5 saito 479: /* split matrix A and B */
1.11 saito 480: a1row = arow/2; a1col = m/2;
1.4 saito 481: MKMAT(a11,a1row,a1col);
1.11 saito 482: MKMAT(a21,a1row,a1col);
483: MKMAT(a12,a1row,a1col);
484: MKMAT(a22,a1row,a1col);
1.4 saito 485:
1.11 saito 486: b1row = m/2; b1col = bcol/2;
1.4 saito 487: MKMAT(b11,b1row,b1col);
1.11 saito 488: MKMAT(b21,b1row,b1col);
489: MKMAT(b12,b1row,b1col);
490: MKMAT(b22,b1row,b1col);
1.4 saito 491:
1.5 saito 492: /* make a11 matrix */
1.4 saito 493: for (i = 0; i < a1row; i++) {
494: for (j = 0; j < a1col; j++) {
1.11 saito 495: a11->body[i][j] = a->body[i][j];
1.4 saito 496: }
497: }
498:
1.5 saito 499: /* make a21 matrix */
1.11 saito 500: for (i = a1row; i < a->row; i++) {
1.4 saito 501: for (j = 0; j < a1col; j++) {
1.11 saito 502: a21->body[i-a1row][j] = a->body[i][j];
1.4 saito 503: }
504: }
505:
1.5 saito 506: /* create a12 matrix */
1.4 saito 507: for (i = 0; i < a1row; i++) {
1.11 saito 508: for (j = a1col; j < a->col; j++) {
509: a12->body[i][j-a1col] = a->body[i][j];
1.4 saito 510: }
511: }
512:
1.5 saito 513: /* create a22 matrix */
1.11 saito 514: for (i = a1row; i < a->row; i++) {
515: for (j = a1col; j < a->col; j++) {
516: a22->body[i-a1row][j-a1col] = a->body[i][j];
1.4 saito 517: }
518: }
519:
520:
1.5 saito 521: /* create b11 submatrix */
1.4 saito 522: for (i = 0; i < b1row; i++) {
523: for (j = 0; j < b1col; j++) {
1.11 saito 524: b11->body[i][j] = b->body[i][j];
1.4 saito 525: }
526: }
527:
1.5 saito 528: /* create b21 submatrix */
1.11 saito 529: for (i = b1row; i < b->row; i++) {
1.4 saito 530: for (j = 0; j < b1col; j++) {
1.11 saito 531: b21->body[i-b1row][j] = b->body[i][j];
1.4 saito 532: }
533: }
534:
1.5 saito 535: /* create b12 submatrix */
1.4 saito 536: for (i = 0; i < b1row; i++) {
1.11 saito 537: for (j = b1col; j < b->col; j++) {
538: b12->body[i][j-b1col] = b->body[i][j];
1.4 saito 539: }
540: }
541:
1.5 saito 542: /* create b22 submatrix */
1.11 saito 543: for (i = b1row; i < b->row; i++) {
544: for (j = b1col; j < b->col; j++) {
545: b22->body[i-b1row][j-b1col] = b->body[i][j];
1.4 saito 546: }
547: }
1.5 saito 548: /* expand matrix by Strassen-Winograd algorithm */
1.4 saito 549: /* s1=A21+A22 */
550: addmat(vl,a21,a22,&s1);
551:
552: /* s2=s1-A11 */
553: submat(vl,s1,a11,&s2);
554:
555: /* t1=B12-B11 */
556: submat(vl, b12, b11, &t1);
557:
558: /* t2=B22-t1 */
559: submat(vl, b22, t1, &t2);
560:
561: /* u=(A11-A21)*(B22-B12) */
562: submat(vl, a11, a21, &ans1);
563: submat(vl, b22, b12, &ans2);
564: mulmatmat(vl, ans1, ans2, &u1);
565:
566: /* v=s1*t1 */
567: mulmatmat(vl, s1, t1, &v1);
568:
569: /* w=A11*B11+s2*t2 */
570: mulmatmat(vl, a11, b11, &ans1);
571: mulmatmat(vl, s2, t2, &ans2);
572: addmat(vl, ans1, ans2, &w1);
573:
574: /* C11 = A11*B11+A12*B21 */
575: mulmatmat(vl, a12, b21, &ans2);
576: addmat(vl, ans1, ans2, &c11);
577:
578: /* C12 = w1+v1+(A12-s2)*B22 */
579: submat(vl, a12, s2, &ans1);
580: mulmatmat(vl, ans1, b22, &ans2);
581: addmat(vl, w1, v1, &ans1);
582: addmat(vl, ans1, ans2, &c12);
583:
584: /* C21 = w1+u1+A22*(B21-t2) */
585: submat(vl, b21, t2, &ans1);
586: mulmatmat(vl, a22, ans1, &ans2);
1.6 saito 587: addmat(vl, w1, u1, &ans1);
588: addmat(vl, ans1, ans2, &c21);
589:
590: /* C22 = w1 + u1 + v1 */
591: addmat(vl, ans1, v1, &c22);
592: }
593:
594: for(i =0; i<c11->row; i++) {
595: for ( j=0; j < c11->col; j++) {
596: t->body[i][j] = c11->body[i][j];
597: }
598: }
599: if (pflag1 == 0) {
600: k = c21->row;
601: } else {
602: k = c21->row - 1;
603: }
604: for(i =0; i<k; i++) {
605: for ( j=0; j < c21->col; j++) {
606: t->body[i+c11->row][j] = c21->body[i][j];
607: }
608: }
609: if (pflag2 == 0) {
610: h = c12->col;
611: } else {
612: h = c12->col -1;
613: }
614: for(i =0; i<c12->row; i++) {
1.4 saito 615: for ( j=0; j < k; j++) {
616: t->body[i][j+c11->col] = c12->body[i][j];
617: }
618: }
619: if (pflag1 == 0) {
620: k = c22->row;
621: } else {
622: k = c22->row -1;
623: }
624: if (pflag2 == 0) {
625: h = c22->col;
626: } else {
627: h = c22->col - 1;
628: }
629: for(i =0; i<k; i++) {
630: for ( j=0; j < h; j++) {
631: t->body[i+c11->row][j+c11->col] = c22->body[i][j];
632: }
633: }
634: *c = t;
635: }
636:
1.11 saito 637: void mulmatmat_miser(vl,a,b,c,ar0,ac0,ar1,ac1,br0,bc0,br1,bc1)
638: VL vl;
639: MAT a,b,*c;
640: int ar0, ac0, ar1, ac1, br0, bc0, br1, bc1;
641: {
642: int arow,bcol,i,j,k,m, h;
643: MAT t, a11, a12, a21, a22;
644: MAT p, b11, b12, b21, b22;
645: MAT ans1, ans2, c11, c12, c21, c22;
646: MAT s1, s2, t1, t2, u1, v1, w1;
647: pointer s,u,v;
648: pointer *ab,*tb, *bb;
649: int a1row, a1col;
650: int b1row, b1col;
651: int pflag1, pflag2;
1.4 saito 652:
1.11 saito 653: arow = ar1-ar0 + 1; m = ac1-ac0 + 1; bcol = bc1 - bc0 + 1;
654: /* mismach col and row */
655: if ( m != br1-br0 + 1 ) {
656: *c = 0; error("mulmat : size mismatch");
657: }
658: else {
659: pflag1 = 0; pflag2 = 0;
660: MKMAT(t,arow,bcol);
661: /* StrassenSize == 0 or matrix size less then StrassenSize,
662: then calc cannonical algorithm. */
663: if((StrassenSize == 0)||(arow<=StrassenSize || m <= StrassenSize) || (m<=StrassenSize || bcol <= StrassenSize)) {
664: for ( i = 0; i < arow; i++ ) {
665: if (i+ar0 > a->row-1) {
666: ab = NULL;
667: } else {
668: ab = BDY(a)[i+ar0];
669: }
670: tb = BDY(t)[i];
671: for ( j = 0; j < bcol; j++ ) {
672: for ( k = 0, s = 0; k < m; k++ ) {
673: if (k+br0 > b->row-1) {
674: bb = NULL;
675: } else {
676: bb = BDY(b)[k+br0];
677: }
678: if ((ab == NULL || k+ac0 > a->col-1) && (bb == NULL || j+bc0 > b->col-1)) {
679: arf_mul(vl,NULL,NULL,(Obj *)&u);
680: } else if ((ab != NULL && k+ac0 <= a->col-1) && (bb == NULL || j+bc0 > b->col-1)){
681: arf_mul(vl,(Obj)ab[k+ac0],NULL,(Obj *)&u);
682: } else if ((ab == NULL || k+ac0 > a->col-1) && (bb != NULL && j+bc0 <= b->col-1)) {
683: arf_mul(vl,NULL,(Obj)bb[j+bc0],(Obj *)&u);
684: } else {
685: arf_mul(vl,(Obj)ab[k+ac0],(Obj)bb[j+bc0],(Obj *)&u);
686: }
687: arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v);
688: s = v;
689: }
690: tb[j] = s;
691: }
692: }
693: *c = t;
694: return;
695:
696: }
697: /* padding odd col and row to even number for zero */
698: i = arow/2;
699: j = arow - i;
700: if (i != j) {
701: arow++;
702: pflag1 = 1;
703: }
704: i = m/2;
705: j = m - i;
706: if (i != j) {
707: m++;
708: pflag2 = 1;
709: }
710:
711: i = bcol/2;
712: j = bcol - i;
713: if (i != j) {
714: bcol++;
715: }
716:
717: /* split matrix A and B */
718: a1row = arow/2; a1col = m/2;
719: b1row = m/2; b1col = bcol/2;
720:
721: /* expand matrix by Strassen-Winograd algorithm */
722: /* s1=A21+A22 */
723: addmat_miser(vl,a,a,&s1, ar0 + a1row, ac0, ar0 + arow -1, ac0 + a1col-1, ar0 + a1row, ac0 + a1col, ar0 + arow -1, ac0 + m-1);
724:
725: /* s2=s1-A11 */
726: submat_miser(vl,s1,a,&s2, 0,0, s1->row-1, s1->col-1, ar0, ac0, ar0 + a1row-1, ac0 + a1col-1);
727:
728: /* t1=B12-B11 */
729: submat_miser(vl, b, b, &t1, br0, bc0 + b1col, br0 + b1row-1, bc0 + bcol - 1, br0,bc0,br0 + b1row-1, bc0 + b1col-1);
730:
731: /* t2=B22-t1 */
732: submat_miser(vl, b, t1, &t2, br0 + b1row, bc0 + b1col, br0 + m-1, bc0 + bcol-1, 0,0,t1->row-1, t1->col-1);
733:
734: /* u=(A11-A21)*(B22-B12) */
735: submat_miser(vl, a, a, &ans1, ar0, ac0, ar0 + a1row-1,ac0 + a1col-1, ar0 + a1row, ac0, ar0 + arow-1, ac0 + a1col-1);
736: submat_miser(vl, b, b, &ans2, br0 + b1row, bc0 + b1col, br0 + m-1, bc0 + bcol-1, br0, bc0 + b1col, br0 + b1row-1, bc0 + bcol-1);
737: mulmatmat_miser(vl, ans1, ans2, &u1, 0, 0, ans1->row -1, ans1->col-1, 0, 0, ans2->row -1, ans2->col-1);
738:
739: /* v=s1*t1 */
740: mulmatmat_miser(vl, s1, t1, &v1, 0, 0, s1->row -1, s1->col-1, 0, 0, t1->row -1, t1->col-1);
741:
742: /* w=A11*B11+s2*t2 */
743: mulmatmat_miser(vl, a, b, &ans1, ar0, ac0, ar0 + a1row-1,ac0 + a1col-1, br0, bc0, br0 + b1row-1,bc0 + b1col-1);
744: mulmatmat_miser(vl, s2, t2, &ans2, 0, 0, s2->row -1, s2->col-1, 0, 0, t2->row -1, t2->col-1);
745: addmat_miser(vl, ans1, ans2, &w1, 0, 0, ans1->row -1, ans1->col-1, 0, 0, ans2->row -1, ans2->col-1);
746:
747: /* C11 = A11*B11+A12*B21 */
748: mulmatmat_miser(vl, a, b, &ans2, ar0, ac0 + a1col, ar0 + a1row-1, ac0 + m-1, br0 + b1row, bc0 + 0, br0 + m-1, bc0 + b1col-1);
749: addmat_miser(vl, ans1, ans2, &c11, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
750:
751: /* C12 = w1+v1+(A12-s2)*B22 */
752: submat_miser(vl, a, s2, &ans1, ar0, ac0 + a1col, ar0 + a1row-1, ac0 + m-1, 0, 0, s2->row -1, s2->col -1);
753: mulmatmat_miser(vl, ans1, b, &ans2, 0, 0, ans1->row -1, ans1->col -1, br0 + b1row, bc0 + b1col, br0 + m-1, bc0 + bcol-1);
754: addmat_miser(vl, w1, v1, &ans1, 0, 0, w1->row -1, w1->col -1, 0,0, v1->row-1, v1->col -1);
755: addmat_miser(vl, ans1, ans2, &c12, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
756:
757: /* C21 = w1+u1+A22*(B21-t2) */
758: submat_miser(vl, b, t2, &ans1, br0 + b1row, bc0 + 0, br0 + m-1, bc0 + b1col-1, 0,0, t2->row-1, t2->col-1);
759: mulmatmat_miser(vl, a, ans1, &ans2, ar0 + a1row, ac0 + a1col, ar0 + arow-1, ac0 + m-1, 0, 0, ans1->row -1, ans1->col -1);
760: addmat_miser(vl, w1, u1, &ans1, 0,0,w1->row -1, w1->col-1, 0,0,u1->row -1, u1->col-1);
761: addmat_miser(vl, ans1, ans2, &c21, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
762:
763: /* C22 = w1 + u1 + v1 */
764: addmat_miser(vl, ans1, v1, &c22, 0, 0, ans1->row -1, ans1->col -1, 0, 0, v1->row-1, v1->col-1);
765: }
766:
767: for(i =0; i<c11->row; i++) {
768: for ( j=0; j < c11->col; j++) {
769: t->body[i][j] = c11->body[i][j];
770: }
771: }
772: if (pflag1 == 0) {
773: k = c21->row;
774: } else {
775: k = c21->row - 1;
776: }
777: for(i =0; i<k; i++) {
778: for ( j=0; j < c21->col; j++) {
779: t->body[i+c11->row][j] = c21->body[i][j];
780: }
781: }
782: if (pflag2 == 0) {
783: h = c12->col;
784: } else {
785: h = c12->col -1;
786: }
787: for(i =0; i<c12->row; i++) {
788: for ( j=0; j < k; j++) {
789: t->body[i][j+c11->col] = c12->body[i][j];
790: }
791: }
792: if (pflag1 == 0) {
793: k = c22->row;
794: } else {
795: k = c22->row -1;
796: }
797: if (pflag2 == 0) {
798: h = c22->col;
799: } else {
800: h = c22->col - 1;
801: }
802: for(i =0; i<k; i++) {
803: for ( j=0; j < h; j++) {
804: t->body[i+c11->row][j+c11->col] = c22->body[i][j];
805: }
806: }
807: *c = t;
808: }
1.1 noro 809:
810: void mulmatvect(vl,a,b,c)
811: VL vl;
812: MAT a;
813: VECT b;
814: VECT *c;
815: {
816: int arow,i,j,m;
817: VECT t;
818: pointer s,u,v;
819: pointer *ab;
820:
821: if ( !a || !b )
822: *c = 0;
823: else if ( a->col != b->len ) {
824: *c = 0; error("mulmatvect : size mismatch");
825: } else {
1.7 noro 826: for ( i = 0; i < b->len; i++ )
827: if ( BDY(b)[i] && OID((Obj)BDY(b)[i]) > O_R )
828: error("mulmatvect : invalid argument");
1.1 noro 829: arow = a->row; m = a->col;
830: MKVECT(t,arow);
831: for ( i = 0; i < arow; i++ ) {
832: for ( j = 0, s = 0, ab = BDY(a)[i]; j < m; j++ ) {
1.9 noro 833: arf_mul(vl,(Obj)ab[j],(Obj)BDY(b)[j],(Obj *)&u); arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v;
1.1 noro 834: }
835: BDY(t)[i] = s;
836: }
837: *c = t;
838: }
839: }
840:
841: void mulvectmat(vl,a,b,c)
842: VL vl;
843: VECT a;
844: MAT b;
845: VECT *c;
846: {
847: int bcol,i,j,m;
848: VECT t;
849: pointer s,u,v;
850:
851: if ( !a || !b )
852: *c = 0;
853: else if ( a->len != b->row ) {
854: *c = 0; error("mulvectmat : size mismatch");
855: } else {
1.7 noro 856: for ( i = 0; i < a->len; i++ )
857: if ( BDY(a)[i] && OID((Obj)BDY(a)[i]) > O_R )
858: error("mulvectmat : invalid argument");
1.1 noro 859: bcol = b->col; m = a->len;
860: MKVECT(t,bcol);
861: for ( j = 0; j < bcol; j++ ) {
862: for ( i = 0, s = 0; i < m; i++ ) {
1.9 noro 863: arf_mul(vl,(Obj)BDY(a)[i],(Obj)BDY(b)[i][j],(Obj *)&u); arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v;
1.1 noro 864: }
865: BDY(t)[j] = s;
866: }
867: *c = t;
868: }
869: }
870:
871: int compmat(vl,a,b)
872: VL vl;
873: MAT a,b;
874: {
875: int i,j,t,row,col;
876:
877: if ( !a )
878: return b?-1:0;
879: else if ( !b )
880: return 1;
881: else if ( a->row != b->row )
882: return a->row>b->row ? 1 : -1;
883: else if (a->col != b->col )
884: return a->col > b->col ? 1 : -1;
885: else {
886: row = a->row; col = a->col;
887: for ( i = 0; i < row; i++ )
888: for ( j = 0; j < col; j++ )
1.9 noro 889: if ( t = arf_comp(vl,(Obj)BDY(a)[i][j],(Obj)BDY(b)[i][j]) )
1.1 noro 890: return t;
891: return 0;
892: }
893: }
894:
895: pointer **almat_pointer(n,m)
896: int n,m;
897: {
898: pointer **mat;
899: int i;
900:
901: mat = (pointer **)MALLOC(n*sizeof(pointer *));
902: for ( i = 0; i < n; i++ )
903: mat[i] = (pointer *)CALLOC(m,sizeof(pointer));
904: return mat;
905: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>