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