[BACK]Return to kanExport0.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/kanExport0.c, Revision 1.36

1.36    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/kanExport0.c,v 1.35 2004/09/15 06:40:26 takayama Exp $  */
1.1       maekawa     2: #include <stdio.h>
                      3: #include "datatype.h"
                      4: #include "stackm.h"
                      5: #include "extern.h"
                      6: #include "extern2.h"
                      7: #include "lookup.h"
                      8: #include "matrix.h"
                      9: #include "gradedset.h"
                     10: #include "kclass.h"
                     11:
                     12: #define universalToPoly(un,rp) (isZero(un)?ZERO:coeffToPoly(un,rp))
                     13:
                     14: static void checkDuplicateName(char *xvars[],char *dvars[],int n);
                     15:
                     16: static void yet() { fprintf(stderr,"Not implemented."); }
                     17:
                     18: int SerialCurrent = -1;  /* Current Serial number of the recieved packet as server. */
                     19:
                     20: int ReverseOutputOrder = 1;
                     21: int WarningNoVectorVariable = 1;
1.19      takayama   22: extern int QuoteMode;
1.1       maekawa    23:
                     24: /** :arithmetic **/
                     25: struct object KooAdd(ob1,ob2)
1.7       takayama   26:      struct object ob1,ob2;
1.1       maekawa    27: {
                     28:   extern struct ring *CurrentRingp;
                     29:   struct object rob = NullObject;
                     30:   POLY r;
                     31:   int s,i;
                     32:   objectp f1,f2,g1,g2;
                     33:   struct object nn,dd;
                     34:
                     35:   switch (Lookup[ob1.tag][ob2.tag]) {
                     36:   case SintegerSinteger:
                     37:     return(KpoInteger(ob1.lc.ival + ob2.lc.ival));
                     38:     break;
                     39:   case SpolySpoly:
                     40:     r = ppAdd(ob1.lc.poly,ob2.lc.poly);
                     41:     rob.tag = Spoly; rob.lc.poly = r;
                     42:     return(rob);
                     43:     break;
                     44:   case SarraySarray:
                     45:     s = getoaSize(ob1);
                     46:     if (s != getoaSize(ob2)) {
                     47:       errorKan1("%s\n","Two arrays must have a same size.");
                     48:     }
                     49:     rob = newObjectArray(s);
                     50:     for (i=0; i<s; i++) {
                     51:       putoa(rob,i,KooAdd(getoa(ob1,i),getoa(ob2,i)));
                     52:     }
                     53:     return(rob);
                     54:     break;
                     55:   case SuniversalNumberSuniversalNumber:
                     56:     rob.tag = SuniversalNumber;
                     57:     rob.lc.universalNumber = newUniversalNumber(0);
                     58:     Cadd(rob.lc.universalNumber,ob1.lc.universalNumber,ob2.lc.universalNumber);
                     59:     return(rob);
                     60:     break;
                     61:   case SuniversalNumberSpoly:
                     62:     rob.tag = Spoly;
                     63:     r = ob2.lc.poly;
                     64:     if (r ISZERO) {
                     65:       /*warningKan("KooAdd(universalNumber,0 polynomial) cannot determine the ring for the result. Assume the current ring.");
                     66:         rob.lc.poly = universalToPoly(ob1.lc.universalNumber,CurrentRingp);*/
                     67:       rob = ob1;
                     68:       return(rob); /* returns universal number. */
                     69:     }
                     70:     rob.lc.poly = ppAdd(universalToPoly(ob1.lc.universalNumber,r->m->ringp),r);
                     71:     return(rob);
                     72:     break;
                     73:   case SpolySuniversalNumber:
                     74:     return(KooAdd(ob2,ob1));
                     75:     break;
                     76:   case SuniversalNumberSinteger:
                     77:     rob.tag = SuniversalNumber;
                     78:     rob.lc.universalNumber = newUniversalNumber(0);
                     79:     nn.tag = SuniversalNumber;
                     80:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob2));
                     81:     Cadd(rob.lc.universalNumber,ob1.lc.universalNumber,nn.lc.universalNumber);
                     82:     return(rob);
                     83:     break;
                     84:   case SintegerSuniversalNumber:
                     85:     rob.tag = SuniversalNumber;
                     86:     rob.lc.universalNumber = newUniversalNumber(0);
                     87:     nn.tag = SuniversalNumber;
                     88:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob1));
                     89:     Cadd(rob.lc.universalNumber,nn.lc.universalNumber,ob2.lc.universalNumber);
                     90:     return(rob);
                     91:     break;
                     92:
                     93:   case SrationalFunctionSrationalFunction:
                     94:     f1 = Knumerator(ob1);
                     95:     f2 = Kdenominator(ob1);
                     96:     g1 = Knumerator(ob2);
                     97:     g2 = Kdenominator(ob2);
                     98:     nn = KooAdd(KooMult(*g2,*f1),KooMult(*f2,*g1));
                     99:     dd = KooMult(*f2,*g2);
                    100:     rob = KnewRationalFunction0(copyObjectp(&nn),copyObjectp(&dd));
                    101:     KisInvalidRational(&rob);
                    102:     return(rob);
                    103:     break;
                    104:   case SpolySrationalFunction:  /* f1 + g1/g2 = (g2 f1 + g1)/g2 */
                    105:   case SuniversalNumberSrationalFunction:
                    106:     g1 = Knumerator(ob2);
                    107:     g2 = Kdenominator(ob2);
                    108:     nn = KooAdd(KooMult(*g2,ob1),*g1);
                    109:     rob = KnewRationalFunction0(copyObjectp(&nn),g2);
                    110:     KisInvalidRational(&rob);
                    111:     return(rob);
                    112:     break;
                    113:   case SrationalFunctionSpoly:
                    114:   case SrationalFunctionSuniversalNumber:
                    115:     return(KooAdd(ob2,ob1));
                    116:     break;
                    117:   case SdoubleSdouble:
                    118:     return(KpoDouble( KopDouble(ob1) + KopDouble(ob2) ));
                    119:     break;
                    120:   case SdoubleSinteger:
                    121:   case SdoubleSuniversalNumber:
                    122:   case SdoubleSrationalFunction:
                    123:     return(KpoDouble( KopDouble(ob1) + toDouble0(ob2) ) );
                    124:     break;
                    125:   case SintegerSdouble:
                    126:   case SuniversalNumberSdouble:
                    127:   case SrationalFunctionSdouble:
                    128:     return(KpoDouble( toDouble0(ob1) + KopDouble(ob2) ) );
                    129:     break;
                    130:   case SclassSclass:
                    131:   case SclassSinteger:
                    132:   case SclassSpoly:
                    133:   case SclassSuniversalNumber:
                    134:   case SclassSrationalFunction:
                    135:   case SclassSdouble:
                    136:   case SpolySclass:
                    137:   case SintegerSclass:
                    138:   case SuniversalNumberSclass:
                    139:   case SrationalFunctionSclass:
                    140:   case SdoubleSclass:
                    141:     return(Kclass_ooAdd(ob1,ob2));
                    142:     break;
                    143:
                    144:
                    145:   default:
1.19      takayama  146:     if (QuoteMode) {
1.22      takayama  147:       rob = addTree(ob1,ob2);
1.19      takayama  148:     }else{
                    149:       warningKan("KooAdd() has not supported yet these objects.\n");
                    150:     }
1.1       maekawa   151:     break;
                    152:   }
                    153:   return(rob);
                    154: }
                    155:
                    156: struct object KooSub(ob1,ob2)
1.7       takayama  157:      struct object ob1,ob2;
1.1       maekawa   158: {
                    159:   struct object rob = NullObject;
                    160:   POLY r;
                    161:   int s,i;
                    162:   objectp f1,f2,g1,g2;
                    163:   extern struct coeff *UniversalZero;
                    164:   struct object nn,dd;
                    165:
                    166:   switch (Lookup[ob1.tag][ob2.tag]) {
                    167:   case SintegerSinteger:
                    168:     return(KpoInteger(ob1.lc.ival - ob2.lc.ival));
                    169:     break;
                    170:   case SpolySpoly:
                    171:     r = ppSub(ob1.lc.poly,ob2.lc.poly);
                    172:     rob.tag = Spoly; rob.lc.poly = r;
                    173:     return(rob);
                    174:     break;
                    175:   case SarraySarray:
                    176:     s = getoaSize(ob1);
                    177:     if (s != getoaSize(ob2)) {
                    178:       errorKan1("%s\n","Two arrays must have a same size.");
                    179:     }
                    180:     rob = newObjectArray(s);
                    181:     for (i=0; i<s; i++) {
                    182:       putoa(rob,i,KooSub(getoa(ob1,i),getoa(ob2,i)));
                    183:     }
                    184:     return(rob);
                    185:     break;
                    186:   case SuniversalNumberSuniversalNumber:
                    187:     rob.tag = SuniversalNumber;
                    188:     rob.lc.universalNumber = newUniversalNumber(0);
                    189:     Csub(rob.lc.universalNumber,ob1.lc.universalNumber,ob2.lc.universalNumber);
                    190:     return(rob);
                    191:     break;
                    192:
                    193:   case SuniversalNumberSpoly:
                    194:     rob.tag = Spoly;
                    195:     r = ob2.lc.poly;
                    196:     if (r ISZERO) {
                    197:       rob = ob1;
                    198:       return(rob); /* returns universal number. */
                    199:     }
                    200:     rob.lc.poly = ppSub(universalToPoly(ob1.lc.universalNumber,r->m->ringp),r);
                    201:     return(rob);
                    202:     break;
                    203:   case SpolySuniversalNumber:
                    204:     rob.tag = Spoly;
                    205:     r = ob1.lc.poly;
                    206:     if (r ISZERO) {
                    207:       rob.tag = SuniversalNumber;
                    208:       rob.lc.universalNumber = newUniversalNumber(0);
                    209:       Csub(rob.lc.universalNumber,UniversalZero,ob2.lc.universalNumber);
                    210:       return(rob); /* returns universal number. */
                    211:     }
                    212:     rob.lc.poly = ppSub(r,universalToPoly(ob2.lc.universalNumber,r->m->ringp));
                    213:     return(rob);
                    214:     break;
                    215:
                    216:   case SuniversalNumberSinteger:
                    217:     rob.tag = SuniversalNumber;
                    218:     rob.lc.universalNumber = newUniversalNumber(0);
                    219:     nn.tag = SuniversalNumber;
                    220:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob2));
                    221:     Csub(rob.lc.universalNumber,ob1.lc.universalNumber,nn.lc.universalNumber);
                    222:     return(rob);
                    223:     break;
                    224:   case SintegerSuniversalNumber:
                    225:     rob.tag = SuniversalNumber;
                    226:     rob.lc.universalNumber = newUniversalNumber(0);
                    227:     nn.tag = SuniversalNumber;
                    228:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob1));
                    229:     Csub(rob.lc.universalNumber,nn.lc.universalNumber,ob2.lc.universalNumber);
                    230:     return(rob);
                    231:     break;
                    232:
                    233:   case SrationalFunctionSrationalFunction:
                    234:     f1 = Knumerator(ob1);
                    235:     f2 = Kdenominator(ob1);
                    236:     g1 = Knumerator(ob2);
                    237:     g2 = Kdenominator(ob2);
                    238:     nn = KooSub(KooMult(*g2,*f1),KooMult(*f2,*g1));
                    239:     dd = KooMult(*f2,*g2);
                    240:     rob = KnewRationalFunction0(copyObjectp(&nn),copyObjectp(&dd));
                    241:     KisInvalidRational(&rob);
                    242:     return(rob);
                    243:     break;
                    244:   case SpolySrationalFunction:  /* f1 - g1/g2 = (g2 f1 - g1)/g2 */
                    245:   case SuniversalNumberSrationalFunction:
                    246:     g1 = Knumerator(ob2);
                    247:     g2 = Kdenominator(ob2);
                    248:     nn = KooSub(KooMult(*g2,ob1),*g1);
                    249:     rob = KnewRationalFunction0(copyObjectp(&nn),g2);
                    250:     KisInvalidRational(&rob);
                    251:     return(rob);
                    252:     break;
                    253:   case SrationalFunctionSpoly:
                    254:   case SrationalFunctionSuniversalNumber: /* f1/f2 - ob2= (f1 - f2*ob2)/f2 */
                    255:     f1 = Knumerator(ob1);
                    256:     f2 = Kdenominator(ob1);
                    257:     nn = KooSub(*f1,KooMult(*f2,ob2));
                    258:     rob = KnewRationalFunction0(copyObjectp(&nn),f2);
                    259:     KisInvalidRational(&rob);
                    260:     return(rob);
                    261:     break;
                    262:
                    263:   case SdoubleSdouble:
                    264:     return(KpoDouble( KopDouble(ob1) - KopDouble(ob2) ));
                    265:     break;
                    266:   case SdoubleSinteger:
                    267:   case SdoubleSuniversalNumber:
                    268:   case SdoubleSrationalFunction:
                    269:     return(KpoDouble( KopDouble(ob1) - toDouble0(ob2) ) );
                    270:     break;
                    271:   case SintegerSdouble:
                    272:   case SuniversalNumberSdouble:
                    273:   case SrationalFunctionSdouble:
                    274:     return(KpoDouble( toDouble0(ob1) - KopDouble(ob2) ) );
                    275:     break;
                    276:
                    277:   default:
1.20      takayama  278:     if (QuoteMode) {
                    279:       rob = minusTree(ob1,ob2);
                    280:     }else{
                    281:       warningKan("KooSub() has not supported yet these objects.\n");
                    282:     }
1.1       maekawa   283:     break;
                    284:   }
                    285:   return(rob);
                    286: }
                    287:
                    288: struct object KooMult(ob1,ob2)
1.7       takayama  289:      struct object ob1,ob2;
1.1       maekawa   290: {
                    291:   struct object rob = NullObject;
                    292:   POLY r;
                    293:   int i,s;
                    294:   objectp f1,f2,g1,g2;
                    295:   struct object dd,nn;
                    296:
                    297:
                    298:   switch (Lookup[ob1.tag][ob2.tag]) {
                    299:   case SintegerSinteger:
                    300:     return(KpoInteger(ob1.lc.ival * ob2.lc.ival));
                    301:     break;
                    302:   case SpolySpoly:
                    303:     r = ppMult(ob1.lc.poly,ob2.lc.poly);
                    304:     rob.tag = Spoly; rob.lc.poly = r;
                    305:     return(rob);
                    306:     break;
                    307:   case SarraySarray:
                    308:     return(KaoMult(ob1,ob2));
                    309:     break;
                    310:   case SpolySarray:
                    311:   case SuniversalNumberSarray:
                    312:   case SrationalFunctionSarray:
                    313:   case SintegerSarray:
                    314:     s = getoaSize(ob2);
                    315:     rob = newObjectArray(s);
                    316:     for (i=0; i<s; i++) {
                    317:       putoa(rob,i,KooMult(ob1,getoa(ob2,i)));
                    318:     }
                    319:     return(rob);
                    320:     break;
                    321:
                    322:   case SarraySpoly:
                    323:   case SarraySuniversalNumber:
                    324:   case SarraySrationalFunction:
                    325:   case SarraySinteger:
                    326:     s = getoaSize(ob1);
                    327:     rob = newObjectArray(s);
                    328:     for (i=0; i<s; i++) {
                    329:       putoa(rob,i,KooMult(getoa(ob1,i),ob2));
                    330:     }
                    331:     return(rob);
                    332:     break;
                    333:
                    334:
                    335:   case SuniversalNumberSuniversalNumber:
                    336:     rob.tag = SuniversalNumber;
                    337:     rob.lc.universalNumber = newUniversalNumber(0);
                    338:     Cmult(rob.lc.universalNumber,ob1.lc.universalNumber,ob2.lc.universalNumber);
                    339:     return(rob);
                    340:     break;
                    341:
                    342:   case SuniversalNumberSpoly:
                    343:     r = ob2.lc.poly;
                    344:     if (r ISZERO) {
                    345:       rob.tag = SuniversalNumber;
                    346:       rob.lc.universalNumber = newUniversalNumber(0);
                    347:       return(rob); /* returns universal number. */
                    348:     }
                    349:     if (isZero(ob1.lc.universalNumber)) {
                    350:       rob.tag = Spoly;
                    351:       rob.lc.poly = ZERO;
                    352:       return(rob);
                    353:     }
                    354:     rob.tag = Spoly;
                    355:     rob.lc.poly = ppMult(universalToPoly(ob1.lc.universalNumber,r->m->ringp),r);
                    356:     return(rob);
                    357:     break;
                    358:   case SpolySuniversalNumber:
                    359:     return(KooMult(ob2,ob1));
                    360:     break;
                    361:
                    362:   case SuniversalNumberSinteger:
                    363:     rob.tag = SuniversalNumber;
                    364:     rob.lc.universalNumber = newUniversalNumber(0);
                    365:     nn.tag = SuniversalNumber;
                    366:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob2));
                    367:     Cmult(rob.lc.universalNumber,ob1.lc.universalNumber,nn.lc.universalNumber);
                    368:     return(rob);
                    369:     break;
                    370:   case SintegerSuniversalNumber:
                    371:     rob.tag = SuniversalNumber;
                    372:     rob.lc.universalNumber = newUniversalNumber(0);
                    373:     nn.tag = SuniversalNumber;
                    374:     nn.lc.universalNumber = newUniversalNumber(KopInteger(ob1));
                    375:     Cmult(rob.lc.universalNumber,nn.lc.universalNumber,ob2.lc.universalNumber);
                    376:     return(rob);
                    377:     break;
                    378:
                    379:   case SrationalFunctionSrationalFunction:
                    380:     f1 = Knumerator(ob1);
                    381:     f2 = Kdenominator(ob1);
                    382:     g1 = Knumerator(ob2);
                    383:     g2 = Kdenominator(ob2);
                    384:     nn = KooMult(*f1,*g1);
                    385:     dd = KooMult(*f2,*g2);
                    386:     rob = KnewRationalFunction0(copyObjectp(&nn),copyObjectp(&dd));
                    387:     KisInvalidRational(&rob);
                    388:     return(rob);
                    389:     break;
                    390:   case SpolySrationalFunction:  /* ob1 g1/g2 */
                    391:   case SuniversalNumberSrationalFunction:
                    392:     g1 = Knumerator(ob2);
                    393:     g2 = Kdenominator(ob2);
                    394:     nn = KooMult(ob1,*g1);
                    395:     rob = KnewRationalFunction0(copyObjectp(&nn),g2);
                    396:     KisInvalidRational(&rob);
                    397:     return(rob);
                    398:     break;
                    399:   case SrationalFunctionSpoly:
                    400:   case SrationalFunctionSuniversalNumber: /* f1*ob2/f2 */
                    401:     f1 = Knumerator(ob1);
                    402:     f2 = Kdenominator(ob1);
                    403:     nn = KooMult(*f1,ob2);
                    404:     rob = KnewRationalFunction0(copyObjectp(&nn),f2);
                    405:     KisInvalidRational(&rob);
                    406:     return(rob);
                    407:     break;
                    408:
                    409:   case SdoubleSdouble:
                    410:     return(KpoDouble( KopDouble(ob1) * KopDouble(ob2) ));
                    411:     break;
                    412:   case SdoubleSinteger:
                    413:   case SdoubleSuniversalNumber:
                    414:   case SdoubleSrationalFunction:
                    415:     return(KpoDouble( KopDouble(ob1) * toDouble0(ob2) ) );
                    416:     break;
                    417:   case SintegerSdouble:
                    418:   case SuniversalNumberSdouble:
                    419:   case SrationalFunctionSdouble:
                    420:     return(KpoDouble( toDouble0(ob1) * KopDouble(ob2) ) );
                    421:     break;
                    422:
                    423:   default:
1.20      takayama  424:     if (QuoteMode) {
1.22      takayama  425:       rob = timesTree(ob1,ob2);
1.20      takayama  426:     }else{
                    427:       warningKan("KooMult() has not supported yet these objects.\n");
                    428:     }
1.1       maekawa   429:     break;
                    430:   }
                    431:   return(rob);
                    432: }
                    433:
                    434:
                    435:
                    436: struct object KoNegate(obj)
1.7       takayama  437:      struct object obj;
1.1       maekawa   438: {
                    439:   struct object rob = NullObject;
                    440:   extern struct ring SmallRing;
                    441:   struct object tob;
                    442:   switch(obj.tag) {
                    443:   case Sinteger:
                    444:     rob = obj;
                    445:     rob.lc.ival = -rob.lc.ival;
                    446:     break;
                    447:   case Spoly:
                    448:     rob.tag = Spoly;
                    449:     rob.lc.poly = ppSub(ZERO,obj.lc.poly);
                    450:     break;
                    451:   case SuniversalNumber:
                    452:     rob.tag = SuniversalNumber;
                    453:     rob.lc.universalNumber = coeffNeg(obj.lc.universalNumber,&SmallRing);
                    454:     break;
                    455:   case SrationalFunction:
                    456:     rob.tag = SrationalFunction;
                    457:     tob = KoNegate(*(Knumerator(obj)));
                    458:     Knumerator(rob) = copyObjectp( &tob);
                    459:     Kdenominator(rob) = Kdenominator(obj);
                    460:     break;
                    461:
                    462:   case Sdouble:
                    463:     rob = KpoDouble( - toDouble0(obj) );
                    464:     break;
                    465:
                    466:   default:
1.20      takayama  467:     if (QuoteMode) {
                    468:       rob = unaryminusTree(obj);
                    469:     }else{
                    470:       warningKan("KoNegate() has not supported yet these objects.\n");
                    471:     }
1.1       maekawa   472:     break;
                    473:   }
                    474:   return(rob);
                    475: }
                    476:
                    477: struct object KoInverse(obj)
1.7       takayama  478:      struct object obj;
1.1       maekawa   479: {
                    480:   struct object rob = NullObject;
                    481:   extern struct coeff *UniversalOne;
                    482:   objectp onep;
                    483:   struct object tob;
                    484:   switch(obj.tag) {
                    485:   case Spoly:
                    486:     tob.tag = SuniversalNumber;
                    487:     tob.lc.universalNumber = UniversalOne;
                    488:     onep = copyObjectp(& tob);
                    489:     rob = KnewRationalFunction0(onep,copyObjectp(&obj));
                    490:     KisInvalidRational(&rob);
                    491:     break;
                    492:   case SuniversalNumber:
                    493:     tob.tag = SuniversalNumber;
                    494:     tob.lc.universalNumber = UniversalOne;
                    495:     onep = copyObjectp(& tob);
                    496:     rob = KnewRationalFunction0(onep,copyObjectp(&obj));
                    497:     KisInvalidRational(&rob);
                    498:     break;
                    499:   case SrationalFunction:
                    500:     rob = obj;
                    501:     Knumerator(rob) = Kdenominator(obj);
                    502:     Kdenominator(rob) = Knumerator(obj);
                    503:     KisInvalidRational(&rob);
                    504:     break;
                    505:   default:
                    506:     warningKan("KoInverse() has not supported yet these objects.\n");
                    507:     break;
                    508:   }
                    509:   return(rob);
                    510: }
                    511:
                    512:
                    513: static int isVector(ob)
1.7       takayama  514:      struct object ob;
1.1       maekawa   515: {
                    516:   int i,n;
                    517:   n = getoaSize(ob);
                    518:   for (i=0; i<n; i++) {
                    519:     if (getoa(ob,i).tag == Sarray) return(0);
                    520:   }
                    521:   return(1);
                    522: }
                    523:
                    524: static int isMatrix(ob,m,n)
1.7       takayama  525:      struct object ob;
                    526:      int m,n;
1.1       maekawa   527: {
                    528:   int i,j;
                    529:   for (i=0; i<m; i++) {
                    530:     if (getoa(ob,i).tag != Sarray) return(0);
                    531:     if (getoaSize(getoa(ob,i)) != n) return(0);
                    532:     for (j=0; j<n; j++) {
                    533:       if (getoa(getoa(ob,i),j).tag != Spoly) return(-1);
                    534:     }
                    535:   }
                    536:   return(1);
                    537: }
                    538:
                    539:
                    540: struct object KaoMult(aa,bb)
1.7       takayama  541:      struct object aa,bb;
                    542:      /* aa and bb is assumed to be array. */
1.1       maekawa   543: {
                    544:   int m,n,m2,n2;
                    545:   int i,j,k;
                    546:   POLY tmp;
                    547:   POLY fik;
                    548:   POLY gkj;
                    549:   struct object rob;
                    550:   int r1,r2;
                    551:   int rsize;
                    552:   struct object tob;
                    553:   struct object ob1;
                    554:   extern struct ring SmallRing;
                    555:
                    556:   m = getoaSize(aa); m2 = getoaSize(bb);
                    557:   if (m == 0 || m2 == 0) errorKan1("%s\n","KaoMult(). Invalid matrix size.");
                    558:
                    559:   /*  new code for vector x vector,... etc */
                    560:   r1 = isVector(aa); r2 = isVector(bb);
                    561:   if (r1 && r2 ) { /* vector X vector ---> scalar.*/
                    562:     rsize = getoaSize(aa);
                    563:     if (rsize != getoaSize(bb)) {
                    564:       errorKan1("%s\n","KaoMult(vector,vector). The size of the vectors must be the same.");
                    565:     }
                    566:     if (r1 != 0) {
                    567:       ob1 = getoa(aa,0);
                    568:       if (ob1.tag == Spoly) {
1.7       takayama  569:         rob.tag = Spoly; rob.lc.poly = ZERO;
1.1       maekawa   570:       }else if (ob1.tag == Sinteger) {
1.7       takayama  571:         rob.tag = Sinteger; rob.lc.ival = 0;
1.1       maekawa   572:       }else {
1.7       takayama  573:         rob.tag = SuniversalNumber;
                    574:         rob.lc.universalNumber = intToCoeff(0,&SmallRing);
1.1       maekawa   575:       }
                    576:     }else{
                    577:       rob.tag = Spoly; rob.lc.poly = ZERO;
                    578:     }
                    579:     for (i=0; i<rsize; i++) {
                    580:       rob = KooAdd(rob,KooMult(getoa(aa,i),getoa(bb,i)));
                    581:     }
                    582:     return(rob);
                    583:   } else if (r1 == 0 && r2 ) { /* matrix X vector ---> vector */
1.7       takayama  584:     /* (m n) (m2=n) */
1.1       maekawa   585:     n = getoaSize(getoa(aa,0));
                    586:     if (isMatrix(aa,m,n) == 0) {
                    587:       errorKan1("%s\n","KaoMult(matrix,vector). The left object is not matrix.");
                    588:     }else if (n != m2) {
                    589:       errorKan1("%s\n","KaoMult(). Invalid matrix and vector sizes for mult.");
                    590:     } else ;
                    591:     rob = newObjectArray(m);
                    592:     for (i=0; i<m; i++) {
                    593:       getoa(rob,i) = KooMult(getoa(aa,i),bb);
                    594:     }
                    595:     return(rob);
                    596:   }else if (r1 && r2 == 0) { /* vector X matrix ---> vector */
                    597:     tob = newObjectArray(1);
                    598:     getoa(tob,0) = aa;  /* [aa] * bb and strip [ ] */
                    599:     tob = KooMult(tob,bb);
                    600:     return(getoa(tob,0));
                    601:   } else ; /* continue: matrix X matrix case. */
                    602:   /* end of new code */
                    603:
                    604:   if (getoa(aa,0).tag != Sarray || getoa(bb,0).tag != Sarray) {
                    605:     errorKan1("%s\n","KaoMult(). Matrix must be given.");
                    606:   }
                    607:   n = getoaSize(getoa(aa,0));
                    608:   n2 = getoaSize(getoa(bb,0));
                    609:   if (n != m2) errorKan1("%s\n","KaoMult(). Invalid matrix size for mult. ((p,q)X(q,r)");
                    610:   r1 = isMatrix(aa,m,n); r2 = isMatrix(bb,m2,n2);
                    611:   if (r1 == -1 || r2 == -1) {
                    612:     /* Object multiplication. Elements are not polynomials. */
                    613:     struct object ofik,ogkj,otmp;
                    614:     rob = newObjectArray(m);
                    615:     for (i=0; i<m; i++) {
                    616:       getoa(rob,i) = newObjectArray(n2);
                    617:     }
                    618:     for (i=0; i<m; i++) {
                    619:       for (j=0; j<n2; j++) {
1.7       takayama  620:         ofik = getoa(getoa(aa,i),0);
                    621:         ogkj = getoa(getoa(bb,0),j);
                    622:         otmp = KooMult( ofik, ogkj);
                    623:         for (k=1; k<n; k++) {
                    624:           ofik = getoa(getoa(aa,i),k);
                    625:           ogkj = getoa(getoa(bb,k),j);
                    626:           otmp = KooAdd(otmp, KooMult( ofik, ogkj));
                    627:         }
                    628:         getoa(getoa(rob,i),j) = otmp;
1.1       maekawa   629:       }
                    630:     }
                    631:     return(rob);
                    632:     /*errorKan1("%s\n","KaoMult().Elements of the matrix must be polynomials.");*/
                    633:   }
                    634:   if (r1 == 0 || r2 == 0)
                    635:     errorKan1("%s\n","KaoMult(). Invalid matrix form for mult.");
                    636:
                    637:   rob = newObjectArray(m);
                    638:   for (i=0; i<m; i++) {
                    639:     getoa(rob,i) = newObjectArray(n2);
                    640:   }
                    641:   for (i=0; i<m; i++) {
                    642:     for (j=0; j<n2; j++) {
                    643:       tmp = ZERO;
                    644:       for (k=0; k<n; k++) {
1.7       takayama  645:         fik = KopPOLY(getoa(getoa(aa,i),k));
                    646:         gkj = KopPOLY(getoa(getoa(bb,k),j));
                    647:         tmp = ppAdd(tmp, ppMult( fik, gkj));
1.1       maekawa   648:       }
                    649:       getoa(getoa(rob,i),j) = KpoPOLY(tmp);
                    650:     }
                    651:   }
                    652:   return(rob);
                    653: }
                    654:
                    655: struct object KooDiv(ob1,ob2)
1.7       takayama  656:      struct object ob1,ob2;
1.1       maekawa   657: {
                    658:   struct object rob = NullObject;
                    659:   switch (Lookup[ob1.tag][ob2.tag]) {
                    660:   case SintegerSinteger:
                    661:     return(KpoInteger((ob1.lc.ival) / (ob2.lc.ival)));
                    662:     break;
                    663:   case SuniversalNumberSuniversalNumber:
                    664:     rob.tag = SuniversalNumber;
                    665:     rob.lc.universalNumber = newUniversalNumber(0);
                    666:     universalNumberDiv(rob.lc.universalNumber,ob1.lc.universalNumber,
1.7       takayama  667:                        ob2.lc.universalNumber);
1.1       maekawa   668:     return(rob);
                    669:     break;
                    670:
                    671:
                    672:   default:
1.20      takayama  673:     if (QuoteMode) {
                    674:       rob = divideTree(ob1,ob2);
                    675:     }else{
                    676:       warningKan("KooDiv() has not supported yet these objects.\n");
                    677:     }
1.1       maekawa   678:     break;
                    679:   }
                    680:   return(rob);
                    681: }
                    682:
                    683: /* :relation */
                    684: KooEqualQ(obj1,obj2)
1.7       takayama  685:      struct object obj1;
                    686:      struct object obj2;
1.1       maekawa   687: {
                    688:   struct object ob;
                    689:   int i;
1.35      takayama  690:   extern int Verbose;
1.1       maekawa   691:   if (obj1.tag != obj2.tag) {
                    692:     warningKan("KooEqualQ(ob1,ob2): the datatypes of ob1 and ob2  are not same. Returns false (0).\n");
1.35      takayama  693:        if (Verbose & 0x10) {
1.36    ! takayama  694:          fprintf(stderr,"obj1(tag:%d)=",obj1.tag);
1.35      takayama  695:          printObject(obj1,0,stderr);
1.36    ! takayama  696:          fprintf(stderr,", obj2(tag:%d)=",obj2.tag);
1.35      takayama  697:          printObject(obj2,0,stderr);
                    698:          fprintf(stderr,"\n"); fflush(stderr);
                    699:        }
1.1       maekawa   700:     return(0);
                    701:   }
                    702:   switch(obj1.tag) {
1.7       takayama  703:   case 0:
                    704:     return(1); /* case of NullObject */
                    705:     break;
                    706:   case Sinteger:
                    707:     if (obj1.lc.ival == obj2.lc.ival) return(1);
                    708:     else return(0);
                    709:     break;
                    710:   case Sstring:
                    711:   case Sdollar:
                    712:     if (strcmp(obj1.lc.str, obj2.lc.str)==0) return(1);
                    713:     else return(0);
                    714:     break;
                    715:   case Spoly:
                    716:     ob = KooSub(obj1,obj2);
                    717:     if (KopPOLY(ob) == ZERO) return(1);
                    718:     else return(0);
                    719:   case Sarray:
                    720:     if (getoaSize(obj1) != getoaSize(obj2)) return(0);
                    721:     for (i=0; i< getoaSize(obj1); i++) {
                    722:       if (KooEqualQ(getoa(obj1,i),getoa(obj2,i))) { ; }
                    723:       else { return(0); }
                    724:     }
                    725:     return(1);
                    726:   case Slist:
                    727:     if (KooEqualQ(*(obj1.lc.op),*(obj2.lc.op))) {
                    728:       if (isNullList(obj1.rc.op)) {
                    729:         if (isNullList(obj2.rc.op)) return(1);
                    730:         else return(0);
1.1       maekawa   731:       }else{
1.7       takayama  732:         if (isNullList(obj2.rc.op)) return(0);
                    733:         return(KooEqualQ(*(obj1.rc.op),*(obj2.rc.op)));
1.1       maekawa   734:       }
1.7       takayama  735:     }else{
                    736:       return(0);
1.1       maekawa   737:     }
1.7       takayama  738:     break;
                    739:   case SuniversalNumber:
                    740:     return(coeffEqual(obj1.lc.universalNumber,obj2.lc.universalNumber));
                    741:     break;
                    742:   case Sring:
                    743:     return(KopRingp(obj1) == KopRingp(obj2));
                    744:     break;
                    745:   case Sclass:
                    746:     return(KclassEqualQ(obj1,obj2));
                    747:     break;
                    748:   case Sdouble:
                    749:     return(KopDouble(obj1) == KopDouble(obj2));
                    750:     break;
                    751:   default:
                    752:     errorKan1("%s\n","KooEqualQ() has not supported these objects yet.");
                    753:     break;
                    754:   }
1.1       maekawa   755: }
                    756:
                    757:
                    758: struct object KoIsPositive(ob1)
1.7       takayama  759:      struct object ob1;
1.1       maekawa   760: {
                    761:   struct object rob = NullObject;
                    762:   switch (ob1.tag) {
                    763:   case Sinteger:
                    764:     return(KpoInteger(ob1.lc.ival > 0));
                    765:     break;
                    766:   default:
                    767:     warningKan("KoIsPositive() has not supported yet these objects.\n");
                    768:     break;
                    769:   }
                    770:   return(rob);
                    771: }
                    772:
                    773: struct object KooGreater(obj1,obj2)
1.7       takayama  774:      struct object obj1;
                    775:      struct object obj2;
1.1       maekawa   776: {
                    777:   struct object ob;
                    778:   int tt;
                    779:   if (obj1.tag != obj2.tag) {
                    780:     errorKan1("%s\n","You cannot compare different kinds of objects.");
                    781:   }
                    782:   switch(obj1.tag) {
1.7       takayama  783:   case 0:
                    784:     return(KpoInteger(1)); /* case of NullObject */
                    785:     break;
                    786:   case Sinteger:
                    787:     if (obj1.lc.ival > obj2.lc.ival) return(KpoInteger(1));
                    788:     else return(KpoInteger(0));
                    789:     break;
                    790:   case Sstring:
                    791:   case Sdollar:
                    792:     if (strcmp(obj1.lc.str, obj2.lc.str)>0) return(KpoInteger(1));
                    793:     else return(KpoInteger(0));
                    794:     break;
                    795:   case Spoly:
                    796:     if ((*mmLarger)(obj1.lc.poly,obj2.lc.poly) == 1) return(KpoInteger(1));
                    797:     else return(KpoInteger(0));
                    798:     break;
                    799:   case SuniversalNumber:
                    800:     tt = coeffGreater(obj1.lc.universalNumber,obj2.lc.universalNumber);
                    801:     if (tt > 0) return(KpoInteger(1));
                    802:     else return(KpoInteger(0));
                    803:     break;
                    804:   case Sdouble:
                    805:     if ( KopDouble(obj1) > KopDouble(obj2) ) return(KpoInteger(1));
                    806:     else return(KpoInteger(0));
                    807:     break;
1.26      takayama  808:   case Sarray:
                    809:   {
                    810:     int i,m1,m2;
                    811:     struct object rr;
                    812:     m1 = getoaSize(obj1); m2 = getoaSize(obj2);
                    813:     for (i=0; i< (m1>m2?m2:m1); i++) {
                    814:       rr=KooGreater(getoa(obj1,i),getoa(obj2,i));
                    815:       if (KopInteger(rr) == 1) return rr;
                    816:       rr=KooGreater(getoa(obj2,i),getoa(obj1,i));
                    817:       if (KopInteger(rr) == 1) return KpoInteger(0);
                    818:     }
                    819:     if (m1 > m2) return KpoInteger(1);
                    820:     else return KpoInteger(0);
                    821:   }
                    822:   break;
1.7       takayama  823:   default:
                    824:     errorKan1("%s\n","KooGreater() has not supported these objects yet.");
                    825:     break;
                    826:   }
1.1       maekawa   827: }
                    828:
                    829: struct object KooLess(obj1,obj2)
1.7       takayama  830:      struct object obj1;
                    831:      struct object obj2;
1.1       maekawa   832: {
                    833:   struct object ob;
                    834:   int tt;
                    835:   if (obj1.tag != obj2.tag) {
                    836:     errorKan1("%s\n","You cannot compare different kinds of objects.");
                    837:   }
                    838:   switch(obj1.tag) {
1.7       takayama  839:   case 0:
                    840:     return(KpoInteger(1)); /* case of NullObject */
                    841:     break;
                    842:   case Sinteger:
                    843:     if (obj1.lc.ival < obj2.lc.ival) return(KpoInteger(1));
                    844:     else return(KpoInteger(0));
                    845:     break;
                    846:   case Sstring:
                    847:   case Sdollar:
                    848:     if (strcmp(obj1.lc.str, obj2.lc.str)<0) return(KpoInteger(1));
                    849:     else return(KpoInteger(0));
                    850:     break;
                    851:   case Spoly:
                    852:     if ((*mmLarger)(obj2.lc.poly,obj1.lc.poly) == 1) return(KpoInteger(1));
                    853:     else return(KpoInteger(0));
                    854:     break;
                    855:   case SuniversalNumber:
                    856:     tt = coeffGreater(obj1.lc.universalNumber,obj2.lc.universalNumber);
                    857:     if (tt < 0) return(KpoInteger(1));
                    858:     else return(KpoInteger(0));
                    859:     break;
                    860:   case Sdouble:
                    861:     if ( KopDouble(obj1) < KopDouble(obj2) ) return(KpoInteger(1));
                    862:     else return(KpoInteger(0));
                    863:     break;
1.26      takayama  864:   case Sarray:
                    865:   {
                    866:     int i,m1,m2;
                    867:     struct object rr;
                    868:     m1 = getoaSize(obj1); m2 = getoaSize(obj2);
                    869:     for (i=0; i< (m1>m2?m2:m1); i++) {
                    870:       rr=KooLess(getoa(obj1,i),getoa(obj2,i));
                    871:       if (KopInteger(rr) == 1) return rr;
                    872:       rr=KooLess(getoa(obj2,i),getoa(obj1,i));
                    873:       if (KopInteger(rr) == 1) return KpoInteger(0);
                    874:     }
                    875:     if (m1 < m2) return KpoInteger(1);
                    876:     else return KpoInteger(0);
                    877:   }
                    878:   break;
1.7       takayama  879:   default:
                    880:     errorKan1("%s\n","KooLess() has not supported these objects yet.");
                    881:     break;
                    882:   }
1.1       maekawa   883: }
                    884:
                    885: /* :conversion */
                    886:
                    887: struct object KdataConversion(obj,key)
1.7       takayama  888:      struct object obj;
                    889:      char *key;
1.1       maekawa   890: {
                    891:   char tmps[128]; /* Assume that double is not more than 128 digits */
                    892:   char intstr[100]; /* Assume that int is not more than 100 digits */
                    893:   struct object rob;
                    894:   extern struct ring *CurrentRingp;
                    895:   extern struct ring SmallRing;
                    896:   int flag;
                    897:   struct object rob1,rob2;
                    898:   char *s;
                    899:   int i;
1.2       takayama  900:   double f;
                    901:   double f2;
1.1       maekawa   902:   /* reports the data type */
                    903:   if (key[0] == 't' || key[0] =='e') {
                    904:     if (strcmp(key,"type?")==0) {
                    905:       rob = KpoInteger(obj.tag);
                    906:       return(rob);
                    907:     }else if (strcmp(key,"type??")==0) {
                    908:       if (obj.tag != Sclass) {
1.7       takayama  909:         rob = KpoInteger(obj.tag);
1.1       maekawa   910:       }else {
1.7       takayama  911:         rob = KpoInteger(ectag(obj));
1.1       maekawa   912:       }
                    913:       return(rob);
                    914:     }else if (strcmp(key,"error")==0) {
                    915:       rob = KnewErrorPacketObj(obj);
                    916:       return(rob);
                    917:     }
                    918:   }
                    919:   switch(obj.tag) {
                    920:   case Snull:
                    921:     if (strcmp(key,"integer") == 0) {
                    922:       rob = KpoInteger(0);
                    923:       return(rob);
                    924:     }else if (strcmp(key,"universalNumber") == 0) {
                    925:       rob.tag = SuniversalNumber;
                    926:       rob.lc.universalNumber = intToCoeff(obj.lc.ival,&SmallRing);
                    927:       return(rob);
                    928:     }else if (strcmp(key,"poly") == 0) {
                    929:       rob = KpoPOLY(ZERO);
1.32      takayama  930:       return rob;
                    931:     }else if (strcmp(key,"array") == 0) {
                    932:       rob = newObjectArray(0);
                    933:       return rob;
1.1       maekawa   934:     }else{
                    935:       warningKan("Sorry. The data conversion from null to this data type has not supported yet.\n");
                    936:     }
                    937:     break;
                    938:   case Sinteger:
                    939:     if (strcmp(key,"string") == 0) { /* ascii code */
                    940:       rob.tag = Sdollar;
                    941:       rob.lc.str = (char *)sGC_malloc(2);
                    942:       if (rob.lc.str == (char *)NULL) errorKan1("%s","No more memory.\n");
                    943:       (rob.lc.str)[0] = obj.lc.ival; (rob.lc.str)[1] = '\0';
                    944:       return(rob);
                    945:     }else if (strcmp(key,"integer")==0) {
                    946:       return(obj);
                    947:     }else if (strcmp(key,"poly") == 0) {
                    948:       rob.tag = Spoly;
                    949:       rob.lc.poly = cxx(obj.lc.ival,0,0,CurrentRingp);
                    950:       return(rob);
                    951:     }else if (strcmp(key,"dollar") == 0) {
                    952:       rob.tag = Sdollar;
                    953:       sprintf(intstr,"%d",obj.lc.ival);
                    954:       rob.lc.str = (char *)sGC_malloc(strlen(intstr)+2);
                    955:       if (rob.lc.str == (char *)NULL) errorKan1("%s","No more memory.\n");
                    956:       strcpy(rob.lc.str,intstr);
                    957:       return(rob);
                    958:     }else if (strcmp(key,"universalNumber")==0) {
1.25      takayama  959:       rob = KintToUniversalNumber(obj.lc.ival);
1.1       maekawa   960:       return(rob);
                    961:     }else if (strcmp(key,"double") == 0) {
                    962:       rob = KpoDouble((double) (obj.lc.ival));
                    963:       return(rob);
                    964:     }else if (strcmp(key,"null") == 0) {
                    965:       rob = NullObject;
                    966:       return(rob);
                    967:     }else{
                    968:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                    969:     }
                    970:     break;
                    971:   case Sdollar:
                    972:     if (strcmp(key,"dollar") == 0 || strcmp(key,"string")==0) {
                    973:       rob = obj;
                    974:       return(rob);
                    975:     }else if (strcmp(key,"literal") == 0) {
                    976:       rob.tag = Sstring;
                    977:       s = (char *) sGC_malloc(sizeof(char)*(strlen(obj.lc.str)+3));
                    978:       if (s == (char *) NULL)   {
1.7       takayama  979:         errorKan1("%s\n","No memory.");
1.1       maekawa   980:       }
                    981:       s[0] = '/';
                    982:       strcpy(&(s[1]),obj.lc.str);
                    983:       rob.lc.str = &(s[1]);
                    984:       /* set the hashing value. */
                    985:       rob2 = lookupLiteralString(s);
                    986:       rob.rc.op = rob2.lc.op;
                    987:       return(rob);
                    988:     }else if (strcmp(key,"poly")==0) {
                    989:       rob.tag = Spoly;
                    990:       rob.lc.poly = stringToPOLY(obj.lc.str,CurrentRingp);
                    991:       return(rob);
                    992:     }else if (strcmp(key,"array")==0) {
                    993:       rob = newObjectArray(strlen(obj.lc.str));
                    994:       for (i=0; i<strlen(obj.lc.str); i++) {
1.7       takayama  995:         putoa(rob,i,KpoInteger((obj.lc.str)[i]));
1.1       maekawa   996:       }
                    997:       return(rob);
                    998:     }else if (strcmp(key,"universalNumber") == 0) {
                    999:       rob.tag = SuniversalNumber;
                   1000:       rob.lc.universalNumber = stringToUniversalNumber(obj.lc.str,&flag);
                   1001:       if (flag == -1) errorKan1("KdataConversion(): %s",
1.7       takayama 1002:                                 "It's not number.\n");
1.2       takayama 1003:       return(rob);
                   1004:     }else if (strcmp(key,"double") == 0) {
                   1005:       /* Check the format.  2.3432 e2 is not allowed. It should be 2.3232e2.*/
                   1006:       flag = 0;
                   1007:       for (i=0; (obj.lc.str)[i] != '\0'; i++) {
1.7       takayama 1008:         if ((obj.lc.str)[i] > ' ' && flag == 0) flag=1;
                   1009:         else if ((obj.lc.str)[i] <= ' ' && flag == 1) flag = 2;
                   1010:         else if ((obj.lc.str)[i] > ' ' && flag == 2) flag=3;
1.2       takayama 1011:       }
                   1012:       if (flag == 3) errorKan1("KdataConversion(): %s","The data for the double contains blanck(s)");
                   1013:       /* Read the double. */
                   1014:       if (sscanf(obj.lc.str,"%lf",&f) <= 0) {
1.7       takayama 1015:         errorKan1("KdataConversion(): %s","It cannot be translated to double.");
1.2       takayama 1016:       }
                   1017:       rob = KpoDouble(f);
1.1       maekawa  1018:       return(rob);
                   1019:     }else if (strcmp(key,"null") == 0) {
                   1020:       rob = NullObject;
                   1021:       return(rob);
                   1022:     }else{
                   1023:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1024:     }
                   1025:     break;
                   1026:   case Sarray:
                   1027:     if (strcmp(key,"array") == 0) {
                   1028:       return(rob);
                   1029:     }else if (strcmp(key,"list") == 0) {
1.32      takayama 1030:       rob = KarrayToList(obj);
1.1       maekawa  1031:       return(rob);
                   1032:     }else if (strcmp(key,"arrayOfPOLY")==0) {
                   1033:       rob = KpoArrayOfPOLY(arrayToArrayOfPOLY(obj));
                   1034:       return(rob);
                   1035:     }else if (strcmp(key,"matrixOfPOLY")==0) {
                   1036:       rob = KpoMatrixOfPOLY(arrayToMatrixOfPOLY(obj));
                   1037:       return(rob);
                   1038:     }else if (strcmp(key,"gradedPolySet")==0) {
                   1039:       rob = KpoGradedPolySet(arrayToGradedPolySet(obj));
                   1040:       return(rob);
                   1041:     }else if (strcmp(key,"null") == 0) {
                   1042:       rob = NullObject;
                   1043:       return(rob);
                   1044:     }else {
1.23      takayama 1045:          { /* Automatically maps the elements. */
                   1046:                int n,i;
                   1047:                n = getoaSize(obj);
                   1048:                rob = newObjectArray(n);
                   1049:                for (i=0; i<n; i++) {
                   1050:                  putoa(rob,i,KdataConversion(getoa(obj,i),key));
                   1051:                }
                   1052:                return(rob);
                   1053:          }
1.1       maekawa  1054:     }
                   1055:     break;
                   1056:   case Spoly:
1.15      takayama 1057:     if ((strcmp(key,"poly")==0) || (strcmp(key,"numerator")==0)) {
1.5       takayama 1058:       rob = obj;
1.1       maekawa  1059:       return(rob);
                   1060:     }else if (strcmp(key,"integer")==0) {
                   1061:       if (obj.lc.poly == ZERO) return(KpoInteger(0));
                   1062:       else {
1.7       takayama 1063:         return(KpoInteger(coeffToInt(obj.lc.poly->coeffp)));
1.1       maekawa  1064:       }
                   1065:     }else if (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1066:       rob.tag = Sdollar;
                   1067:       rob.lc.str = KPOLYToString(KopPOLY(obj));
                   1068:       return(rob);
                   1069:     }else if (strcmp(key,"array") == 0) {
                   1070:       return( POLYToArray(KopPOLY(obj)));
                   1071:     }else if (strcmp(key,"map")==0) {
                   1072:       return(KringMap(obj));
                   1073:     }else if (strcmp(key,"universalNumber")==0) {
                   1074:       if (obj.lc.poly == ZERO) {
1.7       takayama 1075:         rob.tag = SuniversalNumber;
                   1076:         rob.lc.universalNumber = newUniversalNumber(0);
1.1       maekawa  1077:       } else {
1.7       takayama 1078:         if (obj.lc.poly->coeffp->tag == MP_INTEGER) {
                   1079:           rob.tag = SuniversalNumber;
                   1080:           rob.lc.universalNumber = newUniversalNumber2(obj.lc.poly->coeffp->val.bigp);
                   1081:         }else {
                   1082:           rob = NullObject;
                   1083:           warningKan("Coefficient is not MP_INT.");
                   1084:         }
1.1       maekawa  1085:       }
                   1086:       return(rob);
                   1087:     }else if (strcmp(key,"ring")==0) {
                   1088:       if (obj.lc.poly ISZERO) {
1.7       takayama 1089:         warningKan("Zero polynomial does not have the ring structure field.\n");
1.1       maekawa  1090:       }else{
1.7       takayama 1091:         rob.tag = Sring;
                   1092:         rob.lc.ringp = (obj.lc.poly)->m->ringp;
                   1093:         return(rob);
1.1       maekawa  1094:       }
                   1095:     }else if (strcmp(key,"null") == 0) {
                   1096:       rob = NullObject;
                   1097:       return(rob);
                   1098:     }else{
                   1099:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1100:     }
                   1101:     break;
                   1102:   case SarrayOfPOLY:
                   1103:     if (strcmp(key,"array")==0) {
                   1104:       rob = arrayOfPOLYToArray(KopArrayOfPOLYp(obj));
                   1105:       return(rob);
                   1106:     }else{
                   1107:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1108:     }
                   1109:     break;
                   1110:   case SmatrixOfPOLY:
                   1111:     if (strcmp(key,"array")==0) {
                   1112:       rob = matrixOfPOLYToArray(KopMatrixOfPOLYp(obj));
                   1113:       return(rob);
                   1114:     }else if (strcmp(key,"null") == 0) {
                   1115:       rob = NullObject;
                   1116:       return(rob);
                   1117:     }else{
                   1118:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1119:     }
                   1120:     break;
                   1121:   case Slist:
                   1122:     if (strcmp(key,"array") == 0) {
1.32      takayama 1123:       rob = KlistToArray(obj);
1.1       maekawa  1124:       return(rob);
                   1125:     }
                   1126:     break;
                   1127:   case SuniversalNumber:
1.15      takayama 1128:     if ((strcmp(key,"universalNumber")==0) || (strcmp(key,"numerator")==0)) {
1.27      takayama 1129:       rob = obj;
1.1       maekawa  1130:       return(rob);
                   1131:     }else if (strcmp(key,"integer")==0) {
                   1132:       rob = KpoInteger(coeffToInt(obj.lc.universalNumber));
                   1133:       return(rob);
                   1134:     }else if (strcmp(key,"poly")==0) {
                   1135:       rob = KpoPOLY(universalToPoly(obj.lc.universalNumber,CurrentRingp));
                   1136:       return(rob);
                   1137:     }else if (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1138:       rob.tag = Sdollar;
                   1139:       rob.lc.str = coeffToString(obj.lc.universalNumber);
                   1140:       return(rob);
                   1141:     }else if (strcmp(key,"null") == 0) {
                   1142:       rob = NullObject;
                   1143:       return(rob);
                   1144:     }else if (strcmp(key,"double") == 0) {
                   1145:       rob = KpoDouble( toDouble0(obj) );
                   1146:       return(rob);
1.25      takayama 1147:     }else if (strcmp(key,"denominator") == 0) {
                   1148:       rob = KintToUniversalNumber(1);
                   1149:       return(rob);
1.1       maekawa  1150:     }else{
                   1151:       warningKan("Sorry. This type of data conversion of universalNumber has not supported yet.\n");
                   1152:     }
                   1153:     break;
                   1154:   case SrationalFunction:
                   1155:     if (strcmp(key,"rationalFunction")==0) {
                   1156:       return(rob);
                   1157:     } if (strcmp(key,"numerator")==0) {
                   1158:       rob = *(Knumerator(obj));
                   1159:       return(rob);
                   1160:     }else if  (strcmp(key,"denominator")==0) {
                   1161:       rob = *(Kdenominator(obj));
                   1162:       return(rob);
                   1163:     }else if  (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1164:       rob1 = KdataConversion(*(Knumerator(obj)),"string");
                   1165:       rob2 = KdataConversion(*(Kdenominator(obj)),"string");
                   1166:       s = sGC_malloc(sizeof(char)*( strlen(rob1.lc.str) + strlen(rob2.lc.str) + 10));
                   1167:       if (s == (char *)NULL) errorKan1("%s\n","KdataConversion(): No memory");
                   1168:       sprintf(s,"(%s)/(%s)",rob1.lc.str,rob2.lc.str);
                   1169:       rob.tag = Sdollar;
                   1170:       rob.lc.str = s;
                   1171:       return(rob);
                   1172:     }else if  (strcmp(key,"cancel")==0) {
                   1173:       warningKan("Sorry. Data conversion <<cancel>> of rationalFunction has not supported yet.\n");
                   1174:       return(obj);
                   1175:     }else if (strcmp(key,"null") == 0) {
                   1176:       rob = NullObject;
                   1177:       return(rob);
                   1178:     }else if (strcmp(key,"double") == 0) {
                   1179:       rob = KpoDouble( toDouble0(obj) );
                   1180:       return(rob);
                   1181:     }else{
                   1182:       warningKan("Sorry. This type of data conversion of rationalFunction has not supported yet.\n");
                   1183:     }
                   1184:     break;
                   1185:   case Sdouble:
                   1186:     if (strcmp(key,"integer") == 0) {
                   1187:       rob = KpoInteger( (int) KopDouble(obj));
                   1188:       return(rob);
                   1189:     } else if (strcmp(key,"universalNumber") == 0) {
                   1190:       rob.tag = SuniversalNumber;
                   1191:       rob.lc.universalNumber = intToCoeff((int) KopDouble(obj),&SmallRing);
                   1192:       return(rob);
                   1193:     }else if ((strcmp(key,"string") == 0) || (strcmp(key,"dollar") == 0)) {
                   1194:       sprintf(tmps,"%f",KopDouble(obj));
                   1195:       s = sGC_malloc(strlen(tmps)+2);
                   1196:       if (s == (char *)NULL) errorKan1("%s\n","KdataConversion(): No memory");
                   1197:       strcpy(s,tmps);
                   1198:       rob.tag = Sdollar;
                   1199:       rob.lc.str = s;
                   1200:       return(rob);
                   1201:     }else if (strcmp(key,"double")==0) {
                   1202:       return(obj);
                   1203:     }else if (strcmp(key,"null") == 0) {
                   1204:       rob = NullObject;
                   1205:       return(rob);
                   1206:     }else {
                   1207:       warningKan("Sorry. This type of data conversion of rationalFunction has not supported yet.\n");
                   1208:     }
                   1209:     break;
                   1210:   case Sring:
                   1211:     if (strcmp(key,"orderMatrix")==0) {
                   1212:       rob = oGetOrderMatrix(KopRingp(obj));
                   1213:       return(rob);
1.22      takayama 1214:     }else if (strcmp(key,"oxRingStructure")==0) {
                   1215:       rob = oRingToOXringStructure(KopRingp(obj));
                   1216:       return(rob);
1.1       maekawa  1217:     }else{
                   1218:       warningKan("Sorryl This type of data conversion of ringp has not supported yet.\n");
                   1219:     }
                   1220:     break;
                   1221:   default:
                   1222:     warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1223:   }
                   1224:   return(NullObject);
                   1225: }
1.28      takayama 1226:
1.29      takayama 1227: /* cf. macro to_int32 */
                   1228: struct object Kto_int32(struct object ob) {
1.28      takayama 1229:   int n,i;
                   1230:   struct object otmp;
                   1231:   struct object rob;
                   1232:   if (ob.tag == SuniversalNumber) return KdataConversion(ob,"integer");
                   1233:   if (ob.tag == Sarray) {
                   1234:        n = getoaSize(ob);
                   1235:        rob = newObjectArray(n);
                   1236:        for (i=0; i<n; i++) {
1.29      takayama 1237:          otmp = Kto_int32(getoa(ob,i));
1.28      takayama 1238:          putoa(rob,i,otmp);
                   1239:        }
                   1240:        return rob;
                   1241:   }
                   1242:   return ob;
                   1243: }
1.1       maekawa  1244: /* conversion functions between primitive data and objects.
                   1245:    If it's not time critical, it is recommended to use these functions */
                   1246: struct object KpoInteger(k)
1.7       takayama 1247:      int k;
1.1       maekawa  1248: {
                   1249:   struct object obj;
                   1250:   obj.tag = Sinteger;
                   1251:   obj.lc.ival = k; obj.rc.ival = 0;
                   1252:   return(obj);
                   1253: }
                   1254: struct object KpoString(s)
1.7       takayama 1255:      char *s;
1.1       maekawa  1256: {
                   1257:   struct object obj;
                   1258:   obj.tag = Sdollar;
                   1259:   obj.lc.str = s; obj.rc.ival = 0;
                   1260:   return(obj);
                   1261: }
                   1262: struct object KpoPOLY(f)
1.7       takayama 1263:      POLY f;
1.1       maekawa  1264: {
                   1265:   struct object obj;
                   1266:   obj.tag = Spoly;
                   1267:   obj.lc.poly = f; obj.rc.ival = 0;
                   1268:   return(obj);
                   1269: }
                   1270: struct object KpoArrayOfPOLY(ap)
1.7       takayama 1271:      struct arrayOfPOLY *ap ;
1.1       maekawa  1272: {
                   1273:   struct object obj;
                   1274:   obj.tag = SarrayOfPOLY;
                   1275:   obj.lc.arrayp = ap; obj.rc.ival = 0;
                   1276:   return(obj);
                   1277: }
                   1278:
                   1279: struct object KpoMatrixOfPOLY(mp)
1.7       takayama 1280:      struct matrixOfPOLY *mp ;
1.1       maekawa  1281: {
                   1282:   struct object obj;
                   1283:   obj.tag = SmatrixOfPOLY;
                   1284:   obj.lc.matrixp = mp; obj.rc.ival = 0;
                   1285:   return(obj);
                   1286: }
                   1287:
                   1288: struct object KpoRingp(ringp)
1.7       takayama 1289:      struct ring *ringp;
1.1       maekawa  1290: {
                   1291:   struct object obj;
                   1292:   obj.tag = Sring;
                   1293:   obj.lc.ringp = ringp;
                   1294:   return(obj);
                   1295: }
                   1296:
1.22      takayama 1297: struct object KpoUniversalNumber(u)
                   1298:      struct coeff *u;
                   1299: {
                   1300:   struct object obj;
                   1301:   obj.tag = SuniversalNumber;
                   1302:   obj.lc.universalNumber = u;
                   1303:   return(obj);
                   1304: }
1.25      takayama 1305: struct object KintToUniversalNumber(n)
                   1306:         int n;
                   1307: {
                   1308:   struct object rob;
                   1309:   extern struct ring SmallRing;
                   1310:   rob.tag = SuniversalNumber;
                   1311:   rob.lc.universalNumber = intToCoeff(n,&SmallRing);
                   1312:   return(rob);
                   1313: }
                   1314:
1.1       maekawa  1315: /*** conversion 2. Data conversions on arrays and matrices. ****/
                   1316: struct object arrayOfPOLYToArray(aa)
1.7       takayama 1317:      struct arrayOfPOLY *aa;
1.1       maekawa  1318: {
                   1319:   POLY *a;
                   1320:   int size;
                   1321:   struct object r;
                   1322:   int j;
                   1323:   struct object tmp;
                   1324:
                   1325:   size = aa->n; a = aa->array;
                   1326:   r = newObjectArray(size);
                   1327:   for (j=0; j<size; j++) {
                   1328:     tmp.tag = Spoly;
                   1329:     tmp.lc.poly= a[j];
                   1330:     putoa(r,j,tmp);
                   1331:   }
                   1332:   return( r );
                   1333: }
                   1334:
                   1335: struct object matrixOfPOLYToArray(pmat)
1.7       takayama 1336:      struct matrixOfPOLY *pmat;
1.1       maekawa  1337: {
                   1338:   struct object r;
                   1339:   struct object tmp;
                   1340:   int i,j;
                   1341:   int m,n;
                   1342:   POLY *mat;
                   1343:   struct arrayOfPOLY ap;
                   1344:
                   1345:   m = pmat->m; n = pmat->n; mat = pmat->mat;
                   1346:   r = newObjectArray(m);
                   1347:   for (i=0; i<m; i++) {
                   1348:     ap.n = n; ap.array = &(mat[ind(i,0)]);
                   1349:     tmp = arrayOfPOLYToArray(&ap);
                   1350:     /* ind() is the macro defined in matrix.h. */
                   1351:     putoa(r,i,tmp);
                   1352:   }
                   1353:   return(r);
                   1354: }
                   1355:
                   1356: struct arrayOfPOLY *arrayToArrayOfPOLY(oa)
1.7       takayama 1357:      struct object oa;
1.1       maekawa  1358: {
                   1359:   POLY *a;
                   1360:   int size;
                   1361:   int i;
                   1362:   struct object tmp;
                   1363:   struct arrayOfPOLY *ap;
                   1364:
                   1365:   if (oa.tag != Sarray) errorKan1("KarrayToArrayOfPOLY(): %s",
1.7       takayama 1366:                                   "Argument is not array\n");
1.1       maekawa  1367:   size = getoaSize(oa);
                   1368:   a = (POLY *)sGC_malloc(sizeof(POLY)*size);
                   1369:   for (i=0; i<size; i++) {
                   1370:     tmp = getoa(oa,i);
                   1371:     if (tmp.tag != Spoly) errorKan1("KarrayToArrayOfPOLY():%s ",
1.7       takayama 1372:                                     "element must be polynomial.\n");
1.1       maekawa  1373:     a[i] = tmp.lc.poly;
                   1374:   }
                   1375:   ap = (struct arrayOfPOLY *)sGC_malloc(sizeof(struct arrayOfPOLY));
                   1376:   ap->n = size;
                   1377:   ap->array = a;
                   1378:   return(ap);
                   1379: }
                   1380:
                   1381: struct matrixOfPOLY *arrayToMatrixOfPOLY(oa)
1.7       takayama 1382:      struct object oa;
1.1       maekawa  1383: {
                   1384:   POLY *a;
                   1385:   int m;
                   1386:   int n;
                   1387:   int i,j;
                   1388:   struct matrixOfPOLY *ma;
                   1389:
                   1390:   struct object tmp,tmp2;
                   1391:   if (oa.tag != Sarray) errorKan1("KarrayToMatrixOfPOLY(): %s",
1.7       takayama 1392:                                   "Argument is not array\n");
1.1       maekawa  1393:   m = getoaSize(oa);
                   1394:   tmp = getoa(oa,0);
                   1395:   if (tmp.tag != Sarray) errorKan1("arrayToMatrixOfPOLY():%s ",
1.7       takayama 1396:                                    "Argument is not array\n");
1.1       maekawa  1397:   n = getoaSize(tmp);
                   1398:   a = (POLY *)sGC_malloc(sizeof(POLY)*(m*n));
                   1399:   for (i=0; i<m; i++) {
                   1400:     tmp = getoa(oa,i);
                   1401:     if (tmp.tag != Sarray) errorKan1("arrayToMatrixOfPOLY(): %s",
1.7       takayama 1402:                                      "element must be array.\n");
1.1       maekawa  1403:     for (j=0; j<n; j++) {
                   1404:       tmp2 = getoa(tmp,j);
                   1405:       if (tmp2.tag != Spoly) errorKan1("arrayToMatrixOfPOLY(): %s",
1.7       takayama 1406:                                        "element must be a polynomial.\n");
1.1       maekawa  1407:       a[ind(i,j)] = tmp2.lc.poly;
                   1408:       /* we use the macro ind here.  Be careful of using m and n. */
                   1409:     }
                   1410:   }
                   1411:   ma = (struct matrixOfPOLY *)sGC_malloc(sizeof(struct matrixOfPOLY));
                   1412:   ma->m = m; ma->n = n;
                   1413:   ma->mat = a;
                   1414:   return(ma);
                   1415: }
                   1416:
                   1417: /* :misc */
                   1418:
                   1419: /* :ring    :kan */
                   1420: int objArrayToOrderMatrix(oA,order,n,oasize)
1.7       takayama 1421:      struct object oA;
                   1422:      int order[];
                   1423:      int n;
                   1424:      int oasize;
1.1       maekawa  1425: {
                   1426:   int size;
                   1427:   int k,j;
                   1428:   struct object tmpOa;
                   1429:   struct object obj;
                   1430:   if (oA.tag != Sarray) {
                   1431:     warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1432:     return(-1);
                   1433:   }
                   1434:   size = getoaSize(oA);
                   1435:   if (size != oasize) {
                   1436:     warningKan("The row size of the array is wrong.");
                   1437:     return(-1);
                   1438:   }
                   1439:   for (k=0; k<size; k++) {
                   1440:     tmpOa = getoa(oA,k);
                   1441:     if (tmpOa.tag != Sarray) {
                   1442:       warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1443:       return(-1);
                   1444:     }
                   1445:     if (getoaSize(tmpOa) != 2*n) {
                   1446:       warningKan("The column size of the array is wrong.");
                   1447:       return(-1);
                   1448:     }
                   1449:     for (j=0; j<2*n; j++) {
                   1450:       obj = getoa(tmpOa,j);
                   1451:       order[k*2*n+j] = obj.lc.ival;
                   1452:     }
                   1453:   }
                   1454:   return(0);
                   1455: }
                   1456:
                   1457: int KsetOrderByObjArray(oA)
1.7       takayama 1458:      struct object oA;
1.1       maekawa  1459: {
                   1460:   int *order;
                   1461:   int n,c,l, oasize;
                   1462:   extern struct ring *CurrentRingp;
                   1463:   extern int AvoidTheSameRing;
                   1464:   /* n,c,l must be set in the CurrentRing */
                   1465:   if (AvoidTheSameRing) {
                   1466:     errorKan1("%s\n","KsetOrderByObjArray(): You cannot change the order matrix when AvoidTheSameRing == 1.");
                   1467:   }
                   1468:   n = CurrentRingp->n;
                   1469:   c = CurrentRingp->c;
                   1470:   l = CurrentRingp->l;
                   1471:   if (oA.tag != Sarray) {
                   1472:     warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1473:     return(-1);
                   1474:   }
                   1475:   oasize = getoaSize(oA);
                   1476:   order = (int *)sGC_malloc(sizeof(int)*((2*n)*oasize+1));
                   1477:   if (order == (int *)NULL) errorKan1("%s\n","KsetOrderByObjArray(): No memory.");
                   1478:   if (objArrayToOrderMatrix(oA,order,n,oasize) == -1) {
                   1479:     return(-1);
                   1480:   }
                   1481:   setOrderByMatrix(order,n,c,l,oasize); /* Set order to the current ring. */
                   1482:   return(0);
                   1483: }
                   1484:
                   1485: static int checkRelations(c,l,m,n,cc,ll,mm,nn)
1.7       takayama 1486:      int c,l,m,n,cc,ll,mm,nn;
1.1       maekawa  1487: {
                   1488:   if (!(1<=c && c<=l && l<=m && m<=n)) return(1);
                   1489:   if (!(cc<=ll && ll<=mm && mm<=nn && nn <= n)) return(1);
                   1490:   if (!(cc<c || ll < l || mm < m || nn < n)) {
                   1491:     if (WarningNoVectorVariable) {
1.4       takayama 1492:       warningKanNoStrictMode("Ring definition: there is no variable to represent vectors.\n");
1.1       maekawa  1493:     }
                   1494:   }
                   1495:   if (!(cc<=c && ll <= l && mm <= m && nn <= n)) return(1);
                   1496:   return(0);
                   1497: }
                   1498:
                   1499: struct object KgetOrderMatrixOfCurrentRing()
                   1500: {
                   1501:   extern struct ring *CurrentRingp;
                   1502:   return(oGetOrderMatrix(CurrentRingp));
                   1503: }
                   1504:
                   1505:
                   1506: int KsetUpRing(ob1,ob2,ob3,ob4,ob5)
1.7       takayama 1507:      struct object ob1,ob2,ob3,ob4,ob5;
                   1508:      /* ob1 = [x(0), ..., x(n-1)];
                   1509:         ob2 = [D(0), ..., D(n-1)];
                   1510:         ob3 = [p,c,l,m,n,cc,ll,mm,nn,next];
                   1511:         ob4 = Order matrix
                   1512:         ob5 = [(keyword) value (keyword) value ....]
                   1513:      */
1.1       maekawa  1514: #define RP_LIMIT 500
                   1515: {
                   1516:   int i;
                   1517:   struct object ob;
                   1518:   int c,l,m,n;
                   1519:   int cc,ll,mm,nn;
                   1520:   int p;
                   1521:   char **xvars;
                   1522:   char **dvars;
                   1523:   int *outputVars;
                   1524:   int *order;
                   1525:   static int rp = 0;
                   1526:   static struct ring *rstack[RP_LIMIT];
                   1527:
                   1528:   extern struct ring *CurrentRingp;
                   1529:   struct ring *newRingp;
                   1530:   int ob3Size;
                   1531:   struct ring *nextRing;
                   1532:   int oasize;
                   1533:   static int ringSerial = 0;
                   1534:   char *ringName = NULL;
                   1535:   int aa;
                   1536:   extern int AvoidTheSameRing;
                   1537:   extern char *F_mpMult;
                   1538:   char *fmp_mult_saved;
                   1539:   char *mpMultName = NULL;
                   1540:   struct object rob;
                   1541:   struct ring *savedCurrentRingp;
                   1542:
                   1543:   /* To get the ring structure. */
                   1544:   if (ob1.tag == Snull) {
                   1545:     rob = newObjectArray(rp);
                   1546:     for (i=0; i<rp; i++) {
                   1547:       putoa(rob,i,KpoRingp(rstack[i]));
                   1548:     }
                   1549:     KSpush(rob);
                   1550:     return(0);
                   1551:   }
                   1552:
                   1553:   if (ob3.tag != Sarray) errorKan1("%s\n","Error in the 3rd argument. You need to give 4 arguments.");
                   1554:   ob3Size = getoaSize(ob3);
                   1555:   if (ob3Size != 9 && ob3Size != 10)
                   1556:     errorKan1("%s\n","Error in the 3rd argument.");
                   1557:   for (i=0; i<9; i++) {
                   1558:     ob = getoa(ob3,i);
                   1559:     if (ob.tag != Sinteger) errorKan1("%s\n","The 3rd argument should be a list of integers.");
                   1560:   }
                   1561:   if (ob3Size == 10) {
                   1562:     ob = getoa(ob3,9);
                   1563:     if (ob.tag != Sring)
                   1564:       errorKan1("%s\n","The last arguments of the 3rd argument must be a pointer to a ring.");
                   1565:     nextRing = KopRingp(ob);
                   1566:   } else {
                   1567:     nextRing = (struct ring *)NULL;
                   1568:   }
                   1569:
                   1570:   p = getoa(ob3,0).lc.ival;
                   1571:   c = getoa(ob3,1).lc.ival;  l = getoa(ob3,2).lc.ival;
                   1572:   m = getoa(ob3,3).lc.ival;  n = getoa(ob3,4).lc.ival;
                   1573:   cc = getoa(ob3,5).lc.ival;  ll = getoa(ob3,6).lc.ival;
                   1574:   mm = getoa(ob3,7).lc.ival;  nn = getoa(ob3,8).lc.ival;
                   1575:   if (checkRelations(c,l,m,n,cc,ll,mm,nn,n)) {
                   1576:     errorKan1("%s\n","1<=c<=l<=m<=n and cc<=c<=ll<=l<=mm<=m<=nn<=n \nand (cc<c or ll < l or mm < m or nn < n)  must be satisfied.");
                   1577:   }
                   1578:   if (getoaSize(ob2) != n || getoaSize(ob1) != n) {
                   1579:     errorKan1("%s\n","Error in the 1st or 2nd arguments.");
                   1580:   }
                   1581:   for (i=0; i<n; i++) {
                   1582:     if (getoa(ob1,i).tag != Sdollar || getoa(ob2,i).tag != Sdollar) {
                   1583:       errorKan1("%s\n","Error in the 1st or 2nd arguments.");
                   1584:     }
                   1585:   }
                   1586:   xvars = (char **) sGC_malloc(sizeof(char *)*n);
                   1587:   dvars = (char **) sGC_malloc(sizeof(char *)*n);
                   1588:   if (xvars == (char **)NULL || dvars == (char **)NULL) {
                   1589:     fprintf(stderr,"No more memory.\n");
                   1590:     exit(15);
                   1591:   }
                   1592:   for (i=0; i<n; i++) {
                   1593:     xvars[i] = getoa(ob1,i).lc.str;
                   1594:     dvars[i] = getoa(ob2,i).lc.str;
                   1595:   }
                   1596:   checkDuplicateName(xvars,dvars,n);
                   1597:
                   1598:   outputVars = (int *)sGC_malloc(sizeof(int)*n*2);
                   1599:   if (outputVars == NULL) {
                   1600:     fprintf(stderr,"No more memory.\n");
                   1601:     exit(15);
                   1602:   }
                   1603:   if (ReverseOutputOrder) {
                   1604:     for (i=0; i<n; i++) outputVars[i] = n-i-1;
                   1605:     for (i=0; i<n; i++) outputVars[n+i] = 2*n-i-1;
                   1606:   }else{
                   1607:     for (i=0; i<2*n; i++) {
                   1608:       outputVars[i] = i;
                   1609:     }
                   1610:   }
1.28      takayama 1611:
1.29      takayama 1612:   ob4 = Kto_int32(ob4); /* order matrix */
1.1       maekawa  1613:   oasize = getoaSize(ob4);
                   1614:   order = (int *)sGC_malloc(sizeof(int)*((2*n)*oasize+1));
                   1615:   if (order == (int *)NULL) errorKan1("%s\n","No memory.");
                   1616:   if (objArrayToOrderMatrix(ob4,order,n,oasize) == -1) {
                   1617:     errorKan1("%s\n","Errors in the 4th matrix (order matrix).");
                   1618:   }
                   1619:   /* It's better to check the consistency of the order matrix here. */
                   1620:   savedCurrentRingp = CurrentRingp;
                   1621:
                   1622:   newRingp = (struct ring *)sGC_malloc(sizeof(struct ring));
                   1623:   if (newRingp == NULL) errorKan1("%s\n","No more memory.");
                   1624:   /* Generate the new ring before calling setOrder...(). */
                   1625:   *newRingp = *CurrentRingp;
                   1626:   CurrentRingp = newRingp;  /* Push the current ring. */
                   1627:   setOrderByMatrix(order,n,c,l,oasize); /* set order to the CurrentRing. */
                   1628:   CurrentRingp = savedCurrentRingp; /* recover it. */
                   1629:
                   1630:
                   1631:   /* Set the default name of the ring */
                   1632:   ringName = (char *)sGC_malloc(16);
                   1633:   sprintf(ringName,"ring%05d",ringSerial);
                   1634:   ringSerial++;
                   1635:
                   1636:   /* Set the current ring */
                   1637:   newRingp->n = n; newRingp->m = m; newRingp->l = l; newRingp->c = c;
                   1638:   newRingp->nn = nn; newRingp->mm = mm; newRingp->ll = ll;
                   1639:   newRingp->cc = cc;
                   1640:   newRingp->x = xvars;
                   1641:   newRingp->D = dvars;
                   1642:   /* You don't need to set order and orderMatrixSize here.
                   1643:      It was set by setOrder(). */
                   1644:   setFromTo(newRingp);
                   1645:
                   1646:   newRingp->p = p;
                   1647:   newRingp->next = nextRing;
                   1648:   newRingp->multiplication = mpMult;
                   1649:   /* These values  should will be reset if the optional value is given. */
                   1650:   newRingp->schreyer = 0;
                   1651:   newRingp->gbListTower = NULL;
                   1652:   newRingp->outputOrder = outputVars;
1.9       takayama 1653:   newRingp->weightedHomogenization = 0;
1.11      takayama 1654:   newRingp->degreeShiftSize = 0;
1.12      takayama 1655:   newRingp->degreeShiftN = 0;
                   1656:   newRingp->degreeShift = NULL;
1.34      takayama 1657:   newRingp->partialEcart = 0;
                   1658:   newRingp->partialEcartGlobalVarX = NULL;
1.1       maekawa  1659:
                   1660:   if (ob5.tag != Sarray || (getoaSize(ob5) % 2) != 0) {
                   1661:     errorKan1("%s\n","[(keyword) value (keyword) value ....] should be given.");
                   1662:   }
                   1663:   for (i=0; i < getoaSize(ob5); i += 2) {
                   1664:     if (getoa(ob5,i).tag == Sdollar) {
                   1665:       if (strcmp(KopString(getoa(ob5,i)),"mpMult") == 0) {
1.7       takayama 1666:         if (getoa(ob5,i+1).tag != Sdollar) {
                   1667:           errorKan1("%s\n","A keyword should be given. (mpMult)");
                   1668:         }
                   1669:         fmp_mult_saved = F_mpMult;
                   1670:         mpMultName = KopString(getoa(ob5,i+1));
                   1671:         switch_function("mpMult",mpMultName);
                   1672:         /* Note that this cause a global effect. It will be done again. */
                   1673:         newRingp->multiplication = mpMult;
                   1674:         switch_function("mpMult",fmp_mult_saved);
1.1       maekawa  1675:       } else if (strcmp(KopString(getoa(ob5,i)),"coefficient ring") == 0) {
1.7       takayama 1676:         if (getoa(ob5,i+1).tag != Sring) {
                   1677:           errorKan1("%s\n","The pointer to a ring should be given. (coefficient ring)");
                   1678:         }
                   1679:         nextRing = KopRingp(getoa(ob5,i+1));
                   1680:         newRingp->next = nextRing;
1.1       maekawa  1681:       } else if (strcmp(KopString(getoa(ob5,i)),"valuation") == 0) {
1.7       takayama 1682:         errorKan1("%s\n","Not implemented. (valuation)");
1.1       maekawa  1683:       } else if (strcmp(KopString(getoa(ob5,i)),"characteristic") == 0) {
1.7       takayama 1684:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1685:           errorKan1("%s\n","A integer should be given. (characteristic)");
                   1686:         }
                   1687:         p = KopInteger(getoa(ob5,i+1));
                   1688:         newRingp->p = p;
1.1       maekawa  1689:       } else if (strcmp(KopString(getoa(ob5,i)),"schreyer") == 0) {
1.7       takayama 1690:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1691:           errorKan1("%s\n","A integer should be given. (schreyer)");
                   1692:         }
                   1693:         newRingp->schreyer = KopInteger(getoa(ob5,i+1));
1.1       maekawa  1694:       } else if (strcmp(KopString(getoa(ob5,i)),"gbListTower") == 0) {
1.7       takayama 1695:         if (getoa(ob5,i+1).tag != Slist) {
                   1696:           errorKan1("%s\n","A list should be given (gbListTower).");
                   1697:         }
                   1698:         newRingp->gbListTower = newObject();
                   1699:         *((struct object *)(newRingp->gbListTower)) = getoa(ob5,i+1);
1.1       maekawa  1700:       } else if (strcmp(KopString(getoa(ob5,i)),"ringName") == 0) {
1.7       takayama 1701:         if (getoa(ob5,i+1).tag != Sdollar) {
                   1702:           errorKan1("%s\n","A name should be given. (ringName)");
                   1703:         }
                   1704:         ringName = KopString(getoa(ob5,i+1));
1.9       takayama 1705:       } else if (strcmp(KopString(getoa(ob5,i)),"weightedHomogenization") == 0) {
                   1706:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1707:           errorKan1("%s\n","A integer should be given. (weightedHomogenization)");
                   1708:         }
1.11      takayama 1709:         newRingp->weightedHomogenization = KopInteger(getoa(ob5,i+1));
                   1710:       } else if (strcmp(KopString(getoa(ob5,i)),"degreeShift") == 0) {
                   1711:         if (getoa(ob5,i+1).tag != Sarray) {
1.12      takayama 1712:           errorKan1("%s\n","An array of array should be given. (degreeShift)");
1.11      takayama 1713:         }
                   1714:         {
                   1715:           struct object ods;
1.12      takayama 1716:           struct object ods2;
                   1717:           int dssize,k,j,nn;
1.11      takayama 1718:           ods=getoa(ob5,i+1);
1.12      takayama 1719:           if ((getoaSize(ods) < 1) || (getoa(ods,0).tag != Sarray)) {
                   1720:             errorKan1("%s\n", "An array of array should be given. (degreeShift)");
                   1721:           }
                   1722:           nn = getoaSize(ods);
                   1723:           dssize = getoaSize(getoa(ods,0));
1.11      takayama 1724:           newRingp->degreeShiftSize = dssize;
1.12      takayama 1725:           newRingp->degreeShiftN = nn;
                   1726:           newRingp->degreeShift = (int *) sGC_malloc(sizeof(int)*(dssize*nn+1));
1.11      takayama 1727:           if (newRingp->degreeShift == NULL) errorKan1("%s\n","No more memory.");
1.12      takayama 1728:           for (j=0; j<nn; j++) {
                   1729:             ods2 = getoa(ods,j);
                   1730:             for (k=0; k<dssize; k++) {
                   1731:               if (getoa(ods2,k).tag == SuniversalNumber) {
                   1732:                 (newRingp->degreeShift)[j*dssize+k] = coeffToInt(getoa(ods2,k).lc.universalNumber);
                   1733:               }else{
                   1734:                 (newRingp->degreeShift)[j*dssize+k] = KopInteger(getoa(ods2,k));
                   1735:               }
1.11      takayama 1736:             }
                   1737:           }
                   1738:         }
1.34      takayama 1739:       } else if (strcmp(KopString(getoa(ob5,i)),"partialEcartGlobalVarX") == 0) {
                   1740:         if (getoa(ob5,i+1).tag != Sarray) {
                   1741:           errorKan1("%s\n","An array of array should be given. (partialEcart)");
                   1742:         }
                   1743:         {
                   1744:           struct object odv;
                   1745:           struct object ovv;
                   1746:           int k,j,nn;
                   1747:           char *vname;
                   1748:           odv=getoa(ob5,i+1);
                   1749:           nn = getoaSize(odv);
                   1750:           newRingp->partialEcart = nn;
                   1751:           newRingp->partialEcartGlobalVarX = (int *) sGC_malloc(sizeof(int)*nn+1);
                   1752:           if (newRingp->partialEcartGlobalVarX == NULL) errorKan1("%s\n","No more memory.");
                   1753:           for (j=0; j<nn; j++)
                   1754:             (newRingp->partialEcartGlobalVarX)[j] = -1;
                   1755:           for (j=0; j<nn; j++) {
                   1756:             ovv = getoa(odv,j);
                   1757:             if (ovv.tag != Sdollar) errorKan1("%s\n","partialEcartGlobalVarX: string is expected.");
                   1758:             vname = KopString(ovv);
                   1759:             for (k=0; k<n; k++) {
                   1760:               if (strcmp(vname,xvars[k]) == 0) {
                   1761:                 (newRingp->partialEcartGlobalVarX)[j] = k; break;
                   1762:               }else{
                   1763:                 if (k == n-1) errorKan1("%s\n","partialEcartGlobalVarX: no such variable.");
                   1764:               }
                   1765:             }
                   1766:           }
                   1767:         }
                   1768:
1.22      takayama 1769:         switch_function("grade","module1v");
                   1770:         /* Warning: grading is changed to module1v!! */
1.1       maekawa  1771:       } else {
1.7       takayama 1772:         errorKan1("%s\n","Unknown keyword to set_up_ring@");
1.1       maekawa  1773:       }
                   1774:     }else{
                   1775:       errorKan1("%s\n","A keyword enclosed by braces have to be given.");
                   1776:     }
                   1777:   }
                   1778:
                   1779:   newRingp->name = ringName;
                   1780:
                   1781:
                   1782:   if (AvoidTheSameRing) {
                   1783:     aa = isTheSameRing(rstack,rp,newRingp);
                   1784:     if (aa < 0) {
                   1785:       /* This ring has never been defined. */
                   1786:       CurrentRingp = newRingp;
                   1787:       /* Install it to the RingStack */
                   1788:       if (rp <RP_LIMIT) {
1.7       takayama 1789:         rstack[rp] = CurrentRingp; rp++; /* Save the previous ringp */
1.1       maekawa  1790:       }else{
1.7       takayama 1791:         rp = 0;
                   1792:         errorKan1("%s\n","You have defined too many rings. Check the value of RP_LIMIT.");
1.1       maekawa  1793:       }
                   1794:     }else{
                   1795:       /* This ring has been defined. */
                   1796:       /* Discard the newRingp */
                   1797:       CurrentRingp = rstack[aa];
                   1798:       ringSerial--;
                   1799:     }
                   1800:   }else{
                   1801:     CurrentRingp = newRingp;
                   1802:     /* Install it to the RingStack */
                   1803:     if (rp <RP_LIMIT) {
                   1804:       rstack[rp] = CurrentRingp; rp++; /* Save the previous ringp */
                   1805:     }else{
                   1806:       rp = 0;
                   1807:       errorKan1("%s\n","You have defined too many rings. Check the value of RP_LIMIT.");
                   1808:     }
                   1809:   }
                   1810:   if (mpMultName != NULL) {
                   1811:     switch_function("mpMult",mpMultName);
                   1812:   }
                   1813:
                   1814:   initSyzRingp();
                   1815:
                   1816:   return(0);
                   1817: }
                   1818:
                   1819:
                   1820: struct object KsetVariableNames(struct object ob,struct ring *rp)
                   1821: {
                   1822:   int n,i;
                   1823:   struct object ox;
                   1824:   struct object otmp;
                   1825:   char **xvars;
                   1826:   char **dvars;
                   1827:   if (ob.tag  != Sarray) {
                   1828:     errorKan1("%s\n","KsetVariableNames(): the argument must be of the form [(x) (y) (z) ...]");
                   1829:   }
                   1830:   n = rp->n;
                   1831:   ox = ob;
                   1832:   if (getoaSize(ox) != 2*n) {
                   1833:     errorKan1("%s\n","KsetVariableNames(): the argument must be of the form [(x) (y) (z) ...] and the length of [(x) (y) (z) ...] must be equal to the number of x and D variables.");
                   1834:   }
                   1835:   xvars = (char **)sGC_malloc(sizeof(char *)*n);
                   1836:   dvars = (char **)sGC_malloc(sizeof(char *)*n);
                   1837:   if (xvars == NULL || dvars == NULL) {
                   1838:     errorKan1("%s\n","KsetVariableNames(): no more memory.");
                   1839:   }
                   1840:   for (i=0; i<2*n; i++) {
                   1841:     otmp = getoa(ox,i);
                   1842:     if(otmp.tag != Sdollar) {
                   1843:       errorKan1("%s\n","KsetVariableNames(): elements must be strings.");
                   1844:     }
                   1845:     if (i < n) {
                   1846:       xvars[i] = KopString(otmp);
                   1847:     }else{
                   1848:       dvars[i-n] = KopString(otmp);
                   1849:     }
                   1850:   }
                   1851:   checkDuplicateName(xvars,dvars,n);
                   1852:   rp->x = xvars;
                   1853:   rp->D = dvars;
                   1854:   return(ob);
                   1855: }
                   1856:
                   1857:
                   1858:
                   1859: void KshowRing(ringp)
1.7       takayama 1860:      struct ring *ringp;
1.1       maekawa  1861: {
                   1862:   showRing(1,ringp);
                   1863: }
                   1864:
                   1865: struct object KswitchFunction(ob1,ob2)
1.7       takayama 1866:      struct object ob1,ob2;
1.1       maekawa  1867: {
                   1868:   char *ans ;
                   1869:   struct object rob;
                   1870:   int needWarningForAvoidTheSameRing = 0;
                   1871:   extern int AvoidTheSameRing;
                   1872:   if ((ob1.tag != Sdollar) || (ob2.tag != Sdollar)) {
                   1873:     errorKan1("%s\n","$function$ $name$ switch_function\n");
                   1874:   }
                   1875:   if (AvoidTheSameRing && needWarningForAvoidTheSameRing) {
                   1876:     if (strcmp(KopString(ob1),"mmLarger") == 0 ||
                   1877:         strcmp(KopString(ob1),"mpMult") == 0 ||
                   1878:         strcmp(KopString(ob1),"monomialAdd") == 0 ||
                   1879:         strcmp(KopString(ob1),"isSameComponent") == 0) {
                   1880:       fprintf(stderr,",switch_function ==> %s ",KopString(ob1));
                   1881:       warningKan("switch_function might cause a trouble under AvoidTheSameRing == 1.\n");
                   1882:     }
                   1883:   }
                   1884:   if (AvoidTheSameRing) {
                   1885:     if (strcmp(KopString(ob1),"mmLarger") == 0 &&
1.7       takayama 1886:         strcmp(KopString(ob2),"matrix") != 0) {
1.1       maekawa  1887:       fprintf(stderr,"mmLarger = %s",KopString(ob2));
                   1888:       errorKan1("%s\n","mmLarger can set only to matrix under AvoidTheSameRing == 1.");
                   1889:     }
                   1890:   }
                   1891:
                   1892:   ans = switch_function(ob1.lc.str,ob2.lc.str);
                   1893:   if (ans == NULL) {
                   1894:     rob = NullObject;
                   1895:   }else{
                   1896:     rob = KpoString(ans);
                   1897:   }
                   1898:   return(rob);
                   1899:
                   1900: }
                   1901:
                   1902: void KprintSwitchStatus(void)
                   1903: {
                   1904:   print_switch_status();
                   1905: }
                   1906:
                   1907: struct object KoReplace(of,rule)
1.7       takayama 1908:      struct object of;
                   1909:      struct object rule;
1.1       maekawa  1910: {
                   1911:   struct object rob;
                   1912:   POLY f;
                   1913:   POLY lRule[N0*2];
                   1914:   POLY rRule[N0*2];
                   1915:   POLY r;
                   1916:   int i;
                   1917:   int n;
                   1918:   struct object trule;
                   1919:
                   1920:
                   1921:   if (rule.tag != Sarray) {
                   1922:     errorKan1("%s\n"," KoReplace(): The second argument must be array.");
                   1923:   }
                   1924:   n = getoaSize(rule);
                   1925:
1.6       takayama 1926:   if (of.tag == Spoly) {
                   1927:   }else if (of.tag ==Sclass && ectag(of) == CLASSNAME_recursivePolynomial) {
1.7       takayama 1928:     return(KreplaceRecursivePolynomial(of,rule));
1.6       takayama 1929:   }else{
1.1       maekawa  1930:     errorKan1("%s\n"," KoReplace(): The first argument must be a polynomial.");
                   1931:   }
                   1932:   f = KopPOLY(of);
                   1933:
                   1934:   if (f ISZERO) {
                   1935:   }else{
                   1936:     if (n >= 2*(f->m->ringp->n)) {
                   1937:       errorKan1("%s\n"," KoReplace(): too many rules for replacement. ");
                   1938:     }
                   1939:   }
                   1940:
                   1941:   for (i=0; i<n; i++) {
                   1942:     trule = getoa(rule,i);
                   1943:     if (trule.tag != Sarray) {
                   1944:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....].");
                   1945:     }
                   1946:     if (getoaSize(trule) != 2) {
                   1947:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....].");
                   1948:     }
                   1949:
                   1950:     if (getoa(trule,0).tag != Spoly) {
                   1951:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....] where a,b,c,d,... are polynomials.");
                   1952:     }
                   1953:     if (getoa(trule,1).tag != Spoly) {
                   1954:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....] where a,b,c,d,... are polynomials.");
                   1955:     }
                   1956:
                   1957:     lRule[i] = KopPOLY(getoa(trule,0));
                   1958:     rRule[i] = KopPOLY(getoa(trule,1));
                   1959:   }
                   1960:
                   1961:   r = replace(f,lRule,rRule,n);
                   1962:   rob.tag = Spoly; rob.lc.poly = r;
                   1963:
                   1964:   return(rob);
                   1965: }
                   1966:
                   1967:
                   1968: struct object Kparts(f,v)
1.7       takayama 1969:      struct object f;
                   1970:      struct object v;
1.1       maekawa  1971: {
                   1972:   POLY ff;
                   1973:   POLY vv;
                   1974:   struct object obj;
                   1975:   struct matrixOfPOLY *co;
                   1976:   /* check the data type */
                   1977:   if (f.tag != Spoly || v.tag != Spoly)
                   1978:     errorKan1("%s\n","arguments of Kparts() must have polynomial as arguments.");
                   1979:
                   1980:   co = parts(KopPOLY(f),KopPOLY(v));
                   1981:   obj = matrixOfPOLYToArray(co);
                   1982:   return(obj);
                   1983: }
                   1984:
                   1985: struct object Kparts2(f,v)
1.7       takayama 1986:      struct object f;
                   1987:      struct object v;
1.1       maekawa  1988: {
                   1989:   POLY ff;
                   1990:   POLY vv;
                   1991:   struct object obj;
                   1992:   struct matrixOfPOLY *co;
                   1993:   /* check the data type */
                   1994:   if (f.tag != Spoly || v.tag != Spoly)
                   1995:     errorKan1("%s\n","arguments of Kparts2() must have polynomial as arguments.");
                   1996:
                   1997:   obj = parts2(KopPOLY(f),KopPOLY(v));
                   1998:   return(obj);
                   1999: }
                   2000:
                   2001:
                   2002: struct object Kdegree(ob1,ob2)
1.7       takayama 2003:      struct object ob1,ob2;
1.1       maekawa  2004: {
                   2005:   if (ob1.tag != Spoly || ob2.tag != Spoly)
                   2006:     errorKan1("%s\n","The arguments must be polynomials.");
                   2007:
                   2008:   return(KpoInteger(pDegreeWrtV(KopPOLY(ob1),KopPOLY(ob2))));
                   2009: }
                   2010:
                   2011: struct object KringMap(obj)
1.7       takayama 2012:      struct object obj;
1.1       maekawa  2013: {
                   2014:   extern struct ring *CurrentRingp;
                   2015:   extern struct ring *SyzRingp;
                   2016:   POLY f;
                   2017:   POLY r;
                   2018:   if (obj.tag != Spoly)
                   2019:     errorKan1("%s\n","The argments must be polynomial.");
                   2020:   f = KopPOLY(obj);
                   2021:   if (f ISZERO) return(obj);
                   2022:   if (f->m->ringp == CurrentRingp) return(obj);
                   2023:   if (f->m->ringp == CurrentRingp->next) {
                   2024:     r = newCell(newCoeff(),newMonomial(CurrentRingp));
                   2025:     r->coeffp->tag = POLY_COEFF;
                   2026:     r->coeffp->val.f = f;
                   2027:     return(KpoPOLY(r));
                   2028:   }else if (f->m->ringp == SyzRingp) {
                   2029:     return(KpoPOLY(f->coeffp->val.f));
                   2030:   }
                   2031:   errorKan1("%s\n","The ring map is not defined in this case.");
                   2032: }
                   2033:
                   2034:
                   2035: struct object Ksp(ob1,ob2)
1.7       takayama 2036:      struct object ob1,ob2;
1.1       maekawa  2037: {
                   2038:   struct spValue sv;
                   2039:   struct object rob,cob;
                   2040:   POLY f;
                   2041:   if (ob1.tag != Spoly || ob2.tag != Spoly)
                   2042:     errorKan1("%s\n","Ksp(): The arguments must be polynomials.");
                   2043:   sv = (*sp)(ob1.lc.poly,ob2.lc.poly);
                   2044:   f = ppAddv(ppMult(sv.a,KopPOLY(ob1)),
1.7       takayama 2045:              ppMult(sv.b,KopPOLY(ob2)));
1.1       maekawa  2046:   rob = newObjectArray(2);
                   2047:   cob = newObjectArray(2);
                   2048:   putoa(rob,1,KpoPOLY(f));
                   2049:   putoa(cob,0,KpoPOLY(sv.a));
                   2050:   putoa(cob,1,KpoPOLY(sv.b));
                   2051:   putoa(rob,0,cob);
                   2052:   return(rob);
                   2053: }
                   2054:
                   2055: struct object Khead(ob)
1.7       takayama 2056:      struct object ob;
1.1       maekawa  2057: {
                   2058:   if (ob.tag != Spoly) errorKan1("%s\n","Khead(): The argument should be a polynomial.");
                   2059:   return(KpoPOLY(head( KopPOLY(ob))));
                   2060: }
                   2061:
                   2062:
                   2063: /* :eval */
                   2064: struct object Keval(obj)
1.7       takayama 2065:      struct object obj;
1.1       maekawa  2066: {
                   2067:   char *key;
                   2068:   int size;
                   2069:   struct object rob;
                   2070:   rob = NullObject;
                   2071:
                   2072:   if (obj.tag != Sarray)
                   2073:     errorKan1("%s\n","[$key$ arguments] eval");
                   2074:   if (getoaSize(obj) < 1)
                   2075:     errorKan1("%s\n","[$key$ arguments] eval");
                   2076:   if (getoa(obj,0).tag != Sdollar)
                   2077:     errorKan1("%s\n","[$key$ arguments] eval");
                   2078:   key = getoa(obj,0).lc.str;
                   2079:   size = getoaSize(obj);
                   2080:
                   2081:
                   2082:   return(rob);
                   2083: }
                   2084:
                   2085: /* :Utilities */
                   2086: char *KremoveSpace(str)
1.7       takayama 2087:      char str[];
1.1       maekawa  2088: {
                   2089:   int size;
                   2090:   int start;
                   2091:   int end;
                   2092:   char *s;
                   2093:   int i;
                   2094:
                   2095:   size = strlen(str);
                   2096:   for (start = 0; start <= size; start++) {
                   2097:     if (str[start] > ' ') break;
                   2098:   }
                   2099:   for (end = size-1; end >= 0; end--) {
                   2100:     if (str[end] > ' ') break;
                   2101:   }
                   2102:   if (start > end) return((char *) NULL);
                   2103:   s = (char *) sGC_malloc(sizeof(char)*(end-start+2));
                   2104:   if (s == (char *)NULL) errorKan1("%s\n","removeSpace(): No more memory.");
                   2105:   for (i=0; i< end-start+1; i++)
                   2106:     s[i] = str[i+start];
                   2107:   s[end-start+1] = '\0';
                   2108:   return(s);
                   2109: }
                   2110:
                   2111: struct object KtoRecords(ob)
1.7       takayama 2112:      struct object ob;
1.1       maekawa  2113: {
                   2114:   struct object obj;
                   2115:   struct object tmp;
                   2116:   int i;
                   2117:   int size;
                   2118:   char **argv;
                   2119:
                   2120:   obj = NullObject;
                   2121:   switch(ob.tag) {
                   2122:   case Sdollar: break;
                   2123:   default:
                   2124:     errorKan1("%s","Argument of KtoRecords() must be a string enclosed by dollars.\n");
                   2125:     break;
                   2126:   }
                   2127:   size = strlen(ob.lc.str)+3;
                   2128:   argv = (char **) sGC_malloc((size+1)*sizeof(char *));
                   2129:   if (argv == (char **)NULL)
                   2130:     errorKan1("%s","No more memory.\n");
                   2131:   size = KtoArgvbyCurryBrace(ob.lc.str,argv,size);
                   2132:   if (size < 0)
                   2133:     errorKan1("%s"," KtoRecords(): You have an error in the argument.\n");
                   2134:
                   2135:   obj = newObjectArray(size);
                   2136:   for (i=0; i<size; i++) {
                   2137:     tmp.tag = Sdollar;
                   2138:     tmp.lc.str = argv[i];
                   2139:     (obj.rc.op)[i] = tmp;
                   2140:   }
                   2141:   return(obj);
                   2142: }
                   2143:
                   2144: int KtoArgvbyCurryBrace(str,argv,limit)
1.7       takayama 2145:      char *str;
                   2146:      char *argv[];
                   2147:      int limit;
                   2148:      /* This function returns argc */
                   2149:      /* decompose into tokens by the separators
1.1       maekawa  2150:    { }, [ ], and characters of which code is less than SPACE.
                   2151:    Example.   { }  ---> nothing            (argc=0)
                   2152:               {x}----> x                   (argc=1)
                   2153:               {x,y} --> x   y              (argc=2)
1.7       takayama 2154:           {ab, y, z } --> ab   y   z   (argc=3)
1.1       maekawa  2155:               [[ab],c,d]  --> [ab] c   d
                   2156: */
                   2157: {
                   2158:   int argc;
                   2159:   int n;
                   2160:   int i;
                   2161:   int k;
                   2162:   char *a;
                   2163:   char *ident;
                   2164:   int level = 0;
                   2165:   int comma;
                   2166:
                   2167:   if (str == (char *)NULL) {
                   2168:     fprintf(stderr,"You use NULL string to toArgvbyCurryBrace()\n");
                   2169:     return(0);
                   2170:   }
                   2171:
                   2172:   n = strlen(str);
                   2173:   a = (char *) sGC_malloc(sizeof(char)*(n+3));
                   2174:   a[0]=' ';
                   2175:   strcpy(&(a[1]),str);
                   2176:   n = strlen(a); a[0] = '\0';
                   2177:   comma = -1;
                   2178:   for (i=1; i<n; i++) {
                   2179:     if (a[i] == '{' || a[i] == '[') level++;
                   2180:     if (level <= 1 && ( a[i] == ',')) {a[i] = '\0'; ++comma;}
                   2181:     if (level <= 1 && (a[i]=='{' || a[i]=='}' || a[i]=='[' || a[i]==']'))
                   2182:       a[i] = '\0';
                   2183:     if (a[i] == '}' || a[i] == ']') level--;
                   2184:     if ((level <= 1) && (comma == -1) && ( a[i] > ' ')) comma = 0;
                   2185:   }
                   2186:
                   2187:   if (comma == -1) return(0);
                   2188:
                   2189:   argc=0;
                   2190:   for (i=0; i<n; i++) {
                   2191:     if ((a[i] == '\0') && (a[i+1] != '\0')) ++argc;
                   2192:   }
                   2193:   if (argc > limit) return(-argc);
                   2194:
                   2195:   k = 0;
                   2196:   for (i=0; i<n; i++) {
                   2197:     if ((a[i] == '\0') && (a[i+1] != '\0')) {
                   2198:       ident = (char *) sGC_malloc(sizeof(char)*( strlen(&(a[i+1])) + 3));
                   2199:       strcpy(ident,&(a[i+1]));
                   2200:       argv[k] = KremoveSpace(ident);
                   2201:       if (argv[k] != (char *)NULL) k++;
                   2202:       if (k >= limit) errorKan1("%s\n","KtoArgvbyCurryBraces(): k>=limit.");
                   2203:     }
                   2204:   }
                   2205:   argc = k;
                   2206:   /*for (i=0; i<argc; i++) fprintf(stderr,"%d %s\n",i,argv[i]);*/
                   2207:   return(argc);
                   2208: }
                   2209:
1.14      takayama 2210: struct object KstringToArgv(struct object ob) {
                   2211:   struct object rob;
                   2212:   char *s;
                   2213:   int n,wc,i,inblank;
                   2214:   char **argv;
                   2215:   if (ob.tag != Sdollar)
1.22      takayama 2216:     errorKan1("%s\n","KstringToArgv(): the argument must be a string.");
1.14      takayama 2217:   n = strlen(KopString(ob));
                   2218:   s = (char *) sGC_malloc(sizeof(char)*(n+2));
                   2219:   if (s == NULL) errorKan1("%s\n","KstringToArgv(): No memory.");
                   2220:   strcpy(s,KopString(ob));
                   2221:   inblank = 1;  wc = 0;
                   2222:   for (i=0; i<n; i++) {
1.22      takayama 2223:     if (inblank && (s[i] > ' ')) {
                   2224:       wc++; inblank = 0;
                   2225:     }else if ((!inblank) && (s[i] <= ' ')) {
                   2226:       inblank = 1;
                   2227:     }
1.14      takayama 2228:   }
                   2229:   argv = (char **) sGC_malloc(sizeof(char *)*(wc+2));
                   2230:   argv[0] = NULL;
                   2231:   inblank = 1;  wc = 0;
                   2232:   for (i=0; i<n; i++) {
1.22      takayama 2233:     if (inblank && (s[i] > ' ')) {
                   2234:       argv[wc] = &(s[i]); argv[wc+1]=NULL;
                   2235:       wc++; inblank = 0;
                   2236:     }else if ((inblank == 0) && (s[i] <= ' ')) {
                   2237:       inblank = 1; s[i] = 0;
                   2238:     }else if (inblank && (s[i] <= ' ')) {
                   2239:       s[i] = 0;
                   2240:     }
1.14      takayama 2241:   }
                   2242:
                   2243:   rob = newObjectArray(wc);
                   2244:   for (i=0; i<wc; i++) {
1.22      takayama 2245:     putoa(rob,i,KpoString(argv[i]));
                   2246:     /* printf("%s\n",argv[i]); */
1.14      takayama 2247:   }
                   2248:   return(rob);
                   2249: }
1.1       maekawa  2250:
                   2251: static void checkDuplicateName(xvars,dvars,n)
1.7       takayama 2252:      char *xvars[];
                   2253:      char *dvars[];
                   2254:      int n;
1.1       maekawa  2255: {
                   2256:   int i,j;
                   2257:   char *names[N0*2];
                   2258:   for (i=0; i<n; i++) {
                   2259:     names[i] = xvars[i]; names[i+n] = dvars[i];
                   2260:   }
                   2261:   n = 2*n;
                   2262:   for (i=0; i<n; i++) {
                   2263:     for (j=i+1; j<n; j++) {
                   2264:       if (strcmp(names[i],names[j]) == 0) {
1.7       takayama 2265:         fprintf(stderr,"\n%d=%s, %d=%s\n",i,names[i],j,names[j]);
                   2266:         errorKan1("%s\n","Duplicate definition of the name above in SetUpRing().");
1.1       maekawa  2267:       }
                   2268:     }
                   2269:   }
                   2270: }
                   2271:
1.20      takayama 2272: struct object KooPower(struct object ob1,struct object ob2) {
                   2273:   struct object rob;
                   2274:   /* Bug. It has not yet been implemented. */
                   2275:   if (QuoteMode) {
1.22      takayama 2276:     rob = powerTree(ob1,ob2);
1.20      takayama 2277:   }else{
1.22      takayama 2278:     warningKan("KooDiv2() has not supported yet these objects.\n");
1.20      takayama 2279:   }
                   2280:   return(rob);
                   2281: }
1.1       maekawa  2282:
                   2283:
                   2284:
                   2285: struct object KooDiv2(ob1,ob2)
1.7       takayama 2286:      struct object ob1,ob2;
1.1       maekawa  2287: {
                   2288:   struct object rob = NullObject;
                   2289:   POLY f;
                   2290:   extern struct ring *CurrentRingp;
                   2291:   int s,i;
                   2292:   double d;
                   2293:
                   2294:   switch (Lookup[ob1.tag][ob2.tag]) {
                   2295:   case SpolySpoly:
                   2296:   case SuniversalNumberSuniversalNumber:
                   2297:   case SuniversalNumberSpoly:
                   2298:   case SpolySuniversalNumber:
                   2299:     rob = KnewRationalFunction0(copyObjectp(&ob1),copyObjectp(&ob2));
                   2300:     KisInvalidRational(&rob);
                   2301:     return(rob);
                   2302:     break;
                   2303:   case SarraySpoly:
                   2304:   case SarraySuniversalNumber:
                   2305:   case SarraySrationalFunction:
                   2306:     s = getoaSize(ob1);
                   2307:     rob = newObjectArray(s);
                   2308:     for (i=0; i<s; i++) {
                   2309:       putoa(rob,i,KooDiv2(getoa(ob1,i),ob2));
                   2310:     }
                   2311:     return(rob);
                   2312:     break;
                   2313:   case SpolySrationalFunction:
                   2314:   case SrationalFunctionSpoly:
                   2315:   case SrationalFunctionSrationalFunction:
                   2316:   case SuniversalNumberSrationalFunction:
                   2317:   case SrationalFunctionSuniversalNumber:
                   2318:     rob = KoInverse(ob2);
                   2319:     rob = KooMult(ob1,rob);
                   2320:     return(rob);
                   2321:     break;
                   2322:
                   2323:   case SdoubleSdouble:
                   2324:     d = KopDouble(ob2);
                   2325:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2326:     return(KpoDouble( KopDouble(ob1) / d ));
                   2327:     break;
                   2328:   case SdoubleSinteger:
                   2329:   case SdoubleSuniversalNumber:
                   2330:   case SdoubleSrationalFunction:
                   2331:     d = toDouble0(ob2);
                   2332:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2333:     return(KpoDouble( KopDouble(ob1) / d) );
                   2334:     break;
                   2335:   case SintegerSdouble:
                   2336:   case SuniversalNumberSdouble:
                   2337:   case SrationalFunctionSdouble:
                   2338:     d = KopDouble(ob2);
                   2339:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2340:     return(KpoDouble( toDouble0(ob1) / d ) );
                   2341:     break;
                   2342:
                   2343:   default:
1.20      takayama 2344:     if (QuoteMode) {
                   2345:       rob = divideTree(ob1,ob2);
                   2346:     }else{
                   2347:       warningKan("KooDiv2() has not supported yet these objects.\n");
                   2348:     }
1.1       maekawa  2349:     break;
                   2350:   }
                   2351:   return(rob);
                   2352: }
                   2353: /* Template
                   2354:   case SrationalFunctionSrationalFunction:
                   2355:     warningKan("Koo() has not supported yet these objects.\n");
                   2356:     return(rob);
                   2357:     break;
                   2358:   case SpolySrationalFunction:
                   2359:     warningKan("Koo() has not supported yet these objects.\n");
                   2360:     return(rob);
                   2361:     break;
                   2362:   case SrationalFunctionSpoly:
                   2363:     warningKan("Koo() has not supported yet these objects.\n");
                   2364:     return(rob);
                   2365:     break;
                   2366:   case SuniversalNumberSrationalFunction:
                   2367:     warningKan("Koo() has not supported yet these objects.\n");
                   2368:     return(rob);
                   2369:     break;
                   2370:   case SrationalFunctionSuniversalNumber:
                   2371:     warningKan("Koo() has not supported yet these objects.\n");
                   2372:     return(rob);
                   2373:     break;
                   2374: */
                   2375:
                   2376: int KisInvalidRational(op)
1.7       takayama 2377:      objectp op;
1.1       maekawa  2378: {
                   2379:   extern struct coeff *UniversalZero;
                   2380:   if (op->tag != SrationalFunction) return(0);
                   2381:   if (KisZeroObject(Kdenominator(*op))) {
                   2382:     errorKan1("%s\n","KisInvalidRational(): zero division. You have f/0.");
                   2383:   }
                   2384:   if (KisZeroObject(Knumerator(*op))) {
                   2385:     op->tag = SuniversalNumber;
                   2386:     op->lc.universalNumber = UniversalZero;
                   2387:   }
                   2388:   return(0);
                   2389: }
                   2390:
                   2391: struct object KgbExtension(struct object obj)
                   2392: {
                   2393:   char *key;
                   2394:   int size;
                   2395:   struct object keyo;
                   2396:   struct object rob = NullObject;
                   2397:   struct object obj1,obj2,obj3;
                   2398:   POLY f1;
                   2399:   POLY f2;
                   2400:   POLY f3;
                   2401:   POLY f;
                   2402:   int m,i;
                   2403:   struct pairOfPOLY pf;
1.16      takayama 2404:   struct coeff *cont;
1.1       maekawa  2405:
                   2406:   if (obj.tag != Sarray) errorKan1("%s\n","KgbExtension(): The argument must be an array.");
                   2407:   size = getoaSize(obj);
                   2408:   if (size < 1) errorKan1("%s\n","KgbExtension(): Empty array.");
                   2409:   keyo = getoa(obj,0);
                   2410:   if (keyo.tag != Sdollar) errorKan1("%s\n","KgbExtension(): No key word.");
                   2411:   key = KopString(keyo);
                   2412:
                   2413:   /* branch by the key word. */
                   2414:   if (strcmp(key,"isReducible")==0) {
                   2415:     if (size != 3) errorKan1("%s\n","[(isReducible)  poly1 poly2] gbext.");
                   2416:     obj1 = getoa(obj,1);
                   2417:     obj2 = getoa(obj,2);
                   2418:     if (obj1.tag != Spoly || obj2.tag != Spoly)
                   2419:       errorKan1("%s\n","[(isReducible)  poly1 poly2] gb.");
                   2420:     f1 = KopPOLY(obj1);
                   2421:     f2 = KopPOLY(obj2);
                   2422:     rob = KpoInteger((*isReducible)(f1,f2));
                   2423:   }else if (strcmp(key,"lcm") == 0) {
                   2424:     if (size != 3) errorKan1("%s\n","[(lcm)  poly1 poly2] gb.");
                   2425:     obj1 = getoa(obj,1);
                   2426:     obj2 = getoa(obj,2);
                   2427:     if (obj1.tag != Spoly || obj2.tag != Spoly)
                   2428:       errorKan1("%s\n","[(lcm)  poly1 poly2] gbext.");
                   2429:     f1 = KopPOLY(obj1);
                   2430:     f2 = KopPOLY(obj2);
                   2431:     rob = KpoPOLY((*lcm)(f1,f2));
                   2432:   }else if (strcmp(key,"grade")==0) {
                   2433:     if (size != 2) errorKan1("%s\n","[(grade)  poly1 ] gbext.");
                   2434:     obj1 = getoa(obj,1);
                   2435:     if (obj1.tag != Spoly)
                   2436:       errorKan1("%s\n","[(grade)  poly1 ] gbext.");
                   2437:     f1 = KopPOLY(obj1);
                   2438:     rob = KpoInteger((*grade)(f1));
                   2439:   }else if (strcmp(key,"mod")==0) {
                   2440:     if (size != 3) errorKan1("%s\n","[(mod) poly num] gbext");
                   2441:     obj1 = getoa(obj,1);
                   2442:     obj2 = getoa(obj,2);
                   2443:     if (obj1.tag != Spoly || obj2.tag != SuniversalNumber) {
                   2444:       errorKan1("%s\n","The datatype of the argument mismatch: [(mod) polynomial  universalNumber] gbext");
                   2445:     }
                   2446:     rob = KpoPOLY( modulopZ(KopPOLY(obj1),KopUniversalNumber(obj2)) );
                   2447:   }else if (strcmp(key,"tomodp")==0) {
                   2448:     /* The ring must be a ring of characteristic p. */
                   2449:     if (size != 3) errorKan1("%s\n","[(tomod) poly ring] gbext");
                   2450:     obj1 = getoa(obj,1);
                   2451:     obj2 = getoa(obj,2);
                   2452:     if (obj1.tag != Spoly || obj2.tag != Sring) {
                   2453:       errorKan1("%s\n","The datatype of the argument mismatch: [(tomod) polynomial  ring] gbext");
                   2454:     }
                   2455:     rob = KpoPOLY( modulop(KopPOLY(obj1),KopRingp(obj2)) );
                   2456:   }else if (strcmp(key,"tomod0")==0) {
                   2457:     /* Ring must be a ring of characteristic 0. */
                   2458:     if (size != 3) errorKan1("%s\n","[(tomod0) poly ring] gbext");
                   2459:     obj1 = getoa(obj,1);
                   2460:     obj2 = getoa(obj,2);
                   2461:     if (obj1.tag != Spoly || obj2.tag != Sring) {
                   2462:       errorKan1("%s\n","The datatype of the argument mismatch: [(tomod0) polynomial  ring] gbext");
                   2463:     }
                   2464:     errorKan1("%s\n","It has not been implemented.");
                   2465:     rob = KpoPOLY( POLYNULL );
                   2466:   }else if (strcmp(key,"divByN")==0) {
                   2467:     if (size != 3) errorKan1("%s\n","[(divByN) poly num] gbext");
                   2468:     obj1 = getoa(obj,1);
                   2469:     obj2 = getoa(obj,2);
                   2470:     if (obj1.tag != Spoly || obj2.tag != SuniversalNumber) {
                   2471:       errorKan1("%s\n","The datatype of the argument mismatch: [(divByN) polynomial  universalNumber] gbext");
                   2472:     }
                   2473:     pf =  quotientByNumber(KopPOLY(obj1),KopUniversalNumber(obj2));
                   2474:     rob  = newObjectArray(2);
                   2475:     putoa(rob,0,KpoPOLY(pf.first));
                   2476:     putoa(rob,1,KpoPOLY(pf.second));
                   2477:   }else if (strcmp(key,"isConstant")==0) {
                   2478:     if (size != 2) errorKan1("%s\n","[(isConstant) poly ] gbext bool");
                   2479:     obj1 = getoa(obj,1);
                   2480:     if (obj1.tag != Spoly) {
                   2481:       errorKan1("%s\n","The datatype of the argument mismatch: [(isConstant) polynomial] gbext");
                   2482:     }
                   2483:     return(KpoInteger(isConstant(KopPOLY(obj1))));
1.18      takayama 2484:   }else if (strcmp(key,"isConstantAll")==0) {
                   2485:     if (size != 2) errorKan1("%s\n","[(isConstantAll) poly ] gbext bool");
                   2486:     obj1 = getoa(obj,1);
                   2487:     if (obj1.tag != Spoly) {
                   2488:       errorKan1("%s\n","The datatype of the argument mismatch: [(isConstantAll) polynomial] gbext");
                   2489:     }
                   2490:     return(KpoInteger(isConstantAll(KopPOLY(obj1))));
1.1       maekawa  2491:   }else if (strcmp(key,"schreyerSkelton") == 0) {
                   2492:     if (size != 2) errorKan1("%s\n","[(schreyerSkelton) array_of_poly ] gbext array");
                   2493:     obj1 = getoa(obj,1);
                   2494:     return(KschreyerSkelton(obj1));
                   2495:   }else if (strcmp(key,"lcoeff") == 0) {
                   2496:     if (size != 2) errorKan1("%s\n","[(lcoeff) poly] gbext poly");
                   2497:     obj1 = getoa(obj,1);
                   2498:     if (obj1.tag != Spoly) errorKan1("%s\n","[(lcoeff) poly] gbext poly");
                   2499:     f = KopPOLY(obj1);
                   2500:     if (f == POLYNULL) return(KpoPOLY(f));
                   2501:     return(KpoPOLY( newCell(coeffCopy(f->coeffp),newMonomial(f->m->ringp))));
                   2502:   }else if (strcmp(key,"lmonom") == 0) {
                   2503:     if (size != 2) errorKan1("%s\n","[(lmonom) poly] gbext poly");
                   2504:     obj1 = getoa(obj,1);
                   2505:     if (obj1.tag != Spoly) errorKan1("%s\n","[(lmonom) poly] gbext poly");
                   2506:     f = KopPOLY(obj1);
                   2507:     if (f == POLYNULL) return(KpoPOLY(f));
                   2508:     return(KpoPOLY( newCell(intToCoeff(1,f->m->ringp),monomialCopy(f->m))));
                   2509:   }else if (strcmp(key,"toes") == 0) {
                   2510:     if (size != 2) errorKan1("%s\n","[(toes) array] gbext poly");
                   2511:     obj1 = getoa(obj,1);
                   2512:     if (obj1.tag != Sarray) errorKan1("%s\n","[(toes) array] gbext poly");
                   2513:     return(KvectorToSchreyer_es(obj1));
1.3       takayama 2514:   }else if (strcmp(key,"toe_") == 0) {
                   2515:     if (size != 2) errorKan1("%s\n","[(toe_) array] gbext poly");
                   2516:     obj1 = getoa(obj,1);
                   2517:     if (obj1.tag == Spoly) return(obj1);
                   2518:     if (obj1.tag != Sarray) errorKan1("%s\n","[(toe_) array] gbext poly");
                   2519:     return(KpoPOLY(arrayToPOLY(obj1)));
1.1       maekawa  2520:   }else if (strcmp(key,"isOrdered") == 0) {
                   2521:     if (size != 2) errorKan1("%s\n","[(isOrdered) poly] gbext poly");
                   2522:     obj1 = getoa(obj,1);
                   2523:     if (obj1.tag != Spoly) errorKan1("%s\n","[(isOrdered) poly] gbext poly");
                   2524:     return(KisOrdered(obj1));
1.16      takayama 2525:   }else if (strcmp(key,"reduceContent")==0) {
                   2526:     if (size != 2) errorKan1("%s\n","[(reduceContent)  poly1 ] gbext.");
                   2527:     obj1 = getoa(obj,1);
                   2528:     if (obj1.tag != Spoly)
                   2529:       errorKan1("%s\n","[(reduceContent)  poly1 ] gbext.");
                   2530:     f1 = KopPOLY(obj1);
1.22      takayama 2531:     rob = newObjectArray(2);
                   2532:     f1 = reduceContentOfPoly(f1,&cont);
                   2533:     putoa(rob,0,KpoPOLY(f1));
                   2534:     if (f1 == POLYNULL) {
                   2535:       putoa(rob,1,KpoPOLY(f1));
                   2536:     }else{
                   2537:       putoa(rob,1,KpoPOLY(newCell(cont,newMonomial(f1->m->ringp))));
                   2538:     }
1.17      takayama 2539:   }else if (strcmp(key,"ord_ws_all")==0) {
                   2540:     if (size != 3) errorKan1("%s\n","[(ord_ws_all) fv wv] gbext");
                   2541:     obj1 = getoa(obj,1);
                   2542:     obj2 = getoa(obj,2);
                   2543:     rob  = KordWsAll(obj1,obj2);
1.23      takayama 2544:   }else if (strcmp(key,"exponents")==0) {
                   2545:     if (size == 3) {
                   2546:       obj1 = getoa(obj,1);
                   2547:       obj2 = getoa(obj,2);
                   2548:       rob  = KgetExponents(obj1,obj2);
                   2549:     }else if (size == 2) {
                   2550:       obj1 = getoa(obj,1);
                   2551:       obj2 = KpoInteger(2);
                   2552:       rob  = KgetExponents(obj1,obj2);
                   2553:     }else{
                   2554:       errorKan1("%s\n","[(exponents) f type] gbext");
                   2555:     }
1.1       maekawa  2556:   }else {
                   2557:     errorKan1("%s\n","gbext : unknown tag.");
                   2558:   }
                   2559:   return(rob);
                   2560: }
                   2561:
                   2562: struct object KmpzExtension(struct object obj)
                   2563: {
                   2564:   char *key;
                   2565:   int size;
                   2566:   struct object keyo;
                   2567:   struct object rob = NullObject;
                   2568:   struct object obj0,obj1,obj2,obj3;
                   2569:   MP_INT *f;
                   2570:   MP_INT *g;
                   2571:   MP_INT *h;
                   2572:   MP_INT *r0;
                   2573:   MP_INT *r1;
                   2574:   MP_INT *r2;
                   2575:   int gi;
                   2576:   extern struct ring *SmallRingp;
                   2577:
                   2578:
                   2579:   if (obj.tag != Sarray) errorKan1("%s\n","KmpzExtension(): The argument must be an array.");
                   2580:   size = getoaSize(obj);
                   2581:   if (size < 1) errorKan1("%s\n","KmpzExtension(): Empty array.");
                   2582:   keyo = getoa(obj,0);
                   2583:   if (keyo.tag != Sdollar) errorKan1("%s\n","KmpzExtension(): No key word.");
                   2584:   key = KopString(keyo);
                   2585:
                   2586:   /* branch by the key word. */
                   2587:   if (strcmp(key,"gcd")==0) {
                   2588:     if (size != 3) errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2589:     obj1 = getoa(obj,1);
                   2590:     obj2 = getoa(obj,2);
1.24      takayama 2591:     if (obj1.tag != SuniversalNumber) {
                   2592:       obj1 = KdataConversion(obj1,"universalNumber");
                   2593:        }
                   2594:     if (obj2.tag != SuniversalNumber) {
                   2595:       obj2 = KdataConversion(obj2,"universalNumber");
                   2596:        }
1.1       maekawa  2597:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2598:       errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2599:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2600:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2601:       errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2602:     }
                   2603:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2604:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2605:     r1 = newMP_INT();
                   2606:     mpz_gcd(r1,f,g);
                   2607:     rob.tag = SuniversalNumber;
                   2608:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2609:   }else if (strcmp(key,"tdiv_qr")==0) {
                   2610:     if (size != 3) errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2611:     obj1 = getoa(obj,1);
                   2612:     obj2 = getoa(obj,2);
1.24      takayama 2613:     if (obj1.tag != SuniversalNumber) {
                   2614:       obj1 = KdataConversion(obj1,"universalNumber");
                   2615:        }
                   2616:     if (obj2.tag != SuniversalNumber) {
                   2617:       obj2 = KdataConversion(obj2,"universalNumber");
                   2618:        }
1.1       maekawa  2619:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2620:       errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2621:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2622:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2623:       errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2624:     }
                   2625:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2626:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2627:     r1 = newMP_INT();
                   2628:     r2 = newMP_INT();
                   2629:     mpz_tdiv_qr(r1,r2,f,g);
                   2630:     obj1.tag = SuniversalNumber;
                   2631:     obj1.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2632:     obj2.tag = SuniversalNumber;
                   2633:     obj2.lc.universalNumber = mpintToCoeff(r2,SmallRingp);
                   2634:     rob = newObjectArray(2);
                   2635:     putoa(rob,0,obj1); putoa(rob,1,obj2);
                   2636:   } else if (strcmp(key,"cancel")==0) {
                   2637:     if (size != 2) {
                   2638:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2639:     }
                   2640:     obj0 = getoa(obj,1);
                   2641:     if (obj0.tag == SuniversalNumber) return(obj0);
                   2642:     if (obj0.tag != SrationalFunction) {
                   2643:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2644:       return(obj0);
                   2645:     }
                   2646:     obj1 = *(Knumerator(obj0));
                   2647:     obj2 = *(Kdenominator(obj0));
                   2648:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber) {
                   2649:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2650:       return(obj0);
                   2651:     }
                   2652:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2653:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2654:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2655:     }
                   2656:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2657:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2658:
                   2659:     r0 = newMP_INT();
                   2660:     r1 = newMP_INT();
                   2661:     r2 = newMP_INT();
                   2662:     mpz_gcd(r0,f,g);
                   2663:     mpz_divexact(r1,f,r0);
                   2664:     mpz_divexact(r2,g,r0);
                   2665:     obj1.tag = SuniversalNumber;
                   2666:     obj1.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2667:     obj2.tag = SuniversalNumber;
                   2668:     obj2.lc.universalNumber = mpintToCoeff(r2,SmallRingp);
                   2669:
                   2670:     rob = KnewRationalFunction0(copyObjectp(&obj1),copyObjectp(&obj2));
                   2671:     KisInvalidRational(&rob);
                   2672:   }else if (strcmp(key,"sqrt")==0 ||
1.7       takayama 2673:             strcmp(key,"com")==0) {
1.1       maekawa  2674:     /*  One arg functions  */
                   2675:     if (size != 2) errorKan1("%s\n","[key num] mpzext");
                   2676:     obj1 = getoa(obj,1);
1.24      takayama 2677:     if (obj1.tag != SuniversalNumber) {
                   2678:       obj1 = KdataConversion(obj1,"universalNumber");
                   2679:        }
1.1       maekawa  2680:     if (obj1.tag != SuniversalNumber)
                   2681:       errorKan1("%s\n","[key num] mpzext : num must be a universalNumber.");
                   2682:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber))
                   2683:       errorKan1("%s\n","[key num] mpzext : num must be a universalNumber.");
                   2684:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2685:     if (strcmp(key,"sqrt")==0) {
                   2686:       r1 = newMP_INT();
                   2687:       mpz_sqrt(r1,f);
                   2688:     }else if (strcmp(key,"com")==0) {
                   2689:       r1 = newMP_INT();
                   2690:       mpz_com(r1,f);
                   2691:     }
                   2692:     rob.tag = SuniversalNumber;
                   2693:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2694:   }else if (strcmp(key,"probab_prime_p")==0 ||
1.7       takayama 2695:             strcmp(key,"and") == 0 ||
                   2696:             strcmp(key,"ior")==0) {
1.1       maekawa  2697:     /* Two args functions */
                   2698:     if (size != 3) errorKan1("%s\n","[key  num1 num2] mpzext.");
                   2699:     obj1 = getoa(obj,1);
                   2700:     obj2 = getoa(obj,2);
1.24      takayama 2701:     if (obj1.tag != SuniversalNumber) {
                   2702:       obj1 = KdataConversion(obj1,"universalNumber");
                   2703:        }
                   2704:     if (obj2.tag != SuniversalNumber) {
                   2705:       obj2 = KdataConversion(obj2,"universalNumber");
                   2706:        }
1.1       maekawa  2707:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2708:       errorKan1("%s\n","[key num1 num2] mpzext.");
                   2709:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2710:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2711:       errorKan1("%s\n","[key  num1 num2] mpzext.");
                   2712:     }
                   2713:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2714:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2715:     if (strcmp(key,"probab_prime_p")==0) {
                   2716:       gi = (int) mpz_get_si(g);
                   2717:       if (mpz_probab_prime_p(f,gi)) {
1.7       takayama 2718:         rob = KpoInteger(1);
1.1       maekawa  2719:       }else {
1.7       takayama 2720:         rob = KpoInteger(0);
1.1       maekawa  2721:       }
                   2722:     }else if (strcmp(key,"and")==0) {
                   2723:       r1 = newMP_INT();
                   2724:       mpz_and(r1,f,g);
                   2725:       rob.tag = SuniversalNumber;
                   2726:       rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2727:     }else if (strcmp(key,"ior")==0) {
                   2728:       r1 = newMP_INT();
                   2729:       mpz_ior(r1,f,g);
                   2730:       rob.tag = SuniversalNumber;
                   2731:       rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2732:     }
                   2733:
                   2734:   }else if (strcmp(key,"powm")==0) {
                   2735:     /* three args */
                   2736:     if (size != 4) errorKan1("%s\n","[key num1 num2 num3] mpzext");
                   2737:     obj1 = getoa(obj,1); obj2 = getoa(obj,2); obj3 = getoa(obj,3);
1.24      takayama 2738:     if (obj1.tag != SuniversalNumber) {
                   2739:       obj1 = KdataConversion(obj1,"universalNumber");
                   2740:        }
                   2741:     if (obj2.tag != SuniversalNumber) {
                   2742:       obj2 = KdataConversion(obj2,"universalNumber");
                   2743:        }
                   2744:     if (obj3.tag != SuniversalNumber) {
                   2745:       obj3 = KdataConversion(obj3,"universalNumber");
                   2746:        }
1.1       maekawa  2747:     if (obj1.tag != SuniversalNumber ||
                   2748:         obj2.tag != SuniversalNumber ||
                   2749:         obj3.tag != SuniversalNumber ) {
                   2750:       errorKan1("%s\n","[key num1 num2 num3] mpzext : num1, num2 and num3 must be universalNumbers.");
                   2751:     }
                   2752:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2753:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber) ||
                   2754:         ! is_this_coeff_MP_INT(obj3.lc.universalNumber)) {
1.1       maekawa  2755:       errorKan1("%s\n","[key num1 num2 num3] mpzext : num1, num2 and num3 must be universalNumbers.");
                   2756:     }
                   2757:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2758:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2759:     h = coeff_to_MP_INT(obj3.lc.universalNumber);
                   2760:     if (mpz_sgn(g) < 0) errorKan1("%s\n","[(powm) base exp mod] mpzext : exp must not be negative.");
                   2761:     r1 = newMP_INT();
                   2762:     mpz_powm(r1,f,g,h);
                   2763:     rob.tag = SuniversalNumber;
                   2764:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
1.24      takayama 2765:   } else if (strcmp(key,"lcm")==0) {
                   2766:     if (size != 3) errorKan1("%s\n","[(lcm)  universalNumber universalNumber] mpzext.");
                   2767:     obj1 = getoa(obj,1);
                   2768:     obj2 = getoa(obj,2);
                   2769:     if (obj1.tag != SuniversalNumber) {
                   2770:       obj1 = KdataConversion(obj1,"universalNumber");
                   2771:        }
                   2772:     if (obj2.tag != SuniversalNumber) {
                   2773:       obj2 = KdataConversion(obj2,"universalNumber");
                   2774:        }
                   2775:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2776:       errorKan1("%s\n","[lcm num1 num2] mpzext.");
                   2777:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
                   2778:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
                   2779:       errorKan1("%s\n","[(lcm)  universalNumber universalNumber] mpzext.");
                   2780:     }
                   2781:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2782:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2783:     r1 = newMP_INT();
                   2784:     mpz_lcm(r1,f,g);
                   2785:     rob.tag = SuniversalNumber;
                   2786:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
1.1       maekawa  2787:   }else {
                   2788:     errorKan1("%s\n","mpzExtension(): Unknown tag.");
                   2789:   }
                   2790:   return(rob);
                   2791: }
                   2792:
                   2793:
                   2794: /** : context   */
                   2795: struct object KnewContext(struct object superObj,char *name) {
                   2796:   struct context *cp;
                   2797:   struct object ob;
                   2798:   if (superObj.tag != Sclass) {
                   2799:     errorKan1("%s\n","The argument of KnewContext must be a Class.Context");
                   2800:   }
                   2801:   if (superObj.lc.ival != CLASSNAME_CONTEXT) {
                   2802:     errorKan1("%s\n","The argument of KnewContext must be a Class.Context");
                   2803:   }
                   2804:   cp = newContext0((struct context *)(superObj.rc.voidp),name);
                   2805:   ob.tag = Sclass;
                   2806:   ob.lc.ival = CLASSNAME_CONTEXT;
                   2807:   ob.rc.voidp = cp;
                   2808:   return(ob);
                   2809: }
                   2810:
                   2811: struct object KcreateClassIncetance(struct object ob1,
1.7       takayama 2812:                                     struct object ob2,
                   2813:                                     struct object ob3)
1.1       maekawa  2814: {
                   2815:   /* [class-tag super-obj] size [class-tag]  cclass */
                   2816:   struct object ob4;
                   2817:   int size,size2,i;
                   2818:   struct object ob5;
                   2819:   struct object rob;
                   2820:
                   2821:   if (ob1.tag != Sarray)
                   2822:     errorKan1("%s\n","cclass: The first argument must be an array.");
                   2823:   if (getoaSize(ob1) < 1)
                   2824:     errorKan1("%s\n","cclass: The first argument must be [class-tag ....].");
                   2825:   ob4 = getoa(ob1,0);
                   2826:   if (ectag(ob4) != CLASSNAME_CONTEXT)
                   2827:     errorKan1("%s\n","cclass: The first argument must be [class-tag ....].");
                   2828:
                   2829:   if (ob2.tag != Sinteger)
                   2830:     errorKan1("%s\n","cclass: The second argument must be an integer.");
                   2831:   size = KopInteger(ob2);
                   2832:   if (size < 1)
                   2833:     errorKan1("%s\n","cclass: The size must be > 0.");
                   2834:
                   2835:   if (ob3.tag != Sarray)
                   2836:     errorKan1("%s\n","cclass: The third argument must be an array.");
                   2837:   if (getoaSize(ob3) < 1)
                   2838:     errorKan1("%s\n","cclass: The third argument must be [class-tag].");
                   2839:   ob5 = getoa(ob3,0);
                   2840:   if (ectag(ob5) != CLASSNAME_CONTEXT)
                   2841:     errorKan1("%s\n","cclass: The third argument must be [class-tag].");
1.7       takayama 2842:
1.1       maekawa  2843:   rob = newObjectArray(size);
                   2844:   putoa(rob,0,ob5);
                   2845:   if (getoaSize(ob1) < size) size2 = getoaSize(ob1);
                   2846:   else size2 = size;
                   2847:   for (i=1; i<size2; i++) {
                   2848:     putoa(rob,i,getoa(ob1,i));
                   2849:   }
                   2850:   for (i=size2; i<size; i++) {
                   2851:     putoa(rob,i,NullObject);
                   2852:   }
                   2853:   return(rob);
                   2854: }
                   2855:
                   2856:
                   2857: struct object KpoDouble(double a) {
                   2858:   struct object rob;
                   2859:   rob.tag = Sdouble;
                   2860:   /* rob.lc.dbl = (double *)sGC_malloc_atomic(sizeof(double)); */
                   2861:   rob.lc.dbl = (double *)sGC_malloc(sizeof(double));
                   2862:   if (rob.lc.dbl == (double *)NULL) {
                   2863:     fprintf(stderr,"No memory.\n"); exit(10);
                   2864:   }
                   2865:   *(rob.lc.dbl) = a;
                   2866:   return(rob);
                   2867: }
                   2868:
                   2869: double toDouble0(struct object ob) {
                   2870:   double r;
                   2871:   int r3;
                   2872:   struct object ob2;
                   2873:   struct object ob3;
                   2874:   switch(ob.tag) {
                   2875:   case Sinteger:
                   2876:     return( (double) (KopInteger(ob)) );
                   2877:   case SuniversalNumber:
                   2878:     return((double) coeffToInt(ob.lc.universalNumber));
                   2879:   case SrationalFunction:
                   2880:     /* The argument is assumed to be a rational number. */
                   2881:     ob2 = newObjectArray(2);  ob3 = KpoString("cancel");
                   2882:     putoa(ob2,0,ob3); putoa(ob2,1,ob);
                   2883:     ob = KmpzExtension(ob2);
                   2884:     ob2 = *Knumerator(ob);  ob3 = *Kdenominator(ob);
                   2885:     r3 =  coeffToInt(ob3.lc.universalNumber);
                   2886:     if (r3  == 0) {
                   2887:       errorKan1("%s\n","toDouble0(): Division by zero.");
                   2888:       break;
                   2889:     }
                   2890:     r = ((double) coeffToInt(ob2.lc.universalNumber)) / ((double)r3);
                   2891:     return(r);
                   2892:   case Sdouble:
                   2893:     return( KopDouble(ob) );
                   2894:   default:
                   2895:     errorKan1("%s\n","toDouble0(): This type of conversion is not supported.");
                   2896:     break;
                   2897:   }
                   2898:   return(0.0);
                   2899: }
                   2900:
                   2901: struct object KpoGradedPolySet(struct gradedPolySet *grD) {
                   2902:   struct object rob;
                   2903:   rob.tag = Sclass;
                   2904:   rob.lc.ival = CLASSNAME_GradedPolySet;
                   2905:   rob.rc.voidp = (void *) grD;
                   2906:   return(rob);
                   2907: }
                   2908:
                   2909: static char *getspace0(int a) {
                   2910:   char *s;
                   2911:   a = (a > 0? a:-a);
                   2912:   s = (char *) sGC_malloc(a+1);
                   2913:   if (s == (char *)NULL) {
                   2914:     errorKan1("%s\n","no more memory.");
                   2915:   }
                   2916:   return(s);
                   2917: }
                   2918: struct object KdefaultPolyRing(struct object ob) {
                   2919:   struct object rob;
                   2920:   int i,j,k,n;
                   2921:   struct object ob1,ob2,ob3,ob4,ob5;
                   2922:   struct object t1;
                   2923:   char *s1;
                   2924:   extern struct ring *CurrentRingp;
                   2925:   static struct ring *a[N0];
                   2926:
                   2927:   rob = NullObject;
                   2928:   if (ob.tag != Sinteger) {
                   2929:     errorKan1("%s\n","KdefaultPolyRing(): the argument must be integer.");
                   2930:   }
                   2931:   n = KopInteger(ob);
                   2932:   if (n <= 0) {
                   2933:     /* initializing */
                   2934:     for (i=0; i<N0; i++) {
                   2935:       a[i] = (struct ring*) NULL;
                   2936:     }
                   2937:     return(rob);
                   2938:   }
                   2939:
                   2940:   if ( a[n] != (struct ring*)NULL) return(KpoRingp(a[n]));
                   2941:
                   2942:   /* Let's construct ring of polynomials of 2n variables  */
                   2943:   /* x variables */
                   2944:   ob1 = newObjectArray(n);
                   2945:   for (i=0; i<n; i++) {
                   2946:     s1 = getspace0(1+ ((n-i)/10) + 1);
                   2947:     sprintf(s1,"x%d",n-i);
                   2948:     putoa(ob1,i,KpoString(s1));
                   2949:   }
                   2950:   ob2 = newObjectArray(n);
                   2951:   s1 = getspace0(1);
                   2952:   sprintf(s1,"h");
                   2953:   putoa(ob2,0,KpoString(s1));
                   2954:   for (i=1; i<n; i++) {
                   2955:     s1 = getspace0(1+((n+n-i)/10)+1);
                   2956:     sprintf(s1,"x%d",n+n-i);
                   2957:     putoa(ob2,i,KpoString(s1));
                   2958:   }
                   2959:
                   2960:   ob3 = newObjectArray(9);
                   2961:   putoa(ob3,0,KpoInteger(0));
                   2962:   for (i=1; i<9; i++) {
                   2963:     putoa(ob3,i,KpoInteger(n));
                   2964:   }
                   2965:
                   2966:   ob4 = newObjectArray(2*n);
                   2967:   t1 = newObjectArray(2*n);
                   2968:   for (i=0; i<2*n; i++) {
                   2969:     putoa(t1,i,KpoInteger(1));
                   2970:   }
                   2971:   putoa(ob4,0,t1);
                   2972:   for (i=1; i<2*n; i++) {
                   2973:     t1 = newObjectArray(2*n);
                   2974:     for (j=0; j<2*n; j++) {
                   2975:       putoa(t1,j,KpoInteger(0));
                   2976:       if (j == (2*n-i)) {
1.7       takayama 2977:         putoa(t1,j,KpoInteger(-1));
1.1       maekawa  2978:       }
                   2979:     }
                   2980:     putoa(ob4,i,t1);
                   2981:   }
                   2982:
                   2983:   ob5 = newObjectArray(2);
                   2984:   putoa(ob5,0,KpoString("mpMult"));
                   2985:   putoa(ob5,1,KpoString("poly"));
                   2986:
                   2987:   KsetUpRing(ob1,ob2,ob3,ob4,ob5);
                   2988:   a[n] = CurrentRingp;
                   2989:   return(KpoRingp(a[n]));
                   2990: }
                   2991:
                   2992:
1.31      takayama 2993: struct object Krest(struct object ob) {
                   2994:   struct object rob;
                   2995:   struct object *op;
                   2996:   int n,i;
                   2997:   if (ob.tag == Sarray) {
                   2998:     n = getoaSize(ob);
                   2999:     if (n == 0) return ob;
                   3000:     rob = newObjectArray(n-1);
                   3001:     for (i=1; i<n; i++) {
                   3002:       putoa(rob,i-1,getoa(ob,i));
                   3003:     }
                   3004:     return rob;
1.32      takayama 3005:   }else if ((ob.tag == Slist) || (ob.tag == Snull)) {
                   3006:     return Kcdr(ob);
1.31      takayama 3007:   }else{
                   3008:     errorKan1("%s\n","Krest(ob): ob must be an array or a list.");
                   3009:   }
                   3010: }
                   3011: struct object Kjoin(struct object ob1, struct object ob2) {
                   3012:   struct object rob;
                   3013:   int n1,n2,i;
                   3014:   if ((ob1.tag == Sarray) &&  (ob2.tag == Sarray)) {
                   3015:     n1 = getoaSize(ob1); n2 = getoaSize(ob2);
                   3016:     rob = newObjectArray(n1+n2);
                   3017:     for (i=0; i<n1; i++) {
                   3018:       putoa(rob,i,getoa(ob1,i));
                   3019:     }
                   3020:     for (i=n1; i<n1+n2; i++) {
                   3021:       putoa(rob,i,getoa(ob2,i-n1));
                   3022:     }
                   3023:     return rob;
1.32      takayama 3024:   }else if ((ob1.tag == Slist) || (ob1.tag == Snull)) {
                   3025:        if ((ob2.tag == Slist) || (ob2.tag == Snull)) {
                   3026:          return KvJoin(ob1,ob2);
                   3027:        }else{
                   3028:          errorKan1("%s\n","Kjoin: both argument must be a list.");
                   3029:        }
1.31      takayama 3030:   }else{
                   3031:     errorKan1("%s\n","Kjoin: arguments must be arrays.");
                   3032:   }
                   3033: }
1.1       maekawa  3034:
1.33      takayama 3035: struct object Kget(struct object ob1, struct object ob2) {
                   3036:   struct object rob;
                   3037:   struct object tob;
                   3038:   int i,j,size,n;
                   3039:   if (ob2.tag == Sinteger) {
                   3040:     i =ob2.lc.ival;
                   3041:   }else if (ob2.tag == SuniversalNumber) {
                   3042:     i = KopInteger(KdataConversion(ob2,"integer"));
                   3043:   }else if (ob2.tag == Sarray) {
                   3044:     n = getoaSize(ob2);
                   3045:     if (n == 0) return ob1;
                   3046:     rob = ob1;
                   3047:     for (i=0; i<n; i++) {
                   3048:       rob=Kget(rob,getoa(ob2,i));
                   3049:     }
                   3050:     return rob;
                   3051:   }
                   3052:   if (ob1.tag == Sarray) {
                   3053:     size = getoaSize(ob1);
                   3054:     if ((0 <= i) && (i<size)) {
                   3055:       return(getoa(ob1,i));
                   3056:     }else{
                   3057:       errorKan1("%s\n","Kget: Index is out of bound. (get)\n");
                   3058:     }
                   3059:   }else if (ob1.tag == Slist) {
                   3060:     rob = NullObject;
                   3061:     if (i < 0) errorKan1("%s\n","Kget: Index is negative. (get)");
                   3062:     for (j=0; j<i; j++) {
                   3063:       rob = Kcdr(ob1);
                   3064:       if ((ob1.tag == Snull) && (rob.tag == Snull)) {
                   3065:         errorKan1("%s\n","Kget: Index is out of bound. (get) cdr of null list.\n");
                   3066:       }
                   3067:       ob1 = rob;
                   3068:     }
                   3069:     return Kcar(ob1);
                   3070:   }else errorKan1("%s\n","Kget: argument must be an array or a list.");
                   3071: }
1.1       maekawa  3072:
                   3073: /******************************************************************
                   3074:      error handler
                   3075: ******************************************************************/
                   3076:
                   3077: errorKan1(str,message)
1.7       takayama 3078:      char *str;
                   3079:      char *message;
1.1       maekawa  3080: {
                   3081:   extern char *GotoLabel;
                   3082:   extern int GotoP;
                   3083:   extern int ErrorMessageMode;
                   3084:   char tmpc[1024];
1.10      takayama 3085:   cancelAlarm();
1.1       maekawa  3086:   if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
                   3087:     sprintf(tmpc,"\nERROR(kanExport[0|1].c): ");
                   3088:     if (strlen(message) < 900) {
                   3089:       strcat(tmpc,message);
                   3090:     }
                   3091:     pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc));
                   3092:   }
                   3093:   if (ErrorMessageMode != 1) {
                   3094:     fprintf(stderr,"\nERROR(kanExport[0|1].c): ");
                   3095:     fprintf(stderr,str,message);
1.30      takayama 3096:     (void) traceShowStack(); traceClearStack();
1.1       maekawa  3097:   }
                   3098:   /* fprintf(stderr,"Hello "); */
                   3099:   if (GotoP) {
                   3100:     /* fprintf(stderr,"Hello. GOTO "); */
                   3101:     fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel);
                   3102:     GotoP = 0;
                   3103:   }
                   3104:   stdOperandStack(); contextControl(CCRESTORE);
                   3105:   /* fprintf(stderr,"Now. Long jump!\n"); */
1.8       takayama 3106: #if defined(__CYGWIN__)
                   3107:   siglongjmp(EnvOfStackMachine,1);
                   3108: #else
1.1       maekawa  3109:   longjmp(EnvOfStackMachine,1);
1.8       takayama 3110: #endif
1.1       maekawa  3111: }
1.22      takayama 3112:
1.1       maekawa  3113:
                   3114: warningKan(str)
1.7       takayama 3115:      char *str;
1.1       maekawa  3116: {
                   3117:   extern int WarningMessageMode;
                   3118:   extern int Strict;
                   3119:   char tmpc[1024];
                   3120:   if (WarningMessageMode == 1 || WarningMessageMode == 2) {
                   3121:     sprintf(tmpc,"\nWARNING(kanExport[0|1].c): ");
                   3122:     if (strlen(str) < 900) {
                   3123:       strcat(tmpc,str);
                   3124:     }
                   3125:     pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc));
                   3126:   }
                   3127:   if (WarningMessageMode != 1) {
                   3128:     fprintf(stderr,"\nWARNING(kanExport[0|1].c): ");
                   3129:     fprintf(stderr,str);
                   3130:     fprintf(stderr,"\n");
                   3131:   }
                   3132:   /* if (Strict) errorKan1("%s\n"," "); */
                   3133:   if (Strict) errorKan1("%s\n",str);
1.4       takayama 3134:   return(0);
                   3135: }
                   3136:
                   3137: warningKanNoStrictMode(str)
1.7       takayama 3138:      char *str;
1.4       takayama 3139: {
                   3140:   extern int Strict;
                   3141:   int t;
                   3142:   t = Strict;
                   3143:   Strict = 0;
                   3144:   warningKan(str);
                   3145:   Strict = t;
1.1       maekawa  3146:   return(0);
                   3147: }
                   3148:
                   3149:
                   3150:
                   3151:

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