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