=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/engine/mat.c,v retrieving revision 1.2 retrieving revision 1.10 diff -u -p -r1.2 -r1.10 --- OpenXM_contrib2/asir2000/engine/mat.c 2000/08/21 08:31:28 1.2 +++ OpenXM_contrib2/asir2000/engine/mat.c 2003/05/22 07:01:40 1.10 @@ -23,7 +23,7 @@ * shall be made on your publication or presentation in any form of the * results obtained by use of the SOFTWARE. * (4) In the event that you modify the SOFTWARE, you shall notify FLL by - * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification + * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification * for such modification or the source code of the modified part of the * SOFTWARE. * @@ -45,33 +45,36 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/engine/mat.c,v 1.1.1.1 1999/12/03 07:39:08 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/engine/mat.c,v 1.9 2003/05/20 07:19:41 noro Exp $ */ #include "ca.h" +#include "../parse/parse.h" +extern int StrassenSize; + void addmat(vl,a,b,c) VL vl; MAT a,b,*c; { int row,col,i,j; - MAT t; - pointer *ab,*bb,*tb; - - if ( !a ) - *c = b; - else if ( !b ) - *c = a; - else if ( (a->row != b->row) || (a->col != b->col) ) { - *c = 0; error("addmat : size mismatch"); - } else { - row = a->row; col = a->col; - MKMAT(t,row,col); - for ( i = 0; i < row; i++ ) - for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i]; - j < col; j++ ) - addr(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]); - *c = t; - } + MAT t; + pointer *ab,*bb,*tb; + + if ( !a ) + *c = b; + else if ( !b ) + *c = a; + else if ( (a->row != b->row) || (a->col != b->col) ) { + *c = 0; error("addmat : size mismatch add"); + } else { + row = a->row; col = a->col; + MKMAT(t,row,col); + for ( i = 0; i < row; i++ ) + for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i]; + j < col; j++ ) + arf_add(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]); + *c = t; + } } void submat(vl,a,b,c) @@ -79,35 +82,54 @@ VL vl; MAT a,b,*c; { int row,col,i,j; - MAT t; - pointer *ab,*bb,*tb; + MAT t; + pointer *ab,*bb,*tb; - if ( !a ) - chsgnmat(b,c); - else if ( !b ) - *c = a; - else if ( (a->row != b->row) || (a->col != b->col) ) { - *c = 0; error("submat : size mismatch"); - } else { - row = a->row; col = a->col; - MKMAT(t,row,col); - for ( i = 0; i < row; i++ ) - for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i]; - j < col; j++ ) - subr(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]); - *c = t; - } + if ( !a ) + chsgnmat(b,c); + else if ( !b ) + *c = a; + else if ( (a->row != b->row) || (a->col != b->col) ) { + *c = 0; error("submat : size mismatch sub"); + } else { + row = a->row; col = a->col; + MKMAT(t,row,col); + for ( i = 0; i < row; i++ ) + for ( j = 0, ab = BDY(a)[i], bb = BDY(b)[i], tb = BDY(t)[i]; + j < col; j++ ) + arf_sub(vl,(Obj)ab[j],(Obj)bb[j],(Obj *)&tb[j]); + *c = t; + } } void mulmat(vl,a,b,c) VL vl; Obj a,b,*c; { - if ( !a || !b ) + VECT vect; + MAT mat; + + if ( !a && !b ) *c = 0; - else if ( OID(a) <= O_R ) + else if ( !a || !b ) { + if ( !a ) + a = b; + switch ( OID(a) ) { + case O_VECT: + MKVECT(vect,((VECT)a)->len); + *c = (Obj)vect; + break; + case O_MAT: + MKMAT(mat,((MAT)a)->row,((MAT)a)->col); + *c = (Obj)mat; + break; + default: + *c = 0; + break; + } + } else if ( OID(a) <= O_R || OID(a) == O_DP ) mulrmat(vl,(Obj)a,(MAT)b,(MAT *)c); - else if ( OID(b) <= O_R ) + else if ( OID(b) <= O_R || OID(b) == O_DP ) mulrmat(vl,(Obj)b,(MAT)a,(MAT *)c); else switch ( OID(a) ) { @@ -147,7 +169,7 @@ Obj a,b,*c; else if ( OID(b) > O_R ) notdef(vl,a,b,c); else { - divr(vl,(Obj)ONE,b,&t); mulrmat(vl,t,(MAT)a,(MAT *)c); + arf_div(vl,(Obj)ONE,b,&t); mulrmat(vl,t,(MAT)a,(MAT *)c); } } @@ -166,7 +188,7 @@ MAT a,*b; for ( i = 0; i < row; i++ ) for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < col; j++ ) - chsgnr((Obj)ab[j],(Obj *)&tb[j]); + arf_chsgn((Obj)ab[j],(Obj *)&tb[j]); *b = t; } } @@ -177,9 +199,22 @@ MAT a; Obj r; MAT *c; { + int n,i; + MAT t; + if ( !a ) *c = 0; - else if ( !r || !NUM(r) || !RATN(r) || + else if ( !r ) { + if ( a->row != a->col ) { + *c = 0; error("pwrmat : non square matrix"); + } else { + n = a->row; + MKMAT(t,n,n); + for ( i = 0; i < n; i++ ) + t->body[i][i] = ONE; + *c = t; + } + } else if ( !NUM(r) || !RATN(r) || !INT(r) || (SGN((Q)r)<0) || (PL(NM((Q)r))>1) ) { *c = 0; error("pwrmat : invalid exponent"); } else if ( a->row != a->col ) { @@ -226,7 +261,7 @@ MAT b,*c; for ( i = 0; i < row; i++ ) for ( j = 0, bb = BDY(b)[i], tb = BDY(t)[i]; j < col; j++ ) - mulr(vl,(Obj)a,(Obj)bb[j],(Obj *)&tb[j]); + arf_mul(vl,(Obj)a,(Obj)bb[j],(Obj *)&tb[j]); *c = t; } } @@ -235,20 +270,24 @@ void mulmatmat(vl,a,b,c) VL vl; MAT a,b,*c; { +#if 0 int arow,bcol,i,j,k,m; MAT t; pointer s,u,v; pointer *ab,*tb; + /* Mismach col and row */ if ( a->col != b->row ) { *c = 0; error("mulmat : size mismatch"); } else { arow = a->row; m = a->col; bcol = b->col; - MKMAT(t,arow,bcol); + MKMAt(t,arow,bcol); for ( i = 0; i < arow; i++ ) for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < bcol; j++ ) { for ( k = 0, s = 0; k < m; k++ ) { - mulr(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u); addr(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v; + arf_mul(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u); + arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v); + s = v; } tb[j] = s; } @@ -256,6 +295,250 @@ MAT a,b,*c; } } +void Strassen(arg, c) +NODE arg; +Obj *c; +{ + MAT a,b; + VL vl; + + /* tomo */ + a = (MAT)ARG0(arg); + b = (MAT)ARG1(arg); + vl = CO; + strassen(CO, a, b, c); +} + +void strassen(vl,a,b,c) +VL vl; +MAT a,b,*c; +{ +#endif + int arow,bcol,i,j,k,m, h, arowh, bcolh; + MAT t, a11, a12, a21, a22; + MAT p, b11, b12, b21, b22; + MAT ans1, ans2, ans3, c11, c12, c21, c22; + MAT s1, s2, t1, t2, u1, v1, w1, aa, bb; + pointer s,u,v; + pointer *ab,*tb; + int a1row,a2row, a3row,a4row, a1col, a2col, a3col, a4col; + int b1row,b2row, b3row,b4row, b1col, b2col, b3col, b4col; + int pflag1, pflag2; + /* mismach col and row */ + if ( a->col != b->row ) { + *c = 0; error("mulmat : size mismatch"); + } + else { + pflag1 = 0; pflag2 = 0; + arow = a->row; m = a->col; bcol = b->col; + arowh = arow/2; bcolh = bcol/2; + MKMAT(t,arow,bcol); + /* StrassenSize == 0 or matrix size less then StrassenSize, + then calc cannonical algorithm. */ + if((StrassenSize == 0)||(a->row<=StrassenSize || a->col <= StrassenSize) || (b->row<=StrassenSize || b->col <= StrassenSize)) { + for ( i = 0; i < arow; i++ ) + for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < bcol; j++ ) { + for ( k = 0, s = 0; k < m; k++ ) { + arf_mul(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u); + arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v); + s = v; + } + tb[j] = s; + } + *c = t; + return; + } + /* padding odd col and row to even number for zero */ + i = arow/2; + j = arow - i; + if (i != j) { + arow++; + pflag1 = 1; + } + i = m/2; + j = m - i; + if (i != j) { + m++; + pflag2 = 1; + } + MKMAT(aa, arow, m); + for (i = 0; i < a->row; i++) { + for (j = 0; j < a->col; j++) { + aa->body[i][j] = a->body[i][j]; + } + } + i = bcol/2; + j = bcol - i; + if (i != j) { + bcol++; + } + MKMAT(bb, m, bcol); + for (i = 0; i < b->row; i++) { + for ( j = 0; j < b->col; j++) { + bb->body[i][j] = b->body[i][j]; + } + } + + /* split matrix A and B */ + a1row = aa->row/2; a1col = aa->col/2; + MKMAT(a11,a1row,a1col); + MKMAT(a21,a1row,a1col); + MKMAT(a12,a1row,a1col); + MKMAT(a22,a1row,a1col); + + b1row = bb->row/2; b1col = bb->col/2; + MKMAT(b11,b1row,b1col); + MKMAT(b21,b1row,b1col); + MKMAT(b12,b1row,b1col); + MKMAT(b22,b1row,b1col); + + /* make a11 matrix */ + for (i = 0; i < a1row; i++) { + for (j = 0; j < a1col; j++) { + a11->body[i][j] = aa->body[i][j]; + } + } + + /* make a21 matrix */ + for (i = a1row; i < aa->row; i++) { + for (j = 0; j < a1col; j++) { + a21->body[i-a1row][j] = aa->body[i][j]; + } + } + + /* create a12 matrix */ + for (i = 0; i < a1row; i++) { + for (j = a1col; j < aa->col; j++) { + a12->body[i][j-a1col] = aa->body[i][j]; + } + } + + /* create a22 matrix */ + for (i = a1row; i < aa->row; i++) { + for (j = a1col; j < aa->col; j++) { + a22->body[i-a1row][j-a1col] = aa->body[i][j]; + } + } + + + /* create b11 submatrix */ + for (i = 0; i < b1row; i++) { + for (j = 0; j < b1col; j++) { + b11->body[i][j] = bb->body[i][j]; + } + } + + /* create b21 submatrix */ + for (i = b1row; i < bb->row; i++) { + for (j = 0; j < b1col; j++) { + b21->body[i-b1row][j] = bb->body[i][j]; + } + } + + /* create b12 submatrix */ + for (i = 0; i < b1row; i++) { + for (j = b1col; j < bb->col; j++) { + b12->body[i][j-b1col] = bb->body[i][j]; + } + } + + /* create b22 submatrix */ + for (i = b1row; i < bb->row; i++) { + for (j = b1col; j < bb->col; j++) { + b22->body[i-b1row][j-b1col] = bb->body[i][j]; + } + } + /* expand matrix by Strassen-Winograd algorithm */ + /* s1=A21+A22 */ + addmat(vl,a21,a22,&s1); + + /* s2=s1-A11 */ + submat(vl,s1,a11,&s2); + + /* t1=B12-B11 */ + submat(vl, b12, b11, &t1); + + /* t2=B22-t1 */ + submat(vl, b22, t1, &t2); + + /* u=(A11-A21)*(B22-B12) */ + submat(vl, a11, a21, &ans1); + submat(vl, b22, b12, &ans2); + mulmatmat(vl, ans1, ans2, &u1); + + /* v=s1*t1 */ + mulmatmat(vl, s1, t1, &v1); + + /* w=A11*B11+s2*t2 */ + mulmatmat(vl, a11, b11, &ans1); + mulmatmat(vl, s2, t2, &ans2); + addmat(vl, ans1, ans2, &w1); + + /* C11 = A11*B11+A12*B21 */ + mulmatmat(vl, a12, b21, &ans2); + addmat(vl, ans1, ans2, &c11); + + /* C12 = w1+v1+(A12-s2)*B22 */ + submat(vl, a12, s2, &ans1); + mulmatmat(vl, ans1, b22, &ans2); + addmat(vl, w1, v1, &ans1); + addmat(vl, ans1, ans2, &c12); + + /* C21 = w1+u1+A22*(B21-t2) */ + submat(vl, b21, t2, &ans1); + mulmatmat(vl, a22, ans1, &ans2); + addmat(vl, w1, u1, &ans1); + addmat(vl, ans1, ans2, &c21); + + /* C22 = w1 + u1 + v1 */ + addmat(vl, ans1, v1, &c22); + } + + for(i =0; irow; i++) { + for ( j=0; j < c11->col; j++) { + t->body[i][j] = c11->body[i][j]; + } + } + if (pflag1 == 0) { + k = c21->row; + } else { + k = c21->row - 1; + } + for(i =0; icol; j++) { + t->body[i+c11->row][j] = c21->body[i][j]; + } + } + if (pflag2 == 0) { + h = c12->col; + } else { + h = c12->col -1; + } + for(i =0; irow; i++) { + for ( j=0; j < k; j++) { + t->body[i][j+c11->col] = c12->body[i][j]; + } + } + if (pflag1 == 0) { + k = c22->row; + } else { + k = c22->row -1; + } + if (pflag2 == 0) { + h = c22->col; + } else { + h = c22->col - 1; + } + for(i =0; ibody[i+c11->row][j+c11->col] = c22->body[i][j]; + } + } + *c = t; +} + + + void mulmatvect(vl,a,b,c) VL vl; MAT a; @@ -272,11 +555,14 @@ VECT *c; else if ( a->col != b->len ) { *c = 0; error("mulmatvect : size mismatch"); } else { + for ( i = 0; i < b->len; i++ ) + if ( BDY(b)[i] && OID((Obj)BDY(b)[i]) > O_R ) + error("mulmatvect : invalid argument"); arow = a->row; m = a->col; MKVECT(t,arow); for ( i = 0; i < arow; i++ ) { for ( j = 0, s = 0, ab = BDY(a)[i]; j < m; j++ ) { - mulr(vl,(Obj)ab[j],(Obj)BDY(b)[j],(Obj *)&u); addr(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v; + arf_mul(vl,(Obj)ab[j],(Obj)BDY(b)[j],(Obj *)&u); arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v; } BDY(t)[i] = s; } @@ -299,11 +585,14 @@ VECT *c; else if ( a->len != b->row ) { *c = 0; error("mulvectmat : size mismatch"); } else { + for ( i = 0; i < a->len; i++ ) + if ( BDY(a)[i] && OID((Obj)BDY(a)[i]) > O_R ) + error("mulvectmat : invalid argument"); bcol = b->col; m = a->len; MKVECT(t,bcol); for ( j = 0; j < bcol; j++ ) { for ( i = 0, s = 0; i < m; i++ ) { - mulr(vl,(Obj)BDY(a)[i],(Obj)BDY(b)[i][j],(Obj *)&u); addr(vl,(Obj)s,(Obj)u,(Obj *)&v); s = v; + 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; } BDY(t)[j] = s; } @@ -329,7 +618,7 @@ MAT a,b; row = a->row; col = a->col; for ( i = 0; i < row; i++ ) for ( j = 0; j < col; j++ ) - if ( t = compr(vl,(Obj)BDY(a)[i][j],(Obj)BDY(b)[i][j]) ) + if ( t = arf_comp(vl,(Obj)BDY(a)[i][j],(Obj)BDY(b)[i][j]) ) return t; return 0; }