This file is indexed.

/usr/share/singular/LIB/mregular.lib is in singular-data 1:4.1.0-p3+ds-2build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
//////////////////////////////////////////////////////////////////////////////
version="version mregular.lib 4.0.0.0 Jun_2013 "; // $Id: 65c9ff28baa31013620d93b4f3033bcb6d5632ad $
category="Commutative Algebra";
info="
LIBRARY: mregular.lib   Castelnuovo-Mumford regularity of homogeneous ideals
AUTHORS: I.Bermejo,     ibermejo@ull.es
@*       Ph.Gimenez,    pgimenez@agt.uva.es
@*       G.-M.Greuel,   greuel@mathematik.uni-kl.de

OVERVIEW:
 A library for computing the Castelnuovo-Mumford regularity of a homogeneous
 ideal that DOES NOT require the computation of a minimal graded free
 resolution of the ideal.
 It also determines depth(basering/ideal) and satiety(ideal).
 The procedures are based on 3 papers by Isabel Bermejo and Philippe Gimenez:
 'On Castelnuovo-Mumford regularity of projective curves' Proc.Amer.Math.Soc.
 128(5) (2000), 'Computing the Castelnuovo-Mumford regularity of some
 subschemes of Pn using quotients of monomial ideals', Proceedings of
 MEGA-2000, J. Pure Appl. Algebra 164 (2001), and 'Saturation and
 Castelnuovo-Mumford regularity', Preprint (2004).

PROCEDURES:
 regIdeal(id,[,e]);    regularity of homogeneous ideal id
 depthIdeal(id,[,e]);  depth of S/id with S=basering, id homogeneous ideal
 satiety(id,[,e]);     saturation index of homogeneous ideal id
 regMonCurve(li);      regularity of projective monomial curve defined by li
 NoetherPosition(id);  Noether normalization of ideal id
 is_NP(id);            checks whether variables are in Noether position
 is_nested(id);        checks whether monomial ideal id is of nested type
";

LIB "general.lib";
LIB "algebra.lib";
LIB "sing.lib";
LIB "poly.lib";
//////////////////////////////////////////////////////////////////////////////
//
proc regIdeal (ideal i, list #)
"
USAGE:   regIdeal (i[,e]); i ideal, e integer
RETURN:  an integer, the Castelnuovo-Mumford regularity of i.
         (returns -1 if i is not homogeneous)
ASSUME:  i is a homogeneous ideal of the basering S=K[x(0)..x(n)].
         e=0:  (default)
               If K is an infinite field, makes random changes of coordinates.
               If K is a finite field, works over a transcendental extension.
         e=1:  Makes random changes of coordinates even when K is finite.
               It works if it terminates, but may result in an infinite
               loop. After 30 loops, a warning message is displayed and
               -1 is returned.
NOTE:    If printlevel > 0 (default = 0), additional info is displayed:
         dim(S/i), depth(S/i) and end(H^(depth(S/i))(S/i)) are computed,
         and an upper bound for the a-invariant of S/i is given.
         The algorithm also determines whether the regularity is attained
         or not at the last step of a minimal graded free resolution of i,
         and if the answer is positive, the regularity of the Hilbert
         function of S/i is given.
EXAMPLE: example regIdeal; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int e,ii,jj,H,h,d,time,lastv,sat,firstind;
   int lastind,ch,nesttest,NPtest,nl,N,acc;
   intmat ran;
   def r0 = basering;
   int n = nvars(r0)-1;
   if ( size(#) > 0 )
   {
      e = #[1];
   }
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal i,sbi,I,J,K,chcoord,m;
   poly P;
   map phi;
   i = fetch(r0,i);
   time=rtimer;
   sbi=std(i);
   ch=char(r1);
//----- Check ideal homogeneous
   if ( homog(sbi) == 0 )
   {
       "// WARNING from proc regIdeal from lib mregular.lib:
// The ideal is not homogeneous!";
       return (-1);
   }
   I=simplify(lead(sbi),1);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
   {
     dbprint(printlevel-voice+2,
             "// The ideal i is (1)!
// Its Castelnuovo-Mumford regularity is:");
     return (0);
   }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
   {
     dbprint(printlevel-voice+2,
             "// The ideal i is (0)!
// Its Castelnuovo-Mumford regularity is:");
     return (0);
   }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
   {
     H=maxdeg1(minbase(quotient(I,maxideal(1))))+1;
     time=rtimer-time;
     // Additional information:
     dbprint(printlevel-voice+2,
             "// Dimension of S/i : 0");
     dbprint(printlevel-voice+2,
             "// Time for computing regularity: " + string(time) + " sec.");
     dbprint(printlevel-voice+2,
"// The Castelnuovo-Mumford regularity of i coincides with its satiety, and
 // with the regularity of the Hilbert function of S/i. Its value is:");
     return (H);
   }
//----- Determine the situation: NT, or NP, or nothing.
//----- Choose the method depending on the situation, on the
//----- characteristic of the ground field, and on the option argument
//----- in order to get the mon. ideal of nested type associated to i
   if ( e == 1 )
   { ch=0; }
   NPtest=is_NP(I);
   if ( NPtest == 1 )
   {
     nesttest=is_nested(I);
   }
   if ( ch != 0 )
   {
     if ( NPtest == 0 )
     {
       N=d*n-d*(d-1)/2;
       s = "ring rtr = (ch,t(1..N)),x(0..n),dp;";
       execute(s);
       ideal chcoord,m,i,I;
       poly P;
       map phi;
       i=imap(r1,i);
       chcoord=select1(maxideal(1),1..(n-d+1));
       acc=0;
       for ( ii = 1; ii<=d; ii++ )
       {
         matrix trex[1][n-d+ii+1]=t((1+acc)..(n-d+ii+acc)),1;
         m=select1(maxideal(1),1..(n-d+1+ii));
         for ( jj = 1; jj<=n-d+ii+1; jj++ )
         {
           P=P+trex[1,jj]*m[jj];
         }
         chcoord[n-d+1+ii]=P;
         P=0;
         acc=acc+n-d+ii;
         kill trex;
       }
       phi=rtr,chcoord;
       I=simplify(lead(std(phi(i))),1);
       setring r1;
       I=imap(rtr,I);
       attrib(I,"isSB",1);
     }
     else
     {
       if ( nesttest == 0 )
       {
         N=d*(d-1)/2;
         s = "ring rtr = (ch,t(1..N)),x(0..n),dp;";
         execute(s);
         ideal chcoord,m,i,I;
         poly P;
         map phi;
         i=imap(r1,i);
         chcoord=select1(maxideal(1),1..(n-d+2));
         acc=0;
         for ( ii = 1; ii<=d-1; ii++ )
         {
           matrix trex[1][ii+1]=t((1+acc)..(ii+acc)),1;
           m=select1(maxideal(1),(n-d+2)..(n-d+2+ii));
           for ( jj = 1; jj<=ii+1; jj++ )
           {
             P=P+trex[1,jj]*m[jj];
           }
           chcoord[n-d+2+ii]=P;
           P=0;
           acc=acc+ii;
           kill trex;
         }
         phi=rtr,chcoord;
         I=simplify(lead(std(phi(i))),1);
         setring r1;
         I=imap(rtr,I);
         attrib(I,"isSB",1);
       }
     }
   }
   else
   {
     if ( NPtest == 0 )
     {
       while ( nl < 30 )
       {
         chcoord=select1(maxideal(1),1..(n-d+1));
         nl=nl+1;
         for ( ii = 1; ii<=d; ii++ )
         {
           ran=random(100,1,n-d+ii);
           ran=intmat(ran,1,n-d+ii+1);
           ran[1,n-d+ii+1]=1;
           m=select1(maxideal(1),1..(n-d+1+ii));
           for ( jj = 1; jj<=n-d+ii+1; jj++ )
           {
             P=P+ran[1,jj]*m[jj];
           }
           chcoord[n-d+1+ii]=P;
           P=0;
         }
         phi=r1,chcoord;
         dbprint(printlevel-voice+2,"// (1 random change of coord.)");
         I=simplify(lead(std(phi(i))),1);
         attrib(I,"isSB",1);
         NPtest=is_NP(I);
         if ( NPtest == 1 )
         {
           break;
         }
       }
       if ( NPtest == 0 )
       {
       "// WARNING from proc regIdeal from lib mregular.lib:
// The procedure has entered in 30 loops and could not put the variables
// in Noether position: in your example the method using random changes
// of coordinates may enter an infinite loop when the field is finite.
// Try removing this optional argument.";
         return (-1);
       }
       i=phi(i);
       nesttest=is_nested(I);
     }
     if ( nesttest == 0 )
     {
       while ( nl < 30 )
       {
         chcoord=select1(maxideal(1),1..(n-d+2));
         nl=nl+1;
         for ( ii = 1; ii<=d-1; ii++ )
         {
           ran=random(100,1,ii);
           ran=intmat(ran,1,ii+1);
           ran[1,ii+1]=1;
           m=select1(maxideal(1),(n-d+2)..(n-d+2+ii));
           for ( jj = 1; jj<=ii+1; jj++ )
           {
             P=P+ran[1,jj]*m[jj];
           }
           chcoord[n-d+2+ii]=P;
           P=0;
         }
         phi=r1,chcoord;
         dbprint(printlevel-voice+2,"// (1 random change of coord.)");
         I=simplify(lead(std(phi(i))),1);
         attrib(I,"isSB",1);
         nesttest=is_nested(I);
         if ( nesttest == 1 )
         {
           break;
         }
       }
       if ( nesttest == 0 )
       {
       "// WARNING from proc regIdeal from lib mregular.lib:
// The procedure has entered in 30 loops and could not find a monomial
// ideal of nested type with the same regularity as your ideal: in your
// example the method using random changes of coordinates may enter an
// infinite loop when the field is finite.
// Try removing this optional argument.";
         return (-1);
       }
     }
   }
//
// At this stage, we have obtained a monomial ideal I of nested type
// such that reg(i)=reg(I). We now compute reg(I).
//
//----- When S/i is Cohen-Macaulay:
   for ( ii = n-d+2; ii <= n+1; ii++ )
   {
     K=K+select(I,ii);
   }
   if ( size(K) == 0 )
   {
     s="ring nr = ",charstr(r0),",x(0..n-d),dp;";
     execute(s);
     ideal I;
     I = imap(r1,I);
     H=maxdeg1(minbase(quotient(I,maxideal(1))))+1;
     time=rtimer-time;
     // Additional information:
     dbprint(printlevel-voice+2,
               "// S/i is Cohen-Macaulay");
     dbprint(printlevel-voice+2,
               "// Dimension of S/i ( = depth(S/i) ): "+string(d));
     dbprint(printlevel-voice+2,
               "// Regularity attained at the last step of m.g.f.r. of i: YES");
     dbprint(printlevel-voice+2,
               "// Regularity of the Hilbert function of S/i: " + string(H-d));
     dbprint(printlevel-voice+2,
               "// Time for computing regularity: " + string(time) + " sec.");
     dbprint(printlevel-voice+2,
               "// The Castelnuovo-Mumford regularity of i is:");
     return(H);
   }
//----- When d=1:
   if ( d == 1 )
   {
     H=maxdeg1(simplify(reduce(quotient(I,maxideal(1)),I),2))+1;
     sat=H;
     J=subst(I,x(n),1);
     s = "ring nr = ",charstr(r0),",x(0..n-1),dp;";
     execute(s);
     ideal J=imap(r1,J);
     attrib(J,"isSB",1);
     h=maxdeg1(minbase(quotient(J,maxideal(1))))+1;
     time=rtimer-time;
     if ( h > H )
     {
       H=h;
     }
     // Additional information:
     dbprint(printlevel-voice+2,
               "// Dimension of S/i: 1");
     dbprint(printlevel-voice+2,
               "// Depth of S/i: 0");
     dbprint(printlevel-voice+2,
               "// Satiety of i: "+string(sat));
     dbprint(printlevel-voice+2,
               "// Upper bound for the a-invariant of S/i: end(H^1(S/i)) <= "+
               string(h-2));
     if ( H == sat )
     {
       dbprint(printlevel-voice+2,
                   "// Regularity attained at the last step of m.g.f.r. of i: YES");
       dbprint(printlevel-voice+2,
                   "// Regularity of the Hilbert function of S/i: "+string(H));
     }
     else
     {
       dbprint(printlevel-voice+2,
                   "// Regularity attained at the last step of m.g.f.r. of i: NO");
     }
     dbprint(printlevel-voice+2,
                   "// Time for computing regularity: "+ string(time) + " sec.");
     dbprint(printlevel-voice+2,
                   "// The Castelnuovo-Mumford regularity of i is:");
     return(H);
   }
//----- Now d>1 and S/i is not Cohen-Macaulay:
//
//----- First, determine the last variable really occuring
   lastv=n-d;
   h=n;
   while ( lastv == n-d and h > n-d )
   {
     K=select(I,h+1);
     if ( size(K) == 0 )
     {
       h=h-1;
     }
     else
     {
       lastv=h;
     }
   }
//----- and compute Castelnuovo-Mumford regularity:
   s = "ring nr = ",charstr(r0),",x(0..lastv),dp;";
   execute(s);
   ideal I,K,KK,LL;
   I=imap(r1,I);
   attrib(I,"isSB",1);
   K=simplify(reduce(quotient(I,maxideal(1)),I),2);
   H=maxdeg1(K)+1;
   firstind=H;
   KK=minbase(subst(I,x(lastv),1));
   for ( ii = n-lastv; ii<=d-2; ii++ )
   {
     LL=minbase(subst(I,x(n-ii-1),1));
     attrib(LL,"isSB",1);
     s = "ring mr = ",charstr(r0),",x(0..n-ii-1),dp;";
     execute(s);
     ideal K,KK;
     KK=imap(nr,KK);
     attrib(KK,"isSB",1);
     K=simplify(reduce(quotient(KK,maxideal(1)),KK),2);
     h=maxdeg1(K)+1;
     if ( h > H )
     {
       H=h;
     }
     setring nr;
     kill mr;
     KK=LL;
   }
   // We must determine one more sat. index:
   s = "ring mr = ",charstr(r0),",x(0..n-d),dp;";
   execute(s);
   ideal KK,K;
   KK=imap(nr,KK);
   attrib(KK,"isSB",1);
   K=simplify(reduce(quotient(KK,maxideal(1)),KK),2);
   h=maxdeg1(K)+1;
   lastind=h;
   if ( h > H )
   {
     H=h;
   }
   setring nr;
   kill mr;
   time=rtimer-time;
   // Additional information:
   dbprint(printlevel-voice+2,
               "// Dimension of S/i: "+string(d));
   dbprint(printlevel-voice+2,
               "// Depth of S/i: "+string(n-lastv));
   dbprint(printlevel-voice+2,
               "// end(H^"+string(n-lastv)+"(S/i)) = "
               +string(firstind-n+lastv-1));
   dbprint(printlevel-voice+2,
               "// Upper bound for the a-invariant of S/i: end(H^"
               +string(d)+"(S/i)) <= "+string(lastind-d-1));
   if ( H == firstind )
   {
     dbprint(printlevel-voice+2,
                   "// Regularity attained at the last step of m.g.f.r. of i: YES");
     dbprint(printlevel-voice+2,
                   "// Regularity of the Hilbert function of S/i: "
                   +string(H-n+lastv));
   }
   else
   {
     dbprint(printlevel-voice+2,
                   "// Regularity attained at the last step of m.g.f.r. of i: NO");
   }
   dbprint(printlevel-voice+2,
               "// Time for computing regularity: "+ string(time) + " sec.");
   dbprint(printlevel-voice+2,
               "// The Castelnuovo-Mumford regularity of i is:");
   return(H);
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,(x,y,z,t,w),dp;
   ideal i=y2t,x2y-x2z+yt2,x2y2,xyztw,x3z2,y5+xz3w-x2zw2,x7-yt2w4;
   regIdeal(i);
   regIdeal(lead(std(i)));
// Additional information is displayed if you change printlevel (=1);
}
////////////////////////////////////////////////////////////////////////////////
/*
Out-commented examples:
//
   ring s=0,x(0..5),dp;
   ideal i=x(2)^2-x(4)*x(5),x(1)*x(2)-x(0)*x(5),x(0)*x(2)-x(1)*x(4),
           x(1)^2-x(3)*x(5),x(0)*x(1)-x(2)*x(3),x(0)^2-x(3)*x(4);
   regIdeal(i);
   // Our procedure works when a min. graded free resol. can
   // not be computed. In this easy example, regularity can also
   // be obtained using a m.g.f.r.:
   nrows(betti(mres(i,0)));
   ring r1=0,(x,y,z,t),dp;
// Ex.2.5 in [Bermejo-Gimenez], Proc.Amer.Math.Soc. 128(5):
   ideal i  = x17y14-y31, x20y13, x60-y36z24-x20z20t20;
   regIdeal(i);
// Ex.2.9 in [Bermejo-Gimenez], Proc.Amer.Math.Soc. 128(5):
   int k=43;
   ideal j=x17y14-y31,x20y13,x60-y36z24-x20z20t20,y41*z^k-y40*z^(k+1);
   regIdeal(j);
   k=14;
   j=x17y14-y31,x20y13,x60-y36z24-x20z20t20,y41*z^k-y40*z^(k+1);
   regIdeal(j);
   k=22;
   j=x17y14-y31,x20y13,x60-y36z24-x20z20t20,y41*z^k-y40*z^(k+1);
   regIdeal(j);
   k=315;
   j=x17y14-y31,x20y13,x60-y36z24-x20z20t20,y41*z^k-y40*z^(k+1);
   regIdeal(j);
// Example in Rk.2.10 in [Bermejo-Gimenez], ProcAMS 128(5):
   ideal h=x2-3xy+5xt,xy-3y2+5yt,xz-3yz,2xt-yt,y2-yz-2yt;
   regIdeal(h);
// The initial ideal is not saturated
   regIdeal(lead(std(h)));
// More examples:
   i=y4-t3z, x3t-y2z2, x3y2-t2z3, x6-tz5;
   regIdeal(i);
//
   regIdeal(maxideal(4));
//
   ring r2=0,(x,y,z,t,w),dp;
   ideal i = xy-zw,x3-yw2,x2z-y2w,y3-xz2,-y2z3+xw4+tw4+w5,-yz4+x2w3+xtw3+xw4,
            -z5+x2tw2+x2w3+yw4;
   regIdeal(i);
//
   ring r3=0,(x,y,z,t,w,u),dp;
   ideal i=imap(r2,i);
   regIdeal(i);
// Next example is the defining ideal of the 2nd. Veronesean of P3, a variety
// in P8 which is arithmetically Cohen-Macaulay:
   ring r4=0,(a,b,c,d,x(0..9)),dp;
   ideal i= x(0)-ab,x(1)-ac,x(2)-ad,x(3)-bc,x(4)-bd,x(5)-cd,
            x(6)-a2,x(7)-b2,x(8)-c2,x(9)-d2;
   ideal ei=eliminate(i,abcd);
   ring r5=0,x(0..9),dp;
   ideal i=imap(r4,ei);
   regIdeal(i);
// Here is an example where the computation of a m.g.f.r. of I costs:
   ring r8=0,(x,y,z,t,u,a,b),dp;
   ideal i=u-b40,t-a40,x-a23b17,y-a22b18+ab39,z-a25b15;
   ideal ei=eliminate(i,ab); // It takes a few seconds to compute the ideal
   ring r9=0,(x,y,z,t,u),dp;
   ideal i=imap(r8,ei);
   regIdeal(i);   // This is very fast.
// Now you can use mres(i,0) to compute a m.g.f.r. of the ideal!
//
// The computation of the m.g.f.r. of the following example did not succeed
// using the command mres:
   ring r10=0,(x(0..8),s,t),dp;
   ideal i=x(0)-st24,x(1)-s2t23,x(2)-s3t22,x(3)-s9t16,x(4)-s11t14,x(5)-s18t7,
           x(6)-s24t,x(7)-t25,x(8)-s25;
   ideal ei=eliminate(i,st);
   ring r11=0,x(0..8),dp;
   ideal i=imap(r10,ei);
   regIdeal(i);
// More examples where not even sres works:
// Be careful: elimination takes some time here, but it succeeds!
   ring r12=0,(s,t,u,x(0..14)),dp;
   ideal i=x(0)-st6u8,x(1)-s5t3u7,x(2)-t11u4,x(3)-s9t4u2,x(4)-s2t7u6,x(5)-s7t7u,
           x(6)-s10t5,x(7)-s4t6u5,x(8)-s13tu,x(9)-s14u,x(10)-st2u12,x(11)-s3t9u3,
           x(12)-s15,x(13)-t15,x(14)-u15;
   ideal ei=eliminate(i,stu);
   size(ei);
   ring r13=0,x(0..14),dp;
   ideal i=imap(r12,ei);
   size(i);
   regIdeal(i);
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc depthIdeal (ideal i, list #)
"
USAGE:   depthIdeal (i[,e]); i ideal, e integer
RETURN:  an integer, the depth of S/i where S=K[x(0)..x(n)] is the basering.
         (returns -1 if i is not homogeneous or if i=(1))
ASSUME:  i is a proper homogeneous ideal.
         e=0:  (default)
               If K is an infinite field, makes random changes of coordinates.
               If K is a finite field, works over a transcendental extension.
         e=1:  Makes random changes of coordinates even when K is finite.
               It works if it terminates, but may result in an infinite
               loop. After 30 loops, a warning message is displayed and
               -1 is returned.
NOTE:    If printlevel > 0 (default = 0), dim(S/i) is also displayed.
EXAMPLE: example depthIdeal; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int e,ii,jj,h,d,time,lastv,ch,nesttest,NPtest,nl,N,acc;
   intmat ran;
   def r0 = basering;
   int n = nvars(r0)-1;
   if ( size(#) > 0 )
   {
      e = #[1];
   }
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal i,sbi,I,J,K,chcoord,m;
   poly P;
   map phi;
   i = fetch(r0,i);
   time=rtimer;
   sbi=std(i);
   ch=char(r1);
//----- Check ideal homogeneous
   if ( homog(sbi) == 0 )
   {
       "// WARNING from proc depthIdeal from lib mregular.lib:
// The ideal is not homogeneous!";
       return (-1);
   }
   I=simplify(lead(sbi),1);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
     {
       "// WARNING from proc depthIdeal from lib mregular.lib:
// The ideal i is (1)!";
       return (-1);
     }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
     {
       dbprint(printlevel-voice+2,
               "// The ideal i is (0)!
// The depth of S/i is:");
       return (d);
     }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
     {
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i : 0 (S/i is Cohen-Macaulay)");
       dbprint(printlevel-voice+2,
               "// Time for computing the depth: " + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The depth of S/i is:");
       return (0);
     }
//----- Determine the situation: NT, or NP, or nothing.
//----- Choose the method depending on the situation, on the
//----- characteristic of the ground field, and on the option argument
//----- in order to get the mon. ideal of nested type associated to i
   if ( e == 1 )
     {
       ch=0;
     }
   NPtest=is_NP(I);
   if ( NPtest == 1 )
     {
       nesttest=is_nested(I);
     }
   if ( ch != 0 )
     {
       if ( NPtest == 0 )
         {
           N=d*n-d*(d-1)/2;
           s = "ring rtr = (ch,t(1..N)),x(0..n),dp;";
           execute(s);
           ideal chcoord,m,i,I;
           poly P;
           map phi;
           i=imap(r1,i);
           chcoord=select1(maxideal(1),1..(n-d+1));
           acc=0;
           for ( ii = 1; ii<=d; ii++ )
             {
               matrix trex[1][n-d+ii+1]=t((1+acc)..(n-d+ii+acc)),1;
               m=select1(maxideal(1),1..(n-d+1+ii));
               for ( jj = 1; jj<=n-d+ii+1; jj++ )
                 {
                   P=P+trex[1,jj]*m[jj];
                 }
               chcoord[n-d+1+ii]=P;
               P=0;
               acc=acc+n-d+ii;
               kill trex;
             }
               phi=rtr,chcoord;
               I=simplify(lead(std(phi(i))),1);
               setring r1;
               I=imap(rtr,I);
               attrib(I,"isSB",1);
         }
       else
         {
           if ( nesttest == 0 )
             {
               N=d*(d-1)/2;
               s = "ring rtr = (ch,t(1..N)),x(0..n),dp;";
               execute(s);
               ideal chcoord,m,i,I;
               poly P;
               map phi;
               i=imap(r1,i);
               chcoord=select1(maxideal(1),1..(n-d+2));
               acc=0;
               for ( ii = 1; ii<=d-1; ii++ )
                 {
                   matrix trex[1][ii+1]=t((1+acc)..(ii+acc)),1;
                    m=select1(maxideal(1),(n-d+2)..(n-d+2+ii));
                   for ( jj = 1; jj<=ii+1; jj++ )
                     {
                       P=P+trex[1,jj]*m[jj];
                     }
                   chcoord[n-d+2+ii]=P;
                   P=0;
                   acc=acc+ii;
                   kill trex;
                 }
               phi=rtr,chcoord;
               I=simplify(lead(std(phi(i))),1);
               setring r1;
               I=imap(rtr,I);
               attrib(I,"isSB",1);
             }
         }
     }
   else
     {
       if ( NPtest == 0 )
         {
           while ( nl < 30 )
             {
               chcoord=select1(maxideal(1),1..(n-d+1));
               nl=nl+1;
               for ( ii = 1; ii<=d; ii++ )
                 {
                   ran=random(100,1,n-d+ii);
                   ran=intmat(ran,1,n-d+ii+1);
                   ran[1,n-d+ii+1]=1;
                   m=select1(maxideal(1),1..(n-d+1+ii));
                   for ( jj = 1; jj<=n-d+ii+1; jj++ )
                     {
                       P=P+ran[1,jj]*m[jj];
                     }
                   chcoord[n-d+1+ii]=P;
                   P=0;
                 }
               phi=r1,chcoord;
               dbprint(printlevel-voice+2,"// (1 random change of coord.)");
               I=simplify(lead(std(phi(i))),1);
               attrib(I,"isSB",1);
               NPtest=is_NP(I);
               if ( NPtest == 1 )
                 {
                   break;
                 }
             }
           if ( NPtest == 0 )
             {
       "// WARNING from proc depthIdeal from lib mregular.lib:
// The procedure has entered in 30 loops and could not put the variables
// in Noether position: in your example the method using random changes
// of coordinates may enter an infinite loop when the field is finite.
// Try removing this optional argument.";
       return (-1);
             }
           i=phi(i);
           nesttest=is_nested(I);
         }
       if ( nesttest == 0 )
         {
           while ( nl < 30 )
             {
               chcoord=select1(maxideal(1),1..(n-d+2));
               nl=nl+1;
               for ( ii = 1; ii<=d-1; ii++ )
                 {
                   ran=random(100,1,ii);
                   ran=intmat(ran,1,ii+1);
                   ran[1,ii+1]=1;
                   m=select1(maxideal(1),(n-d+2)..(n-d+2+ii));
                   for ( jj = 1; jj<=ii+1; jj++ )
                     {
                       P=P+ran[1,jj]*m[jj];
                     }
                   chcoord[n-d+2+ii]=P;
                   P=0;
                 }
               phi=r1,chcoord;
               dbprint(printlevel-voice+2,"// (1 random change of coord.)");
               I=simplify(lead(std(phi(i))),1);
               attrib(I,"isSB",1);
               nesttest=is_nested(I);
               if ( nesttest == 1 )
                 {
                   break;
                 }
             }
           if ( nesttest == 0 )
             {
       "// WARNING from proc depthIdeal from lib mregular.lib:
// The procedure has entered in 30 loops and could not find a monomial
// ideal of nested type with the same depth as your ideal: in your
// example the method using random changes of coordinates may enter an
// infinite loop when the field is finite.
// Try removing this optional argument.";
       return (-1);
             }
         }
     }
//
// At this stage, we have obtained a monomial ideal I of nested type
// such that depth(S/i)=depth(S/I). We now compute depth(I).
//
//----- When S/i is Cohen-Macaulay:
   for ( ii = n-d+2; ii <= n+1; ii++ )
     {
       K=K+select(I,ii);
     }
   if ( size(K) == 0 )
     {
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: "+string(d)+" (S/i is Cohen-Macaulay)");
       dbprint(printlevel-voice+2,
               "// Time for computing depth: " + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The depth of S/i is:");
       return(d);
     }
//----- When d=1 (and S/i is not Cohen-Macaulay) ==> depth =0:
   if ( d == 1 )
     {
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: 1");
       dbprint(printlevel-voice+2,
               "// Time for computing depth: "+ string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The depth of S/i is:");
       return(0);

     }
//----- Now d>1 and S/i is not Cohen-Macaulay:
//
//----- First, determine the last variable really occuring
       lastv=n-d;
       h=n;
       while ( lastv == n-d and h > n-d )
         {
           K=select(I,h+1);
           if ( size(K) == 0 )
             {
               h=h-1;
             }
           else
             {
               lastv=h;
             }
         }
//----- and compute the depth:
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: "+string(d));
       dbprint(printlevel-voice+2,
               "// Time for computing depth: "+ string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The depth of S/i is:");
       return(n-lastv);
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,(x,y,z,t,w),dp;
   ideal i=y2t,x2y-x2z+yt2,x2y2,xyztw,x3z2,y5+xz3w-x2zw2,x7-yt2w4;
   depthIdeal(i);
   depthIdeal(lead(std(i)));
// Additional information is displayed if you change printlevel (=1);
}
////////////////////////////////////////////////////////////////////////////////
/*
Out-commented examples:
   ring s=0,x(0..5),dp;
   ideal i=x(2)^2-x(4)*x(5),x(1)*x(2)-x(0)*x(5),x(0)*x(2)-x(1)*x(4),
           x(1)^2-x(3)*x(5),x(0)*x(1)-x(2)*x(3),x(0)^2-x(3)*x(4);
   depthIdeal(i);
   // Our procedure works when a min. graded free resol. can
   // not be computed. In this easy example, depth can also
   // be obtained using a m.g.f.r. (Auslander-Buchsbaum formula):
   nvars(s)-ncols(betti(mres(i,0)))+1;
   ring r1=0,(x,y,z,t),dp;
// Ex.2.5 in [Bermejo-Gimenez], Proc.Amer.Math.Soc. 128(5):
   ideal i  = x17y14-y31, x20y13, x60-y36z24-x20z20t20;
   depthIdeal(i);
// Ex.2.9 in [Bermejo-Gimenez], Proc.Amer.Math.Soc. 128(5):
   int k=43;
   ideal j=x17y14-y31,x20y13,x60-y36z24-x20z20t20,y41*z^k-y40*z^(k+1);
   depthIdeal(j);
// Example in Rk.2.10 in [Bermejo-Gimenez], ProcAMS 128(5):
   ideal h=x2-3xy+5xt,xy-3y2+5yt,xz-3yz,2xt-yt,y2-yz-2yt;
   depthIdeal(h);
// The initial ideal is not saturated
   depthIdeal(lead(std(h)));
// More examples:
   i=y4-t3z, x3t-y2z2, x3y2-t2z3, x6-tz5;
   depthIdeal(i);
//
   depthIdeal(maxideal(4));
//
   ring r2=0,(x,y,z,t,w),dp;
   ideal i = xy-zw,x3-yw2,x2z-y2w,y3-xz2,-y2z3+xw4+tw4+w5,-yz4+x2w3+xtw3+xw4,
            -z5+x2tw2+x2w3+yw4;
   depthIdeal(i);
//
   ring r3=0,(x,y,z,t,w,u),dp;
   ideal i=imap(r2,i);
   depthIdeal(i);
// Next example is the defining ideal of the 2nd. Veronesean of P3, a variety
// in P8 which is arithmetically Cohen-Macaulay:
   ring r4=0,(a,b,c,d,x(0..9)),dp;
   ideal i= x(0)-ab,x(1)-ac,x(2)-ad,x(3)-bc,x(4)-bd,x(5)-cd,
            x(6)-a2,x(7)-b2,x(8)-c2,x(9)-d2;
   ideal ei=eliminate(i,abcd);
   ring r5=0,x(0..9),dp;
   ideal i=imap(r4,ei);
   depthIdeal(i);
// Here is an example where the computation of a m.g.f.r. of I costs:
   ring r8=0,(x,y,z,t,u,a,b),dp;
   ideal i=u-b40,t-a40,x-a23b17,y-a22b18+ab39,z-a25b15;
   ideal ei=eliminate(i,ab); // It takes a few seconds to compute the ideal
   ring r9=0,(x,y,z,t,u),dp;
   ideal i=imap(r8,ei);
   depthIdeal(i);   // This is very fast.
// Now you can use mres(i,0) to compute a m.g.f.r. of the ideal!
//
// Another one:
   ring r10=0,(x(0..8),s,t),dp;
   ideal i=x(0)-st24,x(1)-s2t23,x(2)-s3t22,x(3)-s9t16,x(4)-s11t14,x(5)-s18t7,
           x(6)-s24t,x(7)-t25,x(8)-s25;
   ideal ei=eliminate(i,st);
   ring r11=0,x(0..8),dp;
   ideal i=imap(r10,ei);
   depthIdeal(i);
// More examples where not even sres works:
// Be careful: elimination takes some time here, but it succeeds!
ring r12=0,(s,t,u,x(0..14)),dp;
ideal i=x(0)-st6u8,x(1)-s5t3u7,x(2)-t11u4,x(3)-s9t4u2,x(4)-s2t7u6,x(5)-s7t7u,
        x(6)-s10t5,x(7)-s4t6u5,x(8)-s13tu,x(9)-s14u,x(10)-st2u12,x(11)-s3t9u3,
        x(12)-s15,x(13)-t15,x(14)-u15;
ideal ei=eliminate(i,stu);
size(ei);
ring r13=0,x(0..14),dp;
ideal i=imap(r12,ei);
size(i);
depthIdeal(i);
//
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc satiety (ideal i, list #)
"
USAGE:   satiety (i[,e]); i ideal, e integer
RETURN:  an integer, the satiety of i.
         (returns -1 if i is not homogeneous)
ASSUME:  i is a homogeneous ideal of the basering S=K[x(0)..x(n)].
         e=0:  (default)
               The satiety is computed determining the fresh elements in the
               socle of i. It works over arbitrary fields.
         e=1:  Makes random changes of coordinates to find a monomial ideal
               with same satiety. It works over infinite fields only. If K
               is finite, it works if it terminates, but may result in an
               infinite loop. After 30 loops, a warning message is displayed
               and -1 is returned.
THEORY:  The satiety, or saturation index, of a homogeneous ideal i is the
         least integer s such that, for all d>=s, the degree d part of the
         ideals i and isat=sat(i,maxideal(1))[1] coincide.
NOTE:    If printlevel > 0 (default = 0), dim(S/i) is also displayed.
EXAMPLE: example satiety; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int e,ii,jj,h,d,time,lastv,nesttest,NPtest,nl,sat;
   intmat ran;
   def r0 = basering;
   int n = nvars(r0)-1;
   if ( size(#) > 0 )
   {
      e = #[1];
   }
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal i,sbi,I,K,chcoord,m,KK;
   poly P;
   map phi;
   i = fetch(r0,i);
   time=rtimer;
   sbi=std(i);
//----- Check ideal homogeneous
   if ( homog(sbi) == 0 )
   {
       "// WARNING from proc satiety from lib mregular.lib:
// The ideal is not homogeneous!";
       return (-1);
   }
   I=simplify(lead(sbi),1);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
     {
       dbprint(printlevel-voice+2,
               "// The ideal i is (1)!
// Its satiety is:");
       return (0);
     }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
     {
       dbprint(printlevel-voice+2,
               "// The ideal i is (0)!
// Its satiety is:");
       return (0);
     }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
     {
       sat=maxdeg1(minbase(quotient(I,maxideal(1))))+1;
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: 0");
       dbprint(printlevel-voice+2,
               "// Time for computing the satiety: " + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The satiety of i is:");
       return (sat);
     }
//----- When one has option e=1:
//
//----- Determine the situation: NT, or NP, or nothing.
//----- Choose the method depending on the situation in order to
//----- get the mon. ideal of nested type associated to i
   if ( e == 1 )
     {
       NPtest=is_NP(I);
       if ( NPtest == 0 )
         {
           while ( nl < 30 )
             {
               chcoord=select1(maxideal(1),1..(n-d+1));
               nl=nl+1;
               for ( ii = 1; ii<=d; ii++ )
                 {
                   ran=random(100,1,n-d+ii);
                   ran=intmat(ran,1,n-d+ii+1);
                   ran[1,n-d+ii+1]=1;
                   m=select1(maxideal(1),1..(n-d+1+ii));
                   for ( jj = 1; jj<=n-d+ii+1; jj++ )
                     {
                       P=P+ran[1,jj]*m[jj];
                     }
                   chcoord[n-d+1+ii]=P;
                   P=0;
                 }
               phi=r1,chcoord;
               dbprint(printlevel-voice+2,"// (1 random change of coord.)");
               I=simplify(lead(std(phi(i))),1);
               attrib(I,"isSB",1);
               NPtest=is_NP(I);
               if ( NPtest == 1 )
                 {
                   break;
                 }
             }
           if ( NPtest == 0 )
             {
       "// WARNING from proc satiety from lib mregular.lib:
// The procedure has entered in 30 loops and could not put the variables
// in Noether position: in your example the method using random changes
// of coordinates may enter an infinite loop when the field is finite.
// Try removing the optional argument.";
       return (-1);
             }
           i=phi(i);
         }
       nesttest=is_nested(I);
       if ( nesttest == 0 )
         {
           while ( nl < 30 )
             {
               chcoord=select1(maxideal(1),1..(n-d+2));
               nl=nl+1;
               for ( ii = 1; ii<=d-1; ii++ )
                 {
                   ran=random(100,1,ii);
                   ran=intmat(ran,1,ii+1);
                   ran[1,ii+1]=1;
                   m=select1(maxideal(1),(n-d+2)..(n-d+2+ii));
                   for ( jj = 1; jj<=ii+1; jj++ )
                     {
                       P=P+ran[1,jj]*m[jj];
                     }
                   chcoord[n-d+2+ii]=P;
                   P=0;
                 }
               phi=r1,chcoord;
               dbprint(printlevel-voice+2,"// (1 random change of coord.)");
               I=simplify(lead(std(phi(i))),1);
               attrib(I,"isSB",1);
               nesttest=is_nested(I);
               if ( nesttest == 1 )
                 {
                   break;
                 }
             }
           if ( nesttest == 0 )
             {
       "// WARNING from proc satiety from lib mregular.lib:
// The procedure has entered in 30 loops and could not find a monomial
// ideal of nested type with the same satiety as your ideal: in your
// example the method using random changes of coordinates may enter an
// infinite loop when the field is finite.
// Try removing the optional argument.";
       return (-1);
             }
         }
//
// At this stage, we have obtained a monomial ideal I of nested type
// such that depth(S/i)=depth(S/I). We now compute depth(I).
//
//----- When S/i is Cohen-Macaulay:
//
       for ( ii = n-d+2; ii <= n+1; ii++ )
         {
           K=K+select(I,ii);
         }
       if ( size(K) == 0 )
         {
           time=rtimer-time;
           // Additional information:
           dbprint(printlevel-voice+2,
                   "// Dimension of S/i: "+string(d));
           dbprint(printlevel-voice+2,
                   "// Time for computing satiety: " + string(time) + " sec.");
           dbprint(printlevel-voice+2,
                   "// The satiety of i is:");
           return(0);
         }
//----- When d=1 (and S/i is not Cohen-Macaulay) ==> depth =0:
       if ( d == 1 )
         {
           KK=simplify(reduce(quotient(I,maxideal(1)),I),2);
           sat=maxdeg1(KK)+1;
           time=rtimer-time;
           // Additional information:
           dbprint(printlevel-voice+2,
                   "// Dimension of S/i: 1");
           dbprint(printlevel-voice+2,
                   "// Time for computing satiety: "+ string(time) + " sec.");
           dbprint(printlevel-voice+2,
                   "// The satiety of i is:");
           return(sat);
         }
//----- Now d>1 and S/i is not Cohen-Macaulay:
//
//----- First, determine the last variable really occuring
       lastv=n-d;
       h=n;
       while ( lastv == n-d and h > n-d )
         {
           K=select(I,h+1);
           if ( size(K) == 0 )
             {
               h=h-1;
             }
           else
             {
               lastv=h;
             }
         }
//----- and compute the satiety:
       sat=0;
       if ( lastv == n )
         {
           KK=simplify(reduce(quotient(I,maxideal(1)),I),2);
           sat=maxdeg1(KK)+1;
         }
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: "+string(d));
       dbprint(printlevel-voice+2,
               "// Time for computing satiety: "+ string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The satiety of i is:");
       return(sat);
     }
//---- If no option: direct computation
   sat=maxdeg1(reduce(quotient(i,maxideal(1)),sbi))+1;
   time=rtimer-time;
       // Additional information:
   dbprint(printlevel-voice+2,
           "// Dimension of S/i: "+string(d)+";");
   dbprint(printlevel-voice+2,
           "// Time for computing satiety: "+ string(time) + " sec.");
   dbprint(printlevel-voice+2,
           "// The satiety of i is:");
   return(sat);
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,(x,y,z,t,w),dp;
   ideal i=y2t,x2y-x2z+yt2,x2y2,xyztw,x3z2,y5+xz3w-x2zw2,x7-yt2w4;
   satiety(i);
   ideal I=lead(std(i));
   satiety(I);   // First  method: direct computation
   satiety(I,1); // Second method: doing changes of coordinates
// Additional information is displayed if you change printlevel (=1);
}
////////////////////////////////////////////////////////////////////////////////
/*
Out-commented examples:
   ring s1=0,(x,y,z,t),dp;
   ideal I=zt3,z2t2,yz2t,xz2t,xy2t,x3y;
   satiety(I);
   satiety(I,1);
// Another example:
   ring s2=0,(z,y,x),dp;
   ideal I=z38,z26y2,z14y4,z12x,z10x5,z8x9,z6x16,z4x23,z2y6,y32;
   satiety(I);
   satiety(I,1);
// One more:
   ring s3=0,(s,t,u,x(0..8)),dp;
   ideal i=x(0)-st6u8,x(1)-s5t3u7,x(2)-t11u4,x(3)-s9t4u2,
           x(4)-s2t7u6,x(5)-s7t7u,x(6)-s15,x(7)-t15,x(8)-u15;
   ideal ei=eliminate(i,stu);
   size(ei);
   ring s4=0,x(0..8),dp;
   ideal i=imap(s3,ei);
   ideal m=maxideal(1);
   m[8]=m[8]+m[7];
   map phi=m;
   ideal phii=phi(i);
   ideal nI=lead(std(phii));
   ring s5=0,x(0..7),dp;
   ideal nI=imap(s4,nI);
   satiety(nI);
   satiety(nI,1);
   ideal I1=subst(nI,x(7),1);
   ring s6=0,x(0..6),dp;
   ideal I1=imap(s5,I1);
   satiety(I1);
   satiety(I1,1);
   ideal I2=subst(I1,x(6),1);
   ring s7=0,x(0..5),dp;
   ideal I2=imap(s6,I2);
   satiety(I2);
   satiety(I2,1);
//
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc regMonCurve (list #)
"
USAGE:   regMonCurve (a0,...,an) ; ai integers with a0=0 < a1 < ... < an=:d
RETURN:  an integer, the Castelnuovo-Mumford regularity of the projective
         monomial curve C in Pn(K) parametrically defined by
              x(0) = t^d , x(1) = s^(a1)t^(d-a1) , ..... , x(n) = s^d
         where K is the field of complex numbers.
         (returns -1 if a0=0 < a1 < ... < an is not satisfied)
ASSUME:  a0=0 < a1 < ... < an are integers.
NOTES:   1. The defining ideal of the curve C, I in S=K[x(0),...,x(n)], is
            determined by elimination.
         2. The procedure regIdeal has been improved in this case since one
            knows beforehand that the monomial ideal J=lead(std(I)) is of
            nested type if the monomial ordering is dp, and that
            reg(C)=reg(J) (see preprint 'Saturation and Castelnuovo-Mumford
            regularity' by Bermejo-Gimenez, 2004).
         3. If printlevel > 0 (default = 0) additional info is displayed:
            - It says whether C is arithmetically Cohen-Macaulay or not.
            - If C is not arith. Cohen-Macaulay, end(H^1(S/I)) is computed
              and an upper bound for the a-invariant of S/I is given.
            - It also determines one step of the minimal graded free
              resolution (m.g.f.r.) of I where the regularity is attained
              and gives the value of the regularity of the Hilbert function
              of S/I when reg(I) is attained at the last step of a m.g.f.r.
EXAMPLE: example regMonCurve; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int ii,H,h,hh,time,ttime,firstind,lastind;
   int n = size(#)-1;
//------------------  Check assumptions on integers  -------------------------
   if ( #[1] != 0 )
   {"// WARNING from proc regMonCurve from lib mregular.lib:
// USAGE: your input must be a list of integers a0,a1,...,an such that
// a0=0 < a1 < a2 < ... < an";
      return(-1);
   }
   for ( ii=1; ii<= n; ii++ )
   {
      if ( #[ii] >= #[ii+1] )
      {
      "// WARNING from proc regMonCurve from lib mregular.lib:
// USAGE: your input must be a list of integers a0,a1,...,an such that
// a0=0 < a1 < a2 < ... < an";
      return(-1);
      }
   }
   ring R=0,(x(0..n),s,t),dp;
   ideal param,m,i;
   poly f(0..n);
   for (ii=0;ii<=n;ii++)
      {
      f(ii)=s^(#[n+1]-#[ii+1])*t^(#[ii+1]);
      param=param+f(ii);
      }
   m=subst(maxideal(1),s,0);
   m=simplify(subst(m,t,0),2);
   i=matrix(m)-matrix(param);
   ttime=rtimer;
   i=eliminate(i,st);
   ring r=0,(x(1..n),x(0)),dp;
   ideal i,I;
   i=imap(R,i);
   I=minbase(lead(std(i)));
   attrib(I,"isSB",1);
   ttime=rtimer-ttime;
   time=rtimer;
   ring nr=0,x(1..n),dp;
   ideal I,K,KK,J;
   I=imap(r,I);
   attrib(I,"isSB",1);
   K=select(I,n);
//------------------ Cohen-Macaulay case ------------
   if ( size(K) == 0 )
     {
       ring mr=0,x(1..n-1),dp;
       ideal I=imap(nr,I);
       H=maxdeg1(minbase(quotient(I,maxideal(1))))+1;
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// The sequence of integers defines a monomial curve C in P"
               + string(n));
       dbprint(printlevel-voice+2,
               "//    C is arithmetically Cohen-Macaulay");
       dbprint(printlevel-voice+2,
               "//    Regularity attained at the last step of a m.g.f.r. of I(C)");
       dbprint(printlevel-voice+2,
               "//    Regularity of the Hilbert function of S/I(C): "
               + string(H-2));
       dbprint(printlevel-voice+2,
               "//    Time for computing ideal I(C) (by elimination): "
               + string(ttime) + " sec.");
       dbprint(printlevel-voice+2,
               "//    Time for computing reg(C) once I(C) has been determined: "
               + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The Castelnuovo-Mumford regularity of C is:");
       return(H);
     }
   else
     {
       KK=simplify(reduce(quotient(I,maxideal(1)),I),2);
       firstind=maxdeg1(KK)+1;
       J=subst(I,x(n),1);
       ring mr=0,x(1..n-1),dp;
       ideal J=imap(nr,J);
       lastind=maxdeg1(minbase(quotient(J,maxideal(1))))+1;
       H=firstind;
       if ( lastind > H )
         {
           H=lastind;
         }
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// The sequence of integers defines a monomial curve C in P"
               + string(n));
       dbprint(printlevel-voice+2,
               "//    C is not arithmetically Cohen-Macaulay");
       dbprint(printlevel-voice+2,
               "//    end(H^1(S/I(C))) = "
               +string(firstind-2));
       dbprint(printlevel-voice+2,
               "//    Upper bound for the a-invariant of S/I(C): end(H^2(S/I(C))) <= "
               +string(lastind-3));
       if ( H == firstind )
         {
           dbprint(printlevel-voice+2,
                   "//    Regularity attained at the last step of a m.g.f.r. of I(C)");
           dbprint(printlevel-voice+2,
                   "//    Regularity of the Hilbert function of S/I(C): "
                   + string(H-1));
         }
       else
         {
           dbprint(printlevel-voice+2,
                   "//    Regularity attained at the second last step of a m.g.f.r. of I(C)");
           dbprint(printlevel-voice+2,
                   "//    (and not attained at the last step)");
         }
       dbprint(printlevel-voice+2,
               "//    Time for computing ideal I(C) (by elimination): "
               + string(ttime) + " sec.");
       dbprint(printlevel-voice+2,
               "//    Time for computing reg(C) once I(C) has been determined: "
               + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// The Castelnuovo-Mumford regularity of C is:");
       return(H);
     }
}
example
{ "EXAMPLE:"; echo = 2;
// The 1st example is the twisted cubic:
   regMonCurve(0,1,2,3);
// The 2nd. example is the non arithm. Cohen-Macaulay monomial curve in P4
// parametrized by: x(0)-s6,x(1)-s5t,x(2)-s3t3,x(3)-st5,x(4)-t6:
   regMonCurve(0,1,3,5,6);
// Additional information is displayed if you change printlevel (=1);
}
////////////////////////////////////////////////////////////////////////////////
/*
Out-commented examples:
//
// The sequence of integers must be strictly increasing
   regMonCurve(1,4,6,9);
   regMonCurve(0,3,8,5,23);
   regMonCurve(0,4,7,7,9);
//
// A curve in P3 s.t. the regularity is attained at the last step:
   regMonCurve(0,2,12,15);
//
// A curve in P4 s.t. the regularity attained at the last but one
// but NOT at the last step (Ex. 3.3 Preprint 2004):
   regMonCurve(0,5,9,11,20);
//
// A curve in P8 s.t. the m.g.f.r. of the defining ideal is not easily
// obtained through m.g.f.r.:
   regMonCurve(0,1,2,3,9,11,18,24,25);
//
// A curve in P11 of degree 37:
   regMonCurve(0,1,2,7,16,17,25,27,28,30,36,37);
// It takes some time to compute the eliminated ideal; the computation of
// the regularity is then rather fast as one can check using proc regIdeal:
   ring q=0,(s,t,x(0..11)),dp;
   ideal i=x(0)-st36,x(1)-s2t35,x(2)-s7t30,x(3)-s16t21,x(4)-s17t20,x(5)-s25t12,
           x(6)-s27t10,x(7)-s28t9,x(8)-s30t7,x(9)-s36t,x(10)-s37,x(11)-t37;
   ideal ei=eliminate(i,st);
   ring qq=0,x(0..11),dp;
   ideal i=imap(q,ei);
   regIdeal(i);
//
// A curve in P14 of degree 55:
   regMonCurve(0,1,2,7,16,17,25,27,28,30,36,37,40,53,55);
//
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc NoetherPosition (ideal i)
"
USAGE:   NoetherPosition (i); i ideal
RETURN:  ideal such that, for the homogeneous linear transformation
         map phi=S,NoetherPosition(i);
         one has that K[x(n-d+1),...,x(n)] is a Noether normalization of
         S/phi(i) where S=K[x(0),...x(n)] is the basering and d=dim(S/i).
         (returns -1 if i = (0) or (1)).
ASSUME:  The field K is infinite and i is a nonzero proper ideal.
NOTE:    1. It works also if K is a finite field if it terminates, but
            may result in an infinite loop. If the procedure enters more
            than 30 loops, -1 is returned and a warning message is displayed.@*
         2. If printlevel > 0 (default = 0), additional info is displayed:
            dim(S/i) and K[x(n-d+1),...,x(n)] are given.
EXAMPLE: example NoetherPosition; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int ii,jj,d,time,nl,NPtest;
   intmat ran;
   def r0 = basering;
   ideal K,chcoord;
   int n = nvars(r0)-1;
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal i,sbi,I,K,chcoord,m;
   poly P;
   map phi;
   i = fetch(r0,i);
   time=rtimer;
   sbi=std(i);
   I=simplify(lead(sbi),1);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
     {
       "// WARNING from proc NoetherPosition from lib mregular.lib:
// The ideal i is (1)!";
       return (-1);
     }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
     {
       "// WARNING from proc NoetherPosition from lib mregular.lib:
// The ideal i is (0)!";
       return (-1);
     }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
     {
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: 0");
       dbprint(printlevel-voice+2,
               "// Time for computing a Noether normalization: "
               + string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// K is a Noether normalization of S/phi(i)");
       dbprint(printlevel-voice+2,
               "// where the map phi: S --> S is:");
       setring r0;
       return (maxideal(1));
     }
   NPtest=is_NP(I);
   if ( NPtest == 1 )
     {
       K=x(n-d+1..n);
       setring r0;
       K=fetch(r1,K);
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: " + string(d) );
       dbprint(printlevel-voice+2,
               "// Time for computing a Noether normalization: " +
               string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// K[" + string(K) +
               "] is a Noether normalization of S/phi(i)");
       dbprint(printlevel-voice+2,
               "// where the map phi: S --> S is:");
       return (maxideal(1));
     }
//---- Otherwise, random change of coordinates and
//---- test for Noether normalization.
//---- If we were unlucky, another change of coord. will be done:
   while ( nl < 30 )
   {
     chcoord=select1(maxideal(1),1..(n-d+1));
     nl=nl+1;
     for ( ii = 1; ii<=d; ii++ )
     {
       ran=random(100,1,n-d+ii);
       ran=intmat(ran,1,n-d+ii+1);
       ran[1,n-d+ii+1]=1;
       m=select1(maxideal(1),1..(n-d+1+ii));
       for ( jj = 1; jj<=n-d+ii+1; jj++ )
       {
       P=P+ran[1,jj]*m[jj];
       }
       chcoord[n-d+1+ii]=P;
       P=0;
     }
     phi=r1,chcoord;
     dbprint(printlevel-voice+2,"// (1 random change of coord.)");
     I=simplify(lead(std(phi(i))),1);
     attrib(I,"isSB",1);
     NPtest=is_NP(I);
     if ( NPtest == 1 )
       {
       K=x(n-d+1..n);
       setring r0;
       K=fetch(r1,K);
       chcoord=fetch(r1,chcoord);
       time=rtimer-time;
       // Additional information:
       dbprint(printlevel-voice+2,
               "// Dimension of S/i: " + string(d) );
       dbprint(printlevel-voice+2,
               "// Time for computing a Noether normalization: " +
               string(time) + " sec.");
       dbprint(printlevel-voice+2,
               "// K[" + string(K) +
               "] is a Noether normalization of S/phi(i)");
       dbprint(printlevel-voice+2,
               "// where the map phi: S --> S is:");
       return (chcoord);
       }
   }
       "// WARNING from proc NoetherPosition from lib mregular.lib:
// The procedure has entered in more than 30 loops: in your example
// the method may enter an infinite loop over a finite field!";
       return (-1);
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,(x,y,z,t,u),dp;
   ideal i1=y,z,t,u; ideal i2=x,z,t,u; ideal i3=x,y,t,u; ideal i4=x,y,z,u;
   ideal i5=x,y,z,t; ideal i=intersect(i1,i2,i3,i4,i5);
   map phi=r,NoetherPosition(i);
   phi;
   ring r5=5,(x,y,z,t,u),dp;
   ideal i=imap(r,i);
   map phi=r5,NoetherPosition(i);
   phi;
// Additional information is displayed if you change printlevel (=1);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc is_NP (ideal i)
"
USAGE:   is_NP (i); i ideal
RETURN:  1  if K[x(n-d+1),...,x(n)] is a Noether normalization of
            S/i where S=K[x(0),...x(n)] is the basering, and d=dim(S/i),
         0  otherwise.
         (returns -1 if i=(0) or i=(1)).
ASSUME:  i is a nonzero proper homogeneous ideal.
NOTE:    1. If i is not homogeneous and is_NP(i)=1 then K[x(n-d+1),...,x(n)]
            is a Noether normalization of S/i. The converse may be wrong if
            the ideal is not homogeneous.
         2. is_NP is used in the procedures regIdeal, depthIdeal, satiety,
            and NoetherPosition.
EXAMPLE: example is_NP; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int ii,d,dz;
   def r0 = basering;
   int n = nvars(r0)-1;
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal i,sbi,I,J;
   i = fetch(r0,i);
   sbi=std(i);
   I=simplify(lead(sbi),1);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
     {
       "// WARNING from proc is_NP from lib mregular.lib:
// The ideal i is (1)!";
       return (-1);
     }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
     {
       "// WARNING from proc is_NP from lib mregular.lib:
// The ideal i is (0)!";
       return (-1);
     }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
     {
       return (1);
     }
//----- Check Noether position
   J=I;
   for ( ii = n-d+1; ii <= n; ii++ )
     {
       J=subst(J,x(ii),0);
     }
   attrib(J,"isSB",1);
   dz=dim(J);
   if ( dz == d )
     {
       return (1);
     }
   else
     {
       return(0);
     }
}
example
{ "EXAMPLE:"; echo = 2;
   ring r=0,(x,y,z,t,u),dp;
   ideal i1=y,z,t,u; ideal i2=x,z,t,u; ideal i3=x,y,t,u; ideal i4=x,y,z,u;
   ideal i5=x,y,z,t; ideal i=intersect(i1,i2,i3,i4,i5);
   is_NP(i);
   ideal ch=x,y,z,t,x+y+z+t+u;
   map phi=ch;
   is_NP(phi(i));
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

proc is_nested (ideal i)
"
USAGE:   is_nested (i); i monomial ideal
RETURN:  1 if i is of nested type, 0 otherwise.
         (returns -1 if i=(0) or i=(1)).
ASSUME:  i is a nonzero proper monomial ideal.
NOTES:   1. The ideal must be monomial, otherwise the result has no meaning
            (so check this before using this procedure).@*
         2. is_nested is used in procedures depthIdeal, regIdeal and satiety.@*
         3. When i is a monomial ideal of nested type of S=K[x(0)..x(n)],
            the a-invariant of S/i coincides with the upper bound obtained
            using the procedure regIdeal with printlevel > 0.
THEORY:  A monomial ideal is of nested type if its associated primes are all
         of the form (x(0),...,x(i)) for some i<=n.
         (see definition and effective criterion to check this property in
         the preprint 'Saturation and Castelnuovo-Mumford regularity' by
         Bermejo-Gimenez, 2004).
EXAMPLE: example is_nested; shows some examples
"
{
//--------------------------- initialisation ---------------------------------
   int ii,d,tev,lastv,h,NPtest;
   def r0 = basering;
   int n = nvars(r0)-1;
   string s = "ring r1 = ",charstr(r0),",x(0..n),dp;";
   execute(s);
   ideal I,K,KK,LL;
   I = fetch(r0,i);
   I=minbase(I);
   attrib(I,"isSB",1);
   d=dim(I);
//----- If the ideal i is not proper:
   if ( d == -1 )
   {
       "// WARNING from proc is_nested from lib mregular.lib:
// The ideal i is (1)!";
       return (-1);
   }
//----- If the ideal i is 0:
   if ( size(I) == 0 )
     {
       "// WARNING from proc is_nested from lib mregular.lib:
// The ideal i is (0)!";
       return (-1);
     }
//----- When the ideal i is 0-dimensional:
   if ( d == 0 )
     {
       return (1);
     }
//----- Check Noether position
   NPtest=is_NP(I);
   if ( NPtest != 1 )
   {
       return (0);
   }
//----- When ideal is 1-dim. + var. in Noether position -> Nested Type
   if ( d == 1 )
     {
       return (1);
     }
//----- Determ. of the last variable really occuring
   lastv=n-d;
   h=n;
   while ( lastv == n-d and h > n-d )
     {
       K=select(I,h+1);
       if ( size(K) == 0 )
         {
           h=h-1;
         }
       else
         {
           lastv=h;
         }
     }
//----- Check the second property by evaluation when NP + d>1
   KK=subst(I,x(lastv),1);
   for ( ii = n-lastv; ii<=d-2; ii++ )
   {
     LL=minbase(subst(I,x(n-ii-1),1));
     attrib(LL,"isSB",1);
     tev=size(reduce(KK,LL));
     if ( tev > 0 )
     {
       return(0);
     }
     KK=LL;
   }
   return(1);
}
example
{ "EXAMPLE:"; echo = 2;
   ring s=0,(x,y,z,t),dp;
   ideal i1=x2,y3; ideal i2=x3,y2,z2; ideal i3=x3,y2,t2;
   ideal i=intersect(i1,i2,i3);
   is_nested(i);
   ideal ch=x,y,z,z+t;
   map phi=ch;
   ideal I=lead(std(phi(i)));
   is_nested(I);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////