[BACK]Return to minimal.k CVS log [TXT][DIR] Up to [local] / OpenXM / src / k097 / lib / minimal

Annotation of OpenXM/src/k097/lib/minimal/minimal.k, Revision 1.15

1.15    ! takayama    1: /* $OpenXM: OpenXM/src/k097/lib/minimal/minimal.k,v 1.14 2000/06/09 08:04:54 takayama Exp $ */
1.1       takayama    2: #define DEBUG 1
                      3: /* #define ORDINARY 1 */
1.4       takayama    4: /* If you run this program on openxm version 1.1.2 (FreeBSD),
                      5:    make a symbolic link by the command
                      6:    ln -s /usr/bin/cpp /lib/cpp
                      7: */
1.6       takayama    8: #define OFFSET 0
1.14      takayama    9: #define TOTAL_STRATEGY 1
1.6       takayama   10: /* #define OFFSET 20*/
1.1       takayama   11: /* Test sequences.
                     12:    Use load["minimal.k"];;
                     13:
                     14:    a=Sminimal(v);
                     15:    b=a[0];
                     16:    b[1]*b[0]:
                     17:    b[2]*b[1]:
                     18:
                     19:    a = test0();
                     20:    b = a[0];
                     21:    b[1]*b[0]:
                     22:    b[2]*b[1]:
                     23:    a = Sminimal(b[0]);
                     24:
                     25:    a = test1();
                     26:    b=a[0];
                     27:    b[1]*b[0]:
                     28:    b[2]*b[1]:
                     29:
                     30: */
                     31:
                     32:
                     33: load("cohom.k");
                     34: def load_tower() {
                     35:   if (Boundp("k0-tower.sm1.loaded")) {
                     36:   }else{
                     37:     sm1(" [(parse) (k0-tower.sm1) pushfile ] extension ");
                     38:     sm1(" /k0-tower.sm1.loaded 1 def ");
                     39:   }
1.7       takayama   40:   sm1(" oxNoX ");
1.1       takayama   41: }
                     42: load_tower();
                     43: SonAutoReduce = true;
                     44: def Factor(f) {
                     45:    sm1(f, " fctr /FunctionValue set");
                     46: }
                     47: def Reverse(f) {
                     48:    sm1(f," reverse /FunctionValue set");
                     49: }
                     50: def Sgroebner(f) {
                     51:    sm1(" [f] groebner /FunctionValue set");
                     52: }
                     53: def test0() {
                     54:   local f;
                     55:   Sweyl("x,y,z");
                     56:   f = [x^2+y^2+z^2, x*y+x*z+y*z, x*z^2+y*z^2, y^3-x^2*z - x*y*z+y*z^2,
                     57:        -y^2*z^2 + x*z^3 + y*z^3, -z^4];
                     58:   frame=SresolutionFrame(f);
                     59:   Println(frame);
                     60:   /* return(frame); */
                     61:   return(SlaScala(f));
                     62: }
                     63: def test1() {
                     64:   local f;
                     65:   Sweyl("x,y,z");
                     66:   f = [x^2+y^2+z^2, x*y+x*z+y*z, x*z^2+y*z^2, y^3-x^2*z - x*y*z+y*z^2,
                     67:        -y^2*z^2 + x*z^3 + y*z^3, -z^4];
                     68:   return(Sminimal(f));
                     69: }
                     70:
                     71:
                     72:
                     73: def Sweyl(v,w) {
                     74:   /* extern WeightOfSweyl ; */
                     75:   local ww,i,n;
                     76:   if(Length(Arglist) == 1) {
                     77:     sm1(" [v s_ring_of_differential_operators 0 [(schreyer) 1]] define_ring ");
                     78:     sm1(" define_ring_variables ");
                     79:
                     80:     sm1(" [ v to_records pop ] /ww set ");
                     81:     n = Length(ww);
                     82:     WeightOfSweyl = NewArray(n*4);
                     83:     for (i=0; i< n; i++) {
                     84:       WeightOfSweyl[2*i] = ww[i];
                     85:       WeightOfSweyl[2*i+1] = 1;
                     86:     }
                     87:     for (i=0; i< n; i++) {
                     88:       WeightOfSweyl[2*n+2*i] = AddString(["D",ww[i]]);
                     89:       WeightOfSweyl[2*n+2*i+1] = 1;
                     90:     }
                     91:
                     92:   }else{
                     93:     sm1(" [v s_ring_of_differential_operators w s_weight_vector 0 [(schreyer) 1]] define_ring ");
                     94:     sm1(" define_ring_variables ");
                     95:     WeightOfSweyl = w[0];
                     96:   }
                     97: }
                     98:
                     99:
                    100: def Spoly(f) {
                    101:   sm1(f, " toString tparse /FunctionValue set ");
                    102: }
                    103:
                    104: def SreplaceZeroByZeroPoly(f) {
                    105:   if (IsArray(f)) {
                    106:      return(Map(f,"SreplaceZeroByZeroPoly"));
                    107:   }else{
                    108:      if (IsInteger(f)) {
                    109:        return(Poly(ToString(f)));
                    110:      }else{
                    111:        return(f);
                    112:      }
                    113:   }
                    114: }
                    115: def Shomogenize(f) {
                    116:   f = SreplaceZeroByZeroPoly(f);
                    117:   if (IsArray(f)) {
                    118:     sm1(f," sHomogenize2  /FunctionValue set ");
                    119:     /* sm1(f," {sHomogenize2} map  /FunctionValue set ");  */
                    120:     /* Is it correct? Double check.*/
                    121:   }else{
                    122:     sm1(f, " sHomogenize /FunctionValue set ");
                    123:   }
                    124: }
                    125:
                    126: def StoTower() {
                    127:   sm1("  [(AvoidTheSameRing)] pushEnv [ [(AvoidTheSameRing) 0] system_variable (mmLarger) (tower) switch_function ] pop popEnv ");
                    128: }
                    129:
                    130: def SsetTower(tower) {
                    131: sm1(" [(AvoidTheSameRing)] pushEnv
                    132:       [ [(AvoidTheSameRing) 0] system_variable
                    133:         [(gbListTower) tower (list) dc] system_variable
                    134:       ] pop popEnv ");
1.14      takayama  135:       /* sm1("(hoge) message show_ring "); */
1.1       takayama  136: }
                    137:
                    138: def SresolutionFrameWithTower(g,opt) {
                    139:   local gbTower, ans, ff, count, startingGB, opts, skelton,withSkel, autof,
1.15    ! takayama  140:         gbasis, nohomog;
        !           141:   nohomog = false;
        !           142:   count = -1;
1.1       takayama  143:   if (Length(Arglist) >= 2) {
1.15    ! takayama  144:     if (IsInteger(opt)) {
        !           145:       count = opt;
        !           146:     }else if (IsString(opt)) {
        !           147:       if (opt == "homogenized") {
        !           148:          nohomog = true;
        !           149:       }else{
        !           150:          Println("Warning: unknown option");
        !           151:          Println(opt);
        !           152:       }
        !           153:     }
1.1       takayama  154:   }else{
                    155:     count = -1;
                    156:   }
                    157:
                    158:   sm1(" setupEnvForResolution ");
                    159:   /* If I do not put this macro, homogenization
                    160:      make a strange behavior. For example,
                    161:      [(2*x*Dx + 3*y*Dy+6) (0)] homogenize returns
                    162:      [(2*x*Dx*h + 3*y*Dy*h+6*h^3) (0)].
                    163:      4/19, 2000.
                    164:   */
                    165:
                    166:   sm1(" (mmLarger) (matrix) switch_function ");
1.15    ! takayama  167:   if (! nohomog) {
        !           168:     Println("Automatic homogenization.");
        !           169:     g = Map(g,"Shomogenize");
        !           170:   }else{
        !           171:     Println("No automatic homogenization.");
        !           172:   }
1.1       takayama  173:   if (SonAutoReduce) {
                    174:     sm1("[ (AutoReduce) ] system_variable /autof set ");
                    175:     sm1("[ (AutoReduce) 1 ] system_variable ");
                    176:   }
                    177:   gbasis = Sgroebner(g);
                    178:   g = gbasis[0];
                    179:   if (SonAutoReduce) {
                    180:     sm1("[ (AutoReduce) autof] system_variable  ");
                    181:   }
                    182:
                    183:   g = Init(g);
                    184:
                    185: /*  sm1(" setupEnvForResolution-sugar "); */
                    186:   /* -sugar is fine? */
                    187:   sm1(" setupEnvForResolution ");
                    188:
                    189:   Println(g);
                    190:   startingGB = g;
                    191:   /* ans = [ SzeroMap(g) ];  It has not been implemented. see resol1.withZeroMap */
                    192:   ans = [ ];
                    193:   gbTower = [ ];
                    194:   skelton = [ ];
                    195:   while (true) {
                    196:     /* sm1(g," res0Frame /ff set "); */
                    197:     withSkel = Sres0FrameWithSkelton(g);
                    198:     ff = withSkel[0];
                    199:     ans = Append(ans, ff[0]);
                    200:     gbTower = Join([ ff[1] ], gbTower);
                    201:     skelton = Join([ withSkel[1] ], skelton);
                    202:     g = ff[0];
                    203:     if (Length(g) == 0) break;
                    204:     SsetTower( gbTower );
                    205:     if (count == 0) break;
                    206:     count = count - 1;
                    207:   }
                    208:   return([ans,Reverse(gbTower),Join([ [ ] ], Reverse(skelton)),gbasis]);
                    209: }
                    210: HelpAdd(["SresolutionFrameWithTower",
                    211: ["It returs [resolution of the initial, gbTower, skelton, gbasis]",
1.15    ! takayama  212:  "option: \"homogenized\" (no automatic homogenization) ",
1.1       takayama  213:  "Example: Sweyl(\"x,y\");",
                    214:  "         a=SresolutionFrameWithTower([x^3,x*y,y^3-1]);"]]);
                    215:
                    216: def SresolutionFrame(f,opt) {
                    217:   local ans;
1.15    ! takayama  218:   ans = SresolutionFrameWithTower(f,opt);
1.1       takayama  219:   return(ans[0]);
                    220: }
                    221: /* ---------------------------- */
                    222: def ToGradedPolySet(g) {
                    223:   sm1(g," (gradedPolySet) dc /FunctionValue set ");
                    224: }
                    225:
                    226: def NewPolynomialVector(size) {
                    227:   sm1(size," (integer) dc newPolyVector /FunctionValue set ");
                    228: }
                    229:
                    230: def  SturnOffHomogenization() {
                    231:   sm1("
                    232:     [(Homogenize)] system_variable 1 eq
                    233:     { (Warning: Homogenization and ReduceLowerTerms options are automatically turned off.) message
                    234:       [(Homogenize) 0] system_variable
                    235:       [(ReduceLowerTerms) 0] system_variable
                    236:     } {  } ifelse
                    237:   ");
                    238: }
                    239: def  SturnOnHomogenization() {
                    240:   sm1("
                    241:     [(Homogenize)] system_variable 0 eq
                    242:     { (Warning: Homogenization and ReduceLowerTerms options are automatically turned ON.) message
                    243:       [(Homogenize) 1] system_variable
                    244:       [(ReduceLowerTerms) 1] system_variable
                    245:     } {  } ifelse
                    246:   ");
                    247: }
                    248:
                    249: def SschreyerSkelton(g) {
                    250:   sm1(" [(schreyerSkelton) g] gbext /FunctionValue set ");
                    251: }
                    252: def Stoes(g) {
                    253:   if (IsArray(g)) {
                    254:     sm1(g," {toes} map /FunctionValue set ");
                    255:   }else{
                    256:     sm1(g," toes /FunctionValue set ");
                    257:   }
                    258: }
                    259: def Stoes_vec(g) {
                    260:     sm1(g," toes /FunctionValue set ");
                    261: }
                    262:
                    263: def Sres0Frame(g) {
                    264:   local ans;
                    265:   ans = Sres0FrameWithSkelton(g);
                    266:   return(ans[0]);
                    267: }
                    268: def Sres0FrameWithSkelton(g) {
                    269:   local t_syz, nexttower, m, t_gb, skel, betti,
                    270:         gg, k, i, j, pair, tmp, si, sj, grG, syzAll, gLength;
                    271:
                    272:   SturnOffHomogenization();
                    273:
                    274:   g = Stoes(g);
                    275:   skel = SschreyerSkelton(g);
                    276:   /* Print("Skelton is ");
                    277:   sm1_pmat(skel); */
                    278:   betti = Length(skel);
                    279:
                    280:   gLength = Length(g);
                    281:   grG = ToGradedPolySet(g);
                    282:   syzAll = NewPolynomialVector(betti);
                    283:   for (k=0; k<betti; k++) {
                    284:     pair = skel[k];
                    285:     i = pair[0,0];
                    286:     j = pair[0,1];
                    287:     si = pair[1,0];
                    288:     sj = pair[1,1];
                    289:     /* si g[i] + sj g[j] + \sum tmp[2][k] g[k] = 0 in res0 */
                    290:     Print(".");
                    291:
                    292:     t_syz = NewPolynomialVector(gLength);
                    293:     t_syz[i] = si;
                    294:     t_syz[j] = sj;
                    295:     syzAll[k] = t_syz;
                    296:   }
                    297:   t_syz = syzAll;
                    298:   Print("Done. betti="); Println(betti);
                    299:   /* Println(g);  g is in a format such as
                    300:     [e_*x^2 , e_*x*y , 2*x*Dx*h , ...]
                    301:     [e_*x^2 , e_*x*y , 2*x*Dx*h , ...]
                    302:     [y-es*x , 3*es^4*y*Dy-es^5*x , 3*es^5*y*Dy-es^6*x , ...]
                    303:     [3*es^3*y*Dy-es^5*x ]
                    304:   */
                    305:   nexttower = Init(g);
                    306:   SturnOnHomogenization();
                    307:   return([[t_syz, nexttower],skel]);
                    308: }
                    309:
                    310:
                    311: def StotalDegree(f) {
1.14      takayama  312:   local d0;
                    313:   sm1(" [(grade) f] gbext (universalNumber) dc /d0 set ");
                    314:   /* Print("degree of "); Print(f); Print(" is "); Println(d0); */
                    315:   return(d0);
1.1       takayama  316: }
                    317:
                    318: /* Sord_w(x^2*Dx*Dy,[x,-1,Dx,1]); */
                    319: def Sord_w(f,w) {
                    320:   local neww,i,n;
                    321:   n = Length(w);
                    322:   neww = NewArray(n);
                    323:   for (i=0; i<n; i=i+2) {
                    324:     neww[i] = ToString(w[i]);
                    325:   }
                    326:   for (i=1; i<n; i=i+2) {
                    327:     neww[i] = IntegerToSm1Integer(w[i]);
                    328:   }
                    329:   sm1(" f neww ord_w (universalNumber) dc /FunctionValue set ");
                    330: }
                    331:
                    332:
                    333: /* This is not satisfactory. */
                    334: def SinitOfArray(f) {
                    335:   local p,pos,top;
                    336:   if (IsArray(f)) {
                    337:      sm1(f," toes init /p set ");
                    338:      sm1(p," (es). degree (universalNumber) dc /pos set ");
                    339:      return([Init(f[pos]),pos]);
                    340:   } else {
                    341:      return(Init(f));
                    342:   }
                    343: }
                    344:
                    345: def test_SinitOfArray() {
                    346:   local f, frame,p,tower,i,j,k;
                    347:   Sweyl("x,y,z");
                    348:   f = [x^2+y^2+z^2, x*y+x*z+y*z, x*z^2+y*z^2, y^3-x^2*z - x*y*z+y*z^2,
                    349:        -y^2*z^2 + x*z^3 + y*z^3, -z^4];
                    350:   p=SresolutionFrameWithTower(f);
                    351:   sm1_pmat(p);
                    352:   sm1_pmat(SgenerateTable(p[1]));
                    353:   return(p);
                    354:   frame = p[0];
                    355:   sm1_pmat(p[1]);
                    356:   sm1_pmat(frame);
                    357:   sm1_pmat(Map(frame[0],"SinitOfArray"));
                    358:   sm1_pmat(Map(frame[1],"SinitOfArray"));
                    359:   return(p);
                    360: }
                    361:
                    362: /* f is assumed to be a monomial with toes. */
                    363: def Sdegree(f,tower,level) {
1.6       takayama  364:   local i,ww, wd;
                    365:   /* extern WeightOfSweyl; */
                    366:   ww = WeightOfSweyl;
1.5       takayama  367:   f = Init(f);
1.1       takayama  368:   if (level <= 1) return(StotalDegree(f));
                    369:   i = Degree(f,es);
1.6       takayama  370: #ifdef TOTAL_STRATEGY
                    371:   return(StotalDegree(f)+Sdegree(tower[level-2,i],tower,level-1));
                    372: #endif
                    373:   /* Strategy must be compatible with ordering.  */
                    374:   /* Weight vector must be non-negative, too.  */
                    375:   /* See Sdegree, SgenerateTable, reductionTable. */
                    376:   wd = Sord_w(f,ww);
                    377:   return(wd+Sdegree(tower[level-2,i],tower,level-1));
                    378:
1.1       takayama  379: }
                    380:
                    381: def SgenerateTable(tower) {
                    382:   local height, n,i,j, ans, ans_at_each_floor;
                    383:   height = Length(tower);
                    384:   ans = NewArray(height);
                    385:   for (i=0; i<height; i++) {
                    386:     n = Length(tower[i]);
                    387:     ans_at_each_floor=NewArray(n);
                    388:     for (j=0; j<n; j++) {
1.6       takayama  389:       ans_at_each_floor[j] = Sdegree(tower[i,j],tower,i+1)-(i+1)
                    390:                             + OFFSET;
1.1       takayama  391:       /* Println([i,j,ans_at_each_floor[j]]); */
                    392:     }
                    393:     ans[i] = ans_at_each_floor;
                    394:   }
                    395:   return(ans);
                    396: }
                    397: Sweyl("x,y,z");
                    398: v=[[2*x*Dx + 3*y*Dy+6, 0],
                    399:    [3*x^2*Dy + 2*y*Dx, 0],
                    400:    [0,  x^2+y^2],
                    401:    [0,  x*y]];
                    402: /*  SresolutionFrameWithTower(v); */
                    403:
                    404: def SnewArrayOfFormat(p) {
                    405:   if (IsArray(p)) {
                    406:      return(Map(p,"SnewArrayOfFormat"));
                    407:   }else{
                    408:      return(null);
                    409:   }
                    410: }
1.4       takayama  411: def ScopyArray(a) {
                    412:   local n, i,ans;
                    413:   n = Length(a);
                    414:   ans = NewArray(n);
                    415:   for (i=0; i<n; i++) {
                    416:     ans[i] = a[i];
                    417:   }
                    418:   return(ans);
                    419: }
1.1       takayama  420: def SminOfStrategy(a) {
                    421:   local n,i,ans,tt;
                    422:   ans = 100000; /* very big number */
                    423:   if (IsArray(a)) {
                    424:     n = Length(a);
                    425:     for (i=0; i<n; i++) {
                    426:       if (IsArray(a[i])) {
                    427:         tt = SminOfStrategy(a[i]);
                    428:         if (tt < ans) ans = tt;
                    429:       }else{
                    430:         if (a[i] < ans) ans = a[i];
                    431:       }
                    432:     }
                    433:   }else{
                    434:      if (a < ans) ans = a;
                    435:   }
                    436:   return(ans);
                    437: }
                    438: def SmaxOfStrategy(a) {
                    439:   local n,i,ans,tt;
                    440:   ans = -100000; /* very small number */
                    441:   if (IsArray(a)) {
                    442:     n = Length(a);
                    443:     for (i=0; i<n; i++) {
                    444:       if (IsArray(a[i])) {
                    445:         tt = SmaxOfStrategy(a[i]);
                    446:         if (tt > ans) ans = tt;
                    447:       }else{
                    448:         if (a[i] > ans) ans = a[i];
                    449:       }
                    450:     }
                    451:   }else{
                    452:      if (a > ans) ans = a;
                    453:   }
                    454:   return(ans);
                    455: }
                    456:
                    457:
1.15    ! takayama  458: def SlaScala(g,opt) {
1.1       takayama  459:   local rf, tower, reductionTable, skel, redundantTable, bases,
                    460:         strategy, maxOfStrategy, height, level, n, i,
                    461:         freeRes,place, f, reducer,pos, redundant_seq,bettiTable,freeResV,ww,
1.4       takayama  462:         redundantTable_ordinary, redundant_seq_ordinary,
                    463:         reductionTable_tmp;
1.1       takayama  464:   /* extern WeightOfSweyl; */
                    465:   ww = WeightOfSweyl;
1.6       takayama  466:   Print("WeightOfSweyl="); Println(WeightOfSweyl);
1.15    ! takayama  467:   rf = SresolutionFrameWithTower(g,opt);
1.14      takayama  468:   Print("rf="); sm1_pmat(rf);
1.1       takayama  469:   redundant_seq = 1;   redundant_seq_ordinary = 1;
                    470:   tower = rf[1];
                    471:   reductionTable = SgenerateTable(tower);
                    472:   skel = rf[2];
                    473:   redundantTable = SnewArrayOfFormat(rf[1]);
                    474:   redundantTable_ordinary = SnewArrayOfFormat(rf[1]);
                    475:   reducer = SnewArrayOfFormat(rf[1]);
                    476:   freeRes = SnewArrayOfFormat(rf[1]);
                    477:   bettiTable = SsetBettiTable(rf[1],g);
                    478:
                    479:   strategy = SminOfStrategy( reductionTable );
                    480:   maxOfStrategy = SmaxOfStrategy( reductionTable );
                    481:   height = Length(reductionTable);
                    482:   while (strategy <= maxOfStrategy) {
                    483:     for (level = 0; level < height; level++) {
                    484:       n = Length(reductionTable[level]);
1.4       takayama  485:       reductionTable_tmp = ScopyArray(reductionTable[level]);
                    486:       while (SthereIs(reductionTable_tmp,strategy)) {
                    487:         i = SnextI(reductionTable_tmp,strategy,redundantTable,
                    488:                    skel,level,freeRes);
                    489:         Println([level,i]);
                    490:         reductionTable_tmp[i] = -200000;
1.1       takayama  491:         if (reductionTable[level,i] == strategy) {
                    492:            Print("Processing "); Print([level,i]);
                    493:            Print("   Strategy = "); Println(strategy);
                    494:            if (level == 0) {
                    495:              if (IsNull(redundantTable[level,i])) {
                    496:                bases = freeRes[level];
                    497:                /* Println(["At floor : GB=",i,bases,tower[0,i]]); */
                    498:                pos = SwhereInGB(tower[0,i],rf[3,0]);
                    499:                bases[i] = rf[3,0,pos];
                    500:                redundantTable[level,i] = 0;
                    501:                redundantTable_ordinary[level,i] = 0;
                    502:                freeRes[level] = bases;
                    503:                /* Println(["GB=",i,bases,tower[0,i]]); */
                    504:              }
                    505:            }else{ /* level >= 1 */
                    506:              if (IsNull(redundantTable[level,i])) {
                    507:                bases = freeRes[level];
                    508:                f = SpairAndReduction(skel,level,i,freeRes,tower,ww);
                    509:                if (f[0] != Poly("0")) {
                    510:                   place = f[3];
                    511:                   /* (level-1, place) is the place for f[0],
                    512:                      which is a newly obtained  GB. */
                    513: #ifdef ORDINARY
                    514:                   redundantTable[level-1,place] = redundant_seq;
                    515:                   redundant_seq++;
                    516: #else
                    517:                   if (f[4] > f[5]) {
                    518:                     /* Zero in the gr-module */
                    519:                     Print("v-degree of [org,remainder] = ");
                    520:                     Println([f[4],f[5]]);
                    521:                     Print("[level,i] = "); Println([level,i]);
                    522:                     redundantTable[level-1,place] = 0;
                    523:                   }else{
                    524:                     redundantTable[level-1,place] = redundant_seq;
                    525:                     redundant_seq++;
                    526:                   }
                    527: #endif
                    528:                   redundantTable_ordinary[level-1,place]
                    529:                      =redundant_seq_ordinary;
                    530:                   redundant_seq_ordinary++;
                    531:                   bases[i] = SunitOfFormat(place,f[1])-f[1];  /* syzygy */
                    532:                   redundantTable[level,i] = 0;
                    533:                   redundantTable_ordinary[level,i] = 0;
                    534:                   /* i must be equal to f[2], I think. Double check. */
                    535:                   freeRes[level] = bases;
                    536:                   bases = freeRes[level-1];
                    537:                   bases[place] = f[0];
                    538:                   freeRes[level-1] = bases;
                    539:                   reducer[level-1,place] = f[1];
                    540:                }else{
                    541:                   redundantTable[level,i] = 0;
                    542:                   bases = freeRes[level];
                    543:                   bases[i] = f[1];  /* Put the syzygy. */
                    544:                   freeRes[level] = bases;
                    545:                }
                    546:              }
                    547:            } /* end of level >= 1 */
                    548:         }
                    549:       }
                    550:     }
                    551:     strategy++;
                    552:   }
                    553:   n = Length(freeRes);
                    554:   freeResV = SnewArrayOfFormat(freeRes);
                    555:   for (i=0; i<n; i++) {
                    556:     bases = freeRes[i];
                    557:     bases = Sbases_to_vec(bases,bettiTable[i]);
                    558:     freeResV[i] = bases;
                    559:   }
                    560:   return([freeResV, redundantTable,reducer,bettiTable,redundantTable_ordinary]);
                    561: }
1.4       takayama  562:
                    563: def SthereIs(reductionTable_tmp,strategy) {
                    564:   local n,i;
                    565:   n = Length(reductionTable_tmp);
                    566:   for (i=0; i<n; i++) {
                    567:     if (reductionTable_tmp[i] == strategy) {
                    568:       return(true);
                    569:     }
                    570:   }
                    571:   return(false);
                    572: }
                    573:
                    574: def SnextI(reductionTable_tmp,strategy,redundantTable,
                    575:                                   skel,level,freeRes)
                    576: {
                    577:    local ii,n,p,myindex,i,j,bases;
                    578:    n = Length(reductionTable_tmp);
                    579:    if (level == 0) {
                    580:      for (ii=0; ii<n; ii++) {
                    581:        if (reductionTable_tmp[ii] == strategy) {
                    582:           return(ii);
                    583:         }
                    584:       }
                    585:    }else{
                    586:      for (ii=0; ii<n; ii++) {
                    587:        if (reductionTable_tmp[ii] == strategy) {
                    588:          p = skel[level,ii];
                    589:          myindex = p[0];
                    590:          i = myindex[0]; j = myindex[1];
                    591:          bases = freeRes[level-1];
                    592:          if (IsNull(bases[i]) || IsNull(bases[j])) {
                    593:
                    594:          }else{
                    595:            return(ii);
                    596:          }
                    597:        }
                    598:      }
                    599:    }
1.5       takayama  600:    Print("reductionTable_tmp=");
1.4       takayama  601:    Println(reductionTable_tmp);
1.5       takayama  602:    Println("See also reductionTable, strategy, level,i");
1.4       takayama  603:    Error("SnextI: bases[i] or bases[j] is null for all combinations.");
                    604: }
                    605:
                    606:
1.1       takayama  607:
                    608: def SsetBettiTable(freeRes,g) {
                    609:   local level,i, n,bases,ans;
                    610:   ans = NewArray(Length(freeRes)+1);
                    611:   n = Length(freeRes);
                    612:   if (IsArray(g[0])) {
                    613:     ans[0] = Length(g[0]);
                    614:   }else{
                    615:     ans[0] = 1;
                    616:   }
                    617:   for (level=0; level<n; level++) {
                    618:     bases = freeRes[level];
                    619:     if (IsArray(bases)) {
                    620:       ans[level+1] = Length(bases);
                    621:     }else{
                    622:       ans[level+1] = 1;
                    623:     }
                    624:   }
                    625:   return(ans);
                    626: }
                    627:
                    628: def SwhereInGB(f,tower) {
                    629:   local i,n,p,q;
                    630:   n = Length(tower);
                    631:   for (i=0; i<n; i++) {
                    632:     p = MonomialPart(tower[i]);
                    633:     q = MonomialPart(f);
                    634:     if (p == q) return(i);
                    635:   }
                    636:   Println([f,tower]);
                    637:   Error("whereInGB : [f,myset]: f could not be found in the myset.");
                    638: }
                    639: def SunitOfFormat(pos,forms) {
                    640:   local ans,i,n;
                    641:   n = Length(forms);
                    642:   ans = NewArray(n);
                    643:   for (i=0; i<n; i++) {
                    644:     if (i != pos) {
                    645:       ans[i] = Poly("0");
                    646:     }else{
                    647:       ans[i] = Poly("1");
                    648:     }
                    649:   }
                    650:   return(ans);
                    651: }
                    652:
                    653: def Error(s) {
                    654:   sm1(" s error ");
                    655: }
                    656:
                    657: def IsNull(s) {
                    658:   if (Stag(s) == 0) return(true);
                    659:   else return(false);
                    660: }
                    661:
                    662: def StowerOf(tower,level) {
                    663:   local ans,i;
                    664:   ans = [ ];
                    665:   if (level == 0) return([[]]);
                    666:   for (i=0; i<level; i++) {
                    667:     ans = Append(ans,tower[i]);
                    668:   }
                    669:   return(Reverse(ans));
                    670: }
                    671:
                    672: def Sspolynomial(f,g) {
                    673:   if (IsArray(f)) {
                    674:     f = Stoes_vec(f);
                    675:   }
                    676:   if (IsArray(g)) {
                    677:     g = Stoes_vec(g);
                    678:   }
                    679:   sm1("f g spol /FunctionValue set");
                    680: }
                    681:
                    682: def MonomialPart(f) {
                    683:   sm1(" [(lmonom) f] gbext /FunctionValue set ");
                    684: }
                    685:
1.14      takayama  686: /* WARNING:
                    687:   When you use SwhereInTower, you have to change gbList
                    688:   as below. Ofcourse, you should restrore the gbList
                    689:   SsetTower(StowerOf(tower,level));
                    690:   pos = SwhereInTower(syzHead,tower[level]);
                    691: */
1.1       takayama  692: def SwhereInTower(f,tower) {
                    693:   local i,n,p,q;
                    694:   if (f == Poly("0")) return(-1);
                    695:   n = Length(tower);
                    696:   for (i=0; i<n; i++) {
                    697:     p = MonomialPart(tower[i]);
                    698:     q = MonomialPart(f);
                    699:     if (p == q) return(i);
                    700:   }
                    701:   Println([f,tower]);
                    702:   Error("[f,tower]: f could not be found in the tower.");
                    703: }
                    704:
                    705: def Stag(f) {
                    706:   sm1(f," tag (universalNumber) dc /FunctionValue set");
                    707: }
                    708:
                    709: def SpairAndReduction(skel,level,ii,freeRes,tower,ww) {
                    710:   local i, j, myindex, p, bases, tower2, gi, gj,
                    711:        si, sj, tmp, t_syz, pos, ans, ssp, syzHead,pos2,
                    712:        vdeg,vdeg_reduced;
                    713:   Println("SpairAndReduction:");
                    714:
                    715:   if (level < 1) Error("level should be >= 1 in SpairAndReduction.");
                    716:   p = skel[level,ii];
                    717:   myindex = p[0];
                    718:   i = myindex[0]; j = myindex[1];
                    719:   bases = freeRes[level-1];
                    720:   Println(["p and bases ",p,bases]);
                    721:   if (IsNull(bases[i]) || IsNull(bases[j])) {
                    722:     Println([level,i,j,bases[i],bases[j]]);
                    723:     Error("level, i, j : bases[i], bases[j]  must not be NULL.");
                    724:   }
                    725:
                    726:   tower2 = StowerOf(tower,level-1);
                    727:   SsetTower(tower2);
1.14      takayama  728:   Println(["level=",level]);
                    729:   Println(["tower2=",tower2]);
1.1       takayama  730:   /** sm1(" show_ring ");   */
                    731:
                    732:   gi = Stoes_vec(bases[i]);
                    733:   gj = Stoes_vec(bases[j]);
                    734:
                    735:   ssp = Sspolynomial(gi,gj);
                    736:   si = ssp[0,0];
                    737:   sj = ssp[0,1];
                    738:   syzHead = si*es^i;
                    739:   /* This will be the head term, I think. But, double check. */
                    740:   Println([si*es^i,sj*es^j]);
                    741:
                    742:   Print("[gi, gj] = "); Println([gi,gj]);
                    743:   sm1(" [(Homogenize)] system_variable message ");
                    744:   Print("Reduce the element "); Println(si*gi+sj*gj);
                    745:   Print("by  "); Println(bases);
                    746:
                    747:   tmp = Sreduction(si*gi+sj*gj, bases);
                    748:
                    749:   Print("result is "); Println(tmp);
                    750:
1.3       takayama  751:   /* This is essential part for V-minimal resolution. */
                    752:   /* vdeg = SvDegree(si*gi+sj*gj,tower,level-1,ww); */
                    753:   vdeg = SvDegree(si*gi,tower,level-1,ww);
1.1       takayama  754:   vdeg_reduced = SvDegree(tmp[0],tower,level-1,ww);
                    755:   Print("vdegree of the original = "); Println(vdeg);
                    756:   Print("vdegree of the remainder = "); Println(vdeg_reduced);
                    757:
                    758:   t_syz = tmp[2];
                    759:   si = si*tmp[1]+t_syz[i];
                    760:   sj = sj*tmp[1]+t_syz[j];
                    761:   t_syz[i] = si;
                    762:   t_syz[j] = sj;
1.14      takayama  763:
                    764:   SsetTower(StowerOf(tower,level));
1.1       takayama  765:   pos = SwhereInTower(syzHead,tower[level]);
1.14      takayama  766:
                    767:   SsetTower(StowerOf(tower,level-1));
1.1       takayama  768:   pos2 = SwhereInTower(tmp[0],tower[level-1]);
                    769:   ans = [tmp[0],t_syz,pos,pos2,vdeg,vdeg_reduced];
                    770:   /* pos is the place to put syzygy at level. */
                    771:   /* pos2 is the place to put a new GB at level-1. */
                    772:   Println(ans);
                    773:   return(ans);
                    774: }
                    775:
                    776: def Sreduction(f,myset) {
                    777:   local n, indexTable, set2, i, j, tmp, t_syz;
                    778:   n = Length(myset);
                    779:   indexTable = NewArray(n);
                    780:   set2 = [ ];
                    781:   j = 0;
                    782:   for (i=0; i<n; i++) {
                    783:     if (IsNull(myset[i])) {
                    784:       indexTable[i] = -1;
                    785: /*    }else if (myset[i] == Poly("0")) {
                    786:       indexTable[i] = -1;  */
                    787:     }else{
                    788:       set2 = Append(set2,Stoes_vec(myset[i]));
                    789:       indexTable[i] = j;
                    790:       j++;
                    791:     }
                    792:   }
                    793:   sm1(" f toes set2 (gradedPolySet) dc reduction /tmp set ");
                    794:   t_syz = NewArray(n);
                    795:   for (i=0; i<n; i++) {
                    796:     if (indexTable[i] != -1) {
                    797:       t_syz[i] = tmp[2, indexTable[i]];
                    798:     }else{
                    799:       t_syz[i] = Poly("0");
                    800:     }
                    801:   }
                    802:   return([tmp[0],tmp[1],t_syz]);
                    803: }
                    804:
                    805: def Warning(s) {
                    806:   Print("Warning: ");
                    807:   Println(s);
                    808: }
                    809: def RingOf(f) {
                    810:   local r;
                    811:   if (IsPolynomial(f)) {
                    812:     if (f != Poly("0")) {
                    813:       sm1(f," (ring) dc /r set ");
                    814:     }else{
                    815:       sm1(" [(CurrentRingp)] system_variable /r set ");
                    816:     }
                    817:   }else{
                    818:     Warning("RingOf(f): the argument f must be a polynomial. Return the current ring.");
                    819:     sm1(" [(CurrentRingp)] system_variable /r set ");
                    820:   }
                    821:   return(r);
                    822: }
                    823:
                    824: def Sfrom_es(f,size) {
                    825:   local c,ans, i, d, myes, myee, j,n,r,ans2;
                    826:   if (Length(Arglist) < 2) size = -1;
                    827:   if (IsArray(f)) return(f);
                    828:   r = RingOf(f);
                    829:   myes = PolyR("es",r);
                    830:   myee = PolyR("e_",r);
                    831:   if (Degree(f,myee) > 0 && size == -1) {
                    832:     if (size == -1) {
                    833:        sm1(f," (array) dc /ans set");
                    834:        return(ans);
                    835:     }
                    836:   }
                    837:
                    838: /*
                    839:     Coefficients(x^2-1,x):
                    840:     [    [    2 , 0 ]  , [    1 , -1 ]  ]
                    841: */
                    842:   if (Degree(f,myee) > 0) {
                    843:     c = Coefficients(f,myee);
                    844:   }else{
                    845:     c = Coefficients(f,myes);
                    846:   }
                    847:   if (size < 0) {
                    848:     size = c[0,0]+1;
                    849:   }
                    850:   ans = NewArray(size);
                    851:   for (i=0; i<size; i++) {ans[i] = 0;}
                    852:   n = Length(c[0]);
                    853:   for (j=0; j<n; j++) {
                    854:     d = c[0,j];
                    855:     ans[d] = c[1,j];
                    856:   }
                    857:   return(ans);
                    858: }
                    859:
                    860: def Sbases_to_vec(bases,size) {
                    861:   local n, giveSize, newbases,i;
                    862:   /*  bases = [1+es*x, [1,2,3*x]] */
                    863:   if (Length(Arglist) > 1) {
                    864:     giveSize = true;
                    865:   }else{
                    866:     giveSize = false;
                    867:   }
                    868:   n = Length(bases);
                    869:   newbases = NewArray(n);
                    870:   for (i=0; i<n; i++) {
                    871:      if (giveSize) {
                    872:        newbases[i] = Sfrom_es(bases[i], size);
                    873:      }else{
                    874:        newbases[i] = Sfrom_es(bases[i]);
                    875:      }
                    876:   }
                    877:   return(newbases);
                    878: }
                    879:
1.14      takayama  880: HelpAdd(["Sminimal",
                    881: ["It constructs the V-minimal free resolution by LaScala-Stillman's algorithm",
1.15    ! takayama  882:  "option: \"homogenized\" (no automatic homogenization ",
1.14      takayama  883:  "Example:  Sweyl(\"x,y\",[[\"x\",-1,\"y\",-1,\"Dx\",1,\"Dy\",1]]);",
                    884:  "          v=[[2*x*Dx + 3*y*Dy+6, 0],",
                    885:  "             [3*x^2*Dy + 2*y*Dx, 0],",
                    886:  "             [0,  x^2+y^2],",
                    887:  "             [0,  x*y]];",
                    888:  "         a=Sminimal(v);",
                    889:  "         Sweyl(\"x,y\",[[\"x\",-1,\"y\",-1,\"Dx\",1,\"Dy\",1]]);",
                    890:  "         b = ReParse(a[0]); sm1_pmat(b); ",
                    891:  "         IsExact_h(b,[x,y]):",
                    892:  "Note:  a[0] is the V-minimal resolution. a[3] is the Schreyer resolution."]]);
                    893:
1.15    ! takayama  894: def Sminimal(g,opt) {
1.1       takayama  895:   local r, freeRes, redundantTable, reducer, maxLevel,
                    896:         minRes, seq, maxSeq, level, betti, q, bases, dr,
1.14      takayama  897:         betti_levelplus, newbases, i, j,qq, tminRes;
1.15    ! takayama  898:   r = SlaScala(g,opt);
1.1       takayama  899:   /* Should I turn off the tower?? */
                    900:   freeRes = r[0];
                    901:   redundantTable = r[1];
                    902:   reducer = r[2];
                    903:   minRes = SnewArrayOfFormat(freeRes);
                    904:   seq = 0;
                    905:   maxSeq = SgetMaxSeq(redundantTable);
                    906:   maxLevel = Length(freeRes);
                    907:   for (level = 0; level < maxLevel; level++) {
                    908:     minRes[level] = freeRes[level];
                    909:   }
                    910:   seq=maxSeq+1;
                    911:   while (seq > 1) {
                    912:     seq--;
                    913:     for (level = 0; level < maxLevel; level++) {
                    914:       betti = Length(freeRes[level]);
                    915:       for (q = 0; q<betti; q++) {
                    916:         if (redundantTable[level,q] == seq) {
                    917:           Print("[seq,level,q]="); Println([seq,level,q]);
                    918:           if (level < maxLevel-1) {
                    919:             bases = freeRes[level+1];
                    920:             dr = reducer[level,q];
                    921:             dr[q] = -1;
                    922:             newbases = SnewArrayOfFormat(bases);
                    923:             betti_levelplus = Length(bases);
                    924:             /*
                    925:                bases[i,j] ---> bases[i,j]+bases[i,q]*dr[j]
                    926:             */
                    927:             for (i=0; i<betti_levelplus; i++) {
                    928:               newbases[i] = bases[i] + bases[i,q]*dr;
                    929:             }
                    930:             Println(["level, q =", level,q]);
                    931:             Println("bases="); sm1_pmat(bases);
                    932:             Println("dr="); sm1_pmat(dr);
                    933:             Println("newbases="); sm1_pmat(newbases);
                    934:             minRes[level+1] = newbases;
                    935:             freeRes = minRes;
                    936: #ifdef DEBUG
                    937:             for (qq=0; qq<betti; qq++) {
                    938:               if ((redundantTable[level,qq] >= seq) &&
                    939:                   (redundantTable[level,qq] <= maxSeq)) {
                    940:                 for (i=0; i<betti_levelplus; i++) {
                    941:                   if (!IsZero(newbases[i,qq])) {
                    942:                     Println(["[i,qq]=",[i,qq]," is not zero in newbases."]);
                    943:                     Print("redundantTable ="); sm1_pmat(redundantTable[level]);
                    944:                     Error("Stop in Sminimal for debugging.");
                    945:                   }
                    946:                 }
                    947:               }
                    948:             }
                    949: #endif
                    950:           }
                    951:         }
                    952:       }
                    953:     }
                    954:    }
1.14      takayama  955:    tminRes = Stetris(minRes,redundantTable);
                    956:    return([SpruneZeroRow(tminRes), tminRes,
1.3       takayama  957:           [ minRes, redundantTable, reducer,r[3],r[4]],r[0]]);
1.1       takayama  958:   /* r[4] is the redundantTable_ordinary */
1.3       takayama  959:   /* r[0] is the freeResolution */
1.1       takayama  960: }
                    961:
                    962:
                    963: def IsZero(f) {
                    964:   if (IsPolynomial(f)) {
                    965:     return( f == Poly("0"));
                    966:   }else if (IsInteger(f)) {
                    967:     return( f == 0);
                    968:   }else if (IsSm1Integer(f)) {
                    969:     return( f == true );
                    970:   }else if (IsDouble(f)) {
                    971:     return( f == 0.0 );
                    972:   }else if (IsRational(f)) {
                    973:     return(IsZero(Denominator(f)));
                    974:   }else{
                    975:     Error("IsZero: cannot deal with this data type.");
                    976:   }
                    977: }
                    978: def SgetMaxSeq(redundantTable) {
                    979:    local level,i,n,ans, levelMax,bases;
                    980:    levelMax = Length( redundantTable );
                    981:    ans = 0;
                    982:    for (level = 0; level < levelMax; level++) {
                    983:      bases = redundantTable[level];
                    984:      n = Length(bases);
                    985:      for (i=0; i<n; i++) {
                    986:        if (IsInteger( bases[i] )) {
                    987:           if (bases[i] > ans) {
                    988:              ans = bases[i];
                    989:           }
                    990:        }
                    991:      }
                    992:    }
                    993:    return(ans);
                    994: }
                    995:
                    996: def Stetris(freeRes,redundantTable) {
                    997:   local level, i, j, resLength, minRes,
                    998:         bases, newbases, newbases2;
                    999:   minRes = SnewArrayOfFormat(freeRes);
                   1000:   resLength = Length( freeRes );
                   1001:   for (level=0; level<resLength; level++) {
                   1002:     bases = freeRes[level];
                   1003:     newbases = SnewArrayOfFormat(bases);
                   1004:     betti = Length(bases); j = 0;
                   1005:     /* Delete rows */
                   1006:     for (i=0; i<betti; i++) {
                   1007:       if (redundantTable[level,i] < 1) {
                   1008:          newbases[j] = bases[i];
                   1009:          j++;
                   1010:       }
                   1011:     }
                   1012:     bases = SfirstN(newbases,j);
                   1013:     if (level > 0) {
                   1014:       /* Delete columns */
                   1015:       newbases = Transpose(bases);
                   1016:       betti = Length(newbases); j = 0;
                   1017:       newbases2 = SnewArrayOfFormat(newbases);
                   1018:       for (i=0; i<betti; i++) {
                   1019:         if (redundantTable[level-1,i] < 1) {
                   1020:            newbases2[j] = newbases[i];
                   1021:            j++;
                   1022:         }
                   1023:       }
                   1024:       newbases = Transpose(SfirstN(newbases2,j));
                   1025:     }else{
                   1026:       newbases = bases;
                   1027:     }
                   1028:     Println(["level=", level]);
                   1029:     sm1_pmat(bases);
                   1030:     sm1_pmat(newbases);
                   1031:
                   1032:     minRes[level] = newbases;
                   1033:   }
                   1034:   return(minRes);
                   1035: }
                   1036:
                   1037: def SfirstN(bases,k) {
                   1038:    local ans,i;
                   1039:    ans = NewArray(k);
                   1040:    for (i=0; i<k; i++) {
                   1041:      ans[i] = bases[i];
                   1042:    }
                   1043:    return(ans);
                   1044: }
                   1045:
                   1046:
                   1047: /* usage:  tt is tower. ww is weight.
                   1048:     a = SresolutionFrameWithTower(v);
                   1049:     tt = a[1];
                   1050:     ww = [x,1,y,1,Dx,1,Dy,1];
                   1051:     SvDegree(x*es,tt,1,ww):
                   1052:
                   1053: In(17)=tt:
                   1054: [[2*x*Dx , e_*x^2 , e_*x*y , 3*x^2*Dy , e_*y^3 , 9*x*y*Dy^2 , 27*y^2*Dy^3 ]  ,
                   1055:  [es*y , 3*es^3*y*Dy , 3*es^5*y*Dy , 3*x*Dy , es^2*y^2 , 9*y*Dy^2 ]  ,
                   1056:  [3*es^3*y*Dy ]  ]
                   1057: In(18)=SvDegree(x*es,tt,1,ww):
                   1058: 3
                   1059: In(19)=SvDegree(x*es^3,tt,1,ww):
                   1060: 4
                   1061: In(20)=SvDegree(x,tt,2,ww):
                   1062: 4
                   1063:
                   1064: */
                   1065: def SvDegree(f,tower,level,w) {
                   1066:   local i,ans;
                   1067:   if (IsZero(f)) return(null);
1.3       takayama 1068:   f = Init(f);
1.1       takayama 1069:   if (level <= 0) {
                   1070:     return(Sord_w(f,w));
                   1071:   }
                   1072:   i = Degree(f,es);
                   1073:   ans = Sord_w(f,w) +
                   1074:         SvDegree(tower[level-1,i],tower,level-1,w);
                   1075:   return(ans);
                   1076: }
                   1077:
1.2       takayama 1078: def Sannfs(f,v) {
                   1079:   local f2;
                   1080:   f2 = ToString(f);
                   1081:   if (IsArray(v)) {
                   1082:      v = Map(v,"ToString");
                   1083:   }
                   1084:   sm1(" [f2 v] annfs /FunctionValue set ");
                   1085: }
                   1086:
                   1087: /* Sannfs2("x^3-y^2"); */
                   1088: def Sannfs2(f) {
                   1089:   local p,pp;
                   1090:   p = Sannfs(f,"x,y");
1.6       takayama 1091:   sm1(" p 0 get { [(x) (y) (Dx) (Dy)] laplace0 } map /p set ");
1.5       takayama 1092: /*
                   1093:   Sweyl("x,y",[["x",1,"y",1,"Dx",1,"Dy",1,"h",1],
                   1094:                ["x",-1,"y",-1,"Dx",1,"Dy",1]]); */
1.6       takayama 1095:   /* Sweyl("x,y",[["x",1,"y",1,"Dx",1,"Dy",1,"h",1]]); */
1.10      takayama 1096:
1.6       takayama 1097:   Sweyl("x,y",[["x",-1,"y",-1,"Dx",1,"Dy",1]]);
                   1098:   pp = Map(p,"Spoly");
                   1099:   return(Sminimal_v(pp));
                   1100:   /* return(Sminimal(pp)); */
                   1101: }
                   1102:
1.10      takayama 1103: HelpAdd(["Sannfs2",
                   1104: ["Sannfs2(f) constructs the V-minimal free resolution for the weight (-1,1)",
                   1105:  "of the Laplace transform of the annihilating ideal of the polynomial f in x,y.",
                   1106:  "See also Sminimal_v, Sannfs3.",
                   1107:  "Example: a=Sannfs2(\"x^3-y^2\");",
                   1108:  "         b=a[0]; sm1_pmat(b);",
                   1109:  "         b[1]*b[0]:",
                   1110:  "Example: a=Sannfs2(\"x*y*(x-y)*(x+y)\");",
                   1111:  "         b=a[0]; sm1_pmat(b);",
                   1112:  "         b[1]*b[0]:"
                   1113: ]]);
                   1114:
1.6       takayama 1115: /* Do not forget to turn on TOTAL_STRATEGY */
                   1116: def Sannfs2_laScala(f) {
                   1117:   local p,pp;
                   1118:   p = Sannfs(f,"x,y");
                   1119:   /*   Do not make laplace transform.
                   1120:     sm1(" p 0 get { [(x) (y) (Dx) (Dy)] laplace0 } map /p set ");
                   1121:     p = [p];
                   1122:   */
                   1123:   Sweyl("x,y",[["x",-1,"y",-1,"Dx",1,"Dy",1]]);
1.2       takayama 1124:   pp = Map(p[0],"Spoly");
                   1125:   return(Sminimal(pp));
                   1126: }
                   1127:
1.11      takayama 1128: def Sannfs2_laScala2(f) {
                   1129:   local p,pp;
                   1130:   p = Sannfs(f,"x,y");
                   1131:   sm1(" p 0 get { [(x) (y) (Dx) (Dy)] laplace0 } map /p set ");
                   1132:   p = [p];
                   1133:   Sweyl("x,y",[["x",1,"y",1,"Dx",1,"Dy",1,"h",1],
                   1134:                ["x",-1,"y",-1,"Dx",1,"Dy",1]]);
                   1135:   pp = Map(p[0],"Spoly");
                   1136:   return(Sminimal(pp));
                   1137: }
                   1138:
1.3       takayama 1139: def Sannfs3(f) {
                   1140:   local p,pp;
                   1141:   p = Sannfs(f,"x,y,z");
1.6       takayama 1142:   sm1(" p 0 get { [(x) (y) (z) (Dx) (Dy) (Dz)] laplace0 } map /p set ");
1.3       takayama 1143:   Sweyl("x,y,z",[["x",-1,"y",-1,"z",-1,"Dx",1,"Dy",1,"Dz",1]]);
1.6       takayama 1144:   pp = Map(p,"Spoly");
                   1145:   return(Sminimal_v(pp));
1.3       takayama 1146: }
                   1147:
1.10      takayama 1148: HelpAdd(["Sannfs3",
                   1149: ["Sannfs3(f) constructs the V-minimal free resolution for the weight (-1,1)",
                   1150:  "of the Laplace transform of the annihilating ideal of the polynomial f in x,y,z.",
                   1151:  "See also Sminimal_v, Sannfs2.",
                   1152:  "Example: a=Sannfs3(\"x^3-y^2*z^2\");",
                   1153:  "         b=a[0]; sm1_pmat(b);",
                   1154:  "         b[1]*b[0]: b[2]*b[1]:"]]);
                   1155:
1.2       takayama 1156: /*
                   1157:   The betti numbers of most examples are 2,1. (0-th and 1-th).
                   1158:   a=Sannfs2("x*y*(x+y-1)"); ==> The betti numbers are 3, 2.
                   1159:   a=Sannfs2("x^3-y^2-x");    : it causes an error. It should be fixed.
1.3       takayama 1160:   a=Sannfs2("x*y*(x-y)");    : it causes an error. It should be fixed.
1.2       takayama 1161:
                   1162: */
                   1163:
1.11      takayama 1164: def Sannfs3_laScala2(f) {
                   1165:   local p,pp;
                   1166:   p = Sannfs(f,"x,y,z");
                   1167:   sm1(" p 0 get { [(x) (y) (z) (Dx) (Dy) (Dz)] laplace0 } map /p set ");
                   1168:   Sweyl("x,y,z",[["x",1,"y",1,"z",1,"Dx",1,"Dy",1,"Dz",1,"h",1],
                   1169:                  ["x",-1,"y",-1,"z",-1,"Dx",1,"Dy",1,"Dz",1]]);
                   1170:   pp = Map(p,"Spoly");
                   1171:   return(Sminimal(pp));
                   1172: }
1.5       takayama 1173:
                   1174:
1.6       takayama 1175: /*  The below does not use LaScala-Stillman's algorithm. */
1.5       takayama 1176: def Sschreyer(g) {
                   1177:   local rf, tower, reductionTable, skel, redundantTable, bases,
                   1178:         strategy, maxOfStrategy, height, level, n, i,
                   1179:         freeRes,place, f, reducer,pos, redundant_seq,bettiTable,freeResV,ww,
                   1180:         redundantTable_ordinary, redundant_seq_ordinary,
1.6       takayama 1181:         reductionTable_tmp,c2,ii,nn, m,ii, jj, reducerBase;
1.5       takayama 1182:   /* extern WeightOfSweyl; */
                   1183:   ww = WeightOfSweyl;
                   1184:   Print("WeghtOfSweyl="); Println(WeightOfSweyl);
                   1185:   rf = SresolutionFrameWithTower(g);
                   1186:   redundant_seq = 1;   redundant_seq_ordinary = 1;
                   1187:   tower = rf[1];
                   1188:   reductionTable = SgenerateTable(tower);
                   1189:   skel = rf[2];
                   1190:   redundantTable = SnewArrayOfFormat(rf[1]);
                   1191:   redundantTable_ordinary = SnewArrayOfFormat(rf[1]);
                   1192:   reducer = SnewArrayOfFormat(rf[1]);
                   1193:   freeRes = SnewArrayOfFormat(rf[1]);
                   1194:   bettiTable = SsetBettiTable(rf[1],g);
                   1195:
                   1196:   height = Length(reductionTable);
                   1197:   for (level = 0; level < height; level++) {
                   1198:       n = Length(reductionTable[level]);
                   1199:       for (i=0; i<n; i++) {
                   1200:            Println([level,i]);
                   1201:            Print("Processing "); Print([level,i]);
                   1202:            if (level == 0) {
                   1203:              if (IsNull(redundantTable[level,i])) {
                   1204:                bases = freeRes[level];
                   1205:                /* Println(["At floor : GB=",i,bases,tower[0,i]]); */
                   1206:                pos = SwhereInGB(tower[0,i],rf[3,0]);
                   1207:                bases[i] = rf[3,0,pos];
                   1208:                /* redundantTable[level,i] = 0;
                   1209:                redundantTable_ordinary[level,i] = 0; */
                   1210:                freeRes[level] = bases;
                   1211:                /* Println(["GB=",i,bases,tower[0,i]]); */
                   1212:              }
                   1213:            }else{ /* level >= 1 */
                   1214:              if (IsNull(redundantTable[level,i])) {
                   1215:                bases = freeRes[level];
                   1216:                f = SpairAndReduction2(skel,level,i,freeRes,tower,
                   1217:                                       ww,redundantTable);
                   1218:                if (f[0] != Poly("0")) {
                   1219:                   place = f[3];
                   1220:                   /* (level-1, place) is the place for f[0],
                   1221:                      which is a newly obtained  GB. */
                   1222: #ifdef ORDINARY
                   1223:                   redundantTable[level-1,place] = redundant_seq;
                   1224:                   redundant_seq++;
                   1225: #else
                   1226:                   if (f[4] > f[5]) {
                   1227:                     /* Zero in the gr-module */
                   1228:                     Print("v-degree of [org,remainder] = ");
                   1229:                     Println([f[4],f[5]]);
                   1230:                     Print("[level,i] = "); Println([level,i]);
                   1231:                     redundantTable[level-1,place] = 0;
                   1232:                   }else{
                   1233:                     redundantTable[level-1,place] = redundant_seq;
                   1234:                     redundant_seq++;
                   1235:                   }
                   1236: #endif
                   1237:                   redundantTable_ordinary[level-1,place]
                   1238:                      =redundant_seq_ordinary;
                   1239:                   redundant_seq_ordinary++;
                   1240:                   bases[i] = SunitOfFormat(place,f[1])-f[1];  /* syzygy */
                   1241:                   /* redundantTable[level,i] = 0;
                   1242:                   redundantTable_ordinary[level,i] = 0; */
                   1243:                   /* i must be equal to f[2], I think. Double check. */
                   1244:
                   1245:                   /* Correction Of Constant */
1.9       takayama 1246:                   /* Correction of syzygy */
1.7       takayama 1247:                   c2 = f[6];  /* or -f[6]?  Double check. */
                   1248:                   Print("c2="); Println(c2);
1.5       takayama 1249:                   nn = Length(bases);
                   1250:                   for (ii=0; ii<nn;ii++) {
1.8       takayama 1251:                      if ((ii != i) && (! IsNull(bases[ii]))) {
1.7       takayama 1252:                        m = Length(bases[ii]);
                   1253:                        for (jj=0; jj<m; jj++) {
                   1254:                          if (jj != place) {
                   1255:                            bases[ii,jj] = bases[ii,jj]*c2;
                   1256:                          }
                   1257:                        }
1.5       takayama 1258:                      }
                   1259:                   }
                   1260:
1.7       takayama 1261:                   Print("Old freeRes[level] = "); sm1_pmat(freeRes[level]);
1.5       takayama 1262:                   freeRes[level] = bases;
1.7       takayama 1263:                   Print("New freeRes[level] = "); sm1_pmat(freeRes[level]);
1.6       takayama 1264:
                   1265:                  /* Update the freeRes[level-1] */
1.7       takayama 1266:                   Print("Old freeRes[level-1] = "); sm1_pmat(freeRes[level-1]);
1.6       takayama 1267:                   bases = freeRes[level-1];
                   1268:                   bases[place] = f[0];
                   1269:                   freeRes[level-1] = bases;
1.7       takayama 1270:                   Print("New freeRes[level-1] = "); sm1_pmat(freeRes[level-1]);
1.6       takayama 1271:
1.9       takayama 1272:                   reducer[level-1,place] = f[1]-SunitOfFormat(place,f[1]);
                   1273:                    /* This reducer is different from that of SlaScala(). */
                   1274:
                   1275:                   reducerBasis = reducer[level-1];
                   1276:                   nn = Length(reducerBasis);
                   1277:                   for (ii=0; ii<nn;ii++) {
                   1278:                      if ((ii != place) && (! IsNull(reducerBasis[ii]))) {
                   1279:                        m = Length(reducerBasis[ii]);
                   1280:                        for (jj=0; jj<m; jj++) {
                   1281:                          if (jj != place) {
                   1282:                            reducerBasis[ii,jj] = reducerBasis[ii,jj]*c2;
                   1283:                          }
                   1284:                        }
                   1285:                      }
                   1286:                   }
                   1287:                   reducer[level-1] = reducerBasis;
                   1288:
1.5       takayama 1289:                }else{
                   1290:                   /* redundantTable[level,i] = 0; */
                   1291:                   bases = freeRes[level];
                   1292:                   bases[i] = f[1];  /* Put the syzygy. */
                   1293:                   freeRes[level] = bases;
                   1294:                }
                   1295:              }  /* end of level >= 1 */
                   1296:           }
                   1297:     } /* i loop */
1.6       takayama 1298:
                   1299:     /* Triangulate reducer */
                   1300:     if (level >= 1) {
                   1301:       Println(" ");
                   1302:       Print("Triangulating reducer at level "); Println(level-1);
1.9       takayama 1303:       Println("freeRes[level]="); sm1_pmat(freeRes[level]);
1.6       takayama 1304:       reducerBase = reducer[level-1];
                   1305:       Print("reducerBase=");  Println(reducerBase);
1.9       takayama 1306:       Println("Compare freeRes[level] and reducerBase (put -1)");
1.6       takayama 1307:       m = Length(reducerBase);
                   1308:       for (ii=m-1; ii>=0; ii--) {
                   1309:         if (!IsNull(reducerBase[ii])) {
                   1310:            for (jj=ii-1; jj>=0; jj--) {
                   1311:              if (!IsNull(reducerBase[jj])) {
                   1312:               if (!IsZero(reducerBase[jj,ii])) {
1.9       takayama 1313:                 /* reducerBase[ii,ii] should be always constant. */
                   1314:                 reducerBase[jj] = reducerBase[ii,ii]*reducerBase[jj]-reducerBase[jj,ii]*reducerBase[ii];
1.6       takayama 1315:               }
                   1316:              }
                   1317:            }
                   1318:          }
                   1319:        }
                   1320:        Println("New reducer");
                   1321:        sm1_pmat(reducerBase);
                   1322:        reducer[level-1] = reducerBase;
                   1323:     }
                   1324:
1.5       takayama 1325:   } /* level loop */
                   1326:   n = Length(freeRes);
                   1327:   freeResV = SnewArrayOfFormat(freeRes);
                   1328:   for (i=0; i<n; i++) {
                   1329:     bases = freeRes[i];
                   1330:     bases = Sbases_to_vec(bases,bettiTable[i]);
                   1331:     freeResV[i] = bases;
                   1332:   }
1.6       takayama 1333:
                   1334:   /* Mark the non-redundant elements. */
                   1335:   for (i=0; i<n; i++) {
                   1336:     m = Length(redundantTable[i]);
                   1337:     for (jj=0; jj<m; jj++) {
                   1338:       if (IsNull(redundantTable[i,jj])) {
                   1339:         redundantTable[i,jj] = 0;
                   1340:       }
                   1341:     }
                   1342:   }
                   1343:
                   1344:
1.5       takayama 1345:   return([freeResV, redundantTable,reducer,bettiTable,redundantTable_ordinary]);
                   1346: }
                   1347:
                   1348: def SpairAndReduction2(skel,level,ii,freeRes,tower,ww,redundantTable) {
                   1349:   local i, j, myindex, p, bases, tower2, gi, gj,
                   1350:        si, sj, tmp, t_syz, pos, ans, ssp, syzHead,pos2,
                   1351:        vdeg,vdeg_reduced,n,c2;
1.6       takayama 1352:   Println("SpairAndReduction2 : -------------------------");
1.5       takayama 1353:
                   1354:   if (level < 1) Error("level should be >= 1 in SpairAndReduction.");
                   1355:   p = skel[level,ii];
                   1356:   myindex = p[0];
                   1357:   i = myindex[0]; j = myindex[1];
                   1358:   bases = freeRes[level-1];
                   1359:   Println(["p and bases ",p,bases]);
                   1360:   if (IsNull(bases[i]) || IsNull(bases[j])) {
                   1361:     Println([level,i,j,bases[i],bases[j]]);
                   1362:     Error("level, i, j : bases[i], bases[j]  must not be NULL.");
                   1363:   }
                   1364:
                   1365:   tower2 = StowerOf(tower,level-1);
                   1366:   SsetTower(tower2);
1.14      takayama 1367:   Println(["level=",level]);
                   1368:   Println(["tower2=",tower2]);
1.5       takayama 1369:   /** sm1(" show_ring ");   */
                   1370:
                   1371:   gi = Stoes_vec(bases[i]);
                   1372:   gj = Stoes_vec(bases[j]);
                   1373:
                   1374:   ssp = Sspolynomial(gi,gj);
                   1375:   si = ssp[0,0];
                   1376:   sj = ssp[0,1];
                   1377:   syzHead = si*es^i;
                   1378:   /* This will be the head term, I think. But, double check. */
                   1379:   Println([si*es^i,sj*es^j]);
                   1380:
                   1381:   Print("[gi, gj] = "); Println([gi,gj]);
                   1382:   sm1(" [(Homogenize)] system_variable message ");
                   1383:   Print("Reduce the element "); Println(si*gi+sj*gj);
                   1384:   Print("by  "); Println(bases);
                   1385:
                   1386:   tmp = Sreduction(si*gi+sj*gj, bases);
                   1387:
                   1388:   Print("result is "); Println(tmp);
1.6       takayama 1389:   if (!IsZero(tmp[0])) {
                   1390:     Print("Error: base = ");
                   1391:     Println(Map(bases,"Stoes_vec"));
                   1392:     Error("SpairAndReduction2: the remainder should be zero. See tmp. tower2. show_ring.");
                   1393:   }
1.5       takayama 1394:   t_syz = tmp[2];
                   1395:   si = si*tmp[1]+t_syz[i];
                   1396:   sj = sj*tmp[1]+t_syz[j];
                   1397:   t_syz[i] = si;
                   1398:   t_syz[j] = sj;
                   1399:
                   1400:   c2 = null;
                   1401:   /* tmp[0] must be zero */
                   1402:   n = Length(t_syz);
                   1403:   for (i=0; i<n; i++) {
1.6       takayama 1404:      if (IsConstant(t_syz[i])){
                   1405:       if (!IsZero(t_syz[i])) {
1.5       takayama 1406:        if (IsNull(redundantTable[level-1,i])) {
                   1407:          /* i must equal to pos2 below. */
                   1408:          c2 = -t_syz[i];
1.6       takayama 1409:          tmp[0] = c2*Stoes_vec(freeRes[level-1,i]);
1.5       takayama 1410:          t_syz[i] = 0;
1.6       takayama 1411:          /* tmp[0] = t_syz . g */
1.5       takayama 1412:          /* break; does not work. Use */
                   1413:          i = n;
                   1414:        }
1.6       takayama 1415:       }
1.5       takayama 1416:      }
                   1417:   }
                   1418:
                   1419:   /* This is essential part for V-minimal resolution. */
                   1420:   /* vdeg = SvDegree(si*gi+sj*gj,tower,level-1,ww); */
                   1421:   vdeg = SvDegree(si*gi,tower,level-1,ww);
                   1422:   vdeg_reduced = SvDegree(tmp[0],tower,level-1,ww);
                   1423:   Print("vdegree of the original = "); Println(vdeg);
                   1424:   Print("vdegree of the remainder = "); Println(vdeg_reduced);
                   1425:
1.11      takayama 1426:   if (!IsNull(vdeg_reduced)) {
                   1427:     if (vdeg_reduced < vdeg) {
                   1428:       Println("--- Special in V-minimal!");
                   1429:       Println(tmp[0]);
                   1430:       Println("syzygy="); sm1_pmat(t_syz);
                   1431:       Print("[vdeg, vdeg_reduced] = "); Println([vdeg,vdeg_reduced]);
                   1432:     }
                   1433:   }
                   1434:
1.14      takayama 1435:   SsetTower(StowerOf(tower,level));
                   1436:   pos = SwhereInTower(syzHead,tower[level]);
1.11      takayama 1437:
1.14      takayama 1438:   SsetTower(StowerOf(tower,level-1));
1.5       takayama 1439:   pos2 = SwhereInTower(tmp[0],tower[level-1]);
                   1440:   ans = [tmp[0],t_syz,pos,pos2,vdeg,vdeg_reduced,c2];
                   1441:   /* pos is the place to put syzygy at level. */
                   1442:   /* pos2 is the place to put a new GB at level-1. */
                   1443:   Println(ans);
1.14      takayama 1444:   Println("--- end of SpairAndReduction2  ");
1.5       takayama 1445:   return(ans);
                   1446: }
1.6       takayama 1447:
1.10      takayama 1448: HelpAdd(["Sminimal_v",
                   1449: ["It constructs the V-minimal free resolution from the Schreyer resolution",
                   1450:  "step by step.",
1.14      takayama 1451:  "This code still contains bugs. It sometimes outputs wrong answer.",
1.10      takayama 1452:  "Example:   Sweyl(\"x,y\",[[\"x\",-1,\"y\",-1,\"Dx\",1,\"Dy\",1]]);",
                   1453:  "          v=[[2*x*Dx + 3*y*Dy+6, 0],",
                   1454:  "             [3*x^2*Dy + 2*y*Dx, 0],",
                   1455:  "             [0,  x^2+y^2],",
                   1456:  "             [0,  x*y]];",
                   1457:  "         a=Sminimal_v(v);",
                   1458:  "         sm1_pmat(a[0]); b=a[0]; b[1]*b[0]:",
                   1459:  "Note:  a[0] is the V-minimal resolution. a[3] is the Schreyer resolution."]]);
                   1460:
1.14      takayama 1461: /* This code still contains bugs. It sometimes outputs wrong answer. */
                   1462: /* See test12() in minimal-test.k.  */
                   1463: /* There may be remaining 1, too */
1.6       takayama 1464: def Sminimal_v(g) {
                   1465:   local r, freeRes, redundantTable, reducer, maxLevel,
                   1466:         minRes, seq, maxSeq, level, betti, q, bases, dr,
1.10      takayama 1467:         betti_levelplus, newbases, i, j,qq,tminRes;
1.6       takayama 1468:   r = Sschreyer(g);
                   1469:   sm1_pmat(r);
                   1470:   Debug_Sminimal_v = r;
                   1471:   Println(" Return value of Schreyer(g) is set to Debug_Sminimal_v");
                   1472:   /* Should I turn off the tower?? */
                   1473:   freeRes = r[0];
                   1474:   redundantTable = r[1];
                   1475:   reducer = r[2];
                   1476:   minRes = SnewArrayOfFormat(freeRes);
                   1477:   seq = 0;
                   1478:   maxSeq = SgetMaxSeq(redundantTable);
                   1479:   maxLevel = Length(freeRes);
                   1480:   for (level = 0; level < maxLevel; level++) {
                   1481:     minRes[level] = freeRes[level];
                   1482:   }
                   1483:   for (level = 0; level < maxLevel; level++) {
                   1484:       betti = Length(freeRes[level]);
                   1485:       for (q = betti-1; q>=0; q--) {
                   1486:         if (redundantTable[level,q] > 0) {
                   1487:           Print("[seq,level,q]="); Println([seq,level,q]);
                   1488:           if (level < maxLevel-1) {
                   1489:             bases = freeRes[level+1];
                   1490:             dr = reducer[level,q];
1.9       takayama 1491:             /* dr[q] = -1;  We do not need this in our reducer format. */
                   1492:             /* dr[q] should be a non-zero constant. */
1.6       takayama 1493:             newbases = SnewArrayOfFormat(bases);
                   1494:             betti_levelplus = Length(bases);
                   1495:             /*
                   1496:                bases[i,j] ---> bases[i,j]+bases[i,q]*dr[j]
                   1497:             */
                   1498:             for (i=0; i<betti_levelplus; i++) {
1.9       takayama 1499:               newbases[i] = dr[q]*bases[i] - bases[i,q]*dr;
1.6       takayama 1500:             }
                   1501:             Println(["level, q =", level,q]);
                   1502:             Println("bases="); sm1_pmat(bases);
                   1503:             Println("dr="); sm1_pmat(dr);
                   1504:             Println("newbases="); sm1_pmat(newbases);
                   1505:             minRes[level+1] = newbases;
                   1506:             freeRes = minRes;
                   1507: #ifdef DEBUG
1.9       takayama 1508:             for (qq=q; qq<betti; qq++) {
1.6       takayama 1509:                 for (i=0; i<betti_levelplus; i++) {
1.9       takayama 1510:                   if ((!IsZero(newbases[i,qq])) && (redundantTable[level,qq] >0)) {
1.6       takayama 1511:                     Println(["[i,qq]=",[i,qq]," is not zero in newbases."]);
                   1512:                     Print("redundantTable ="); sm1_pmat(redundantTable[level]);
                   1513:                     Error("Stop in Sminimal for debugging.");
                   1514:                   }
                   1515:                 }
                   1516:             }
                   1517: #endif
                   1518:           }
                   1519:         }
                   1520:       }
                   1521:    }
1.10      takayama 1522:    tminRes = Stetris(minRes,redundantTable);
                   1523:    return([SpruneZeroRow(tminRes), tminRes,
1.6       takayama 1524:           [ minRes, redundantTable, reducer,r[3],r[4]],r[0]]);
                   1525:   /* r[4] is the redundantTable_ordinary */
                   1526:   /* r[0] is the freeResolution */
                   1527: }
                   1528:
                   1529: /* Sannfs2("x*y*(x-y)*(x+y)"); is a test problem */
1.10      takayama 1530: /* x y (x+y-1)(x-2),  x^3-y^2, x^3 - y^2 z^2,
                   1531:    x y z (x+y+z-1) seems to be interesting, because the first syzygy
                   1532:   contains 1.
                   1533: */
                   1534:
                   1535: def CopyArray(m) {
                   1536:   local ans,i,n;
                   1537:   if (IsArray(m)) {
                   1538:      n = Length(m);
                   1539:      ans = NewArray(n);
                   1540:      for (i=0; i<n; i++) {
                   1541:        ans[i] = CopyArray(m[i]);
                   1542:      }
                   1543:      return(ans);
                   1544:   }else{
                   1545:      return(m);
                   1546:   }
                   1547: }
                   1548: HelpAdd(["CopyArray",
                   1549: ["It duplicates the argument array recursively.",
                   1550:  "Example: m=[1,[2,3]];",
                   1551:  "         a=CopyArray(m); a[1] = \"Hello\";",
                   1552:  "         Println(m); Println(a);"]]);
                   1553:
                   1554: def IsZeroVector(m) {
                   1555:   local n,i;
                   1556:   n = Length(m);
                   1557:   for (i=0; i<n; i++) {
                   1558:     if (!IsZero(m[i])) {
                   1559:       return(false);
                   1560:     }
                   1561:   }
                   1562:   return(true);
                   1563: }
                   1564:
                   1565: def SpruneZeroRow(res) {
                   1566:   local minRes, n,i,j,m, base,base2,newbase,newbase2, newMinRes;
                   1567:
                   1568:   minRes = CopyArray(res);
                   1569:   n = Length(minRes);
                   1570:   for (i=0; i<n; i++) {
                   1571:     base = minRes[i];
                   1572:     m = Length(base);
                   1573:     if (i != n-1) {
                   1574:       base2 = minRes[i+1];
                   1575:       base2 = Transpose(base2);
                   1576:     }
                   1577:     newbase = [ ];
                   1578:     newbase2 = [ ];
                   1579:     for (j=0; j<m; j++) {
                   1580:       if (!IsZeroVector(base[j])) {
                   1581:         newbase = Append(newbase,base[j]);
                   1582:         if (i != n-1) {
                   1583:           newbase2 = Append(newbase2,base2[j]);
                   1584:         }
                   1585:       }
                   1586:     }
                   1587:     minRes[i] = newbase;
                   1588:     if (i != n-1) {
                   1589:       if (newbase2 == [ ]) {
                   1590:         minRes[i+1] = [ ];
                   1591:       }else{
                   1592:         minRes[i+1] = Transpose(newbase2);
                   1593:       }
                   1594:     }
                   1595:   }
                   1596:
                   1597:   newMinRes = [ ];
                   1598:   n = Length(minRes);
                   1599:   i = 0;
                   1600:   while (i < n ) {
                   1601:     base = minRes[i];
                   1602:     if (base == [ ]) {
                   1603:       i = n; /* break; */
                   1604:     }else{
                   1605:       newMinRes = Append(newMinRes,base);
                   1606:     }
                   1607:     i++;
                   1608:   }
                   1609:   return(newMinRes);
                   1610: }
                   1611:
                   1612: def testAnnfs2(f) {
                   1613:   local a,i,n;
                   1614:   a = Sannfs2(f);
                   1615:   b=a[0];
                   1616:   n = Length(b);
                   1617:   Println("------ V-minimal free resolution -----");
                   1618:   sm1_pmat(b);
                   1619:   Println("----- Is it complex?  ---------------");
                   1620:   for (i=0; i<n-1; i++) {
                   1621:     Println(b[i+1]*b[i]);
                   1622:   }
                   1623:   return(a);
                   1624: }
                   1625: def testAnnfs3(f) {
                   1626:   local a,i,n;
                   1627:   a = Sannfs3(f);
                   1628:   b=a[0];
                   1629:   n = Length(b);
                   1630:   Println("------ V-minimal free resolution -----");
                   1631:   sm1_pmat(b);
                   1632:   Println("----- Is it complex?  ---------------");
                   1633:   for (i=0; i<n-1; i++) {
                   1634:     Println(b[i+1]*b[i]);
                   1635:   }
1.11      takayama 1636:   return(a);
                   1637: }
                   1638:
                   1639: def ToString_array(p) {
                   1640:   local ans;
                   1641:   if (IsArray(p)) {
                   1642:     ans = Map(p,"ToString_array");
                   1643:   }else{
                   1644:     ans = ToString(p);
                   1645:   }
                   1646:   return(ans);
                   1647: }
                   1648:
                   1649: /* sm1_res_div([[x],[y]],[[x^2],[x*y],[y^2]],[x,y]): */
                   1650:
                   1651: def sm1_res_div(I,J,V) {
                   1652:   I = ToString_array(I);
                   1653:   J = ToString_array(J);
                   1654:   V = ToString_array(V);
                   1655:   sm1(" [[ I J]  V ] res*div /FunctionValue set ");
                   1656: }
                   1657:
                   1658: /* It has not yet been working */
                   1659: def sm1_res_kernel_image(m,n,v) {
                   1660:   m = ToString_array(m);
                   1661:   n = ToString_array(n);
                   1662:   v = ToString_array(v);
                   1663:   sm1(" [m n v] res-kernel-image /FunctionValue set ");
                   1664: }
                   1665: def Skernel(m,v) {
                   1666:   m = ToString_array(m);
                   1667:   v = ToString_array(v);
                   1668:   sm1(" [ m v ] syz /FunctionValue set ");
                   1669: }
                   1670:
                   1671: def test3() {
                   1672:   local a1,a2,b1,b2;
                   1673:   a1 = Sannfs3("x^3-y^2*z^2");
                   1674:   a1 = a1[0];
                   1675:   a2 = Sannfs3_laScala2("x^3-y^2*z^2");
                   1676:   a2 = a2[0];
                   1677:   b1 = a1[1];
                   1678:   b2 = a2[1];
                   1679:   sm1_pmat(b2);
                   1680:   Println("  OVER ");
                   1681:   sm1_pmat(b1);
                   1682:   return([sm1_res_div(b2,b1,["x","y","z"]),b2,b1,a2,a1]);
                   1683: }
                   1684:
                   1685: def test4() {
                   1686:   local a,b;
                   1687:   a = Sannfs3_laScala2("x^3-y^2*z^2");
                   1688:   b = a[0];
                   1689:   sm1_pmat( sm1_res_kernel_image(b[0],b[1],[x,y,z]));
                   1690:   sm1_pmat( sm1_res_kernel_image(b[1],b[2],[x,y,z]));
                   1691:   return(a);
                   1692: }
                   1693:
                   1694: def sm1_gb(f,v) {
                   1695:   f =ToString_array(f);
                   1696:   v = ToString_array(v);
                   1697:   sm1(" [f v] gb /FunctionValue set ");
1.13      takayama 1698: }
                   1699:
1.11      takayama 1700:
1.12      takayama 1701: def SisComplex(a) {
                   1702:   local n,i,j,k,b,p,q;
                   1703:   n = Length(a);
                   1704:   for (i=0; i<n-1; i++) {
                   1705:     if (Length(a[i+1]) != 0) {
                   1706:       b = a[i+1]*a[i];
                   1707:       p = Length(b); q = Length(b[0]);
                   1708:       for (j=0; j<p; j++) {
                   1709:         for (k=0; k<q; k++) {
                   1710:           if (!IsZero(b[j,k])) {
                   1711:              Print("Is is not complex at ");
                   1712:              Println([i,j,k]);
                   1713:              return(false);
                   1714:           }
                   1715:         }
                   1716:       }
                   1717:     }
                   1718:   }
                   1719:   return(true);
1.14      takayama 1720: }
                   1721:
                   1722: def IsExact_h(c,v) {
                   1723:   local a;
                   1724:   v = ToString_array(v);
                   1725:   a = [c,v];
                   1726:   sm1(a," isExact_h /FunctionValue set ");
                   1727: }
                   1728: HelpAdd(["IsExact_h",
                   1729: ["IsExact_h(complex,var): bool",
                   1730:  "It checks the given complex is exact or not in D<h> (homogenized Weyl algebra)",
                   1731:  "cf. ReParse"
                   1732: ]]);
                   1733:
                   1734: def ReParse(a) {
                   1735:   local c;
                   1736:   if (IsArray(a)) {
                   1737:     c = Map(a,"ReParse");
                   1738:   }else{
                   1739:     sm1(a," toString . /c set");
                   1740:   }
                   1741:   return(c);
                   1742: }
                   1743: HelpAdd(["ReParse",
                   1744: ["Reparse(obj): obj",
                   1745:  "It parses the given object in the current ring.",
                   1746:  "Outputs from SlaScala, Sschreyer may cause a trouble in other functions,",
                   1747:  "because it uses the Schreyer order.",
                   1748:  "In this case, ReParse the outputs from these functions.",
                   1749:  "cf. IsExaxt_h"
                   1750: ]]);

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