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
|
\documentclass{article}
\usepackage{/home/axiomgnu/new/mnt/linux/bin/tex/noweb}
\begin{document}
\title{Sorting Facilities}
\author{Timothy Daly}
\maketitle
\begin{abstract}
\end{abstract}
\eject
\tableofcontents
\eject
This is a survey of the explicitly mentioned sorting algorithms
in the Axiom algebra code. Note that there are cases of "embedded"
sorts as in the {\bf chainSubResultants} method from the
{\bf PseudoRemainderSequence \cite{1}} domain. There are also
implicit sorts as items are added to lists individually in sorted
order.
\subsection{aggcat.spad}
\subsubsection{FiniteLinearAggregate}
++ A finite linear aggregate is a linear aggregate of finite length.
++ The finite property of the aggregate adds several exports to the
++ list of exports from {\bf LinearAggregate} such as
++ {\bf reverse}, {\bf sort}, and so on.
<<FiniteLinearAggregate>>=
FiniteLinearAggregate(S:Type): Category == LinearAggregate S with
finiteAggregate
sort: ((S,S)->B,%) -> %
++ sort(p,a) returns a copy of {\bf a} sorted
++ using total ordering predicate p.
if S has OrderedSet then
OrderedSet
sort: % -> %
++ sort(u) returns an u with elements in ascending order.
++ Note: {\bf sort(u) = sort(<=,u)}.
if % has shallowlyMutable then
sort_!: ((S,S)->B,%) -> %
++ sort!(p,u) returns u with its elements ordered by p.
if S has OrderedSet then sort_!: % -> %
++ sort!(u) returns u with its elements in ascending order.
add
if S has OrderedSet then
sort l == sort(_<$S, l)
if % has shallowlyMutable then
sort(f, l) == sort_!(f, copy l)
if S has OrderedSet then
sort_! l == sort_!(_<$S, l)
@
\subsubsection{OneDimensionalArrayAggregate}
One-dimensional-array aggregates serves as models for one-dimensional arrays.
Categorically, these aggregates are finite linear aggregates
with the {\bf shallowlyMutable} property, that is, any component of
the array may be changed without affecting the
identity of the overall array.
Array data structures are typically represented by a fixed area in storage and
therefore cannot efficiently grow or shrink on demand as can list structures
(see however {\bf FlexibleArray} for a data structure which
is a cross between a list and an array).
Iteration over, and access to, elements of arrays is extremely fast
(and often can be optimized to open-code).
Insertion and deletion however is generally slow since an entirely new
data structure must be created for the result.
<<OneDimensionalArrayAggregate>>=
OneDimensionalArrayAggregate(S:Type): Category ==
FiniteLinearAggregate S with shallowlyMutable
add
sort_!(f, a) == quickSort(f, a)$FiniteLinearAggregateSort(S, %)
@
\subsubsection{ListAggregate}
A list aggregate is a model for a linked list data structure.
A linked list is a versatile
data structure. Insertion and deletion are efficient and
searching is a linear operation.
{\bf ListAggregate} uses the operation {\bf split\_!} which is inherited from
{\bf StreamAggregate} which inherites it from it's implementing Domain
{\bf UnaryRecursiveAggregate}.
A unary-recursive aggregate is a one where nodes may have either
0 or 1 children.
This aggregate models, though not precisely, a linked
list possibly with a single cycle.
A node with one children models a non-empty list, with the
{\bf value} of the list designating the head, or {\bf first}, of the
list, and the child designating the tail, or {\bf rest}, of the list.
A node with no child then designates the empty list.
Since these aggregates are recursive aggregates, they may be cyclic.
There we see the definition of {\bf split\_!} as:
<<UnaryRecursiveAggregate>>=
UnaryRecursiveAggregate(S:Type): Category == RecursiveAggregate S with
rest: % -> %
++ rest(u) returns an aggregate consisting of all but the first
++ element of u
++ (equivalently, the next node of u).
rest: (%,N) -> %
++ rest(u,n) returns the \axiom{n}th (n >= 0) node of u.
++ Note: \axiom{rest(u,0) = u}.
if % has shallowlyMutable then
split_!: (%,I) -> %
++ split!(u,n) splits u into two aggregates: \axiom{v = rest(u,n)}
++ and \axiom{w = first(u,n)}, returning \axiom{v}.
++ Note: afterwards \axiom{rest(u,n)} returns \axiom{empty()}.
add
rest(x, n) ==
for i in 1..n repeat
empty? x => error "Index out of range"
x := rest x
x
if S has SetCategory then
split_!(p, n) ==
n < 1 => error "index out of range"
p := rest(p, (n - 1)::N)
q := rest p
setrest_!(p, empty())
q
@
<<ListAggregate>>=
ListAggregate(S:Type): Category == Join(StreamAggregate S,
FiniteLinearAggregate S, ExtensibleLinearAggregate S) with
add
mergeSort: ((S, S) -> B, %, I) -> %
mergeSort(f, p, n) ==
if n = 2 and f(first rest p, first p) then p := reverse_! p
n < 3 => p
l := (n quo 2)::N
q := split_!(p, l)
p := mergeSort(f, p, l)
q := mergeSort(f, q, n - l)
merge_!(f, p, q)
reverse_! x ==
empty? x => x
empty?(y := rest x) => x
setrest_!(x, empty())
while not empty? y repeat
z := rest y
setrest_!(y, x)
x := y
y := z
x
merge_!(f, p, q) ==
empty? p => q
empty? q => p
eq?(p, q) => error "cannot merge a list into itself"
if f(first p, first q)
then (r := t := p; p := rest p)
else (r := t := q; q := rest q)
while not empty? p and not empty? q repeat
if f(first p, first q)
then (setrest_!(t, p); t := p; p := rest p)
else (setrest_!(t, q); t := q; q := rest q)
setrest_!(t, if empty? p then q else p)
r
sort_!(f, l) == mergeSort(f, l, #l)
list x == concat(x, empty())
reduce(f, x) ==
empty? x => error "reducing over an empty list needs the 3 argument form"
reduce(f, rest x, first x)
merge(f, p, q) == merge_!(f, copy p, copy q)
select_!(f, x) ==
while not empty? x and not f first x repeat x := rest x
empty? x => x
y := x
z := rest y
while not empty? z repeat
if f first z then (y := z; z := rest z)
else (z := rest z; setrest_!(y, z))
x
insert_!(s:S, x:%, i:I) ==
i < (m := minIndex x) => error "index out of range"
i = m => concat(s, x)
y := rest(x, (i - 1 - m)::N)
z := rest y
setrest_!(y, concat(s, z))
x
insert_!(w:%, x:%, i:I) ==
i < (m := minIndex x) => error "index out of range"
i = m => concat_!(w, x)
y := rest(x, (i - 1 - m)::N)
z := rest y
setrest_!(y, w)
concat_!(y, z)
x
remove_!(f:S -> B, x:%) ==
while not empty? x and f first x repeat x := rest x
empty? x => x
p := x
q := rest x
while not empty? q repeat
if f first q then q := setrest_!(p, rest q)
else (p := q; q := rest q)
x
delete_!(x:%, i:I) ==
i < (m := minIndex x) => error "index out of range"
i = m => rest x
y := rest(x, (i - 1 - m)::N)
setrest_!(y, rest(y, 2))
x
delete_!(x:%, i:U) ==
(l := lo i) < (m := minIndex x) => error "index out of range"
h := if hasHi i then hi i else maxIndex x
h < l => x
l = m => rest(x, (h + 1 - m)::N)
t := rest(x, (l - 1 - m)::N)
setrest_!(t, rest(t, (h - l + 2)::N))
x
find(f, x) ==
while not empty? x and not f first x repeat x := rest x
empty? x => "failed"
first x
position(f:S -> B, x:%) ==
for k in minIndex(x).. while not empty? x and not f first x repeat
x := rest x
empty? x => minIndex(x) - 1
k
sorted?(f, l) ==
empty? l => true
p := rest l
while not empty? p repeat
not f(first l, first p) => return false
p := rest(l := p)
true
reduce(f, x, i) ==
r := i
while not empty? x repeat (r := f(r, first x); x := rest x)
r
if S has SetCategory then
reduce(f, x, i,a) ==
r := i
while not empty? x and r ^= a repeat
r := f(r, first x)
x := rest x
r
new(n, s) ==
l := empty()
for k in 1..n repeat l := concat(s, l)
l
map(f, x, y) ==
z := empty()
while not empty? x and not empty? y repeat
z := concat(f(first x, first y), z)
x := rest x
y := rest y
reverse_! z
copy x ==
y := empty()
for k in 0.. while not empty? x repeat
k = cycleMax and cyclic? x => error "cyclic list"
y := concat(first x, y)
x := rest x
reverse_! y
copyInto_!(y, x, s) ==
s < (m := minIndex y) => error "index out of range"
z := rest(y, (s - m)::N)
while not empty? z and not empty? x repeat
setfirst_!(z, first x)
x := rest x
z := rest z
y
if S has SetCategory then
position(w, x, s) ==
s < (m := minIndex x) => error "index out of range"
x := rest(x, (s - m)::N)
for k in s.. while not empty? x and w ^= first x repeat
x := rest x
empty? x => minIndex x - 1
k
removeDuplicates_! l ==
p := l
while not empty? p repeat
p := setrest_!(p, remove_!(#1 = first p, rest p))
l
if S has OrderedSet then
x < y ==
while not empty? x and not empty? y repeat
first x ^= first y => return(first x < first y)
x := rest x
y := rest y
empty? x => not empty? y
false
@
\subsection{alql.spad}
\subsubsection{DataList}
This domain provides some nice functions on lists
<<DataList>>=
DataList(S:OrderedSet) : Exports == Implementation where
Exports == ListAggregate(S) with
elt: (%,"sort") -> %
++ {\bf l.sort} returns {\bf l} with elements sorted.
++ Note: {\bf l.sort = sort(l)}
Implementation == List(S) add
elt(x,"sort") == sort(x)
@
\subsection{carten.spad}
\subsubsection{CartesianTensor}
This is an instance of an "embedded sort" that should probably
call one of the general purpose sort routines.
{\bf CartesianTensor(minix,dim,R)} provides Cartesian tensors with
components belonging to a commutative ring R. These tensors
can have any number of indices. Each index takes values from
{\bf minix} to {\bf minix + dim - 1}.
<<CartesianTensor>>=
CartesianTensor(minix, dim, R): Exports == Implementation where
Exports ==> Join(GradedAlgebra(R, NNI), GradedModule(I, NNI)) with
Implementation ==> add
INDEX ==> Vector Integer -- 1-based entries from minix..minix+dim-1
-- permsign!(v) = 1, 0, or -1 according as
-- v is an even, is not, or is an odd permutation of minix..minix+#v-1.
permsign_!(v: INDEX): Integer ==
-- sum minix..minix+#v-1.
maxix := minix+#v-1
psum := (((maxix+1)*maxix - minix*(minix-1)) exquo 2)::Integer
-- +/v ^= psum => 0
n := 0
for i in 1..#v repeat n := n + v.i
n ^= psum => 0
-- Bubble sort! This is pretty grotesque.
totTrans: Integer := 0
nTrans: Integer := 1
while nTrans ^= 0 repeat
nTrans := 0
for i in 1..#v-1 for j in 2..#v repeat
if v.i > v.j then
nTrans := nTrans + 1
e := v.i; v.i := v.j; v.j := e
totTrans := totTrans + nTrans
for i in 1..dim repeat
if v.i ^= minix+i-1 then return 0
odd? totTrans => -1
1
@
\subsection{clifford.spad}
\subsubsection{CliffordAlgebra}
Examples of {\bf Clifford Algebras} are: gaussians, quaternions, exterior
algebras and spin algebras.
<<CliffordAlgebra>>=
CliffordAlgebra(n, K, Q): T == Impl where
n: PositiveInteger
K: Field
Q: QuadraticForm(n, K)
PI ==> PositiveInteger
NNI==> NonNegativeInteger
T ==> Join(Ring, Algebra(K), VectorSpace(K)) with
e: PI -> %
++ e(n) produces the appropriate unit element.
monomial: (K, List PI) -> %
++ monomial(c,[i1,i2,...,iN]) produces the value given by
++ \spad{c*e(i1)*e(i2)*...*e(iN)}.
coefficient: (%, List PI) -> K
++ coefficient(x,[i1,i2,...,iN]) extracts the coefficient of
++ \spad{e(i1)*e(i2)*...*e(iN)} in x.
recip: % -> Union(%, "failed")
++ recip(x) computes the multiplicative inverse of x or "failed"
++ if x is not invertible.
Impl ==> add
Qeelist := [Q unitVector(i::PositiveInteger) for i in 1..n]
dim := 2**n
Rep := PrimitiveArray K
New ==> new(dim, 0$K)$Rep
x, y, z: %
c: K
m: Integer
characteristic() == characteristic()$K
dimension() == dim::CardinalNumber
x = y ==
for i in 0..dim-1 repeat
if x.i ^= y.i then return false
true
x + y == (z := New; for i in 0..dim-1 repeat z.i := x.i + y.i; z)
x - y == (z := New; for i in 0..dim-1 repeat z.i := x.i - y.i; z)
- x == (z := New; for i in 0..dim-1 repeat z.i := - x.i; z)
m * x == (z := New; for i in 0..dim-1 repeat z.i := m*x.i; z)
c * x == (z := New; for i in 0..dim-1 repeat z.i := c*x.i; z)
0 == New
1 == (z := New; z.0 := 1; z)
coerce(m): % == (z := New; z.0 := m::K; z)
coerce(c): % == (z := New; z.0 := c; z)
e b ==
b::NNI > n => error "No such basis element"
iz := 2**((b-1)::NNI)
z := New; z.iz := 1; z
-- The ei*ej products could instead be precomputed in
-- a (2**n)**2 multiplication table.
addMonomProd(c1: K, b1: NNI, c2: K, b2: NNI, z: %): % ==
c := c1 * c2
bz := b2
for i in 0..n-1 | bit?(b1,i) repeat
-- Apply rule ei*ej = -ej*ei for i^=j
k := 0
for j in i+1..n-1 | bit?(b1, j) repeat k := k+1
for j in 0..i-1 | bit?(bz, j) repeat k := k+1
if odd? k then c := -c
-- Apply rule ei**2 = Q(ei)
if bit?(bz,i) then
c := c * Qeelist.(i+1)
bz:= (bz - 2**i)::NNI
else
bz:= bz + 2**i
z.bz := z.bz + c
z
x * y ==
z := New
for ix in 0..dim-1 repeat
if x.ix ^= 0 then for iy in 0..dim-1 repeat
if y.iy ^= 0 then addMonomProd(x.ix,ix,y.iy,iy,z)
z
canonMonom(c: K, lb: List PI): Record(coef: K, basel: NNI) ==
-- 0. Check input
for b in lb repeat b > n => error "No such basis element"
-- 1. Apply identity ei*ej = -ej*ei, i^=j.
-- The Rep assumes n is small so bubble sort is ok.
-- Using bubble sort keeps the exchange info obvious.
wasordered := false
exchanges := 0
while not wasordered repeat
wasordered := true
for i in 1..#lb-1 repeat
if lb.i > lb.(i+1) then
t := lb.i; lb.i := lb.(i+1); lb.(i+1) := t
exchanges := exchanges + 1
wasordered := false
if odd? exchanges then c := -c
-- 2. Prepare the basis element
-- Apply identity ei*ei = Q(ei).
bz := 0
for b in lb repeat
bn := (b-1)::NNI
if bit?(bz, bn) then
c := c * Qeelist bn
bz:= ( bz - 2**bn )::NNI
else
bz:= bz + 2**bn
[c, bz::NNI]
monomial(c, lb) ==
r := canonMonom(c, lb)
z := New
z r.basel := r.coef
z
coefficient(z, lb) ==
r := canonMonom(1, lb)
r.coef = 0 => error "Cannot take coef of 0"
z r.basel/r.coef
Ex ==> OutputForm
coerceMonom(c: K, b: NNI): Ex ==
b = 0 => c::Ex
ml := [sub("e"::Ex, i::Ex) for i in 1..n | bit?(b,i-1)]
be := reduce("*", ml)
c = 1 => be
c::Ex * be
coerce(x): Ex ==
tl := [coerceMonom(x.i,i) for i in 0..dim-1 | x.i^=0]
null tl => "0"::Ex
reduce("+", tl)
localPowerSets(j:NNI): List(List(PI)) ==
l: List List PI := list []
j = 0 => l
Sm := localPowerSets((j-1)::NNI)
Sn: List List PI := []
for x in Sm repeat Sn := cons(cons(j pretend PI, x),Sn)
append(Sn, Sm)
powerSets(j:NNI):List List PI == map(reverse, localPowerSets j)
Pn:List List PI := powerSets(n)
recip(x: %): Union(%, "failed") ==
one:% := 1
-- tmp:c := x*yC - 1$C
rhsEqs : List K := []
lhsEqs: List List K := []
lhsEqi: List K
for pi in Pn repeat
rhsEqs := cons(coefficient(one, pi), rhsEqs)
lhsEqi := []
for pj in Pn repeat
lhsEqi := cons(coefficient(x*monomial(1,pj),pi),lhsEqi)
lhsEqs := cons(reverse(lhsEqi),lhsEqs)
ans := particularSolution(matrix(lhsEqs),
vector(rhsEqs))$LinearSystemMatrixPackage(K, Vector K, Vector K, Matrix K)
ans case "failed" => "failed"
ansP := parts(ans)
ansC:% := 0
for pj in Pn repeat
cj:= first ansP
ansP := rest ansP
ansC := ansC + cj*monomial(1,pj)
ansC
@
\subsection{defaults.spad}
\subsubsection{FiniteLinearAggregateSort}
++ This package exports 3 sorting algorithms which work over
++ FiniteLinearAggregates.
<<FiniteLinearAggregateSort>>=
FiniteLinearAggregateSort(S, V): Exports == Implementation where
S: Type
V: FiniteLinearAggregate(S) with shallowlyMutable
B ==> Boolean
I ==> Integer
Exports ==> with
quickSort: ((S, S) -> B, V) -> V
++ quickSort(f, agg) sorts the aggregate agg with the ordering function
++ f using the quicksort algorithm.
heapSort : ((S, S) -> B, V) -> V
++ heapSort(f, agg) sorts the aggregate agg with the ordering function
++ f using the heapsort algorithm.
shellSort: ((S, S) -> B, V) -> V
++ shellSort(f, agg) sorts the aggregate agg with the ordering function
++ f using the shellSort algorithm.
Implementation ==> add
siftUp : ((S, S) -> B, V, I, I) -> Void
partition: ((S, S) -> B, V, I, I, I) -> I
QuickSort: ((S, S) -> B, V, I, I) -> V
quickSort(l, r) == QuickSort(l, r, minIndex r, maxIndex r)
siftUp(l, r, i, n) ==
t := qelt(r, i)
while (j := 2*i+1) < n repeat
if (k := j+1) < n and l(qelt(r, j), qelt(r, k)) then j := k
if l(t,qelt(r,j)) then
qsetelt_!(r, i, qelt(r, j))
qsetelt_!(r, j, t)
i := j
else leave
heapSort(l, r) ==
not zero? minIndex r => error "not implemented"
n := (#r)::I
for k in shift(n,-1) - 1 .. 0 by -1 repeat siftUp(l, r, k, n)
for k in n-1 .. 1 by -1 repeat
swap_!(r, 0, k)
siftUp(l, r, 0, k)
r
partition(l, r, i, j, k) ==
-- partition r[i..j] such that r.s <= r.k <= r.t
x := qelt(r, k)
t := qelt(r, i)
qsetelt_!(r, k, qelt(r, j))
while i < j repeat
if l(x,t) then
qsetelt_!(r, j, t)
j := j-1
t := qsetelt_!(r, i, qelt(r, j))
else (i := i+1; t := qelt(r, i))
qsetelt_!(r, j, x)
j
QuickSort(l, r, i, j) ==
n := j - i
if one? n and l(qelt(r, j), qelt(r, i)) then swap_!(r, i, j)
n < 2 => return r
-- for the moment split at the middle item
k := partition(l, r, i, j, i + shift(n,-1))
QuickSort(l, r, i, k - 1)
QuickSort(l, r, k + 1, j)
shellSort(l, r) ==
m := minIndex r
n := maxIndex r
-- use Knuths gap sequence: 1,4,13,40,121,...
g := 1
while g <= (n-m) repeat g := 3*g+1
g := g quo 3
while g > 0 repeat
for i in m+g..n repeat
j := i-g
while j >= m and l(qelt(r, j+g), qelt(r, j)) repeat
swap_!(r,j,j+g)
j := j-g
g := g quo 3
r
@
\subsection{e04agents.spad}
\subsubsection{e04AgentsPackage}
<<e04AgentsPackage>>=
e04AgentsPackage(): E == I where
++ Author: Brian Dupee
++ Date Created: February 1996
++ Date Last Updated: June 1996
++ Basic Operations: simple? linear?, quadratic?, nonLinear?
++ Description:
++ \axiomType{e04AgentsPackage} is a package of numerical agents to be used
++ to investigate attributes of an input function so as to decide the
++ \axiomFun{measure} of an appropriate numerical optimization routine.
E ==> with
finiteBound:(LOCDF,DF) -> LDF
++ finiteBound(l,b) repaces all instances of an infinite entry in
++ \axiom{l} by a finite entry \axiom{b} or \axiom{-b}.
sortConstraints:NOA -> NOA
++ sortConstraints(args) uses a simple bubblesort on the list of
++ constraints using the degree of the expression on which to sort.
++ Of course, it must match the bounds to the constraints.
sumOfSquares:EDF -> Union(EDF,"failed")
++ sumOfSquares(f) returns either an expression for which the square is
++ the original function of "failed".
splitLinear:EDF -> EDF
++ splitLinear(f) splits the linear part from an expression which it
++ returns.
simpleBounds?:LEDF -> Boolean
++ simpleBounds?(l) returns true if the list of expressions l are
++ simple.
linear?:LEDF -> Boolean
++ linear?(l) returns true if all the bounds l are either linear or
++ simple.
linear?:EDF -> Boolean
++ linear?(e) tests if \axiom{e} is a linear function.
linearMatrix:(LEDF, NNI) -> MDF
++ linearMatrix(l,n) returns a matrix of coefficients of the linear
++ functions in \axiom{l}. If l is empty, the matrix has at least one
++ row.
linearPart:LEDF -> LEDF
++ linearPart(l) returns the list of linear functions of \axiom{l}.
nonLinearPart:LEDF -> LEDF
++ nonLinearPart(l) returns the list of non-linear functions of \axiom{l}.
quadratic?:EDF -> Boolean
++ quadratic?(e) tests if \axiom{e} is a quadratic function.
variables:LSA -> LS
++ variables(args) returns the list of variables in \axiom{args.lfn}
varList:(EDF,NNI) -> LS
++ varList(e,n) returns a list of \axiom{n} indexed variables with name
++ as in \axiom{e}.
changeNameToObjf:(Symbol,Result) -> Result
++ changeNameToObjf(s,r) changes the name of item \axiom{s} in \axiom{r}
++ to objf.
expenseOfEvaluation:LSA -> F
++ expenseOfEvaluation(o) returns the intensity value of the
++ cost of evaluating the input set of functions. This is in terms
++ of the number of ``operational units''. It returns a value
++ in the range [0,1].
optAttributes:Union(noa:NOA,lsa:LSA) -> List String
++ optAttributes(o) is a function for supplying a list of attributes
++ of an optimization problem.
I ==> add
import ExpertSystemToolsPackage, ExpertSystemContinuityPackage
sumOfSquares2:EFI -> Union(EFI,"failed")
nonLinear?:EDF -> Boolean
finiteBound2:(OCDF,DF) -> DF
functionType:EDF -> String
finiteBound2(a:OCDF,b:DF):DF ==
not finite?(a) =>
positive?(a) => b
-b
retract(a)@DF
finiteBound(l:LOCDF,b:DF):LDF == [finiteBound2(i,b) for i in l]
sortConstraints(args:NOA):NOA ==
Args := copy args
c:LEDF := Args.cf
l:LOCDF := Args.lb
u:LOCDF := Args.ub
m:INT := (# c) - 1
n:INT := (# l) - m
for j in m..1 by -1 repeat
for i in 1..j repeat
s:EDF := c.i
t:EDF := c.(i+1)
if linear?(t) and (nonLinear?(s) or quadratic?(s)) then
swap!(c,i,i+1)$LEDF
swap!(l,n+i-1,n+i)$LOCDF
swap!(u,n+i-1,n+i)$LOCDF
Args
changeNameToObjf(s:Symbol,r:Result):Result ==
a := remove!(s,r)$Result
a case Any =>
insert!([objf@Symbol,a],r)$Result
r
r
sum(a:EDF,b:EDF):EDF == a+b
variables(args:LSA): LS == variables(reduce(sum,(args.lfn)))
sumOfSquares(f:EDF):Union(EDF,"failed") ==
e := edf2efi(f)
s:Union(EFI,"failed") := sumOfSquares2(e)
s case EFI =>
map(fi2df,s)$EF2(FI,DF)
"failed"
sumOfSquares2(f:EFI):Union(EFI,"failed") ==
p := retractIfCan(f)@Union(PFI,"failed")
p case PFI =>
r := squareFreePart(p)$PFI
(p=r)@Boolean => "failed"
tp := totalDegree(p)$PFI
tr := totalDegree(r)$PFI
t := tp quo tr
found := false
q := r
for i in 2..t by 2 repeat
s := q**2
(s=p)@Boolean =>
found := true
leave
q := r**i
if found then
q :: EFI
else
"failed"
"failed"
splitLinear(f:EDF):EDF ==
out := 0$EDF
(l := isPlus(f)$EDF) case LEDF =>
for i in l repeat
if not quadratic? i then
out := out + i
out
out
edf2pdf(f:EDF):PDF == (retract(f)@PDF)$EDF
varList(e:EDF,n:NNI):LS ==
s := name(first(variables(edf2pdf(e))$PDF)$LS)$Symbol
[subscript(s,[t::OutputForm]) for t in expand([1..n])$Segment(Integer)]
functionType(f:EDF):String ==
n := #(variables(f))$EDF
p := (retractIfCan(f)@Union(PDF,"failed"))$EDF
p case PDF =>
d := totalDegree(p)$PDF
one?(n*d) => "simple"
one?(d) => "linear"
(d=2)@Boolean => "quadratic"
"non-linear"
"non-linear"
simpleBounds?(l: LEDF):Boolean ==
a := true
for e in l repeat
not (functionType(e) = "simple")@Boolean =>
a := false
leave
a
simple?(e:EDF):Boolean == (functionType(e) = "simple")@Boolean
linear?(e:EDF):Boolean == (functionType(e) = "linear")@Boolean
quadratic?(e:EDF):Boolean == (functionType(e) = "quadratic")@Boolean
nonLinear?(e:EDF):Boolean == (functionType(e) = "non-linear")@Boolean
linear?(l: LEDF):Boolean ==
a := true
for e in l repeat
s := functionType(e)
(s = "quadratic")@Boolean or (s = "non-linear")@Boolean =>
a := false
leave
a
simplePart(l:LEDF):LEDF == [i for i in l | simple?(i)]
linearPart(l:LEDF):LEDF == [i for i in l | linear?(i)]
nonLinearPart(l:LEDF):LEDF ==
[i for i in l | not linear?(i) and not simple?(i)]
linearMatrix(l:LEDF, n:NNI):MDF ==
empty?(l) => mat([],n)
L := linearPart l
M := zero(max(1,# L)$NNI,n)$MDF
vars := varList(first(l)$LEDF,n)
row:INT := 1
for a in L repeat
for j in monomials(edf2pdf(a))$PDF repeat
col:INT := 1
for c in vars repeat
if ((first(variables(j)$PDF)$LS)=c)@Boolean then
M(row,col):= first(coefficients(j)$PDF)$LDF
col := col+1
row := row + 1
M
expenseOfEvaluation(o:LSA):F ==
expenseOfEvaluation(vector(copy o.lfn)$VEDF)
optAttributes(o:Union(noa:NOA,lsa:LSA)):List String ==
o case noa =>
n := o.noa
s1:String := "The object function is " functionType(n.fn)
if empty?(n.lb) then
s2:String := "There are no bounds on the variables"
else
s2:String := "There are simple bounds on the variables"
c := n.cf
if empty?(c) then
s3:String := "There are no constraint functions"
else
t := #(c)
lin := #(linearPart(c))
nonlin := #(nonLinearPart(c))
s3:String := "There are " string(lin)$String " linear and "_
string(nonlin)$String " non-linear constraints"
[s1,s2,s3]
l := o.lsa
s:String := "non-linear"
if linear?(l.lfn) then
s := "linear"
["The object functions are " s]
@
\subsection{expexpan.spad}
++ UnivariatePuiseuxSeriesWithExponentialSingularity is a domain used to
++ represent functions with essential singularities. Objects in this
++ domain are sums, where each term in the sum is a univariate Puiseux
++ series times the exponential of a univariate Puiseux series. Thus,
++ the elements of this domain are sums of expressions of the form
++ \spad{g(x) * exp(f(x))}, where g(x) is a univariate Puiseux series
++ and f(x) is a univariate Puiseux series with no terms of non-negative
++ degree.
\subsubsection{UnivariatePuiseuxSeriesWithExponentialSingularity}
<<UnivariatePuiseuxSeriesWithExponentialSingularity>>=
UnivariatePuiseuxSeriesWithExponentialSingularity(R,FE,var,cen):_
Exports == Implementation where
R : Join(OrderedSet,RetractableTo Integer,_
LinearlyExplicitRingOver Integer,GcdDomain)
FE : Join(AlgebraicallyClosedField,TranscendentalFunctionCategory,_
FunctionSpace R)
var : Symbol
cen : FE
B ==> Boolean
I ==> Integer
L ==> List
RN ==> Fraction Integer
UPXS ==> UnivariatePuiseuxSeries(FE,var,cen)
EXPUPXS ==> ExponentialOfUnivariatePuiseuxSeries(FE,var,cen)
OFE ==> OrderedCompletion FE
Result ==> Union(OFE,"failed")
PxRec ==> Record(k: Fraction Integer,c:FE)
Term ==> Record(%coef:UPXS,%expon:EXPUPXS,%expTerms:List PxRec)
-- the %expTerms field is used to record the list of the terms (a 'term'
-- records an exponent and a coefficient) in the exponent %expon
TypedTerm ==> Record(%term:Term,%type:String)
-- a term together with a String which tells whether it has an infinite,
-- zero, or unknown limit as var -> cen+
TRec ==> Record(%zeroTerms: List Term,_
%infiniteTerms: List Term,_
%failedTerms: List Term,_
%puiseuxSeries: UPXS)
SIGNEF ==> ElementaryFunctionSign(R,FE)
Exports ==> Join(FiniteAbelianMonoidRing(UPXS,EXPUPXS),IntegralDomain) with
limitPlus : % -> Union(OFE,"failed")
++ limitPlus(f(var)) returns \spad{limit(var -> cen+,f(var))}.
dominantTerm : % -> Union(TypedTerm,"failed")
++ dominantTerm(f(var)) returns the term that dominates the limiting
++ behavior of \spad{f(var)} as \spad{var -> cen+} together with a
++ \spadtype{String} which briefly describes that behavior. The
++ value of the \spadtype{String} will be \spad{"zero"} (resp.
++ \spad{"infinity"}) if the term tends to zero (resp. infinity)
++ exponentially and will \spad{"series"} if the term is a
++ Puiseux series.
Implementation ==> PolynomialRing(UPXS,EXPUPXS) add
makeTerm : (UPXS,EXPUPXS) -> Term
coeff : Term -> UPXS
exponent : Term -> EXPUPXS
exponentTerms : Term -> List PxRec
setExponentTerms_! : (Term,List PxRec) -> List PxRec
computeExponentTerms_! : Term -> List PxRec
terms : % -> List Term
sortAndDiscardTerms: List Term -> TRec
termsWithExtremeLeadingCoef : (L Term,RN,I) -> Union(L Term,"failed")
filterByOrder: (L Term,(RN,RN) -> B) -> Record(%list:L Term,%order:RN)
dominantTermOnList : (L Term,RN,I) -> Union(Term,"failed")
iDominantTerm : L Term -> Union(Record(%term:Term,%type:String),"failed")
retractIfCan f ==
(numberOfMonomials f = 1) and (zero? degree f) => leadingCoefficient f
"failed"
recip f ==
numberOfMonomials f = 1 =>
monomial(inv leadingCoefficient f,- degree f)
"failed"
makeTerm(coef,expon) == [coef,expon,empty()]
coeff term == term.%coef
exponent term == term.%expon
exponentTerms term == term.%expTerms
setExponentTerms_!(term,list) == term.%expTerms := list
computeExponentTerms_! term ==
setExponentTerms_!(term,entries complete terms exponent term)
terms f ==
-- terms with a higher order singularity will appear closer to the
-- beginning of the list because of the ordering in EXPPUPXS;
-- no "expnonent terms" are computed by this function
zero? f => empty()
concat(makeTerm(leadingCoefficient f,degree f),terms reductum f)
sortAndDiscardTerms termList ==
-- 'termList' is the list of terms of some function f(var), ordered
-- so that terms with a higher order singularity occur at the
-- beginning of the list.
-- This function returns lists of candidates for the "dominant
-- term" in 'termList', i.e. the term which describes the
-- asymptotic behavior of f(var) as var -> cen+.
-- 'zeroTerms' will contain terms which tend to zero exponentially
-- and contains only those terms with the lowest order singularity.
-- 'zeroTerms' will be non-empty only when there are no terms of
-- infinite or series type.
-- 'infiniteTerms' will contain terms which tend to infinity
-- exponentially and contains only those terms with the highest
-- order singularity.
-- 'failedTerms' will contain terms which have an exponential
-- singularity, where we cannot say whether the limiting value
-- is zero or infinity. Only terms with a higher order sigularity
-- than the terms on 'infiniteList' are included.
-- 'pSeries' will be a Puiseux series representing a term without an
-- exponential singularity. 'pSeries' will be non-zero only when no
-- other terms are known to tend to infinity exponentially
zeroTerms : List Term := empty()
infiniteTerms : List Term := empty()
failedTerms : List Term := empty()
-- we keep track of whether or not we've found an infinite term
-- if so, 'infTermOrd' will be set to a negative value
infTermOrd : RN := 0
-- we keep track of whether or not we've found a zero term
-- if so, 'zeroTermOrd' will be set to a negative value
zeroTermOrd : RN := 0
ord : RN := 0; pSeries : UPXS := 0 -- dummy values
while not empty? termList repeat
-- 'expon' is a Puiseux series
expon := exponent(term := first termList)
-- quit if there is an infinite term with a higher order singularity
(ord := order(expon,0)) > infTermOrd => leave "infinite term dominates"
-- if ord = 0, we've hit the end of the list
(ord = 0) =>
-- since we have a series term, don't bother with zero terms
leave(pSeries := coeff(term); zeroTerms := empty())
coef := coefficient(expon,ord)
-- if we can't tell if the lowest order coefficient is positive or
-- negative, we have a "failed term"
(signum := sign(coef)$SIGNEF) case "failed" =>
failedTerms := concat(term,failedTerms)
termList := rest termList
-- if the lowest order coefficient is positive, we have an
-- "infinite term"
(sig := signum :: Integer) = 1 =>
infTermOrd := ord
infiniteTerms := concat(term,infiniteTerms)
-- since we have an infinite term, don't bother with zero terms
zeroTerms := empty()
termList := rest termList
-- if the lowest order coefficient is negative, we have a
-- "zero term" if there are no infinite terms and no failed
-- terms, add the term to 'zeroTerms'
if empty? infiniteTerms then
zeroTerms :=
ord = zeroTermOrd => concat(term,zeroTerms)
zeroTermOrd := ord
list term
termList := rest termList
-- reverse "failed terms" so that higher order singularities
-- appear at the beginning of the list
[zeroTerms,infiniteTerms,reverse_! failedTerms,pSeries]
termsWithExtremeLeadingCoef(termList,ord,signum) ==
-- 'termList' consists of terms of the form [g(x),exp(f(x)),...];
-- when 'signum' is +1 (resp. -1), this function filters 'termList'
-- leaving only those terms such that coefficient(f(x),ord) is
-- maximal (resp. minimal)
while (coefficient(exponent first termList,ord) = 0) repeat
termList := rest termList
empty? termList => error "UPXSSING: can't happen"
coefExtreme := coefficient(exponent first termList,ord)
outList := list first termList; termList := rest termList
for term in termList repeat
(coefDiff := coefficient(exponent term,ord) - coefExtreme) = 0 =>
outList := concat(term,outList)
(sig := sign(coefDiff)$SIGNEF) case "failed" => return "failed"
(sig :: Integer) = signum => outList := list term
outList
filterByOrder(termList,predicate) ==
-- 'termList' consists of terms of the form [g(x),exp(f(x)),expTerms],
-- where 'expTerms' is a list containing some of the terms in the
-- series f(x).
-- The function filters 'termList' and, when 'predicate' is < (resp. >),
-- leaves only those terms with the lowest (resp. highest) order term
-- in 'expTerms'
while empty? exponentTerms first termList repeat
termList := rest termList
empty? termList => error "UPXSING: can't happen"
ordExtreme := (first exponentTerms first termList).k
outList := list first termList
for term in rest termList repeat
not empty? exponentTerms term =>
(ord := (first exponentTerms term).k) = ordExtreme =>
outList := concat(term,outList)
predicate(ord,ordExtreme) =>
ordExtreme := ord
outList := list term
-- advance pointers on "exponent terms" on terms on 'outList'
for term in outList repeat
setExponentTerms_!(term,rest exponentTerms term)
[outList,ordExtreme]
dominantTermOnList(termList,ord0,signum) ==
-- finds dominant term on 'termList'
-- it is known that "exponent terms" of order < 'ord0' are
-- the same for all terms on 'termList'
newList := termsWithExtremeLeadingCoef(termList,ord0,signum)
newList case "failed" => "failed"
termList := newList :: List Term
empty? rest termList => first termList
filtered :=
signum = 1 => filterByOrder(termList,#1 < #2)
filterByOrder(termList,#1 > #2)
termList := filtered.%list
empty? rest termList => first termList
dominantTermOnList(termList,filtered.%order,signum)
iDominantTerm termList ==
termRecord := sortAndDiscardTerms termList
zeroTerms := termRecord.%zeroTerms
infiniteTerms := termRecord.%infiniteTerms
failedTerms := termRecord.%failedTerms
pSeries := termRecord.%puiseuxSeries
-- in future versions, we will deal with "failed terms"
-- at present, if any occur, we cannot determine the limit
not empty? failedTerms => "failed"
not zero? pSeries => [makeTerm(pSeries,0),"series"]
not empty? infiniteTerms =>
empty? rest infiniteTerms => [first infiniteTerms,"infinity"]
for term in infiniteTerms repeat computeExponentTerms_! term
ord0 := order exponent first infiniteTerms
(dTerm := dominantTermOnList(infiniteTerms,ord0,1)) case "failed" =>
return "failed"
[dTerm :: Term,"infinity"]
empty? rest zeroTerms => [first zeroTerms,"zero"]
for term in zeroTerms repeat computeExponentTerms_! term
ord0 := order exponent first zeroTerms
(dTerm := dominantTermOnList(zeroTerms,ord0,-1)) case "failed" =>
return "failed"
[dTerm :: Term,"zero"]
dominantTerm f == iDominantTerm terms f
limitPlus f ==
-- list the terms occurring in 'f'; if there are none, then f = 0
empty?(termList := terms f) => 0
-- compute dominant term
(tInfo := iDominantTerm termList) case "failed" => "failed"
termInfo := tInfo :: Record(%term:Term,%type:String)
domTerm := termInfo.%term
(type := termInfo.%type) = "series" =>
-- find limit of series term
(ord := order(pSeries := coeff domTerm,1)) > 0 => 0
coef := coefficient(pSeries,ord)
member?(var,variables coef) => "failed"
ord = 0 => coef :: OFE
-- in the case of an infinite limit, we need to know the sign
-- of the first non-zero coefficient
(signum := sign(coef)$SIGNEF) case "failed" => "failed"
(signum :: Integer) = 1 => plusInfinity()
minusInfinity()
type = "zero" => 0
-- examine lowest order coefficient in series part of 'domTerm'
ord := order(pSeries := coeff domTerm)
coef := coefficient(pSeries,ord)
member?(var,variables coef) => "failed"
(signum := sign(coef)$SIGNEF) case "failed" => "failed"
(signum :: Integer) = 1 => plusInfinity()
minusInfinity()
@
\subsection{gbeuclid.spad}
\subsubsection{EuclideanGroebnerBasisPackage}
++ Description: \spadtype{EuclideanGroebnerBasisPackage} computes groebner
++ bases for polynomial ideals over euclidean domains.
++ The basic computation provides
++ a distinguished set of generators for these ideals.
++ This basis allows an easy test for membership: the operation
++ \spadfun{euclideanNormalForm} returns zero on ideal members. The string
++ "info" and "redcrit" can be given as additional args to provide
++ incremental information during the computation. If "info" is given,
++ a computational summary is given for each s-polynomial. If "redcrit"
++ is given, the reduced critical pairs are printed. The term ordering
++ is determined by the polynomial type used. Suggested types include
++ \spadtype{DistributedMultivariatePolynomial},
++ \spadtype{HomogeneousDistributedMultivariatePolynomial},
++ \spadtype{GeneralDistributedMultivariatePolynomial}.
<<EuclideanGroebnerBasisPackage>>=
EuclideanGroebnerBasisPackage(Dom, Expon, VarSet, Dpol): T == C where
Dom: EuclideanDomain
Expon: OrderedAbelianMonoidSup
VarSet: OrderedSet
Dpol: PolynomialCategory(Dom, Expon, VarSet)
T== with
euclideanNormalForm: (Dpol, List(Dpol) ) -> Dpol
++ euclideanNormalForm(poly,gb) reduces the polynomial poly modulo the
++ precomputed groebner basis gb giving a canonical representative
++ of the residue class.
euclideanGroebner: List(Dpol) -> List(Dpol)
++ euclideanGroebner(lp) computes a groebner basis for a polynomial ideal
++ over a euclidean domain generated by the list of polynomials lp.
euclideanGroebner: (List(Dpol), String) -> List(Dpol)
++ euclideanGroebner(lp, infoflag) computes a groebner basis
++ for a polynomial ideal over a euclidean domain
++ generated by the list of polynomials lp.
++ During computation, additional information is printed out
++ if infoflag is given as
++ either "info" (for summary information) or
++ "redcrit" (for reduced critical pairs)
euclideanGroebner: (List(Dpol), String, String ) -> List(Dpol)
++ euclideanGroebner(lp, "info", "redcrit") computes a groebner basis
++ for a polynomial ideal generated by the list of polynomials lp.
++ If the second argument is "info", a summary is given of the critical pairs.
++ If the third argument is "redcrit", critical pairs are printed.
C== add
Ex ==> OutputForm
lc ==> leadingCoefficient
red ==> reductum
import OutputForm
------ Definition list of critPair
------ lcmfij is now lcm of headterm of poli and polj
------ lcmcij is now lcm of of lc poli and lc polj
critPair ==>Record(lcmfij: Expon, lcmcij: Dom, poli:Dpol, polj: Dpol )
Prinp ==> Record( ci:Dpol,tci:Integer,cj:Dpol,tcj:Integer,c:Dpol,
tc:Integer,rc:Dpol,trc:Integer,tH:Integer,tD:Integer)
------ Definition of intermediate functions
strongGbasis: (List(Dpol), Integer, Integer) -> List(Dpol)
eminGbasis: List(Dpol) -> List(Dpol)
ecritT: (critPair ) -> Boolean
ecritM: (Expon, Dom, Expon, Dom) -> Boolean
ecritB: (Expon, Dom, Expon, Dom, Expon, Dom) -> Boolean
ecrithinH: (Dpol, List(Dpol)) -> Boolean
ecritBonD: (Dpol, List(critPair)) -> List(critPair)
ecritMTondd1:(List(critPair)) -> List(critPair)
ecritMondd1:(Expon, Dom, List(critPair)) -> List(critPair)
crithdelH: (Dpol, List(Dpol)) -> List(Dpol)
eupdatF: (Dpol, List(Dpol) ) -> List(Dpol)
updatH: (Dpol, List(Dpol), List(Dpol), List(Dpol) ) -> List(Dpol)
sortin: (Dpol, List(Dpol) ) -> List(Dpol)
eRed: (Dpol, List(Dpol), List(Dpol) ) -> Dpol
ecredPol: (Dpol, List(Dpol) ) -> Dpol
esPol: (critPair) -> Dpol
updatD: (List(critPair), List(critPair)) -> List(critPair)
lepol: Dpol -> Integer
prinshINFO : Dpol -> Void
prindINFO: (critPair, Dpol, Dpol,Integer,Integer,Integer) -> Integer
prinpolINFO: List(Dpol) -> Void
prinb: Integer -> Void
------ MAIN ALGORITHM GROEBNER ------------------------
euclideanGroebner( Pol: List(Dpol) ) ==
eminGbasis(strongGbasis(Pol,0,0))
euclideanGroebner( Pol: List(Dpol), xx1: String) ==
xx1 = "redcrit" =>
eminGbasis(strongGbasis(Pol,1,0))
xx1 = "info" =>
eminGbasis(strongGbasis(Pol,2,1))
print(" "::Ex)
print("WARNING: options are - redcrit and/or info - "::Ex)
print(" you didn't type them correct"::Ex)
print(" please try again"::Ex)
print(" "::Ex)
[]
euclideanGroebner( Pol: List(Dpol), xx1: String, xx2: String) ==
(xx1 = "redcrit" and xx2 = "info") or
(xx1 = "info" and xx2 = "redcrit") =>
eminGbasis(strongGbasis(Pol,1,1))
xx1 = "redcrit" and xx2 = "redcrit" =>
eminGbasis(strongGbasis(Pol,1,0))
xx1 = "info" and xx2 = "info" =>
eminGbasis(strongGbasis(Pol,2,1))
print(" "::Ex)
print("WARNING: options are - redcrit and/or info - "::Ex)
print(" you didn't type them correct"::Ex)
print(" please try again "::Ex)
print(" "::Ex)
[]
------ calculate basis
strongGbasis(Pol: List(Dpol),xx1: Integer, xx2: Integer ) ==
dd1, D : List(critPair)
--------- create D and Pol
Pol1:= sort( (degree #1 > degree #2) or
((degree #1 = degree #2 ) and
sizeLess?(leadingCoefficient #2,leadingCoefficient #1)),
Pol)
Pol:= [first(Pol1)]
H:= Pol
Pol1:= rest(Pol1)
D:= nil
while ^null Pol1 repeat
h:= first(Pol1)
Pol1:= rest(Pol1)
en:= degree(h)
lch:= lc h
dd1:= [[sup(degree(x), en), lcm(leadingCoefficient x, lch), x, h]$critPair
for x in Pol]
D:= updatD(ecritMTondd1(sort((#1.lcmfij < #2.lcmfij) or
(( #1.lcmfij = #2.lcmfij ) and
( sizeLess?(#1.lcmcij,#2.lcmcij)) ),
dd1)), ecritBonD(h,D))
Pol:= cons(h, eupdatF(h, Pol))
((en = degree(first(H))) and (leadingCoefficient(h) = leadingCoefficient(first(H)) ) ) =>
" go to top of while "
H:= updatH(h,H,crithdelH(h,H),[h])
H:= sort((degree #1 > degree #2) or
((degree #1 = degree #2 ) and
sizeLess?(leadingCoefficient #2,leadingCoefficient #1)), H)
D:= sort((#1.lcmfij < #2.lcmfij) or
(( #1.lcmfij = #2.lcmfij ) and
( sizeLess?(#1.lcmcij,#2.lcmcij)) ) ,D)
xx:= xx2
-------- loop
while ^null D repeat
D0:= first D
ep:=esPol(D0)
D:= rest(D)
eh:= ecredPol(eRed(ep,H,H),H)
if xx1 = 1 then
prinshINFO(eh)
eh = 0 =>
if xx2 = 1 then
ala:= prindINFO(D0,ep,eh,#H, #D, xx)
xx:= 2
" go to top of while "
eh := unitCanonical eh
e:= degree(eh)
leh:= lc eh
dd1:= [[sup(degree(x), e), lcm(leadingCoefficient x, leh), x, eh]$critPair
for x in Pol]
D:= updatD(ecritMTondd1(sort( (#1.lcmfij <
#2.lcmfij) or (( #1.lcmfij = #2.lcmfij ) and
( sizeLess?(#1.lcmcij,#2.lcmcij)) ), dd1)), ecritBonD(eh,D))
Pol:= cons(eh,eupdatF(eh,Pol))
^ecrithinH(eh,H) or
((e = degree(first(H))) and (leadingCoefficient(eh) = leadingCoefficient(first(H)) ) ) =>
if xx2 = 1 then
ala:= prindINFO(D0,ep,eh,#H, #D, xx)
xx:= 2
" go to top of while "
H:= updatH(eh,H,crithdelH(eh,H),[eh])
H:= sort( (degree #1 > degree #2) or
((degree #1 = degree #2 ) and
sizeLess?(leadingCoefficient #2,leadingCoefficient #1)), H)
if xx2 = 1 then
ala:= prindINFO(D0,ep,eh,#H, #D, xx)
xx:= 2
" go to top of while "
if xx2 = 1 then
prinpolINFO(Pol)
print(" THE GROEBNER BASIS over EUCLIDEAN DOMAIN"::Ex)
if xx1 = 1 and xx2 ^= 1 then
print(" THE GROEBNER BASIS over EUCLIDEAN DOMAIN"::Ex)
H
--------------------------------------
--- erase multiple of e in D2 using crit M
ecritMondd1(e: Expon, c: Dom, D2: List(critPair))==
null D2 => nil
x:= first(D2)
ecritM(e,c, x.lcmfij, lcm(leadingCoefficient(x.poli), leadingCoefficient(x.polj)))
=> ecritMondd1(e, c, rest(D2))
cons(x, ecritMondd1(e, c, rest(D2)))
-------------------------------
ecredPol(h: Dpol, F: List(Dpol) ) ==
h0:Dpol:= 0
null F => h
while h ^= 0 repeat
h0:= h0 + monomial(leadingCoefficient(h),degree(h))
h:= eRed(red(h), F, F)
h0
----------------------------
--- reduce dd1 using crit T and crit M
ecritMTondd1(dd1: List(critPair))==
null dd1 => nil
f1:= first(dd1)
s1:= #(dd1)
cT1:= ecritT(f1)
s1= 1 and cT1 => nil
s1= 1 => dd1
e1:= f1.lcmfij
r1:= rest(dd1)
f2:= first(r1)
e1 = f2.lcmfij and f1.lcmcij = f2.lcmcij =>
cT1 => ecritMTondd1(cons(f1, rest(r1)))
ecritMTondd1(r1)
dd1 := ecritMondd1(e1, f1.lcmcij, r1)
cT1 => ecritMTondd1(dd1)
cons(f1, ecritMTondd1(dd1))
-----------------------------
--- erase elements in D fullfilling crit B
ecritBonD(h:Dpol, D: List(critPair))==
null D => nil
x:= first(D)
x1:= x.poli
x2:= x.polj
ecritB(degree(h), leadingCoefficient(h), degree(x1),leadingCoefficient(x1),degree(x2),leadingCoefficient(x2)) =>
ecritBonD(h, rest(D))
cons(x, ecritBonD(h, rest(D)))
-----------------------------
--- concat F and h and erase multiples of h in F
eupdatF(h: Dpol, F: List(Dpol)) ==
null F => nil
f1:= first(F)
ecritM(degree h, leadingCoefficient(h), degree f1, leadingCoefficient(f1))
=> eupdatF(h, rest(F))
cons(f1, eupdatF(h, rest(F)))
-----------------------------
--- concat H and h and erase multiples of h in H
updatH(h: Dpol, H: List(Dpol), Hh: List(Dpol), Hhh: List(Dpol)) ==
null H => append(Hh,Hhh)
h1:= first(H)
hlcm:= sup(degree(h1), degree(h))
plc:= extendedEuclidean(leadingCoefficient(h), leadingCoefficient(h1))
hp:= monomial(plc.coef1,subtractIfCan(hlcm, degree(h))::Expon)*h +
monomial(plc.coef2,subtractIfCan(hlcm, degree(h1))::Expon)*h1
(ecrithinH(hp, Hh) and ecrithinH(hp, Hhh)) =>
hpp:= append(rest(H),Hh)
hp:= ecredPol(eRed(hp,hpp,hpp),hpp)
updatH(h, rest(H), crithdelH(hp,Hh),cons(hp,crithdelH(hp,Hhh)))
updatH(h, rest(H), Hh,Hhh)
--------------------------------------------------
---- delete elements in cons(h,H)
crithdelH(h: Dpol, H: List(Dpol))==
null H => nil
h1:= first(H)
dh1:= degree h1
dh:= degree h
ecritM(dh, lc h, dh1, lc h1) => crithdelH(h, rest(H))
dh1 = sup(dh,dh1) =>
plc:= extendedEuclidean( lc h1, lc h)
cons(plc.coef1*h1 + monomial(plc.coef2,subtractIfCan(dh1,dh)::Expon)*h,
crithdelH(h,rest(H)))
cons(h1, crithdelH(h,rest(H)))
eminGbasis(F: List(Dpol)) ==
null F => nil
newbas := eminGbasis rest F
cons(ecredPol( first(F), newbas),newbas)
------------------------------------------------
--- does h belong to H
ecrithinH(h: Dpol, H: List(Dpol))==
null H => true
h1:= first(H)
ecritM(degree h1, lc h1, degree h, lc h) => false
ecrithinH(h, rest(H))
-----------------------------
--- calculate euclidean S-polynomial of a critical pair
esPol(p:critPair)==
Tij := p.lcmfij
fi := p.poli
fj := p.polj
lij:= lcm(leadingCoefficient(fi), leadingCoefficient(fj))
red(fi)*monomial((lij exquo leadingCoefficient(fi))::Dom,
subtractIfCan(Tij, degree fi)::Expon) -
red(fj)*monomial((lij exquo leadingCoefficient(fj))::Dom,
subtractIfCan(Tij, degree fj)::Expon)
----------------------------
--- euclidean reduction mod F
eRed(s: Dpol, H: List(Dpol), Hh: List(Dpol)) ==
( s = 0 or null H ) => s
f1:= first(H)
ds:= degree s
lf1:= leadingCoefficient(f1)
ls:= leadingCoefficient(s)
e: Union(Expon, "failed")
(((e:= subtractIfCan(ds, degree f1)) case "failed" ) or sizeLess?(ls, lf1) ) =>
eRed(s, rest(H), Hh)
sdf1:= divide(ls, lf1)
q1:= sdf1.quotient
sdf1.remainder = 0 =>
eRed(red(s) - monomial(q1,e)*reductum(f1), Hh, Hh)
eRed(s -(monomial(q1, e)*f1), rest(H), Hh)
----------------------------
--- crit T true, if e1 and e2 are disjoint
ecritT(p: critPair) ==
pi:= p.poli
pj:= p.polj
ci:= lc pi
cj:= lc pj
(p.lcmfij = degree pi + degree pj) and (p.lcmcij = ci*cj)
----------------------------
--- crit M - true, if lcm#2 multiple of lcm#1
ecritM(e1: Expon, c1: Dom, e2: Expon, c2: Dom) ==
en: Union(Expon, "failed")
((en:=subtractIfCan(e2, e1)) case "failed") or
((c2 exquo c1) case "failed") => false
true
----------------------------
--- crit B - true, if eik is a multiple of eh and eik ^equal
--- lcm(eh,ei) and eik ^equal lcm(eh,ek)
ecritB(eh:Expon, ch: Dom, ei:Expon, ci: Dom, ek:Expon, ck: Dom) ==
eik:= sup(ei, ek)
cik:= lcm(ci, ck)
ecritM(eh, ch, eik, cik) and
^ecritM(eik, cik, sup(ei, eh), lcm(ci, ch)) and
^ecritM(eik, cik, sup(ek, eh), lcm(ck, ch))
-------------------------------
--- reduce p1 mod lp
euclideanNormalForm(p1: Dpol, lp: List(Dpol))==
eRed(p1, lp, lp)
---------------------------------
--- insert element in sorted list
sortin(p1: Dpol, lp: List(Dpol))==
null lp => [p1]
f1:= first(lp)
elf1:= degree(f1)
ep1:= degree(p1)
((elf1 < ep1) or ((elf1 = ep1) and
sizeLess?(leadingCoefficient(f1),leadingCoefficient(p1)))) =>
cons(f1,sortin(p1, rest(lp)))
cons(p1,lp)
updatD(D1: List(critPair), D2: List(critPair)) ==
null D1 => D2
null D2 => D1
dl1:= first(D1)
dl2:= first(D2)
(dl1.lcmfij < dl2.lcmfij) => cons(dl1, updatD(D1.rest, D2))
cons(dl2, updatD(D1, D2.rest))
---- calculate number of terms of polynomial
lepol(p1:Dpol)==
n: Integer
n:= 0
while p1 ^= 0 repeat
n:= n + 1
p1:= red(p1)
n
---- print blanc lines
prinb(n: Integer)==
for i in 1..n repeat messagePrint(" ")
---- print reduced critpair polynom
prinshINFO(h: Dpol)==
prinb(2)
messagePrint(" reduced Critpair - Polynom :")
prinb(2)
print(h::Ex)
prinb(2)
-------------------------------
---- print info string
prindINFO(cp: critPair, ps: Dpol, ph: Dpol, i1:Integer,
i2:Integer, n:Integer) ==
ll: List Prinp
a: Dom
cpi:= cp.poli
cpj:= cp.polj
if n = 1 then
prinb(1)
messagePrint("you choose option -info- ")
messagePrint("abbrev. for the following information strings are")
messagePrint(" ci => Leading monomial for critpair calculation")
messagePrint(" tci => Number of terms of polynomial i")
messagePrint(" cj => Leading monomial for critpair calculation")
messagePrint(" tcj => Number of terms of polynomial j")
messagePrint(" c => Leading monomial of critpair polynomial")
messagePrint(" tc => Number of terms of critpair polynomial")
messagePrint(" rc => Leading monomial of redcritpair polynomial")
messagePrint(" trc => Number of terms of redcritpair polynomial")
messagePrint(" tF => Number of polynomials in reduction list F")
messagePrint(" tD => Number of critpairs still to do")
prinb(4)
n:= 2
prinb(1)
a:= 1
ph = 0 =>
ps = 0 =>
ll:= [[monomial(a,degree(cpi)),lepol(cpi),monomial(a,degree(cpj)),
lepol(cpj),ps,0,ph,0,i1,i2]$Prinp]
print(ll::Ex)
prinb(1)
n
ll:= [[monomial(a,degree(cpi)),lepol(cpi),
monomial(a,degree(cpj)),lepol(cpj),monomial(a,degree(ps)),
lepol(ps), ph,0,i1,i2]$Prinp]
print(ll::Ex)
prinb(1)
n
ll:= [[monomial(a,degree(cpi)),lepol(cpi),
monomial(a,degree(cpj)),lepol(cpj),monomial(a,degree(ps)),
lepol(ps),monomial(a,degree(ph)),lepol(ph),i1,i2]$Prinp]
print(ll::Ex)
prinb(1)
n
-------------------------------
---- print the groebner basis polynomials
prinpolINFO(pl: List(Dpol))==
n:Integer
n:= #pl
prinb(1)
n = 1 =>
print(" There is 1 Groebner Basis Polynomial "::Ex)
prinb(2)
print(" There are "::Ex)
prinb(1)
print(n::Ex)
prinb(1)
print(" Groebner Basis Polynomials. "::Ex)
prinb(2)
@
\subsection{kl.spad}
\subsubsection{SortedCache}
++ A sorted cache of a cachable set S is a dynamic structure that
++ keeps the elements of S sorted and assigns an integer to each
++ element of S once it is in the cache. This way, equality and ordering
++ on S are tested directly on the integers associated with the elements
++ of S, once they have been entered in the cache.
<<SortedCache>>=
SortedCache(S:CachableSet): Exports == Implementation where
N ==> NonNegativeInteger
DIFF ==> 1024
Exports ==> with
clearCache : () -> Void
++ clearCache() empties the cache.
cache : () -> List S
++ cache() returns the current cache as a list.
enterInCache: (S, S -> Boolean) -> S
++ enterInCache(x, f) enters x in the cache, calling \spad{f(y)} to
++ determine whether x is equal to y. It returns x with an integer
++ associated with it.
enterInCache: (S, (S, S) -> Integer) -> S
++ enterInCache(x, f) enters x in the cache, calling \spad{f(x, y)} to
++ determine whether \spad{x < y (f(x,y) < 0), x = y (f(x,y) = 0)}, or
++ \spad{x > y (f(x,y) > 0)}.
++ It returns x with an integer associated with it.
Implementation ==> add
shiftCache : (List S, N) -> Void
insertInCache: (List S, List S, S, N) -> S
cach := [nil()]$Record(cche:List S)
cache() == cach.cche
shiftCache(l, n) ==
for x in l repeat setPosition(x, n + position x)
void
clearCache() ==
for x in cache repeat setPosition(x, 0)
cach.cche := nil()
void
enterInCache(x:S, equal?:S -> Boolean) ==
scan := cache()
while not null scan repeat
equal?(y := first scan) =>
setPosition(x, position y)
return y
scan := rest scan
setPosition(x, 1 + #cache())
cach.cche := concat(cache(), x)
x
enterInCache(x:S, triage:(S, S) -> Integer) ==
scan := cache()
pos:N:= 0
for i in 1..#scan repeat
zero?(n := triage(x, y := first scan)) =>
setPosition(x, position y)
return y
n<0 => return insertInCache(first(cache(),(i-1)::N),scan,x,pos)
scan := rest scan
pos := position y
setPosition(x, pos + DIFF)
cach.cche := concat(cache(), x)
x
insertInCache(before, after, x, pos) ==
if ((pos+1) = position first after) then shiftCache(after, DIFF)
setPosition(x, pos + (((position first after) - pos)::N quo 2))
cach.cche := concat(before, concat(x, after))
x
@
\subsection{list.spad}
\subsubsection{IndexedList}
++ \spadtype{IndexedList} is a basic implementation of the functions
++ in \spadtype{ListAggregate}, often using functions in the underlying
++ LISP system. The second parameter to the constructor (\spad{mn})
++ is the beginning index of the list. That is, if \spad{l} is a
++ list, then \spad{elt(l,mn)} is the first value. This constructor
++ is probably best viewed as the implementation of singly-linked
++ lists that are addressable by index rather than as a mere wrapper
++ for LISP lists.
<<IndexedList>>=
IndexedList(S:Type, mn:Integer): ListAggregate S == add
#x == LENGTH(x)$Lisp
concat(s:S,x:%) == CONS(s,x)$Lisp
eq?(x,y) == EQ(x,y)$Lisp
first x == SPADfirst(x)$Lisp
elt(x,"first") == SPADfirst(x)$Lisp
empty() == NIL$Lisp
empty? x == NULL(x)$Lisp
rest x == CDR(x)$Lisp
elt(x,"rest") == CDR(x)$Lisp
setfirst_!(x,s) ==
empty? x => error "Cannot update an empty list"
Qfirst RPLACA(x,s)$Lisp
setelt(x,"first",s) ==
empty? x => error "Cannot update an empty list"
Qfirst RPLACA(x,s)$Lisp
setrest_!(x,y) ==
empty? x => error "Cannot update an empty list"
Qrest RPLACD(x,y)$Lisp
setelt(x,"rest",y) ==
empty? x => error "Cannot update an empty list"
Qrest RPLACD(x,y)$Lisp
construct l == l pretend %
parts s == s pretend List S
reverse_! x == NREVERSE(x)$Lisp
reverse x == REVERSE(x)$Lisp
minIndex x == mn
rest(x, n) ==
for i in 1..n repeat
if Qnull x then error "index out of range"
x := Qrest x
x
copy x ==
y := empty()
for i in 0.. while not Qnull x repeat
if Qeq(i,cycleMax) and cyclic? x then error "cyclic list"
y := Qcons(Qfirst x,y)
x := Qrest x
(NREVERSE(y)$Lisp)@%
if S has SetCategory then
coerce(x):OutputForm ==
-- displays cycle with overbar over the cycle
y := empty()$List(OutputForm)
s := cycleEntry x
while Qneq(x, s) repeat
y := concat((first x)::OutputForm, y)
x := rest x
y := reverse_! y
empty? s => bracket y
-- cyclic case: z is cylic part
z := list((first x)::OutputForm)
while Qneq(s, rest x) repeat
x := rest x
z := concat((first x)::OutputForm, z)
bracket concat_!(y, overbar commaSeparate reverse_! z)
x = y ==
Qeq(x,y) => true
while not Qnull x and not Qnull y repeat
Qfirst x ^=$S Qfirst y => return false
x := Qrest x
y := Qrest y
Qnull x and Qnull y
latex(x : %): String ==
s : String := "\left["
while not Qnull x repeat
s := concat(s, latex(Qfirst x)$S)$String
x := Qrest x
if not Qnull x then s := concat(s, ", ")$String
concat(s, " \right]")$String
member?(s,x) ==
while not Qnull x repeat
if s = Qfirst x then return true else x := Qrest x
false
-- Lots of code from parts of AGGCAT, repeated here to
-- get faster compilation
concat_!(x:%,y:%) ==
Qnull x =>
Qnull y => x
Qpush(first y,x)
QRPLACD(x,rest y)$Lisp
x
z:=x
while not Qnull Qrest z repeat
z:=Qrest z
QRPLACD(z,y)$Lisp
x
-- Then a quicky:
if S has SetCategory then
removeDuplicates_! l ==
p := l
while not Qnull p repeat
-- p := setrest_!(p, remove_!(#1 = Qfirst p, Qrest p))
-- far too expensive - builds closures etc.
pp:=p
f:S:=Qfirst p
p:=Qrest p
while not Qnull (pr:=Qrest pp) repeat
if (Qfirst pr)@S = f then QRPLACD(pp,Qrest pr)$Lisp
else pp:=pr
l
-- then sorting
mergeSort: ((S, S) -> Boolean, %, Integer) -> %
sort_!(f, l) == mergeSort(f, l, #l)
merge_!(f, p, q) ==
Qnull p => q
Qnull q => p
Qeq(p, q) => error "cannot merge a list into itself"
if f(Qfirst p, Qfirst q)
then (r := t := p; p := Qrest p)
else (r := t := q; q := Qrest q)
while not Qnull p and not Qnull q repeat
if f(Qfirst p, Qfirst q)
then (QRPLACD(t, p)$Lisp; t := p; p := Qrest p)
else (QRPLACD(t, q)$Lisp; t := q; q := Qrest q)
QRPLACD(t, if Qnull p then q else p)$Lisp
r
split_!(p, n) ==
n < 1 => error "index out of range"
p := rest(p, (n - 1)::NonNegativeInteger)
q := Qrest p
QRPLACD(p, NIL$Lisp)$Lisp
q
mergeSort(f, p, n) ==
if n = 2 and f(first rest p, first p) then p := reverse_! p
n < 3 => p
l := (n quo 2)::NonNegativeInteger
q := split_!(p, l)
p := mergeSort(f, p, l)
q := mergeSort(f, q, n - l)
merge_!(f, p, q)
@
\subsubsection{List}
++ \spadtype{List} implements singly-linked lists that are
++ addressable by indices; the index of the first element
++ is 1. In addition to the operations provided by
++ \spadtype{IndexedList}, this constructor provides some
++ LISP-like functions such as \spadfun{null} and \spadfun{cons}.
<<List>>=
List(S:Type): ListAggregate S with
nil : () -> %
++ nil() returns the empty list.
null : % -> Boolean
++ null(u) tests if list \spad{u} is the
++ empty list.
cons : (S, %) -> %
++ cons(element,u) appends \spad{element} onto the front
++ of list \spad{u} and returns the new list. This new list
++ and the old one will share some structure.
append : (%, %) -> %
++ append(u1,u2) appends the elements of list \spad{u1}
++ onto the front of list \spad{u2}. This new list
++ and \spad{u2} will share some structure.
if S has SetCategory then
setUnion : (%, %) -> %
++ setUnion(u1,u2) appends the two lists u1 and u2, then
++ removes all duplicates. The order of elements in the
++ resulting list is unspecified.
setIntersection : (%, %) -> %
++ setIntersection(u1,u2) returns a list of the elements
++ that lists \spad{u1} and \spad{u2} have in common.
++ The order of elements in the resulting list is unspecified.
setDifference : (%, %) -> %
++ setDifference(u1,u2) returns a list of the elements
++ of \spad{u1} that are not also in \spad{u2}.
++ The order of elements in the resulting list is unspecified.
if S has OpenMath then OpenMath
== IndexedList(S, LISTMININDEX) add
nil() == NIL$Lisp
null l == NULL(l)$Lisp
cons(s, l) == CONS(s, l)$Lisp
append(l:%, t:%) == APPEND(l, t)$Lisp
if S has OpenMath then
writeOMList(dev: OpenMathDevice, x: %): Void ==
OMputApp(dev)
OMputSymbol(dev, "list1", "list")
-- The following didn't compile because the compiler isn't
-- convinced that `xval' is a S. Duhhh! MCD.
--for xval in x repeat
-- OMwrite(dev, xval, false)
while not null x repeat
OMwrite(dev,first x,false)
x := rest x
OMputEndApp(dev)
OMwrite(x: %): String ==
s: String := ""
sp := OM_-STRINGTOSTRINGPTR(s)$Lisp
dev: OpenMathDevice := OMopenString(sp pretend String, OMencodingXML)
OMputObject(dev)
writeOMList(dev, x)
OMputEndObject(dev)
OMclose(dev)
s := OM_-STRINGPTRTOSTRING(sp)$Lisp pretend String
s
OMwrite(x: %, wholeObj: Boolean): String ==
s: String := ""
sp := OM_-STRINGTOSTRINGPTR(s)$Lisp
dev: OpenMathDevice := OMopenString(sp pretend String, OMencodingXML)
if wholeObj then
OMputObject(dev)
writeOMList(dev, x)
if wholeObj then
OMputEndObject(dev)
OMclose(dev)
s := OM_-STRINGPTRTOSTRING(sp)$Lisp pretend String
s
OMwrite(dev: OpenMathDevice, x: %): Void ==
OMputObject(dev)
writeOMList(dev, x)
OMputEndObject(dev)
OMwrite(dev: OpenMathDevice, x: %, wholeObj: Boolean): Void ==
if wholeObj then
OMputObject(dev)
writeOMList(dev, x)
if wholeObj then
OMputEndObject(dev)
if S has SetCategory then
setUnion(l1:%,l2:%) == removeDuplicates concat(l1,l2)
setIntersection(l1:%,l2:%) ==
u :% := empty()
l1 := removeDuplicates l1
while not empty? l1 repeat
if member?(first l1,l2) then u := cons(first l1,u)
l1 := rest l1
u
setDifference(l1:%,l2:%) ==
l1 := removeDuplicates l1
lu:% := empty()
while not empty? l1 repeat
l11:=l1.1
if not member?(l11,l2) then lu := concat(l11,lu)
l1 := rest l1
lu
if S has ConvertibleTo InputForm then
convert(x:%):InputForm ==
convert concat(convert("construct"::Symbol)@InputForm,
[convert a for a in (x pretend List S)]$List(InputForm))
@
\subsection{perm.spad}
\subsubsection{Permutation}
++ Description: Permutation(S) implements the group of all bijections
++ on a set S, which move only a finite number of points.
++ A permutation is considered as a map from S into S. In particular
++ multiplication is defined as composition of maps:
++ {\em pi1 * pi2 = pi1 o pi2}.
++ The internal representation of permuatations are two lists
++ of equal length representing preimages and images.
<<Permutation>>=
Permutation(S:SetCategory): public == private where
B ==> Boolean
PI ==> PositiveInteger
I ==> Integer
L ==> List
NNI ==> NonNegativeInteger
V ==> Vector
PT ==> Partition
OUTFORM ==> OutputForm
RECCYPE ==> Record(cycl: L L S, permut: %)
RECPRIM ==> Record(preimage: L S, image : L S)
public ==> PermutationCategory S with
listRepresentation: % -> RECPRIM
++ listRepresentation(p) produces a representation {\em rep} of
++ the permutation p as a list of preimages and images, i.e
++ p maps {\em (rep.preimage).k} to {\em (rep.image).k} for all
++ indices k.
coercePreimagesImages : List List S -> %
++ coercePreimagesImages(lls) coerces the representation {\em lls}
++ of a permutation as a list of preimages and images to a permutation.
coerce : List List S -> %
++ coerce(lls) coerces a list of cycles {\em lls} to a
++ permutation, each cycle being a list with not
++ repetitions, is coerced to the permutation, which maps
++ {\em ls.i} to {\em ls.i+1}, indices modulo the length of the list,
++ then these permutations are mutiplied.
++ Error: if repetitions occur in one cycle.
coerce : List S -> %
++ coerce(ls) coerces a cycle {\em ls}, i.e. a list with not
++ repetitions to a permutation, which maps {\em ls.i} to
++ {\em ls.i+1}, indices modulo the length of the list.
++ Error: if repetitions occur.
coerceListOfPairs : List List S -> %
++ coerceListOfPairs(lls) coerces a list of pairs {\em lls} to a
++ permutation.
++ Error: if not consistent, i.e. the set of the first elements
++ coincides with the set of second elements.
--coerce : % -> OUTFORM
++ coerce(p) generates output of the permutation p with domain
++ OutputForm.
degree : % -> NonNegativeInteger
++ degree(p) retuns the number of points moved by the
++ permutation p.
movedPoints : % -> Set S
++ movedPoints(p) returns the set of points moved by the permutation p.
cyclePartition : % -> Partition
++ cyclePartition(p) returns the cycle structure of a permutation
++ p including cycles of length 1 only if S is finite.
order : % -> NonNegativeInteger
++ order(p) returns the order of a permutation p as a group element.
numberOfCycles : % -> NonNegativeInteger
++ numberOfCycles(p) returns the number of non-trivial cycles of
++ the permutation p.
sign : % -> Integer
++ sign(p) returns the signum of the permutation p, +1 or -1.
even? : % -> Boolean
++ even?(p) returns true if and only if p is an even permutation,
++ i.e. {\em sign(p)} is 1.
odd? : % -> Boolean
++ odd?(p) returns true if and only if p is an odd permutation
++ i.e. {\em sign(p)} is {\em -1}.
sort : L % -> L %
++ sort(lp) sorts a list of permutations {\em lp} according to
++ cycle structure first according to length of cycles,
++ second, if S has \spadtype{Finite} or S has
++ \spadtype{OrderedSet} according to lexicographical order of
++ entries in cycles of equal length.
if S has Finite then
fixedPoints : % -> Set S
++ fixedPoints(p) returns the points fixed by the permutation p.
if S has IntegerNumberSystem or S has Finite then
coerceImages : L S -> %
++ coerceImages(ls) coerces the list {\em ls} to a permutation
++ whose image is given by {\em ls} and the preimage is fixed
++ to be {\em [1,...,n]}.
++ Note: {coerceImages(ls)=coercePreimagesImages([1,...,n],ls)}.
private ==> add
-- representation of the object:
Rep := V L S
-- import of domains and packages
import OutputForm
import Vector List S
-- variables
p,q : %
exp : I
-- local functions first, signatures:
smaller? : (S,S) -> B
rotateCycle: L S -> L S
coerceCycle: L L S -> %
smallerCycle?: (L S, L S) -> B
shorterCycle?:(L S, L S) -> B
permord:(RECCYPE,RECCYPE) -> B
coerceToCycle:(%,B) -> L L S
duplicates?: L S -> B
smaller?(a:S, b:S): B ==
S has OrderedSet => a <$S b
S has Finite => lookup a < lookup b
false
rotateCycle(cyc: L S): L S ==
-- smallest element is put in first place
-- doesn't change cycle if underlying set
-- is not ordered or not finite.
min:S := first cyc
minpos:I := 1 -- 1 = minIndex cyc
for i in 2..maxIndex cyc repeat
if smaller?(cyc.i,min) then
min := cyc.i
minpos := i
one? minpos => cyc
concat(last(cyc,((#cyc-minpos+1)::NNI)),first(cyc,(minpos-1)::NNI))
coerceCycle(lls : L L S): % ==
perm : % := 1
for lists in reverse lls repeat
perm := cycle lists * perm
perm
smallerCycle?(cyca: L S, cycb: L S): B ==
#cyca ^= #cycb =>
#cyca < #cycb
for i in cyca for j in cycb repeat
i ^= j => return smaller?(i, j)
false
shorterCycle?(cyca: L S, cycb: L S): B ==
#cyca < #cycb
permord(pa: RECCYPE, pb : RECCYPE): B ==
for i in pa.cycl for j in pb.cycl repeat
i ^= j => return smallerCycle?(i, j)
#pa.cycl < #pb.cycl
coerceToCycle(p: %, doSorting?: B): L L S ==
preim := p.1
im := p.2
cycles := nil()$(L L S)
while not null preim repeat
-- start next cycle
firstEltInCycle: S := first preim
nextCycle : L S := list firstEltInCycle
preim := rest preim
nextEltInCycle := first im
im := rest im
while nextEltInCycle ^= firstEltInCycle repeat
nextCycle := cons(nextEltInCycle, nextCycle)
i := position(nextEltInCycle, preim)
preim := delete(preim,i)
nextEltInCycle := im.i
im := delete(im,i)
nextCycle := reverse nextCycle
-- check on 1-cycles, we don't list these
if not null rest nextCycle then
if doSorting? and (S has OrderedSet or S has Finite) then
-- put smallest element in cycle first:
nextCycle := rotateCycle nextCycle
cycles := cons(nextCycle, cycles)
not doSorting? => cycles
-- sort cycles
S has OrderedSet or S has Finite =>
sort(smallerCycle?,cycles)$(L L S)
sort(shorterCycle?,cycles)$(L L S)
duplicates? (ls : L S ): B ==
x := copy ls
while not null x repeat
member? (first x ,rest x) => return true
x := rest x
false
-- now the exported functions
listRepresentation p ==
s : RECPRIM := [p.1,p.2]
coercePreimagesImages preImageAndImage ==
p : % := [preImageAndImage.1,preImageAndImage.2]
movedPoints p == construct p.1 --check on fixed points !!
degree p == #movedPoints p
p = q ==
#(preimp := p.1) ^= #(preimq := q.1) => false
for i in 1..maxIndex preimp repeat
pos := position(preimp.i, preimq)
pos = 0 => return false
(p.2).i ^= (q.2).pos => return false
true
orbit(p ,el) ==
-- start with a 1-element list:
out : Set S := brace list el
el2 := eval(p, el)
while el2 ^= el repeat
-- be carefull: insert adds one element
-- as side effect to out
insert_!(el2, out)
el2 := eval(p, el2)
out
cyclePartition p ==
partition([#c for c in coerceToCycle(p, false)])$Partition
order p ==
ord: I := lcm removeDuplicates convert cyclePartition p
ord::NNI
sign(p) ==
even? p => 1
- 1
even?(p) == even?(#(p.1) - numberOfCycles p)
-- see the book of James and Kerber on symmetric groups
-- for this formula.
odd?(p) == odd?(#(p.1) - numberOfCycles p)
pa < pb ==
pacyc:= coerceToCycle(pa,true)
pbcyc:= coerceToCycle(pb,true)
for i in pacyc for j in pbcyc repeat
i ^= j => return smallerCycle? ( i, j )
maxIndex pacyc < maxIndex pbcyc
coerce(lls : L L S): % == coerceCycle lls
coerce(ls : L S): % == cycle ls
sort(inList : L %): L % ==
not (S has OrderedSet or S has Finite) => inList
ownList: L RECCYPE := nil()$(L RECCYPE)
for sigma in inList repeat
ownList :=
cons([coerceToCycle(sigma,true),sigma]::RECCYPE, ownList)
ownList := sort(permord, ownList)$(L RECCYPE)
outList := nil()$(L %)
for rec in ownList repeat
outList := cons(rec.permut, outList)
reverse outList
coerce (p: %): OUTFORM ==
cycles: L L S := coerceToCycle(p,true)
outfmL : L OUTFORM := nil()
for cycle in cycles repeat
outcycL: L OUTFORM := nil()
for elt in cycle repeat
outcycL := cons(elt :: OUTFORM, outcycL)
outfmL := cons(paren blankSeparate reverse outcycL, outfmL)
-- The identity element will be output as 1:
null outfmL => outputForm(1@Integer)
-- represent a single cycle in the form (a b c d)
-- and not in the form ((a b c d)):
null rest outfmL => first outfmL
hconcat reverse outfmL
cycles(vs ) == coerceCycle vs
cycle(ls) ==
#ls < 2 => 1
duplicates? ls => error "cycle: the input contains duplicates"
[ls, append(rest ls, list first ls)]
coerceListOfPairs(loP) ==
preim := nil()$(L S)
im := nil()$(L S)
for pair in loP repeat
if first pair ^= second pair then
preim := cons(first pair, preim)
im := cons(second pair, im)
duplicates?(preim) or duplicates?(im) or brace(preim)$(Set S) _
^= brace(im)$(Set S) =>
error "coerceListOfPairs: the input cannot be interpreted as a permutation"
[preim, im]
q * p ==
-- use vectors for efficiency??
preimOfp : V S := construct p.1
imOfp : V S := construct p.2
preimOfq := q.1
imOfq := q.2
preimOfqp := nil()$(L S)
imOfqp := nil()$(L S)
-- 1 = minIndex preimOfp
for i in 1..(maxIndex preimOfp) repeat
-- find index of image of p.i in q if it exists
j := position(imOfp.i, preimOfq)
if j = 0 then
-- it does not exist
preimOfqp := cons(preimOfp.i, preimOfqp)
imOfqp := cons(imOfp.i, imOfqp)
else
-- it exists
el := imOfq.j
-- if the composition fixes the element, we don't
-- have to do anything
if el ^= preimOfp.i then
preimOfqp := cons(preimOfp.i, preimOfqp)
imOfqp := cons(el, imOfqp)
-- we drop the parts of q which have to do with p
preimOfq := delete(preimOfq, j)
imOfq := delete(imOfq, j)
[append(preimOfqp, preimOfq), append(imOfqp, imOfq)]
1 == new(2,empty())$Rep
inv p == [p.2, p.1]
eval(p, el) ==
pos := position(el, p.1)
pos = 0 => el
(p.2).pos
elt(p, el) == eval(p, el)
numberOfCycles p == #coerceToCycle(p, false)
if S has IntegerNumberSystem then
coerceImages (image) ==
preImage : L S := [i::S for i in 1..maxIndex image]
p : % := [preImage,image]
if S has Finite then
coerceImages (image) ==
preImage : L S := [index(i::PI)::S for i in 1..maxIndex image]
p : % := [preImage,image]
fixedPoints ( p ) == complement movedPoints p
cyclePartition p ==
pt := partition([#c for c in coerceToCycle(p, false)])$Partition
pt +$PT conjugate(partition([#fixedPoints(p)])$PT)$PT
@
\subsection{polset.spad}
\subsubsection{PolynomialSetCategory}
<<PolynomialSetCategory>>=
PolynomialSetCategory(R:Ring, E:OrderedAbelianMonoidSup,_
VarSet:OrderedSet, P:RecursivePolynomialCategory(R,E,VarSet)): Category ==
Join(SetCategory,Collection(P),CoercibleTo(List(P))) with
finiteAggregate
retractIfCan : List(P) -> Union($,"failed")
++ \axiom{retractIfCan(lp)} returns an element of the domain whose elements
++ are the members of \axiom{lp} if such an element exists, otherwise
++ \axiom{"failed"} is returned.
retract : List(P) -> $
++ \axiom{retract(lp)} returns an element of the domain whose elements
++ are the members of \axiom{lp} if such an element exists, otherwise
++ an error is produced.
mvar : $ -> VarSet
++ \axiom{mvar(ps)} returns the main variable of the non constant polynomial
++ with the greatest main variable, if any, else an error is returned.
variables : $ -> List VarSet
++ \axiom{variables(ps)} returns the decreasingly sorted list of the
++ variables which are variables of some polynomial in \axiom{ps}.
mainVariables : $ -> List VarSet
++ \axiom{mainVariables(ps)} returns the decreasingly sorted list of the
++ variables which are main variables of some polynomial in \axiom{ps}.
mainVariable? : (VarSet,$) -> Boolean
++ \axiom{mainVariable?(v,ps)} returns true iff \axiom{v} is the main variable
++ of some polynomial in \axiom{ps}.
collectUnder : ($,VarSet) -> $
++ \axiom{collectUnder(ps,v)} returns the set consisting of the
++ polynomials of \axiom{ps} with main variable less than \axiom{v}.
collect : ($,VarSet) -> $
++ \axiom{collect(ps,v)} returns the set consisting of the
++ polynomials of \axiom{ps} with \axiom{v} as main variable.
collectUpper : ($,VarSet) -> $
++ \axiom{collectUpper(ps,v)} returns the set consisting of the
++ polynomials of \axiom{ps} with main variable greater than \axiom{v}.
sort : ($,VarSet) -> Record(under:$,floor:$,upper:$)
++ \axiom{sort(v,ps)} returns \axiom{us,vs,ws} such that \axiom{us}
++ is \axiom{collectUnder(ps,v)}, \axiom{vs} is \axiom{collect(ps,v)}
++ and \axiom{ws} is \axiom{collectUpper(ps,v)}.
trivialIdeal?: $ -> Boolean
++ \axiom{trivialIdeal?(ps)} returns true iff \axiom{ps} does
++ not contain non-zero elements.
if R has IntegralDomain
then
roughBase? : $ -> Boolean
++ \axiom{roughBase?(ps)} returns true iff for every pair \axiom{{p,q}}
++ of polynomials in \axiom{ps} their leading monomials are
++ relatively prime.
roughSubIdeal? : ($,$) -> Boolean
++ \axiom{roughSubIdeal?(ps1,ps2)} returns true iff it can proved
++ that all polynomials in \axiom{ps1} lie in the ideal generated by
++ \axiom{ps2} in \axiom{\axiom{(R)^(-1) P}} without computing Groebner bases.
roughEqualIdeals? : ($,$) -> Boolean
++ \axiom{roughEqualIdeals?(ps1,ps2)} returns true iff it can
++ proved that \axiom{ps1} and \axiom{ps2} generate the same ideal
++ in \axiom{(R)^(-1) P} without computing Groebner bases.
roughUnitIdeal? : $ -> Boolean
++ \axiom{roughUnitIdeal?(ps)} returns true iff \axiom{ps} contains some
++ non null element lying in the base ring \axiom{R}.
headRemainder : (P,$) -> Record(num:P,den:R)
++ \axiom{headRemainder(a,ps)} returns \axiom{[b,r]} such that the leading
++ monomial of \axiom{b} is reduced in the sense of Groebner bases w.r.t.
++ \axiom{ps} and \axiom{r*a - b} lies in the ideal generated by \axiom{ps}.
remainder : (P,$) -> Record(rnum:R,polnum:P,den:R)
++ \axiom{remainder(a,ps)} returns \axiom{[c,b,r]} such that \axiom{b} is fully
++ reduced in the sense of Groebner bases w.r.t. \axiom{ps},
++ \axiom{r*a - c*b} lies in the ideal generated by \axiom{ps}.
++ Furthermore, if \axiom{R} is a gcd-domain, \axiom{b} is primitive.
rewriteIdealWithHeadRemainder : (List(P),$) -> List(P)
++ \axiom{rewriteIdealWithHeadRemainder(lp,cs)} returns \axiom{lr} such that
++ the leading monomial of every polynomial in \axiom{lr} is reduced
++ in the sense of Groebner bases w.r.t. \axiom{cs} and \axiom{(lp,cs)}
++ and \axiom{(lr,cs)} generate the same ideal in \axiom{(R)^(-1) P}.
rewriteIdealWithRemainder : (List(P),$) -> List(P)
++ \axiom{rewriteIdealWithRemainder(lp,cs)} returns \axiom{lr} such that
++ every polynomial in \axiom{lr} is fully reduced in the sense
++ of Groebner bases w.r.t. \axiom{cs} and \axiom{(lp,cs)} and
++ \axiom{(lr,cs)} generate the same ideal in \axiom{(R)^(-1) P}.
triangular? : $ -> Boolean
++ \axiom{triangular?(ps)} returns true iff \axiom{ps} is a triangular set,
++ i.e. two distinct polynomials have distinct main variables
++ and no constant lies in \axiom{ps}.
add
NNI ==> NonNegativeInteger
B ==> Boolean
elements: $ -> List(P)
elements(ps:$):List(P) ==
lp : List(P) := members(ps)$$
variables1(lp:List(P)):(List VarSet) ==
lvars : List(List(VarSet)) := [variables(p)$P for p in lp]
sort(#1 > #2, removeDuplicates(concat(lvars)$List(VarSet)))
variables2(lp:List(P)):(List VarSet) ==
lvars : List(VarSet) := [mvar(p)$P for p in lp]
sort(#1 > #2, removeDuplicates(lvars)$List(VarSet))
variables (ps:$) ==
variables1(elements(ps))
mainVariables (ps:$) ==
variables2(remove(ground?,elements(ps)))
mainVariable? (v,ps) ==
lp : List(P) := remove(ground?,elements(ps))
while (not empty? lp) and (not (mvar(first(lp)) = v)) repeat
lp := rest lp
(not empty? lp)
collectUnder (ps,v) ==
lp : List P := elements(ps)
lq : List P := []
while (not empty? lp) repeat
p := first lp
lp := rest lp
if (ground?(p)) or (mvar(p) < v)
then
lq := cons(p,lq)
construct(lq)$$
collectUpper (ps,v) ==
lp : List P := elements(ps)
lq : List P := []
while (not empty? lp) repeat
p := first lp
lp := rest lp
if (not ground?(p)) and (mvar(p) > v)
then
lq := cons(p,lq)
construct(lq)$$
collect (ps,v) ==
lp : List P := elements(ps)
lq : List P := []
while (not empty? lp) repeat
p := first lp
lp := rest lp
if (not ground?(p)) and (mvar(p) = v)
then
lq := cons(p,lq)
construct(lq)$$
sort (ps,v) ==
lp : List P := elements(ps)
us : List P := []
vs : List P := []
ws : List P := []
while (not empty? lp) repeat
p := first lp
lp := rest lp
if (ground?(p)) or (mvar(p) < v)
then
us := cons(p,us)
else
if (mvar(p) = v)
then
vs := cons(p,vs)
else
ws := cons(p,ws)
[construct(us)$$,construct(vs)$$,construct(ws)$$]$Record(under:$,floor:$,upper:$)
ps1 = ps2 ==
{p for p in elements(ps1)} =$(Set P) {p for p in elements(ps2)}
exactQuo : (R,R) -> R
localInf? (p:P,q:P):B ==
degree(p) <$E degree(q)
localTriangular? (lp:List(P)):B ==
lp := remove(zero?, lp)
empty? lp => true
any? (ground?, lp) => false
lp := sort(mvar(#1)$P > mvar(#2)$P, lp)
p,q : P
p := first lp
lp := rest lp
while (not empty? lp) and (mvar(p) > mvar((q := first(lp)))) repeat
p := q
lp := rest lp
empty? lp
triangular? ps ==
localTriangular? elements ps
trivialIdeal? ps ==
empty?(remove(zero?,elements(ps))$(List(P)))$(List(P))
if R has IntegralDomain
then
roughUnitIdeal? ps ==
any?(ground?,remove(zero?,elements(ps))$(List(P)))$(List P)
relativelyPrimeLeadingMonomials? (p:P,q:P):B ==
dp : E := degree(p)
dq : E := degree(q)
(sup(dp,dq)$E =$E dp +$E dq)@B
roughBase? ps ==
lp := remove(zero?,elements(ps))$(List(P))
empty? lp => true
rB? : B := true
while (not empty? lp) and rB? repeat
p := first lp
lp := rest lp
copylp := lp
while (not empty? copylp) and rB? repeat
rB? := relativelyPrimeLeadingMonomials?(p,first(copylp))
copylp := rest copylp
rB?
roughSubIdeal?(ps1,ps2) ==
lp: List(P) := rewriteIdealWithRemainder(elements(ps1),ps2)
empty? (remove(zero?,lp))
roughEqualIdeals? (ps1,ps2) ==
ps1 =$$ ps2 => true
roughSubIdeal?(ps1,ps2) and roughSubIdeal?(ps2,ps1)
if (R has GcdDomain) and (VarSet has ConvertibleTo (Symbol))
then
LPR ==> List Polynomial R
LS ==> List Symbol
if R has EuclideanDomain
then
exactQuo(r:R,s:R):R ==
r quo$R s
else
exactQuo(r:R,s:R):R ==
(r exquo$R s)::R
headRemainder (a,ps) ==
lp1 : List(P) := remove(zero?, elements(ps))$(List(P))
empty? lp1 => [a,1$R]
any?(ground?,lp1) => [reductum(a),1$R]
r : R := 1$R
lp1 := sort(localInf?, reverse elements(ps))
lp2 := lp1
e : Union(E, "failed")
while (not zero? a) and (not empty? lp2) repeat
p := first lp2
if ((e:= subtractIfCan(degree(a),degree(p))) case E)
then
g := gcd((lca := leadingCoefficient(a)),(lcp := leadingCoefficient(p)))$R
(lca,lcp) := (exactQuo(lca,g),exactQuo(lcp,g))
a := lcp * reductum(a) - monomial(lca, e::E)$P * reductum(p)
r := r * lcp
lp2 := lp1
else
lp2 := rest lp2
[a,r]
makeIrreducible! (frac:Record(num:P,den:R)):Record(num:P,den:R) ==
g := gcd(frac.den,frac.num)$P
one? g => frac
frac.num := exactQuotient!(frac.num,g)
frac.den := exactQuo(frac.den,g)
frac
remainder (a,ps) ==
hRa := makeIrreducible! headRemainder (a,ps)
a := hRa.num
r : R := hRa.den
zero? a => [1$R,a,r]
b : P := monomial(1$R,degree(a))$P
c : R := leadingCoefficient(a)
while not zero?(a := reductum a) repeat
hRa := makeIrreducible! headRemainder (a,ps)
a := hRa.num
r := r * hRa.den
g := gcd(c,(lca := leadingCoefficient(a)))$R
b := ((hRa.den) * exactQuo(c,g)) * b + monomial(exactQuo(lca,g),degree(a))$P
c := g
[c,b,r]
rewriteIdealWithHeadRemainder(ps,cs) ==
trivialIdeal? cs => ps
roughUnitIdeal? cs => [0$P]
ps := remove(zero?,ps)
empty? ps => ps
any?(ground?,ps) => [1$P]
rs : List P := []
while not empty? ps repeat
p := first ps
ps := rest ps
p := (headRemainder(p,cs)).num
if not zero? p
then
if ground? p
then
ps := []
rs := [1$P]
else
primitivePart! p
rs := cons(p,rs)
removeDuplicates rs
rewriteIdealWithRemainder(ps,cs) ==
trivialIdeal? cs => ps
roughUnitIdeal? cs => [0$P]
ps := remove(zero?,ps)
empty? ps => ps
any?(ground?,ps) => [1$P]
rs : List P := []
while not empty? ps repeat
p := first ps
ps := rest ps
p := (remainder(p,cs)).polnum
if not zero? p
then
if ground? p
then
ps := []
rs := [1$P]
else
rs := cons(unitCanonical(p),rs)
removeDuplicates rs
@
\subsection{sortpak.spad}
\subsubsection{SortPackage}
++ This package exports sorting algorithnms
<<SortPackage>>=
SortPackage(S,A) : Exports == Implementation where
S: Type
A: IndexedAggregate(Integer,S)
with (finiteAggregate; shallowlyMutable)
Exports == with
bubbleSort_!: (A,(S,S) -> Boolean) -> A
++ bubbleSort!(a,f) \undocumented
insertionSort_!: (A, (S,S) -> Boolean) -> A
++ insertionSort!(a,f) \undocumented
if S has OrderedSet then
bubbleSort_!: A -> A
++ bubbleSort!(a) \undocumented
insertionSort_!: A -> A
++ insertionSort! \undocumented
Implementation == add
bubbleSort_!(m,f) ==
n := #m
for i in 1..(n-1) repeat
for j in n..(i+1) by -1 repeat
if f(m.j,m.(j-1)) then swap_!(m,j,j-1)
m
insertionSort_!(m,f) ==
for i in 2..#m repeat
j := i
while j > 1 and f(m.j,m.(j-1)) repeat
swap_!(m,j,j-1)
j := (j - 1) pretend PositiveInteger
m
if S has OrderedSet then
bubbleSort_!(m) == bubbleSort_!(m,_<$S)
insertionSort_!(m) == insertionSort_!(m,_<$S)
if A has UnaryRecursiveAggregate(S) then
bubbleSort_!(m,fn) ==
empty? m => m
l := m
while not empty? (r := l.rest) repeat
r := bubbleSort_!(r,fn)
x := l.first
if fn(r.first,x) then
l.first := r.first
r.first := x
l.rest := r
l := l.rest
m
@
\eject
\begin{thebibliography}{99}
\bibitem{1} {\bf PseudoRemainderSequence in prs.spad}
\end{thebibliography}
\end{document}
|