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

1.24    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/kanExport0.c,v 1.23 2004/07/30 11:21:55 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;
                    690:   if (obj1.tag != obj2.tag) {
                    691:     warningKan("KooEqualQ(ob1,ob2): the datatypes of ob1 and ob2  are not same. Returns false (0).\n");
                    692:     return(0);
                    693:   }
                    694:   switch(obj1.tag) {
1.7       takayama  695:   case 0:
                    696:     return(1); /* case of NullObject */
                    697:     break;
                    698:   case Sinteger:
                    699:     if (obj1.lc.ival == obj2.lc.ival) return(1);
                    700:     else return(0);
                    701:     break;
                    702:   case Sstring:
                    703:   case Sdollar:
                    704:     if (strcmp(obj1.lc.str, obj2.lc.str)==0) return(1);
                    705:     else return(0);
                    706:     break;
                    707:   case Spoly:
                    708:     ob = KooSub(obj1,obj2);
                    709:     if (KopPOLY(ob) == ZERO) return(1);
                    710:     else return(0);
                    711:   case Sarray:
                    712:     if (getoaSize(obj1) != getoaSize(obj2)) return(0);
                    713:     for (i=0; i< getoaSize(obj1); i++) {
                    714:       if (KooEqualQ(getoa(obj1,i),getoa(obj2,i))) { ; }
                    715:       else { return(0); }
                    716:     }
                    717:     return(1);
                    718:   case Slist:
                    719:     if (KooEqualQ(*(obj1.lc.op),*(obj2.lc.op))) {
                    720:       if (isNullList(obj1.rc.op)) {
                    721:         if (isNullList(obj2.rc.op)) return(1);
                    722:         else return(0);
1.1       maekawa   723:       }else{
1.7       takayama  724:         if (isNullList(obj2.rc.op)) return(0);
                    725:         return(KooEqualQ(*(obj1.rc.op),*(obj2.rc.op)));
1.1       maekawa   726:       }
1.7       takayama  727:     }else{
                    728:       return(0);
1.1       maekawa   729:     }
1.7       takayama  730:     break;
                    731:   case SuniversalNumber:
                    732:     return(coeffEqual(obj1.lc.universalNumber,obj2.lc.universalNumber));
                    733:     break;
                    734:   case Sring:
                    735:     return(KopRingp(obj1) == KopRingp(obj2));
                    736:     break;
                    737:   case Sclass:
                    738:     return(KclassEqualQ(obj1,obj2));
                    739:     break;
                    740:   case Sdouble:
                    741:     return(KopDouble(obj1) == KopDouble(obj2));
                    742:     break;
                    743:   default:
                    744:     errorKan1("%s\n","KooEqualQ() has not supported these objects yet.");
                    745:     break;
                    746:   }
1.1       maekawa   747: }
                    748:
                    749:
                    750: struct object KoIsPositive(ob1)
1.7       takayama  751:      struct object ob1;
1.1       maekawa   752: {
                    753:   struct object rob = NullObject;
                    754:   switch (ob1.tag) {
                    755:   case Sinteger:
                    756:     return(KpoInteger(ob1.lc.ival > 0));
                    757:     break;
                    758:   default:
                    759:     warningKan("KoIsPositive() has not supported yet these objects.\n");
                    760:     break;
                    761:   }
                    762:   return(rob);
                    763: }
                    764:
                    765: struct object KooGreater(obj1,obj2)
1.7       takayama  766:      struct object obj1;
                    767:      struct object obj2;
1.1       maekawa   768: {
                    769:   struct object ob;
                    770:   int tt;
                    771:   if (obj1.tag != obj2.tag) {
                    772:     errorKan1("%s\n","You cannot compare different kinds of objects.");
                    773:   }
                    774:   switch(obj1.tag) {
1.7       takayama  775:   case 0:
                    776:     return(KpoInteger(1)); /* case of NullObject */
                    777:     break;
                    778:   case Sinteger:
                    779:     if (obj1.lc.ival > obj2.lc.ival) return(KpoInteger(1));
                    780:     else return(KpoInteger(0));
                    781:     break;
                    782:   case Sstring:
                    783:   case Sdollar:
                    784:     if (strcmp(obj1.lc.str, obj2.lc.str)>0) return(KpoInteger(1));
                    785:     else return(KpoInteger(0));
                    786:     break;
                    787:   case Spoly:
                    788:     if ((*mmLarger)(obj1.lc.poly,obj2.lc.poly) == 1) return(KpoInteger(1));
                    789:     else return(KpoInteger(0));
                    790:     break;
                    791:   case SuniversalNumber:
                    792:     tt = coeffGreater(obj1.lc.universalNumber,obj2.lc.universalNumber);
                    793:     if (tt > 0) return(KpoInteger(1));
                    794:     else return(KpoInteger(0));
                    795:     break;
                    796:   case Sdouble:
                    797:     if ( KopDouble(obj1) > KopDouble(obj2) ) return(KpoInteger(1));
                    798:     else return(KpoInteger(0));
                    799:     break;
                    800:   default:
                    801:     errorKan1("%s\n","KooGreater() has not supported these objects yet.");
                    802:     break;
                    803:   }
1.1       maekawa   804: }
                    805:
                    806: struct object KooLess(obj1,obj2)
1.7       takayama  807:      struct object obj1;
                    808:      struct object obj2;
1.1       maekawa   809: {
                    810:   struct object ob;
                    811:   int tt;
                    812:   if (obj1.tag != obj2.tag) {
                    813:     errorKan1("%s\n","You cannot compare different kinds of objects.");
                    814:   }
                    815:   switch(obj1.tag) {
1.7       takayama  816:   case 0:
                    817:     return(KpoInteger(1)); /* case of NullObject */
                    818:     break;
                    819:   case Sinteger:
                    820:     if (obj1.lc.ival < obj2.lc.ival) return(KpoInteger(1));
                    821:     else return(KpoInteger(0));
                    822:     break;
                    823:   case Sstring:
                    824:   case Sdollar:
                    825:     if (strcmp(obj1.lc.str, obj2.lc.str)<0) return(KpoInteger(1));
                    826:     else return(KpoInteger(0));
                    827:     break;
                    828:   case Spoly:
                    829:     if ((*mmLarger)(obj2.lc.poly,obj1.lc.poly) == 1) return(KpoInteger(1));
                    830:     else return(KpoInteger(0));
                    831:     break;
                    832:   case SuniversalNumber:
                    833:     tt = coeffGreater(obj1.lc.universalNumber,obj2.lc.universalNumber);
                    834:     if (tt < 0) return(KpoInteger(1));
                    835:     else return(KpoInteger(0));
                    836:     break;
                    837:   case Sdouble:
                    838:     if ( KopDouble(obj1) < KopDouble(obj2) ) return(KpoInteger(1));
                    839:     else return(KpoInteger(0));
                    840:     break;
                    841:   default:
                    842:     errorKan1("%s\n","KooLess() has not supported these objects yet.");
                    843:     break;
                    844:   }
1.1       maekawa   845: }
                    846:
                    847: /* :conversion */
                    848:
                    849: struct object KdataConversion(obj,key)
1.7       takayama  850:      struct object obj;
                    851:      char *key;
1.1       maekawa   852: {
                    853:   char tmps[128]; /* Assume that double is not more than 128 digits */
                    854:   char intstr[100]; /* Assume that int is not more than 100 digits */
                    855:   struct object rob;
                    856:   extern struct ring *CurrentRingp;
                    857:   extern struct ring SmallRing;
                    858:   int flag;
                    859:   struct object rob1,rob2;
                    860:   char *s;
                    861:   int i;
1.2       takayama  862:   double f;
                    863:   double f2;
1.1       maekawa   864:   /* reports the data type */
                    865:   if (key[0] == 't' || key[0] =='e') {
                    866:     if (strcmp(key,"type?")==0) {
                    867:       rob = KpoInteger(obj.tag);
                    868:       return(rob);
                    869:     }else if (strcmp(key,"type??")==0) {
                    870:       if (obj.tag != Sclass) {
1.7       takayama  871:         rob = KpoInteger(obj.tag);
1.1       maekawa   872:       }else {
1.7       takayama  873:         rob = KpoInteger(ectag(obj));
1.1       maekawa   874:       }
                    875:       return(rob);
                    876:     }else if (strcmp(key,"error")==0) {
                    877:       rob = KnewErrorPacketObj(obj);
                    878:       return(rob);
                    879:     }
                    880:   }
                    881:   switch(obj.tag) {
                    882:   case Snull:
                    883:     if (strcmp(key,"integer") == 0) {
                    884:       rob = KpoInteger(0);
                    885:       return(rob);
                    886:     }else if (strcmp(key,"universalNumber") == 0) {
                    887:       rob.tag = SuniversalNumber;
                    888:       rob.lc.universalNumber = intToCoeff(obj.lc.ival,&SmallRing);
                    889:       return(rob);
                    890:     }else if (strcmp(key,"poly") == 0) {
                    891:       rob = KpoPOLY(ZERO);
                    892:     }else{
                    893:       warningKan("Sorry. The data conversion from null to this data type has not supported yet.\n");
                    894:     }
                    895:     break;
                    896:   case Sinteger:
                    897:     if (strcmp(key,"string") == 0) { /* ascii code */
                    898:       rob.tag = Sdollar;
                    899:       rob.lc.str = (char *)sGC_malloc(2);
                    900:       if (rob.lc.str == (char *)NULL) errorKan1("%s","No more memory.\n");
                    901:       (rob.lc.str)[0] = obj.lc.ival; (rob.lc.str)[1] = '\0';
                    902:       return(rob);
                    903:     }else if (strcmp(key,"integer")==0) {
                    904:       return(obj);
                    905:     }else if (strcmp(key,"poly") == 0) {
                    906:       rob.tag = Spoly;
                    907:       rob.lc.poly = cxx(obj.lc.ival,0,0,CurrentRingp);
                    908:       return(rob);
                    909:     }else if (strcmp(key,"dollar") == 0) {
                    910:       rob.tag = Sdollar;
                    911:       sprintf(intstr,"%d",obj.lc.ival);
                    912:       rob.lc.str = (char *)sGC_malloc(strlen(intstr)+2);
                    913:       if (rob.lc.str == (char *)NULL) errorKan1("%s","No more memory.\n");
                    914:       strcpy(rob.lc.str,intstr);
                    915:       return(rob);
                    916:     }else if (strcmp(key,"universalNumber")==0) {
                    917:       rob.tag = SuniversalNumber;
                    918:       rob.lc.universalNumber = intToCoeff(obj.lc.ival,&SmallRing);
                    919:       return(rob);
                    920:     }else if (strcmp(key,"double") == 0) {
                    921:       rob = KpoDouble((double) (obj.lc.ival));
                    922:       return(rob);
                    923:     }else if (strcmp(key,"null") == 0) {
                    924:       rob = NullObject;
                    925:       return(rob);
                    926:     }else{
                    927:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                    928:     }
                    929:     break;
                    930:   case Sdollar:
                    931:     if (strcmp(key,"dollar") == 0 || strcmp(key,"string")==0) {
                    932:       rob = obj;
                    933:       return(rob);
                    934:     }else if (strcmp(key,"literal") == 0) {
                    935:       rob.tag = Sstring;
                    936:       s = (char *) sGC_malloc(sizeof(char)*(strlen(obj.lc.str)+3));
                    937:       if (s == (char *) NULL)   {
1.7       takayama  938:         errorKan1("%s\n","No memory.");
1.1       maekawa   939:       }
                    940:       s[0] = '/';
                    941:       strcpy(&(s[1]),obj.lc.str);
                    942:       rob.lc.str = &(s[1]);
                    943:       /* set the hashing value. */
                    944:       rob2 = lookupLiteralString(s);
                    945:       rob.rc.op = rob2.lc.op;
                    946:       return(rob);
                    947:     }else if (strcmp(key,"poly")==0) {
                    948:       rob.tag = Spoly;
                    949:       rob.lc.poly = stringToPOLY(obj.lc.str,CurrentRingp);
                    950:       return(rob);
                    951:     }else if (strcmp(key,"array")==0) {
                    952:       rob = newObjectArray(strlen(obj.lc.str));
                    953:       for (i=0; i<strlen(obj.lc.str); i++) {
1.7       takayama  954:         putoa(rob,i,KpoInteger((obj.lc.str)[i]));
1.1       maekawa   955:       }
                    956:       return(rob);
                    957:     }else if (strcmp(key,"universalNumber") == 0) {
                    958:       rob.tag = SuniversalNumber;
                    959:       rob.lc.universalNumber = stringToUniversalNumber(obj.lc.str,&flag);
                    960:       if (flag == -1) errorKan1("KdataConversion(): %s",
1.7       takayama  961:                                 "It's not number.\n");
1.2       takayama  962:       return(rob);
                    963:     }else if (strcmp(key,"double") == 0) {
                    964:       /* Check the format.  2.3432 e2 is not allowed. It should be 2.3232e2.*/
                    965:       flag = 0;
                    966:       for (i=0; (obj.lc.str)[i] != '\0'; i++) {
1.7       takayama  967:         if ((obj.lc.str)[i] > ' ' && flag == 0) flag=1;
                    968:         else if ((obj.lc.str)[i] <= ' ' && flag == 1) flag = 2;
                    969:         else if ((obj.lc.str)[i] > ' ' && flag == 2) flag=3;
1.2       takayama  970:       }
                    971:       if (flag == 3) errorKan1("KdataConversion(): %s","The data for the double contains blanck(s)");
                    972:       /* Read the double. */
                    973:       if (sscanf(obj.lc.str,"%lf",&f) <= 0) {
1.7       takayama  974:         errorKan1("KdataConversion(): %s","It cannot be translated to double.");
1.2       takayama  975:       }
                    976:       rob = KpoDouble(f);
1.1       maekawa   977:       return(rob);
                    978:     }else if (strcmp(key,"null") == 0) {
                    979:       rob = NullObject;
                    980:       return(rob);
                    981:     }else{
                    982:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                    983:     }
                    984:     break;
                    985:   case Sarray:
                    986:     if (strcmp(key,"array") == 0) {
                    987:       return(rob);
                    988:     }else if (strcmp(key,"list") == 0) {
                    989:       rob = *( arrayToList(obj) );
                    990:       return(rob);
                    991:     }else if (strcmp(key,"arrayOfPOLY")==0) {
                    992:       rob = KpoArrayOfPOLY(arrayToArrayOfPOLY(obj));
                    993:       return(rob);
                    994:     }else if (strcmp(key,"matrixOfPOLY")==0) {
                    995:       rob = KpoMatrixOfPOLY(arrayToMatrixOfPOLY(obj));
                    996:       return(rob);
                    997:     }else if (strcmp(key,"gradedPolySet")==0) {
                    998:       rob = KpoGradedPolySet(arrayToGradedPolySet(obj));
                    999:       return(rob);
                   1000:     }else if (strcmp(key,"null") == 0) {
                   1001:       rob = NullObject;
                   1002:       return(rob);
                   1003:     }else {
1.23      takayama 1004:          { /* Automatically maps the elements. */
                   1005:                int n,i;
                   1006:                n = getoaSize(obj);
                   1007:                rob = newObjectArray(n);
                   1008:                for (i=0; i<n; i++) {
                   1009:                  putoa(rob,i,KdataConversion(getoa(obj,i),key));
                   1010:                }
                   1011:                return(rob);
                   1012:          }
1.1       maekawa  1013:     }
                   1014:     break;
                   1015:   case Spoly:
1.15      takayama 1016:     if ((strcmp(key,"poly")==0) || (strcmp(key,"numerator")==0)) {
1.5       takayama 1017:       rob = obj;
1.1       maekawa  1018:       return(rob);
                   1019:     }else if (strcmp(key,"integer")==0) {
                   1020:       if (obj.lc.poly == ZERO) return(KpoInteger(0));
                   1021:       else {
1.7       takayama 1022:         return(KpoInteger(coeffToInt(obj.lc.poly->coeffp)));
1.1       maekawa  1023:       }
                   1024:     }else if (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1025:       rob.tag = Sdollar;
                   1026:       rob.lc.str = KPOLYToString(KopPOLY(obj));
                   1027:       return(rob);
                   1028:     }else if (strcmp(key,"array") == 0) {
                   1029:       return( POLYToArray(KopPOLY(obj)));
                   1030:     }else if (strcmp(key,"map")==0) {
                   1031:       return(KringMap(obj));
                   1032:     }else if (strcmp(key,"universalNumber")==0) {
                   1033:       if (obj.lc.poly == ZERO) {
1.7       takayama 1034:         rob.tag = SuniversalNumber;
                   1035:         rob.lc.universalNumber = newUniversalNumber(0);
1.1       maekawa  1036:       } else {
1.7       takayama 1037:         if (obj.lc.poly->coeffp->tag == MP_INTEGER) {
                   1038:           rob.tag = SuniversalNumber;
                   1039:           rob.lc.universalNumber = newUniversalNumber2(obj.lc.poly->coeffp->val.bigp);
                   1040:         }else {
                   1041:           rob = NullObject;
                   1042:           warningKan("Coefficient is not MP_INT.");
                   1043:         }
1.1       maekawa  1044:       }
                   1045:       return(rob);
                   1046:     }else if (strcmp(key,"ring")==0) {
                   1047:       if (obj.lc.poly ISZERO) {
1.7       takayama 1048:         warningKan("Zero polynomial does not have the ring structure field.\n");
1.1       maekawa  1049:       }else{
1.7       takayama 1050:         rob.tag = Sring;
                   1051:         rob.lc.ringp = (obj.lc.poly)->m->ringp;
                   1052:         return(rob);
1.1       maekawa  1053:       }
                   1054:     }else if (strcmp(key,"null") == 0) {
                   1055:       rob = NullObject;
                   1056:       return(rob);
                   1057:     }else{
                   1058:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1059:     }
                   1060:     break;
                   1061:   case SarrayOfPOLY:
                   1062:     if (strcmp(key,"array")==0) {
                   1063:       rob = arrayOfPOLYToArray(KopArrayOfPOLYp(obj));
                   1064:       return(rob);
                   1065:     }else{
                   1066:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1067:     }
                   1068:     break;
                   1069:   case SmatrixOfPOLY:
                   1070:     if (strcmp(key,"array")==0) {
                   1071:       rob = matrixOfPOLYToArray(KopMatrixOfPOLYp(obj));
                   1072:       return(rob);
                   1073:     }else if (strcmp(key,"null") == 0) {
                   1074:       rob = NullObject;
                   1075:       return(rob);
                   1076:     }else{
                   1077:       warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1078:     }
                   1079:     break;
                   1080:   case Slist:
                   1081:     if (strcmp(key,"array") == 0) {
                   1082:       rob = listToArray(&obj);
                   1083:       return(rob);
                   1084:     }
                   1085:     break;
                   1086:   case SuniversalNumber:
1.15      takayama 1087:     if ((strcmp(key,"universalNumber")==0) || (strcmp(key,"numerator")==0)) {
1.1       maekawa  1088:       return(rob);
                   1089:     }else if (strcmp(key,"integer")==0) {
                   1090:       rob = KpoInteger(coeffToInt(obj.lc.universalNumber));
                   1091:       return(rob);
                   1092:     }else if (strcmp(key,"poly")==0) {
                   1093:       rob = KpoPOLY(universalToPoly(obj.lc.universalNumber,CurrentRingp));
                   1094:       return(rob);
                   1095:     }else if (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1096:       rob.tag = Sdollar;
                   1097:       rob.lc.str = coeffToString(obj.lc.universalNumber);
                   1098:       return(rob);
                   1099:     }else if (strcmp(key,"null") == 0) {
                   1100:       rob = NullObject;
                   1101:       return(rob);
                   1102:     }else if (strcmp(key,"double") == 0) {
                   1103:       rob = KpoDouble( toDouble0(obj) );
                   1104:       return(rob);
                   1105:     }else{
                   1106:       warningKan("Sorry. This type of data conversion of universalNumber has not supported yet.\n");
                   1107:     }
                   1108:     break;
                   1109:   case SrationalFunction:
                   1110:     if (strcmp(key,"rationalFunction")==0) {
                   1111:       return(rob);
                   1112:     } if (strcmp(key,"numerator")==0) {
                   1113:       rob = *(Knumerator(obj));
                   1114:       return(rob);
                   1115:     }else if  (strcmp(key,"denominator")==0) {
                   1116:       rob = *(Kdenominator(obj));
                   1117:       return(rob);
                   1118:     }else if  (strcmp(key,"string")==0 || strcmp(key,"dollar")==0) {
                   1119:       rob1 = KdataConversion(*(Knumerator(obj)),"string");
                   1120:       rob2 = KdataConversion(*(Kdenominator(obj)),"string");
                   1121:       s = sGC_malloc(sizeof(char)*( strlen(rob1.lc.str) + strlen(rob2.lc.str) + 10));
                   1122:       if (s == (char *)NULL) errorKan1("%s\n","KdataConversion(): No memory");
                   1123:       sprintf(s,"(%s)/(%s)",rob1.lc.str,rob2.lc.str);
                   1124:       rob.tag = Sdollar;
                   1125:       rob.lc.str = s;
                   1126:       return(rob);
                   1127:     }else if  (strcmp(key,"cancel")==0) {
                   1128:       warningKan("Sorry. Data conversion <<cancel>> of rationalFunction has not supported yet.\n");
                   1129:       return(obj);
                   1130:     }else if (strcmp(key,"null") == 0) {
                   1131:       rob = NullObject;
                   1132:       return(rob);
                   1133:     }else if (strcmp(key,"double") == 0) {
                   1134:       rob = KpoDouble( toDouble0(obj) );
                   1135:       return(rob);
                   1136:     }else{
                   1137:       warningKan("Sorry. This type of data conversion of rationalFunction has not supported yet.\n");
                   1138:     }
                   1139:     break;
                   1140:   case Sdouble:
                   1141:     if (strcmp(key,"integer") == 0) {
                   1142:       rob = KpoInteger( (int) KopDouble(obj));
                   1143:       return(rob);
                   1144:     } else if (strcmp(key,"universalNumber") == 0) {
                   1145:       rob.tag = SuniversalNumber;
                   1146:       rob.lc.universalNumber = intToCoeff((int) KopDouble(obj),&SmallRing);
                   1147:       return(rob);
                   1148:     }else if ((strcmp(key,"string") == 0) || (strcmp(key,"dollar") == 0)) {
                   1149:       sprintf(tmps,"%f",KopDouble(obj));
                   1150:       s = sGC_malloc(strlen(tmps)+2);
                   1151:       if (s == (char *)NULL) errorKan1("%s\n","KdataConversion(): No memory");
                   1152:       strcpy(s,tmps);
                   1153:       rob.tag = Sdollar;
                   1154:       rob.lc.str = s;
                   1155:       return(rob);
                   1156:     }else if (strcmp(key,"double")==0) {
                   1157:       return(obj);
                   1158:     }else if (strcmp(key,"null") == 0) {
                   1159:       rob = NullObject;
                   1160:       return(rob);
                   1161:     }else {
                   1162:       warningKan("Sorry. This type of data conversion of rationalFunction has not supported yet.\n");
                   1163:     }
                   1164:     break;
                   1165:   case Sring:
                   1166:     if (strcmp(key,"orderMatrix")==0) {
                   1167:       rob = oGetOrderMatrix(KopRingp(obj));
                   1168:       return(rob);
1.22      takayama 1169:     }else if (strcmp(key,"oxRingStructure")==0) {
                   1170:       rob = oRingToOXringStructure(KopRingp(obj));
                   1171:       return(rob);
1.1       maekawa  1172:     }else{
                   1173:       warningKan("Sorryl This type of data conversion of ringp has not supported yet.\n");
                   1174:     }
                   1175:     break;
                   1176:   default:
                   1177:     warningKan("Sorry. This type of data conversion has not supported yet.\n");
                   1178:   }
                   1179:   return(NullObject);
                   1180: }
                   1181:
                   1182: /* conversion functions between primitive data and objects.
                   1183:    If it's not time critical, it is recommended to use these functions */
                   1184: struct object KpoInteger(k)
1.7       takayama 1185:      int k;
1.1       maekawa  1186: {
                   1187:   struct object obj;
                   1188:   obj.tag = Sinteger;
                   1189:   obj.lc.ival = k; obj.rc.ival = 0;
                   1190:   return(obj);
                   1191: }
                   1192: struct object KpoString(s)
1.7       takayama 1193:      char *s;
1.1       maekawa  1194: {
                   1195:   struct object obj;
                   1196:   obj.tag = Sdollar;
                   1197:   obj.lc.str = s; obj.rc.ival = 0;
                   1198:   return(obj);
                   1199: }
                   1200: struct object KpoPOLY(f)
1.7       takayama 1201:      POLY f;
1.1       maekawa  1202: {
                   1203:   struct object obj;
                   1204:   obj.tag = Spoly;
                   1205:   obj.lc.poly = f; obj.rc.ival = 0;
                   1206:   return(obj);
                   1207: }
                   1208: struct object KpoArrayOfPOLY(ap)
1.7       takayama 1209:      struct arrayOfPOLY *ap ;
1.1       maekawa  1210: {
                   1211:   struct object obj;
                   1212:   obj.tag = SarrayOfPOLY;
                   1213:   obj.lc.arrayp = ap; obj.rc.ival = 0;
                   1214:   return(obj);
                   1215: }
                   1216:
                   1217: struct object KpoMatrixOfPOLY(mp)
1.7       takayama 1218:      struct matrixOfPOLY *mp ;
1.1       maekawa  1219: {
                   1220:   struct object obj;
                   1221:   obj.tag = SmatrixOfPOLY;
                   1222:   obj.lc.matrixp = mp; obj.rc.ival = 0;
                   1223:   return(obj);
                   1224: }
                   1225:
                   1226: struct object KpoRingp(ringp)
1.7       takayama 1227:      struct ring *ringp;
1.1       maekawa  1228: {
                   1229:   struct object obj;
                   1230:   obj.tag = Sring;
                   1231:   obj.lc.ringp = ringp;
                   1232:   return(obj);
                   1233: }
                   1234:
1.22      takayama 1235: struct object KpoUniversalNumber(u)
                   1236:      struct coeff *u;
                   1237: {
                   1238:   struct object obj;
                   1239:   obj.tag = SuniversalNumber;
                   1240:   obj.lc.universalNumber = u;
                   1241:   return(obj);
                   1242: }
1.1       maekawa  1243: /*** conversion 2. Data conversions on arrays and matrices. ****/
                   1244: struct object arrayOfPOLYToArray(aa)
1.7       takayama 1245:      struct arrayOfPOLY *aa;
1.1       maekawa  1246: {
                   1247:   POLY *a;
                   1248:   int size;
                   1249:   struct object r;
                   1250:   int j;
                   1251:   struct object tmp;
                   1252:
                   1253:   size = aa->n; a = aa->array;
                   1254:   r = newObjectArray(size);
                   1255:   for (j=0; j<size; j++) {
                   1256:     tmp.tag = Spoly;
                   1257:     tmp.lc.poly= a[j];
                   1258:     putoa(r,j,tmp);
                   1259:   }
                   1260:   return( r );
                   1261: }
                   1262:
                   1263: struct object matrixOfPOLYToArray(pmat)
1.7       takayama 1264:      struct matrixOfPOLY *pmat;
1.1       maekawa  1265: {
                   1266:   struct object r;
                   1267:   struct object tmp;
                   1268:   int i,j;
                   1269:   int m,n;
                   1270:   POLY *mat;
                   1271:   struct arrayOfPOLY ap;
                   1272:
                   1273:   m = pmat->m; n = pmat->n; mat = pmat->mat;
                   1274:   r = newObjectArray(m);
                   1275:   for (i=0; i<m; i++) {
                   1276:     ap.n = n; ap.array = &(mat[ind(i,0)]);
                   1277:     tmp = arrayOfPOLYToArray(&ap);
                   1278:     /* ind() is the macro defined in matrix.h. */
                   1279:     putoa(r,i,tmp);
                   1280:   }
                   1281:   return(r);
                   1282: }
                   1283:
                   1284: struct arrayOfPOLY *arrayToArrayOfPOLY(oa)
1.7       takayama 1285:      struct object oa;
1.1       maekawa  1286: {
                   1287:   POLY *a;
                   1288:   int size;
                   1289:   int i;
                   1290:   struct object tmp;
                   1291:   struct arrayOfPOLY *ap;
                   1292:
                   1293:   if (oa.tag != Sarray) errorKan1("KarrayToArrayOfPOLY(): %s",
1.7       takayama 1294:                                   "Argument is not array\n");
1.1       maekawa  1295:   size = getoaSize(oa);
                   1296:   a = (POLY *)sGC_malloc(sizeof(POLY)*size);
                   1297:   for (i=0; i<size; i++) {
                   1298:     tmp = getoa(oa,i);
                   1299:     if (tmp.tag != Spoly) errorKan1("KarrayToArrayOfPOLY():%s ",
1.7       takayama 1300:                                     "element must be polynomial.\n");
1.1       maekawa  1301:     a[i] = tmp.lc.poly;
                   1302:   }
                   1303:   ap = (struct arrayOfPOLY *)sGC_malloc(sizeof(struct arrayOfPOLY));
                   1304:   ap->n = size;
                   1305:   ap->array = a;
                   1306:   return(ap);
                   1307: }
                   1308:
                   1309: struct matrixOfPOLY *arrayToMatrixOfPOLY(oa)
1.7       takayama 1310:      struct object oa;
1.1       maekawa  1311: {
                   1312:   POLY *a;
                   1313:   int m;
                   1314:   int n;
                   1315:   int i,j;
                   1316:   struct matrixOfPOLY *ma;
                   1317:
                   1318:   struct object tmp,tmp2;
                   1319:   if (oa.tag != Sarray) errorKan1("KarrayToMatrixOfPOLY(): %s",
1.7       takayama 1320:                                   "Argument is not array\n");
1.1       maekawa  1321:   m = getoaSize(oa);
                   1322:   tmp = getoa(oa,0);
                   1323:   if (tmp.tag != Sarray) errorKan1("arrayToMatrixOfPOLY():%s ",
1.7       takayama 1324:                                    "Argument is not array\n");
1.1       maekawa  1325:   n = getoaSize(tmp);
                   1326:   a = (POLY *)sGC_malloc(sizeof(POLY)*(m*n));
                   1327:   for (i=0; i<m; i++) {
                   1328:     tmp = getoa(oa,i);
                   1329:     if (tmp.tag != Sarray) errorKan1("arrayToMatrixOfPOLY(): %s",
1.7       takayama 1330:                                      "element must be array.\n");
1.1       maekawa  1331:     for (j=0; j<n; j++) {
                   1332:       tmp2 = getoa(tmp,j);
                   1333:       if (tmp2.tag != Spoly) errorKan1("arrayToMatrixOfPOLY(): %s",
1.7       takayama 1334:                                        "element must be a polynomial.\n");
1.1       maekawa  1335:       a[ind(i,j)] = tmp2.lc.poly;
                   1336:       /* we use the macro ind here.  Be careful of using m and n. */
                   1337:     }
                   1338:   }
                   1339:   ma = (struct matrixOfPOLY *)sGC_malloc(sizeof(struct matrixOfPOLY));
                   1340:   ma->m = m; ma->n = n;
                   1341:   ma->mat = a;
                   1342:   return(ma);
                   1343: }
                   1344:
                   1345: /* :misc */
                   1346:
                   1347: /* :ring    :kan */
                   1348: int objArrayToOrderMatrix(oA,order,n,oasize)
1.7       takayama 1349:      struct object oA;
                   1350:      int order[];
                   1351:      int n;
                   1352:      int oasize;
1.1       maekawa  1353: {
                   1354:   int size;
                   1355:   int k,j;
                   1356:   struct object tmpOa;
                   1357:   struct object obj;
                   1358:   if (oA.tag != Sarray) {
                   1359:     warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1360:     return(-1);
                   1361:   }
                   1362:   size = getoaSize(oA);
                   1363:   if (size != oasize) {
                   1364:     warningKan("The row size of the array is wrong.");
                   1365:     return(-1);
                   1366:   }
                   1367:   for (k=0; k<size; k++) {
                   1368:     tmpOa = getoa(oA,k);
                   1369:     if (tmpOa.tag != Sarray) {
                   1370:       warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1371:       return(-1);
                   1372:     }
                   1373:     if (getoaSize(tmpOa) != 2*n) {
                   1374:       warningKan("The column size of the array is wrong.");
                   1375:       return(-1);
                   1376:     }
                   1377:     for (j=0; j<2*n; j++) {
                   1378:       obj = getoa(tmpOa,j);
                   1379:       order[k*2*n+j] = obj.lc.ival;
                   1380:     }
                   1381:   }
                   1382:   return(0);
                   1383: }
                   1384:
                   1385: int KsetOrderByObjArray(oA)
1.7       takayama 1386:      struct object oA;
1.1       maekawa  1387: {
                   1388:   int *order;
                   1389:   int n,c,l, oasize;
                   1390:   extern struct ring *CurrentRingp;
                   1391:   extern int AvoidTheSameRing;
                   1392:   /* n,c,l must be set in the CurrentRing */
                   1393:   if (AvoidTheSameRing) {
                   1394:     errorKan1("%s\n","KsetOrderByObjArray(): You cannot change the order matrix when AvoidTheSameRing == 1.");
                   1395:   }
                   1396:   n = CurrentRingp->n;
                   1397:   c = CurrentRingp->c;
                   1398:   l = CurrentRingp->l;
                   1399:   if (oA.tag != Sarray) {
                   1400:     warningKan("The argument should be of the form [ [...] [...] ... [...]].");
                   1401:     return(-1);
                   1402:   }
                   1403:   oasize = getoaSize(oA);
                   1404:   order = (int *)sGC_malloc(sizeof(int)*((2*n)*oasize+1));
                   1405:   if (order == (int *)NULL) errorKan1("%s\n","KsetOrderByObjArray(): No memory.");
                   1406:   if (objArrayToOrderMatrix(oA,order,n,oasize) == -1) {
                   1407:     return(-1);
                   1408:   }
                   1409:   setOrderByMatrix(order,n,c,l,oasize); /* Set order to the current ring. */
                   1410:   return(0);
                   1411: }
                   1412:
                   1413: static int checkRelations(c,l,m,n,cc,ll,mm,nn)
1.7       takayama 1414:      int c,l,m,n,cc,ll,mm,nn;
1.1       maekawa  1415: {
                   1416:   if (!(1<=c && c<=l && l<=m && m<=n)) return(1);
                   1417:   if (!(cc<=ll && ll<=mm && mm<=nn && nn <= n)) return(1);
                   1418:   if (!(cc<c || ll < l || mm < m || nn < n)) {
                   1419:     if (WarningNoVectorVariable) {
1.4       takayama 1420:       warningKanNoStrictMode("Ring definition: there is no variable to represent vectors.\n");
1.1       maekawa  1421:     }
                   1422:   }
                   1423:   if (!(cc<=c && ll <= l && mm <= m && nn <= n)) return(1);
                   1424:   return(0);
                   1425: }
                   1426:
                   1427: struct object KgetOrderMatrixOfCurrentRing()
                   1428: {
                   1429:   extern struct ring *CurrentRingp;
                   1430:   return(oGetOrderMatrix(CurrentRingp));
                   1431: }
                   1432:
                   1433:
                   1434: int KsetUpRing(ob1,ob2,ob3,ob4,ob5)
1.7       takayama 1435:      struct object ob1,ob2,ob3,ob4,ob5;
                   1436:      /* ob1 = [x(0), ..., x(n-1)];
                   1437:         ob2 = [D(0), ..., D(n-1)];
                   1438:         ob3 = [p,c,l,m,n,cc,ll,mm,nn,next];
                   1439:         ob4 = Order matrix
                   1440:         ob5 = [(keyword) value (keyword) value ....]
                   1441:      */
1.1       maekawa  1442: #define RP_LIMIT 500
                   1443: {
                   1444:   int i;
                   1445:   struct object ob;
                   1446:   int c,l,m,n;
                   1447:   int cc,ll,mm,nn;
                   1448:   int p;
                   1449:   char **xvars;
                   1450:   char **dvars;
                   1451:   int *outputVars;
                   1452:   int *order;
                   1453:   static int rp = 0;
                   1454:   static struct ring *rstack[RP_LIMIT];
                   1455:
                   1456:   extern struct ring *CurrentRingp;
                   1457:   struct ring *newRingp;
                   1458:   int ob3Size;
                   1459:   struct ring *nextRing;
                   1460:   int oasize;
                   1461:   static int ringSerial = 0;
                   1462:   char *ringName = NULL;
                   1463:   int aa;
                   1464:   extern int AvoidTheSameRing;
                   1465:   extern char *F_mpMult;
                   1466:   char *fmp_mult_saved;
                   1467:   char *mpMultName = NULL;
                   1468:   struct object rob;
                   1469:   struct ring *savedCurrentRingp;
                   1470:
                   1471:   /* To get the ring structure. */
                   1472:   if (ob1.tag == Snull) {
                   1473:     rob = newObjectArray(rp);
                   1474:     for (i=0; i<rp; i++) {
                   1475:       putoa(rob,i,KpoRingp(rstack[i]));
                   1476:     }
                   1477:     KSpush(rob);
                   1478:     return(0);
                   1479:   }
                   1480:
                   1481:   if (ob3.tag != Sarray) errorKan1("%s\n","Error in the 3rd argument. You need to give 4 arguments.");
                   1482:   ob3Size = getoaSize(ob3);
                   1483:   if (ob3Size != 9 && ob3Size != 10)
                   1484:     errorKan1("%s\n","Error in the 3rd argument.");
                   1485:   for (i=0; i<9; i++) {
                   1486:     ob = getoa(ob3,i);
                   1487:     if (ob.tag != Sinteger) errorKan1("%s\n","The 3rd argument should be a list of integers.");
                   1488:   }
                   1489:   if (ob3Size == 10) {
                   1490:     ob = getoa(ob3,9);
                   1491:     if (ob.tag != Sring)
                   1492:       errorKan1("%s\n","The last arguments of the 3rd argument must be a pointer to a ring.");
                   1493:     nextRing = KopRingp(ob);
                   1494:   } else {
                   1495:     nextRing = (struct ring *)NULL;
                   1496:   }
                   1497:
                   1498:   p = getoa(ob3,0).lc.ival;
                   1499:   c = getoa(ob3,1).lc.ival;  l = getoa(ob3,2).lc.ival;
                   1500:   m = getoa(ob3,3).lc.ival;  n = getoa(ob3,4).lc.ival;
                   1501:   cc = getoa(ob3,5).lc.ival;  ll = getoa(ob3,6).lc.ival;
                   1502:   mm = getoa(ob3,7).lc.ival;  nn = getoa(ob3,8).lc.ival;
                   1503:   if (checkRelations(c,l,m,n,cc,ll,mm,nn,n)) {
                   1504:     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.");
                   1505:   }
                   1506:   if (getoaSize(ob2) != n || getoaSize(ob1) != n) {
                   1507:     errorKan1("%s\n","Error in the 1st or 2nd arguments.");
                   1508:   }
                   1509:   for (i=0; i<n; i++) {
                   1510:     if (getoa(ob1,i).tag != Sdollar || getoa(ob2,i).tag != Sdollar) {
                   1511:       errorKan1("%s\n","Error in the 1st or 2nd arguments.");
                   1512:     }
                   1513:   }
                   1514:   xvars = (char **) sGC_malloc(sizeof(char *)*n);
                   1515:   dvars = (char **) sGC_malloc(sizeof(char *)*n);
                   1516:   if (xvars == (char **)NULL || dvars == (char **)NULL) {
                   1517:     fprintf(stderr,"No more memory.\n");
                   1518:     exit(15);
                   1519:   }
                   1520:   for (i=0; i<n; i++) {
                   1521:     xvars[i] = getoa(ob1,i).lc.str;
                   1522:     dvars[i] = getoa(ob2,i).lc.str;
                   1523:   }
                   1524:   checkDuplicateName(xvars,dvars,n);
                   1525:
                   1526:   outputVars = (int *)sGC_malloc(sizeof(int)*n*2);
                   1527:   if (outputVars == NULL) {
                   1528:     fprintf(stderr,"No more memory.\n");
                   1529:     exit(15);
                   1530:   }
                   1531:   if (ReverseOutputOrder) {
                   1532:     for (i=0; i<n; i++) outputVars[i] = n-i-1;
                   1533:     for (i=0; i<n; i++) outputVars[n+i] = 2*n-i-1;
                   1534:   }else{
                   1535:     for (i=0; i<2*n; i++) {
                   1536:       outputVars[i] = i;
                   1537:     }
                   1538:   }
                   1539:
                   1540:   oasize = getoaSize(ob4);
                   1541:   order = (int *)sGC_malloc(sizeof(int)*((2*n)*oasize+1));
                   1542:   if (order == (int *)NULL) errorKan1("%s\n","No memory.");
                   1543:   if (objArrayToOrderMatrix(ob4,order,n,oasize) == -1) {
                   1544:     errorKan1("%s\n","Errors in the 4th matrix (order matrix).");
                   1545:   }
                   1546:   /* It's better to check the consistency of the order matrix here. */
                   1547:   savedCurrentRingp = CurrentRingp;
                   1548:
                   1549:   newRingp = (struct ring *)sGC_malloc(sizeof(struct ring));
                   1550:   if (newRingp == NULL) errorKan1("%s\n","No more memory.");
                   1551:   /* Generate the new ring before calling setOrder...(). */
                   1552:   *newRingp = *CurrentRingp;
                   1553:   CurrentRingp = newRingp;  /* Push the current ring. */
                   1554:   setOrderByMatrix(order,n,c,l,oasize); /* set order to the CurrentRing. */
                   1555:   CurrentRingp = savedCurrentRingp; /* recover it. */
                   1556:
                   1557:
                   1558:   /* Set the default name of the ring */
                   1559:   ringName = (char *)sGC_malloc(16);
                   1560:   sprintf(ringName,"ring%05d",ringSerial);
                   1561:   ringSerial++;
                   1562:
                   1563:   /* Set the current ring */
                   1564:   newRingp->n = n; newRingp->m = m; newRingp->l = l; newRingp->c = c;
                   1565:   newRingp->nn = nn; newRingp->mm = mm; newRingp->ll = ll;
                   1566:   newRingp->cc = cc;
                   1567:   newRingp->x = xvars;
                   1568:   newRingp->D = dvars;
                   1569:   /* You don't need to set order and orderMatrixSize here.
                   1570:      It was set by setOrder(). */
                   1571:   setFromTo(newRingp);
                   1572:
                   1573:   newRingp->p = p;
                   1574:   newRingp->next = nextRing;
                   1575:   newRingp->multiplication = mpMult;
                   1576:   /* These values  should will be reset if the optional value is given. */
                   1577:   newRingp->schreyer = 0;
                   1578:   newRingp->gbListTower = NULL;
                   1579:   newRingp->outputOrder = outputVars;
1.9       takayama 1580:   newRingp->weightedHomogenization = 0;
1.11      takayama 1581:   newRingp->degreeShiftSize = 0;
1.12      takayama 1582:   newRingp->degreeShiftN = 0;
                   1583:   newRingp->degreeShift = NULL;
1.1       maekawa  1584:
                   1585:   if (ob5.tag != Sarray || (getoaSize(ob5) % 2) != 0) {
                   1586:     errorKan1("%s\n","[(keyword) value (keyword) value ....] should be given.");
                   1587:   }
                   1588:   for (i=0; i < getoaSize(ob5); i += 2) {
                   1589:     if (getoa(ob5,i).tag == Sdollar) {
                   1590:       if (strcmp(KopString(getoa(ob5,i)),"mpMult") == 0) {
1.7       takayama 1591:         if (getoa(ob5,i+1).tag != Sdollar) {
                   1592:           errorKan1("%s\n","A keyword should be given. (mpMult)");
                   1593:         }
                   1594:         fmp_mult_saved = F_mpMult;
                   1595:         mpMultName = KopString(getoa(ob5,i+1));
                   1596:         switch_function("mpMult",mpMultName);
                   1597:         /* Note that this cause a global effect. It will be done again. */
                   1598:         newRingp->multiplication = mpMult;
                   1599:         switch_function("mpMult",fmp_mult_saved);
1.1       maekawa  1600:       } else if (strcmp(KopString(getoa(ob5,i)),"coefficient ring") == 0) {
1.7       takayama 1601:         if (getoa(ob5,i+1).tag != Sring) {
                   1602:           errorKan1("%s\n","The pointer to a ring should be given. (coefficient ring)");
                   1603:         }
                   1604:         nextRing = KopRingp(getoa(ob5,i+1));
                   1605:         newRingp->next = nextRing;
1.1       maekawa  1606:       } else if (strcmp(KopString(getoa(ob5,i)),"valuation") == 0) {
1.7       takayama 1607:         errorKan1("%s\n","Not implemented. (valuation)");
1.1       maekawa  1608:       } else if (strcmp(KopString(getoa(ob5,i)),"characteristic") == 0) {
1.7       takayama 1609:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1610:           errorKan1("%s\n","A integer should be given. (characteristic)");
                   1611:         }
                   1612:         p = KopInteger(getoa(ob5,i+1));
                   1613:         newRingp->p = p;
1.1       maekawa  1614:       } else if (strcmp(KopString(getoa(ob5,i)),"schreyer") == 0) {
1.7       takayama 1615:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1616:           errorKan1("%s\n","A integer should be given. (schreyer)");
                   1617:         }
                   1618:         newRingp->schreyer = KopInteger(getoa(ob5,i+1));
1.1       maekawa  1619:       } else if (strcmp(KopString(getoa(ob5,i)),"gbListTower") == 0) {
1.7       takayama 1620:         if (getoa(ob5,i+1).tag != Slist) {
                   1621:           errorKan1("%s\n","A list should be given (gbListTower).");
                   1622:         }
                   1623:         newRingp->gbListTower = newObject();
                   1624:         *((struct object *)(newRingp->gbListTower)) = getoa(ob5,i+1);
1.1       maekawa  1625:       } else if (strcmp(KopString(getoa(ob5,i)),"ringName") == 0) {
1.7       takayama 1626:         if (getoa(ob5,i+1).tag != Sdollar) {
                   1627:           errorKan1("%s\n","A name should be given. (ringName)");
                   1628:         }
                   1629:         ringName = KopString(getoa(ob5,i+1));
1.9       takayama 1630:       } else if (strcmp(KopString(getoa(ob5,i)),"weightedHomogenization") == 0) {
                   1631:         if (getoa(ob5,i+1).tag != Sinteger) {
                   1632:           errorKan1("%s\n","A integer should be given. (weightedHomogenization)");
                   1633:         }
1.11      takayama 1634:         newRingp->weightedHomogenization = KopInteger(getoa(ob5,i+1));
                   1635:       } else if (strcmp(KopString(getoa(ob5,i)),"degreeShift") == 0) {
                   1636:         if (getoa(ob5,i+1).tag != Sarray) {
1.12      takayama 1637:           errorKan1("%s\n","An array of array should be given. (degreeShift)");
1.11      takayama 1638:         }
                   1639:         {
                   1640:           struct object ods;
1.12      takayama 1641:           struct object ods2;
                   1642:           int dssize,k,j,nn;
1.11      takayama 1643:           ods=getoa(ob5,i+1);
1.12      takayama 1644:           if ((getoaSize(ods) < 1) || (getoa(ods,0).tag != Sarray)) {
                   1645:             errorKan1("%s\n", "An array of array should be given. (degreeShift)");
                   1646:           }
                   1647:           nn = getoaSize(ods);
                   1648:           dssize = getoaSize(getoa(ods,0));
1.11      takayama 1649:           newRingp->degreeShiftSize = dssize;
1.12      takayama 1650:           newRingp->degreeShiftN = nn;
                   1651:           newRingp->degreeShift = (int *) sGC_malloc(sizeof(int)*(dssize*nn+1));
1.11      takayama 1652:           if (newRingp->degreeShift == NULL) errorKan1("%s\n","No more memory.");
1.12      takayama 1653:           for (j=0; j<nn; j++) {
                   1654:             ods2 = getoa(ods,j);
                   1655:             for (k=0; k<dssize; k++) {
                   1656:               if (getoa(ods2,k).tag == SuniversalNumber) {
                   1657:                 (newRingp->degreeShift)[j*dssize+k] = coeffToInt(getoa(ods2,k).lc.universalNumber);
                   1658:               }else{
                   1659:                 (newRingp->degreeShift)[j*dssize+k] = KopInteger(getoa(ods2,k));
                   1660:               }
1.11      takayama 1661:             }
                   1662:           }
                   1663:         }
1.22      takayama 1664:         switch_function("grade","module1v");
                   1665:         /* Warning: grading is changed to module1v!! */
1.1       maekawa  1666:       } else {
1.7       takayama 1667:         errorKan1("%s\n","Unknown keyword to set_up_ring@");
1.1       maekawa  1668:       }
                   1669:     }else{
                   1670:       errorKan1("%s\n","A keyword enclosed by braces have to be given.");
                   1671:     }
                   1672:   }
                   1673:
                   1674:   newRingp->name = ringName;
                   1675:
                   1676:
                   1677:   if (AvoidTheSameRing) {
                   1678:     aa = isTheSameRing(rstack,rp,newRingp);
                   1679:     if (aa < 0) {
                   1680:       /* This ring has never been defined. */
                   1681:       CurrentRingp = newRingp;
                   1682:       /* Install it to the RingStack */
                   1683:       if (rp <RP_LIMIT) {
1.7       takayama 1684:         rstack[rp] = CurrentRingp; rp++; /* Save the previous ringp */
1.1       maekawa  1685:       }else{
1.7       takayama 1686:         rp = 0;
                   1687:         errorKan1("%s\n","You have defined too many rings. Check the value of RP_LIMIT.");
1.1       maekawa  1688:       }
                   1689:     }else{
                   1690:       /* This ring has been defined. */
                   1691:       /* Discard the newRingp */
                   1692:       CurrentRingp = rstack[aa];
                   1693:       ringSerial--;
                   1694:     }
                   1695:   }else{
                   1696:     CurrentRingp = newRingp;
                   1697:     /* Install it to the RingStack */
                   1698:     if (rp <RP_LIMIT) {
                   1699:       rstack[rp] = CurrentRingp; rp++; /* Save the previous ringp */
                   1700:     }else{
                   1701:       rp = 0;
                   1702:       errorKan1("%s\n","You have defined too many rings. Check the value of RP_LIMIT.");
                   1703:     }
                   1704:   }
                   1705:   if (mpMultName != NULL) {
                   1706:     switch_function("mpMult",mpMultName);
                   1707:   }
                   1708:
                   1709:   initSyzRingp();
                   1710:
                   1711:   return(0);
                   1712: }
                   1713:
                   1714:
                   1715: struct object KsetVariableNames(struct object ob,struct ring *rp)
                   1716: {
                   1717:   int n,i;
                   1718:   struct object ox;
                   1719:   struct object otmp;
                   1720:   char **xvars;
                   1721:   char **dvars;
                   1722:   if (ob.tag  != Sarray) {
                   1723:     errorKan1("%s\n","KsetVariableNames(): the argument must be of the form [(x) (y) (z) ...]");
                   1724:   }
                   1725:   n = rp->n;
                   1726:   ox = ob;
                   1727:   if (getoaSize(ox) != 2*n) {
                   1728:     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.");
                   1729:   }
                   1730:   xvars = (char **)sGC_malloc(sizeof(char *)*n);
                   1731:   dvars = (char **)sGC_malloc(sizeof(char *)*n);
                   1732:   if (xvars == NULL || dvars == NULL) {
                   1733:     errorKan1("%s\n","KsetVariableNames(): no more memory.");
                   1734:   }
                   1735:   for (i=0; i<2*n; i++) {
                   1736:     otmp = getoa(ox,i);
                   1737:     if(otmp.tag != Sdollar) {
                   1738:       errorKan1("%s\n","KsetVariableNames(): elements must be strings.");
                   1739:     }
                   1740:     if (i < n) {
                   1741:       xvars[i] = KopString(otmp);
                   1742:     }else{
                   1743:       dvars[i-n] = KopString(otmp);
                   1744:     }
                   1745:   }
                   1746:   checkDuplicateName(xvars,dvars,n);
                   1747:   rp->x = xvars;
                   1748:   rp->D = dvars;
                   1749:   return(ob);
                   1750: }
                   1751:
                   1752:
                   1753:
                   1754: void KshowRing(ringp)
1.7       takayama 1755:      struct ring *ringp;
1.1       maekawa  1756: {
                   1757:   showRing(1,ringp);
                   1758: }
                   1759:
                   1760: struct object KswitchFunction(ob1,ob2)
1.7       takayama 1761:      struct object ob1,ob2;
1.1       maekawa  1762: {
                   1763:   char *ans ;
                   1764:   struct object rob;
                   1765:   int needWarningForAvoidTheSameRing = 0;
                   1766:   extern int AvoidTheSameRing;
                   1767:   if ((ob1.tag != Sdollar) || (ob2.tag != Sdollar)) {
                   1768:     errorKan1("%s\n","$function$ $name$ switch_function\n");
                   1769:   }
                   1770:   if (AvoidTheSameRing && needWarningForAvoidTheSameRing) {
                   1771:     if (strcmp(KopString(ob1),"mmLarger") == 0 ||
                   1772:         strcmp(KopString(ob1),"mpMult") == 0 ||
                   1773:         strcmp(KopString(ob1),"monomialAdd") == 0 ||
                   1774:         strcmp(KopString(ob1),"isSameComponent") == 0) {
                   1775:       fprintf(stderr,",switch_function ==> %s ",KopString(ob1));
                   1776:       warningKan("switch_function might cause a trouble under AvoidTheSameRing == 1.\n");
                   1777:     }
                   1778:   }
                   1779:   if (AvoidTheSameRing) {
                   1780:     if (strcmp(KopString(ob1),"mmLarger") == 0 &&
1.7       takayama 1781:         strcmp(KopString(ob2),"matrix") != 0) {
1.1       maekawa  1782:       fprintf(stderr,"mmLarger = %s",KopString(ob2));
                   1783:       errorKan1("%s\n","mmLarger can set only to matrix under AvoidTheSameRing == 1.");
                   1784:     }
                   1785:   }
                   1786:
                   1787:   ans = switch_function(ob1.lc.str,ob2.lc.str);
                   1788:   if (ans == NULL) {
                   1789:     rob = NullObject;
                   1790:   }else{
                   1791:     rob = KpoString(ans);
                   1792:   }
                   1793:   return(rob);
                   1794:
                   1795: }
                   1796:
                   1797: void KprintSwitchStatus(void)
                   1798: {
                   1799:   print_switch_status();
                   1800: }
                   1801:
                   1802: struct object KoReplace(of,rule)
1.7       takayama 1803:      struct object of;
                   1804:      struct object rule;
1.1       maekawa  1805: {
                   1806:   struct object rob;
                   1807:   POLY f;
                   1808:   POLY lRule[N0*2];
                   1809:   POLY rRule[N0*2];
                   1810:   POLY r;
                   1811:   int i;
                   1812:   int n;
                   1813:   struct object trule;
                   1814:
                   1815:
                   1816:   if (rule.tag != Sarray) {
                   1817:     errorKan1("%s\n"," KoReplace(): The second argument must be array.");
                   1818:   }
                   1819:   n = getoaSize(rule);
                   1820:
1.6       takayama 1821:   if (of.tag == Spoly) {
                   1822:   }else if (of.tag ==Sclass && ectag(of) == CLASSNAME_recursivePolynomial) {
1.7       takayama 1823:     return(KreplaceRecursivePolynomial(of,rule));
1.6       takayama 1824:   }else{
1.1       maekawa  1825:     errorKan1("%s\n"," KoReplace(): The first argument must be a polynomial.");
                   1826:   }
                   1827:   f = KopPOLY(of);
                   1828:
                   1829:   if (f ISZERO) {
                   1830:   }else{
                   1831:     if (n >= 2*(f->m->ringp->n)) {
                   1832:       errorKan1("%s\n"," KoReplace(): too many rules for replacement. ");
                   1833:     }
                   1834:   }
                   1835:
                   1836:   for (i=0; i<n; i++) {
                   1837:     trule = getoa(rule,i);
                   1838:     if (trule.tag != Sarray) {
                   1839:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....].");
                   1840:     }
                   1841:     if (getoaSize(trule) != 2) {
                   1842:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....].");
                   1843:     }
                   1844:
                   1845:     if (getoa(trule,0).tag != Spoly) {
                   1846:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....] where a,b,c,d,... are polynomials.");
                   1847:     }
                   1848:     if (getoa(trule,1).tag != Spoly) {
                   1849:       errorKan1("%s\n"," KoReplace(): The second argument must be of the form [[a b] [c d] ....] where a,b,c,d,... are polynomials.");
                   1850:     }
                   1851:
                   1852:     lRule[i] = KopPOLY(getoa(trule,0));
                   1853:     rRule[i] = KopPOLY(getoa(trule,1));
                   1854:   }
                   1855:
                   1856:   r = replace(f,lRule,rRule,n);
                   1857:   rob.tag = Spoly; rob.lc.poly = r;
                   1858:
                   1859:   return(rob);
                   1860: }
                   1861:
                   1862:
                   1863: struct object Kparts(f,v)
1.7       takayama 1864:      struct object f;
                   1865:      struct object v;
1.1       maekawa  1866: {
                   1867:   POLY ff;
                   1868:   POLY vv;
                   1869:   struct object obj;
                   1870:   struct matrixOfPOLY *co;
                   1871:   /* check the data type */
                   1872:   if (f.tag != Spoly || v.tag != Spoly)
                   1873:     errorKan1("%s\n","arguments of Kparts() must have polynomial as arguments.");
                   1874:
                   1875:   co = parts(KopPOLY(f),KopPOLY(v));
                   1876:   obj = matrixOfPOLYToArray(co);
                   1877:   return(obj);
                   1878: }
                   1879:
                   1880: struct object Kparts2(f,v)
1.7       takayama 1881:      struct object f;
                   1882:      struct object v;
1.1       maekawa  1883: {
                   1884:   POLY ff;
                   1885:   POLY vv;
                   1886:   struct object obj;
                   1887:   struct matrixOfPOLY *co;
                   1888:   /* check the data type */
                   1889:   if (f.tag != Spoly || v.tag != Spoly)
                   1890:     errorKan1("%s\n","arguments of Kparts2() must have polynomial as arguments.");
                   1891:
                   1892:   obj = parts2(KopPOLY(f),KopPOLY(v));
                   1893:   return(obj);
                   1894: }
                   1895:
                   1896:
                   1897: struct object Kdegree(ob1,ob2)
1.7       takayama 1898:      struct object ob1,ob2;
1.1       maekawa  1899: {
                   1900:   if (ob1.tag != Spoly || ob2.tag != Spoly)
                   1901:     errorKan1("%s\n","The arguments must be polynomials.");
                   1902:
                   1903:   return(KpoInteger(pDegreeWrtV(KopPOLY(ob1),KopPOLY(ob2))));
                   1904: }
                   1905:
                   1906: struct object KringMap(obj)
1.7       takayama 1907:      struct object obj;
1.1       maekawa  1908: {
                   1909:   extern struct ring *CurrentRingp;
                   1910:   extern struct ring *SyzRingp;
                   1911:   POLY f;
                   1912:   POLY r;
                   1913:   if (obj.tag != Spoly)
                   1914:     errorKan1("%s\n","The argments must be polynomial.");
                   1915:   f = KopPOLY(obj);
                   1916:   if (f ISZERO) return(obj);
                   1917:   if (f->m->ringp == CurrentRingp) return(obj);
                   1918:   if (f->m->ringp == CurrentRingp->next) {
                   1919:     r = newCell(newCoeff(),newMonomial(CurrentRingp));
                   1920:     r->coeffp->tag = POLY_COEFF;
                   1921:     r->coeffp->val.f = f;
                   1922:     return(KpoPOLY(r));
                   1923:   }else if (f->m->ringp == SyzRingp) {
                   1924:     return(KpoPOLY(f->coeffp->val.f));
                   1925:   }
                   1926:   errorKan1("%s\n","The ring map is not defined in this case.");
                   1927: }
                   1928:
                   1929:
                   1930: struct object Ksp(ob1,ob2)
1.7       takayama 1931:      struct object ob1,ob2;
1.1       maekawa  1932: {
                   1933:   struct spValue sv;
                   1934:   struct object rob,cob;
                   1935:   POLY f;
                   1936:   if (ob1.tag != Spoly || ob2.tag != Spoly)
                   1937:     errorKan1("%s\n","Ksp(): The arguments must be polynomials.");
                   1938:   sv = (*sp)(ob1.lc.poly,ob2.lc.poly);
                   1939:   f = ppAddv(ppMult(sv.a,KopPOLY(ob1)),
1.7       takayama 1940:              ppMult(sv.b,KopPOLY(ob2)));
1.1       maekawa  1941:   rob = newObjectArray(2);
                   1942:   cob = newObjectArray(2);
                   1943:   putoa(rob,1,KpoPOLY(f));
                   1944:   putoa(cob,0,KpoPOLY(sv.a));
                   1945:   putoa(cob,1,KpoPOLY(sv.b));
                   1946:   putoa(rob,0,cob);
                   1947:   return(rob);
                   1948: }
                   1949:
                   1950: struct object Khead(ob)
1.7       takayama 1951:      struct object ob;
1.1       maekawa  1952: {
                   1953:   if (ob.tag != Spoly) errorKan1("%s\n","Khead(): The argument should be a polynomial.");
                   1954:   return(KpoPOLY(head( KopPOLY(ob))));
                   1955: }
                   1956:
                   1957:
                   1958: /* :eval */
                   1959: struct object Keval(obj)
1.7       takayama 1960:      struct object obj;
1.1       maekawa  1961: {
                   1962:   char *key;
                   1963:   int size;
                   1964:   struct object rob;
                   1965:   rob = NullObject;
                   1966:
                   1967:   if (obj.tag != Sarray)
                   1968:     errorKan1("%s\n","[$key$ arguments] eval");
                   1969:   if (getoaSize(obj) < 1)
                   1970:     errorKan1("%s\n","[$key$ arguments] eval");
                   1971:   if (getoa(obj,0).tag != Sdollar)
                   1972:     errorKan1("%s\n","[$key$ arguments] eval");
                   1973:   key = getoa(obj,0).lc.str;
                   1974:   size = getoaSize(obj);
                   1975:
                   1976:
                   1977:   return(rob);
                   1978: }
                   1979:
                   1980: /* :Utilities */
                   1981: char *KremoveSpace(str)
1.7       takayama 1982:      char str[];
1.1       maekawa  1983: {
                   1984:   int size;
                   1985:   int start;
                   1986:   int end;
                   1987:   char *s;
                   1988:   int i;
                   1989:
                   1990:   size = strlen(str);
                   1991:   for (start = 0; start <= size; start++) {
                   1992:     if (str[start] > ' ') break;
                   1993:   }
                   1994:   for (end = size-1; end >= 0; end--) {
                   1995:     if (str[end] > ' ') break;
                   1996:   }
                   1997:   if (start > end) return((char *) NULL);
                   1998:   s = (char *) sGC_malloc(sizeof(char)*(end-start+2));
                   1999:   if (s == (char *)NULL) errorKan1("%s\n","removeSpace(): No more memory.");
                   2000:   for (i=0; i< end-start+1; i++)
                   2001:     s[i] = str[i+start];
                   2002:   s[end-start+1] = '\0';
                   2003:   return(s);
                   2004: }
                   2005:
                   2006: struct object KtoRecords(ob)
1.7       takayama 2007:      struct object ob;
1.1       maekawa  2008: {
                   2009:   struct object obj;
                   2010:   struct object tmp;
                   2011:   int i;
                   2012:   int size;
                   2013:   char **argv;
                   2014:
                   2015:   obj = NullObject;
                   2016:   switch(ob.tag) {
                   2017:   case Sdollar: break;
                   2018:   default:
                   2019:     errorKan1("%s","Argument of KtoRecords() must be a string enclosed by dollars.\n");
                   2020:     break;
                   2021:   }
                   2022:   size = strlen(ob.lc.str)+3;
                   2023:   argv = (char **) sGC_malloc((size+1)*sizeof(char *));
                   2024:   if (argv == (char **)NULL)
                   2025:     errorKan1("%s","No more memory.\n");
                   2026:   size = KtoArgvbyCurryBrace(ob.lc.str,argv,size);
                   2027:   if (size < 0)
                   2028:     errorKan1("%s"," KtoRecords(): You have an error in the argument.\n");
                   2029:
                   2030:   obj = newObjectArray(size);
                   2031:   for (i=0; i<size; i++) {
                   2032:     tmp.tag = Sdollar;
                   2033:     tmp.lc.str = argv[i];
                   2034:     (obj.rc.op)[i] = tmp;
                   2035:   }
                   2036:   return(obj);
                   2037: }
                   2038:
                   2039: int KtoArgvbyCurryBrace(str,argv,limit)
1.7       takayama 2040:      char *str;
                   2041:      char *argv[];
                   2042:      int limit;
                   2043:      /* This function returns argc */
                   2044:      /* decompose into tokens by the separators
1.1       maekawa  2045:    { }, [ ], and characters of which code is less than SPACE.
                   2046:    Example.   { }  ---> nothing            (argc=0)
                   2047:               {x}----> x                   (argc=1)
                   2048:               {x,y} --> x   y              (argc=2)
1.7       takayama 2049:           {ab, y, z } --> ab   y   z   (argc=3)
1.1       maekawa  2050:               [[ab],c,d]  --> [ab] c   d
                   2051: */
                   2052: {
                   2053:   int argc;
                   2054:   int n;
                   2055:   int i;
                   2056:   int k;
                   2057:   char *a;
                   2058:   char *ident;
                   2059:   int level = 0;
                   2060:   int comma;
                   2061:
                   2062:   if (str == (char *)NULL) {
                   2063:     fprintf(stderr,"You use NULL string to toArgvbyCurryBrace()\n");
                   2064:     return(0);
                   2065:   }
                   2066:
                   2067:   n = strlen(str);
                   2068:   a = (char *) sGC_malloc(sizeof(char)*(n+3));
                   2069:   a[0]=' ';
                   2070:   strcpy(&(a[1]),str);
                   2071:   n = strlen(a); a[0] = '\0';
                   2072:   comma = -1;
                   2073:   for (i=1; i<n; i++) {
                   2074:     if (a[i] == '{' || a[i] == '[') level++;
                   2075:     if (level <= 1 && ( a[i] == ',')) {a[i] = '\0'; ++comma;}
                   2076:     if (level <= 1 && (a[i]=='{' || a[i]=='}' || a[i]=='[' || a[i]==']'))
                   2077:       a[i] = '\0';
                   2078:     if (a[i] == '}' || a[i] == ']') level--;
                   2079:     if ((level <= 1) && (comma == -1) && ( a[i] > ' ')) comma = 0;
                   2080:   }
                   2081:
                   2082:   if (comma == -1) return(0);
                   2083:
                   2084:   argc=0;
                   2085:   for (i=0; i<n; i++) {
                   2086:     if ((a[i] == '\0') && (a[i+1] != '\0')) ++argc;
                   2087:   }
                   2088:   if (argc > limit) return(-argc);
                   2089:
                   2090:   k = 0;
                   2091:   for (i=0; i<n; i++) {
                   2092:     if ((a[i] == '\0') && (a[i+1] != '\0')) {
                   2093:       ident = (char *) sGC_malloc(sizeof(char)*( strlen(&(a[i+1])) + 3));
                   2094:       strcpy(ident,&(a[i+1]));
                   2095:       argv[k] = KremoveSpace(ident);
                   2096:       if (argv[k] != (char *)NULL) k++;
                   2097:       if (k >= limit) errorKan1("%s\n","KtoArgvbyCurryBraces(): k>=limit.");
                   2098:     }
                   2099:   }
                   2100:   argc = k;
                   2101:   /*for (i=0; i<argc; i++) fprintf(stderr,"%d %s\n",i,argv[i]);*/
                   2102:   return(argc);
                   2103: }
                   2104:
1.14      takayama 2105: struct object KstringToArgv(struct object ob) {
                   2106:   struct object rob;
                   2107:   char *s;
                   2108:   int n,wc,i,inblank;
                   2109:   char **argv;
                   2110:   if (ob.tag != Sdollar)
1.22      takayama 2111:     errorKan1("%s\n","KstringToArgv(): the argument must be a string.");
1.14      takayama 2112:   n = strlen(KopString(ob));
                   2113:   s = (char *) sGC_malloc(sizeof(char)*(n+2));
                   2114:   if (s == NULL) errorKan1("%s\n","KstringToArgv(): No memory.");
                   2115:   strcpy(s,KopString(ob));
                   2116:   inblank = 1;  wc = 0;
                   2117:   for (i=0; i<n; i++) {
1.22      takayama 2118:     if (inblank && (s[i] > ' ')) {
                   2119:       wc++; inblank = 0;
                   2120:     }else if ((!inblank) && (s[i] <= ' ')) {
                   2121:       inblank = 1;
                   2122:     }
1.14      takayama 2123:   }
                   2124:   argv = (char **) sGC_malloc(sizeof(char *)*(wc+2));
                   2125:   argv[0] = NULL;
                   2126:   inblank = 1;  wc = 0;
                   2127:   for (i=0; i<n; i++) {
1.22      takayama 2128:     if (inblank && (s[i] > ' ')) {
                   2129:       argv[wc] = &(s[i]); argv[wc+1]=NULL;
                   2130:       wc++; inblank = 0;
                   2131:     }else if ((inblank == 0) && (s[i] <= ' ')) {
                   2132:       inblank = 1; s[i] = 0;
                   2133:     }else if (inblank && (s[i] <= ' ')) {
                   2134:       s[i] = 0;
                   2135:     }
1.14      takayama 2136:   }
                   2137:
                   2138:   rob = newObjectArray(wc);
                   2139:   for (i=0; i<wc; i++) {
1.22      takayama 2140:     putoa(rob,i,KpoString(argv[i]));
                   2141:     /* printf("%s\n",argv[i]); */
1.14      takayama 2142:   }
                   2143:   return(rob);
                   2144: }
1.1       maekawa  2145:
                   2146: static void checkDuplicateName(xvars,dvars,n)
1.7       takayama 2147:      char *xvars[];
                   2148:      char *dvars[];
                   2149:      int n;
1.1       maekawa  2150: {
                   2151:   int i,j;
                   2152:   char *names[N0*2];
                   2153:   for (i=0; i<n; i++) {
                   2154:     names[i] = xvars[i]; names[i+n] = dvars[i];
                   2155:   }
                   2156:   n = 2*n;
                   2157:   for (i=0; i<n; i++) {
                   2158:     for (j=i+1; j<n; j++) {
                   2159:       if (strcmp(names[i],names[j]) == 0) {
1.7       takayama 2160:         fprintf(stderr,"\n%d=%s, %d=%s\n",i,names[i],j,names[j]);
                   2161:         errorKan1("%s\n","Duplicate definition of the name above in SetUpRing().");
1.1       maekawa  2162:       }
                   2163:     }
                   2164:   }
                   2165: }
                   2166:
1.20      takayama 2167: struct object KooPower(struct object ob1,struct object ob2) {
                   2168:   struct object rob;
                   2169:   /* Bug. It has not yet been implemented. */
                   2170:   if (QuoteMode) {
1.22      takayama 2171:     rob = powerTree(ob1,ob2);
1.20      takayama 2172:   }else{
1.22      takayama 2173:     warningKan("KooDiv2() has not supported yet these objects.\n");
1.20      takayama 2174:   }
                   2175:   return(rob);
                   2176: }
1.1       maekawa  2177:
                   2178:
                   2179:
                   2180: struct object KooDiv2(ob1,ob2)
1.7       takayama 2181:      struct object ob1,ob2;
1.1       maekawa  2182: {
                   2183:   struct object rob = NullObject;
                   2184:   POLY f;
                   2185:   extern struct ring *CurrentRingp;
                   2186:   int s,i;
                   2187:   double d;
                   2188:
                   2189:   switch (Lookup[ob1.tag][ob2.tag]) {
                   2190:   case SpolySpoly:
                   2191:   case SuniversalNumberSuniversalNumber:
                   2192:   case SuniversalNumberSpoly:
                   2193:   case SpolySuniversalNumber:
                   2194:     rob = KnewRationalFunction0(copyObjectp(&ob1),copyObjectp(&ob2));
                   2195:     KisInvalidRational(&rob);
                   2196:     return(rob);
                   2197:     break;
                   2198:   case SarraySpoly:
                   2199:   case SarraySuniversalNumber:
                   2200:   case SarraySrationalFunction:
                   2201:     s = getoaSize(ob1);
                   2202:     rob = newObjectArray(s);
                   2203:     for (i=0; i<s; i++) {
                   2204:       putoa(rob,i,KooDiv2(getoa(ob1,i),ob2));
                   2205:     }
                   2206:     return(rob);
                   2207:     break;
                   2208:   case SpolySrationalFunction:
                   2209:   case SrationalFunctionSpoly:
                   2210:   case SrationalFunctionSrationalFunction:
                   2211:   case SuniversalNumberSrationalFunction:
                   2212:   case SrationalFunctionSuniversalNumber:
                   2213:     rob = KoInverse(ob2);
                   2214:     rob = KooMult(ob1,rob);
                   2215:     return(rob);
                   2216:     break;
                   2217:
                   2218:   case SdoubleSdouble:
                   2219:     d = KopDouble(ob2);
                   2220:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2221:     return(KpoDouble( KopDouble(ob1) / d ));
                   2222:     break;
                   2223:   case SdoubleSinteger:
                   2224:   case SdoubleSuniversalNumber:
                   2225:   case SdoubleSrationalFunction:
                   2226:     d = toDouble0(ob2);
                   2227:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2228:     return(KpoDouble( KopDouble(ob1) / d) );
                   2229:     break;
                   2230:   case SintegerSdouble:
                   2231:   case SuniversalNumberSdouble:
                   2232:   case SrationalFunctionSdouble:
                   2233:     d = KopDouble(ob2);
                   2234:     if (d == 0.0) errorKan1("%s\n","KooDiv2, Division by zero.");
                   2235:     return(KpoDouble( toDouble0(ob1) / d ) );
                   2236:     break;
                   2237:
                   2238:   default:
1.20      takayama 2239:     if (QuoteMode) {
                   2240:       rob = divideTree(ob1,ob2);
                   2241:     }else{
                   2242:       warningKan("KooDiv2() has not supported yet these objects.\n");
                   2243:     }
1.1       maekawa  2244:     break;
                   2245:   }
                   2246:   return(rob);
                   2247: }
                   2248: /* Template
                   2249:   case SrationalFunctionSrationalFunction:
                   2250:     warningKan("Koo() has not supported yet these objects.\n");
                   2251:     return(rob);
                   2252:     break;
                   2253:   case SpolySrationalFunction:
                   2254:     warningKan("Koo() has not supported yet these objects.\n");
                   2255:     return(rob);
                   2256:     break;
                   2257:   case SrationalFunctionSpoly:
                   2258:     warningKan("Koo() has not supported yet these objects.\n");
                   2259:     return(rob);
                   2260:     break;
                   2261:   case SuniversalNumberSrationalFunction:
                   2262:     warningKan("Koo() has not supported yet these objects.\n");
                   2263:     return(rob);
                   2264:     break;
                   2265:   case SrationalFunctionSuniversalNumber:
                   2266:     warningKan("Koo() has not supported yet these objects.\n");
                   2267:     return(rob);
                   2268:     break;
                   2269: */
                   2270:
                   2271: int KisInvalidRational(op)
1.7       takayama 2272:      objectp op;
1.1       maekawa  2273: {
                   2274:   extern struct coeff *UniversalZero;
                   2275:   if (op->tag != SrationalFunction) return(0);
                   2276:   if (KisZeroObject(Kdenominator(*op))) {
                   2277:     errorKan1("%s\n","KisInvalidRational(): zero division. You have f/0.");
                   2278:   }
                   2279:   if (KisZeroObject(Knumerator(*op))) {
                   2280:     op->tag = SuniversalNumber;
                   2281:     op->lc.universalNumber = UniversalZero;
                   2282:   }
                   2283:   return(0);
                   2284: }
                   2285:
                   2286: struct object KgbExtension(struct object obj)
                   2287: {
                   2288:   char *key;
                   2289:   int size;
                   2290:   struct object keyo;
                   2291:   struct object rob = NullObject;
                   2292:   struct object obj1,obj2,obj3;
                   2293:   POLY f1;
                   2294:   POLY f2;
                   2295:   POLY f3;
                   2296:   POLY f;
                   2297:   int m,i;
                   2298:   struct pairOfPOLY pf;
1.16      takayama 2299:   struct coeff *cont;
1.1       maekawa  2300:
                   2301:   if (obj.tag != Sarray) errorKan1("%s\n","KgbExtension(): The argument must be an array.");
                   2302:   size = getoaSize(obj);
                   2303:   if (size < 1) errorKan1("%s\n","KgbExtension(): Empty array.");
                   2304:   keyo = getoa(obj,0);
                   2305:   if (keyo.tag != Sdollar) errorKan1("%s\n","KgbExtension(): No key word.");
                   2306:   key = KopString(keyo);
                   2307:
                   2308:   /* branch by the key word. */
                   2309:   if (strcmp(key,"isReducible")==0) {
                   2310:     if (size != 3) errorKan1("%s\n","[(isReducible)  poly1 poly2] gbext.");
                   2311:     obj1 = getoa(obj,1);
                   2312:     obj2 = getoa(obj,2);
                   2313:     if (obj1.tag != Spoly || obj2.tag != Spoly)
                   2314:       errorKan1("%s\n","[(isReducible)  poly1 poly2] gb.");
                   2315:     f1 = KopPOLY(obj1);
                   2316:     f2 = KopPOLY(obj2);
                   2317:     rob = KpoInteger((*isReducible)(f1,f2));
                   2318:   }else if (strcmp(key,"lcm") == 0) {
                   2319:     if (size != 3) errorKan1("%s\n","[(lcm)  poly1 poly2] gb.");
                   2320:     obj1 = getoa(obj,1);
                   2321:     obj2 = getoa(obj,2);
                   2322:     if (obj1.tag != Spoly || obj2.tag != Spoly)
                   2323:       errorKan1("%s\n","[(lcm)  poly1 poly2] gbext.");
                   2324:     f1 = KopPOLY(obj1);
                   2325:     f2 = KopPOLY(obj2);
                   2326:     rob = KpoPOLY((*lcm)(f1,f2));
                   2327:   }else if (strcmp(key,"grade")==0) {
                   2328:     if (size != 2) errorKan1("%s\n","[(grade)  poly1 ] gbext.");
                   2329:     obj1 = getoa(obj,1);
                   2330:     if (obj1.tag != Spoly)
                   2331:       errorKan1("%s\n","[(grade)  poly1 ] gbext.");
                   2332:     f1 = KopPOLY(obj1);
                   2333:     rob = KpoInteger((*grade)(f1));
                   2334:   }else if (strcmp(key,"mod")==0) {
                   2335:     if (size != 3) errorKan1("%s\n","[(mod) poly num] gbext");
                   2336:     obj1 = getoa(obj,1);
                   2337:     obj2 = getoa(obj,2);
                   2338:     if (obj1.tag != Spoly || obj2.tag != SuniversalNumber) {
                   2339:       errorKan1("%s\n","The datatype of the argument mismatch: [(mod) polynomial  universalNumber] gbext");
                   2340:     }
                   2341:     rob = KpoPOLY( modulopZ(KopPOLY(obj1),KopUniversalNumber(obj2)) );
                   2342:   }else if (strcmp(key,"tomodp")==0) {
                   2343:     /* The ring must be a ring of characteristic p. */
                   2344:     if (size != 3) errorKan1("%s\n","[(tomod) poly ring] gbext");
                   2345:     obj1 = getoa(obj,1);
                   2346:     obj2 = getoa(obj,2);
                   2347:     if (obj1.tag != Spoly || obj2.tag != Sring) {
                   2348:       errorKan1("%s\n","The datatype of the argument mismatch: [(tomod) polynomial  ring] gbext");
                   2349:     }
                   2350:     rob = KpoPOLY( modulop(KopPOLY(obj1),KopRingp(obj2)) );
                   2351:   }else if (strcmp(key,"tomod0")==0) {
                   2352:     /* Ring must be a ring of characteristic 0. */
                   2353:     if (size != 3) errorKan1("%s\n","[(tomod0) poly ring] gbext");
                   2354:     obj1 = getoa(obj,1);
                   2355:     obj2 = getoa(obj,2);
                   2356:     if (obj1.tag != Spoly || obj2.tag != Sring) {
                   2357:       errorKan1("%s\n","The datatype of the argument mismatch: [(tomod0) polynomial  ring] gbext");
                   2358:     }
                   2359:     errorKan1("%s\n","It has not been implemented.");
                   2360:     rob = KpoPOLY( POLYNULL );
                   2361:   }else if (strcmp(key,"divByN")==0) {
                   2362:     if (size != 3) errorKan1("%s\n","[(divByN) poly num] gbext");
                   2363:     obj1 = getoa(obj,1);
                   2364:     obj2 = getoa(obj,2);
                   2365:     if (obj1.tag != Spoly || obj2.tag != SuniversalNumber) {
                   2366:       errorKan1("%s\n","The datatype of the argument mismatch: [(divByN) polynomial  universalNumber] gbext");
                   2367:     }
                   2368:     pf =  quotientByNumber(KopPOLY(obj1),KopUniversalNumber(obj2));
                   2369:     rob  = newObjectArray(2);
                   2370:     putoa(rob,0,KpoPOLY(pf.first));
                   2371:     putoa(rob,1,KpoPOLY(pf.second));
                   2372:   }else if (strcmp(key,"isConstant")==0) {
                   2373:     if (size != 2) errorKan1("%s\n","[(isConstant) poly ] gbext bool");
                   2374:     obj1 = getoa(obj,1);
                   2375:     if (obj1.tag != Spoly) {
                   2376:       errorKan1("%s\n","The datatype of the argument mismatch: [(isConstant) polynomial] gbext");
                   2377:     }
                   2378:     return(KpoInteger(isConstant(KopPOLY(obj1))));
1.18      takayama 2379:   }else if (strcmp(key,"isConstantAll")==0) {
                   2380:     if (size != 2) errorKan1("%s\n","[(isConstantAll) poly ] gbext bool");
                   2381:     obj1 = getoa(obj,1);
                   2382:     if (obj1.tag != Spoly) {
                   2383:       errorKan1("%s\n","The datatype of the argument mismatch: [(isConstantAll) polynomial] gbext");
                   2384:     }
                   2385:     return(KpoInteger(isConstantAll(KopPOLY(obj1))));
1.1       maekawa  2386:   }else if (strcmp(key,"schreyerSkelton") == 0) {
                   2387:     if (size != 2) errorKan1("%s\n","[(schreyerSkelton) array_of_poly ] gbext array");
                   2388:     obj1 = getoa(obj,1);
                   2389:     return(KschreyerSkelton(obj1));
                   2390:   }else if (strcmp(key,"lcoeff") == 0) {
                   2391:     if (size != 2) errorKan1("%s\n","[(lcoeff) poly] gbext poly");
                   2392:     obj1 = getoa(obj,1);
                   2393:     if (obj1.tag != Spoly) errorKan1("%s\n","[(lcoeff) poly] gbext poly");
                   2394:     f = KopPOLY(obj1);
                   2395:     if (f == POLYNULL) return(KpoPOLY(f));
                   2396:     return(KpoPOLY( newCell(coeffCopy(f->coeffp),newMonomial(f->m->ringp))));
                   2397:   }else if (strcmp(key,"lmonom") == 0) {
                   2398:     if (size != 2) errorKan1("%s\n","[(lmonom) poly] gbext poly");
                   2399:     obj1 = getoa(obj,1);
                   2400:     if (obj1.tag != Spoly) errorKan1("%s\n","[(lmonom) poly] gbext poly");
                   2401:     f = KopPOLY(obj1);
                   2402:     if (f == POLYNULL) return(KpoPOLY(f));
                   2403:     return(KpoPOLY( newCell(intToCoeff(1,f->m->ringp),monomialCopy(f->m))));
                   2404:   }else if (strcmp(key,"toes") == 0) {
                   2405:     if (size != 2) errorKan1("%s\n","[(toes) array] gbext poly");
                   2406:     obj1 = getoa(obj,1);
                   2407:     if (obj1.tag != Sarray) errorKan1("%s\n","[(toes) array] gbext poly");
                   2408:     return(KvectorToSchreyer_es(obj1));
1.3       takayama 2409:   }else if (strcmp(key,"toe_") == 0) {
                   2410:     if (size != 2) errorKan1("%s\n","[(toe_) array] gbext poly");
                   2411:     obj1 = getoa(obj,1);
                   2412:     if (obj1.tag == Spoly) return(obj1);
                   2413:     if (obj1.tag != Sarray) errorKan1("%s\n","[(toe_) array] gbext poly");
                   2414:     return(KpoPOLY(arrayToPOLY(obj1)));
1.1       maekawa  2415:   }else if (strcmp(key,"isOrdered") == 0) {
                   2416:     if (size != 2) errorKan1("%s\n","[(isOrdered) poly] gbext poly");
                   2417:     obj1 = getoa(obj,1);
                   2418:     if (obj1.tag != Spoly) errorKan1("%s\n","[(isOrdered) poly] gbext poly");
                   2419:     return(KisOrdered(obj1));
1.16      takayama 2420:   }else if (strcmp(key,"reduceContent")==0) {
                   2421:     if (size != 2) errorKan1("%s\n","[(reduceContent)  poly1 ] gbext.");
                   2422:     obj1 = getoa(obj,1);
                   2423:     if (obj1.tag != Spoly)
                   2424:       errorKan1("%s\n","[(reduceContent)  poly1 ] gbext.");
                   2425:     f1 = KopPOLY(obj1);
1.22      takayama 2426:     rob = newObjectArray(2);
                   2427:     f1 = reduceContentOfPoly(f1,&cont);
                   2428:     putoa(rob,0,KpoPOLY(f1));
                   2429:     if (f1 == POLYNULL) {
                   2430:       putoa(rob,1,KpoPOLY(f1));
                   2431:     }else{
                   2432:       putoa(rob,1,KpoPOLY(newCell(cont,newMonomial(f1->m->ringp))));
                   2433:     }
1.17      takayama 2434:   }else if (strcmp(key,"ord_ws_all")==0) {
                   2435:     if (size != 3) errorKan1("%s\n","[(ord_ws_all) fv wv] gbext");
                   2436:     obj1 = getoa(obj,1);
                   2437:     obj2 = getoa(obj,2);
                   2438:     rob  = KordWsAll(obj1,obj2);
1.23      takayama 2439:   }else if (strcmp(key,"exponents")==0) {
                   2440:     if (size == 3) {
                   2441:       obj1 = getoa(obj,1);
                   2442:       obj2 = getoa(obj,2);
                   2443:       rob  = KgetExponents(obj1,obj2);
                   2444:     }else if (size == 2) {
                   2445:       obj1 = getoa(obj,1);
                   2446:       obj2 = KpoInteger(2);
                   2447:       rob  = KgetExponents(obj1,obj2);
                   2448:     }else{
                   2449:       errorKan1("%s\n","[(exponents) f type] gbext");
                   2450:     }
1.1       maekawa  2451:   }else {
                   2452:     errorKan1("%s\n","gbext : unknown tag.");
                   2453:   }
                   2454:   return(rob);
                   2455: }
                   2456:
                   2457: struct object KmpzExtension(struct object obj)
                   2458: {
                   2459:   char *key;
                   2460:   int size;
                   2461:   struct object keyo;
                   2462:   struct object rob = NullObject;
                   2463:   struct object obj0,obj1,obj2,obj3;
                   2464:   MP_INT *f;
                   2465:   MP_INT *g;
                   2466:   MP_INT *h;
                   2467:   MP_INT *r0;
                   2468:   MP_INT *r1;
                   2469:   MP_INT *r2;
                   2470:   int gi;
                   2471:   extern struct ring *SmallRingp;
                   2472:
                   2473:
                   2474:   if (obj.tag != Sarray) errorKan1("%s\n","KmpzExtension(): The argument must be an array.");
                   2475:   size = getoaSize(obj);
                   2476:   if (size < 1) errorKan1("%s\n","KmpzExtension(): Empty array.");
                   2477:   keyo = getoa(obj,0);
                   2478:   if (keyo.tag != Sdollar) errorKan1("%s\n","KmpzExtension(): No key word.");
                   2479:   key = KopString(keyo);
                   2480:
                   2481:   /* branch by the key word. */
                   2482:   if (strcmp(key,"gcd")==0) {
                   2483:     if (size != 3) errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2484:     obj1 = getoa(obj,1);
                   2485:     obj2 = getoa(obj,2);
1.24    ! takayama 2486:     if (obj1.tag != SuniversalNumber) {
        !          2487:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2488:        }
        !          2489:     if (obj2.tag != SuniversalNumber) {
        !          2490:       obj2 = KdataConversion(obj2,"universalNumber");
        !          2491:        }
1.1       maekawa  2492:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2493:       errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2494:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2495:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2496:       errorKan1("%s\n","[(gcd)  universalNumber universalNumber] mpzext.");
                   2497:     }
                   2498:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2499:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2500:     r1 = newMP_INT();
                   2501:     mpz_gcd(r1,f,g);
                   2502:     rob.tag = SuniversalNumber;
                   2503:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2504:   }else if (strcmp(key,"tdiv_qr")==0) {
                   2505:     if (size != 3) errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2506:     obj1 = getoa(obj,1);
                   2507:     obj2 = getoa(obj,2);
1.24    ! takayama 2508:     if (obj1.tag != SuniversalNumber) {
        !          2509:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2510:        }
        !          2511:     if (obj2.tag != SuniversalNumber) {
        !          2512:       obj2 = KdataConversion(obj2,"universalNumber");
        !          2513:        }
1.1       maekawa  2514:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2515:       errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2516:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2517:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2518:       errorKan1("%s\n","[(tdiv_qr)  universalNumber universalNumber] mpzext.");
                   2519:     }
                   2520:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2521:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2522:     r1 = newMP_INT();
                   2523:     r2 = newMP_INT();
                   2524:     mpz_tdiv_qr(r1,r2,f,g);
                   2525:     obj1.tag = SuniversalNumber;
                   2526:     obj1.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2527:     obj2.tag = SuniversalNumber;
                   2528:     obj2.lc.universalNumber = mpintToCoeff(r2,SmallRingp);
                   2529:     rob = newObjectArray(2);
                   2530:     putoa(rob,0,obj1); putoa(rob,1,obj2);
                   2531:   } else if (strcmp(key,"cancel")==0) {
                   2532:     if (size != 2) {
                   2533:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2534:     }
                   2535:     obj0 = getoa(obj,1);
                   2536:     if (obj0.tag == SuniversalNumber) return(obj0);
                   2537:     if (obj0.tag != SrationalFunction) {
                   2538:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2539:       return(obj0);
                   2540:     }
                   2541:     obj1 = *(Knumerator(obj0));
                   2542:     obj2 = *(Kdenominator(obj0));
                   2543:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber) {
                   2544:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2545:       return(obj0);
                   2546:     }
                   2547:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2548:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2549:       errorKan1("%s\n","[(cancel)  universalNumber/universalNumber] mpzext.");
                   2550:     }
                   2551:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2552:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2553:
                   2554:     r0 = newMP_INT();
                   2555:     r1 = newMP_INT();
                   2556:     r2 = newMP_INT();
                   2557:     mpz_gcd(r0,f,g);
                   2558:     mpz_divexact(r1,f,r0);
                   2559:     mpz_divexact(r2,g,r0);
                   2560:     obj1.tag = SuniversalNumber;
                   2561:     obj1.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2562:     obj2.tag = SuniversalNumber;
                   2563:     obj2.lc.universalNumber = mpintToCoeff(r2,SmallRingp);
                   2564:
                   2565:     rob = KnewRationalFunction0(copyObjectp(&obj1),copyObjectp(&obj2));
                   2566:     KisInvalidRational(&rob);
                   2567:   }else if (strcmp(key,"sqrt")==0 ||
1.7       takayama 2568:             strcmp(key,"com")==0) {
1.1       maekawa  2569:     /*  One arg functions  */
                   2570:     if (size != 2) errorKan1("%s\n","[key num] mpzext");
                   2571:     obj1 = getoa(obj,1);
1.24    ! takayama 2572:     if (obj1.tag != SuniversalNumber) {
        !          2573:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2574:        }
1.1       maekawa  2575:     if (obj1.tag != SuniversalNumber)
                   2576:       errorKan1("%s\n","[key num] mpzext : num must be a universalNumber.");
                   2577:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber))
                   2578:       errorKan1("%s\n","[key num] mpzext : num must be a universalNumber.");
                   2579:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2580:     if (strcmp(key,"sqrt")==0) {
                   2581:       r1 = newMP_INT();
                   2582:       mpz_sqrt(r1,f);
                   2583:     }else if (strcmp(key,"com")==0) {
                   2584:       r1 = newMP_INT();
                   2585:       mpz_com(r1,f);
                   2586:     }
                   2587:     rob.tag = SuniversalNumber;
                   2588:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2589:   }else if (strcmp(key,"probab_prime_p")==0 ||
1.7       takayama 2590:             strcmp(key,"and") == 0 ||
                   2591:             strcmp(key,"ior")==0) {
1.1       maekawa  2592:     /* Two args functions */
                   2593:     if (size != 3) errorKan1("%s\n","[key  num1 num2] mpzext.");
                   2594:     obj1 = getoa(obj,1);
                   2595:     obj2 = getoa(obj,2);
1.24    ! takayama 2596:     if (obj1.tag != SuniversalNumber) {
        !          2597:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2598:        }
        !          2599:     if (obj2.tag != SuniversalNumber) {
        !          2600:       obj2 = KdataConversion(obj2,"universalNumber");
        !          2601:        }
1.1       maekawa  2602:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
                   2603:       errorKan1("%s\n","[key num1 num2] mpzext.");
                   2604:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2605:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
1.1       maekawa  2606:       errorKan1("%s\n","[key  num1 num2] mpzext.");
                   2607:     }
                   2608:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2609:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2610:     if (strcmp(key,"probab_prime_p")==0) {
                   2611:       gi = (int) mpz_get_si(g);
                   2612:       if (mpz_probab_prime_p(f,gi)) {
1.7       takayama 2613:         rob = KpoInteger(1);
1.1       maekawa  2614:       }else {
1.7       takayama 2615:         rob = KpoInteger(0);
1.1       maekawa  2616:       }
                   2617:     }else if (strcmp(key,"and")==0) {
                   2618:       r1 = newMP_INT();
                   2619:       mpz_and(r1,f,g);
                   2620:       rob.tag = SuniversalNumber;
                   2621:       rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2622:     }else if (strcmp(key,"ior")==0) {
                   2623:       r1 = newMP_INT();
                   2624:       mpz_ior(r1,f,g);
                   2625:       rob.tag = SuniversalNumber;
                   2626:       rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
                   2627:     }
                   2628:
                   2629:   }else if (strcmp(key,"powm")==0) {
                   2630:     /* three args */
                   2631:     if (size != 4) errorKan1("%s\n","[key num1 num2 num3] mpzext");
                   2632:     obj1 = getoa(obj,1); obj2 = getoa(obj,2); obj3 = getoa(obj,3);
1.24    ! takayama 2633:     if (obj1.tag != SuniversalNumber) {
        !          2634:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2635:        }
        !          2636:     if (obj2.tag != SuniversalNumber) {
        !          2637:       obj2 = KdataConversion(obj2,"universalNumber");
        !          2638:        }
        !          2639:     if (obj3.tag != SuniversalNumber) {
        !          2640:       obj3 = KdataConversion(obj3,"universalNumber");
        !          2641:        }
1.1       maekawa  2642:     if (obj1.tag != SuniversalNumber ||
                   2643:         obj2.tag != SuniversalNumber ||
                   2644:         obj3.tag != SuniversalNumber ) {
                   2645:       errorKan1("%s\n","[key num1 num2 num3] mpzext : num1, num2 and num3 must be universalNumbers.");
                   2646:     }
                   2647:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
1.7       takayama 2648:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber) ||
                   2649:         ! is_this_coeff_MP_INT(obj3.lc.universalNumber)) {
1.1       maekawa  2650:       errorKan1("%s\n","[key num1 num2 num3] mpzext : num1, num2 and num3 must be universalNumbers.");
                   2651:     }
                   2652:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
                   2653:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
                   2654:     h = coeff_to_MP_INT(obj3.lc.universalNumber);
                   2655:     if (mpz_sgn(g) < 0) errorKan1("%s\n","[(powm) base exp mod] mpzext : exp must not be negative.");
                   2656:     r1 = newMP_INT();
                   2657:     mpz_powm(r1,f,g,h);
                   2658:     rob.tag = SuniversalNumber;
                   2659:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
1.24    ! takayama 2660:   } else if (strcmp(key,"lcm")==0) {
        !          2661:     if (size != 3) errorKan1("%s\n","[(lcm)  universalNumber universalNumber] mpzext.");
        !          2662:     obj1 = getoa(obj,1);
        !          2663:     obj2 = getoa(obj,2);
        !          2664:     if (obj1.tag != SuniversalNumber) {
        !          2665:       obj1 = KdataConversion(obj1,"universalNumber");
        !          2666:        }
        !          2667:     if (obj2.tag != SuniversalNumber) {
        !          2668:       obj2 = KdataConversion(obj2,"universalNumber");
        !          2669:        }
        !          2670:     if (obj1.tag != SuniversalNumber || obj2.tag != SuniversalNumber)
        !          2671:       errorKan1("%s\n","[lcm num1 num2] mpzext.");
        !          2672:     if (! is_this_coeff_MP_INT(obj1.lc.universalNumber) ||
        !          2673:         ! is_this_coeff_MP_INT(obj2.lc.universalNumber)) {
        !          2674:       errorKan1("%s\n","[(lcm)  universalNumber universalNumber] mpzext.");
        !          2675:     }
        !          2676:     f = coeff_to_MP_INT(obj1.lc.universalNumber);
        !          2677:     g = coeff_to_MP_INT(obj2.lc.universalNumber);
        !          2678:     r1 = newMP_INT();
        !          2679:     mpz_lcm(r1,f,g);
        !          2680:     rob.tag = SuniversalNumber;
        !          2681:     rob.lc.universalNumber = mpintToCoeff(r1,SmallRingp);
1.1       maekawa  2682:   }else {
                   2683:     errorKan1("%s\n","mpzExtension(): Unknown tag.");
                   2684:   }
                   2685:   return(rob);
                   2686: }
                   2687:
                   2688:
                   2689: /** : context   */
                   2690: struct object KnewContext(struct object superObj,char *name) {
                   2691:   struct context *cp;
                   2692:   struct object ob;
                   2693:   if (superObj.tag != Sclass) {
                   2694:     errorKan1("%s\n","The argument of KnewContext must be a Class.Context");
                   2695:   }
                   2696:   if (superObj.lc.ival != CLASSNAME_CONTEXT) {
                   2697:     errorKan1("%s\n","The argument of KnewContext must be a Class.Context");
                   2698:   }
                   2699:   cp = newContext0((struct context *)(superObj.rc.voidp),name);
                   2700:   ob.tag = Sclass;
                   2701:   ob.lc.ival = CLASSNAME_CONTEXT;
                   2702:   ob.rc.voidp = cp;
                   2703:   return(ob);
                   2704: }
                   2705:
                   2706: struct object KcreateClassIncetance(struct object ob1,
1.7       takayama 2707:                                     struct object ob2,
                   2708:                                     struct object ob3)
1.1       maekawa  2709: {
                   2710:   /* [class-tag super-obj] size [class-tag]  cclass */
                   2711:   struct object ob4;
                   2712:   int size,size2,i;
                   2713:   struct object ob5;
                   2714:   struct object rob;
                   2715:
                   2716:   if (ob1.tag != Sarray)
                   2717:     errorKan1("%s\n","cclass: The first argument must be an array.");
                   2718:   if (getoaSize(ob1) < 1)
                   2719:     errorKan1("%s\n","cclass: The first argument must be [class-tag ....].");
                   2720:   ob4 = getoa(ob1,0);
                   2721:   if (ectag(ob4) != CLASSNAME_CONTEXT)
                   2722:     errorKan1("%s\n","cclass: The first argument must be [class-tag ....].");
                   2723:
                   2724:   if (ob2.tag != Sinteger)
                   2725:     errorKan1("%s\n","cclass: The second argument must be an integer.");
                   2726:   size = KopInteger(ob2);
                   2727:   if (size < 1)
                   2728:     errorKan1("%s\n","cclass: The size must be > 0.");
                   2729:
                   2730:   if (ob3.tag != Sarray)
                   2731:     errorKan1("%s\n","cclass: The third argument must be an array.");
                   2732:   if (getoaSize(ob3) < 1)
                   2733:     errorKan1("%s\n","cclass: The third argument must be [class-tag].");
                   2734:   ob5 = getoa(ob3,0);
                   2735:   if (ectag(ob5) != CLASSNAME_CONTEXT)
                   2736:     errorKan1("%s\n","cclass: The third argument must be [class-tag].");
1.7       takayama 2737:
1.1       maekawa  2738:   rob = newObjectArray(size);
                   2739:   putoa(rob,0,ob5);
                   2740:   if (getoaSize(ob1) < size) size2 = getoaSize(ob1);
                   2741:   else size2 = size;
                   2742:   for (i=1; i<size2; i++) {
                   2743:     putoa(rob,i,getoa(ob1,i));
                   2744:   }
                   2745:   for (i=size2; i<size; i++) {
                   2746:     putoa(rob,i,NullObject);
                   2747:   }
                   2748:   return(rob);
                   2749: }
                   2750:
                   2751:
                   2752: struct object KpoDouble(double a) {
                   2753:   struct object rob;
                   2754:   rob.tag = Sdouble;
                   2755:   /* rob.lc.dbl = (double *)sGC_malloc_atomic(sizeof(double)); */
                   2756:   rob.lc.dbl = (double *)sGC_malloc(sizeof(double));
                   2757:   if (rob.lc.dbl == (double *)NULL) {
                   2758:     fprintf(stderr,"No memory.\n"); exit(10);
                   2759:   }
                   2760:   *(rob.lc.dbl) = a;
                   2761:   return(rob);
                   2762: }
                   2763:
                   2764: double toDouble0(struct object ob) {
                   2765:   double r;
                   2766:   int r3;
                   2767:   struct object ob2;
                   2768:   struct object ob3;
                   2769:   switch(ob.tag) {
                   2770:   case Sinteger:
                   2771:     return( (double) (KopInteger(ob)) );
                   2772:   case SuniversalNumber:
                   2773:     return((double) coeffToInt(ob.lc.universalNumber));
                   2774:   case SrationalFunction:
                   2775:     /* The argument is assumed to be a rational number. */
                   2776:     ob2 = newObjectArray(2);  ob3 = KpoString("cancel");
                   2777:     putoa(ob2,0,ob3); putoa(ob2,1,ob);
                   2778:     ob = KmpzExtension(ob2);
                   2779:     ob2 = *Knumerator(ob);  ob3 = *Kdenominator(ob);
                   2780:     r3 =  coeffToInt(ob3.lc.universalNumber);
                   2781:     if (r3  == 0) {
                   2782:       errorKan1("%s\n","toDouble0(): Division by zero.");
                   2783:       break;
                   2784:     }
                   2785:     r = ((double) coeffToInt(ob2.lc.universalNumber)) / ((double)r3);
                   2786:     return(r);
                   2787:   case Sdouble:
                   2788:     return( KopDouble(ob) );
                   2789:   default:
                   2790:     errorKan1("%s\n","toDouble0(): This type of conversion is not supported.");
                   2791:     break;
                   2792:   }
                   2793:   return(0.0);
                   2794: }
                   2795:
                   2796: struct object KpoGradedPolySet(struct gradedPolySet *grD) {
                   2797:   struct object rob;
                   2798:   rob.tag = Sclass;
                   2799:   rob.lc.ival = CLASSNAME_GradedPolySet;
                   2800:   rob.rc.voidp = (void *) grD;
                   2801:   return(rob);
                   2802: }
                   2803:
                   2804: static char *getspace0(int a) {
                   2805:   char *s;
                   2806:   a = (a > 0? a:-a);
                   2807:   s = (char *) sGC_malloc(a+1);
                   2808:   if (s == (char *)NULL) {
                   2809:     errorKan1("%s\n","no more memory.");
                   2810:   }
                   2811:   return(s);
                   2812: }
                   2813: struct object KdefaultPolyRing(struct object ob) {
                   2814:   struct object rob;
                   2815:   int i,j,k,n;
                   2816:   struct object ob1,ob2,ob3,ob4,ob5;
                   2817:   struct object t1;
                   2818:   char *s1;
                   2819:   extern struct ring *CurrentRingp;
                   2820:   static struct ring *a[N0];
                   2821:
                   2822:   rob = NullObject;
                   2823:   if (ob.tag != Sinteger) {
                   2824:     errorKan1("%s\n","KdefaultPolyRing(): the argument must be integer.");
                   2825:   }
                   2826:   n = KopInteger(ob);
                   2827:   if (n <= 0) {
                   2828:     /* initializing */
                   2829:     for (i=0; i<N0; i++) {
                   2830:       a[i] = (struct ring*) NULL;
                   2831:     }
                   2832:     return(rob);
                   2833:   }
                   2834:
                   2835:   if ( a[n] != (struct ring*)NULL) return(KpoRingp(a[n]));
                   2836:
                   2837:   /* Let's construct ring of polynomials of 2n variables  */
                   2838:   /* x variables */
                   2839:   ob1 = newObjectArray(n);
                   2840:   for (i=0; i<n; i++) {
                   2841:     s1 = getspace0(1+ ((n-i)/10) + 1);
                   2842:     sprintf(s1,"x%d",n-i);
                   2843:     putoa(ob1,i,KpoString(s1));
                   2844:   }
                   2845:   ob2 = newObjectArray(n);
                   2846:   s1 = getspace0(1);
                   2847:   sprintf(s1,"h");
                   2848:   putoa(ob2,0,KpoString(s1));
                   2849:   for (i=1; i<n; i++) {
                   2850:     s1 = getspace0(1+((n+n-i)/10)+1);
                   2851:     sprintf(s1,"x%d",n+n-i);
                   2852:     putoa(ob2,i,KpoString(s1));
                   2853:   }
                   2854:
                   2855:   ob3 = newObjectArray(9);
                   2856:   putoa(ob3,0,KpoInteger(0));
                   2857:   for (i=1; i<9; i++) {
                   2858:     putoa(ob3,i,KpoInteger(n));
                   2859:   }
                   2860:
                   2861:   ob4 = newObjectArray(2*n);
                   2862:   t1 = newObjectArray(2*n);
                   2863:   for (i=0; i<2*n; i++) {
                   2864:     putoa(t1,i,KpoInteger(1));
                   2865:   }
                   2866:   putoa(ob4,0,t1);
                   2867:   for (i=1; i<2*n; i++) {
                   2868:     t1 = newObjectArray(2*n);
                   2869:     for (j=0; j<2*n; j++) {
                   2870:       putoa(t1,j,KpoInteger(0));
                   2871:       if (j == (2*n-i)) {
1.7       takayama 2872:         putoa(t1,j,KpoInteger(-1));
1.1       maekawa  2873:       }
                   2874:     }
                   2875:     putoa(ob4,i,t1);
                   2876:   }
                   2877:
                   2878:   ob5 = newObjectArray(2);
                   2879:   putoa(ob5,0,KpoString("mpMult"));
                   2880:   putoa(ob5,1,KpoString("poly"));
                   2881:
                   2882:   KsetUpRing(ob1,ob2,ob3,ob4,ob5);
                   2883:   a[n] = CurrentRingp;
                   2884:   return(KpoRingp(a[n]));
                   2885: }
                   2886:
                   2887:
                   2888:
                   2889:
                   2890:
                   2891: /******************************************************************
                   2892:      error handler
                   2893: ******************************************************************/
                   2894:
                   2895: errorKan1(str,message)
1.7       takayama 2896:      char *str;
                   2897:      char *message;
1.1       maekawa  2898: {
                   2899:   extern char *GotoLabel;
                   2900:   extern int GotoP;
                   2901:   extern int ErrorMessageMode;
                   2902:   char tmpc[1024];
1.10      takayama 2903:   cancelAlarm();
1.1       maekawa  2904:   if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
                   2905:     sprintf(tmpc,"\nERROR(kanExport[0|1].c): ");
                   2906:     if (strlen(message) < 900) {
                   2907:       strcat(tmpc,message);
                   2908:     }
                   2909:     pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc));
                   2910:   }
                   2911:   if (ErrorMessageMode != 1) {
                   2912:     fprintf(stderr,"\nERROR(kanExport[0|1].c): ");
                   2913:     fprintf(stderr,str,message);
                   2914:   }
                   2915:   /* fprintf(stderr,"Hello "); */
                   2916:   if (GotoP) {
                   2917:     /* fprintf(stderr,"Hello. GOTO "); */
                   2918:     fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel);
                   2919:     GotoP = 0;
                   2920:   }
                   2921:   stdOperandStack(); contextControl(CCRESTORE);
                   2922:   /* fprintf(stderr,"Now. Long jump!\n"); */
1.8       takayama 2923: #if defined(__CYGWIN__)
                   2924:   siglongjmp(EnvOfStackMachine,1);
                   2925: #else
1.1       maekawa  2926:   longjmp(EnvOfStackMachine,1);
1.8       takayama 2927: #endif
1.1       maekawa  2928: }
1.22      takayama 2929:
1.1       maekawa  2930:
                   2931: warningKan(str)
1.7       takayama 2932:      char *str;
1.1       maekawa  2933: {
                   2934:   extern int WarningMessageMode;
                   2935:   extern int Strict;
                   2936:   char tmpc[1024];
                   2937:   if (WarningMessageMode == 1 || WarningMessageMode == 2) {
                   2938:     sprintf(tmpc,"\nWARNING(kanExport[0|1].c): ");
                   2939:     if (strlen(str) < 900) {
                   2940:       strcat(tmpc,str);
                   2941:     }
                   2942:     pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc));
                   2943:   }
                   2944:   if (WarningMessageMode != 1) {
                   2945:     fprintf(stderr,"\nWARNING(kanExport[0|1].c): ");
                   2946:     fprintf(stderr,str);
                   2947:     fprintf(stderr,"\n");
                   2948:   }
                   2949:   /* if (Strict) errorKan1("%s\n"," "); */
                   2950:   if (Strict) errorKan1("%s\n",str);
1.4       takayama 2951:   return(0);
                   2952: }
                   2953:
                   2954: warningKanNoStrictMode(str)
1.7       takayama 2955:      char *str;
1.4       takayama 2956: {
                   2957:   extern int Strict;
                   2958:   int t;
                   2959:   t = Strict;
                   2960:   Strict = 0;
                   2961:   warningKan(str);
                   2962:   Strict = t;
1.1       maekawa  2963:   return(0);
                   2964: }
                   2965:
                   2966:
                   2967:
                   2968:

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