[BACK]Return to mat.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/mat.c, Revision 1.13

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.13    ! saito      48:  * $OpenXM: OpenXM_contrib2/asir2000/engine/mat.c,v 1.12 2004/08/18 06:30:07 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:        int arow,bcol,i,j,k,m, h, arowh, bcolh;
                    370:        MAT t, a11, a12, a21, a22;
                    371:        MAT p, b11, b12, b21, b22;
                    372:        MAT ans1, ans2, ans3, c11, c12, c21, c22;
                    373:        MAT s1, s2, t1, t2, u1, v1, w1, aa, bb;
                    374:        pointer s,u,v;
                    375:        pointer *ab,*tb;
                    376:        int a1row,a2row, a3row,a4row, a1col, a2col, a3col, a4col;
                    377:        int b1row,b2row, b3row,b4row, b1col, b2col, b3col, b4col;
                    378:        int pflag1, pflag2;
1.5       saito     379:        /* mismach col and row */
1.4       saito     380:        if ( a->col != b->row ) {
                    381:                *c = 0; error("mulmat : size mismatch");
                    382:        }
                    383:        else {
                    384:                pflag1 = 0; pflag2 = 0;
                    385:                arow = a->row; m = a->col; bcol = b->col;
                    386:                MKMAT(t,arow,bcol);
                    387:                /* StrassenSize == 0 or matrix size less then StrassenSize,
1.5       saito     388:                then calc cannonical algorithm. */
                    389:                if((StrassenSize == 0)||(a->row<=StrassenSize || a->col <= StrassenSize) || (b->row<=StrassenSize || b->col <= StrassenSize)) {
1.4       saito     390:                        for ( i = 0; i < arow; i++ )
                    391:                                for ( j = 0, ab = BDY(a)[i], tb = BDY(t)[i]; j < bcol; j++ ) {
                    392:                                        for ( k = 0, s = 0; k < m; k++ ) {
1.9       noro      393:                                                arf_mul(vl,(Obj)ab[k],(Obj)BDY(b)[k][j],(Obj *)&u);
                    394:                                                arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v);
1.4       saito     395:                                                s = v;
                    396:                                        }
                    397:                                        tb[j] = s;
                    398:                                }
                    399:                *c = t;
                    400:                return;
                    401:                }
1.5       saito     402:                /* padding odd col and row to even number for zero */
1.4       saito     403:                i = arow/2;
                    404:                j = arow - i;
                    405:                if (i != j) {
                    406:                        arow++;
                    407:                        pflag1 = 1;
                    408:                }
                    409:                i = m/2;
                    410:                j = m - i;
                    411:                if (i != j) {
                    412:                        m++;
                    413:                        pflag2 = 1;
                    414:                }
                    415:
1.5       saito     416:                /* split matrix A and B */
1.11      saito     417:                a1row = arow/2; a1col = m/2;
1.4       saito     418:                MKMAT(a11,a1row,a1col);
1.11      saito     419:                MKMAT(a21,a1row,a1col);
                    420:                MKMAT(a12,a1row,a1col);
                    421:                MKMAT(a22,a1row,a1col);
1.4       saito     422:
1.11      saito     423:                b1row = m/2; b1col = bcol/2;
1.4       saito     424:                MKMAT(b11,b1row,b1col);
1.11      saito     425:                MKMAT(b21,b1row,b1col);
                    426:                MKMAT(b12,b1row,b1col);
                    427:                MKMAT(b22,b1row,b1col);
1.4       saito     428:
1.5       saito     429:                /* make a11 matrix */
1.4       saito     430:                for (i = 0; i < a1row; i++) {
                    431:                        for (j = 0; j < a1col; j++) {
1.11      saito     432:                                a11->body[i][j] = a->body[i][j];
1.4       saito     433:                        }
                    434:                }
                    435:
1.5       saito     436:                /* make a21 matrix */
1.11      saito     437:                for (i = a1row; i < a->row; i++) {
1.4       saito     438:                        for (j = 0; j < a1col; j++) {
1.11      saito     439:                                a21->body[i-a1row][j] = a->body[i][j];
1.4       saito     440:                        }
                    441:                }
                    442:
1.5       saito     443:                /* create a12 matrix */
1.4       saito     444:                for (i = 0; i < a1row; i++) {
1.11      saito     445:                        for (j = a1col; j < a->col; j++) {
                    446:                                a12->body[i][j-a1col] = a->body[i][j];
1.4       saito     447:                        }
                    448:                }
                    449:
1.5       saito     450:                /* create a22 matrix */
1.11      saito     451:     for (i = a1row; i < a->row; i++) {
                    452:       for (j = a1col; j < a->col; j++) {
                    453:         a22->body[i-a1row][j-a1col] = a->body[i][j];
1.4       saito     454:       }
                    455:    }
                    456:
                    457:
1.5       saito     458:                /* create b11 submatrix */
1.4       saito     459:                for (i = 0; i < b1row; i++) {
                    460:                        for (j = 0; j < b1col; j++) {
1.11      saito     461:                                b11->body[i][j] = b->body[i][j];
1.4       saito     462:                        }
                    463:                }
                    464:
1.5       saito     465:                /* create b21 submatrix */
1.11      saito     466:                for (i = b1row; i < b->row; i++) {
1.4       saito     467:                        for (j = 0; j < b1col; j++) {
1.11      saito     468:                                b21->body[i-b1row][j] = b->body[i][j];
1.4       saito     469:                        }
                    470:                }
                    471:
1.5       saito     472:                /* create b12 submatrix */
1.4       saito     473:                for (i = 0; i < b1row; i++) {
1.11      saito     474:                        for (j = b1col; j < b->col; j++) {
                    475:                                b12->body[i][j-b1col] = b->body[i][j];
1.4       saito     476:                        }
                    477:                }
                    478:
1.5       saito     479:                /* create b22 submatrix */
1.11      saito     480:                for (i = b1row; i < b->row; i++) {
                    481:                        for (j = b1col; j < b->col; j++) {
                    482:                                b22->body[i-b1row][j-b1col] = b->body[i][j];
1.4       saito     483:                        }
                    484:                }
1.5       saito     485:                /* expand matrix by Strassen-Winograd algorithm */
1.4       saito     486:                /* s1=A21+A22 */
                    487:                addmat(vl,a21,a22,&s1);
                    488:
                    489:                /* s2=s1-A11 */
                    490:                submat(vl,s1,a11,&s2);
                    491:
                    492:                /* t1=B12-B11 */
                    493:                submat(vl, b12, b11, &t1);
                    494:
                    495:                /* t2=B22-t1 */
                    496:                submat(vl, b22, t1, &t2);
                    497:
                    498:                /* u=(A11-A21)*(B22-B12) */
                    499:                submat(vl, a11, a21, &ans1);
                    500:                submat(vl, b22, b12, &ans2);
                    501:                mulmatmat(vl, ans1, ans2, &u1);
                    502:
                    503:                /* v=s1*t1 */
                    504:                mulmatmat(vl, s1, t1, &v1);
                    505:
                    506:                /* w=A11*B11+s2*t2 */
                    507:                mulmatmat(vl, a11, b11, &ans1);
                    508:                mulmatmat(vl, s2, t2, &ans2);
                    509:                addmat(vl, ans1, ans2, &w1);
                    510:
                    511:                /* C11 = A11*B11+A12*B21 */
                    512:                mulmatmat(vl, a12, b21, &ans2);
                    513:                addmat(vl, ans1, ans2, &c11);
                    514:
                    515:                /* C12 = w1+v1+(A12-s2)*B22 */
                    516:                submat(vl, a12, s2, &ans1);
                    517:                mulmatmat(vl, ans1, b22, &ans2);
                    518:                addmat(vl, w1, v1, &ans1);
                    519:                addmat(vl, ans1, ans2, &c12);
                    520:
                    521:                /* C21 = w1+u1+A22*(B21-t2) */
                    522:                submat(vl, b21, t2, &ans1);
                    523:                mulmatmat(vl, a22, ans1, &ans2);
1.6       saito     524:                addmat(vl, w1, u1, &ans1);
                    525:                addmat(vl, ans1, ans2, &c21);
                    526:
                    527:                /* C22 = w1 + u1 + v1 */
                    528:                addmat(vl, ans1, v1, &c22);
                    529:        }
                    530:
                    531:        for(i =0; i<c11->row; i++) {
                    532:                for ( j=0; j < c11->col; j++) {
                    533:                        t->body[i][j] = c11->body[i][j];
                    534:                }
                    535:        }
                    536:        if (pflag1 == 0) {
                    537:                        k = c21->row;
                    538:        } else {
                    539:                        k = c21->row - 1;
                    540:        }
                    541:        for(i =0; i<k; i++) {
                    542:                for ( j=0; j < c21->col; j++) {
                    543:                        t->body[i+c11->row][j] = c21->body[i][j];
                    544:                }
                    545:        }
                    546:        if (pflag2 == 0) {
                    547:                h = c12->col;
                    548:        } else {
                    549:                h = c12->col -1;
                    550:        }
                    551:        for(i =0; i<c12->row; i++) {
1.4       saito     552:                for ( j=0; j < k; j++) {
                    553:                        t->body[i][j+c11->col] = c12->body[i][j];
                    554:                }
                    555:        }
                    556:        if (pflag1 == 0) {
                    557:                k = c22->row;
                    558:        } else {
                    559:                k = c22->row -1;
                    560:        }
                    561:        if (pflag2 == 0) {
                    562:                h = c22->col;
                    563:        } else {
                    564:                h = c22->col - 1;
                    565:        }
                    566:        for(i =0; i<k; i++) {
                    567:                for ( j=0; j < h; j++) {
                    568:                        t->body[i+c11->row][j+c11->col] = c22->body[i][j];
                    569:                }
                    570:        }
                    571:        *c = t;
                    572: }
                    573:
1.11      saito     574: void mulmatmat_miser(vl,a,b,c,ar0,ac0,ar1,ac1,br0,bc0,br1,bc1)
                    575: VL vl;
                    576: MAT a,b,*c;
                    577: int ar0, ac0, ar1, ac1, br0, bc0, br1, bc1;
                    578: {
                    579:        int arow,bcol,i,j,k,m, h;
                    580:        MAT t, a11, a12, a21, a22;
                    581:        MAT p, b11, b12, b21, b22;
                    582:        MAT ans1, ans2, c11, c12, c21, c22;
                    583:        MAT s1, s2, t1, t2, u1, v1, w1;
                    584:        pointer s,u,v;
                    585:        pointer *ab,*tb, *bb;
                    586:        int a1row, a1col;
                    587:        int b1row, b1col;
                    588:        int pflag1, pflag2;
1.4       saito     589:
1.11      saito     590:        arow = ar1-ar0 + 1; m = ac1-ac0 + 1; bcol = bc1 - bc0 + 1;
                    591:        /* mismach col and row */
                    592:        if ( m != br1-br0 + 1 ) {
                    593:                *c = 0; error("mulmat : size mismatch");
                    594:        }
                    595:        else {
                    596:                pflag1 = 0; pflag2 = 0;
                    597:                MKMAT(t,arow,bcol);
                    598:                /* StrassenSize == 0 or matrix size less then StrassenSize,
                    599:                then calc cannonical algorithm. */
                    600:                if((StrassenSize == 0)||(arow<=StrassenSize || m <= StrassenSize) || (m<=StrassenSize || bcol <= StrassenSize)) {
                    601:                        for ( i = 0; i < arow; i++ ) {
                    602:                                if (i+ar0 > a->row-1) {
                    603:                                        ab = NULL;
                    604:                                } else {
                    605:                ab = BDY(a)[i+ar0];
                    606:                                }
                    607:                                tb = BDY(t)[i];
                    608:                                for ( j = 0; j < bcol; j++ ) {
                    609:                                        for ( k = 0, s = 0; k < m; k++ ) {
                    610:                                                if (k+br0 > b->row-1) {
                    611:                                                        bb = NULL;
                    612:                                                } else {
                    613:                                                        bb = BDY(b)[k+br0];
                    614:                                                }
                    615:                                                if ((ab == NULL || k+ac0 > a->col-1) && (bb == NULL || j+bc0 > b->col-1)) {
                    616:                                                        arf_mul(vl,NULL,NULL,(Obj *)&u);
                    617:                                                } else if ((ab != NULL && k+ac0 <= a->col-1) && (bb == NULL || j+bc0 > b->col-1)){
                    618:                                                        arf_mul(vl,(Obj)ab[k+ac0],NULL,(Obj *)&u);
                    619:                                                } else if ((ab == NULL || k+ac0 > a->col-1) && (bb != NULL && j+bc0 <= b->col-1)) {
                    620:                                                        arf_mul(vl,NULL,(Obj)bb[j+bc0],(Obj *)&u);
                    621:                                                } else {
                    622:                                                        arf_mul(vl,(Obj)ab[k+ac0],(Obj)bb[j+bc0],(Obj *)&u);
                    623:                                                }
                    624:                                                arf_add(vl,(Obj)s,(Obj)u,(Obj *)&v);
                    625:                                                s = v;
                    626:                                        }
                    627:                                        tb[j] = s;
                    628:                                }
                    629:                        }
                    630:                *c = t;
                    631:                return;
                    632:
                    633:                }
                    634:                /* padding odd col and row to even number for zero */
                    635:                i = arow/2;
                    636:                j = arow - i;
                    637:                if (i != j) {
                    638:                        arow++;
                    639:                        pflag1 = 1;
                    640:                }
                    641:                i = m/2;
                    642:                j = m - i;
                    643:                if (i != j) {
                    644:                        m++;
                    645:                        pflag2 = 1;
                    646:                }
                    647:
                    648:                i = bcol/2;
                    649:                j = bcol - i;
                    650:                if (i != j) {
                    651:                        bcol++;
                    652:                }
                    653:
                    654:                /* split matrix A and B */
                    655:                a1row = arow/2; a1col = m/2;
                    656:                b1row = m/2; b1col = bcol/2;
                    657:
                    658:                /* expand matrix by Strassen-Winograd algorithm */
                    659:                /* s1=A21+A22 */
                    660:                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);
                    661:
                    662:                /* s2=s1-A11 */
                    663:                submat_miser(vl,s1,a,&s2, 0,0, s1->row-1, s1->col-1, ar0, ac0, ar0 + a1row-1, ac0 + a1col-1);
                    664:
                    665:                /* t1=B12-B11 */
                    666:                submat_miser(vl, b, b, &t1, br0, bc0 + b1col, br0 + b1row-1, bc0 + bcol - 1, br0,bc0,br0 + b1row-1, bc0 + b1col-1);
                    667:
                    668:                /* t2=B22-t1 */
                    669:                submat_miser(vl, b, t1, &t2, br0 + b1row, bc0 + b1col, br0 + m-1, bc0 + bcol-1, 0,0,t1->row-1, t1->col-1);
                    670:
                    671:                /* u=(A11-A21)*(B22-B12) */
                    672:                submat_miser(vl, a, a, &ans1, ar0, ac0, ar0 + a1row-1,ac0 + a1col-1, ar0 + a1row, ac0, ar0 + arow-1, ac0 + a1col-1);
                    673:                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);
                    674:                mulmatmat_miser(vl, ans1, ans2, &u1, 0, 0, ans1->row -1, ans1->col-1, 0, 0, ans2->row -1, ans2->col-1);
                    675:
                    676:                /* v=s1*t1 */
                    677:                mulmatmat_miser(vl, s1, t1, &v1, 0, 0, s1->row -1, s1->col-1, 0, 0, t1->row -1, t1->col-1);
                    678:
                    679:                /* w=A11*B11+s2*t2 */
                    680:                mulmatmat_miser(vl, a, b, &ans1, ar0, ac0, ar0 + a1row-1,ac0 + a1col-1, br0, bc0, br0 + b1row-1,bc0 + b1col-1);
                    681:                mulmatmat_miser(vl, s2, t2, &ans2, 0, 0, s2->row -1, s2->col-1, 0, 0, t2->row -1, t2->col-1);
                    682:                addmat_miser(vl, ans1, ans2, &w1, 0, 0, ans1->row -1, ans1->col-1, 0, 0, ans2->row -1, ans2->col-1);
                    683:
                    684:                /* C11 = A11*B11+A12*B21 */
                    685:                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);
                    686:                addmat_miser(vl, ans1, ans2, &c11, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
                    687:
                    688:                /* C12 = w1+v1+(A12-s2)*B22 */
                    689:                submat_miser(vl, a, s2, &ans1, ar0, ac0 + a1col, ar0 + a1row-1, ac0 + m-1, 0, 0, s2->row -1, s2->col -1);
                    690:                mulmatmat_miser(vl, ans1, b, &ans2, 0, 0, ans1->row -1, ans1->col -1, br0 + b1row, bc0 + b1col, br0 + m-1, bc0 + bcol-1);
                    691:                addmat_miser(vl, w1, v1, &ans1, 0, 0, w1->row -1, w1->col -1, 0,0, v1->row-1, v1->col -1);
                    692:                addmat_miser(vl, ans1, ans2, &c12, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
                    693:
                    694:                /* C21 = w1+u1+A22*(B21-t2) */
                    695:                submat_miser(vl, b, t2, &ans1, br0 + b1row, bc0 + 0, br0 + m-1, bc0 + b1col-1, 0,0, t2->row-1, t2->col-1);
                    696:                mulmatmat_miser(vl, a, ans1, &ans2, ar0 + a1row, ac0 + a1col, ar0 + arow-1, ac0 + m-1, 0, 0, ans1->row -1, ans1->col -1);
                    697:                addmat_miser(vl, w1, u1, &ans1, 0,0,w1->row -1, w1->col-1, 0,0,u1->row -1, u1->col-1);
                    698:                addmat_miser(vl, ans1, ans2, &c21, 0, 0, ans1->row -1, ans1->col -1, 0, 0, ans2->row -1, ans2->col-1);
                    699:
                    700:                /* C22 = w1 + u1 + v1 */
                    701:                addmat_miser(vl, ans1, v1, &c22, 0, 0, ans1->row -1, ans1->col -1, 0, 0, v1->row-1, v1->col-1);
                    702:        }
                    703:
                    704:        for(i =0; i<c11->row; i++) {
                    705:                for ( j=0; j < c11->col; j++) {
                    706:                        t->body[i][j] = c11->body[i][j];
                    707:                }
                    708:        }
                    709:        if (pflag1 == 0) {
                    710:                        k = c21->row;
                    711:        } else {
                    712:                        k = c21->row - 1;
                    713:        }
                    714:        for(i =0; i<k; i++) {
                    715:                for ( j=0; j < c21->col; j++) {
                    716:                        t->body[i+c11->row][j] = c21->body[i][j];
                    717:                }
                    718:        }
                    719:        if (pflag2 == 0) {
                    720:                h = c12->col;
                    721:        } else {
                    722:                h = c12->col -1;
                    723:        }
                    724:        for(i =0; i<c12->row; i++) {
                    725:                for ( j=0; j < k; j++) {
                    726:                        t->body[i][j+c11->col] = c12->body[i][j];
                    727:                }
                    728:        }
                    729:        if (pflag1 == 0) {
                    730:                k = c22->row;
                    731:        } else {
                    732:                k = c22->row -1;
                    733:        }
                    734:        if (pflag2 == 0) {
                    735:                h = c22->col;
                    736:        } else {
                    737:                h = c22->col - 1;
                    738:        }
                    739:        for(i =0; i<k; i++) {
                    740:                for ( j=0; j < h; j++) {
                    741:                        t->body[i+c11->row][j+c11->col] = c22->body[i][j];
                    742:                }
                    743:        }
                    744:        *c = t;
                    745: }
1.1       noro      746:
                    747: void mulmatvect(vl,a,b,c)
                    748: VL vl;
                    749: MAT a;
                    750: VECT b;
                    751: VECT *c;
                    752: {
                    753:        int arow,i,j,m;
                    754:        VECT t;
                    755:        pointer s,u,v;
                    756:        pointer *ab;
                    757:
                    758:        if ( !a || !b )
                    759:                *c = 0;
                    760:        else if ( a->col != b->len ) {
                    761:                *c = 0; error("mulmatvect : size mismatch");
                    762:        } else {
1.7       noro      763:                for ( i = 0; i < b->len; i++ )
                    764:                        if ( BDY(b)[i] && OID((Obj)BDY(b)[i]) > O_R )
                    765:                                error("mulmatvect : invalid argument");
1.1       noro      766:                arow = a->row; m = a->col;
                    767:                MKVECT(t,arow);
                    768:                for ( i = 0; i < arow; i++ ) {
                    769:                        for ( j = 0, s = 0, ab = BDY(a)[i]; j < m; j++ ) {
1.9       noro      770:                                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      771:                        }
                    772:                        BDY(t)[i] = s;
                    773:                }
                    774:                *c = t;
                    775:        }
                    776: }
                    777:
                    778: void mulvectmat(vl,a,b,c)
                    779: VL vl;
                    780: VECT a;
                    781: MAT b;
                    782: VECT *c;
                    783: {
                    784:        int bcol,i,j,m;
                    785:        VECT t;
                    786:        pointer s,u,v;
                    787:
                    788:        if ( !a || !b )
                    789:                *c = 0;
                    790:        else if ( a->len != b->row ) {
                    791:                *c = 0; error("mulvectmat : size mismatch");
                    792:        } else {
1.7       noro      793:                for ( i = 0; i < a->len; i++ )
                    794:                        if ( BDY(a)[i] && OID((Obj)BDY(a)[i]) > O_R )
                    795:                                error("mulvectmat : invalid argument");
1.1       noro      796:                bcol = b->col; m = a->len;
                    797:                MKVECT(t,bcol);
                    798:                for ( j = 0; j < bcol; j++ ) {
                    799:                        for ( i = 0, s = 0; i < m; i++ ) {
1.9       noro      800:                                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      801:                        }
                    802:                        BDY(t)[j] = s;
                    803:                }
                    804:                *c = t;
                    805:        }
                    806: }
                    807:
                    808: int compmat(vl,a,b)
                    809: VL vl;
                    810: MAT a,b;
                    811: {
                    812:        int i,j,t,row,col;
                    813:
                    814:        if ( !a )
                    815:                return b?-1:0;
                    816:        else if ( !b )
                    817:                return 1;
                    818:        else if ( a->row != b->row )
                    819:                return a->row>b->row ? 1 : -1;
                    820:        else if (a->col != b->col )
                    821:                return a->col > b->col ? 1 : -1;
                    822:        else {
                    823:                row = a->row; col = a->col;
                    824:                for ( i = 0; i < row; i++ )
                    825:                        for ( j = 0; j < col; j++ )
1.9       noro      826:                                if ( t = arf_comp(vl,(Obj)BDY(a)[i][j],(Obj)BDY(b)[i][j]) )
1.1       noro      827:                                        return t;
                    828:                return 0;
                    829:        }
                    830: }
                    831:
                    832: pointer **almat_pointer(n,m)
                    833: int n,m;
                    834: {
                    835:        pointer **mat;
                    836:        int i;
                    837:
                    838:        mat = (pointer **)MALLOC(n*sizeof(pointer *));
                    839:        for ( i = 0; i < n; i++ )
                    840:                mat[i] = (pointer *)CALLOC(m,sizeof(pointer));
                    841:        return mat;
                    842: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>