File: ReesAlgebra.m2

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (2649 lines) | stat: -rw-r--r-- 84,503 bytes parent folder | download
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
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
-------------------------------------------------------------------------
-- PURPOSE : Compute the Rees algebra of a module as it is defined in the 
--           paper "What is the Rees algebra of a module?" by Craig Huneke, 
--           David Eisenbud and Bernde Ulrich.
--           Also to compute many of the structures that require a Rees 
--           algebra, including 
-- analyticSpread
-- specialFiber
-- idealIntegralClosure
-- distinguished -- distinguished subvarieties of  a variety 
--                  (components of the support of the normal cone)
-- PROGRAMMERs : Rees algebra code written by David Eisenbud,
--               Amelia Taylor, Sorin Popescu, and students (see the JSAG description)
-- UPDATE HISTORY : created 27 October 2006 
-- 	     	    updated 29 June 2008
--                  updated 19-21 July 2017 (Berkeley M2 Workgroup)
--                  updated November 2017
--
---------------------------------------------------------------------------
newPackage(
	"ReesAlgebra",
    	Version => "2.3", 
    	Date => "November 2019",
    	Authors => {{
		  Name => "David Eisenbud",
		  Email => "de@msri.org"},
	     {Name => "Amelia Taylor",
   	     Email => "originalbrickhouse@gmail.com"},
             {Name => "Sorin Popescu",
	      Email => "sorin@math.sunysb.edu"},
	     {Name => "Michael E. Stillman", Email => "mike@math.cornell.edu"}},  
    	DebuggingMode => false,
    	Headline => "Rees algebras",
	Keywords => {"Commutative Algebra"},
	Certification => {
	     "journal name" => "The Journal of Software for Algebra and Geometry",
	     "journal URI" => "http://j-sag.org/",
	     "article title" => "The ReesAlgebra package in Macaulay2",
	     "acceptance date" => "21 May 2018",
	     "published article URI" => "https://msp.org/jsag/2018/8-1/p05.xhtml",
	     "published article DOI" => "10.2140/jsag.2018.8.49",
	     "published code URI" => "https://msp.org/jsag/2018/8-1/jsag-v8-n1-x05-ReesAlgebra.m2",
	     "repository code URI" => "http://github.com/Macaulay2/M2/blob/master/M2/Macaulay2/packages/ReesAlgebra.m2",
	     "release at publication" => "0ccfca1d3d08d13ed0da78435b2106209fcee1b1",	    -- git commit number in hex
	     "version at publication" => "2.2",
	     "volume number" => "8",
	     "volume URI" => "https://msp.org/jsag/2018/8-1/"
	     }
	)
-*
restart
uninstallPackage "ReesAlgebra"
restart
installPackage  "ReesAlgebra"
viewHelp ReesAlgebra
check "ReesAlgebra"
*-

export{
  "analyticSpread", 
  "distinguished",
  "intersectInP",
  "isLinearType", 
  "minimalReduction",
  "isReduction",
  "multiplicity",
  "normalCone", 
  "reductionNumber",
  "reesIdeal",
  "reesAlgebra",
  "specialFiberIdeal",
  "specialFiber",
  "symmetricKernel", 
  "versalEmbedding",
  "whichGm",
  "Tries",
  "jacobianDual",
  "symmetricAlgebraIdeal",
  "expectedReesIdeal",
  "PlaneCurveSingularities",
  --synonyms
  "associatedGradedRing" => "normalCone",
  "reesAlgebraIdeal" => "reesIdeal",
  "Trim" -- option in reesIdeal
  }


symmetricAlgebraIdeal = method(Options =>
    {             VariableBaseName => "w"
     })
symmetricAlgebraIdeal Module := Ideal => o -> M -> (
    ideal presentation symmetricAlgebra(M, o))
symmetricAlgebraIdeal Ideal := Ideal => o -> M -> (
    ideal presentation symmetricAlgebra(module M, o))


    
symmetricKernel = method(Options=>{Variable => "w"})
symmetricKernel(Matrix) := Ideal => o -> (f) -> (
     if rank source f == 0 then return trim ideal(0_(ring f));
     w := o.Variable;
     if instance(w,String) then w = getSymbol w;
     S := symmetricAlgebra(source f, VariableBaseName => w);
     T := symmetricAlgebra target f;
     trim ker symmetricAlgebra(T,S,f))

versalEmbedding = method()
versalEmbedding(Ideal) :=
versalEmbedding(Module) := Matrix => (M) -> (
     if (class M) === Ideal then M = module M;
     UE := transpose syz transpose presentation M;
     map(target UE, M, UE)
     )

fixupw = w -> if instance(w,String) then getSymbol w else w

reesIdeal = method(
    Options => {
	  Jacobian =>false,
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w",
	  Trim => true
	  }
      )

--the following uses a versal embedding
reesIdeal(Module) := Ideal => o -> M -> (
     if o.Trim == true then P := presentation minimalPresentation M else P = presentation M;
     UE := transpose syz transpose P;
     symmetricKernel(UE,Variable => fixupw o.Variable)
     )

--in the case of ideals we don't need a versal embedding; any embedding in the ring will do.
reesIdeal(Ideal) := Ideal => o-> (J) -> (
    if o.Trim == true then J' := mingens J else J' = gens J;
     symmetricKernel(J', Variable => fixupw o.Variable)
     )

-- the following method, usually faster,
-- needs a user-provided non-zerodivisor a such that M[a^{-1}] is of linear type.

reesIdeal(Module,RingElement) := Ideal => o-> (I,I0) ->(
    if o.Trim == true then I' := trim I else I' = I;
    K' := if o.Jacobian == true then expectedReesIdeal I' else(
    K' = symmetricAlgebraIdeal I';
    R := ring K';
    IR := substitute(I0, R);
    trim saturate(K',IR)
    )
    )

reesIdeal(Ideal, RingElement) := Ideal => o -> (I,a) -> (
     if o.Trim == true then I' := trim I else I' = I;
     reesIdeal(module I', a, Trim => o.Trim)
     )

reesAlgebra = method (TypicalValue=>Ring,
            Options => {Jacobian => false,
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  }
    )
-- accepts a Module, Ideal, or pair (depending on the method) and
-- returns the quotient ring isomorphic to the Rees Algebra rather
-- than just the defining ideal as in reesIdeal. 

reesAlgebra Ideal :=
reesAlgebra Module := o-> M ->  quotient reesIdeal(M, o)

reesAlgebra(Ideal, RingElement) :=
reesAlgebra(Module, RingElement) := o->(M,a)-> quotient reesIdeal(M,a,o)
       
isLinearType=method(TypicalValue =>Boolean,
        Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null--,
	  --Variable => "w"
	  }
)

isLinearType(Ideal):=
isLinearType(Module):= o-> N->(
     if class N === Ideal then N = module N;
     M := prune N;
     I := reesIdeal (M,o);
     S := ring I;
     P := promote(presentation M, S);
     J := ideal((vars S) * P);
     ((gens I) % J) == 0)

isLinearType(Ideal, RingElement):=
isLinearType(Module, RingElement):= o-> (N,a)->(
     if class N === Ideal then N = module N;
     M := prune N;
     I := reesIdeal(M,a,o);
     S := ring I;
     P := promote(presentation M, S);
     J := ideal((vars S) * P);
     ((gens I) % J) == 0)

normalCone = method(TypicalValue => Ring, 
    	    Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  }
)
normalCone(Ideal) := o -> I -> (
     RI := reesAlgebra(I,o);
     RI/promote(I,RI)
     )

normalCone(Ideal, RingElement) := o -> (I,a) -> (
     RI := reesAlgebra(I,a,o);
     RI/promote(I,RI)     
     )

multiplicity = method(
    	    Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  }
)

multiplicity(Ideal) := ZZ => o -> I ->  (
     RI := normalCone (I,o);
     J := ideal RI;
     J1 := first flattenRing J;
     S1 := newRing(ring J1, Degrees=>{numgens ring J1 : 1});
     degree substitute(J1,S1)
     )

multiplicity(Ideal,RingElement) := ZZ => o -> (I,a) ->  (
     RI := normalCone (I,a,o);
     J := ideal RI;
     J1 := first flattenRing J;
     S1 := newRing(ring J1, Degrees=>{numgens ring J1 : 1});
     degree substitute(J1,S1)
     )


isEquigenerated = A -> (
    if isHomogeneous A and 
    all(A_*, a->degree a == degree(A_*_0)) then true else false)


specialFiberIdeal=method(TypicalValue=>Ideal, 
        Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w",
	  Jacobian =>false,
	  Trim => true
	  }
      )
specialFiberIdeal(Ideal):= o-> I ->(
if isEquigenerated I then(
    kk := ultimate(coefficientRing, ring I);
    Z := symbol Z;
    ker map(ring I, kk[Z_0..Z_(numgens I -1)], gens I)) else
specialFiberIdeal (module I, o))


specialFiberIdeal(Module):= o->i->(
     Reesi:= reesIdeal(i, o);     
     S := ring Reesi;
     kk := ultimate(coefficientRing, S);
     T := kk(monoid [gens S]);
     minimalpres := map(T,S);
     trim minimalpres Reesi
     )
 
specialFiberIdeal(Ideal, RingElement):= o->(i,i0) ->(
if isEquigenerated i then return(
    kk := ultimate(coefficientRing, ring i);
    w := symbol w;
    ker map(ring i, kk[w_0..w_(numgens i -1)], gens i));
specialFiberIdeal(module i, i0))

specialFiberIdeal(Module,RingElement):= o->(i,a)->(
     Reesi:= reesIdeal(i, o);     
     S := ring Reesi;
     kk := ultimate(coefficientRing, S);
     T := kk[gens S];
     minimalpres := map(T,S);
     trim minimalpres Reesi
     )


--The following returns a ring with just the new vars.
--The order of the generators is supposed to be the same as the order
--of the given generators of I.
specialFiber=method(TypicalValue=>Ring, 
            Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w",
	  Jacobian => false,
	  Trim => true
	  }
      )

specialFiber(Ideal):= 
specialFiber(Module):= o->i->(
     spIdeal := specialFiberIdeal(i,o);
     (ring spIdeal)/spIdeal
     )

specialFiber(Ideal, RingElement):= 
specialFiber(Module, RingElement):= o->(i,a)->(
     spIdeal := specialFiberIdeal(i,a,o);
     (ring spIdeal)/spIdeal
     )

isReduction=method(TypicalValue=>Boolean, 
       Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  }
    )

--test whether the SECOND arg is a reduction of the FIRST arg
isReduction(Module,Module):= 
isReduction(Ideal,Ideal):= o->(I,J)->(
     if isSubset(J, I) then (
    	     I' := trim I;
	     Sfib:= specialFiber(I', o);
	     Ifib:=ideal presentation Sfib;
	     kk := coefficientRing Sfib;
	     M := sub(gens J // gens I', kk);
	     M = promote(M, Sfib);
	     L :=(vars Sfib)*M;
	     0===dim ideal L)
     else false)

isReduction(Module,Module,RingElement):= 
isReduction(Ideal,Ideal,RingElement):= o->(I,J,a)->(
     if isSubset(J, I) then (
	     Sfib :=specialFiber(I, a, o);
	     Ifib:= ideal presentation Sfib;
	     kk := coefficientRing Sfib;
	     M := sub(gens J // gens I, kk);
	     M = promote(M, Sfib);
	     L :=(vars Sfib)*M; 
	     0===dim ideal L)
     else false)

analyticSpread = method(
           Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null--,
	  --Variable => "w"
	  }
      )

analyticSpread(Ideal) := 
analyticSpread(Module) := ZZ => o->(M) -> dim specialFiberIdeal(M,o)

analyticSpread(Ideal,RingElement) :=
analyticSpread(Module,RingElement) := ZZ => o->(M,a) -> dim specialFiberIdeal(M,a,o)
 

distinguished = method(Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  }
)

distinguished(RingMap, Ideal) := o -> (f,I) ->(
--f: S -> R,  I\subset S, J\subset R, f(I)\subset J:
    S := source f;
    R := target f;
    NI := normalCone (I,o);
    NJ := normalCone(f I,o);
    K := ker map(NJ,NI,(vars NJ));
    L := decompose K;
    M := apply(L,P->(Pcomponent := K:(saturate(K,P))));
    --the P-primary component. The multiplicity is
    --computed as (degree M_i)/(degree L_i)
    prune NI;
    mp := NI.minimalPresentationMap;
    apply(#L, i -> {(degree mp(M_i))/(degree mp(L_i)),kernel(map(NI/L_i, S/I))})
    )

distinguished(Ideal,Ideal) := o -> (I,J) -> (
    --I,J ideals in the same ring S
    S := ring I;
    f := map(S/J,S);
    distinguished(f,I)
)

distinguished(Ideal) := o -> I -> (
    S := ring I;
    f := map(S,S);
    distinguished(f,I)
)

intersectInP = method(Options=>{
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  Variable => "w"
	  })

intersectInP(Ideal,Ideal) := o->(I,J) ->(
    --I,J in a polynomial ring; intersection done with the diagonal, then pulled back
    P := ring I;
    kk := coefficientRing P;
    n := numgens P;
    if P =!=ring J then error"requires two ideals in the same ring";
    if not isPolynomialRing P and isField kk then 
         error" ring should be a polynomial ring over a field";
    X:=symbol X;
    Y:=symbol Y;
    PP := kk[X_0..X_(n-1),Y_0..Y_(n-1)];
    diag := ideal apply(n, i-> X_i-Y_i);
    toP := map(P,PP/diag,vars P | vars P);
    inX := map(PP,P,apply(n,i->X_i));
    inY := map(PP,P,apply(n,i->Y_i));
    II := inX I + inY J;
    L := distinguished(diag,II);
    apply(L, l-> {l_0, trim toP l_1})
)


rand = method()
rand(Ideal, ZZ, ZZ) := (I,s,d) ->
    --s elements of degree d chosen at random from I
    ideal ((gens I)*random(source gens I, (ring I)^{s:-d}))

rand(Ideal, ZZ) := (I,s) ->(
    --without the third argument d, the function takes
    --random linear combinations of the generators, without
    --regard for the degrees, thus sometimes inhomogeneous.
    kk := ultimate(coefficientRing, ring I);
    choose1 := I -> sum(I_*, i-> random(kk)*i);
    ideal apply(s, i-> choose1 I))

rand(Module, ZZ) := (M,s) ->(
    --random linear combinations of the generators, without
    --regard for the degrees, thus sometimes inhomogeneous.
    kk := ultimate(coefficientRing, ring M);
    choose1 := M -> sum(M_*, i-> random(kk)*i);
    map(M,(ring M)^s, matrix apply(s, i-> choose1 M))
    )

minimalReduction = method(
                   Options => {
	  DegreeLimit => {},
	  BasisElementLimit => infinity,
	  PairLimit => infinity,
	  MinimalGenerators => true,
	  Strategy => null,
	  --Variable => "w",
	  Tries => 20
	  }
      )

minimalReduction Ideal := Ideal => o -> i -> (
     S:=ring i;
     ell := analyticSpread(i,
	  DegreeLimit => o.DegreeLimit,
	  BasisElementLimit => o.BasisElementLimit,
	  PairLimit => o.PairLimit,
	  MinimalGenerators => true,
	  Strategy => o.Strategy
	 ); -- the list is necessary because isReduction doesn't know about "Tries"
     J:=null;
     for b from 1 to o.Tries do(
     J = rand(i, ell);
     if isReduction(i,J,
	  DegreeLimit => o.DegreeLimit,
	  BasisElementLimit => o.BasisElementLimit,
	  PairLimit => o.PairLimit,
	  MinimalGenerators => true,
	  Strategy => o.Strategy
	  )
      then  return J);
     <<o.Tries <<" iterations were not enough to randomly find a minimal reduction"; endl;
     error("not random enough")
          )


reductionNumber = method()
reductionNumber (Ideal,Ideal) := (i,j) -> (
     rN:=0;
     I := (gens i)%j; -- will be a power of i
     if isHomogeneous j then (
     	  while I!=0 do (
    	       j = trim(i*j);
	       I = (gens trim (i*ideal I))%j;
     	       rN =rN+1))
     else(
	  M:= ideal vars ring i; -- we're pretending to be in a local ring
     	  while I!=0 do (
    	       j = trim(i*j+M*ideal I);
	       I = (gens trim (i*ideal I))%j;
     	       rN =rN+1));
     rN)

whichGm = method()
whichGm Ideal := i -> (
     --This *probabilistic* procedure returns the largest number m for which the ideal i satisfies
     --the condition
     --
     --G_m: i_P is generated by <= codim P elements for all P with codim P < m.
     --
     f:=presentation module i;
     S:=ring f;
     if f==0 then "infinity" else(
     q:=rank target f;
     maxSource := (max degrees source f)_0;
     minTarget := (min degrees target f)_0;
     randomMinor := (f,t)->(
	  if t<=0 then ideal(1_S) else
	  if t >min(rank source f, rank target f) then ideal(0_S) else
	   ideal det (random(S^{t:-minTarget},target f)*f*random(source f, S^{t:-maxSource})));
     d:=dim ring i;
     m:=codim i;
     j:=i+randomMinor(f,q-m);
          while m<d+1 and codim j > m do (
	       m=m+1;
	       j=j+randomMinor(f, q-m));
     if m<=d then m else "infinity"))
 
------------------------------------------------------------------
 
jacobianDual = method(Options=>{Variable => "w"})

jacobianDual Matrix := o-> phi ->(
 S := ring phi;
 X := vars S;
 ST := symmetricAlgebra(target phi, VariableBaseName => fixupw o.Variable);
 (vars ST * promote(phi, ST))//promote(X,ST)
          )

jacobianDual(Matrix,Matrix, Matrix) := o -> (phi,X,T) -> (
    --Suppose that T is a 1 x m matrix of variables in the ring ST = R[T_0..T_(m-1)],
    --and phi is a matrix over ST that is defined over the subring R.
    --Suppose also that  X is a 1 x n matrix defined over ST whose
    --entries generate ideal containing the entries of the matrix phi.
    --the routine returns a matrix psi over ST such that 
    --T phi = X psi.
    --Thus psi is a Jacobian dual of phi with respect to X.
    if numcols T != numrows phi then error"if phi has m rows then T must have m cols.";
    psi := (T * phi)//X;
    --check that this worked:
    if not T*phi == X*psi then error"requires 
         ideal flatten entries matrix phi subset ideal flatten entries X";
    psi
    )

expectedReesIdeal = method()

expectedReesIdeal Ideal := I -> expectedReesIdeal module I
expectedReesIdeal Module := Ideal => I -> (
    S := ring I;
    I1 := symmetricAlgebraIdeal I;    
    S1 := ring I1;    
    if numgens I < numgens S then return I1;
    X := promote(vars ring I, S1);
    jImat := jacobianDual (presentation I, X, vars S1);
    I2 := minors(numrows jImat,jImat);
    trim(I1+I2)
    )

beginDocumentation()

///
restart
uninstallPackage "ReesAlgebra"
restart
installPackage  "ReesAlgebra"
viewHelp ReesAlgebra
check "ReesAlgebra"
///


doc ///
  Key
    ReesAlgebra
  Headline
    Compute Rees algebras and their invariants
  Description
    Text
     The Rees Algebra of an ideal is the
     commutative algebra analogue of the blow up in algebraic
     geometry. (In fact, the  ``Rees Algebra''
     is sometimes called the ``blowup algebra''.) 
     A great deal of modern
     commutative algebra is devoted to studying them.
     
     Classically the Rees algebra appeared as the bihomogeneous coordinate
     ring of the blowup of a projective variety along a subvariety or
     subscheme, used for resolution of singularities. 
     Though this is computationally slow on interesting examples,
     we illustrate with some elementary cases of resolution of plane curve
     singularities in @TO PlaneCurveSingularities@.

     The Rees algebra was 
     studied in the commutative algebra context (in the case where M is an ideal of a ring R), 
     by David Rees in
     a famous paper,
     
     {\em On a problem of Zariski}, Illinois J. Math. (1958) 145-149).
 
     In fact, 
     Rees mainly studied the ring 
     $R[It,t^{-1}]$, now also called the `extended Rees
     Algebra' of I. 

     The original goal of this package, first written around 2002,
     was to compute the Rees
     algebra of a module as it is defined in the paper {\em What is the
     Rees algebra of a module?}, by Craig Huneke, David Eisenbud and Bernd
     Ulrich, Proc. Am. Math. Soc. 131, 701-708, 2002.
     It has since expanded to include routines
     for computing many of the invariants of an ideal or module
     defined in terms of Rees algebras.
     
     The Rees algebra, or more precisely the associated graded ring, which
     we compute as a biproduct, plays a central role in modern intersection
     theory: it is the basis of the Fulton-MacPherson definition of the
     intersection product in the Chow ring. We illustrate this in
     @TO distinguished@ and @TO intersectInP@.

     The Rees algebra of a module M is defined 
     by a certain ideal in the symmetric
     algebra $Sym(M)$ of $M$, or, as in this package, 
     by an ideal in the symmetric algebra of any
     free module $F$ that maps onto $M$. 
     When $\phi: M \to G$ is the {\em versal embedding}
     of $M$, then, by the definition of Huneke-Eisenbud-Ulrich,
     the {\em Rees ideal of M} is the kernel of $Sym(\phi)$. Thus the
     Rees Algebra of M is the image of $Sym(\phi)$.
     
     In most cases the kernel of the 
     $Sym(\phi)$ is the same for any embedding $\phi$ of
     $M$ into a free module:
     
     {\bf Theorem (Eisenbud-Huneke-Ulrich, Thms 0.2 and 1.4):} Let R be a Noetherian ring
     and let $M$ be a finitely generated R-module. Let $\phi: M \to G$
     be a versal map of $M$ to a free module. Assume that $\phi$ is an inclusion, and let
     $\psi: M \to G'$ be any inclusion. If $R$ is torsion-free over $\ZZ$
     or $R$ is unmixed and generically Gorenstein, or $M$ is free locally
     at each associated prime of $R$, or $G=R$, then the kernel of $Sym(\phi)$ and the
     kernel of $Sym(\psi)$ are equal.
          
     It follows that in the good cases above the Rees 
     ideal is equal to the saturation
     of the defining ideal of symmetric 
     algebra of $M$ with respect to any
     element f of the ground ring such 
     that $M[f^{-1}]$ is free, or is simply {\em of linear type},
     meaning that $Sym(\phi)$ is a monomorphism. This is the case,
     for example, when M is an ideal and $M[f^{-1}]$ is generated
     by a regular sequence.
     This fact often leads to 
     a faster computation than computing the
     kernel of $Sym(\phi)$ directly.
       
     Here is an example of the pathological case of
     inclusions $\phi: M \to G$ and $\psi: M \to G'$ where $ker(\phi) \neq ker(\psi)$.
     In the following, any finite characteristic would work as well.
    Example
     p = 5;
     R = ZZ/p[x,y,z]/(ideal(x^p,y^p)+(ideal(x,y,z))^(p+1));
     M = module ideal(z);
    Text
     It is easy to check that M \cong R^1/(x,y,z)^p.
     We write iota: M\to R^1 for the embedding as an ideal
     and psi for the embedding M \to R^2 sending z to (x,y).
    Example
     iota = map(R^1,M,matrix{{z}});
     psi = map(R^2,M,matrix{{x},{y}});
    Text
     Finally, a versal embedding is M \to R^3,
     sending z to (x,y,z):
    Example
     phi = versalEmbedding(M);
    Text
     We now compute the kernels of the three maps
     on symmetric algebras:
    Example
     Iiota = symmetricKernel iota;
     Ipsi = symmetricKernel psi;
     Iphi = symmetricKernel phi;
    Text 
     and check that the ones corresponding to phi and iota
     are equal, whereas the ones corresponding to psi and phi
     are not:
    Example
     Iiota == Iphi    
     Ipsi == Iphi
    Text
     In fact, they differ in degree p:
    Example
     numcols basis(p,Iphi) 
     numcols basis(p,Ipsi)
  SeeAlso
   PlaneCurveSingularities
   distinguished
   intersectInP
///

doc ///
   Key
    symmetricAlgebraIdeal
    (symmetricAlgebraIdeal,Ideal)
    (symmetricAlgebraIdeal,Module)
    [symmetricAlgebraIdeal,VariableBaseName]
   Headline
    Ideal of the symmetric algebra of an ideal or module
   Usage
    I = symmetricAlgebraIdeal J
   Inputs
    I:Ideal
    I: Module
   Outputs
    J:Ideal
   Description
    Text
     Uses the built-in  function @TO symmetricAlgebra@. The function returns J an ideal in a
     new ring, with generators corresponding to those of th eideal or module I. The name
     of the new generators may be set, for example to T, with the form
     
     symmetricAlgebraIdeal(J, VariableBaseName =>"T")
   SeeAlso
    reesIdeal
///

-- viewHelp symmetricAlgebra

doc ///
  Key
     symmetricKernel
     (symmetricKernel,Matrix)
  Headline
     Compute the Rees ring of the image of a matrix  
  Usage
     I = symmetricKernel f
  Inputs
    f:Matrix
  Outputs
    :Ideal
      the defining ideal of the image of $Sym(f)$
  Description
   Text
     Given a map between free modules $f: F \to G$ this function computes the
     kernel of the induced map of symmetric algebras, $Sym(f): Sym(F) \to
     Sym(G)$ as an ideal in $Sym(F)$.  When $f$ defines a versal embedding
     of $Im f$ then by the definition
     of Huneke-Eisenbud-Ulrich) this is equal to the defining ideal of the Rees
     algebra of the module Im f, the Rees ideal of M.
     
     When $M$ is an ideal (and in general in characteristic 0) then, by a 
     theorem of Eisenbud-Huneke-Ulrich, 
     any embedding of M into a free module may be used,
     and it follows that the Rees ideal is equal to the saturation
     of the defining ideal of symmetric algebra of M with respect to any
     element f of the ground ring such that M[f^{-1}] is free. And this
     often gives a faster computation.
     
     Most users will prefer to use one of the front 
     end commands @TO reesAlgebra@, @TO reesIdeal@ to compute the ideal.
   Example
     R = QQ[a..e]
     J = monomialCurveIdeal(R, {1,2,3})
     symmetricKernel (gens J)
   Text
     Let I be the ideal returned and let S be the ring it lives in 
    (also printed). The ring S/I is isomorphic to 
    the Rees algebra R[Jt].  We can get the same information
    above using {\tt reesIdeal(J)}, see @TO reesIdeal@. Note that the degree length
    of S is one more than the degree length of R; the old variables
    now have first degree 0, while the new variables have first degree 1.
   Example
     S = ring oo;
     (monoid S).Options.Degrees
   Text
     The function {\tt symmetricKernel} can also be computed over a quotient ring.
   Example
     R = QQ[x,y,z]/ideal(x*y^2-z^9)
     J = ideal(x,y,z)
     symmetricKernel(gens J)
   Text
     The many ways of working with this function allows the system 
     to compute both the classic Rees algebra of an ideal over a ring 
     (polynomial or quotient) and to compute the Rees algebra of a 
     module or ideal using a versal embedding as described in the paper 
     of Eisenbud, Huneke and Ulrich.  It also allows different ways of 
     setting up the quotient ring.
  SeeAlso
     reesIdeal
     reesAlgebra
     versalEmbedding
///


doc ///
  Key
    Trim  
  Headline
    Choose whether to trim (or find minimal generators) for the ideal or module.
  Usage
    reesIdeal(..., Trim => true)
  Description
   Text
    Note that when Trim=>true, the generators used will be the ones (and in the order) M2 likes,
    possibly not the original ones.
  SeeAlso
   reesIdeal
   reesAlgebra
   specialFiberIdeal
   specialFiber
   expectedReesIdeal
///

///
  Description
    Text
      When searching for a minimal reduction of an ideal over a field with
      a small number of elements, random choices of generators are often
      not good enough. This option controls how many times the routine
      will try new random choices before giving up and reporting an error.
    Example
      setRandomSeed(314159268)
      kk=ZZ/2
      S = kk[a,b,c,d];
      I = monomialCurveIdeal(S, {1,3,4});
      minimalReduction(I, Tries=>30);
///
      
doc ///
  Key
    [minimalReduction, Tries]
    Tries
  Headline
    Set the number of random tries to compute a minimal reduction
  Usage
    minimalReduction(..., Tries => 20)
  Description
    Text
      When searching for a minimal reduction of an ideal over a field with
      a small number of elements, random choices of generators are often
      not good enough. This option controls how many times the routine
      will try new random choices before giving up and reporting an error.
    Example
      setRandomSeed(314159268)
      kk=ZZ/2
      S = kk[a,b,c,d];
      I = monomialCurveIdeal(S, {1,3,4});
      minimalReduction(I, Tries=>30);
///

doc ///
  Key
    versalEmbedding
    (versalEmbedding,Ideal)
    (versalEmbedding,Module)
  Headline
    Compute a versal embedding
  Usage
    u = versalEmbedding M
  Inputs
    M:Module
      or @ofClass Ideal@
  Outputs
    u:Matrix
      a matrix that induces a versal embedding of the R-module M
      into a free R-module.
  Description
   Text
      For any module M over a Noetherian ring R there is a map $u: M \to H$
      that is versal for maps from M to free modules; that is,
      such that any map from M to a free module factors through u. Such a map
      may be constructed by choosing a set of s generators for Hom(M,R), and using
      them as the components of a map $u: M \to H := R^s$.
      
      (NOTE: In the paper of  Eisenbud, Huneke and Ulrich
      cited below, the versal map is described with the 
      term ``universal'', which is misleading, since the induced map
      from H is generally not unique.)
      
      Suppose that $M$ has a free presentation $F \to G$, and let $u1$ be the
      map $u1: G\to H$ induced by composing $u$ with the surjection $p: G \to
      M$.  By definition, the Rees algebra of $M$ is the image of the induced
      map $Sym(u1): Sym(G)\to Sym(H)$, and thus can be computed with
      symmetricKernel(u1).  The map u is computed from the dual of the first
      syzygy map of the dual of the presentation of $M$.

      We first give a simple example looking at the syzygy matrix of the cube of
      the maximal ideal of a polynomial ring.
   Example
      S = ZZ/101[x,y,z];
      FF=res ((ideal vars S)^3);
      M=coker (FF.dd_2)
      versalEmbedding M
   Text
      A more complicated example.
   Example
      x = symbol x;
      R=QQ[x_1..x_8];
      m1=genericMatrix(R,x_1,2,2); m2=genericMatrix(R,x_5,2,2);
      m=m1*m2
      d1=minors(2,m1); d2=minors(2,m2);
      M=matrix{{0,d1_0,m_(0,0),m_(0,1)},	   {0,0,m_(1,0),m_(1,1)},	   {0,0,0,d2_0},	   {0,0,0,0}}
      M=M-(transpose M);
      N= coker(res coker transpose M).dd_2
      versalEmbedding(N)
   Text
      Here is an example from the paper "What is the Rees Algebra of a
      Module" by David Eisenbud, Craig Huneke and Bernd Ulrich,
      Proc. Am. Math. Soc. 131, 701-708, 2002.  The example shows that one
      cannot, in general, define the Rees algebra of a module by using *any*
      embedding of that module, even when the module is isomorphic to an ideal;
      this is the reason for using the map provided by the routine
      versalEmbedding. Note that the same paper shows that such problems do
      not arise when the ring is torsion-free as a ZZ-module, or when one takes
      the natural embedding of the ideal into the ring.
   Example
      p = 3;
      S = ZZ/p[x,y,z];
      R = S/((ideal(x^p,y^p))+(ideal(x,y,z))^(p+1))
      I = module ideal(z)
   Text
      As a module (or ideal), $Hom(I,R^1)$ is minimally generated by 3 elements,
      and thus a versal embedding of $I$ into a free module is into $R^3$.
   Example
      betti Hom(I,R^1)
      ui = versalEmbedding I
   Text
      it is injective:
   Example
      kernel ui
   Text
      It is easy to make two other embeddings of $I$ into free modules. One is
      the natural inclusion of $I$ into $R$ as an ideal:
   Example
      inci = map(R^1,I,matrix{{z}})
      kernel inci
   Text
      Another is the map defined by multiplication by x and y. 
   Example
      gi = map(R^2, I, matrix{{x},{y}})
      kernel gi
   Text
      We can compose $ui, inci$ and $gi$ with a surjection $R\to i$ to get maps
      $u:R^1 \to R^3, inc: R^1 \to R^1$ and $g:R^1 \to R^2$ having image $i$.
   Example
      u= map(R^3,R^{-1},ui)
      inc=map(R^1, R^{-1}, matrix{{z}})
      g=map(R^2, R^{-1}, matrix{{x},{y}})
   Text
      We now form the symmetric kernels of these maps and compare them.  Note
      that since symmetricKernel defines a new ring, we must bring them to the
      same ring to make the comparison.  First the map u, which would be used
      by reesIdeal:
   Example
      A=symmetricKernel u
   Text
      Next the inclusion:
   Example
      B1=symmetricKernel inc
      B=(map(ring A, ring B1)) B1
   Text
      Finally, the map g1:
   Example
      C1 = symmetricKernel g
      C=(map(ring A, ring C1)) C1
   Text
      The following test yields ``true'', as implied by the theorem of
      Eisenbud, Huneke and Ulrich.
   Example
      A==B
   Text
      But the following yields ``false'', showing that one must take care
      in general, which inclusion one uses.
   Example
      A==C
  SeeAlso
      reesIdeal
      reesAlgebra
      symmetricKernel
///


doc ///
  Key
    reesIdeal
    (reesIdeal,Ideal)
    (reesIdeal,Module)
    (reesIdeal,Ideal, RingElement)
    (reesIdeal,Module, RingElement)
    [reesIdeal,Jacobian]
    [reesIdeal,Trim]    
  Headline
    Compute the defining ideal of the Rees Algebra
  Usage
    reesIdeal M
    reesIdeal(M,f)
  Inputs
    M:Module
      or @ofClass Ideal@ of a quotient polynomial ring $R$
    f:RingElement
      any non-zerodivisor in ideal or the first Fitting ideal of the module.  Optional
  Outputs
    :Ideal
      defining the Rees algebra of M
  Description
    Text
      This routine gives the user a choice between two methods for finding the
      defining ideal of the Rees algebra of an ideal or module $M$ over a ring
      $R$: The command {\tt reesIdeal(M)}
      computes a versal embedding $g: M\to G$ and a surjection $f: F\to M$
      and returns the result of symmetricKernel(gf). 
      
      When M is an ideal (the usual case) or in characteristic 0, the same
      ideal can be computed by an alternate method that is often faster.
      If the
      user knows a non-zerodivisor $a\in{} R$ such that $M[a^{-1}$ is a free
      module (for example, when M is an ideal, any non-zerodivisor $a \in{} M$ 
      then it is often much faster to compute
        {\tt reesIdeal(M,a)}
      which computes the saturation of the defining ideal of the symmetric algebra
      of M with respect to a. This
      gives the correct answer even under the slightly weaker hypothesis that
      $M[a^{-1}]$ is {\em of linear type}. (See also @TO isLinearType@.)

   Example
      kk = ZZ/101;
      S=kk[x_0..x_4];
      i= trim monomialCurveIdeal(S,{2,3,5,6})
      time V1 = reesIdeal i; 
      time V2 = reesIdeal(i,i_0); 
   Text
     The following example shows how we handle degrees
   Example
      S=kk[a,b,c]
      m=matrix{{a,0},{b,a},{0,b}}
      i=minors(2,m)
      time I1 = reesIdeal i;
      time I2 = reesIdeal(i,i_0);
      transpose gens I1
      transpose gens I2      
   Text
      {\bf Investigating plane curve singularities:} 
      
      Proj of the Rees algebra of I \subset{} R
      is the blowup of I in spec R. Thus the Rees algebra is a basic construction in
      resolution of singularities. Here we work out a simple case:
   Example
      R = ZZ/32003[x,y]
      I = ideal(x,y)
      cusp = ideal(x^2-y^3)
      RI = reesIdeal(I)
      S = ring RI
      totalTransform = substitute(cusp, S) + RI
      D = decompose totalTransform -- the components are the strict transform of the cuspidal curve and the exceptional curve 
      totalTransform = first flattenRing totalTransform
      L = primaryDecomposition totalTransform 
      apply(L, i -> (degree i)/(degree radical i))
   Text
      The total transform of the cusp contains the exceptional divisor with
      multiplicity two.  The strict transform of the cusp is a smooth curve but
      is tangent to the exceptional divisor
   Example
      use ring L_0
      singular = ideal(singularLocus(L_0));
      SL = saturate(singular, ideal(x,y));
      saturate(SL, ideal(w_0,w_1))
   Text
      This shows that the strict transform is smooth.
  SeeAlso
    symmetricKernel
    reesAlgebra
///

doc ///
  Key
    reesAlgebra
    (reesAlgebra,Ideal)
    (reesAlgebra, Module)
    (reesAlgebra,Ideal, RingElement)
    (reesAlgebra,Module, RingElement)
  Headline
    Compute the defining ideal of the Rees Algebra
  Usage
    A = reesAlgebra M
    A = reesAlgebra(M,f)
  Inputs
    M:Module
      or @ofClass Ideal@ of a quotient polynomial ring $R$
    f:RingElement
      any non-zerodivisor in ideal or the first Fitting ideal of the module.  Optional
  Outputs
    A:Ring
      defining the Rees algebra of M
  Description
    Text
      If $M$ is an ideal or module over a ring $R$, and $F\to M$ is a
      surjection from a free module, then reesAlgebra(M) returns the ring
      $Sym(F)/J$, where $J = reesIdeal(M)$.  
      
      In the following example, we find the Rees Algebra of a monomial curve
      singularity.  We also demonstrate the use of @TO reesIdeal@, @TO symmetricKernel@,
      @TO isLinearType@, @TO normalCone@, @TO associatedGradedRing@, @TO specialFiberIdeal@.
    Example
      S = QQ[x_0..x_3]
      i = monomialCurveIdeal(S,{3,7,8})      
      I = reesIdeal i;
      reesIdeal(i, Variable=>v)
      I=reesIdeal(i,i_0);
      (J=symmetricKernel gens i);
      isLinearType(i,i_0)
      isLinearType i
      reesAlgebra (i,i_0)
      trim ideal normalCone (i, i_0)
      trim ideal associatedGradedRing (i,i_0)
      trim specialFiberIdeal (i,i_0)
  SeeAlso
    reesIdeal
    symmetricKernel
///


doc ///
  Key
    isLinearType
    (isLinearType, Module)
    (isLinearType, Ideal) 
    (isLinearType,Module, RingElement)
    (isLinearType, Ideal, RingElement)
    
  Headline
     Determine whether module has linear type
  Usage
     isLinearType M
     isLinearType(M,f)
  Inputs
     M:Module
       or @ofClass Ideal@
     f:RingElement
      any non-zero divisor modulo the ideal or module.  Optional
  Outputs
     :Boolean
       true if M is of linear type, false otherwise
  Description
   Text
     A module or ideal $M$ is said to be ``of linear type'' if the natural map
     from the symmetric algebra of $M$ to the Rees algebra of $M$ is an
     isomorphism. It is known, for example, that any complete intersection
     ideal is of linear type.

     This routine computes the @TO reesIdeal@ of M.  Giving the element f
     computes the @TO reesIdeal@ in a different manner, which is sometimes
     faster, sometimes slower.  
   Example
      S = QQ[x_0..x_4]
      i = monomialCurveIdeal(S,{2,3,5,6})
      isLinearType i
      isLinearType(i, i_0)
      I = reesIdeal i
      select(I_*, f -> first degree f > 1)
   Example
      S = ZZ/101[x,y,z]
      for p from 1 to 5 do print isLinearType (ideal vars S)^p
  SeeAlso
    reesIdeal
    monomialCurveIdeal
///

doc ///
  Key
    isReduction
    (isReduction, Ideal, Ideal)
    (isReduction, Ideal, Ideal, RingElement)
    (isReduction, Module, Module)
    (isReduction, Module, Module, RingElement)
    
  Headline
     Determine whether an ideal is a reduction
  Usage
     t=isReduction(I,J)
     t=isReduction(I,J,f)
  Inputs
     I:Ideal
     J:Ideal
     f:RingElement
       an optional element, which is a non-zerodivisor modulo M and the ring of M
  Outputs
     t:Boolean
       true if J is a reduction of I, false otherwise
  Description
   Text
    For an ideal $I$, a subideal $J$ of $I$ is said to be a {\bf reduction}
    of $I$ if there exists a nonnegative integer n such that 
    $JI^{n}=I^{n+1}$.

    This function returns true if $J$ is a reduction of $I$ and returns false
    if $J$ is not a subideal of $I$ or $J$ is a subideal but not a reduction of $I$.  
   Example
    S = ZZ/5[x,y]
    I = ideal(x^3,x*y,y^4)
    J = ideal(x*y, x^3+y^4)
    isReduction(I,J)
    isReduction(J,I)
    isReduction(I,I)
    g = I_0
    isReduction(I,J,g)
    isReduction(J,I,g)
    isReduction(I,I,g)

  SeeAlso
    minimalReduction
    reductionNumber
///


doc ///
  Key
    normalCone
    (normalCone, Ideal)
    (normalCone, Ideal, RingElement)
    
  Headline
    The normal cone of a subscheme
  Usage
    normalCone I
    normalCone(I,f)
  Inputs
    I:Ideal
    f:RingElement
      optional argument, if given it should be a non-zero divisor in the ideal I
  Outputs
    :Ring
      the ring $R[It] \otimes{} R/I$ of the normal cone of $I$
  Description
    Text
      The normal cone of an ideal $I\subset{} R$ is the ring $R/I \oplus{} I/I^2
      \oplus \ldots$, also called the associated graded ring of $R$ with
      respect to $I$.  If $S$ is the Rees algebra of $I$, then this ring is
      isomorphic to $S/IS$, which is how it is computed here.
  SeeAlso
    reesAlgebra
    associatedGradedRing
    normalCone
///




doc ///
  Key
    multiplicity
    (multiplicity, Ideal)
    (multiplicity, Ideal, RingElement)
    
  Headline
     Compute the Hilbert-Samuel multiplicity of an ideal
  Usage
     multiplicity I
     multiplicity(I,f)
  Inputs
    I:Ideal
    f:RingElement
      optional argument, if given it should be a non-zero divisor in the ideal I
  Outputs
    :ZZ
      the normalized leading coefficient of the Hilbert-Samuel polynomial of $I$
  Description
   Text
     Given an ideal $I\subset{} R$, ``multiplicity I'' returns the degree of the
     normal cone of $I$.  When $R/I$ has finite length this is the sum of the
     Samuel multiplicities of $I$ at the various localizations of $R$. When $I$
     is generated by a complete intersection, this is the length of the ring
     $R/I$ but in general it is greater. For example,
   Example
     R=ZZ/101[x,y]
     I = ideal(x^3, x^2*y, y^3)
     multiplicity I
     degree I
  Caveat
     The normal cone is computed using the Rees algebra, thus may be slow.
  SeeAlso
///

doc ///
  Key
    specialFiberIdeal
    (specialFiberIdeal, Module)
    (specialFiberIdeal, Ideal)
    (specialFiberIdeal, Module, RingElement)
    (specialFiberIdeal, Ideal, RingElement)
    [specialFiberIdeal, Jacobian]
    [specialFiberIdeal, Trim]
  Headline
     Special fiber of a blowup     
  Usage
     specialFiberIdeal M
     specialFiberIdeal(M,f)
  Inputs
     M:Module
       or @ofClass Ideal@
     f:RingElement
      a non-zerodivisor such that $M[f^{-1}]$ is a free module when $M$ is a module, an element in $M$ when $M$ is an ideal
  Outputs
     :Ideal
  Description
   Text
     Let $M$ be an $R = k[x_1,\ldots,x_n]/J$-module (for example an ideal), and
     let $mm=ideal vars R = (x_1,\ldots,x_n)$, and suppose that $M$ is a
     homomorphic image of the free module $F$. Let $T$ be the Rees algebra of
     $M$. The call specialFiberIdeal(M) returns the ideal $J\subset{} Sym(F)$
     such that $Sym(F)/J \cong{} T/mm*T$; that is, $specialFiberIdeal(M) =
     reesIdeal(M)+mm*Sym(F).$

     The name derives from the fact that $Proj(T/mm*T)$ is the special fiber of
     the blowup of $Spec R$ along the subscheme defined by $I$.
     
     With the default Trim => true, the computation begins by computing minimal generators,
     which may result in a change of generators of M
   Example
     R=QQ[a..h]
     M=matrix{{a,b,c,d},{e,f,g,h}}
     analyticSpread minors(2,M)
     specialFiberIdeal minors(2,M)
   Text
     If M is an n x n+1 matrix in n variables, and all generators have the 
     same degree d, with ell = n as expected,
     then the special fiber is a rational hypersurface of degree $D := d^n$, and
     the reduction number is D-1.
   Example
     n = 2
     x = symbol x
     S = ZZ/32003[x_1..x_n]
     M = matrix{{x_1,x_2,0},{0,x_1,x_2}}
     I = minors(n,M)
     specialFiber(I,I_0)  
  Caveat
     Special fiber is here defined to be the fiber of the blowup over the
     subvariety defined by the vars of the original ring. Note that if the
     original ring is a tower ring, this might not be the fiber over the
     closed point! To get the closed
     fiber, flatten the base ring first. 

  SeeAlso
     reesIdeal
///

doc ///
  Key
    specialFiber
    (specialFiber, Module)
    (specialFiber, Ideal)
    (specialFiber, Module, RingElement)
    (specialFiber, Ideal, RingElement)
    [specialFiber, Jacobian]
    [specialFiber, Trim]    
  Headline
     Special fiber of a blowup     
  Usage
     specialFiber M
     specialFiber(M,f)
  Inputs
     M:Module
       or @ofClass Ideal@
     f:RingElement
       an optional element, which is a non-zerodivisor such that $M[f^{-1}]$ is a free module when $M$ is a module, an element in $M$ when $M$ is an ideal
  Outputs
     :Ring
  Description
   Text
     Let $M$ be an $R = k[x_1,\ldots,x_n]/J$-module (for example an ideal), and
     let $mm=ideal vars R = (x_1,\ldots,x_n)$, and suppose that $M$ is a
     homomorphic image of the free module $F$ with $m+1$ generators. Let $T$ be the Rees algebra of
     $M$. The call specialFiber(M) returns the ideal $J\subset{} k[w_0,\dots,w_m]$
     such that $k[w_0,\dots,w_m]/J \cong{} T/mm*T$; that is, $specialFiber(M) =
     reesIdeal(M)+mm*Sym(F)$. This routine differs from @TO specialFiberIdeal@ in that
     the ambient ring of the output ideal is $k[w_0,\dots,w_m]$ rather than
     $R[w_0,\dots,w_m]$. 
     The coefficient ring $k$ used is always the 
     @TO ultimate@ @TO2 {coefficientRing, "coefficient ring"} @ of $R$.
     
     The name derives from the fact that $Proj(T/mm*T)$ is the special fiber of
     the blowup of $Spec R$ along the subscheme defined by $I$.

     With the default Trim => true, the computation begins by computing minimal generators,
     which may result in a change of generators of M
   Example
     R=QQ[a..h]
     M=matrix{{a,b,c,d},{e,f,g,h}}
     analyticSpread minors(2,M)
     specialFiber minors(2,M)
  SeeAlso
     reesIdeal
     specialFiberIdeal
///

doc ///
  Key
    analyticSpread
    (analyticSpread, Module)
    (analyticSpread, Ideal)
    (analyticSpread, Module, RingElement)
    (analyticSpread, Ideal, RingElement)
  Headline
     Compute the analytic spread of a module or ideal
  Usage
     analyticSpread M
     analyticSpread(M,f)
  Inputs
     M:Module
       or @ofClass Ideal@
     f:RingElement
       an optional element, which is a non-zerodivisor such that $M[f^{-1}]$ is a free module when $M$ is a module, an element in $M$ when $M$ is an ideal
  Outputs
     :ZZ
       the analytic spread of a module or an ideal $M$
  Description
   Text
     The analytic spread of a module is the dimension of its special fiber
     ring.  When $I$ is an ideal (and more generally, with the right
     definitions) the analytic spread of $I$ is the smallest number of
     generators of an ideal $J$ such that $I$ is integral over $J$. See for
     example the book Integral closure of ideals, rings, and modules. London
     Mathematical Society Lecture Note Series, 336. Cambridge University Press,
     Cambridge, 2006, by Craig Huneke and Irena Swanson.
   Example
     R=QQ[a..h]
     M=matrix{{a,b,c,d},{e,f,g,h}}
     analyticSpread minors(2,M)
     specialFiberIdeal minors(2,M)
     R=QQ[a,b,c,d]
     M=matrix{{a,b,c,d},{b,c,d,a}}
     analyticSpread minors(2,M)
     specialFiberIdeal minors(2,M)
  SeeAlso
     specialFiberIdeal
     reesIdeal
///

doc ///
  Key
    distinguished
    (distinguished, RingMap, Ideal)
    (distinguished, Ideal, Ideal)
    (distinguished, Ideal)    
  Headline
     Compute the distinguished subvarieties of a pullback, intersection or cone
  Usage
     L = distinguished(f,I)
     L = distinguished(I,J)
     L = distinguished(I)     
  Inputs
    f:RingMap
    I:Ideal
    J:Ideal
  Outputs
    L:List
  Description
   Text
     Suppose that f:S\to R is a map of rings, and I is an ideal of S.
     Let K be the kernel of the map of associated graded rings
     gr_I(S) \to gr_(fI)R.

     The distinguished primes p_i in S/I are the intersections of the
     minimal primes P_i over K with S/I \subset{} gr_IS, that is,
     the minimal primes of the support in R/I of the normal cone of f(I).
     The multiplicity associated
     with p_i is by definition the multiplicity of P_i in the primary
     decomposition of K.
     
     Distinguished subvarieties and their multiplicity
     (defined by the distinguished primes, usually
     in the global case of a quasi-projective variety and its sheaf of rings) 
     play a central role in the Fulton-MacPherson
     construction of refined intersection products. See William Fulton, Intersection Theory,
     Section 6.1 for the geometric context and the general case, and the explanation
     in the article Rees Algebras in JSAG (submitted).
     
     This application is illustrated in the code for @TO intersectInP@.
     
     We allow the special cases
     
     {\tt distinguished(I,J) := distinguished(f,I)}, with f:S\to S/J the projection
     
     and
     
     {\tt distinguished(I) := distinguished(f,I)}, with f:S\to S the identity.
          
     which computes the distinguished primes in the support of the normal cone gr_IS
     itself.
     An interesting application is given in the paper

     ``A geometric effective Nullstellensatz,''
     Invent. Math. 137 (1999), no. 2, 427--448 by
     Ein and Lazarsfeld.
     
     Here is an example showing that associated primes need not be distinguished primes:
   Example
     R = ZZ/101[a,b]
     I = ideal(a^2, a*b)
     ass I 	 
   Text
     There is one minimal associated prime (a thick line in $P^3$) and one
     embedded prime.
   Example
     distinguished I
     intersectInP(I,I)
  SeeAlso
   intersectInP
   saturate
///

doc ///
   Key
    intersectInP
    (intersectInP, Ideal, Ideal)
   Headline
    Compute distinguished varieties for an intersection in A^n or P^n
   Usage
    L = intersectInP(I,J)
   Inputs
    I:Ideal
     of a polynomial ring P over a field
    J:Ideal
     of the same ring
   Outputs
    L:List
   Description
    Text
     This function applies the technology of @TO distinguished @ to compute
     the distinguished subvarieties, with their multiplicities, for an intersection
     in affine or projective space. The function @TO distinguished @ is actually applied
     to the diagonal ideal in P**P and the ideal I**P + P**I, and the answer is
     pulled back to P.
    Example
     kk = ZZ/101
     P = kk[x,y]
     I = ideal"x2-y";J=ideal y
     intersectInP(I,J)
     I = ideal"x4+y3+1"
     intersectInP(I,J)
     I = ideal"x2y";J=ideal"xy2"
     intersectInP(I,J)
     intersectInP(I,I)
    Text
     Note that in the last two cases, which are improper intersections of
     two cubics, the total multiplicity is 9 = 3*3. But this is not always the case
     (in the actual definition of the intersection product, the multiplicity is
     multiplied by the class of a certain cycle supported on the distinguished subvariety).
    Example
     I = ideal"y-x2"
     intersectInP(I,I)
   Caveat
   SeeAlso
    distinguished
///

doc ///
   Key
    [intersectInP,BasisElementLimit]
    [intersectInP,DegreeLimit]
    [intersectInP,MinimalGenerators]
    [intersectInP,PairLimit]
    [intersectInP,Strategy]
    [intersectInP,Variable]
    [multiplicity,Variable]
   Headline
    Option for intersectInP
   Description
    Text
     see the options for @TO saturate@.
   SeeAlso
    intersectInP
    distinguished
    saturate
///



doc ///
  Key
    minimalReduction
    (minimalReduction, Ideal)
    
  Headline
    Find a minimal reduction of an ideal
  Usage
    J = minimalReduction I
  Inputs
    I:Ideal
  Outputs
    :Ideal
      A minimal reduction of I (defined below)
  Description
    Text
      {\tt minimalReduction} takes an ideal I that is homogeneous or inhomogeneous
      (in the latter case the ideal is to be regarded as an ideal in the 
      localization of the polynomial ring at the origin.). It returns an ideal $J$
      contained in $I$, with a minimal number of generators 
      such that $I$ is integrally dependent on $J$. This minimal number is called
      the analyticSpread of $I$.
     
      This routine is probabilistic: $J$ is computed as the ideal generated by the
      right number of random linear combinations of the generators of $I$. However, the
      routine checks rigorously that the output ideal is a reduction, and tries
      probabilistically again if it is not. If it cannot find a minimal reduction after
      a certain number of tries, it returns an error. The number of tries defaults
      to 20, but can be set with the optional argument @TO Tries@.

      To say that $I$ is integrally dependent on $J$ means that
      $JI^k = I^{k+1}$ for some non-negative integer $k$.  The smallest $k$ with this
      property is called the reduction number of $I$, and can be computed
      with @TO reductionNumber@ i.

      See the book 
      Huneke, Craig; Swanson, Irena: Integral closure of ideals, rings, and modules. 
      London Mathematical Society Lecture Note Series, 336. Cambridge University Press, Cambridge, 2006.
      for further information.
    Example
      kk = ZZ/101;
      S = kk[a..c];
      m = ideal vars S;
      i = (ideal"a,b")*m+ideal"c3"
      analyticSpread i
      minimalReduction i
    Text
      Note that this is inhomogeneous--
      it is generated by 3 random linear combinations
      of the generators of i.
      There is no homogeneous ideal with just 3 generators
      on which i is integrally dependent.
    Example
      f = gens i
      for a from 0 to 3 do(jhom:=ideal (f*random(source f, S^{3-a:-2,a:-3})); print(i^6 == (i^5)*jhom))
  Caveat
     It is possible that the ideal returned is not a minimal reduction,
     due to the probabilistic nature of the routine.  This will be addressed in a future version
     of the package.  The larger the size of the base field, the less likely this is to happen.
  SeeAlso
    analyticSpread
    reductionNumber
    whichGm
///

doc ///
  Key
    reductionNumber
    (reductionNumber, Ideal, Ideal)
  Headline
    Reduction number of one ideal with respect to another
  Usage
    k = reductionNumber(I,J)
  Inputs
    I:Ideal
    J:Ideal
  Outputs
    :ZZ
      the reduction number of $I$ (defined below)
  Description
    Text
      The function {\tt reductionNumber} takes a pair of ideals $I,J$, homogeneous or inhomogeneous
      (in the latter case $I$ and $J$ are to be regarded as ideals in the 
      localization of the polynomial ring at the origin.).
      The ideal $J$ must be a reduction of $I$ (that is, $J\subset{} I$
      and $I$ is integrally dependent on $J$. This condition is checked by
      the function @TO isReduction@. It returns the smallest integer $k$ such that
      $JI^k = I^{k+1}$.
      
      For further information, see the book:
      Huneke, Craig; Swanson, Irena: Integral closure of ideals, rings, and modules, 
      London Mathematical Society Lecture Note Series, 336. Cambridge University Press, 
      Cambridge, 2006.
    Example
      setRandomSeed()
      kk = ZZ/32003;
      S = kk[a..c];
      m = ideal vars S;
      i = (ideal"a,b")*m+ideal"c3"
      analyticSpread i
      j=minimalReduction i
      reductionNumber (i,j)
  Caveat
     It is possible for the routine to not finish in reasonable time, due to the
     probabilistic nature of the routine.  What happens is that 
     the routine @TO minimalReduction@ occasionally, but rarely, returns an ideal
     which is not a minimal reduction.  In this case, the routine goes into an infinite loop.
     This will be addressed in a future version
     of the package.  In the meantime, simply interrupt the routine, and restart the
     computation.
  SeeAlso
    analyticSpread
    minimalReduction
    whichGm
///

doc ///
  Key
    whichGm
    (whichGm, Ideal)
  Headline
    Largest Gm satisfied by an ideal
  Usage
    whichGm I
  Inputs
    I:Ideal
  Outputs
    :ZZ
      what it does
  Description
    Text
      An ideal $I$ in a ring $S$ is said to satisfy the condition $G_m$ if, for every prime ideal $P$ of
      codimension $0<k<m$, the ideal $I_P$ in $S_P$ can be generated by at most $k$ elements. 
      
      The command {\tt whichGm I}
      returns the largest $m$ such that $I$ satisfies $G_m$, or infinity if $I$ satisfies $G_m$ 
      for every $m$.

      This condition arises frequently in work of Vasconcelos and Ulrich and their schools
      on Rees algebras and powers of ideals. See for example
      Morey, Susan; Ulrich, Bernd:
      Rees algebras of ideals with low codimension.  
      Proc. Amer. Math. Soc.  124  (1996),  no. 12, 3653--3661.
    Example
      kk=ZZ/101;
      S=kk[a..c];
      m=ideal vars S
      i=(ideal"a,b")*m+ideal"c3"
      whichGm i
  SeeAlso
    analyticSpread
    minimalReduction
    reductionNumber
///

doc ///
   Key
    jacobianDual
    (jacobianDual, Matrix)
    (jacobianDual, Matrix, Matrix, Matrix)
   Headline
    Computes the 'jacobian dual', part of a method of finding generators for Rees Algebra ideals
   Usage
    psi = jacobianDual phi
    psi = jacobianDual(phi, X, T)
   Inputs
    phi:Matrix
     presentation matrix of an ideal
    X:Matrix
     row matrix generating an ideal that contains the entries of phi
    T:Matrix
     row matrix of variables that will be generators of the Rees algebra of I
   Outputs
    psi:Matrix
     the `Jacobian Dual', which satisfies $T*phi = X*psi$
   Description
    Text
     Let I be an ideal of R and let phi be the presentation matrix of I as a module.
     The symmetric algebra of I has the form 
     
     $Sym_R(I) = R[T_0..T_m]/ideal(T*phi)$
     
     where the T_i correspond to the generators of I. If $X = matrix\{\{x_1..x_n\}\}$,
     with x_i \in{} R, and ideal X contains the entries of the matrix phi, then there is 
     a matrix psi defined over R[T_0..T_m], called the Jacobian Dual of phi with respect to X,
     such that $T*phi = X*psi$. (the matrix psi is generally
     not unique; Macaulay2 computes it using Groebner division with remainder.)
     
     In the form {\tt psi = jacobianDual phi},
     a new ring ST := S[T_0..T_m] is created, and the vector X is set to the variables
     of R. The result is returned as a matrix over ST. 
     To do the computation in a ring previously defined computed, 
     use the form {\tt psi = jacobianDual(phi, X,T)};
     in this case, the matrices phi, X, T should all be defined over the
     same ring ST, 
     the matrix T should be a row of variables of ST, and
     the matrix phi should have entries in a subring not involving the entries of T.
      
     If I is an ideal of grade >=1 and ideal X contains a nonzerodivisor of R
     (which will be automatic if I has finite projective dimension) then
     ideal X has grade >= 1 on the Rees algebra. Since ideal(T*phi) is contained in the
     defining ideal of the Rees algebra, the vector X is annihilated by the matrix
     psi when regarded over the Rees algebra. If also the number of relations of I
     is >= the number of generators of I, this implies that the maximal minors of
     psi annihilate  the x_i as elements of the Rees algebra, and thus that the maximal
     minors of psi are inside the ideal of the Rees algebra. In very favorable circumstances,
     one may even have the equality reesIdeal I = ideal(T*phi)+ideal minors(psi): For example:
     
     Theorem (S. Morey and B. Ulrich, Rees Algebras of Ideals with Low Codimension, Proc. Am. Math.
     Soc. 124 (1996) 3653--3661):
     Let R be a local Gorenstein ring with infinite residue field, let I be a perfect ideal
     of grade 2 with n generators, and let phi be the presentation matrix of I. Let
     ell = ell(I) be the analytic spread. Suppose that
     I satisfies the condition G_{ell} or, equivalently, that the n-p sized minors of phi 
     have codimension >p for 1<= p < ell. The following conditions are equivalent:
     
     1) reesAlgebra I is Cohen-Macaulay and I_(n-ell)(phi) = I_1(phi)^{n-ell}
     2) reductionNumber I < ell and I_(n+1-ell)(phi) = I_1(phi)^{n+1-ell}
     3) reesIdeal I = symmetricAlgebraIdeal I + minors(n, jacobianDual phi)
     
     We start with the presentation matrix phi of an (n+1)-generator perfect ideal
     Such that the first row consists of the n
     variables of the ring, and the rest of whose rows are reasonably general (in this
     case random quadrics):
    Example
     setRandomSeed 0
     n=3;
     kk = ZZ/101;
     S = kk[a_0..a_(n-2)];
     phi' = map(S^(n),S^(n-1), (i,j) -> if i == 0 then a_j else random(2,S));
     I = minors(n-1,phi');
     betti (F = res I)
     phi = F.dd_2;
     jphi = jacobianDual phi
    Text
     We first compute the analytic spread ell and the reduction number r
    Example
     ell = analyticSpread I
     r = reductionNumber(I, minimalReduction I)
    Text
     Now we can check the condition G_{ell}, first probabilistically
    Example
     whichGm I >= ell
    Text
     and now deterministically
    Example
     apply(toList(1..ell-1), p-> {p+1, codim minors(n-p, phi)})
    Text
     We now check the three equivalent conditions of the Morey-Ulrich Theorem.
     Note that since ell = n-1 in this case, the second part of conditions
     1,2 is vacuously satisfied, and since r<ell,
     the conditions must all be satisfied.
     We first check that reesAlgebra I is Cohen-Macaulay:
    Example
     reesI = reesIdeal I;
     codim reesI
     betti res reesI
    Text
     Finally, we wish to see that reesIdeal I is generated by the ideal 
     of the symmetric algebra together with the jacobian dual:
    Example
     psi = jacobianDual phi
    Text
     We now compute the ideal J of the symmetric algebra; the call symmetricAlgebra I
     would return the ideal over a different ring, so we do it by hand:
    Example
     ST = ring psi
     T = vars ST
     J = ideal(T*promote(phi, ST))
     betti res J
     J1 = minors(ell, psi)
     betti (G = res trim (J+J1))
     betti res reesIdeal I
    Text
     The name Jacobian Dual comes from the case where phi is a matrix of linear forms
     the x_i are the variables of R, and the generators of I are forms, all of the same degree D;
     in this case Euler's formula sum(df_i/dx_j*xj) = Df can be used to express the
     entries of psi in terms of the derivatives of the entries of phi, at least when
     the degrees of the columns of phi are nonzero in the coefficient field.
     
     Explicitly, let x_1,...,x_n be the variables of R, and let phi be a presentation matrix for I.
     Since all the f_i have the same degree, if follows that,
     for each j, the entries phi_(i,j) will all have the same degree, say D_j = deg phi_(i,j).
     Let ST be the polynomial ring R[T_0..T_m], where the T_i correspond to f_i, and let
     X=matrix{{x_1,...,x_n}}, and T=matrix{{T_0,...,T_m}} be row matrices over ST.
     In this case, by Euler's formula, we may take
     
     psi_{k,j}=(1/D_j)*sum_i(d phi_{i,j}/d x_k*T_i),
   Caveat
     The division with
     remainder step is usually fast, but if this
     ever becomes a bottleneck it would be possible to test for the degree condition and
     use Euler's formula in the case where it applies.
   SeeAlso
    whichGm
    expectedReesIdeal
    reesAlgebra
    reesAlgebraIdeal
    reesIdeal
    specialFiberIdeal
///
doc ///
  Key
    [symmetricKernel, Variable]
    [reesIdeal, Variable]
    [reesAlgebra, Variable]
    [associatedGradedRing, Variable]
    [specialFiberIdeal, Variable]
    [specialFiber, Variable]
    [distinguished, Variable]
    [isReduction, Variable]
    [jacobianDual, Variable]

  Headline
    Choose name for variables in the created ring
  Usage
    symmetricKernel(...,Variable=>w)
    reesIdeal(...,Variable=>w)
    reesAlgebra(...,Variable=>w)
    specialFiberIdeal(...,Variable=>w)
    specialFiber(...,Variable=>w)    
    distinguished(...,Variable=>w)
    isReduction(...,Variable=>w)
    jacobianDual(...,Variable=>w)

  Description
    Text
      Each of these functions creates a new ring of the form R[w_0,\ldots, w_r]
      or R[w_0,\ldots, w_r]/J, where R is the ring of the input ideal or module
      (except for @TO specialFiber@, which creates a ring $K[w_0,\ldots, w_r]$, 
      where $K$ is the ultimate coefficient ring of the input ideal or module.)
      This option allows the user to change the names of the new variables in this ring.
      The default variable is w.
    Example
      R = QQ[x,y,z]/ideal(x*y^2-z^9)
      J = ideal(x,y,z)
      I = reesIdeal(J, Variable => p)
    Text
      To lift the result to an ideal in a flattened ring, use @TO flattenRing@:
    Example
      describe ring I
      I1 = first flattenRing I
      describe ring oo      
    Text
      Note that the rings of I and I1 both have bigradings. Use @TO newRing@ to
      make a new ring with different degrees.
    Example
      S = newRing(ring I1, Degrees=>{numgens ring I1:1})
      describe S
      I2 = sub(I1,vars S)
      res I2
  SeeAlso
    flattenRing
    newRing
    substitute
///

doc ///
   Key
    [reesIdeal, Strategy]
    [reesAlgebra,Strategy]
    [isLinearType,Strategy]
    [isReduction, Strategy]    	  
    [normalCone, Strategy]    	  
    [multiplicity, Strategy]    	  
    [specialFiberIdeal, Strategy]    	  
    [specialFiber, Strategy]    	  
    [analyticSpread, Strategy]    	  
    [distinguished,Strategy]
    [minimalReduction, Strategy]    	  
   Headline
    Choose a strategy for the saturation step
   Usage
    reesIdeal(...,Strategy => X)
   Description
    Text
     where X is is one of @TO Iterate@, @TO Linear@, @TO Bayer@, @TO Eliminate@.
     These are described in the documentation node for @TO saturate@.
     
     The Rees algebra S(M) of a submodule M of a free module (most importantly,
     an ideal in the ring), is equal to the symmetric algebra Sym_k(M) mod torsion.
     computing this torsion is the slow link in most of the programs in this
     package. The fastest way to compute it is usually by saturating the ideal
     defining the symmetric algebra with respect
     to an element in that ideal.
   SeeAlso
    reesIdeal
    reesAlgebra
    isLinearType
    isReduction
    normalCone
    multiplicity
    specialFiberIdeal
    specialFiber
    analyticSpread
    distinguished
    minimalReduction
    saturate
///
doc ///
   Key
    [reesIdeal, PairLimit]
    [minimalReduction, PairLimit]
    [distinguished,PairLimit]
    [analyticSpread, PairLimit]
    [specialFiber, PairLimit]
    [specialFiberIdeal, PairLimit]
    [multiplicity, PairLimit]
    [normalCone, PairLimit]
    [isReduction, PairLimit]
    [isLinearType,PairLimit]
    [reesAlgebra,PairLimit]
   Headline
    Bound the number of s-pairs considered in the saturation step
   Usage
    reesIdeal(...,PairLimit => X)
   Description
    Text
     Here X is a positive integer. Each of these functions computes the Rees
     Algebra using a saturation step, and the optional argument causes the saturation
     process to stop after that number of s-pairs is found.
     This is described in the documentation node for @TO saturate@.
   SeeAlso
    reesIdeal
    reesAlgebra
    isLinearType
    isReduction
    normalCone
    multiplicity
    specialFiberIdeal
    specialFiber
    analyticSpread
    distinguished
    minimalReduction
    saturate
///
doc ///
   Key
    [reesIdeal, MinimalGenerators]
    [minimalReduction, MinimalGenerators]
    [distinguished,MinimalGenerators]
    [analyticSpread, MinimalGenerators]
    [specialFiber, MinimalGenerators]
    [specialFiberIdeal, MinimalGenerators]
    [multiplicity, MinimalGenerators]
    [normalCone, MinimalGenerators]
    [isReduction, MinimalGenerators]
    [isLinearType,MinimalGenerators]
    [reesAlgebra,MinimalGenerators]
   Headline
    Whether the saturation step returns minimal generators
   Usage
    reesIdeal(...,MinimalGenerators => X)
   Description
    Text
     Here X is of type boolean. Each of these functions involves the
     computation of a Rees algebra, which may involve a saturation step.
     This optional argument determines whether or not 
     the output of the saturation step will be forced to have a minimal generating set.
     This is described in the documentation node for @TO saturate@.
   SeeAlso
    reesIdeal
    reesAlgebra
    isLinearType
    isReduction
    normalCone
    multiplicity
    specialFiberIdeal
    specialFiber
    analyticSpread
    distinguished
    minimalReduction
    saturate
///
doc ///
   Key
    [minimalReduction, BasisElementLimit]
    [reesIdeal, BasisElementLimit]
    [distinguished,BasisElementLimit]
    [analyticSpread, BasisElementLimit]
    [specialFiber, BasisElementLimit]
    [multiplicity, BasisElementLimit]
    [normalCone, BasisElementLimit]
    [isReduction, BasisElementLimit]
    [isLinearType,BasisElementLimit]
    [reesAlgebra,BasisElementLimit]
    [specialFiberIdeal, BasisElementLimit]
   Headline
    Bound the number of Groebner basis elements to compute in the saturation step
   Usage
    reesIdeal(...,BasisElementLimit => X)
   Description
    Text
     Here X is a positive integer. Each of these functions computes the Rees
     Algebra using a saturation step, and the optional argument causes the saturation
     process to stop after that number of s-pairs is found.
     This is described in the documentation node for @TO saturate@.
   SeeAlso
    reesIdeal
    reesAlgebra
    isLinearType
    isReduction
    normalCone
    multiplicity
    specialFiberIdeal
    specialFiber
    analyticSpread
    distinguished
    minimalReduction
    saturate
///
doc ///
   Key
    [reesIdeal, DegreeLimit]
    [minimalReduction, DegreeLimit]
    [distinguished,DegreeLimit]
    [analyticSpread, DegreeLimit]
    [specialFiber, DegreeLimit]
    [normalCone, DegreeLimit]
    [multiplicity, DegreeLimit]
    [isReduction, DegreeLimit]
    [isLinearType,DegreeLimit]
    [reesAlgebra,DegreeLimit]
    [specialFiberIdeal, DegreeLimit]
   Headline
    Bound the degrees considered in the saturation step. Defaults to infinity
   Usage
    reesIdeal(...,DegreeLimit => X)
   Description
    Text
     where X is a non-negative integer. Stop computation at degree X.
     This is described in the documentation node for @TO saturate@.
     Here X is a positive integer. Each of these functions computes the Rees
     Algebra using a saturation step, and the optional argument causes the saturation
     process to stop after that number of s-pairs is found.
     This is described in the documentation node for @TO saturate@.
   SeeAlso
    reesIdeal
    reesAlgebra
    isLinearType
    isReduction
    normalCone
    multiplicity
    specialFiberIdeal
    specialFiber
    analyticSpread
    distinguished
    minimalReduction
    saturate
///


doc ///
   Key
    expectedReesIdeal
    (expectedReesIdeal, Ideal)
    (expectedReesIdeal, Module)    
   Headline
    symmetric algebra ideal plus jacobian dual
   Usage
    J = expectedReesIdeal M
   Inputs
    M:Ideal
    M:Module
   Outputs
    J:Ideal
   Description
    Text
     Let M be an R-module with g generators and free presentation phi: R^h \to R^g. The symmetric algebra of M
     can be written as R[T_1,\dots,T_g]/J, where J is the ideal generated by the entries of the 1 x h matrix
     T*m, where T = (T_1..T_g). If the entries of m are all contained in an ideal (X_1..X_n) (for example, when
     m is a minimal presentation and the X_i generate the maximal ideal, there is a matrix psi: R[Z]^h \to R[Z]^n
     such that T*phi = X*psi. Under reasonable hypotheses (eg when R is a domain) the relation
     X*psi = 0 in the Rees algebra implies that the n x n minors of psi are 0. Thus these minors lie in the ideal
     defining the Rees algebra. The expectedReesIdeal is the sum of the ideals (T*phi) and the ideal of nxn minors of psi.
     Under particularly good circumstances this sum is known to be equal to the ideal of the Rees algebra. More generally,
     it may speed computations of @TO reesIdeal@ to start with this sum rather than with the ideal T*phi, as in the following
     example. (This can be turned off with the Jacobian=>false option.)
    
     The term 'Expected Rees Ideal' for the sum of 
     of the ideal of the symmetric algebra of I with
     the ideal of maximal minors of the Jacobian dual matrix of a presentation of I
     is derived from the paper 
     "Rees Algebras of Ideals of Low Codimension", Proc. Am. Math. Soc. 1996
     of Colley and Ulrich. Building on the paper 
     "Ideals with Expected Reduction Number", Am. J. Math 1996,
     they prove that this ideal is in fact equal to the 
     ideal of the Rees algebra of I when I is a codimension 2 perfect ideal whose
     Hilbert-Burch matrix has a special form. See @TO jacobianDual@ for an example.
    Example
     setRandomSeed 0
     n = 5
     S = ZZ/101[x_0..x_(n-2)];
     M1 = random(S^(n-1),S^{n-1:-2});
     M = M1||vars S
     I = minors(n-1, M);
     time rI = expectedReesIdeal I; -- n= 5 case takes < 1 sec.
     --time rrI = reesIdeal(I,I_0); -- n = 5 case ~20 sec
     --time rrrI = reesIdeal I; -- n = 4 case > 1 minute; I didn't wait to see!
     --assert(rI == (map(ring rI, ring rrI, vars ring rI)) rrI)
     kk = ZZ/101;
     S = kk[x,y,z];
     m = random(S^3, S^{4:-2});
     I = minors(3,m);
     time reesIdeal (I, I_0);
     time reesIdeal (I, I_0, Jacobian =>false);
   SeeAlso
    symmetricAlgebraIdeal
    jacobianDual
///

///     


///

///
  uninstallPackage "ReesAlgebra"
  restart
  installPackage "ReesAlgebra"
  check "ReesAlgebra"
  viewHelp PlaneCurveSingularities

restart
loadPackage("ReesAlgebra", Reload=>true)
///

doc ///
   Key
    PlaneCurveSingularities
   Headline
    Using the Rees Algebra to resolve plane curve singularities
   Description
    Text
     The Rees Algebra of an ideal I appeared classically as
     the bihomogeneous coordinate ring of the
     blow up of the ideal I, used in resolution of singularities. Though the general
     case is still out of reach, we illustrate with some simple examples of plane
     curve singularities. 
     
     First the cusp in the affine plane
    Example
     R = ZZ/32003[x,y]
     cusp = ideal(x^2-y^3)
     mm = radical ideal singularLocus cusp
    Text
     The cusp is singular at the maximal ideal (x,y), so we blow that up,
     and examine the ``total transform'', that is, the ideal generated by the 
     x^2-y^3 in the Rees algebra. 
    Example
     B = first flattenRing reesAlgebra(mm)
    Text
     Application of {\tt first flattenRing} serves to make B a quotient of the polynomial
     ring T in 4 variables; otherwise it would be a quotient of R[w_0,w_1], which
     Macaulay2 treats as a polynomial ring in 2 variables, and the calculation of
     the singular locus later on would be wrong.
    Example
     vars B
     proj = map(B,R,{x,y})
     totalTransform = proj cusp
     D = decompose totalTransform 
     D/codim
    Text
     We see that the reduced preimage consists of two codimension 1 components,
     the `exceptional divisor', which is the pullback of the point we blew up, (x,y),
     and the `strict transform'.
     The two components meet in a double point in the 2 dimensional variety
     B \subset{} A^2\times P^1. We have to saturate with respect to the irrelevant
     ideal to understand what's going on.
    Example
     irrelB = ideal(B_0,B_1)
     doublePoint = saturate(D_0+D_1, irrelB)
     codim doublePoint
     degree doublePoint
    Text
     We can see the multiplicities of these components by comparing their degrees to the
     degrees of the reduced components
    Example
     divisors = primaryDecomposition totalTransform
     strictTransform = divisors_0
     exceptional = divisors_1
     divisors/(i-> degree i/degree radical i)
    Text
     That is, the exceptional component occurs with multiplicity 2 (in general we'd 
     get the exceptional component with multiplicity equal to the multiplicity of the
     singular point we blew up.)
     
     We next investigate the singularity of the strict transform. We want to see
     it as a curve in P^1 x A^2, that is, as an ideal of  T = kk[w_0,w_1,x,y]
    Example
     T = ring ideal B
     irrelT  = ideal(w_0,w_1)
     sing = saturate(ideal singularLocus strictTransform, irrelT)
    Text
     We see that the singular locus of the strict transform is empty; that is, the curve is smooth.

     We could have made the computation in B as well:
    Example
     jacobianMatrix = diff(vars B, transpose gens strictTransform)
     codim strictTransform
     jacobianIdeal  = strictTransform+ minors(1,jacobianMatrix)
     sing1 = saturate(jacobianIdeal, irrelB)

    Text
     Next we look at the desingularization of a tacnode; it will take two blowups.
    Example
     R = ZZ/32003[x,y]
     tacnode = ideal(x^2-y^4)
     sing = ideal singularLocus tacnode
     mm = radical sing
     B1 = first flattenRing reesAlgebra mm
     proj1 = map(B1,R,{x,y})
     irrelB1 = ideal(w_0,w_1)
     totalTransform1 = proj1 tacnode
     netList (D1 = decompose totalTransform1)
     strictTransform1 = saturate(totalTransform1,proj1 mm )
    Text
     Here proj1 mm is the ideal of the exceptional divisor. 
     The strict transform is, by definition, obtained by saturating it away,
     The strict transform of the tacnode is not yet smooth: it consists of
     two smooth branches, meeting transversely at a point:
    Example
     strictTransform1 == intersect(D1_1,D1_2)
     degree (D1_1+D1_2)
    Text
     We compute the singular point of the strict transform:
    Example
     mm1 = sub(radical ideal singularLocus strictTransform1, B1)
    Text
     ...and blow up B1, getting a variety in P^2 x P^1 x A^2
    Example
     B2 = first flattenRing reesAlgebra(mm1, Variable => p)
     vars B2
     proj2 = map(B2,B1,{w_0,w_1,x,y})
     irrelB2 = ideal(p_0,p_1,p_2)
     irrelTot = (proj2 irrelB1) *irrelB2
     totalTransform2 = saturate(proj2 proj1 tacnode, irrelTot)
     exceptional2 = saturate(proj2 proj1 mm, irrelTot)
     netList(D2 = decompose totalTransform2)
     netList(E2 = decompose exceptional2)
     strictTransform2 = saturate(totalTransform2, exceptional2)
    Text 
     We compute the singular locus once again:
    Example
     time sing2 = ideal singularLocus strictTransform2;
     saturate(sing2, sub(irrelTot, ring sing2))
    Text
     The answer, {\tt ideal 1} shows that the second blowup desingularizes the tacnode.
    Text
     It is not necessary to repeatedly blow up closed points: there is always a 
     single ideal that can be blown up to desingularize 
     (Hartshorne, Algebraic Geometry,Thm II.7.17).
     In this case, blowing-up (x,y^2) desingularizes the tacnode x^2-y^4 in a single step.
    Example
     R = ZZ/32003[x,y];
     tacnode = ideal(x^2-y^4);
     mm = ideal(x,y^2);
     B = first flattenRing reesAlgebra mm;
     irrelB = ideal(w_0,w_1);
     proj = map(B,R,{x,y});
     totalTransform = proj tacnode
     netList (D = decompose totalTransform)
     exceptional = proj mm
     strictTransform = saturate(totalTransform, exceptional);
     netList decompose strictTransform
     sing0 = sub(ideal singularLocus strictTransform, B);
     sing = saturate(sing0,irrelB)
    Text
     So this single blowup is already nonsingular.
 ///

///
  uninstallPackage "ReesAlgebra"
  restart
  installPackage "ReesAlgebra"
  check "ReesAlgebra"
  viewHelp ReesAlgebra
///

-----TESTS-----
TEST///
--TEST for jacobianDual
setRandomSeed 0
     d=2
     S = ZZ/101[a_0..a_(d-1)]
     kk = ZZ/101
     mlin = transpose vars S
     mquad = random(S^d, S^{-1,-4,d-2:-2})
     Irand = minors(d,mlin|mquad)
     X = vars S
     phi = syz gens Irand;
     psi = jacobianDual phi

     T = symbol T
     ST = kk[T_0..T_d, x_0..x_(d-1)] 
     X = matrix{toList(x_0..x_(d-1))}
     Ts = matrix{{T_0,T_1..T_d}}
     phi1 = (map(ST,S,X)) phi
     psi1 = jacobianDual(phi1, X, Ts)
     f = map(ST, ring psi, vars ST)
     assert(f psi - psi1 == 0)
     m = matrix {{-15*T_1-8*T_2, T_0*x_0^3+14*T_0*x_0*x_1^2-24*T_0*x_1^3+18*T_2},
      {T_0*x_0^3-16*T_0*x_0^2*x_1+2*T_0*x_0*x_1^2+32*T_0*x_1^3+45*T_1+40*T_2,
      -11*T_0*x_1^3-11*T_1+43*T_2}}
     f psi - m
///

TEST///
--test for expectedReesIdeal
     setRandomSeed 0
     n = 3
     S = ZZ/101[x_0..x_(n-2)];
     M1 = random(S^(n-1),S^{n-1:-2});
     M = M1||vars S
     I = minors(n-1, M);
     time rI = expectedReesIdeal I
     time rrI = reesIdeal I;
     time rrI = reesIdeal(I,I_0); -- ~20 sec
     assert(betti rrI == betti rI)     
///

///
restart
uninstallPackage "ReesAlgebra"
installPackage "ReesAlgebra"
check "ReesAlgebra"
///

TEST///
--TEST for versalEmbedding
p=3
S=ZZ/p[x,y,z]
R=S/((ideal(x^p,y^p))+(ideal(x,y,z))^(p+1))
i=module ideal(z)
ui=versalEmbedding i
assert(kernel ui == ideal(0_R))
inci=map(R^1,i,matrix{{z}})
assert(kernel inci == 0)
gi=map(R^2, i, matrix{{x},{y}})
assert(kernel gi == 0)
u= map(R^3,R^{-1},ui)
inc=map(R^1, R^{-1}, matrix{{z}})
g=map(R^2, R^{-1}, matrix{{x},{y}})
A=symmetricKernel u
B1=symmetricKernel inc
B=(map(ring A, ring B1)) B1
C1 = symmetricKernel g
C=(map(ring A, ring C1)) C1
assert((A==B)==true)
assert((A==C)==false)
///

--- A very basic tests of reesIdeal - a few more after this. 
TEST///
S=ZZ/101[x,y]
i=ideal"x5,y5, x3y2"
V1 = reesIdeal(i)
use ring V1
assert(V1 == ideal(x^2*w_1-y^2*w_2,y*w_1^2-x*w_0*w_2,x^3*w_0-y^3*w_1,x*w_1^3-y*w_0*w_2^2,w_1^5-w_0^2*w_2^3))
V2 = reesIdeal(i,i_0)
use ring V2
assert(V2 == ideal(x^2*w_1-y^2*w_2,y*w_1^2-x*w_0*w_2,x^3*w_0-y^3*w_1,x*w_1^3-y*w_0*w_2^2,w_1^5-w_0^2*w_2^3))
///

-- 3 very simple tests.  The first tests just reesIdeal, the second
-- just reesAlgebra and the third tests both. 
TEST///
S = ZZ/101[x,y]
M = module ideal(x,y)
V = reesIdeal M
use ring V
assert(V == ideal (-w_0*y+w_1*x))
use S
M = module (ideal(x,y))^2
R = reesAlgebra M
assert(numgens R + numgens coefficientRing R == 5)
use ambient R
assert(ideal R == ideal (-w_1*y+w_2*x, -w_0*y + w_1*x, w_1^2 - w_0*w_2))
use S
M = module (ideal (x,y))^3
V = reesIdeal M
use ring V
assert(V == ideal (-w_2*y+w_3*x,-w_1*y+w_2*x,-w_0*y+w_1*x,w_2^2-w_1*w_3,w_1*w_2-w_0*w_3,w_1^2-w_0*w_2))
R = reesAlgebra M
assert(numgens R + numgens coefficientRing R == 6)
use ambient R
assert(ideal R == ideal (-w_2*y+w_3*x,-w_1*y+w_2*x,-w_0*y+w_1*x,w_2^2-w_1*w_3,w_1*w_2-w_0*w_3,w_1^2-w_0*w_2))
///

--- Checking that the two methods for getting a Rees Ideal yields the
--- same answer.  This is now an example as well. 
TEST///
x = symbol x
S=ZZ/101[x_0..x_4]
i=monomialCurveIdeal(S,{4,5,6,7})
M1 = gens gb reesIdeal i; 
M2 = gens gb reesIdeal(i,i_0);
M1 = substitute(M1, ring M2);
assert(M2 == M1)
///

///
restart
loadPackage ("ReesAlgebra", Reload =>true)
S=ZZ/101[x_0..x_4]
i=monomialCurveIdeal(S,{5,8,9,11})
time M1 = gens gb reesIdeal i; 
time M2 = gens gb reesIdeal(i,i_0);
time M3 = gens gb reesIdeal(i,i_0, Strategy => Bayer);
time M4 = gens gb reesIdeal(i, Strategy => Bayer);
M1 = substitute(M1, ring M2);
M4 = substitute(M4, ring M2);
assert(M2 == M1)
assert(M2 == M4)

///


--- Testing analyticSpread
TEST ///
R=QQ[a,b,c,d,e,f]
M=matrix{{a,c,e},{b,d,f}}
assert(analyticSpread image M == 3)
///

---Testing specialFiberIdeal
TEST///
R=ZZ/23[a,b,c,d]
msq=ideal(a^2, a*b, b^2,a*c,b*c, c^2,a*d, b*d, c*d, d^2)
sfi=specialFiberIdeal(msq)
S=ring sfi
T=ZZ/23[S_0,S_1,S_2,S_3,S_4,S_5,S_6,S_7,S_8,S_9]
M=matrix{{S_0,S_1,S_3,S_6},{S_1,S_2,S_4,S_7},{S_3,S_4,S_5,S_8},{S_6,S_7,S_8,S_9}}
i=minors(2,M)
assert(sfi == i)
///


---Testing minimalReduction, isReduction, reductionNumber
TEST///
S = ZZ/5[x,y]
I = ideal(x^3,x*y,y^4)
J = ideal(x*y, x^3+y^4)
assert(isReduction(I,J)==true)
assert(isReduction(J,I)==false)
K= minimalReduction I
assert(reductionNumber(I,J)==1)
assert(isReduction(I,K)==true)
assert(reductionNumber(I,K)==1)
///
--testing multiplicity
TEST///
R=ZZ/101[x,y]
I = ideal(x^3, x^2*y, y^3)
assert(multiplicity I==9)
R = ZZ/101[x,y]/ideal(x^3-y^3)
I = ideal(x^2,y^2)
assert(multiplicity I==6)
///

--Testing which Gm
TEST///
kk=ZZ/101;
S=kk[a..c];
m=ideal vars S
i=(ideal"a,b")*m+ideal"c3"
assert(whichGm i==3)
///

TEST///
--Test for isLinearType
S = ZZ/101[x,y]
M = module ideal(x,y)
E = {true, false, false, false, false}
assert({true, false, false, false, false} == 
    for p from 1 to 5 list(isLinearType (ideal vars S)^p))
///

TEST///
--Associated Graded ring and Normal Cone very basic test
R=ZZ/23[x]
I=ideal(x)
A=associatedGradedRing I
S=ring ideal A
assert(dim S==2)
assert(codim A==1)
N=normalCone I
s=ring ideal N
assert(dim s==2)
assert(codim N==1)
///


TEST///
--Test for distinguished
R=ZZ/101[x,y,u,v]
I=ideal(x^2, x*y*u^2+2*x*y*u*v+x*y*v^2,y^2)
p = map(R/I,R)
assert(distinguished I == {{2, p ideal(x,y)}})
///

TEST///
kk = ZZ/101
S = kk[x,y]
I = ideal"x2y";J=ideal"xy2"
assert(intersectInP(I,J) == {{5, ideal (y, x)}, {2, ideal y}, {2, ideal x}})
I = ideal"y-x2";J=ideal y
assert(intersectInP(I,J) == {{2, ideal (y, x)}})
///

TEST///
--Test for symmetricAlgebraIdeal
R=ZZ/101[x,y,u,v]
I=ideal vars R
J = symmetricAlgebraIdeal I
S = ring J
m = promote(vars R,S)||vars S
assert(J == minors(2,m))
///

end--
restart
uninstallPackage "ReesAlgebra"
restart
installPackage "ReesAlgebra"
check "ReesAlgebra"
viewHelp ReesAlgebra

----