File: Bertini.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 (3845 lines) | stat: -rw-r--r-- 151,982 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
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
bertiniPresent := run ("type bertini >/dev/null 2>&1") === 0

newPackage(
  "Bertini",
  Version => "2.1.2.3",
  Date => "July 2020",
  Authors => {
    {Name => "Elizabeth Gross",
     Email=> "elizabeth.gross@sjsu.edu",
     HomePage => "http://math.sjsu.edu/~egross"},
    {Name => "Jose Israel Rodriguez",
     Email => "Jose@math.wisc.edu",
     HomePage =>"https://www.math.wisc.edu/~jose/"},
    {Name => "Dan Bates",
     Email => "bates@math.colostate.edu",
     HomePage => "http://www.math.colostate.edu/~bates"},
    {Name => "Anton Leykin",
     Email => "leykin@math.gatech.edu",
     HomePage => "http://www.math.gatech.edu/~leykin"}
  },
  Headline => "interface to Bertini",
  Keywords => {"Numerical Algebraic Geometry", "Interfaces"},
  Configuration => { "BERTINIexecutable"=>"bertini" },
  AuxiliaryFiles => true,
  PackageExports => {"NAGtypes"},
  PackageImports => {"NAGtypes"},
  CacheExampleOutput => true,
  OptionalComponentsPresent => bertiniPresent
)

exportMutable{"storeBM2Files"
  }

export {
  "SetParameterGroup",
  "bertiniUserHomotopy",
  "ReturnPoints",
  "PrintMidStatus",
  "OutputStyle",--TODO remove this option
  "TopDirectory",
  "StorageFolder",
  "RandomGamma",
  "SubFolder",
  "StartParameters",
  "StartPoints",
  "subPoint",
  "OrderPaths",
  "bertiniZeroDimSolve",
  "bertiniParameterHomotopy",
  "bertiniPosDimSolve",
  "bertiniSample",
  "bertiniTrackHomotopy",
  "bertiniComponentMemberTest",
  "bertiniRefineSols",
  "MultiplicityTol",
  "ConditionNumTol",
  "MPType",
  "PRECISION",
  "IsProjective",
  "ODEPredictor",
  "TrackTolBeforeEG",
  "TrackTolDuringEG",
  "FinalTol",
  "MaxNorm",
  "MinStepSizeBeforeEG",
  "MinStepSizeDuringEG",
  "ImagThreshold",
  "CoeffBound",
  "DegreeBound",
  "CondNumThreshold",
  "RandomSeed",
  "SingValZeroTol",
  "EndGameNum",
  "UseRegeneration",
  "SecurityLevel",
  "ScreenOut",
  "OutputLevel",
  "StepsForIncrease",
  "MaxNewtonIts",
  "MaxStepSize",
  "MaxNumberSteps",
  "MaxCycleNum",
  "RegenStartLevel",
  "ParameterValues",
  "NameB'InputFile",--This option allows us to change the name of the input file.
  "NameParameterFile",
  "NameSolutionsFile",
  "NameIncidenceMatrixFile",
  "NameStartFile",
  "NameFunctionFile",
--
  "makeB'InputFile",
  "BertiniInputConfiguration", --This option is a list of pairs of strings. These will be written in the CONFIG part of the Bertini input file.
  "HomVariableGroup", --A list of lists of homogeneous variable groups.
  "AffVariableGroup", --A list of lists of affine variable groups.
  "ParameterGroup",
  "VariableList",
  "PathVariable",
  "RandomComplex",
  "RandomReal",  --a list of unknowns whose values will be fixed by Bertini
  "B'Constants",--A list of pairs
  "B'Polynomials", --a list of polynomials whose zero set we want to solve; when used then the NamePolynomials option is disabled and the polynomials are automatically named "jade"
  "NamePolynomials", --A list of names of the polynomials which we want to find the common zero set of.
  "B'Functions", --A list of list of pairs.
--
  "runBertini",
  "InputFileDirectory",
  "StartFileDirectory",
  "StartParameterFileDirectory",
  "B'Exe",
  "NumberToB'String",
  "M2Precision",--needs doc
  "writeParameterFile",
  "writeStartFile",
  "importParameterFile",   --need doc
  "importSolutionsFile",
  "importIncidenceMatrix",
  "SaveData",
  "SolutionFileStyle",
  "radicalList",
--  "B'MultiProjectivePoint",
  "makeB'Section",
  "makeB'Slice",
  "ContainsPoint",
  "B'NumberCoefficients",
  "B'Homogenization",
  "RandomCoefficientGenerator",
  "B'SectionString",
  "B'Section",
  "NameB'Section",
  "ContainsMultiProjectivePoint",--Eventually we will want to have multiprojective points.
  "NameB'Slice",
  "ListB'Sections",
  "makeB'TraceInput",
  "PreparePH2",
  "readFile",
  "valueBM2",
  "NameMainDataFile",
--  "linesPerSolutions",
  "PathNumber",
  "FinalTValue",
  "MaxPrecisionUtilized",
  "PrecisionIncreased",
  "AccuracyEstInternal",
  "AccuracyEst",
  "PathsWithSameEndpoint",
  "importMainDataFile",
  "CycleNumber",
  "FunctionResidual",
  "Dimension",
  "SolutionType",
  "DeflationsNeeded",
--  "B'WitnessSet",
  "SpecifyDim",
  "NameWitnessSliceFile",
  "importSliceFile",
  "TextScripts",
  "NameWitnessSolutionsFile",
  "SpecifyComponent",
  "makeWitnessSetFiles",
  "makeSampleSolutionsFile",
  "NameSampleSolutionsFile",
  "TestSolutions",
  "makeMembershipFile",
  "ComponentNumber",
  "sortMainDataComponents",
  "moveB'File",
  "CopyB'File",
  "MoveToDirectory",
  "SpecifyVariables",
  "SubIntoCC"
    }

  protect SolutionNumber
  protect StartSystem
  protect NewtonResidual
  protect MaximumPrecision
  protect runType
  protect compnum
  protect dimen
  protect numpts
  protect digits
  protect RawData
  protect WitnessData
  protect WitnessDataFileName
  protect ComponentNumber
  protect NVariety
  protect PathVariable
--  protect Parameters -- used in NAGtypes
  protect ParameterValues
  protect CycleNumber
  protect FunctionResidual
  protect StartSolutions
  protect FailedPath
  protect AllowStrings

--##########################################################################--
-- GLOBAL VARIABLES
--##########################################################################--

DBG = 0 -- debug level (10=keep temp files)
BERTINIexe=(options Bertini).Configuration#"BERTINIexecutable"
--needsPackage"NAGtypes"
needsPackage "SimpleDoc"
     storeBM2Files = temporaryFileName();
     makeDirectory storeBM2Files
-- Bertini interface for M2
-- used by ../NumericalAlgebraicGeometry.m2

--BertiniVariety = new Type of MutableHashTable


-- The following seven exported methods are front ends for various Bertini
-- functions.
-- Each calls bertiniSolve() with the appropriate input data and
-- toggle (corresp. to the type of run).
-- bertiniSolve then does all the work of building the input file,
-- calling bertini, and calling the appropriate output parser.
knownConfigs={
	MPType=>-1,PRECISION=>-1,ODEPredictor=>-1,
	TrackTolBeforeEG=>-1,TrackTolDuringEG=>-1,FinalTol=>-1,MaxNorm=>-1,
	MinStepSizeBeforeEG=>-1,MinStepSizeDuringEG=>-1,ImagThreshold=>-1,
	CoeffBound=>-1,DegreeBound=>-1,CondNumThreshold=>-1,RandomSeed=>-1,
	SingValZeroTol=>-1,EndGameNum=>-1,UseRegeneration=>-1,SecurityLevel=>-1,
	ScreenOut=>-1,OutputLevel=>-1,StepsForIncrease=>-1,MaxNewtonIts=>-1,
	MaxStepSize=>-1,MaxNumberSteps=>-1,MaxCycleNum=>-1,RegenStartLevel=>-1
	}
bertiniZeroDimSolve = method(TypicalValue => List, Options=>{
  IsProjective =>-1,
  UseRegeneration =>-1,
  OutputStyle=>"OutPoints",--{"OutPoints","OutSolutions","OutNone"}--The output can be lists of Points (A muteable hash table), or lists of Solutions (list of complex numbers that are coordinates), or can be None (All information is stored on as a text file in the directory where the computation was ran).
  TopDirectory=>storeBM2Files,
	BertiniInputConfiguration=>{},
	AffVariableGroup=>{},
	HomVariableGroup=>{},
  RandomComplex=>{}, --A list or a list of list of symbols that denote random complex numbers.
  RandomReal=>{}, --A list or a list of list of symbols that denote random real numbers.
  B'Constants=>{},--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
  B'Functions=>{},--A list of pairs consisting of a name and a polynomial.
  NameSolutionsFile=>"raw_solutions",
  NameMainDataFile=>"main_data",
	M2Precision=>53,
  Verbose=>false
	} )
bertiniZeroDimSolve(Ideal) := o -> (I) ->bertiniZeroDimSolve( I_*,o )
bertiniZeroDimSolve(List) := o -> (myPol) ->(
    --myPol are your polynomial system that you want to solve. If empty return empty.
  if myPol=={} then error"Polynomial system is the empty list. ";
--%%--Bertini is text based. So directories have to be specified to store these text files which are read by Bertini.
--%%%%--When loading Bertini.m2 a temporary directory is made where files are stored by default: storeBM2Files.
--%%%%--To change the default directory, set the TopDirectory option to the directory you would like.
  myTopDir:=o.TopDirectory;
--%%-- We set AffVariableGroup and HomVariableGroup. If the user does not specify these groups then AffVariableGroup is taken to be the generators of the ring the first element of myPol.
  myAVG:= o.AffVariableGroup;
  myHVG:= o.HomVariableGroup;
--%%-- If the user does not specify variable groups then myAVG is set to the generators of the ring of the first polynomial.
  if myAVG==={} and myHVG==={}
  then (
    if not member (class first myPol,{String,B'Section,B'Slice,Product,Symbol})
    then (
	if o.IsProjective==-1 
    	then (myAVG=gens ring first myPol)
	else (myHVG=gens ring first myPol))
  else error"AffVariableGroup or HomVariableGroup need to be set. "    );
--%%-- Verbose set greater than 1 will print the variable groups.
--  if o.Verbose then print myAVG;
--  if o.Verbose then  print myHVG;
--%%--We need to set the CONFIGS of the Bertini input file.
--%%%%--These CONFIGS come in two flavors:
--%%%%--If the same configuration is set twice then Bertini will use the one set last.
--%%%%--The first is in BertiniInputConfiguration where we just list the configurations.
  myConfigs:=(o.BertiniInputConfiguration);
  if o.UseRegeneration===1 then myConfigs=myConfigs|{"UseRegeneration"=>1};
--  TODO: Regeneration test R=QQ[x]; length(bertiniZeroDimSolve({x^2}))==1;  bertiniZeroDimSolve({x^2},UseRegeneration=>1)=={}
--  print myConfigs;
--%%-- We use the makeB'InputFile method to write a Bertini file.
  makeB'InputFile(myTopDir,
    B'Polynomials=>myPol,
    AffVariableGroup=>myAVG,
    HomVariableGroup=>myHVG,
--%%--These are extra options the user can specify. For more information refer to their documentation.
    BertiniInputConfiguration=>myConfigs,
    RandomComplex=>o.RandomComplex,--A list or a list of list of symbols that denote random complex numbers.
    RandomReal=>o.RandomReal, --A list or a list of list of symbols that denote random real numbers.
    B'Constants=>o.B'Constants,--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
    B'Functions=>o.B'Functions--A list of pairs consisting of a name and a polynomial.
    );
--%%--Check for some errors.
--%%%%--
  if o.NameSolutionsFile=!="raw_solutions" and o.OutputStyle=!="OutSolutions"
  then error"If NameSolutionsFile is set then OutputStyle should be set to OutSolutions. ";
--%%--We call Bertini and solve the zero dimensional system.
    successRun:=runBertini(myTopDir,Verbose=>o.Verbose);
--    print successRun;
--%%--After completing the Bertini runs we import the results into Macaulay2; this is the list called theSols below.
--%%%%--Depending on the OutputStyle option we import nothing, main_data files to give Points, or raw_solutions files.
    if o.OutputStyle==="OutPoints"
    then theSols:=importMainDataFile(myTopDir,NameMainDataFile=>o.NameMainDataFile,M2Precision=>o.M2Precision);
    if o.OutputStyle==="OutSolutions"
    then theSols=importSolutionsFile(myTopDir,NameSolutionsFile=>o.NameSolutionsFile,OrderPaths=>true,M2Precision=>o.M2Precision);
--
    if o.OutputStyle=!="OutNone"
    then return theSols)

--For zero dim solve OutStyle and NameSolutionsFile need to both be changed.
--Do an error for this.

bertiniPosDimSolve = method(TypicalValue => NumericalVariety, Options=>{
  BertiniInputConfiguration=>{},
	Verbose=>false,
	IsProjective=>-1
  })
bertiniPosDimSolve List := o -> F -> (
--F is the list of polynomials
   L := {runType=>2};
   o2 := new OptionTable from L;
   o3 := o ++ o2;
   bertiniSolve(F,o3)
   )
bertiniPosDimSolve Ideal := o -> I -> bertiniPosDimSolve(I_*, o)


bertiniSample = method(TypicalValue => List, Options=>{Verbose=>false,
	BertiniInputConfiguration=>{},
	IsProjective=>-1
  })
bertiniSample (ZZ, WitnessSet) := o -> (n, W) -> (
  --W is a witness set
  -- n is the number of points to sample
  L := {runType=>3,dimen=>dim W, compnum => W.cache.ComponentNumber,numpts => n, WitnessData=>W.cache.WitnessDataFileName};
  o2 := new OptionTable from L;
  o3 := o ++ o2 ;
  bertiniSolve(equations W,o3)
  )


bertiniComponentMemberTest = method(TypicalValue => List, Options=>{Verbose=>false,
  BertiniInputConfiguration=>{},
	IsProjective=>-1})
bertiniComponentMemberTest (List, NumericalVariety) := o -> (pts, NV) -> (
--pts, list of pts to test
--NV, numerical variety
	 L := {
     BertiniInputConfiguration=>o.BertiniInputConfiguration,
     runType=>4,
     StartSolutions=>pts,
     WitnessData=>NV.WitnessDataFileName,
	   NVariety=>NV};
     o2 := new OptionTable from L;
     o3 := o ++ o2;
     bertiniSolve(NV.Equations, o3)
     )

bertiniRefineSols = method(TypicalValue => List, Options=>{Verbose=>false,
  BertiniInputConfiguration=>{},
  IsProjective=>-1
  })
bertiniRefineSols (ZZ, List, List) := o -> (d, F,p) -> (
  --d, number of digits
  --F is the list of polynomials.
  --p, list of points to sharpen
   L := {BertiniInputConfiguration=>o.BertiniInputConfiguration,
     runType=>5,
     StartSolutions=>p,
     digits=>d};
   o2 := new OptionTable from L;
   o3 := o ++ o2;
   bertiniSolve(F, o3)
   )


bertiniTrackHomotopy = method(TypicalValue => List, Options=>{
  Verbose=>false,
  BertiniInputConfiguration=>{},
  IsProjective=>-1} )
bertiniTrackHomotopy (RingElement, List, List) := o -> (t, H, S1) -> (
  --t, path variable
  --H, homotopy
  --S1, solutions to start system
   L := {BertiniInputConfiguration=>o.BertiniInputConfiguration,
     runType=>6,
     StartSolutions=>S1,
     PathVariable=>t};
   o2 := new OptionTable from L;
   o3 := o ++ o2;
   bertiniSolve(H,o3)
   )

--This is a type 2 user-defined homotopy
bertiniUserHomotopy = method(TypicalValue => List, Options=>{
  Verbose=>false,
	OutputStyle=>"OutPoints",--{"OutPoints","OutSolutions","OutNone"}--The output can be lists of Points (A muteable hash table), or lists of Solutions (list of complex numbers that are coordinates), or can be None (All information is stored on as a text file in the directory where the computation was ran).
	TopDirectory=>storeBM2Files,
	B'Functions=>{},
	BertiniInputConfiguration=>{},
	AffVariableGroup=>{},
	HomVariableGroup=>{},
	RandomComplex=>{},
	RandomReal=>{},
	B'Constants=>{},--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
	B'Functions=>{},--A list of pairs consisting of a name and a polynomial.
	M2Precision=>53
--	IsProjective=>-1
	--NonPolynomial=>false
	} )
bertiniUserHomotopy(Thing,List, List, List) := o -> (pathT, SPG, myPol, S1) -> (
--%%--Bertini is text based. So directories have to be specified to store these text files which are read by Bertini.
--%%%%--When loading Bertini.m2 a temporary directory is made where files are stored by default: storeBM2Files.
--%%%%--To change the default directory, set the TopDirectory option to the directory you would like.
  myTopDir:=o.TopDirectory;
--if o.NonPolynomial===false then()
--%%-- We set AffVariableGroup and HomVariableGroup. If the user does not specify these groups then AffVariableGroup is taken to be the generators of the ring the first element of myPol with myParams deleted.
  myAVG:= o.AffVariableGroup;
  myHVG:= o.HomVariableGroup;
  myParams:= for i in SPG list if class i===Option then first i else i;
  if myAVG==={} and myHVG==={}
  then (
    if not member (class first myPol,{String,B'Section,B'Slice,Product,Symbol})
    then (myAVG=gens ring first myPol;
      for i in flatten myParams do myAVG=delete(i,myAVG);
      myAVG=delete(pathT,myAVG))
  else error"AffVariableGroup or HomVariableGroup need to be set. ");
--%%-- We use the bWriteInputFile method to write a Bertini file.
  makeB'InputFile(myTopDir,
    SetParameterGroup=>SPG,
    B'Polynomials=>myPol,
    AffVariableGroup=>myAVG,
    HomVariableGroup=>myHVG,
    PathVariable=>{pathT},
--%%--These are extra options the user can specify. For more information refer to their documentation.
    BertiniInputConfiguration=>({{"UserHomotopy",2}}|o.BertiniInputConfiguration),
    RandomComplex=>o.RandomComplex,--A list or a list of list of symbols that denote random complex numbers.
    RandomReal=>o.RandomReal, --A list or a list of list of symbols that denote random real numbers.
    B'Constants=>o.B'Constants,--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
    B'Functions=>o.B'Functions--A list of pairs consisting of a name and a polynomial.
    );
--  print 1;
    writeStartFile(myTopDir,S1);
    runBertini(myTopDir,Verbose=>o.Verbose);
--    print 2;
--%%%%--Depending on the OutputStyle option, the style of this text file can be main_data or a list of coordinates.
--%%--After completing the Bertini runs we import the results into Macaulay2; this is the list called allSols below.
--%%%%--Depending on the OutputStyle option we import nothing, main_data files to give Points, or raw_solutions files.
    allSols:={};
    if o.OutputStyle==="OutPoints"
    then  allSols=importMainDataFile(myTopDir,M2Precision=>o.M2Precision,NameMainDataFile=>"main_data");
    if o.OutputStyle==="OutSolutions"
    then allSols=importSolutionsFile(myTopDir,NameSolutionsFile=>"raw_solutions",OrderPaths=>true,M2Precision=>o.M2Precision);
    if o.OutputStyle=!="OutNone"
    then return allSols)
--bertiniUserHomotopy(RingElement, List, List) := o -> (pathT, myPol, S1) -> bertiniUserHomotopy(pathT,{},myPol,S1)




bertiniParameterHomotopy = method(TypicalValue => List, Options=>{
	OutputStyle=>"OutPoints",--{"OutPoints","OutSolutions","OutNone"}--The output can be lists of Points (A muteable hash table), or lists of Solutions (list of complex numbers that are coordinates), or can be None (All information is stored on as a text file in the directory where the computation was ran).
	TopDirectory=>storeBM2Files,
	B'Functions=>{},
	BertiniInputConfiguration=>{},
	AffVariableGroup=>{},
	HomVariableGroup=>{},
	RandomComplex=>{}, --A list or a list of list of symbols that denote random complex numbers.
	RandomReal=>{}, --A list or a list of list of symbols that denote random real numbers.
	B'Constants=>{},--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
	B'Functions=>{},--A list of pairs consisting of a name and a polynomial.
	M2Precision=>53,
	Verbose=>false
	} )
bertiniParameterHomotopy (List, List, List) := o -> (myPol, myParams, myParValues) ->(
    --myPol are your polynomial system that you want to solve.
    --myParams are your parameters.
    --myParValues are the values the parameters will take.
--%%--Bertini is text based. So directories have to be specified to store these text files which are read by Bertini.
--%%%%--When loading Bertini.m2 a temporary directory is made where files are stored by default: storeBM2Files.
--%%%%--To change the default directory, set the TopDirectory option to the directory you would like.
  myTopDir:=o.TopDirectory;
--%%-- We set AffVariableGroup and HomVariableGroup. If the user does not specify these groups then AffVariableGroup is taken to be the generators of the ring the first element of myPol with myParams deleted.
  myAVG:= o.AffVariableGroup;
  myHVG:= o.HomVariableGroup;
  if myAVG==={} and myHVG==={}
  then (
    if not member (class first myPol,{String,B'Section,B'Slice,Product,Symbol})
    then (myAVG=gens ring first myPol;
      for i in myParams do myAVG=delete(i,myAVG))
  else error"AffVariableGroup or HomVariableGroup need to be set. "    );
--  print myAVG;
--  print myHVG;
--%%-- We use the makeB'InputFile method to write a Bertini file.
  makeB'InputFile(myTopDir,
    ParameterGroup=>myParams,
    B'Polynomials=>myPol,
    AffVariableGroup=>myAVG,
    HomVariableGroup=>myHVG,
--%%--These are extra options the user can specify. For more information refer to their documentation.
    BertiniInputConfiguration=>({{ParameterHomotopy,1}}|o.BertiniInputConfiguration),
    B'Functions=>o.B'Functions,
    RandomComplex=>o.RandomComplex,--A list or a list of list of symbols that denote random complex numbers.
    RandomReal=>o.RandomReal, --A list or a list of list of symbols that denote random real numbers.
    B'Constants=>o.B'Constants,--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
    B'Functions=>o.B'Functions--A list of pairs consisting of a name and a polynomial.
    );
--%%--We call Bertini and solve the parameter homotopy for random parameters.
--%%%%--The PreparePH2=>true, will automatically adjust the Bertini input file to set ParameterHomotopy=2.
--&&&&--Refer to the Bertini manual for more details on parameter homotopies.
    runBertini(myTopDir,PreparePH2=>true,Verbose=>o.Verbose);
--%%--For each set of parameter values, i.e. each element of myParValues we will do a Bertini run.
--%%%%--The output of run # will be stored as a text file named "ph_jade_#".
--%%%%--Depending on the OutputStyle option, the style of this text file can be main_data or a list of coordinates.
    runNumber:=0;
    for i in myParValues do(
      writeParameterFile(myTopDir,i);
      runBertini(myTopDir,Verbose=>o.Verbose);
      if o.OutputStyle==="OutPoints" then moveB'File(myTopDir,"main_data","ph_jade_"|runNumber);
      if o.OutputStyle==="OutNone" then moveB'File(myTopDir,"raw_solutions","ph_jade_"|runNumber);
      if o.OutputStyle==="OutSolutions" then moveB'File(myTopDir,"raw_solutions","ph_jade_"|runNumber);
      runNumber=runNumber+1
      );
--%%--After completing the Bertini runs we import the results into Macaulay2; this is the list called allSols below.
--%%%%--Depending on the OutputStyle option we import nothing, main_data files to give Points, or raw_solutions files.
    allSols:={};
    if o.OutputStyle==="OutPoints"
    then for i from 0 to #myParValues-1 do allSols=allSols|{importMainDataFile(myTopDir,M2Precision=>o.M2Precision,NameMainDataFile=>"ph_jade_"|i)};
    if o.OutputStyle==="OutSolutions"
    then for i from 0 to #myParValues-1 do allSols=allSols|{importSolutionsFile(myTopDir,NameSolutionsFile=>"ph_jade_"|i,OrderPaths=>true,M2Precision=>o.M2Precision)};
--
    if o.OutputStyle=!="OutNone"
    then return allSols)


---------------------------------------------------
-- bertiniSolve: This is the main control function:
---------------------------------------------------

bertiniSolve = method(TypicalValue => List, Options=>{
  BertiniInputConfiguration => {},
	AllowStrings=>-1,
  MultiplicityTol=>1e-6,
	Verbose=>false,
  IsProjective=>-1,Parameters=>null,ParameterValues=>null,StartSystem=>{},
	StartSolutions=>{},NVariety=>null, RawData=>null,WitnessData=>null,
	dimen=>-1,compnum=>-1,numpts=>-1,Points=>{},digits=>-1,runType=>0,
	PathVariable=>null})
bertiniSolve List := o -> F -> (  -- F is the list of polynomials
	  dir := makeBertiniInput(F,o);   -- creates the input file
          if o.Verbose then stdio << "The version of Bertini
	    you have installed on your computer
	    was used for this run. \nBertini is under ongoing development by
	    D. Bates, J. Hauenstein, A. Sommese, and C. Wampler.\n\n";

	  --if o.WriteOnly=!=-1 then break "Write Only";

	  if o.runType == 2 then ( -- PosDim
    	    run("cd "|dir|"; "|BERTINIexe|" >bertini_session.log");
	    -- runs Bertini, storing screen output to bertini_session.log
            );

           if o.runType == 3 then ( -- Sample
    	    run("cd "|dir|"; "|BERTINIexe|" < sample_script >bertini_session.log");
	    -- runs Bertini, storing screen output to bertini_session.log
            );

          if o.runType == 4 then ( -- Membership
	    run("cd "|dir|"; "|BERTINIexe|" >bertini_session.log");
	    -- runs Bertini, storing screen output to bertini_session.log
            );

          if o.runType == 5 then ( -- Refine/Sharpen
    	    run("cd "|dir|"; "|BERTINIexe|" < sharpen_script >bertini_session.log");
	    -- runs Bertini, storing screen output to bertini_session.log--OUREDIT
            );

          if o.runType == 6 then ( -- track homotopy
    	    run("cd "|dir|"; "|BERTINIexe|" >bertini_session.log");
	    -- runs Bertini, storing screen output to bertini_session.log
            );

         readSolutionsBertini(dir,F,o) -- o contains runType,
	 --so we can switch inside readSolutionsBertini
         )


-------------------
-- makeBertiniInput
-------------------

makeBertiniInput = method(TypicalValue=>Nothing,Options=>{
  BertiniInputConfiguration=>{},
	AllowStrings=>-1,
  MultiplicityTol=>1e-6,
	Verbose=>false,
  Parameters=>null,ParameterValues=>null,StartSystem=>{},
	StartSolutions=>{},RawData=>null,WitnessData=>null,NVariety=>null,
	IsProjective=>-1,
	dimen=>-1,compnum=>-1,numpts=>-1,Points=>{},digits=>-1,runType=>0,PathVariable=>null})
makeBertiniInput List := o -> T -> ( -- T=polynomials
    startS1:=apply(o.StartSolutions,
	p->(if instance(p,AbstractPoint) then coordinates p else p));
    t:=o.PathVariable;
    gamma:=random(CC);
    params:=o.Parameters;
    --v := gens ring T#0; -- variables

    if o.AllowStrings===-1 then   v := gens ring T#0 -- variables
      else v = o.AllowStrings;

    if o.runType==6  then (v=delete(t,v));  --special for runtype6

    dir := temporaryFileName(); -- build a directory to store temporary data
    makeDirectory dir;
    f := openOut (dir|"/input"); -- typical (but not only possible) name for
    --Bertini's input file

    -- The following block is the config section of the input file

    f << "CONFIG\n\n";-- starting the config section of the input file

    -- for each user-provided option, we write the appropriate config to the file:
    scan(o.BertiniInputConfiguration,i->f<<(toString first i) <<": "<<(toString last i)<<" ;\n");
    -- now we handle the various runType options:

    if o.runType == 2 then --pos dim run
        f << "TRACKTYPE: 1;\n";

    if o.runType == 3 then --sample component
        f << "TRACKTYPE: 2;\n";

    if o.runType == 4 then --membership test
        f << "TRACKTYPE: 3;\n";

    if o.runType == 5 then ( --refine solutions
        if o.IsProjective==-1 then f << "SHARPENONLY: 1;\n UserHomotopy: 1; \n"
	    else f << "SHARPENONLY: 1;\n UserHomotopy: 2; \n");

    if o.runType == 6 then ( --trackHomotopy
        if o.IsProjective==-1 then f << "USERHOMOTOPY: 1;\n"
	    else f << "USERHOMOTOPY: 2;\n");

    f << endl << "END;\n\n";  -- end of config section

    -- The following block is the input section of the input file

    f << "INPUT" << endl << endl;

    if o.IsProjective==1 then (
	f << "hom_variable_group ")
        else (
	    if member(o.runType,{1,5,6}) then  -- if user-defined,
	        --declaration type of vars is "variable"
		f << "variable "
		else f << "variable_group ");-- if not user-defined,
	        --dec type of vars if "variable_group"

    scan(#v, i->  -- now we list the variables in a single list
       if i<#v-1 then f << toString v#i << ", "
	   else f << toString v#i << ";" << endl
       );
    f << "function "; -- "function" section
    scan(#T, i-> -- here are the function names
       if i<#T-1
          then f << "f" << i << ", "
          else f << "f" << i << ";" << endl << endl
       );

    if (o.runType==6) then (f << "pathvariable "<<" daejT; " <<endl;
	--we chose daejT because we needed a name no one would choose
	--so we chose our initials and T
	f << "parameter "<<toString(t)|" ;" <<endl;
        f << toString(t)|"= daejT ;"<<endl);

    bertiniNumbers := p->if class p === CC then (
	toString realPart p | "+" | toString imaginaryPart p | "*I"
	)
        else (
	    L := toExternalString p;
            L = replace("p"|toString precision p, "", L);
            L = replace("\\bii\\b", "I", L);
            L = replace("([0-9])e([0-9])", "\\1E\\2", L);
            L
            );

     -- if o.SubFunctions=!=-1 then (
	--  for i in o.SubFunctions do (
	  --    f << toString (i_0) << " = " << toString(i_1)<< " ;\n")
	  -- );

      --The next lines of code write the polynomials to the input file called f:

      -- non-param runs: just write out the polynomials
      if (o.runType!=1 and o.runType!=5) then (
	   scan(#T, i -> f << "f" << i << " = " << (if class T#i===String then
		   T#i else bertiniNumbers T#i) << ";" << endl)
	   )
      -- param runs: write out polys and some other stuff
      else ((  -- refine sols runs: write out polys and some other stuff
                 f << "pathvariable t;\n"
                 << "parameter s;\n"
                 << "s = t;\n\n";
                 scan(#T, i -> f << "f" << i << " = "
		     << (if class T#i===String then T#i else bertiniNumbers T#i)
		     << ";" << endl)
	         );
             );

      f << endl << "END;" << endl << endl;
      close f;

      --Now we build auxiliary files for various sorts of runs:

      if member(o.runType,{1,6}) then ( -- writing out start file in the case of a param run
	  f = openOut (dir|"/start"); -- the only name for Bertini's start solutions file
          f << #startS1 << endl << endl;
          scan(startS1, s->(
		 scan(s, c-> f << realPart c << " " << imaginaryPart c << ";" << endl );
		 f << endl;
		 ));
          close f;
          );

       if (o.runType==4) then ( -- writing out file with points in the case of
	   --a membership run
          f = openOut (dir|"/member_points"); -- the only name for Bertini's
	  --membership points file
          f << #startS1 << endl << endl;
          scan(startS1, s->(
		 scan(s, c-> f << realPart c << " " << imaginaryPart c << ";" << endl );
		 f << endl;
		 ));
          close f;
          );


       if (o.runType==5) then ( -- writing out file with points in the case of
	   --a refine run
	   f = openOut (dir|"/sharpen_script"); -- writing out file with
	   --query responses in case of a refine/sharpen run
           f << "5" << endl << o.digits << endl << "1" << endl;
           close f;

           --create raw_data in tmp directory

	   f =openOut(dir|"/raw_data");
  	   f << toString(#v)<<endl;
	   f << toString(0)<<endl;
	   for i from 0 to #startS1-1 do(
	       f << toString(i)<<endl;
	       f << toString(52)<<endl;
	       --f << "1 0" <<endl; --working in affine space, don't need this line
	       scan(startS1_i,
		   c->f<<realPart(c) <<" "<<imaginaryPart(c)<<endl);
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;
		   f << "1" <<endl;);

	   f << "-1"<<endl;
	   f << endl;
	   f << "2 0"<<endl; -- precision type, not using equation by equation
	   f << endl;
	   f<< "0 "|toString(#v)<<endl; -- no patch, number of variables
	   f << endl;
     	   f << "-1"<<endl;
	   f << "1 1"<<endl; -- gamma
	   f << endl;
	   f<< "0 0"<<endl;
	   f << endl;
	   f<<"0 0"<<endl;
	   close f;

           --create midpath_data in tmp directory

           f =openOut(dir|"/midpath_data");
           f << "This file needs to be created by bertiniRefineSols for Bertini" << endl;
           close f;
           );

      if (o.runType==3) then (  --copies witness_data file to tmp directory
	  copyFile(o.WitnessData, dir|"/witness_data")
          );

      if (o.runType==4) then (  --copies witness_data file to tmp directory
	  copyFile(o.WitnessData, dir|"/witness_data")
          );

      if (o.runType==3) then ( -- writing out file with query responses in case
	  --of a sample run
	  f = openOut(dir|"/sample_script");
	  f << o.dimen << endl << o.compnum << endl << o.numpts << endl << "0" <<
	      endl << "sample_points" << endl;
       -- sampled points will be written file named sample_points
          close f;
          );

	  if o.Verbose then stdio
	       << "Temporary directory for input and output files:" << dir << endl << endl;

      dir
      )



-----------------------
-- readSolutionsBertini
-----------------------

readSolutionsBertini = method(TypicalValue=>NumericalVariety, Options=>{
  BertiniInputConfiguration=>{},
  MultiplicityTol=>1e-6,
	Verbose=>false,
  AllowStrings=>-1,
	IsProjective=>-1,Parameters=>null,
	ParameterValues=>null, StartSystem=>{},NVariety=>null,
	StartSolutions=>{},RawData=>null,WitnessData=>null,
	dimen=>-1,compnum=>-1,numpts=>-1,Points=>{},
	digits=>-1,runType=>0,PathVariable=>null})

readSolutionsBertini (String,List) := o -> (dir,F) -> (
  -- dir=directory holding the output files, options are same as bertiniSolve
  local pt;
  local coord;
  local coords;
  local funcResid;
  local condNum;
  local newtonResid;
  local lastT;
  local cycleNum;
  local success;
  local solNum;
  local numVars;
  local a;
  local numCodims;
  local ptsInCodim;
  local ptType;
  local ptMult;
  local compNum;
  local numDeflations;
  local nv;
  local ws;
  local codimen;
  local listOfCodims;
  local randDims;
  local numRands;
  local numToSkip;
  local linCoeffDims;
  local numLinCoeffs;
  local rw;
  local mat;
  local coefParts;
  local M;
  local colsToSkip;
  local N;
  local dehomCoords;
  local vars;
  local R;

  s := {};
  if (member(o.runType,{0,8}))
  then (
    sessionLog:= lines get (dir|"/bertini_session.log"); -- get contents of session log
    --and check for rank error
    --TODO incorporate this error in bertiniZeroDimSolve
    scan(sessionLog, i->if i=="The system has no zero dimensional solutions based on its rank!" then
	     error  "The system has no zero dimensional solutions based on its rank!");
    failedPaths := lines get (dir|"/failed_paths"); -- get contents of failed paths file and check if non-empty
    if  failedPaths=!={""} then (
	  if o.Verbose then stdio << "Warning: Some paths failed, the set of solutions may be incomplete" <<endl<<endl) ;
  --raw_data, for zeroDim

--raw_data output file structure:
--  #var's (incl. homog. var.!!)
--  0
--  blocks as follows:
--    path_num
--    max prec used
--    coords (proj!!)
--    fxn resid
--    cond_num
--    Newton resid
--    last Tval
--    useless here (accuracy estimate -- diff bw last two extrapolations to t=0)
--    useless here (Tval of first prec increase)
--    cycle number
--    success?  (1 for yes)
--    NOTE:  # paths ending at same point is NOT reported in this file
-- needs to be computed...only available in human-readable main_data!!!
--  -1 (at end of blocks)
--  junk at end is the matrix of patch coefficients
    -- MPType on first line, then number or rows & columns on second,
    -- then the coeffs
    l := lines get (dir|"/raw_data"); -- grabs all lines of the file
    numVars = value(first l);
    l = drop(l,2);
    solNum = value(first l);
    l = drop(l,1);
    --Now we go through all blocks of solutions
    -- each block contains the coordinates of the solution
    -- and a bunch of other stuff.
    wList := {}; --list of witness sets
    pts:={};
    while solNum > -1 do ( -- -1 in solNum position (top of solution block)
	--is key to end of solutions.
       	maxPrec := value(first l);
      	l = drop(l,1);
      	coords = {};
    	for j from 1 to numVars do ( -- grab each coordinate
    	    -- use regexp to get the two numbers from the string
    	    coord = select("[0-9.e+-]+", cleanupOutput(first l));
    	    coords = join(coords, {toCC(53, value(coord#0),value(coord#1))});
    	    -- NOTE: we convert to a 53 bit floating point complex type
    	    -- beware that we might be losing data here!!!
	    l = drop(l,1);
	    );
        -- now we dehomogenize, assuming the first variable is the hom coord:
    	dehomCoords = {};
    	if o.IsProjective==-1
      	then for j from 1 to numVars-1 do (
	    dehomCoords = join(dehomCoords, {coords#j / coords#0});
	    )	       	
	else for j from 0 to numVars-1 do (
	    dehomCoords = join(dehomCoords, {coords#j });
	    );
      	pt = point {dehomCoords}; --we want to output these
      	pt.cache.MaximumPrecision=maxPrec;
      	pt.FunctionResidual = value(cleanupOutput(first l)); l=drop(l,1);
      	pt.cache.ConditionNumber = value(cleanupOutput(first l)); l=drop(l,1);
      	pt.cache.NewtonResidual = value(cleanupOutput(first l)); l=drop(l,1);
      	pt.cache.LastT = value(cleanupOutput(first l)); l=drop(l,3);
      	pt.cache.CycleNumber = value(first l); l=drop(l,1);
      	if(value(first l)=!=1) then pt.cache.SolutionStatus=FailedPath else pt.cache.SolutionStatus=null;
      	l = drop(l,1);
      	pt.cache.SolutionNumber = value(first l);
      	solNum = pt.cache.SolutionNumber;
      	l = drop(l,1);
    	pts = join(pts,{pt});
    	);
      pts = solutionsWithMultiplicity(pts, Tolerance=>o.MultiplicityTol);
      if o#?UseRegeneration then(
          if o.UseRegeneration==1 then return pts
          );
      checkMultiplicity(pts);
      checkConditionNumber(pts, 1e10);--TODO: 1e10 specifies a condition number tolerance that should be an option.
      for i in pts do (
        if (i.cache.SolutionStatus=!=Singular
          and i.cache.SolutionStatus=!=FailedPath
      	  and i.cache.SolutionStatus=!=RefinementFailure)
        then i.cache.SolutionStatus=Regular);
    	return pts
    	   )
  else if (o.runType == 1 or o.runType==6 or o.runType==5) then (
    -- get contents of session log and check errors
    sessionLog = lines get (dir|"/bertini_session.log");
    scan(sessionLog, i->if i=="ERROR: The matrix has more columns than rows in QLP_L_mp!!" then
	  error  "The matrix has more columns than rows in QLP_L_mp!"
	  );
    l = lines get (dir|"/raw_data"); -- grabs all lines of the file
    numVars = value(first l);
    l = drop(l,2);
    solNum = value(first l);
    l = drop(l,1);
    --Now we go through all blocks of solutions
    -- (each block contains the coordinates of the solution and other stuff)
    pts={};
    prec'value := (P,s) -> ( -- P:ZZ and s:String
  	  where'is'e := regex("e",s);
    	if where'is'e===null
      then value(s|"p" | toString P)
  	   else (
         pos := first first where'is'e;
         value (substring((0,pos),s) | "p" | toString P | substring((pos,#s-pos),s))
         ));
    while solNum > -1 do (
  	-- -1 in solNum position (top of solution block) is key to end of solutions.
    	maxPrec = value(first l);
    	l = drop(l,1);
    	bitPrec := ceiling((log 10/log 2)*o.digits);
    	coords = {};
    	for j from 1 to numVars do ( -- grab each coordinate
    	    -- use regexp to get the two numbers from the string
    	    coord = select("[0-9.e+-]+", cleanupOutput(first l));
    	    if (o.runType==1 or o.runType==6)
            then (
		coords = join(coords, {toCC(53, value(coord#0),value(coord#1))}))
	    -- NOTE: we convert to a 53 bit floating point complex type
	    -- beware that we might be losing data here!!!
    	    else (coords = join(coords,
		    {toCC(bitPrec, prec'value(bitPrec,coord#0), prec'value(bitPrec,coord#1))}
		    ));
            l = drop(l,1);
    	    );	  
    	pt = point{coords}; --we want to output these
      	pt.cache.MaximumPrecision=maxPrec;
    	pt.cache.FunctionResidual = value(cleanupOutput(first l)); l=drop(l,1);
	pt.cache.ConditionNumber = value(cleanupOutput(first l)); l=drop(l,1);
	pt.cache.NewtonResidual = value(cleanupOutput(first l)); l=drop(l,1);
	pt.cache.LastT = value(cleanupOutput(first l)); l=drop(l,3);
	pt.cache.CycleNumber = value(first l); l=drop(l,1);
    	if(value(first l)=!=1) and o.runType==5
      	then pt.cache.SolutionStatus=RefinementFailure else pt.cache.SolutionStatus=null;
    	if(value(first l)=!=1) and o.runType=!=5 then  pt.cache.SolutionStatus=FailedPath;
    	l=drop(l,1);
      	pt.cache.SolutionNumber = value(first l);
     	solNum=pt.cache.SolutionNumber;
      	l = drop(l,1);
    	pts=join(pts,{pt})
      );
    pts=solutionsWithMultiplicity(pts, Tolerance => o.MultiplicityTol);
    if o#?UseRegeneration then(
        if o.UseRegeneration==1 then return pts
        );
    checkMultiplicity(pts);
    checkConditionNumber(pts, 1e10);--TODO: 1e10 specifies a condition number tolerance that should be an option.
    for i in pts do (
      if (i.cache.SolutionStatus=!=Singular
        and i.cache.SolutionStatus=!=FailedPath
        and i.cache.SolutionStatus=!=RefinementFailure)
      then i.cache.SolutionStatus=Regular);
    return pts ) else
  --if PosDim, we read in the output from witness_data
   if (o.runType == 2) then (
--witness_data output file structure:
--  #var's (incl. homog. var.!!)
--  #nonempty codims
--  blocks by codim (1 block per codim):
--    codim
--    total #points in this codim (over all irred. comps.)
--    blocks by points (1 block per point):
--      max prec used
--      coords (proj!!)
--      max prec used (useless!)
--      last approx of point on path before convergence to t=0 (useless!)
--      cond_num
--      corank (useless!)
--      smallest nonzero sing val (useless!)
--      largest zero sing val (useless!)
--      type
--      multiplicity
--      component number
--      deflations needed for this point
--    -1 (at end of blocks)
--  junk at end is the matrix of slice coefficients and such.
    l = lines get (dir|"/witness_data"); -- grabs all lines of the file
    numVars = value(first l);  l=drop(l,1);
    numCodims = value(first l); l=drop(l,1);
    wList = {};  --list of witness sets
    listOfCodims = {};  --keeps track of codimension of each witness set;
    --needed since we add slice data later.
    for codimNum from 1 to numCodims do (
	pts := {};  --for each codim, we store all points and
	--all codims (next line), then sort after gathering all points in the codim
      	compNums := {};
      	maxCompNum := 0;  --keeps track of max component number in this codim
      	codimen = value(first l); l=drop(l,1);
      	ptsInCodim = value(first l); l=drop(l,1);
      	for ptNum from 1 to ptsInCodim do (
  	    maxPrec := value(first l);
            l = drop(l,1);
            coords = {};
            for j from 1 to numVars do ( -- grab each coordinate
              	-- use regexp to get the two numbers from the string
  	      	coord = select("[0-9.e+-]+", cleanupOutput(first l));
          	coords = join(coords, {toCC(maxPrec, value(coord#0),value(coord#1))});
	      	-- NOTE: we convert to maxPrec bits complex type
          	l = drop(l,1);
        	);
            l = drop(l,numVars+1);  -- don't need second copy of point or
  	    --extra copy of maxPrec
            -- now we dehomogenize, assuming the first variable is the hom coord:
  	    dehomCoords = {};
  	    if o.IsProjective==-1
            then (
	        for j from 1 to numVars-1 do (
  		    dehomCoords = join(dehomCoords, {coords#j / coords#0});
          	    ))
  	    else for j from 0 to numVars-1 do (
  		    dehomCoords = join(dehomCoords, {coords#j });
                    );
  	    condNum = value(cleanupOutput(first l)); l=drop(l,4);
            ptType = value(first l); l=drop(l,1);
            ptMult = value(first l); l=drop(l,1);
            compNum = value(first l); l=drop(l,1);
            numDeflations = value(first l); l=drop(l,1);
            pt = point {dehomCoords};
            pts = join(pts,{pt});
            compNums = join(compNums,{compNum});
            if (compNum > maxCompNum) then maxCompNum=compNum;
            );
      	for j from 0 to maxCompNum do (
	    --loop through the component numbers in this codim
	    --to break them into witness sets
  	    ptsInWS := {}; --stores all points in the same witness set
  	    for k from 0 to #pts-1 do (
  	      	--save the point if its in the current component (component j)
  	      	if (compNums#k == j) then ptsInWS = join(ptsInWS,{pts#k});
  	      	);
  	    N = map(CC^0,CC^numVars,0); -- this is a dummy, will grab slice data later
  	    ws = if o.IsProjective===1 then ( 
		W := projectiveWitnessSet(ideal F, N -* fake affine chart *-, N, ptsInWS); 
		W
		) else witnessSet(ideal F, N, ptsInWS);
	    ws.cache.IsIrreducible = true;
	    --turn these points into a witness set
	    -- ws = witnessSet(ideal F,N, ptsInWS); --turn these points into a witness set
            ws.cache.ComponentNumber=j;
	    ws.cache.WitnessDataFileName=dir|"/witness_data";
	    wList = join(wList, {ws}); --add witness set to list
	    listOfCodims = join(listOfCodims, {codimen});
	    );
    	);
    -- now we grab the slice data, at the end of the witness_data file,
    --to be inserted into the witnessSets with dim>0
    l = drop(l,3); -- -1, blank line, MPType
    randDims = select("[0-9]+", first l);  -- grabs #rows,
    --#cols for the matrix used to randomize the system
    l = drop(l,1);
    numRands = value(randDims#0) * value(randDims#1);  -- numRands is the
    --number of random numbers we want to skip next
    l = drop(l,numRands+1);   -- includes blank line after rands
    -- next we have the same number of integers
    --(degrees needed to keep homogenization right)
    l = drop(l,numRands);
    -- next we have an integer and a list of row vectors
    --(the number of which is the initial integer).  Again related to
    --homogenization.
    numToSkip = select("[0-9]+", first l);
    l = drop(l,value(numToSkip#0)+3); -- dropping all those,
    --plus line containing integer (before), then blank line, and one more line
    --finally, we have the number of linears and the number of coefficients per linear
    linCoeffDims = select("[0-9-]+", first l);
    l = drop(l,1);
    --now we just read in the matrix
    numLinCoeffs = value(linCoeffDims#0) * value(linCoeffDims#1);
    rw = {};
    mat = {};


    for i from 1 to value(linCoeffDims#0) do ( 
	for j from 1 to value(linCoeffDims#1) do (
            coefParts = select("[0-9-]+/[0-9-]+", first l);
            rw = join(rw, {toCC(53,value(coefParts#0)) + 
		    ii*toCC(53,value(coefParts#1))});  
	    -- definitely losing data here, going from rational number to float!
            l = drop(l,1);
            );
        mat = join(mat, {rw});  
        rw = {};
        );
    
    M = if #mat>0 then transpose matrix(mat) else map(CC^(numVars+1),CC^0,0); --stores all slices

    -- Finally, we can cycle through the witness sets in nv
    -- and add the slice data.
    -- There are length listOfCodims witness sets,
    -- the first of which uses the full set of slices (all of M).
    -- The higher codimensions need higher-dimensional hyperplane sections,
    -- so fewer slices (part of M).
    -- The lowest slice is kept longest.
    -- Ex:  If there is a codim 1 set with a 2x4 matrix of slice data,
    -- a subsequent codim 2 set would have a
    -- 1x4 matrix of slice data consists of the second (not first)
    -- line of the codim 1 slice data.
    wList = for codimNum from 0 to length listOfCodims - 1 list (
	--We store the cols of M needed for this particular codimNum in coeffList,
	--then turn it into a matrix and store it the witness set.
	colsToSkip = listOfCodims#codimNum - listOfCodims#0;
	N = transpose submatrix(M,,colsToSkip..numcols M - 1);
	if o.IsProjective===1 then N = map(CC^(numrows N),CC^1,0)|N; -- constant terms are 0xb
	-- rearrange columns so slice from NAGtypes
	--returns the correct linear functional
	firstCol:=N_{0};
	N=submatrix'(N, ,{0})|firstCol;
	W := wList#codimNum;
	W' := witnessSet(W.Equations, N, W.Points);
        for k in keys W.cache do W'.cache#k = W.cache#k;
	W' 
	);
    nv = numericalVariety wList;
    nv.WitnessDataFileName=dir|"/witness_data";
    nv.Equations=F;
    return nv
  )
  ----- start Sample
  else if (o.runType == 3) then (
       l = lines get (dir|"/sample_points"); -- grabs all lines of the file
       var's := gens ring F#0; -- variables
       ---Should this be changed to getting the number of
       -- vars directly from main_data? 3/6/14
       numVars = #var's;
       numberOfSolutions := value(first l);
       l = drop(l,1);

       --Now we go through all blocks of solutions
       -- (each block contains the coordinates of the solution and other stuff).

       solNum = 1;

       pts={};

       while solNum <= numberOfSolutions do (
       -- -1 in solNum position (top of solution block) is key to
       -- end of solutions.
     	    solNum=solNum+1;
	    maxPrec = value(first l);
            l = drop(l,1);
            coords = {};
            for j from 1 to numVars do ( -- grab each coordinate
	      -- use regexp to get the two numbers from the string
              coord = select("[0-9.e+-]+", cleanupOutput(first l));
              coords = join(coords, {toCC(53, value(coord#0),value(coord#1))});
	      -- NOTE: we convert to a 53 bit floating point complex type
	      -- beware that we might be losing data here!!!
              l = drop(l,1);
	      );

            pt = point{coords}; --we want to output these
	    pts=join(pts,{pt})
            );

       solNum=1;
       return pts
       )

-- component membership

  else if (o.runType==4) then (
       NV := o.NVariety;
       firstl := lines get (dir | "/witness_data");
       numVars = value(first firstl)-1;
       coDims := {};
       comps := {};
       l = lines get (dir | "/incidence_matrix");
       -- grabs lines of incidence_matrix file
       numCoDims := value first l;
       l=drop(l,1);

       for coDimNum from 1 to numCoDims do ( --get co-dimensions of components
           coDims = append(coDims, value ("{"|replace (" ", ",", l_0)|"}"));
           l=drop(l,1)
           );

       wSets := {}; --list of lists of witness sets for each point
       l = drop(l,3);
       for i from 1 to #o.StartSolutions do (
          --getting row from incidence matrix and dropping extra space
	 testVector := drop(value ("{"|replace (" ", ",", l_0)|"}"), -1);
	 witSets'forOnePoint := {};
         for j from 0 to numCoDims-1 do(
           subTestVector := take(testVector, coDims_j_1);
	   --get component numbers that with positive result
	   compNums := positions(subTestVector, k->k==1);
	    --grabs witness sets in this component
           possWitSets := NV#(numVars-coDims_j_0);
	   --select witness sets with positive result
	   witSets := select(possWitSets, k->member(k.cache.ComponentNumber, compNums));
    	   witSets'forOnePoint = witSets'forOnePoint | witSets;
	   testVector=drop(testVector, coDims_j_1);
	   );

	 wSets = append(wSets,witSets'forOnePoint); --append to larger list that we will output
         );
       return wSets
  )
  else error "unknown output file";
  )



-*
restart
path
path=prepend("/Users/jo/Documents/GoodGit/AntonM2/M2/Macaulay2/packages",path)
needsPackage"Bertini"
debug Bertini
R = CC[x,y,z,t]
I = ideal(x + 3, y+1)
I = ideal(x*(x + 3), x*(y+1)*(z-t^2))
I = ideal(x^2*(x + 3), x^2*(y+1)*(z-t^2))
I = ideal(x,y,z)
I = ideal(x,2*z-t,x-2*y-1)

nv = bertiniPosDimSolve(I_*, Verbose => true)
w = first components nv
F = polySystem slice w
pts2 = w#Points
pts2 / (p -> norm evaluate(F,p)) -- this value is >> 0
nv#WitnessDataFileName
PWD  = new MutableHashTable from {IsProjective=>-1}
A = parseWitnessDataFile(PWD,first separate("w",nv#WitnessDataFileName),"witness_data")
peek PWD
peek PWD#"WS"#0
peek PWD#"WS"#1
peek PWD#"WS"#2

(matrix{{1_CC}}|sub(vars R,    matrix PWD#"WS"#0#0))* transpose PWD#"SliceData" 
PWD#"Directory"

R = CC[x,y,z,t];I = ideal(x,y);
nv = bertiniPosDimSolve(I_*, Verbose => true,IsProjective=>1)
PWD  = new MutableHashTable from {IsProjective=>1}
A = parseWitnessDataFile(PWD,first separate("w",nv#WitnessDataFileName),"witness_data")
PWD#"RemainingFile"
sub(vars R,    matrix PWD#"WS"#0#0)*transpose  PWD#"SliceData" 


R = CC[x,y,z,t]
I = ideal(x + 3, y+1)
IP =-1
nv = bertiniPosDimSolve(I_*, Verbose => true)
PWD  = new MutableHashTable from {IsProjective=>IP}
A = parseWitnessDataFile(PWD,first separate("w",nv#WitnessDataFileName),"witness_data")
PWD#"RemainingFile"
(matrix{{1}}|sub(vars R,    matrix PWD#"WS"#0#0)) * transpose PWD#"SliceData" 

PWD#"SliceData"
PWD#"WS"#1//toList/(i->i#"ComponentNumber")
PWD#"WS"#1//toList/(i->i#"Multiplicity")
PWD#"WS"#0//toList/(i->i#"Multiplicity")
*-
--This method is used for debugging parsing witness data files. 
parseWitnessDataFile = method(TypicalValue=>MutableHashTable)
parseWitnessDataFile (MutableHashTable,String,String) := (PWD,dir,name) -> (
    --PWD :=new MutableHashTable from {};
    PWD#"Directory"=dir;
    PWD#"Name"=name;
    if  dir_-1=!="/" then dir =dir|"/";
    l := lines get (dir|name); -- grabs all lines of the file
    numVars := value(first l);  
    PWD#"NumVars"=numVars;
    l = drop(l,1);
    maxCodim := value(first l); 
    PWD#"MaxCodim"=maxCodim;--Number of equidimensional witness sets
    l=drop(l,1);    
    --list of witness sets indexed by codimension
    wList := new MutableList from for i to maxCodim-1 list null;
    --keeps track of codimension of each witness set; 
    trueCodimension := new MutableList from for i to maxCodim-1 list null;     
    --componentIndex#i number of components in codimension i.        
    componentIndex := new MutableList from for i to maxCodim-1 list null;  
    --numPoints#i number of pts in codimension i.
    numPoints := new MutableList from for i to maxCodim-1 list null;  
    scan(PWD#"MaxCodim",
	ic->(
	    print 1;
	    trueCodimension#ic = value(first l); 
	    l=drop(l,1);
            if componentIndex#ic===null then componentIndex#ic={};
	    numPoints#ic = value(first l);
	    l=drop(l,1);
	    pts := new MutableList from for i to numPoints#ic-1 list null ;  
	    -- We now construct a new point using the type Point.
    	    print"numPoints#ic loop";
--
            scan(numPoints#ic,
		ptNum->( -- !!! none of the tests/examples seem to reach this part !!! 
		    maxPrec := value(first l);
            	    l = drop(l,1);
            	    coords := new MutableList from for i to numVars-1 list null;
    	    	    print"numVars loop";
            	    scan(numVars,
			j->( -- grab each coordinate
              		    -- use regexp to get the two numbers from the string
	      		    coord := select("[0-9.e+-]+", cleanupOutput(first l));  
	      		    -- NOTE: we convert to maxPrec bits complex type
              		    coords#j = toCC(maxPrec, value(coord#0),value(coord#1));  
              		    l = drop(l,1);
              		    )
			);
    	    	    --If we have an affine variety, we homogenize by the first coordinate. 
            	    l = drop(l,numVars+1);  -- don't need second copy of point or extra copy of maxPrec
	    	    pt := point (
			if PWD.IsProjective===1 
		    	then toList coords 
            	    	-- If we have an affine variety we dehomogenize, assuming the first variable is the hom coord:
	    	    	else (1/coords#0)*toList drop(coords,1)
			);    	    
    	    	    pt.cache#"ProjectiveCoordinates"=coords;
	    	    pt.cache#"MaxPrecisionBits"=maxPrec;
	    	    condNum := value(cleanupOutput(first l)); 
	    	    pt.cache#"ConditionNumber"=condNum;
	    	    l=drop(l,4);
    	    	    --What is type?
            	    ptType := value(first l); l=drop(l,1);
	    	    pt.cache#"PointType"=ptType;
            	    ptMult := value(first l); l=drop(l,1);
            	    pt.cache#"Multiplicity"=ptMult;
    	    	    compNum := value(first l); l=drop(l,1);
	    	    pt.cache#"ComponentNumber"=compNum;
            	    numDeflations := value(first l); l=drop(l,1);
    	    	    pt.cache#"NumDeflations"=numDeflations;
    	    	    --Append pt to pts
    	    	    print coordinates pt;
            	    pts#ptNum = pt;
    	    	    print (componentIndex#ic);
            	    if not member(compNum,componentIndex#ic)
	    	    then componentIndex#ic = append(componentIndex#ic,compNum)            	     
		    )
		);
	    wList#ic =  pts
	    )
	);
    PWD#"WS"=wList;
    -- now we grab the slice data, at the end of the witness_data file, 
    --to be inserted into the witnessSets with dim>0
    l = drop(l,2); -- These are the lines {-1, blank line} 
    --MPType line
    PWD#"MPType"=first l; 
    l=drop(l,1);

    --#cols for the matrix used to randomize the system 
    randDims := select("[0-9]+", first l);  -- grabs #rows,     
    l = drop(l,1);
    
    -- numRands is the number of random numbers we want to skip next    
    numRands := value(randDims#0) * value(randDims#1);  
    
    l = drop(l,numRands+1);   -- includes blank line after rands    
    
    -- next we have the same number of integers 
    --(degrees needed to keep homogenization right)
    l = drop(l,numRands);
    
    -- next we have an integer and a list of row vectors 
    --(the number of which is the initial integer).  Again related to homogenization.    
    numToSkip := select("[0-9]+", first l);
    
    l = drop(l,value(numToSkip#0)+3); -- dropping all those, 
    --plus line containing integer (before), then blank line, and one more line
    
    --finally, we have the number of linears and the number of coefficients per linear
    (numberOfLinears,numberOfCoefficientsPerLinear) := toSequence select("[0-9-]+", first l);
    l = drop(l,1);

    --now we just read in the matrix
    numLinCoeffs := value(numberOfLinears) * value(numberOfCoefficientsPerLinear);
    rw := {};
    mat := {};
    PWD#"NumberOfLinears" =value(numberOfLinears);
    PWD#"NumberOfCoefficientsPerLinear" =value(numberOfCoefficientsPerLinear);    
    for i from 1 to PWD#"NumberOfLinears"  do ( 
	for j from 1 to PWD#"NumberOfCoefficientsPerLinear"  do (
            coefParts := select("[0-9-]+/[0-9-]+", first l);
            rw = join(rw, {toCC(53,value(coefParts#0)) + 
		    ii*toCC(53,value(coefParts#1))});  
	    -- definitely losing data here, going from rational number to float!
            l = drop(l,1);
            );
        mat = join(mat, {rw});  
        rw = {};
        );    
    M := matrix(mat);
    PWD#"SliceData"=M;   
    PWD#"RemainingFile"=l;
    -- Finally, we can cycle through the witness sets in nv 
    -- and add the slice data.
    -- There are length listOfCodims witness sets, 
    -- the first of which uses the full set of slices (all of M).
    -- The higher codimensions need higher-dimensional hyperplane sections, 
    -- so fewer slices (part of M).
    -- The lowest slice is kept longest.  
    -- Ex:  If there is a codim 1 set with a 2x4 matrix of slice data, 
    -- a subsequent codim 2 set would have a 
    -- 1x4 matrix of slice data consists of the second (not first) 
    -- line of the codim 1 slice data.
    PWD);


-------------------------------------------------------
---functions used by bertiniSolve, makeBertiniInput,
---and readBertiniSolutions----------------------------
-------------------------------------------------------

cleanupOutput = method(TypicalValue=>String)
cleanupOutput String := s -> (
  t := replace("E", "e", s);
  t = replace("[(,)]","", t);
  t = replace("e\\+","e",t)
  )


checkConditionNumber=(listOfPoints, tolerance)->(
    for i in listOfPoints do (
      if i.cache.ConditionNumber>tolerance
	  and i.cache.SolutionStatus=!=FailedPath
          and i.cache.SolutionStatus=!=RefinementFailure
          then i.cache.SolutionStatus=Singular)
      )

checkMultiplicity=(listOfPoints)->(
    for i in listOfPoints do
      if i.cache.Multiplicity>1 and i.cache.SolutionStatus=!=FailedPath
        and i.cache.SolutionStatus=!=RefinementFailure
        then i.cache.SolutionStatus=Singular)

---- November 2014 additions
--FUNCTION 1: makeB'InputFile
--the input of makeB'InputFile is a string of the directory where we want to write the files.


pairTypes={List,Option}
makeB'InputFile = method(TypicalValue => String, Options=>{
	StorageFolder=>null,
	NameB'InputFile=>"input",  --This option allows us to change the name of the input file that we will make.
	BertiniInputConfiguration=>{}, --This option is a list of pairs of strings or options. These will be written in the CONFIG part of the Bertini input file.
--For different functions using Bertini one must state "homogeneous variable groups", "affine variable groups", "parameters", "variables", or "path variables".
	HomVariableGroup=>{}, --A list  of homogeneous variable groups or a list of list of homogeneous variable groups
	AffVariableGroup=>{}, --A list  of affine variable groups or a list of list of affine variable groups.
    	ParameterGroup=>{}, --A list of parameters or list of list of parameters.
    	VariableList=>{}, --A list of variables or a list of list of variables.
    	PathVariable=>{}, --A list of path variables or a list of list of path variables.
    	RandomComplex=>{}, --A list or a list of list of symbols that denote random complex numbers.
    	RandomReal=>{}, --A list or a list of list of symbols that denote random real numbers.
	B'Constants=>{},--A list of pairs. Each pair consists of a symbol that will be set to a string and a number.
	NamePolynomials=>{}, --A list of names (names are always strings) of the polynomials which we want to find the common zero set of.
	B'Polynomials=>{},--A list  of polynomials we want to solve.
	B'Functions=>{},--A list of pairs consisting of a name and a polynomial.
	Verbose=>false,
	SetParameterGroup=>{}
	})
makeB'InputFile(String) := o ->(IFD)->(
    IFD=addSlash(IFD);
--Warnings are printed here.
     if #o.B'Polynomials===0 and #o.NamePolynomials===0 then (print "Warning: NamePolynomials and B'Polynomials are both empty.");
     if #o.B'Polynomials=!=0 and #o.NamePolynomials=!=0 then (print "Warning: NamePolynomials and B'Polynomials are both non-empty.");
     if #o.VariableList===0 and #o.AffVariableGroup===0 and #o.HomVariableGroup===0 then stdio << "Warning: VariableList, AffVariableGroup, and HomVariableGroup are all empty." <<endl<<endl;
--Errors are printed here.
     for onePair to #o.B'Constants-1 do if  class((o.B'Constants)_onePair)===List and #((o.B'Constants)_onePair)=!=2 then  error ("B'Constants is not a list of pairs because of element "|onePair);
     for onePair to #o.B'Functions-1 do if #((o.B'Functions)_onePair)=!=2  and class ((o.B'Functions)_onePair)=!=B'Section and class ((o.B'Functions)_onePair)=!=B'Slice and class ((o.B'Functions)_onePair)=!=Option then  error ("B'Functions is not a list of pairs because of element "|onePair);
     for onePair to #o.B'Functions-1 do if  class ((o.B'Functions)_onePair)===B'Section and not member(NameB'Section,keys ((o.B'Functions)_onePair)) then  error ("B'Functions contains an unnamed B'Section because of element "|onePair|". Set the NameB'Section option.");
     for onePair to #o.B'Functions-1 do if  class ((o.B'Functions)_onePair)===B'Slice and not member(NameB'Slice,keys ((o.B'Functions)_onePair)) then  error ("B'Functions contains an unnamed B'Slice because of element "|onePair|". Set the NameB'Slice option. ");
--Now we write the file. The first thing we do is create a file named "input" by default (this default is changed by the NameB'InputFile option).
     if o.StorageFolder=!=null
     then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if o.Verbose then print filesGoHere;
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
     else filesGoHere=IFD;
     openedInputFile:= openOut(filesGoHere|o.NameB'InputFile);
     openedInputFile <<  endl  << "% This input file was written with the Bertini.m2 Macaulay2 package." << endl<<endl;
--The first part of a Bertini input file is the configurations.  We write the configurations followed by a line "%%%ENDCONFIG;". We use this line as marker to write configurations after writing the initial file.
     openedInputFile << "CONFIG" << endl << endl;
     for oneConfig in o.BertiniInputConfiguration do (
       if class oneConfig===Option
       then openedInputFile << toUpper toString((toList oneConfig)_0) << " : " << toString((toList oneConfig)_1) << " ; " << endl
       else if class oneConfig===List then openedInputFile << toString(oneConfig_0) << " : " << toString(oneConfig_1) << " ; " << endl
       else error("BertiniInputConfiguration has an unreadable element: "|toString oneConfig));
     openedInputFile <<  endl << "%%%ENDCONFIG;" << endl;
     openedInputFile << "END;" << endl << endl;
--The second part of a Bertini input file is the INPUT.
     openedInputFile << "INPUT" << endl << endl;
-----Write the Variable groups, parameters, and constants.
--Write the homogeneous variable groups
     if o.HomVariableGroup=!={} and o.AffVariableGroup=!={} then print "Warning: The HomVariableGroup is written first and then the AffVariableGroup is written second.";
     if #o.HomVariableGroup=!=0 and class ((o.HomVariableGroup)_0 )=!=List then theHomVariableGroup:={o.HomVariableGroup} else theHomVariableGroup=o.HomVariableGroup;
     if #theHomVariableGroup=!=0 then
     for oneGroup in theHomVariableGroup do (
--	 openedInputFile << "hom_variable_group "  ;
--	 for j to #oneGroup-2 do (openedInputFile <<toString (oneGroup_j)  << ", ");
--	 openedInputFile << toString(oneGroup_(-1)) << " ; "<< endl
	 writeNamedListToB'InputFile("hom_variable_group",oneGroup,openedInputFile)
	 );
--Write the affine variable groups
     if #o.AffVariableGroup=!=0 and class ((o.AffVariableGroup)_0 )=!=List then theAffVariableGroup:={o.AffVariableGroup} else theAffVariableGroup=o.AffVariableGroup;
     if #theAffVariableGroup=!=0 then
     for oneGroup in theAffVariableGroup do (
	 --openedInputFile << "variable_group "  ;
	 --for j to #oneGroup-2 do (openedInputFile <<toString (oneGroup_j)  << ", ");
	 --openedInputFile << toString(oneGroup_(-1)) << " ; "<< endl
	 writeNamedListToB'InputFile("variable_group",oneGroup,openedInputFile)
	 );
     openedInputFile <<endl;
--Write  variable groups
     if #o.VariableList=!=0 and class ((o.VariableList)_0 )=!=List then theVariableList:={o.VariableList} else theVariableList=o.VariableList;
     if #theVariableList=!=0 then
     for oneGroup in theVariableList do (
	 openedInputFile << "variable "  ;
	 for j to #oneGroup-2 do (openedInputFile <<toString (oneGroup_j)  << ", ");
	 openedInputFile << toString(oneGroup_(-1)) << " ; "<< endl);
     openedInputFile <<endl;
--Write the parameters
     if #o.ParameterGroup=!=0 and class ((o.ParameterGroup)_0 )=!=List then theParameterGroup:={o.ParameterGroup} else theParameterGroup=o.ParameterGroup;
     if #theParameterGroup=!=0 then
     for oneGroup in theParameterGroup do (
--	 openedInputFile << "parameter "  ;
--	 for j to #oneGroup-2 do (openedInputFile <<toString (oneGroup_j)  << ", ");
--	 openedInputFile << toString(oneGroup_(-1)) << " ; "<< endl
	 writeNamedListToB'InputFile("parameter",oneGroup,openedInputFile)
	 );
     openedInputFile <<endl;
--write the path variable
     if #o.PathVariable=!=0 and class ((o.PathVariable) )=!=List then thePathVariable:={o.PathVariable} else thePathVariable=o.PathVariable;
     if #thePathVariable=!=0
     then (
       openedInputFile << "pathvariable "  ;
       for j to #thePathVariable-2 do (openedInputFile <<toString (thePathVariable_j)  << ", ");
       openedInputFile << toString(thePathVariable_(-1)) << " ; "<< endl);
     openedInputFile <<endl;
--If userdefined homotopy then we write the parameters and in terms of the path variable.
     if #o.PathVariable=!=0 then(
     if #o.SetParameterGroup=!=0 and not member( class((o.SetParameterGroup)_0 ),pairTypes) then error"Parameters should be set in terms of the pathvariable, e.g., x=>t,y=>t^2. ";
     oneGroupNames:=for i in o.SetParameterGroup list if class i ===List then first i else if class i===Option then first toList i;
     writeNamedListToB'InputFile("parameter",oneGroupNames,openedInputFile);
     for onePair in o.SetParameterGroup do (
       if class onePair===List
       then openedInputFile << toString(onePair_0) << " = "<<toString(onePair_1)<< " ; "<<endl << endl;
       if class onePair===Option
       then openedInputFile << toString( (toList onePair)_0) << " = "<<toString( (toList onePair)_1)<< " ; "<<endl << endl;
 	    );
       openedInputFile << endl; );
--Write the random complex constants
     if #o.RandomComplex=!=0 then (
       if class(o.RandomComplex_0)=!=List
       then theRandomComplex:={o.RandomComplex}
       else theRandomComplex=o.RandomComplex;
       for aGroup in theRandomComplex do(
     	 --openedInputFile << "random "  ;
     	 --for j to #aGroup-2 do (openedInputFile <<toString (aGroup_j)  << ", ");
     	 --openedInputFile << toString(aGroup_(-1)) << " ; "<< endl
	 writeNamedListToB'InputFile("random",aGroup,openedInputFile)
	 ));
--Write the random real constants
     if #o.RandomReal=!=0 then (
       if class(o.RandomReal_0)=!=List
       then theRandomReal:={o.RandomReal}
       else theRandomReal=o.RandomReal;
       for aGroup in theRandomReal do(
--     	 openedInputFile << "random_real "  ;
--     	 for j to #aGroup-2 do (openedInputFile <<toString (aGroup_j)  << ", ");
--     	 openedInputFile << toString(aGroup_(-1)) << " ; "<< endl
	 writeNamedListToB'InputFile("random_real",aGroup,openedInputFile)
	 ));
--Write the  constants and also the constant ii=I
     if #o.B'Constants=!=0 then (
     openedInputFile << "constant "  ;
     openedInputFile << "ii"  << ", ";
     pairsB'Constants:=for i in o.B'Constants list
       if class i ===List then i else if class i===Option then toList i else error"B'Constants has an invalid element.";
     for j to #(pairsB'Constants)-2 do (openedInputFile <<toString ((pairsB'Constants)_j_0)  << ", ");
     openedInputFile << (pairsB'Constants_(-1))_0 << " ; "<< endl;
     openedInputFile << "ii = I"  << "; "<<endl;
     for onePair in (pairsB'Constants) do (
	 openedInputFile << toString(onePair_0) << " = " <<toString(onePair_1) <<" ; "<<endl
	 ));
--write just the constant "ii = I"
     if #o.B'Constants===0 then (
     openedInputFile << "constant "  ;
     openedInputFile << "ii"  << "; "<<endl;
     openedInputFile << "ii = I"  << "; "<<endl);
     openedInputFile <<endl;
--
--We write the names of the polynomials we want to solve.
-- if B'Polynomials is not used then we do the following to name the polynomials.
     if #o.B'Polynomials===0 and #o.NamePolynomials=!=0 then   (
     openedInputFile << "function "  ;
     for j to #(o.NamePolynomials)-2 do (openedInputFile <<toString ((o.NamePolynomials)_j)  << ", ");
     openedInputFile << (o.NamePolynomials_(-1)) << " ; "<< endl);
--if B'Polynomials is used then we do the following to name the polynomials.
     if #o.B'Polynomials=!=0  then (
     openedInputFile << "function "  ;
     for j to #(o.B'Polynomials)-2 do (openedInputFile << "jade"|j  << ", ");
     openedInputFile << "jade"|toString(#(o.B'Polynomials)-1) << " ; "<< endl);
--
     openedInputFile <<endl;
--Now we write B'Functions followed by the B'Polynomials.
--write the B'Functions
    if #o.B'Functions=!=0 then (
    for onePair in  o.B'Functions do (
      if class onePair===List
      then openedInputFile << toString(onePair_0) << " = "<<toString(onePair_1)<< " ; "<<endl << endl;
      if class onePair===Option
      then openedInputFile << toString( (toList onePair)_0) << " = "<<toString( (toList onePair)_1)<< " ; "<<endl << endl;
      if class onePair===B'Section
      then (openedInputFile << toString(onePair#NameB'Section) << " = "<<par'String(onePair#B'SectionString)<< " ; "<<endl << endl );
      if class onePair===B'Slice
      then for aSection to #(onePair#B'SectionString)-1 do
        (openedInputFile << toString((onePair#NameB'Slice)_aSection) << " = "<<par'String((onePair#B'SectionString)_aSection)<< " ; "<<endl << endl )
 	    );
	openedInputFile << endl);
--Write the B'Polynomials
    if #o.B'Polynomials=!=0 then (
    for onePolynomialIndex to  #o.B'Polynomials-1 do (
      if class ((o.B'Polynomials)_onePolynomialIndex)===B'Section
      then (
	if  member(NameB'Section,keys ((o.B'Polynomials)_onePolynomialIndex)) then print ("Warning: Element "|onePolynomialIndex|" of B'Polynomials is a B'Section with a set NameB'Section option that will be ignored. ");
	if not member(B'SectionString,keys ((o.B'Polynomials)_onePolynomialIndex)) then error("Element "|onePolynomialIndex|" of B'Polynomials is a B'Section with an unset B'SectionString option. ");
	openedInputFile << "jade"|toString(onePolynomialIndex) << " = "<<((o.B'Polynomials)_onePolynomialIndex)#B'SectionString<< " ; "<<endl << endl
	)
      else if class ((o.B'Polynomials)_onePolynomialIndex)===B'Slice
      then error("Element "|onePolynomialIndex|" of B'Polynomials is a B'Slice. B'Slice's must be converted to a list of B'Sections. ")
      else openedInputFile << "jade"|toString(onePolynomialIndex) << " = "<<toString((o.B'Polynomials)_onePolynomialIndex)<< " ; "<<endl << endl
	    );
	openedInputFile << endl);
    openedInputFile << "END;" << endl << endl;
    close openedInputFile        		);

writeNamedListToB'InputFile=(nameList,oneList,openedInputFile)->(
    openedInputFile << nameList|" "  ;
    for j to #oneList-2 do (openedInputFile <<toString (oneList_j)  << ", ");
    openedInputFile << toString(oneList_(-1)) << " ; "<< endl;
    openedInputFile <<endl;
    )


addSlash=(aString)->(
    if aString_-1===" " then error (aString|" cannot end with whitespace.");
    if aString_-1=!="/" then aString=aString|"/";
    return aString    )

makeSampleSolutionsFile = method(TypicalValue => Nothing, Options=>{
	NameSolutionsFile=>"sample_solutions_file",
	NameB'InputFile=>"input",
	StorageFolder=>null,
	SpecifyComponent=>{},
	Verbose=>false
		})
makeSampleSolutionsFile(String,Number) := o ->(IFD,aNumber)->(
    IFD=addSlash(IFD);
    if o.StorageFolder=!=null
    then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
    else filesGoHere=addSlash(IFD);
    theNumberOfPoints:=aNumber;
    if o.SpecifyComponent==={}
    then error"SpecifyComponent option must be set to a point or a list {dimension,component number}.";
    if  class o.SpecifyComponent===List     then (
      theDim:=(o.SpecifyComponent)_0;
      theComponent:=(o.SpecifyComponent)_1) else if instance(o.SpecifyComponent,AbstractPoint) then(
      theDim=(o.SpecifyComponent).cache.Dimension;
      theComponent=(o.SpecifyComponent).cache.ComponentNumber);
    if theNumberOfPoints<1 then error" The number of sample points should be positive. ";
    if not fileExists(filesGoHere|"witness_data") then error"witness_data file does not exist. ";
    s:= run("sed -i -e 's/%%%ENDCONFIG/TRACKTYPE : 2; %%%ENDCONFIG/' "|IFD|o.NameB'InputFile);
    tempfileName:="JADE_tracktype2_1";
    PFile:= openOut(filesGoHere|tempfileName);
    PFile << toString(theDim) << endl ;
    PFile << toString(theComponent) << endl ;
    PFile << toString(theNumberOfPoints) << endl ;
    PFile << "0" << endl ;
    PFile << toString(o.NameSolutionsFile) << endl ;
    close PFile;
    runBertini(IFD,TextScripts=>tempfileName,StorageFolder=>o.StorageFolder,Verbose=>o.Verbose);
    removeFile(filesGoHere|tempfileName)            )


makeMembershipFile = method(TypicalValue => Nothing, Options=>{
	NameSolutionsFile=>"member_points",
	NameB'InputFile=>"input",
	StorageFolder=>null,
	TestSolutions=>{},
	M2Precision=>53,
	Verbose=>false
		})
makeMembershipFile(String) := o ->(IFD)->(
    IFD=addSlash(IFD);
    if o.StorageFolder=!=null
    then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
    else filesGoHere=addSlash(IFD);
    if o.TestSolutions=!={}
    then writeStartFile(IFD,o.TestSolutions,
	NameStartFile=>o.NameSolutionsFile,
	M2Precision=>o.M2Precision	);
    if not fileExists(IFD|o.NameSolutionsFile) then error("The file "|o.NameSolutionsFile|" does not exist in "|IFD|". ");
    if o.Verbose then print (filesGoHere);
    if o.Verbose then print o.NameSolutionsFile;
    moveB'File(IFD,o.NameSolutionsFile,"member_points");
    if not fileExists(filesGoHere|"witness_data") then error"witness_data file does not exist. ";
    s:= run("sed -i -e 's/%%%ENDCONFIG/TRACKTYPE : 3; %%%ENDCONFIG/' "|IFD|o.NameB'InputFile);
    runBertini(IFD,StorageFolder=>o.StorageFolder,Verbose=>o.Verbose)
    )





replaceFirstLine = method(TypicalValue => Nothing, Options=>{
	})
replaceFirstLine(String,String,Thing) := o ->(filesGoHere,fileName,aString)->(
    if toString(filesGoHere)_-1==="/" then aDir:=filesGoHere else aDir=filesGoHere|"/";
    run("sed -i -e "|toExternalString("1s/.*/")|toString(aString)|toExternalString("/")|" "|aDir|fileName)
    )


------------------------------------------------------------------------------


--run("sed -i -e "|toExternalString("1s/.*/")|toString(STuFF)|toExternalString("/")|" "|theDir|"/input")
readFile = method(TypicalValue => Nothing, Options=>{
	})
readFile(String,String,Number) := o ->(filesGoHere,fileName,aInteger)->(
    if toString(filesGoHere)_-1==="/" then aDir:=filesGoHere else aDir=filesGoHere|"/";
    aFile:=openIn(aDir|fileName);
    s:=read(aFile,aInteger);
    close aFile;
    return s
    );
readFile(String,Number) := o ->(filesGoHere,aInteger)->(
    if toString(filesGoHere)_-1==="/" then aDir:=filesGoHere else aDir=filesGoHere|"/";
    aFile:=openIn(aDir|"bertini_session.log");
    s:=read(aFile,aInteger);
    close aFile;
    return s
    );
readFile(String) := o ->(filesGoHere)->(
    if toString(filesGoHere)_-1==="/" then aDir:=filesGoHere else aDir=filesGoHere|"/";
    aFile:=openIn(aDir|"bertini_session.log");
    s:=read(aFile,10000);
    close aFile;
    return s
    );

valueBM2=method(TypicalValue=>String,Options=>{
	M2Precision=>53})
valueBM2(String) := o->(aString)->(
    if class aString =!=String
    then error"Input should be a string. ";
    sepSpaces:=select("[0-9e.+-]+",aString);
    if #sepSpaces===2
    then  (
      coordRealPart:=select("[0-9.+-]+",sepSpaces_0);
      coordImagPart:=select("[0-9.+-]+",sepSpaces_1);
      if #coordRealPart===1 then coordRealPart=append(coordRealPart,"0");
      if #coordImagPart===1 then coordImagPart=append(coordImagPart,"0");
      oneCoord:={coordRealPart_0,coordRealPart_1,coordImagPart_0,coordImagPart_1};
      return (value((oneCoord_0)|"p"|o.M2Precision|"e"|toString(value(oneCoord_1)))+
	ii*value((oneCoord_2)|"p"|o.M2Precision|"e"|toString(value(oneCoord_3)))
	  ))
    else if #sepSpaces===1
    then (
      coordRealPart=select("[0-9.+-]+",sepSpaces_0);
      if #coordRealPart===1 then coordRealPart=append(coordRealPart,"0");
      oneCoord={coordRealPart_0,coordRealPart_1};
      return	(value((oneCoord_0)|"p"|o.M2Precision|"e"|toString(value(oneCoord_1)))
	  ))
    else error"String formatted incorrectly. "
    );

importMainDataFile=method(TypicalValue=>String,Options=>{
	M2Precision=>53,
	NameMainDataFile=>"main_data",
	SpecifyDim=>false,
	Verbose=>false
	})
importMainDataFile(String) := o->(aString)->(
    aString=addSlash aString;
    allInfo:=lines get(aString|o.NameMainDataFile);
    theNumberOfVariables:=value ( (separate(" ",allInfo_0))_3);
    theVariables:=drop(separate(" ",allInfo_1),1);
    zeroDimCase:=false;
    posDimCase:=false;
    regenZeroDimCase:=false;
    if replace("-","",allInfo_3)=!=allInfo_3
    then zeroDimCase=true
    else if replace("is being used","",allInfo_4)=!=allInfo_4
    then regenZeroDimCase=true
    else posDimCase= true;
    if (zeroDimCase or regenZeroDimCase)
    then (
    if zeroDimCase then allInfo=drop(allInfo,4);
    if regenZeroDimCase then allInfo=drop(allInfo,7);
    linesPerSolutions:=theNumberOfVariables+13;
    theListOfPoints:={};
    while #select("Solution",allInfo_0)=!=0 do(
      if o.Verbose then print "win";
      --coordinates
      theCoords:={};
      for i to theNumberOfVariables-1 do(
	  theCoords=append(theCoords,valueBM2(allInfo_(i+10),M2Precision=>o.M2Precision) ) );
      aNewPoint:=point{theCoords};
      --Sol. Number and path number
      theLine0:=separate(" ",allInfo_0);
      aNewPoint.cache.SolutionNumber=value (theLine0_1);
      if o.Verbose then print theLine0;
      aNewPoint.cache.PathNumber=value replace("\\)","",(theLine0_4));
      --Estimated condition number
      theLine1:=separate(":",allInfo_1);
      aNewPoint.cache.ConditionNumber=valueBM2(theLine1_1);
      --FunctionResidual
      theLine2:=separate(":",allInfo_2);
      aNewPoint.cache.FunctionResidual=valueBM2(theLine2_1);
      --NewtonResidual
      theLine3:=separate(":",allInfo_3);
      aNewPoint.cache.NewtonResidual=valueBM2(theLine3_1);
      --FinalTvalue
      theLine4:=separate(":",allInfo_4);
      aNewPoint.cache.FinalTValue=valueBM2(theLine4_1);
      --MaxPrecisionUtilized
      theLine5:=separate(":",allInfo_5);
      aNewPoint.cache.MaxPrecisionUtilized=valueBM2(theLine5_1);
      --PrecisionIncreased
      theLine6:=separate(":",allInfo_6);
      aNewPoint.cache.PrecisionIncreased=valueBM2(theLine6_1);
      --Accuracy Estimate1
      theLine7:=separate(":",allInfo_7);
      aNewPoint.cache.AccuracyEstInternal=valueBM2(theLine7_1);
      --Accuracy Estimate2
      theLine8:=separate(":",allInfo_8);
      if theLine8_1===replace("infinity","",theLine8_1)
      then aNewPoint.cache.AccuracyEst=valueBM2(theLine8_1)
      else aNewPoint.cache.AccuracyEst= infinity;
      --CycleNumber
      theLine9:=separate(":",allInfo_9);
      aNewPoint.cache.CycleNumber=valueBM2(theLine9_1);
      --paths with same endpoint
      theLineX:=separate(":",allInfo_(10+theNumberOfVariables));
      aNewPoint.cache.PathsWithSameEndpoint=drop(drop(separate("  ",theLineX_1),1),-1);--Why the double space? --Do we want all paths or other paths????
      --multiplicity
      theLineY:=separate(":",allInfo_(10+theNumberOfVariables+1));
      aNewPoint.cache.Multiplicity=value(theLineY_1);
      theListOfPoints=append(theListOfPoints,aNewPoint);
      if o.Verbose then   print linesPerSolutions;
      allInfo=drop(allInfo,linesPerSolutions);
      if o.Verbose then print allInfo
      );
    return theListOfPoints);
    if posDimCase
    then   (
    if o.Verbose then print 1;
    allInfo=drop(allInfo,4);
    linesPerSolutions=theNumberOfVariables+6;
    theListOfPoints={};
    while #select("reproduce",allInfo_0)=!=1 do(
      if o.Verbose then print 2;
      if #select("DIMENSION",allInfo_0)=!=0
      then (
	if o.Verbose then print 3;
	theDim:=value (select("[0-9]+",allInfo_0))_0;
        if o.SpecifyDim=!=false and o.SpecifyDim=!=theDim then dimFlag:=false else dimFlag=true;
	allInfo=drop(allInfo,1))
      else if #select("NONSINGULAR",allInfo_0)=!=0 and #select("UNCLASSIFIED",allInfo_0)===0
      then (
	if o.Verbose then print 4;
	solUnclassified:=0;
	theSolutionType:="NONSINGULAR";
	allInfo=drop(allInfo,1))
      else if #select("SINGULAR",allInfo_0)=!=0 and #select("NON",allInfo_0)===0 and #select("UNCLASSIFIED",allInfo_0)===0
      then (
	if o.Verbose then print 5;
	solUnclassified=0;
	theSolutionType="SINGULAR";
	allInfo=drop(allInfo,1))
      else if #select("UNCLASSIFIED NONSINGULAR",allInfo_0)=!=0
      then (
	if o.Verbose then print 5.1;
	solUnclassified=1;
	theSolutionType="NONSINGULAR";
	allInfo=drop(allInfo,1))
      else if #select("UNCLASSIFIED SINGULAR",allInfo_0)=!=0
      then (
	if o.Verbose then print 5.2;
    	solUnclassified=1;
	theSolutionType="SINGULAR";
	allInfo=drop(allInfo,1))
      else if #select("---------------",allInfo_0)=!=0
      then (
	if dimFlag
	then (
	    theCoords={};
      	    for i to theNumberOfVariables-1 do(
	  	theCoords=append(theCoords,valueBM2(allInfo_(i+4-solUnclassified)) ) );
            aNewPoint = point{theCoords};
	    aNewPoint.cache.Dimension=theDim;
	    aNewPoint.cache.SolutionType=theSolutionType;
	    aNewPoint.cache.PathNumber=value ((separate(":",allInfo_1))_1);
	    --
	    if solUnclassified===0
	    then  aNewPoint.cache.ComponentNumber=value ((separate(":",allInfo_2))_1)
            else aNewPoint.cache.ComponentNumber=-1;
	    aNewPoint.cache.ConditionNumber=valueBM2((separate(":",allInfo_(3-solUnclassified)))_1);
      	    --multiplicity
            aNewPoint.cache.Multiplicity=value( (separate(":",allInfo_(4+theNumberOfVariables-solUnclassified)))_1);
            aNewPoint.cache.DeflationsNeeded=value( (separate(":",allInfo_(4+theNumberOfVariables+1-solUnclassified)))_1);
      	    theListOfPoints=append(theListOfPoints,aNewPoint);
      	    --print linesPerSolutions;
      	    allInfo=drop(allInfo,linesPerSolutions-solUnclassified))
        else (allInfo=drop(allInfo,linesPerSolutions); print "1"	))
      else allInfo=drop(allInfo,1));
    return theListOfPoints
    ))



--FUNCTION 2 runBertini
--To run bertini we need to say where we want to output the files.
--Additional options are speciifying the location of the input file (the default is that the input file is located where we output the files)
--B'Exe is how we call Bertini. The default option is how Bertini is usually called in M2 in the init file.
--InputFileName is default to be input. But we can change this if we wanted to.
runBertini= method(TypicalValue => String, Options=>{
	NameB'InputFile=>"input",
    	StorageFolder=>null,
	PreparePH2=>false,
	B'Exe=>BERTINIexe,
	TextScripts=>"",
	Verbose=>false})
runBertini(String) := o ->(IFD)->(--IFD=input file directory
    	IFD=addSlash(IFD);
    	if o.StorageFolder=!=null
    	then (
	  filesGoHere:=addSlash(IFD|o.StorageFolder);
	  if not fileExists(filesGoHere) then mkdir(filesGoHere))
        else filesGoHere=addSlash(IFD);
    	if o.TextScripts=!="" then theTS:=" < "|o.TextScripts else theTS="";
    	if o.Verbose then print o.B'Exe;
    	runSuccess:=run("cd "|filesGoHere|"; "|(o.B'Exe)|" "|IFD|o.NameB'InputFile|theTS|" >bertini_session.log");
    	if runSuccess=!=0
	then (
	  print fileExists(filesGoHere|"bertini_session.log");
	  print readFile(filesGoHere,"bertini_session.log",10000);
	  error"Bertini run failed. ");
	if o.PreparePH2=!=false and runSuccess===0
	then (
	  s:= run("sed -i -e 's/%%%ENDCONFIG/	 PARAMETERHOMOTOPY : 2; %%%ENDCONFIG/' "|IFD|o.NameB'InputFile);
	  moveFile(filesGoHere|"nonsingular_solutions",filesGoHere|"start"));
	);

--Helper function
convertRealNumber=(aNumber)->(
    realPartSeparate:=separate("p",toExternalString ( aNumber));
    realPartMantissa:=realPartSeparate_0;
    if 1=!=#realPartSeparate
    then (separateExponent:=separate("e",realPartSeparate_1);
    	if 1==#separateExponent
    	then realPartExponent:="0"
    	else realPartExponent=(separateExponent)_1;
    	return(realPartMantissa|"e"|realPartExponent))
    else return(realPartMantissa|"e0"));


--takes a number and outputs a string to write in a bertini file: ###e# ###e#
NumberToB'String= method(TypicalValue => Thing, Options=>{
	M2Precision=>53})
NumberToB'String(Thing) := o ->(aNumber)->(
    if class aNumber ===String then print "Warning: String may not  be converted correctly.";
    if class aNumber ===QQ then print "Warning: rational numbers will be converted to floating point.";
    if class aNumber ===String then return aNumber;
    aCNumber:=sub(aNumber,CC_(o.M2Precision));
    return(convertRealNumber(realPart aCNumber)|" "|convertRealNumber(imaginaryPart aCNumber))
    )	;

--takes a number and outputs a string to write in a bertini file: ###e# ###e#
importParameterFile= method(TypicalValue => String, Options=>{
	M2Precision=>53,
	NameParameterFile=>"final_parameters",
	StorageFolder=>null})
importParameterFile(String) := o ->(aString)->(
    aString=addSlash aString;
    if o.StorageFolder=!=null
    then aString=addSlash(aString|o.StorageFolder);
    if class o.NameParameterFile===String then NPF:=o.NameParameterFile;
    if o.NameParameterFile===1 then NPF="start_parameters";
    if o.NameParameterFile===2 then NPF="final_parameters";
    if o.NameParameterFile===3 then NPF="random_values";
    aString=aString|NPF;
    if false===fileExists aString
    then error"The file "|NPF|" does not exist at "|aString|". ";
    getLines:=apply(lines get (aString),i->select("[0-9e.+-]+",i)); -- grabs all lines of the solution file and selects desired words
    expectedNumberOfParameters:=value (getLines_0_0);
    getLines=drop(getLines,2);
    collectedCoordinates:={};
    for i in getLines do (
    if #i==2 then  (
	coordRealPart:=select("[0-9.+-]+",i_0);
	coordImagPart:=select("[0-9.+-]+",i_1);
	if #coordRealPart===1 then coordRealPart=append(coordRealPart,"0");
	if #coordImagPart===1 then coordImagPart=append(coordImagPart,"0");
	oneCoord:={coordRealPart_0,coordRealPart_1,coordImagPart_0,coordImagPart_1};
	collectedCoordinates=append(collectedCoordinates,
	    value((oneCoord_0)|"p"|o.M2Precision|"e"|toString(value(oneCoord_1)))+
	    ii*value((oneCoord_2)|"p"|o.M2Precision|"e"|toString(value(oneCoord_3)))
		   )) else
    if  #i>2  then print ("Warning, a line was not parsed: "|i_0|"...");
    if  #i===1 then   print ("Warning, a line was not parsed: "|i_0|"...");
    );
    if #collectedCoordinates=!= expectedNumberOfParameters then
    print("Warning: Expected "|expectedNumberOfParameters|" parameter(s) but found "|toString(#collectedCoordinates)|" parameter(s).");
    return collectedCoordinates);  --This needs to be documented


writeParameterFile = method(TypicalValue=>Nothing,Options=>{
	NameParameterFile=>"final_parameters",
	M2Precision=>53,
	StorageFolder=>null
	})
writeParameterFile(String,List) := o ->(IFD,listParameters)->(
     IFD=addSlash IFD;
     if o.StorageFolder=!=null
     then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
     else filesGoHere=addSlash(IFD);
     PFile:= openOut(filesGoHere|o.NameParameterFile);
     PFile << toString(length listParameters) << endl << endl;
     for c in listParameters do (
	 	 PFile <<NumberToB'String(c,M2Precision=>o.M2Precision) <<endl
	 );
     PFile << endl;
     close PFile);

writeStartFile = method(TypicalValue=>Nothing,Options=>{
	NameStartFile=>"start",
	M2Precision=>53,
    	StorageFolder=>null
	})
writeStartFile(String,List) := o ->(IFD,listOfListCoords) ->(
     if instance(first listOfListCoords,AbstractPoint) then listOfListCoords=listOfListCoords/coordinates;
     IFD=addSlash(IFD);
     if o.StorageFolder=!=null
     then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
     else filesGoHere=addSlash(IFD);
     PFile:= openOut(filesGoHere|o.NameStartFile);
     PFile << toString(length listOfListCoords) << endl ;
     for listCoords in listOfListCoords do (
	 PFile<<endl;
	 for c in listCoords do(
             PFile <<NumberToB'String(c,M2Precision=>o.M2Precision) <<endl
	 ));
     PFile << endl;
     close PFile);



importSolutionsFile= method(TypicalValue=>Nothing,Options=>{
	NameSolutionsFile=>"raw_solutions",
	M2Precision=>53, OrderPaths=>false,
	StorageFolder=>null,
	Verbose=>false })
importSolutionsFile(String) := o -> (importFrom)-> (
    importFrom=addSlash importFrom;
    if o.StorageFolder=!=null
    then importFrom=addSlash(importFrom|o.StorageFolder);
    if  class o.NameSolutionsFile===String then NSF:=o.NameSolutionsFile;
    if  o.NameSolutionsFile===0 then NSF="nonsingular_solutions";
    if  o.NameSolutionsFile===1 then NSF="real_finite_solutions";
    if  o.NameSolutionsFile===2 then NSF="infinite_solutions";
    if  o.NameSolutionsFile===3 then NSF="finite_solutions";
    if  o.NameSolutionsFile===4 then NSF="start";
    if  o.NameSolutionsFile===5 then NSF="raw_solutions";
    importFrom=importFrom|NSF;
    if false=== fileExists importFrom then error ("File "|NSF|" does not exist.");
    importedFileLines := apply(lines get (importFrom),i->select("[0-9.e+-]+",i)); -- grabs all lines of the solution file and selects desired words.
    if o.Verbose then for i in importedFileLines do print i;
    numberOfsolutionsInFile:=value(importedFileLines_0_0);--the first line of the solution file gives the number of solutions in the file
    if numberOfsolutionsInFile==0 then return {};
    importedFileLines=drop(importedFileLines,1);--drop the first  line
    storeSolutions:={};---We will store the solutions we specified and return this in the end
    collectedCoordinates:={};
    if o.Verbose then print collectedCoordinates;
    if o.OrderPaths===false then(
    for i in importedFileLines do(
    	if o.Verbose then print( "i",i);
	if #i==2 then  (
	    coordRealPart:=select("[0-9.+-]+",i_0);
	    coordImagPart:=select("[0-9.+-]+",i_1);
	    if #coordRealPart===1 then coordRealPart=append(coordRealPart,"0");
	    if #coordImagPart===1 then coordImagPart=append(coordImagPart,"0");
	    oneCoord:={coordRealPart_0,coordRealPart_1,coordImagPart_0,coordImagPart_1};
--	    print oneCoord;
	    collectedCoordinates=append(collectedCoordinates,
	    	value((oneCoord_0)|"p"|o.M2Precision|"e"|toString(value(oneCoord_1)))+
	    	ii*value((oneCoord_2)|"p"|o.M2Precision|"e"|toString(value(oneCoord_3)))
		   ));
--	print collectedCoordinates;
    	if  #i>2  then error ("Line was not parsed: "|i_0|"...")));
    if o.OrderPaths===true then(
    solutionCount:=0;
    for i in importedFileLines do(
      if #i==1 then (
	collectedCoordinates=append(collectedCoordinates,value(i_0));
	solutionCount=solutionCount+1);
      if #i==2 then (
        coordRealPart:=select("[0-9.+-]+",i_0);
	coordImagPart:=select("[0-9.+-]+",i_1);
	if #coordRealPart===1 then coordRealPart=append(coordRealPart,"0");
	if #coordImagPart===1 then coordImagPart=append(coordImagPart,"0");
	oneCoord:={coordRealPart_0,coordRealPart_1,coordImagPart_0,coordImagPart_1};
	if o.Verbose then print oneCoord;
	collectedCoordinates=append(collectedCoordinates,
	  value((oneCoord_0)|"p"|o.M2Precision|"e"|toString(value(oneCoord_1)))+
	    ii*value((oneCoord_2)|"p"|o.M2Precision|"e"|toString(value(oneCoord_3)))
	      ));
	if o.Verbose then print collectedCoordinates;
    	if  #i>2  then error ("Line was not parsed: "|i_0|"...")));
    	numberOfCoordinates:=numerator(#collectedCoordinates/numberOfsolutionsInFile);
	if o.Verbose then print numberOfCoordinates;
    	storeSolutions=for i to numberOfsolutionsInFile-1 list
	  for j to numberOfCoordinates-1 list collectedCoordinates_(i*numberOfCoordinates+j);
    	if o.OrderPaths===true then(
	  if o.Verbose then print "inLoop";
	  sortStoreSolutions:=sort storeSolutions;
	  storeSolutions=for i in sortStoreSolutions list drop(i,1);
    	  if o.Verbose then for i in sortStoreSolutions do print i_0;
    	  if #storeSolutions=!=solutionCount then print "Warning: Unexpected solution count. OrderPaths option should only be set to 'true' when importing solution files with path numbers."
	    );
    return storeSolutions    );






importIncidenceMatrix= method(TypicalValue=>Nothing,Options=>{
	NameIncidenceMatrixFile=>"incidence_matrix",
	StorageFolder=>null,
	Verbose=>false })
importIncidenceMatrix(String) := o -> (importFrom)-> (
    if  class o.NameIncidenceMatrixFile===String
    then NSF:=o.NameIncidenceMatrixFile;
    importFrom=addSlash importFrom;
    if o.StorageFolder=!=null
    then importFrom=addSlash(importFrom|o.StorageFolder);
    importFrom=importFrom|NSF;
    if false=== fileExists importFrom then error ("File "|NSF|" does not exist.");
    importedFileLines := apply(lines get (importFrom),i->select("[0-9.e+-]+",i)); -- grabs all lines of the file and selects desired words.
    if o.Verbose then for i in importedFileLines do print i;
    numberOfNonEmptyCodims:=value(importedFileLines_0_0);--the first line of the incident_matrix file gives the number of non-empty codims. see page p.299 of [NSPSB]
    importedFileLines=drop(importedFileLines,1);--drop the first  line
    indexListForComponents:={};---We will index the components by codimension and component number.
    for i to numberOfNonEmptyCodims-1 do (
      currentCodim:=value(importedFileLines_i_0);
      numberOfComponentsInCurrentCodim:=value(importedFileLines_i_1);
      for i to numberOfComponentsInCurrentCodim-1 do indexListForComponents=append(indexListForComponents,(currentCodim,i))
      );
    totalNumComponents:=#indexListForComponents;
    importedFileLines=drop(importedFileLines,numberOfNonEmptyCodims+1);--the plus one is for an empty line that we need to drop
    numberOfTestPoints:=value(importedFileLines_0_0);
    importedFileLines=drop(importedFileLines,1+1);--the plus one is for an empty line that we need to drop
    incidenceList:={};
    for i to numberOfTestPoints-1 do(
      inComponent:={};
      for oneTest to totalNumComponents-1 do if value(importedFileLines_i_oneTest)===1 then inComponent=append(inComponent,indexListForComponents_oneTest);
      incidenceList=append(incidenceList,inComponent)
      );
    return incidenceList    );




-------MULTIPROJECTIVE POINTS AND SLICES
--B'MultiProjectivePoint=new Type of MutableHashTable;
B'Section=new Type of MutableHashTable;
B'Slice= new Type of MutableHashTable;
--B'WitnessSet= new Type of MutableHashTable;

par'String=(aString)->("("|toString(aString)|")");
makeB'Section = method(TypicalValue=>Nothing,Options=>{
	ContainsPoint=>{},
	B'NumberCoefficients=>{},
    	B'Homogenization=>1,
	RandomCoefficientGenerator=>(()->(2*random(CC)-random(CC))),
	NameB'Section=>null
	 })
makeB'Section(List) := o -> (oneVariableGroup)-> (
    theSection:=new B'Section;
    theSectionString:="";
    theNumberCoefficients:={};
    createsNumbers:=o.RandomCoefficientGenerator;
    theSpecifiedCoefficients:={};
    if o.B'NumberCoefficients=!={} then (
      theSpecifiedCoefficients=o.B'NumberCoefficients;
      theNumberCoefficients=o.B'NumberCoefficients);
    for aVar to #oneVariableGroup-1 do (
      if theSpecifiedCoefficients==={} then (
	theCoefficient:=createsNumbers();
	theNumberCoefficients=append(theNumberCoefficients,theCoefficient))
      else  theCoefficient=theSpecifiedCoefficients_aVar;
      theSectionString=theSectionString|par'String theCoefficient;
      if oneVariableGroup_aVar=!=null then  (
	theSectionString=theSectionString|"*";
    	if parent class o.ContainsPoint===MutableHashTable
	then theContainsPoint:=(o.ContainsPoint#Coordinates)
	else  theContainsPoint=o.ContainsPoint;
	if o.ContainsPoint==={}
	then theSectionString=theSectionString|par'String oneVariableGroup_aVar
	else theSectionString=theSectionString|"("|toString(oneVariableGroup_aVar)|"-"|par'String(o.B'Homogenization)|"*"|par'String ( (theContainsPoint)_aVar)|")"
	);
      if #oneVariableGroup-1=!=aVar then theSectionString=theSectionString|"+");
    theSection.B'SectionString=theSectionString;
    theSection.B'NumberCoefficients=theNumberCoefficients;
    if o.ContainsPoint=!={} then theSection.B'Homogenization=o.B'Homogenization;
    if o.NameB'Section=!=null then theSection.NameB'Section=toString(o.NameB'Section);
    return theSection
  )



makeB'Slice = method(TypicalValue=>Nothing,Options=>{
	ContainsMultiProjectivePoint=>{},
    	ContainsPoint=>{},
	B'NumberCoefficients=>{},
	B'Homogenization=>{},
	RandomCoefficientGenerator=>(()->(2*random(CC)-random(CC))),
	NameB'Slice=>null
	 })
makeB'Slice(Thing,List) := o ->(sliceType,multipleVariableGroups)->(
--
    if class sliceType===ZZ then (
      numberOfSections:=sliceType;
      numSliceTypes:=1;
      AssumeOneGroup:=true);
    if class sliceType===List then (
      numberOfSections=sum sliceType;
      numSliceTypes=#sliceType;
      AssumeOneGroup=false);
    if multipleVariableGroups==={} then error "An empty list is not a valid input.";
----------
    if AssumeOneGroup===true then (
      if class multipleVariableGroups_0===List then error"If sliceType is an integer the second input cannot be a list of lists.";
      multipleVariableGroups={multipleVariableGroups};
      if o.B'Homogenization=!={} and class o.B'Homogenization===List then error"If sliceType is an integer then B'Homogenization must be {} or not a list.";
      if o.B'Homogenization==={} then theHomogenization:={1};
      if o.B'NumberCoefficients=!={} then(
	if class ((o.B'NumberCoefficients)_0_0)===List then error"When sliceType is an integer B'NumberCoefficients cannot be a list of lists. ";
	if class ((o.B'NumberCoefficients)_0_0)=!=List then  theCoefs:=o.B'NumberCoefficients));
----------
    if AssumeOneGroup===false then (
      if class o.B'Homogenization=!=List then error"When sliceType is a list, B'Homogenization should be a list.";
      if o.B'Homogenization==={} then theHomogenization=for i in multipleVariableGroups list 1;
      if o.B'Homogenization=!={} then theHomogenization=o.B'Homogenization;
      if o.B'NumberCoefficients=!={} then(
	if class ((o.B'NumberCoefficients)_0)=!=List then error"When sliceType is a list B'NumberCoefficients should be a list of lists. ";
	if class ((o.B'NumberCoefficients)_0)===List then  theCoefs=o.B'NumberCoefficients));
----------
    if o.B'NumberCoefficients==={} then theCoefs=for i to numberOfSections-1 list {};
--    print numberOfSections;
--    print theCoefs;
    if #theCoefs=!=numberOfSections then error "The number of sets of coefficients of B'NumberCoefficients does not match the number of sections to be made. ";
    if #theHomogenization=!=#multipleVariableGroups then error "B'Homogenization does not match the number of variable groups. ";
    if class o.NameB'Slice===List and #o.NameB'Slice=!=numberOfSections then error"When NameB'Slice is a list, the number of elements should equal the number of sections being made. ";
--
    createsNumbers:=o.RandomCoefficientGenerator;
    if class sliceType===ZZ then sliceType={sliceType};
--
    theSlice:= new B'Slice;
    listSections:={};
--
    if o.ContainsMultiProjectivePoint=!={} and parent class o.ContainsMultiProjectivePoint ===MutableHashTable then  theMultiProjectivePoint:=o.ContainsMultiProjectivePoint#Coordinates;
    if o.ContainsMultiProjectivePoint=!={} and parent class o.ContainsMultiProjectivePoint ===VisibleList then  theMultiProjectivePoint=o.ContainsMultiProjectivePoint;
    if o.ContainsPoint=!={} and parent class o.ContainsPoint===MutableHashTable then  theMultiProjectivePoint={o.ContainsPoint#Coordinates};
    if o.ContainsPoint=!={} and parent class o.ContainsPoint===VisibleList then  theMultiProjectivePoint={o.ContainsPoint};
    if o.ContainsPoint==={} and o.ContainsMultiProjectivePoint==={} then theMultiProjectivePoint=for i to numberOfSections list {};
--    print theMultiProjectivePoint;
    sliceCount:=0;
    for useGroup to numSliceTypes-1 do(
      for oneSlice to (sliceType_useGroup)-1 do(
	oneVariableGroup:=multipleVariableGroups_(useGroup);
      	oneSetCoefs:=theCoefs_sliceCount;
      	theNameB'Section:=if o.NameB'Slice===null
	then null else(
          if class o.NameB'Slice===List
	  then ((o.NameB'Slice)_sliceCount)
	  else (toString (o.NameB'Slice)|toString sliceCount));
      listSections=append(listSections,makeB'Section(oneVariableGroup,
	  B'Homogenization=>theHomogenization_useGroup,
	  ContainsPoint=>theMultiProjectivePoint_(useGroup),
	  B'NumberCoefficients=>oneSetCoefs,
	  RandomCoefficientGenerator=>createsNumbers,
	  NameB'Section=>theNameB'Section
	  ));
      sliceCount=sliceCount+1) );
    theSlice.ListB'Sections=listSections;
    theSlice.B'SectionString=for i in theSlice#ListB'Sections list i#B'SectionString;
    theSlice.B'NumberCoefficients=for i in theSlice#ListB'Sections list i#B'NumberCoefficients;
    if o.B'Homogenization=!={} then theSlice.B'Homogenization=for i in theSlice#ListB'Sections list i#B'Homogenization;
    theSlice.NameB'Slice=if o.NameB'Slice=!=null then for i in theSlice#ListB'Sections list i#NameB'Section;
    return theSlice)


sortMainDataComponents = method(TypicalValue=>List,Options=>{
	 })
sortMainDataComponents(List) := o ->(importedMD)->(
    organizedData:={};
    while #importedMD>0 do(
      firstPoint:=importedMD_0;
      oneComponent:={};
      for onePoint in importedMD do(
	if (firstPoint.cache.Dimension)==(onePoint.cache.Dimension) and
	(firstPoint.cache.ComponentNumber==onePoint.cache.ComponentNumber) then (
	  oneComponent=append(oneComponent,onePoint);
          importedMD=delete(onePoint,importedMD)));
      organizedData=append(organizedData,oneComponent));
  return organizedData)



subPoint = method(TypicalValue=>List,Options=>{
	SpecifyVariables=>false,
	SubIntoCC=>false,
	M2Precision=>53
	 })
subPoint(Thing,List,Thing) := o ->(polyOrMatrix,listVars,aPoint)->(
    if o.SubIntoCC===true and o.SpecifyVariables=!=false then (
      if #o.SpecifyVariables=!=listVars then print"Warning: SubIntoCC may set unassigned variables to be zero." );
    if instance(aPoint, AbstractPoint) then coords:=coordinates aPoint else
    if instance(aPoint, Matrix) then coords=flatten entries aPoint else
    if instance(aPoint, List) then coords=aPoint else print "class of "|toString aPoint|" is not recognized.";
    if false=== o.SpecifyVariables then selectedVars:=listVars else selectedVars=o.SpecifyVariables;
    afterSub:=sub(polyOrMatrix,flatten for i to #listVars-1 list
      if member(listVars_i,selectedVars) then listVars_i=>coords_i else {}
    );
    if o.SubIntoCC===true then
      return sub(afterSub,CC_(o.M2Precision)) else if
      o.SubIntoCC===false then return afterSub else error"SubIntoCC should be set to true or false.")


moveB'File = method(TypicalValue=>List,Options=>{
    	SubFolder=>null,
	MoveToDirectory=>null,
  	CopyB'File=>false
	 })
moveB'File(String,String,String) := o ->(storeFiles,originalName,newName)->(
    if o.SubFolder=!=null and o.MoveToDirectory=!=null then error"SubFolder and MoveToDirectory cannot both be set.";
--
    storeFiles=addSlash(storeFiles);
--
    if o.SubFolder=!=null then finalDirectory:=storeFiles|o.SubFolder;
    if o.MoveToDirectory=!=null then finalDirectory=o.MoveToDirectory;
    if o.MoveToDirectory===null and o.SubFolder===null then finalDirectory=storeFiles;
--
    if finalDirectory_-1===" " then error ("MoveToDirectory nor SubFolder cannot end with whitespace.");
    if finalDirectory_-1=!="/" then finalDirectory=finalDirectory|"/";
--
  if (storeFiles|originalName)=!=(finalDirectory|newName)
  then(
    if o.CopyB'File===false then moveFile(storeFiles|originalName,finalDirectory|newName);
    if o.CopyB'File===true then copyFile(storeFiles|originalName,finalDirectory|newName))
)

--TODO: radicalList needs a more descriptive name
		 radicalList=method(TypicalValue=>Thing,Options=>{
		 	})
		 radicalList(List,Number) := o ->(aList,aTolerance)->(
		     newList:={aList_0};
		     for i to #aList-1 do (
		 	appendToList:=true;
		 	for j in newList do if (abs(j-aList_i)<aTolerance) then appendToList=false;
		 	if appendToList then newList=append(newList,aList_i));
		     return newList)
		 radicalList(List) := o ->(aList)->(
		     aTolerance:=1e-10;
		     newList:={aList_0};
		     for i to #aList-1 do (
		 	appendToList:=true;
		 	for j in newList do if (abs(j-aList_i)<aTolerance) then appendToList=false;
		 	if appendToList then newList=append(newList,aList_i));
		     return newList)







--##########################################################################--
-- TESTS
--##########################################################################--

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniIsProjective.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniParameterHomotopy.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniPosDimSolve.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniRefineSols.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniSample-bertiniComponentMemberTest.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniTrackHomotopy.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniZeroDimSolve.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/makeBInputFile.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/runBertini.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/importSolutionsFile.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/importMainDataFile.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/makeBSection.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/makeBSlice.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/makeMembershipFile.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/subPoint.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/moveBFile.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/radicalList.tst.m2")
///

TEST///
load concatenate(Bertini#"source directory","./Bertini/TST/bertiniUserHomotopy.tst.m2")
///

--##########################################################################--
-- DOCUMENTATION
--##########################################################################--

beginDocumentation()

doc ///
  Key
    Bertini
  Headline
    software for numerical algebraic geometry
  Description
    Text
      Interfaces the functionality of the software {\tt Bertini}
      to solve polynomial systems and perform calculations in
      {\em numerical algebraic geometry}. The software is available at
      @HREF"http://bertini.nd.edu/"@. {\tt Bertini} is under ongoing development by
      D. Bates, J. Hauenstein, A. Sommese, and C. Wampler.

      The user may place the executable program {\tt bertini} in the execution path.
      Alternatively, the path to the executable needs to be specified, for instance,
    Example
      needsPackage("Bertini", Configuration=>{"BERTINIexecutable"=>"/folder/subfolder/bertini"})
    Text
      Below is a simple example using the most popular function,
      a basic zero-dimensional solve with no special options.
    Example
      R = CC[x,y]
      F = {x^2-1,y^2-2}
      solns = bertiniZeroDimSolve(F)
///;
------------------------------------------------------
------MAIN METHODS ------------
------------------------------------------------------
--bertiniZeroDimSolve,bertiniPosDimSolve,bertiniParameterHomotopy,bertiniUserHomotopy,bertiniComponentMemberTest,bertiniSample
doc ///
  Key
    bertiniZeroDimSolve
    (bertiniZeroDimSolve,Ideal)
    (bertiniZeroDimSolve,List)
  Headline
    a main method to solve a zero-dimensional system of equations
  Usage
    S = bertiniZeroDimSolve F
    S = bertiniZeroDimSolve I
    S = bertiniZeroDimSolve(I, UseRegeneration=>1)
  Inputs
    F:List
      a list of ring elements (system need not be square)
    I:Ideal
      an ideal defining a variety
  Outputs
    S:List
      a list of points that are contained in the variety of F
  Description
    Text
      This method finds isolated solutions to the system F via numerical polynomial homotopy continuation
      by (1) building a Bertini input file from the system F,
      (2) calling Bertini on this input file,
      (3) returning solutions from a machine readable file that is an output from Bertini.
    Example
      R = CC[x,y];
      F = {x^2-1,y^2-2};
      S = bertiniZeroDimSolve F
    Text
      Each solution is of type @TO Point@.  Additional information about the solution can be accessed by using @TO peek@.
    Example
      peek S_0
    Text
      Bertini uses a multihomogeneous homotopy as a default, but regeneration can be deployed with the option UseRegeneration=>1 .
    Example
      R = CC[x];
      F = {x^2*(x-1)};
      S = bertiniZeroDimSolve F
      B = bertiniZeroDimSolve(F,UseRegeneration=>1)
    Text
      Variables must begin with a letter (lowercase or capital) and
      can only contain letters, numbers, underscores, and square brackets.
      Regeneration in bertiniZeroDimSolve only finds nonsingular isolated points.
///

--Options
doc ///
  Key
    TopDirectory
    [bertiniParameterHomotopy, TopDirectory]
    [bertiniZeroDimSolve,TopDirectory]
    [bertiniUserHomotopy,TopDirectory]
  Headline
    Option to change directory for file storage.
  Usage
    bertiniParameterHomotopy(...,TopDirectory=>String)
    bertiniZeroDimSolve(...,TopDirectory=>String)
    bertiniUserHomotopy(...,TopDirectory=>String)
  Description
    Text
      This option specifies a directory to store Bertini output files.
///

doc ///
  Key
    UseRegeneration
  Headline
    an option specifying when to use regeneration
  Usage
    bertiniParameterHomotopy(...,TopDirectory=>String)
    bertiniZeroDimSolve(...,TopDirectory=>String)
    bertiniUserHomotopy(...,TopDirectory=>String)
  Description
    Text
      This option is set to 1 to have Bertini use regeneration when solving a polynomial system.

///

doc ///
  Key
    bertiniPosDimSolve
    (bertiniPosDimSolve,Ideal)
    (bertiniPosDimSolve,List)
  Headline
    a main method that is used to produce witness sets
  Usage
    V = bertiniPosDimSolve I
    V = bertiniPosDimSolve F
  Inputs
    F:List
      a list of ring elements defining a variety
  Outputs
    V:NumericalVariety
      a numerical irreducible decomposition of the variety defined by F
  Description
    Text
      The method {\tt bertiniPosDimSolve} calls  {\tt Bertini} to find
      a numerical irreducible decomposition of the zero-set of F.  The decomposition is
      returned as the @TO NumericalVariety@ NV.  Witness sets of NV contain approximations
      to solutions of the system F=0.
      Bertini (1) writes the system to temporary files,
      (2) invokes {\tt Bertini}'s solver with {\tt TrackType => 1},
      (3) Bertini uses a cascade homotopy to find witness supersets in each dimension,
      (4) removes extra points using a membership test or local dimension test,
      (5) deflates singular witness points, and finally
      (6) decomposes using a combination of monodromy and a linear trace test
    Example
      R = QQ[x,y,z]
      F = {(y^2+x^2+z^2-1)*x,(y^2+x^2+z^2-1)*y}
      S = bertiniPosDimSolve F
      S#1_0#Points -- 1_0 chooses the first witness set in dimension 1
    Text
      Each @TO WitnessSet@ is accessed by dimension and then list position.
    Example
      S#1 --first specify dimension
      peek oo_0 --then list position
    Text
      In the example, we find two components, one component has dimension 1 and degree 1 and the other has
      dimension 2 and degree 2.  We get the same results using symbolic methods.
    Example
      PD=primaryDecomposition( ideal F)
      dim PD_0
      degree PD_0
      dim PD_1
      degree PD_1
///

doc ///
  Key
    bertiniSample
    (bertiniSample, ZZ, WitnessSet)
  Headline
    a main method to sample points from an irreducible component of a variety
  Usage
    V = bertiniSample (n, W)
  Inputs
    n:ZZ
      an integer specifying the number of desired sample points
    W:WitnessSet
      a witness set for an irreducible component
  Outputs
    L:List
      a list of sample points
  Description
    Text
      Samples points from an irreducible component of a variety using Bertini.  The irreducible
      component needs to be in its numerical form as a @TO WitnessSet@.  The method
      @TO bertiniPosDimSolve@ can be used to generate a witness set for the component.
      Bertini (1) writes the witness set to a temporary file,
      (2) invokes {\tt Bertini}'s solver with option {\tt TrackType => 2}, and
      (3 moves the hyperplanes defined in the @TO WitnessSet@ W within the space until the desired points are sampled,
      (4) stores the output of {\tt Bertini} in a temporary file, and finally
      (5) parses and outputs the solutions.
    Example
      R = CC[x,y,z]
      F = { (y^2+x^2+z^2-1)*x, (y^2+x^2+z^2-1)*y }
      NV = bertiniPosDimSolve(F)
      W = NV#1_0 --z-axis
      bertiniSample(4, W)
///


doc ///
  Key
    bertiniTrackHomotopy
    (bertiniTrackHomotopy, RingElement, List, List)
  Headline
    a main method to track using a user-defined homotopy
  Usage
    S0=bertiniTrackHomotopy(t, H, S1)
  Inputs
    t:RingElement
      a path variable
    H:List
      a list polynomials that define the homotopy with respect to the path variable
    S1:List
      a list of solutions to the start system
  Outputs
    S0:List
      a list of solutions to the target system
  Description
    Text
      This method calls {\tt Bertini} to track a user-defined homotopy.  The
      user needs to specify the homotopy H, the path variable t, and a list
      of start solutions S1.
      Bertini (1) writes the homotopy and start solutions to temporary files,
      (2) invokes {\tt Bertini}'s solver with configuration keyword {\tt UserHomotopy => 1}
      in the affine case and {\tt UserHomotopy => 2} in the projective situation,
      (3) stores the output of {\tt Bertini} in a temporary file, and
      (4) parses a machine readable file to output a list of solutions.
    Example
      R = CC[x,t]; -- include the path variable in the ring
      H = { (x^2-1)*t + (x^2-2)*(1-t)};
      sol1 = point {{1}};
      sol2 = point {{-1}};
      S1= { sol1, sol2  };--solutions to H when t=1
      S0 = bertiniTrackHomotopy (t, H, S1) --solutions to H when t=0
      peek S0_0
    Text
      In the previous example, we solved $x^2-2$ by moving
      from $x^2-1$ with a linear homotopy. {\tt Bertini} tracks homotopies starting at
      $t=1$ and ending at $t=0$. Final solutions are of the type Point.
    Example
      R=CC[x,y,t]; -- include the path variable in the ring
      f1=(x^2-y^2);
      f2=(2*x^2-3*x*y+5*y^2);
      H = { f1*t + f2*(1-t)}; --H is a list of polynomials in x,y,t
      sol1=    point{{1,1}}--{{x,y}} coordinates
      sol2=    point{{ -1,1}}
      S1={sol1,sol2}--solutions to H when t=1
      S0=bertiniTrackHomotopy(t, H, S1, IsProjective=>1) --solutions to H when t=0
    Text
      Variables must begin with a letter (lowercase or capital) and
      can only contain letters, numbers, underscores, and square brackets.
///

doc ///
  Key
    bertiniUserHomotopy
    (bertiniUserHomotopy, Thing, List, List, List)
  Headline
    a main method to track a user-defined homotopy
  Usage
    S0=bertiniUserHomotopy(t, P, H, S1)
  Inputs
    t:RingElement
      a path variable
    P: List
      a list of options that set the parameters
    H:List
      a list of polynomials that define the homotopy
    S1:List
      a list of solutions to the start system
  Outputs
    S0:List
      a list of solutions to the target system
  Description
    Text
      This method calls {\tt Bertini} to track a user-defined homotopy.  The
      user needs to specify the homotopy H, the path variable t, and a list
      of start solutions S1.
      Bertini (1) writes the homotopy and start solutions to temporary files,
      (2) invokes {\tt Bertini}'s solver with configuration keyword {\tt UserHomotopy => 2},
      (3) stores the output of {\tt Bertini} in a temporary file, and
      (4) parses a machine readable file to output a list of solutions.
    Example
      R = CC[x,a,t]; -- include the path variable in the ring
      H = { (x^2-1)*a + (x^2-2)*(1-a)};
      sol1 = point {{1}};
      sol2 = point {{-1}};
      S1= { sol1, sol2  };--solutions to H when t=1
      S0 = bertiniUserHomotopy (t,{a=>t}, H, S1) --solutions to H when t=0
      peek S0_0
    Example
      R=CC[x,y,t,a]; -- include the path variable in the ring
      f1=(x^2-y^2);
      f2=(2*x^2-3*x*y+5*y^2);
      H = { f1*a + f2*(1-a)}; --H is a list of polynomials in x,y,t
      sol1=    point{{1,1}}--{{x,y}} coordinates
      sol2=    point{{ -1,1}}
      S1={sol1,sol2}--solutions to H when t=1
      S0=bertiniUserHomotopy(t,{a=>t}, H, S1, HomVariableGroup=>{x,y}) --solutions to H when t=0
///


doc ///
  Key
    bertiniComponentMemberTest
    (bertiniComponentMemberTest, List, NumericalVariety)
  Headline
    a main method to test whether points lie on a given variety
  Usage
    L = bertiniComponentMemberTest (pts, NV)
  Inputs
    pts:List
      a list of points to test
    NV:NumericalVariety
      a numerical variety to test if points lie on a given irreducible component
  Outputs
    L:List
      entries are lists of witness sets containing the test point
  Description
    Text
      This method checks whether the test points pts lie on NV using {\tt Bertini} by
      (1) writing the witness set information of NV and the test points to temporary files,
      (2) invokes {\tt Bertini}'s solver with option {\tt TRACKTYPE => 3},
      (3) stores output of {\tt Bertini} in temporary file,
      (4) parses and outputs the solutions.
    Example
      R = CC[x,y,z];
      F = {(y^2+x^2+z^2-1)*x,(y^2+x^2+z^2-1)*y};
      NV = bertiniPosDimSolve(F)
      pts = {{0,0,0}} --z-axis
      bertiniComponentMemberTest(pts, NV)
    Text
      In the current implementation, at most one witness set is listed for each test point although the point may lie on more than one component.
///

doc ///
  Key
    bertiniRefineSols
    (bertiniRefineSols, ZZ, List, List)
  Headline
    sharpen solutions to a prescribed number of digits
  Usage
    S = bertiniRefineSols(d, F, l)
  Inputs
    d:ZZ
      an integer specifying the number of digits of precision
    F:List
      a list of polynomials (system need not be square)
    l:List
      a list of points to be sharpened
  Outputs
    S:List
      a list of solutions of type Point
  Description
    Text
      This method takes the list l of solutions of F and sharpens them to d digits using the sharpening module of {\tt Bertini}.
    Example
      R = CC[x,y];
      F = {x^2-2,y^2-2};
      sols = bertiniZeroDimSolve (F)
      S = bertiniRefineSols (100, F, sols)
      coords = coordinates S_0
      coords_0
    Text
      @TO bertiniRefineSols@ will only refine non-singular solutions and does not currently work for homogeneous systems.
///

doc ///
  Key
    bertiniParameterHomotopy
    (bertiniParameterHomotopy,List,List,List)
  Headline
    a main method to perform a parameter homotopy in Bertini
  Usage
    S = bertiniParameterHomotopy(F,P,T)
  Inputs
    F:List
      a list of polynomials
    P:List
      a list of parameter indeterminants
    T:List
      a list of lists of target parameter values
  Outputs
    S:List
      a list whose entries are lists of solutions for each target system
  Description
    Text
      This method numerically solves several polynomial systems from
      a parameterized family at once.  The list F is a system of polynomials
      in ring variables and the parameters listed in P.  The list T is the
      set of parameter values for which solutions to F are desired.  Both stages of
      {\tt Bertini}'s parameter homotopy method are called with {\tt bertiniParameterHomotopy}.
      First, {\tt Bertini} assigns a random complex number to each parameter
      and solves the resulting system, then, after this initial phase, {\tt Bertini} computes solutions
      for every given choice of parameters using a number of paths equal to the exact root count in the
      first stage.
    Example
      R=CC[u1,u2,u3,x,y];
      f1=u1*(y-1)+u2*(y-2)+u3*(y-3); --parameters are u1, u2, and u3
      f2=(x-11)*(x-12)*(x-13)-u1;
      paramValues0={1,0,0};
      paramValues1={0,1+2*ii,0};
      bPH=bertiniParameterHomotopy( {f1,f2}, {u1,u2,u3},{paramValues0 ,paramValues1 })
      bPH_0--the solutions to the system with parameters set equal to paramValues0
    Example
      R=CC[x,y,z,u1,u2]
      f1=x^2+y^2-z^2
      f2=u1*x+u2*y
      finalParameters0={0,1}
      finalParameters1={1,0}
      bPH=bertiniParameterHomotopy( {f1,f2}, {u1,u2},{finalParameters0 ,finalParameters1 },HomVariableGroup=>{x,y,z})
      bPH_0--The two solutions for finalParameters0
    Example
      finParamValues={{1},{2}}
      bPH1=bertiniParameterHomotopy( {"x^2-u1"}, {u1},finParamValues,AffVariableGroup=>{x})
      bPH2=bertiniParameterHomotopy( {"x^2-u1"}, {u1},finParamValues,AffVariableGroup=>{x},OutputStyle=>"OutSolutions")
      class bPH1_0_0
      class bPH2_0_0
    Example
      dir1 := temporaryFileName(); -- build a directory to store temporary data
      makeDirectory dir1;
      bPH5=bertiniParameterHomotopy( {"x^2-u1"}, {u1},{{1},{2}},AffVariableGroup=>{x},OutputStyle=>"OutNone",TopDirectory=>dir1)
      B0=importSolutionsFile(dir1,NameSolutionsFile=>"ph_jade_0")
      B1=importSolutionsFile(dir1,NameSolutionsFile=>"ph_jade_1")
///

------------------------------------------------------------------
------FUNCTIONS WRITING FILES WITHOUT CALLING BERTINI ------------
------------------------------------------------------------------

doc ///
  Key
    makeB'InputFile
    (makeB'InputFile,String)
  Headline
    write a Bertini input file in a directory
  Usage
    makeB'InputFile(s)
  Inputs
    s:String
      a directory where the input file will be written
  Description
    Text
      This function writes a Bertini input file.
      The user can specify CONFIGS for the file using the BertiniInputConfiguration option.
      The user should specify variable groups with the AffVariableGroup (affine variable group) option or HomVariableGroup (homogeneous variable group) option.
      The user should specify the polynomial system they want to solve with the  B'Polynomials option or B'Functions option.
      If B'Polynomials is not used then the user should use the  NamePolynomials option.
    Example
      R=QQ[x1,x2,y]
      theDir = temporaryFileName();
      makeDirectory theDir
      makeB'InputFile(theDir,
	      BertiniInputConfiguration=>{MPType=>2},
     	  AffVariableGroup=>{{x1,x2},{y}},
	      B'Polynomials=>{y*(x1+x2+1)^2+1,x1-x2+1,y-2});
    Example
      R=QQ[x1,x2,y,X]
      makeB'InputFile(theDir,
	      BertiniInputConfiguration=>{MPType=>2},
     	  AffVariableGroup=>{{x1,x2},{y}},
	      NamePolynomials=>{f1,f2,f3},
	      B'Functions=>{
  	     {X,x1+x2+1},
  	     {f1,y*X^2+1},
  	     {f2,x1-x2+1},
  	     {f3,y-2}});
    Example
      R=QQ[x1,x2,y,X]
      makeB'InputFile(theDir,
	       BertiniInputConfiguration=>{MPype=>2},
     	   AffVariableGroup=>{{x1,x2},{y}},
	        B'Polynomials=>{y*X^2+1,x1-x2+1,y-2},
	         B'Functions=>{
	            {X,x1+x2+1}});
    Text
      Variables must begin with a letter (lowercase or capital) and can only
      contain letters, numbers, underscores, and square brackets.
      "jade" should not be used in any expression.
      "I" can only be used to represent the complex number sqrt(-1).
///


doc ///
  Key
    writeStartFile
    (writeStartFile,String,List)
  Headline
    Writes the list of list of coordinates to a file that Bertini can read.
  Usage
    writeStartFile(s,v)
  Inputs
    s:String
      The directory where the Bertini file will be written.
    v:List
      A list of list numbers that will be written to the file.
  Description
    Text
      This function can be used to write "start" files and any other solution file using the option NameStartFile=>"AnyNameYouWant".
    Example
      coordinatesOfTwoPnts={{1,0},{3,4}}
      writeStartFile(storeBM2Files,coordinatesOfTwoPnts);
///




doc ///
 Key
   importSolutionsFile
   (importSolutionsFile,String)
 Headline
   Imports coordinates from a Bertini solution file.
 Usage
   importSolutionsFile(s)
 Inputs
   s:String
     The directory where the file is stored.
 Description
   Text
     After Bertini does a run many files are created.
     This function imports the coordinates of solutions from the simple "raw_solutions" file.
     By using the option NameSolutionsFile=>"real_finite_solutions" we would import solutions from real finite solutions.
     Other common file names are "nonsingular_solutions", "finite_solutions", "infinite_solutions", and "singular_solutions".
   Text
     If the NameSolutionsFile option
     is set to 0 then "nonsingular_solutions" is imported,
     is set to 1 then "real_finite_solutions" is imported,
     is set to 2 then "infinite_solutions" is imported,
     is set to 3 then "finite_solutions" is imported,
     is set to 4 then "start" is imported,
     is set to 5 then "raw_solutions" is imported.
   Example
     R=QQ[x,y]
     makeB'InputFile(storeBM2Files,
     	 AffVariableGroup=>{{x,y}},
	 B'Polynomials=>{x^2-1,y^3-1});
     runBertini(storeBM2Files)
     importSolutionsFile(storeBM2Files)
     importSolutionsFile(storeBM2Files,NameSolutionsFile=>"real_finite_solutions")
     importSolutionsFile(storeBM2Files,NameSolutionsFile=>0)

///;


doc ///
 Key
   importParameterFile
   (importParameterFile,String)
 Headline
   Imports parameters from a Bertini parameter file.
 Usage
   importParameterFile(s)
 Inputs
   s:String
     The directory where the file is stored.
 Description
   Text
     After Bertini does a parameter homotopy many files are created.
     This function imports the parameters from  the "final_parameters" file as the default.
   Example
     writeParameterFile(storeBM2Files,{1,2},NameParameterFile=>"final_parameters");
     importParameterFile(storeBM2Files)

///;

doc ///
 Key
   importMainDataFile
   (importMainDataFile,String)
 Headline
   This function imports points from the main data file form a Bertini run.
 Usage
   importMainDataFile(theDir)
 Inputs
   theDir:String
     The directory where the main_data file is located.
 Description
   Text
     This function does not import a list of coordinates. Instead it imports points from a main_data file. These points contain coordinates, condition numbers, and etc.
     The information the points contain depend on if regeneration was used and if a TrackType 0 or 1 was used.
     When TrackType 1 is used, UNCLASSIFIED points will have component number -1.
   Example
     makeB'InputFile(storeBM2Files,
       AffVariableGroup=>{x,y,z},
       BertiniInputConfiguration=>{{TrackType,1}},
       B'Polynomials=>{"(x^2+y^2+z^2-1)*y"});
     runBertini(storeBM2Files)
     thePoints=importMainDataFile(storeBM2Files)
     witnessPointsDim1= importMainDataFile(storeBM2Files,SpecifyDim=>1)--We can choose which dimension we import points from. There are no witness points in dimension 1.
     sortMainDataComponents(thePoints)
///;


doc ///
 Key
   importIncidenceMatrix
   (importIncidenceMatrix,String)
 Headline
   Imports an incidence matrix file after running makeMembershipFile.
 Usage
   importIncidenceMatrix(s)
 Inputs
   s:String
     The directory where the file is stored.
 Description
   Text
     After running makeMembershipFile Bertini produces an incidence_matrix file.
     The incidence_matrix says which points belong to which components.
     Our incidence matrix is flattened to a list.
     The number of elements in theIM is equal to the number of points in the solutions file.
     Each element of theIM is a list of sequences of 2 elements (codim,component Number).
     Note that we follow the Bertini convention and switch from (dimension,component number) indexing to (codimension,component number) indexing.
   Text
     If the NameIncidenceMatrixFile option is set when we want to import files with a different name.
   Example
    makeB'InputFile(storeBM2Files,
    	BertiniInputConfiguration=>{{TrackType,1}},    AffVariableGroup=>{x,y,z},    B'Polynomials=>{"z*((x+y+z)^3-1)","z*(y^2-3+z)"}    );
    runBertini(storeBM2Files)
    makeSampleSolutionsFile(storeBM2Files,2,SpecifyComponent=>{1,0})
    makeMembershipFile(storeBM2Files,NameSolutionsFile=>"sample_solutions_file")
    theIM=importIncidenceMatrix(storeBM2Files)

///;


doc ///
  Key
    IsProjective
    [bertiniTrackHomotopy, IsProjective]
    --   [bertiniParameterHomotopy, IsProjective]
    [bertiniComponentMemberTest, IsProjective]
    [bertiniPosDimSolve, IsProjective]
    [bertiniRefineSols, IsProjective]
    [bertiniSample, IsProjective]
    --   [bertiniZeroDimSolve, IsProjective]
  Headline
    optional argument to specify whether to use homogeneous coordinates
  Description
    Text
      When set to 1, this option indicates that the input system is homogenized and the output should be given in projective space.
    Example
      R = CC[x,y,z];
      f = {(x^2+y^2-z^2)*(z-x),(x^2+y^2-z^2)*(z+y)};
      bertiniPosDimSolve(f,IsProjective=>1);
///;


doc ///
  Key
    "Variable groups"
    AffVariableGroup
    HomVariableGroup
    [bertiniParameterHomotopy, AffVariableGroup]
    [bertiniParameterHomotopy, HomVariableGroup]
    [makeB'InputFile, AffVariableGroup]
    [makeB'InputFile, HomVariableGroup]
    [bertiniZeroDimSolve,HomVariableGroup]
    [bertiniZeroDimSolve,AffVariableGroup]
    [bertiniUserHomotopy,AffVariableGroup]
  Headline
    an option to group variables and use multihomogeneous homotopies
  Description
    Text
      Grouping the variables has Bertini solve zero dimensional systems using multihomogeneous homotopies.
    Example
      R = CC[x,y];
      F1 = {x*y+1,2*x*y+3*x+4*y+5};
      bertiniZeroDimSolve(F1, AffVariableGroup=>{{x},{y}});
      hR =CC[x0,x1,y0,y1]
      F2 = {x1*y1+x0*y0,2*x1*y1+3*x1*y0+4*x0*y1+5*x0*y0};
      bertiniZeroDimSolve(F2,HomVariableGroup=>{{x0,x1},{y0,y1}});

///;

doc ///
  Key
    "Bertini input configuration"
    MPType
    PRECISION
    ODEPredictor
    TrackTolBeforeEG
    TrackTolDuringEG
    FinalTol
    MaxNorm
    MinStepSizeBeforeEG
    MinStepSizeDuringEG
    ImagThreshold
    CoeffBound
    DegreeBound
    CondNumThreshold
    RandomSeed
    SingValZeroTol
    EndGameNum
    SecurityLevel
    ScreenOut
    OutputLevel
    StepsForIncrease
    MaxNewtonIts
    MaxStepSize
    MaxNumberSteps
    MaxCycleNum
    RegenStartLevel
  Headline
    a configuration option for a Bertini input file
  Description
    Text
      Refer to Appendix E of SIAM Bertini book for full details and list of options.

      MPType: Type of precision (0=double, 1=fixed higher, 2=adaptive).

      PRECISION: Precision, in bits, when used MPType=1. Precision is capitalized here to not conflict with Precision.

      ODEPredictor: Choice of predictor method (9 choices).

      TrackTolBeforeEG: Before endgame zone, Newton error must be less than this for success.

      TrackTolDuringEG: Same as previous, but during endgame.

      FinalTol: Path is deemed successful if final two endpoint approximations agree to FinalTol.

      MaxNorm: If SecurityLevel=0, path is truncated if two consecutive endpoint approximations exceed this value.

      MinStepSizeBeforeEG: Path is truncated if stepsize drops below this level before endgame.

      MinStepSizeDuringEG: Same as previous, but during endgame.

      ImagThreshold: Endpoint deemed real if infinity norm is smaller than this.

      CoeffBound: Useful only if MPType=2, bound on sum of coefficients of each polynomial.

      DegreeBound: Useful only if MPType=2, bound on degree of each polynomial.

      CondNumThreshold: Endpoint is deemed singular if multiple paths lead to it or condition number exceeds this.

      RandomSeed: Useful to repeat runs with the same random numbers.

      SingValZeroTol: Singular value is considered 0 if less than this value, when using fixed precision.

      EndGameNum: Choice of endgame (1=power series, 2=Cauchy, 3=trackback Cauchy).

      UseRegeneration: 1 to use regeneration for a zero-dimensional run.

      SecurityLevel: 1 to avoid truncation of possibly-infinite paths.

      ScreenOut: Level of output to the screen.

      OutputLevel: Level of output to files.

      StepsForIncrease: Number of consecutive Newton corrector successes before increase of stepsize.

      MaxNewtonIts: Newton corrector step deemed failed if no convergence prior to this number of iterations.

      MaxStepSize: Largest stepsize allowed.

      MaxNumberSteps: Max number of steps for entire path.  Path failure if number of steps exceeds this.

      MaxCycleNum: Max cycle number considered during endgame.

      RegenStartLevel: Level at which regeneration begins.

      There are two recommended ways of using the optional arguments based on zero-dim solving and pos-dim solving.

      (1) Specify individual parameters in a function call:
    Example
      CC[x,y]; F = {x^2-1,y^2-1};
      bertiniZeroDimSolve(F,BertiniInputConfiguration=>{RandomSeed=>0,TrackTolBeforeEG=>1e-6,FinalTol=>1e-100})
    Text
      (2) Store your frequently used favorites in an OptionTable
      and pass it as the last argument in each function call:
    Example
      opts = new OptionTable from {BertiniInputConfiguration=>{RandomSeed=>0,TrackTolBeforeEG=>1e-6,FinalTol=>1e-100}}
      G = {x^2+y^2-1};
      bertiniPosDimSolve(G,opts)
///;

doc///
 Key
 	 "Bertini input file declarations: random numbers"
   RandomReal
   RandomComplex
   [makeB'InputFile, RandomReal]
   [makeB'InputFile, RandomComplex]
   [bertiniParameterHomotopy,RandomComplex]
   [bertiniParameterHomotopy,RandomReal]
   [bertiniZeroDimSolve,RandomComplex]
   [bertiniZeroDimSolve,RandomReal]
 Headline
   an option which designates symbols/strings/variables that will be set to be a random real number or random complex number
 Description
   Text
     This option should be set to a list of symbols, strings, or variables.
     Elements of this list will be fixed to random real/complex numbers when Bertini is called.
   Example
     R=QQ[x,y,c1,c2]
     makeB'InputFile(storeBM2Files,
	      AffVariableGroup=>{{x,y}},
	      RandomReal=>{c1,c2},--c1=.1212, c2=.4132 may be written to the input file.
	      B'Polynomials=>{x-c1,y-c2});
   Example
     R=QQ[x,y,c1,c2]
     makeB'InputFile(storeBM2Files,
	      AffVariableGroup=>{{x,y}},
	      RandomComplex=>{c1,c2},--c1=.1212+ii*.1344, c2=.4132-ii*.2144 are written to the input file.
	      B'Polynomials=>{x-c1,y-c2});
   Text
     AFTER Bertini is run, the random values are stored in a file named "random_values".

///;



doc///
 Key
   B'Constants
   [makeB'InputFile, B'Constants]
   [bertiniParameterHomotopy,B'Constants]
   [bertiniZeroDimSolve,B'Constants]
 Headline
   an option to designate the constants for a Bertini Input file
 Description
   Text
     This option should be set to a list of options X=>v with X denoting the
     constant as an indeterminant and v as it's value.
   Example
     R=QQ[z,a,b,c]
     makeB'InputFile(storeBM2Files,
	      BertiniInputConfiguration=>{MPType=>2},
	      AffVariableGroup=>{{z}},
	       B'Constants=>{a=>2,b=>3+2*ii,c=>3/2},
	        B'Polynomials=>{a*z^2+b*z+c});
///;


doc ///
  Key
    [bertiniTrackHomotopy, Verbose]
    [bertiniComponentMemberTest, Verbose]
    [bertiniPosDimSolve, Verbose]
    [bertiniRefineSols, Verbose]
    [bertiniSample, Verbose]
    [bertiniZeroDimSolve, Verbose]
    [bertiniParameterHomotopy, Verbose]
    [makeB'InputFile, Verbose]
    [makeMembershipFile, Verbose]
    [importIncidenceMatrix,Verbose]
    [importMainDataFile,Verbose]
    [importSolutionsFile,Verbose]
    [runBertini,Verbose]
    [makeSampleSolutionsFile,Verbose]
    [bertiniUserHomotopy,Verbose]
  Headline
    Option to silence additional output
  Usage
    bertiniTrackHomotopyVerbose(...,Verbose=>Boolean)
    bertiniUserHomotopyVerbose(...,Verbose=>Boolean)
    bertiniPosDimSolve(...,Verbose=>Boolean)
    bertiniRefineSols(...,Verbose=>Boolean)
    bertiniSample(...,Verbose=>Boolean)
    bertiniZeroDimSolve(...,Verbose=>Boolean)
    bertiniParameterHomotopy(...,Verbose=>Boolean)
    makeB'InputFile(...,Verbose=>Boolean)
    makeMembershipFile(...,Verbose=>Boolean)
    b'PHGaloisGroup(...,Verbose=>Boolean)
    b'PHMonodromyCollect(...,Verbose=>Boolean)
    importIncidenceMatrix(...,Verbose=>Boolean)
    importMainDataFile(...,Verbose=>Boolean)
    importSliceFile(...,Verbose=>Boolean)
    importSolutionsFile(...,Verbose=>Boolean)
    runBertini(...,Verbose=>Boolean)
  Description
    Text
      Use {\tt Verbose=>false} to silence additional output.
///;



doc ///
 Key
   moveB'File
   (moveB'File,String,String,String)
   [moveB'File,MoveToDirectory]
   [moveB'File,SubFolder]
 Headline
   Move or copy files.
 Usage
   moveB'File(s,f,n)
 Inputs
   s:String
     A string giving a directory.
   f:String
     A name of a file.
   s:String
     A new name for the file.
 Description
   Text
     This function takes the file f in the directory s and renames it to n.
   Example
     writeParameterFile(storeBM2Files,{2,3,5,7});
     fileExists(storeBM2Files|"/final_parameters")
     moveB'File(storeBM2Files,"final_parameters","start_parameters")
     fileExists(storeBM2Files|"/final_parameters")
     fileExists(storeBM2Files|"/start_parameters")
     moveB'File(storeBM2Files,"start_parameters","backup",CopyB'File=>true)
     fileExists(storeBM2Files|"/start_parameters")
     fileExists(storeBM2Files|"/backup")
   Text
     The options MoveToDirectory and SubFolder give greater control for where to move the file.
   Example
     Dir1 = temporaryFileName();
     makeDirectory Dir1
     writeParameterFile(storeBM2Files,{2,3,5,7});
     moveB'File(storeBM2Files,"final_parameters","start_parameters",MoveToDirectory=>Dir1)
     fileExists(Dir1|"/start_parameters")
   Example
     makeDirectory (storeBM2Files|"/Dir2")
     writeParameterFile(storeBM2Files,{2,3,5,7});
     moveB'File(storeBM2Files,"final_parameters","start_parameters",SubFolder=>"Dir2")
     fileExists(storeBM2Files|"/Dir2/start_parameters")

///;

doc ///
 Key
   CopyB'File
   [moveB'File, CopyB'File]
 Headline
   an optional argument to specify whether make a copy of the file.
 Description
   Text
     When set to true, a file is copy of the file is made rather than just moved. The default in moveB'File is set to false.
///;


doc ///
 Key
   B'Section
 Headline
   a mutable hash table that gives information about a hyperplane used to slice a numerical variety.
 Description
   Text
     B'Section is a type of mutable hash table. It can be created using makeB'Section.
///;

doc ///
 Key
   B'Slice
 Headline
   a mutable hash table that gives information about a linear space used to slice a numerical variety.
 Description
   Text
     B'Slice is a type of mutable hash table. It can be created using makeB'Slice.
///;

doc ///
 Key
   makeB'Section
   (makeB'Section,List)
   [makeB'Slice,B'Homogenization]
   [makeB'Section,B'NumberCoefficients]
   [makeB'Section,NameB'Section]
   [makeB'Section,B'Homogenization]
   [makeB'Section,ContainsPoint]
   [makeB'Section,RandomCoefficientGenerator]
 Headline
   makeB'Section creates a hash table that represents a hyperplane.
 Usage
   makeB'Section(l)
 Inputs
   l:List
     A list of variables.
 Description
   Text
     makeB'Section allows for easy creation of equations that define hyperplanes.
     The default creates a hash table with two keys: B'NumberCoefficients and B'SectionString.
     The first key is a list of numbers in CC that are coefficients, and the second key is a string representing the linear polynomial.
     The option RandomCoefficientGenerator can be set to a function to generate random numbers for the coefficients.
   Text
     To get affine linear equations include 1 in the input list.
   Text
     To have an affine linear equation that contains a particular point we set the ContainsPoint option to a list of coordinates or a point.
     To get an homogeneous equation that contains a projective point we have to set the ContainsPoint option as well as the B'Homogenization option.
   Example
     s=makeB'Section({x,y,z})
     class s
     randomRealCoefficientGenerator=()->random(RR)
     sReal=makeB'Section({x,y,z},RandomCoefficientGenerator=>randomRealCoefficientGenerator)
     sReal#B'NumberCoefficients
     randomRationalCoefficientGenerator=()->random(QQ)
     sRational=makeB'Section({x,y,z},RandomCoefficientGenerator=>randomRationalCoefficientGenerator)
     sRational#B'NumberCoefficients
   Example
     affineSection=makeB'Section({x,y,z,1})
   Example
     X={x,y,z}
     P={1,2,3}
     affineContainingPoint=makeB'Section({x,y,z},ContainsPoint=>P)
     r= affineContainingPoint#B'SectionString
     print r
   Example
     rHomogeSection= makeB'Section({x,y,z},ContainsPoint=>P,B'Homogenization=>"x+y+z")
     peek rHomogeSection
     print rHomogeSection#B'SectionString
   Example
     f="y^3-x*y+1"
     s1=makeB'Section({x,y,1})
     makeB'InputFile(storeBM2Files,
       AffVariableGroup=>{x,y},
       B'Polynomials=>{f,s1});
     runBertini(storeBM2Files)
     #importSolutionsFile(storeBM2Files)==3

///;


doc ///
 Key
   makeB'Slice
   NameB'Slice
   (makeB'Slice,Thing,List)
   [makeB'Slice,B'NumberCoefficients]
   [makeB'Slice,ContainsMultiProjectivePoint]
   [makeB'Slice,ContainsPoint]
   [makeB'Slice,NameB'Slice]
   [makeB'Slice,RandomCoefficientGenerator]
 Headline
   makeB'Slice creates a hash table that represents a linear slice.
 Usage
   makeB'Slice(sliceType,variableGroups)
 Inputs
   sliceType:List
     A list of integers or integer.
   variableGroups:List
     A list of list of variables or list of variables.
 Description
   Text
     makeB'Slice allows for easy creation of equations that define linear spaces, i.e. slices.
     The default creates a hash table with two keys: B'NumberCoefficients and B'SectionString.
     When we have a multiprojective variety we can different types of slices.
     To make a slice we need to specify the type of slice we want followed by variable groups.
   Example
     sliceType={1,1}
     variableGroups={{x0,x1},{y0,y1,y2}}
     xySlice=makeB'Slice(sliceType,variableGroups)
     peek xySlice
     --Our slice consists of two sections.
     --The ith section is in the variables variableGroups_(sliceType_i)
     for i in  xySlice#B'SectionString do print i
   Example
     --Using the NameB'Slice option we can put a slice in the B'Functions option.
     aSlice=makeB'Slice(3,{x,y,z,1},NameB'Slice=>"f");
     aSlice#NameB'Slice
     makeB'InputFile(storeBM2Files,AffVariableGroup=>{x,y,z},B'Functions=>{aSlice},NamePolynomials=>{"f0","f1","f2"});
   Example
     --We can use slices to determine multidegrees.
     f1="x0*y0+x1*y0+x2*y2"
     f2="x0*y0^2+x1*y1*y2+x2*y0*y2"
     variableGroups={{x0,x1,x2},{y0,y1,y2}}
     xxSlice=makeB'Slice({2,0},variableGroups)
     xySlice=makeB'Slice({1,1},variableGroups)
     yySlice=makeB'Slice({0,2},variableGroups)
     makeB'InputFile(storeBM2Files,
    	 HomVariableGroup=>variableGroups,
    	 B'Polynomials=>{f1,f2}|xxSlice#ListB'Sections);
     runBertini(storeBM2Files)
     xxDegree=#importSolutionsFile(storeBM2Files)
     makeB'InputFile(storeBM2Files,
    	 HomVariableGroup=>variableGroups,
    	 B'Polynomials=>{f1,f2}|xySlice#ListB'Sections);
     runBertini(storeBM2Files)
     xyDegree=#importSolutionsFile(storeBM2Files)
     makeB'InputFile(storeBM2Files,
    	 HomVariableGroup=>variableGroups,
    	 B'Polynomials=>{f1,f2}|yySlice#ListB'Sections);
     runBertini(storeBM2Files)
     yyDegree=#importSolutionsFile(storeBM2Files)

///;



doc ///
 Key
   radicalList
   (radicalList,List)
   (radicalList,List,Number)
 Headline
   A support function that removes multiplicities of numbers in a list up to a tolerance.
 Usage
   radicalList(List,Number)
   radicalList(List)
 Inputs
   L:List
     A list of complex or real numbers.
   N:Number
     A small real number.
 Description
   Text
     This outputs a sublist of complex or real numbers that all have distinct norms up to the tolerance N (default is 1e-10).
   Example
     radicalList({2.000,1.999})
     radicalList({2.000,1.999},1e-10)
     radicalList({2.000,1.999},1e-2)
///;


doc ///
 Key
   NumberToB'String
   (NumberToB'String,Thing)
 Headline
   Translates a number to a string that Bertini can read.
 Usage
   NumberToB'String(n)
 Inputs
   n:Thing
     n is a number.
 Description
   Text
     This function takes a number as an input then outputs a string to represent this number to Bertini.
     The numbers are converted to floating point to precision determined by the option M2Precision.
   Example
     NumberToB'String(2+5*ii)
     NumberToB'String(1/3,M2Precision=>16)
     NumberToB'String(1/3,M2Precision=>128)

///;


doc ///
 Key
   valueBM2
   (valueBM2,String)
 Headline
   This function makes a number in CC from a string.
 Usage
   valueBM2(s)
 Inputs
   s:String
     A string that gives a coordinate.
 Description
   Text
     This function take a string representing a coordinate in a Bertini solutions file or parameter file and makes a number in CC.
     We can adjust the precision using the M2Precision option.
     Fractions should not be in the string s.
   Example
     valueBM2("1.22e-2 4e-5")
     valueBM2("1.22 4e-5")
     valueBM2("1.22 4")
     valueBM2("1.22e+2 4 ")
     n1=valueBM2("1.11",M2Precision=>52)
     n2=valueBM2("1.11",M2Precision=>300)
     toExternalString n1
     toExternalString n2
///;


doc ///
 Key
   subPoint
   (subPoint,Thing,List,Thing)
 Headline
   This function evaluates a polynomial or matrix at a point.
 Usage
   subPoint(f,v,p)
 Inputs
   f:Thing
     A polynomial or a matrix.
   v:List
     List of variables that we will be evaluated at the point.
   p:Thing
     A point or a list of coordinates or a matrix.
 Caveat
   When SubIntoCC is set to true then unset variables will be set to zero or unexpected values.
 Description
   Text
     Evaluate f at a point.
   Example
     R=CC[x,y,z]
     f=z*x+y
     subPoint(f,{x,y},{.1,.2})
     subPoint(f,{x,y,z},{.1,.2,.3},SpecifyVariables=>{y})
   Example
     R=CC_200[x,y,z]
     f=z*x+y
     subPoint(f,{x,y,z},{.1,.2,.3},SubIntoCC=>true)
     subPoint(f,{x,y,z},{.1234567890123456789012345678901234567890p200,
	     0,1},SubIntoCC=>true,M2Precision=>200)

///;

undocumented {
  SetParameterGroup,
ReturnPoints,
B'SectionString,
ContainsMultiProjectivePoint,
ListB'Sections,
PrintMidStatus,
OutputStyle,--TODO remove this option
StorageFolder,
RandomGamma,
SubFolder,
StartParameters,
StartPoints,
OrderPaths,
MultiplicityTol,
ConditionNumTol,
ParameterValues,
NameB'InputFile,--This option allows us to change the name of the input file.
NameParameterFile,
NameSolutionsFile,
NameIncidenceMatrixFile,
NameStartFile,
NameFunctionFile,
--
BertiniInputConfiguration, --This option is a list of pairs of strings. These will be written in the CONFIG part of the Bertini input file.
ParameterGroup,
VariableList,
PathVariable,
B'Polynomials, --a list of polynomials whose zero set we want to solve; when used then the NamePolynomials option is disabled and the polynomials are automatically named jade
NamePolynomials, --A list of names of the polynomials which we want to find the common zero set of.
B'Functions, --A list of list of pairs.
--
runBertini,
InputFileDirectory,
StartFileDirectory,
StartParameterFileDirectory,
B'Exe,
M2Precision,--needs doc
writeParameterFile,
SaveData,
SolutionFileStyle,
--  B'MultiProjectivePoint,
ContainsPoint,
B'NumberCoefficients,
B'Homogenization,
RandomCoefficientGenerator,
NameB'Section,
makeB'TraceInput,
replaceFirstLine,
PreparePH2,
readFile,
NameMainDataFile,
--  linesPerSolutions,
PathNumber,
FinalTValue,
MaxPrecisionUtilized,
PrecisionIncreased,
AccuracyEstInternal,
AccuracyEst,
PathsWithSameEndpoint,
CycleNumber,
FunctionResidual,
Dimension,
SolutionType,
DeflationsNeeded,
--  B'WitnessSet,
SpecifyDim,
NameWitnessSliceFile,
importSliceFile,
TextScripts,
NameWitnessSolutionsFile,
SpecifyComponent,
makeWitnessSetFiles,
makeSampleSolutionsFile,
NameSampleSolutionsFile,
TestSolutions,
makeMembershipFile,
ComponentNumber,
sortMainDataComponents,
MoveToDirectory,
SpecifyVariables,
SubIntoCC
}
end
installPackage("Bertini",RemakeAllDocumentation=>true)

--load "./Bertini/doc.m2";

end
makeWitnessSetFiles = method(TypicalValue => Nothing, Options=>{
	NameWitnessSliceFile=>"linear_slice_file",
    	NameSolutionsFile=>"witness_solutions_file",
	NameB'InputFile=>"input",
	SpecifyComponent=>-2,
	StorageFolder=>null,
	Verbose=>false
		})
makeWitnessSetFiles(String,Number) := o ->(IFD,theDim)->(
    IFD=addSlash(IFD);
    if o.StorageFolder=!=null
    then (
	 filesGoHere:=addSlash(IFD|o.StorageFolder);
	 if not fileExists(filesGoHere) then mkdir(filesGoHere))
    else filesGoHere=addSlash(IFD);
    if not fileExists(filesGoHere|"witness_data") then error"witness_data file does not exist. ";
    s:= run("sed -i -e 's/%%%ENDCONFIG/TRACKTYPE : 4; %%%ENDCONFIG/' "|IFD|o.NameB'InputFile);
    tempfileName:="JADE_tracktype4_1";
    PFile:= openOut(filesGoHere|tempfileName);
    PFile << toString(theDim) << endl ;
    PFile << toString(o.SpecifyComponent) << endl ;
    PFile << toString(o.NameSolutionsFile) << endl ;
    PFile << toString(o.NameWitnessSliceFile) << endl ;
    close PFile;
    runBertini(filesGoHere,TextScripts=>tempfileName,Verbose=>o.Verbose);
    removeFile(filesGoHere|tempfileName);
        )

        --TODO fix this test
        TEST///
        load concatenate(Bertini#"source directory","./Bertini/TST/makeSampleSolutions.tst.m2")
        ///