[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.14

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

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