summaryrefslogtreecommitdiff
path: root/lisp/calc/calc-ext.el
blob: 22b366a04d9f23574438e87d7fccda1f45d91077 (plain)
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
;;; calc-ext.el --- various extension functions for Calc

;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
;;   2005, 2006, 2007 Free Software Foundation, Inc.

;; Author: David Gillespie <daveg@synaptics.com>
;; Maintainer: Jay Belanger <belanger@truman.edu>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;;; Code:

(require 'calc)
(require 'calc-macs)

(defvar math-simplifying nil)
(defvar math-living-dangerously nil)   ; true if unsafe simplifications are okay.
(defvar math-integrating nil)

(defvar math-rewrite-selections nil)

(defvar math-compose-level 0)
(defvar math-comp-selected nil)
(defvar math-comp-tagged nil)
(defvar math-comp-sel-hpos nil)
(defvar math-comp-sel-vpos nil)
(defvar math-comp-sel-cpos nil)
(defvar math-compose-hash-args nil)

(defvar calc-alg-map)
(defvar calc-alg-esc-map)

;;; The following was made a function so that it could be byte-compiled.
(defun calc-init-extensions ()

  (define-key calc-mode-map ":" 'calc-fdiv)
  (define-key calc-mode-map "\\" 'calc-idiv)
  (define-key calc-mode-map "|" 'calc-concat)
  (define-key calc-mode-map "!" 'calc-factorial)
  (define-key calc-mode-map "C" 'calc-cos)
  (define-key calc-mode-map "E" 'calc-exp)
  (define-key calc-mode-map "H" 'calc-hyperbolic)
  (define-key calc-mode-map "I" 'calc-inverse)
  (define-key calc-mode-map "J" 'calc-conj)
  (define-key calc-mode-map "L" 'calc-ln)
  (define-key calc-mode-map "N" 'calc-eval-num)
  (define-key calc-mode-map "P" 'calc-pi)
  (define-key calc-mode-map "Q" 'calc-sqrt)
  (define-key calc-mode-map "R" 'calc-round)
  (define-key calc-mode-map "S" 'calc-sin)
  (define-key calc-mode-map "T" 'calc-tan)
  (define-key calc-mode-map "U" 'calc-undo)
  (define-key calc-mode-map "X" 'calc-call-last-kbd-macro)
  (define-key calc-mode-map "o" 'calc-realign)
  (define-key calc-mode-map "p" 'calc-precision)
  (define-key calc-mode-map "w" 'calc-why)
  (define-key calc-mode-map "x" 'calc-execute-extended-command)
  (define-key calc-mode-map "y" 'calc-copy-to-buffer)

  (define-key calc-mode-map "(" 'calc-begin-complex)
  (define-key calc-mode-map ")" 'calc-end-complex)
  (define-key calc-mode-map "[" 'calc-begin-vector)
  (define-key calc-mode-map "]" 'calc-end-vector)
  (define-key calc-mode-map "," 'calc-comma)
  (define-key calc-mode-map ";" 'calc-semi)
  (define-key calc-mode-map "`" 'calc-edit)
  (define-key calc-mode-map "=" 'calc-evaluate)
  (define-key calc-mode-map "~" 'calc-num-prefix)
  (define-key calc-mode-map "<" 'calc-scroll-left)
  (define-key calc-mode-map ">" 'calc-scroll-right)
  (define-key calc-mode-map "{" 'calc-scroll-down)
  (define-key calc-mode-map "}" 'calc-scroll-up)
  (define-key calc-mode-map "\C-k" 'calc-kill)
  (define-key calc-mode-map "\M-k" 'calc-copy-as-kill)
  (define-key calc-mode-map "\C-w" 'calc-kill-region)
  (define-key calc-mode-map "\M-w" 'calc-copy-region-as-kill)
  (define-key calc-mode-map "\C-y" 'calc-yank)
  (define-key calc-mode-map [mouse-2] 'calc-yank)
  (define-key calc-mode-map "\C-_" 'calc-undo)
  (define-key calc-mode-map "\C-xu" 'calc-undo)
  (define-key calc-mode-map "\M-\C-m" 'calc-last-args)

  (define-key calc-mode-map "a" nil)
  (define-key calc-mode-map "a?" 'calc-a-prefix-help)
  (define-key calc-mode-map "aa" 'calc-apart)
  (define-key calc-mode-map "ab" 'calc-substitute)
  (define-key calc-mode-map "ac" 'calc-collect)
  (define-key calc-mode-map "ad" 'calc-derivative)
  (define-key calc-mode-map "ae" 'calc-simplify-extended)
  (define-key calc-mode-map "af" 'calc-factor)
  (define-key calc-mode-map "ag" 'calc-poly-gcd)
  (define-key calc-mode-map "ai" 'calc-integral)
  (define-key calc-mode-map "am" 'calc-match)
  (define-key calc-mode-map "an" 'calc-normalize-rat)
  (define-key calc-mode-map "ap" 'calc-poly-interp)
  (define-key calc-mode-map "ar" 'calc-rewrite)
  (define-key calc-mode-map "as" 'calc-simplify)
  (define-key calc-mode-map "at" 'calc-taylor)
  (define-key calc-mode-map "av" 'calc-alg-evaluate)
  (define-key calc-mode-map "ax" 'calc-expand)
  (define-key calc-mode-map "aA" 'calc-abs)
  (define-key calc-mode-map "aF" 'calc-curve-fit)
  (define-key calc-mode-map "aI" 'calc-num-integral)
  (define-key calc-mode-map "aM" 'calc-map-equation)
  (define-key calc-mode-map "aN" 'calc-find-minimum)
  (define-key calc-mode-map "aP" 'calc-poly-roots)
  (define-key calc-mode-map "aS" 'calc-solve-for)
  (define-key calc-mode-map "aR" 'calc-find-root)
  (define-key calc-mode-map "aT" 'calc-tabulate)
  (define-key calc-mode-map "aX" 'calc-find-maximum)
  (define-key calc-mode-map "a+" 'calc-summation)
  (define-key calc-mode-map "a-" 'calc-alt-summation)
  (define-key calc-mode-map "a*" 'calc-product)
  (define-key calc-mode-map "a\\" 'calc-poly-div)
  (define-key calc-mode-map "a%" 'calc-poly-rem)
  (define-key calc-mode-map "a/" 'calc-poly-div-rem)
  (define-key calc-mode-map "a=" 'calc-equal-to)
  (define-key calc-mode-map "a#" 'calc-not-equal-to)
  (define-key calc-mode-map "a<" 'calc-less-than)
  (define-key calc-mode-map "a>" 'calc-greater-than)
  (define-key calc-mode-map "a[" 'calc-less-equal)
  (define-key calc-mode-map "a]" 'calc-greater-equal)
  (define-key calc-mode-map "a." 'calc-remove-equal)
  (define-key calc-mode-map "a{" 'calc-in-set)
  (define-key calc-mode-map "a&" 'calc-logical-and)
  (define-key calc-mode-map "a|" 'calc-logical-or)
  (define-key calc-mode-map "a!" 'calc-logical-not)
  (define-key calc-mode-map "a:" 'calc-logical-if)
  (define-key calc-mode-map "a_" 'calc-subscript)
  (define-key calc-mode-map "a\"" 'calc-expand-formula)

  (define-key calc-mode-map "b" nil)
  (define-key calc-mode-map "b?" 'calc-b-prefix-help)
  (define-key calc-mode-map "ba" 'calc-and)
  (define-key calc-mode-map "bc" 'calc-clip)
  (define-key calc-mode-map "bd" 'calc-diff)
  (define-key calc-mode-map "bl" 'calc-lshift-binary)
  (define-key calc-mode-map "bn" 'calc-not)
  (define-key calc-mode-map "bo" 'calc-or)
  (define-key calc-mode-map "bp" 'calc-pack-bits)
  (define-key calc-mode-map "br" 'calc-rshift-binary)
  (define-key calc-mode-map "bt" 'calc-rotate-binary)
  (define-key calc-mode-map "bu" 'calc-unpack-bits)
  (define-key calc-mode-map "bw" 'calc-word-size)
  (define-key calc-mode-map "bx" 'calc-xor)
  (define-key calc-mode-map "bB" 'calc-log)
  (define-key calc-mode-map "bD" 'calc-fin-ddb)
  (define-key calc-mode-map "bF" 'calc-fin-fv)
  (define-key calc-mode-map "bI" 'calc-fin-irr)
  (define-key calc-mode-map "bL" 'calc-lshift-arith)
  (define-key calc-mode-map "bM" 'calc-fin-pmt)
  (define-key calc-mode-map "bN" 'calc-fin-npv)
  (define-key calc-mode-map "bP" 'calc-fin-pv)
  (define-key calc-mode-map "bR" 'calc-rshift-arith)
  (define-key calc-mode-map "bS" 'calc-fin-sln)
  (define-key calc-mode-map "bT" 'calc-fin-rate)
  (define-key calc-mode-map "bY" 'calc-fin-syd)
  (define-key calc-mode-map "b#" 'calc-fin-nper)
  (define-key calc-mode-map "b%" 'calc-percent-change)

  (define-key calc-mode-map "c" nil)
  (define-key calc-mode-map "c?" 'calc-c-prefix-help)
  (define-key calc-mode-map "cc" 'calc-clean)
  (define-key calc-mode-map "cd" 'calc-to-degrees)
  (define-key calc-mode-map "cf" 'calc-float)
  (define-key calc-mode-map "ch" 'calc-to-hms)
  (define-key calc-mode-map "cp" 'calc-polar)
  (define-key calc-mode-map "cr" 'calc-to-radians)
  (define-key calc-mode-map "cC" 'calc-cos)
  (define-key calc-mode-map "cF" 'calc-fraction)
  (define-key calc-mode-map "c%" 'calc-convert-percent)

  (define-key calc-mode-map "d" nil)
  (define-key calc-mode-map "d?" 'calc-d-prefix-help)
  (define-key calc-mode-map "d0" 'calc-decimal-radix)
  (define-key calc-mode-map "d2" 'calc-binary-radix)
  (define-key calc-mode-map "d6" 'calc-hex-radix)
  (define-key calc-mode-map "d8" 'calc-octal-radix)
  (define-key calc-mode-map "db" 'calc-line-breaking)
  (define-key calc-mode-map "dc" 'calc-complex-notation)
  (define-key calc-mode-map "dd" 'calc-date-notation)
  (define-key calc-mode-map "de" 'calc-eng-notation)
  (define-key calc-mode-map "df" 'calc-fix-notation)
  (define-key calc-mode-map "dg" 'calc-group-digits)
  (define-key calc-mode-map "dh" 'calc-hms-notation)
  (define-key calc-mode-map "di" 'calc-i-notation)
  (define-key calc-mode-map "dj" 'calc-j-notation)
  (define-key calc-mode-map "dl" 'calc-line-numbering)
  (define-key calc-mode-map "dn" 'calc-normal-notation)
  (define-key calc-mode-map "do" 'calc-over-notation)
  (define-key calc-mode-map "dp" 'calc-show-plain)
  (define-key calc-mode-map "dr" 'calc-radix)
  (define-key calc-mode-map "ds" 'calc-sci-notation)
  (define-key calc-mode-map "dt" 'calc-truncate-stack)
  (define-key calc-mode-map "dw" 'calc-auto-why)
  (define-key calc-mode-map "dz" 'calc-leading-zeros)
  (define-key calc-mode-map "dB" 'calc-big-language)
  (define-key calc-mode-map "dD" 'calc-redo)
  (define-key calc-mode-map "dC" 'calc-c-language)
  (define-key calc-mode-map "dE" 'calc-eqn-language)
  (define-key calc-mode-map "dF" 'calc-fortran-language)
  (define-key calc-mode-map "dM" 'calc-mathematica-language)
  (define-key calc-mode-map "dN" 'calc-normal-language)
  (define-key calc-mode-map "dO" 'calc-flat-language)
  (define-key calc-mode-map "dP" 'calc-pascal-language)
  (define-key calc-mode-map "dT" 'calc-tex-language)
  (define-key calc-mode-map "dL" 'calc-latex-language)
  (define-key calc-mode-map "dU" 'calc-unformatted-language)
  (define-key calc-mode-map "dW" 'calc-maple-language)
  (define-key calc-mode-map "d[" 'calc-truncate-up)
  (define-key calc-mode-map "d]" 'calc-truncate-down)
  (define-key calc-mode-map "d." 'calc-point-char)
  (define-key calc-mode-map "d," 'calc-group-char)
  (define-key calc-mode-map "d\"" 'calc-display-strings)
  (define-key calc-mode-map "d<" 'calc-left-justify)
  (define-key calc-mode-map "d=" 'calc-center-justify)
  (define-key calc-mode-map "d>" 'calc-right-justify)
  (define-key calc-mode-map "d{" 'calc-left-label)
  (define-key calc-mode-map "d}" 'calc-right-label)
  (define-key calc-mode-map "d'" 'calc-display-raw)
  (define-key calc-mode-map "d " 'calc-refresh)
  (define-key calc-mode-map "d\r" 'calc-refresh-top)
  (define-key calc-mode-map "d@" 'calc-toggle-banner)

  (define-key calc-mode-map "f" nil)
  (define-key calc-mode-map "f?" 'calc-f-prefix-help)
  (define-key calc-mode-map "fb" 'calc-beta)
  (define-key calc-mode-map "fe" 'calc-erf)
  (define-key calc-mode-map "fg" 'calc-gamma)
  (define-key calc-mode-map "fh" 'calc-hypot)
  (define-key calc-mode-map "fi" 'calc-im)
  (define-key calc-mode-map "fj" 'calc-bessel-J)
  (define-key calc-mode-map "fn" 'calc-min)
  (define-key calc-mode-map "fr" 'calc-re)
  (define-key calc-mode-map "fs" 'calc-sign)
  (define-key calc-mode-map "fx" 'calc-max)
  (define-key calc-mode-map "fy" 'calc-bessel-Y)
  (define-key calc-mode-map "fA" 'calc-abssqr)
  (define-key calc-mode-map "fB" 'calc-inc-beta)
  (define-key calc-mode-map "fE" 'calc-expm1)
  (define-key calc-mode-map "fF" 'calc-floor)
  (define-key calc-mode-map "fG" 'calc-inc-gamma)
  (define-key calc-mode-map "fI" 'calc-ilog)
  (define-key calc-mode-map "fL" 'calc-lnp1)
  (define-key calc-mode-map "fM" 'calc-mant-part)
  (define-key calc-mode-map "fQ" 'calc-isqrt)
  (define-key calc-mode-map "fS" 'calc-scale-float)
  (define-key calc-mode-map "fT" 'calc-arctan2)
  (define-key calc-mode-map "fX" 'calc-xpon-part)
  (define-key calc-mode-map "f[" 'calc-decrement)
  (define-key calc-mode-map "f]" 'calc-increment)

  (define-key calc-mode-map "g" nil)
  (define-key calc-mode-map "g?" 'calc-g-prefix-help)
  (define-key calc-mode-map "ga" 'calc-graph-add)
  (define-key calc-mode-map "gb" 'calc-graph-border)
  (define-key calc-mode-map "gc" 'calc-graph-clear)
  (define-key calc-mode-map "gd" 'calc-graph-delete)
  (define-key calc-mode-map "gf" 'calc-graph-fast)
  (define-key calc-mode-map "gg" 'calc-graph-grid)
  (define-key calc-mode-map "gh" 'calc-graph-header)
  (define-key calc-mode-map "gk" 'calc-graph-key)
  (define-key calc-mode-map "gj" 'calc-graph-juggle)
  (define-key calc-mode-map "gl" 'calc-graph-log-x)
  (define-key calc-mode-map "gn" 'calc-graph-name)
  (define-key calc-mode-map "gp" 'calc-graph-plot)
  (define-key calc-mode-map "gq" 'calc-graph-quit)
  (define-key calc-mode-map "gr" 'calc-graph-range-x)
  (define-key calc-mode-map "gs" 'calc-graph-line-style)
  (define-key calc-mode-map "gt" 'calc-graph-title-x)
  (define-key calc-mode-map "gv" 'calc-graph-view-commands)
  (define-key calc-mode-map "gx" 'calc-graph-display)
  (define-key calc-mode-map "gz" 'calc-graph-zero-x)
  (define-key calc-mode-map "gA" 'calc-graph-add-3d)
  (define-key calc-mode-map "gC" 'calc-graph-command)
  (define-key calc-mode-map "gD" 'calc-graph-device)
  (define-key calc-mode-map "gF" 'calc-graph-fast-3d)
  (define-key calc-mode-map "gG" 'calc-argument)
  (define-key calc-mode-map "gH" 'calc-graph-hide)
  (define-key calc-mode-map "gK" 'calc-graph-kill)
  (define-key calc-mode-map "gL" 'calc-graph-log-y)
  (define-key calc-mode-map "gN" 'calc-graph-num-points)
  (define-key calc-mode-map "gO" 'calc-graph-output)
  (define-key calc-mode-map "gP" 'calc-graph-print)
  (define-key calc-mode-map "gR" 'calc-graph-range-y)
  (define-key calc-mode-map "gS" 'calc-graph-point-style)
  (define-key calc-mode-map "gT" 'calc-graph-title-y)
  (define-key calc-mode-map "gV" 'calc-graph-view-trail)
  (define-key calc-mode-map "gX" 'calc-graph-geometry)
  (define-key calc-mode-map "gZ" 'calc-graph-zero-y)
  (define-key calc-mode-map "g\C-l" 'calc-graph-log-z)
  (define-key calc-mode-map "g\C-r" 'calc-graph-range-z)
  (define-key calc-mode-map "g\C-t" 'calc-graph-title-z)

  (define-key calc-mode-map "h" 'calc-help-prefix)

  (define-key calc-mode-map "j" nil)
  (define-key calc-mode-map "j?" 'calc-j-prefix-help)
  (define-key calc-mode-map "ja" 'calc-select-additional)
  (define-key calc-mode-map "jb" 'calc-break-selections)
  (define-key calc-mode-map "jc" 'calc-clear-selections)
  (define-key calc-mode-map "jd" 'calc-show-selections)
  (define-key calc-mode-map "je" 'calc-enable-selections)
  (define-key calc-mode-map "jl" 'calc-select-less)
  (define-key calc-mode-map "jm" 'calc-select-more)
  (define-key calc-mode-map "jn" 'calc-select-next)
  (define-key calc-mode-map "jo" 'calc-select-once)
  (define-key calc-mode-map "jp" 'calc-select-previous)
  (define-key calc-mode-map "jr" 'calc-rewrite-selection)
  (define-key calc-mode-map "js" 'calc-select-here)
  (define-key calc-mode-map "jv" 'calc-sel-evaluate)
  (define-key calc-mode-map "ju" 'calc-unselect)
  (define-key calc-mode-map "jC" 'calc-sel-commute)
  (define-key calc-mode-map "jD" 'calc-sel-distribute)
  (define-key calc-mode-map "jE" 'calc-sel-jump-equals)
  (define-key calc-mode-map "jI" 'calc-sel-isolate)
  (define-key calc-mode-map "jJ" 'calc-conj)
  (define-key calc-mode-map "jL" 'calc-commute-left)
  (define-key calc-mode-map "jM" 'calc-sel-merge)
  (define-key calc-mode-map "jN" 'calc-sel-negate)
  (define-key calc-mode-map "jO" 'calc-select-once-maybe)
  (define-key calc-mode-map "jR" 'calc-commute-right)
  (define-key calc-mode-map "jS" 'calc-select-here-maybe)
  (define-key calc-mode-map "jU" 'calc-sel-unpack)
  (define-key calc-mode-map "j&" 'calc-sel-invert)
  (define-key calc-mode-map "j\r" 'calc-copy-selection)
  (define-key calc-mode-map "j\n" 'calc-copy-selection)
  (define-key calc-mode-map "j\010" 'calc-del-selection)
  (define-key calc-mode-map "j\177" 'calc-del-selection)
  (define-key calc-mode-map "j'" 'calc-enter-selection)
  (define-key calc-mode-map "j`" 'calc-edit-selection)
  (define-key calc-mode-map "j+" 'calc-sel-add-both-sides)
  (define-key calc-mode-map "j-" 'calc-sel-sub-both-sides)
  (define-key calc-mode-map "j*" 'calc-sel-mult-both-sides)
  (define-key calc-mode-map "j/" 'calc-sel-div-both-sides)
  (define-key calc-mode-map "j\"" 'calc-sel-expand-formula)

  (define-key calc-mode-map "k" nil)
  (define-key calc-mode-map "k?" 'calc-k-prefix-help)
  (define-key calc-mode-map "ka" 'calc-random-again)
  (define-key calc-mode-map "kb" 'calc-bernoulli-number)
  (define-key calc-mode-map "kc" 'calc-choose)
  (define-key calc-mode-map "kd" 'calc-double-factorial)
  (define-key calc-mode-map "ke" 'calc-euler-number)
  (define-key calc-mode-map "kf" 'calc-prime-factors)
  (define-key calc-mode-map "kg" 'calc-gcd)
  (define-key calc-mode-map "kh" 'calc-shuffle)
  (define-key calc-mode-map "kl" 'calc-lcm)
  (define-key calc-mode-map "km" 'calc-moebius)
  (define-key calc-mode-map "kn" 'calc-next-prime)
  (define-key calc-mode-map "kp" 'calc-prime-test)
  (define-key calc-mode-map "kr" 'calc-random)
  (define-key calc-mode-map "ks" 'calc-stirling-number)
  (define-key calc-mode-map "kt" 'calc-totient)
  (define-key calc-mode-map "kB" 'calc-utpb)
  (define-key calc-mode-map "kC" 'calc-utpc)
  (define-key calc-mode-map "kE" 'calc-extended-gcd)
  (define-key calc-mode-map "kF" 'calc-utpf)
  (define-key calc-mode-map "kK" 'calc-keep-args)
  (define-key calc-mode-map "kN" 'calc-utpn)
  (define-key calc-mode-map "kP" 'calc-utpp)
  (define-key calc-mode-map "kT" 'calc-utpt)

  (define-key calc-mode-map "m" nil)
  (define-key calc-mode-map "m?" 'calc-m-prefix-help)
  (define-key calc-mode-map "ma" 'calc-algebraic-mode)
  (define-key calc-mode-map "md" 'calc-degrees-mode)
  (define-key calc-mode-map "me" 'calc-embedded-preserve-modes)
  (define-key calc-mode-map "mf" 'calc-frac-mode)
  (define-key calc-mode-map "mg" 'calc-get-modes)
  (define-key calc-mode-map "mh" 'calc-hms-mode)
  (define-key calc-mode-map "mi" 'calc-infinite-mode)
  (define-key calc-mode-map "mm" 'calc-save-modes)
  (define-key calc-mode-map "mp" 'calc-polar-mode)
  (define-key calc-mode-map "mr" 'calc-radians-mode)
  (define-key calc-mode-map "ms" 'calc-symbolic-mode)
  (define-key calc-mode-map "mt" 'calc-total-algebraic-mode)
  (define-key calc-mode-map "\emt" 'calc-total-algebraic-mode)
  (define-key calc-mode-map "\em\et" 'calc-total-algebraic-mode)
  (define-key calc-mode-map "mv" 'calc-matrix-mode)
  (define-key calc-mode-map "mw" 'calc-working)
  (define-key calc-mode-map "mx" 'calc-always-load-extensions)
  (define-key calc-mode-map "mA" 'calc-alg-simplify-mode)
  (define-key calc-mode-map "mB" 'calc-bin-simplify-mode)
  (define-key calc-mode-map "mC" 'calc-auto-recompute)
  (define-key calc-mode-map "mD" 'calc-default-simplify-mode)
  (define-key calc-mode-map "mE" 'calc-ext-simplify-mode)
  (define-key calc-mode-map "mF" 'calc-settings-file-name)
  (define-key calc-mode-map "mM" 'calc-more-recursion-depth)
  (define-key calc-mode-map "mN" 'calc-num-simplify-mode)
  (define-key calc-mode-map "mO" 'calc-no-simplify-mode)
  (define-key calc-mode-map "mR" 'calc-mode-record-mode)
  (define-key calc-mode-map "mS" 'calc-shift-prefix)
  (define-key calc-mode-map "mU" 'calc-units-simplify-mode)
  (define-key calc-mode-map "mX" 'calc-load-everything)

  (define-key calc-mode-map "r" nil)
  (define-key calc-mode-map "r?" 'calc-r-prefix-help)

  (define-key calc-mode-map "s" nil)
  (define-key calc-mode-map "s?" 'calc-s-prefix-help)
  (define-key calc-mode-map "sc" 'calc-copy-variable)
  (define-key calc-mode-map "sd" 'calc-declare-variable)
  (define-key calc-mode-map "se" 'calc-edit-variable)
  (define-key calc-mode-map "si" 'calc-insert-variables)
  (define-key calc-mode-map "sk" 'calc-copy-special-constant)
  (define-key calc-mode-map "sl" 'calc-let)
  (define-key calc-mode-map "sm" 'calc-store-map)
  (define-key calc-mode-map "sn" 'calc-store-neg)
  (define-key calc-mode-map "sp" 'calc-permanent-variable)
  (define-key calc-mode-map "sr" 'calc-recall)
  (define-key calc-mode-map "ss" 'calc-store)
  (define-key calc-mode-map "st" 'calc-store-into)
  (define-key calc-mode-map "su" 'calc-unstore)
  (define-key calc-mode-map "sx" 'calc-store-exchange)
  (define-key calc-mode-map "sA" 'calc-edit-AlgSimpRules)
  (define-key calc-mode-map "sD" 'calc-edit-Decls)
  (define-key calc-mode-map "sE" 'calc-edit-EvalRules)
  (define-key calc-mode-map "sF" 'calc-edit-FitRules)
  (define-key calc-mode-map "sG" 'calc-edit-GenCount)
  (define-key calc-mode-map "sH" 'calc-edit-Holidays)
  (define-key calc-mode-map "sI" 'calc-edit-IntegLimit)
  (define-key calc-mode-map "sL" 'calc-edit-LineStyles)
  (define-key calc-mode-map "sP" 'calc-edit-PointStyles)
  (define-key calc-mode-map "sR" 'calc-edit-PlotRejects)
  (define-key calc-mode-map "sS" 'calc-sin)
  (define-key calc-mode-map "sT" 'calc-edit-TimeZone)
  (define-key calc-mode-map "sU" 'calc-edit-Units)
  (define-key calc-mode-map "sX" 'calc-edit-ExtSimpRules)
  (define-key calc-mode-map "s+" 'calc-store-plus)
  (define-key calc-mode-map "s-" 'calc-store-minus)
  (define-key calc-mode-map "s*" 'calc-store-times)
  (define-key calc-mode-map "s/" 'calc-store-div)
  (define-key calc-mode-map "s^" 'calc-store-power)
  (define-key calc-mode-map "s|" 'calc-store-concat)
  (define-key calc-mode-map "s&" 'calc-store-inv)
  (define-key calc-mode-map "s[" 'calc-store-decr)
  (define-key calc-mode-map "s]" 'calc-store-incr)
  (define-key calc-mode-map "s:" 'calc-assign)
  (define-key calc-mode-map "s=" 'calc-evalto)

  (define-key calc-mode-map "t" nil)
  (define-key calc-mode-map "t?" 'calc-t-prefix-help)
  (define-key calc-mode-map "tb" 'calc-trail-backward)
  (define-key calc-mode-map "td" 'calc-trail-display)
  (define-key calc-mode-map "tf" 'calc-trail-forward)
  (define-key calc-mode-map "th" 'calc-trail-here)
  (define-key calc-mode-map "ti" 'calc-trail-in)
  (define-key calc-mode-map "tk" 'calc-trail-kill)
  (define-key calc-mode-map "tm" 'calc-trail-marker)
  (define-key calc-mode-map "tn" 'calc-trail-next)
  (define-key calc-mode-map "to" 'calc-trail-out)
  (define-key calc-mode-map "tp" 'calc-trail-previous)
  (define-key calc-mode-map "tr" 'calc-trail-isearch-backward)
  (define-key calc-mode-map "ts" 'calc-trail-isearch-forward)
  (define-key calc-mode-map "ty" 'calc-trail-yank)
  (define-key calc-mode-map "t[" 'calc-trail-first)
  (define-key calc-mode-map "t]" 'calc-trail-last)
  (define-key calc-mode-map "t<" 'calc-trail-scroll-left)
  (define-key calc-mode-map "t>" 'calc-trail-scroll-right)
  (define-key calc-mode-map "t{" 'calc-trail-backward)
  (define-key calc-mode-map "t}" 'calc-trail-forward)
  (define-key calc-mode-map "t." 'calc-full-trail-vectors)
  (define-key calc-mode-map "tC" 'calc-convert-time-zones)
  (define-key calc-mode-map "tD" 'calc-date)
  (define-key calc-mode-map "tI" 'calc-inc-month)
  (define-key calc-mode-map "tJ" 'calc-julian)
  (define-key calc-mode-map "tM" 'calc-new-month)
  (define-key calc-mode-map "tN" 'calc-now)
  (define-key calc-mode-map "tP" 'calc-date-part)
  (define-key calc-mode-map "tT" 'calc-tan)
  (define-key calc-mode-map "tU" 'calc-unix-time)
  (define-key calc-mode-map "tW" 'calc-new-week)
  (define-key calc-mode-map "tY" 'calc-new-year)
  (define-key calc-mode-map "tZ" 'calc-time-zone)
  (define-key calc-mode-map "t+" 'calc-business-days-plus)
  (define-key calc-mode-map "t-" 'calc-business-days-minus)

  (define-key calc-mode-map "u" 'nil)
  (define-key calc-mode-map "u?" 'calc-u-prefix-help)
  (define-key calc-mode-map "ua" 'calc-autorange-units)
  (define-key calc-mode-map "ub" 'calc-base-units)
  (define-key calc-mode-map "uc" 'calc-convert-units)
  (define-key calc-mode-map "ud" 'calc-define-unit)
  (define-key calc-mode-map "ue" 'calc-explain-units)
  (define-key calc-mode-map "ug" 'calc-get-unit-definition)
  (define-key calc-mode-map "up" 'calc-permanent-units)
  (define-key calc-mode-map "ur" 'calc-remove-units)
  (define-key calc-mode-map "us" 'calc-simplify-units)
  (define-key calc-mode-map "ut" 'calc-convert-temperature)
  (define-key calc-mode-map "uu" 'calc-undefine-unit)
  (define-key calc-mode-map "uv" 'calc-enter-units-table)
  (define-key calc-mode-map "ux" 'calc-extract-units)
  (define-key calc-mode-map "uV" 'calc-view-units-table)
  (define-key calc-mode-map "uC" 'calc-vector-covariance)
  (define-key calc-mode-map "uG" 'calc-vector-geometric-mean)
  (define-key calc-mode-map "uM" 'calc-vector-mean)
  (define-key calc-mode-map "uN" 'calc-vector-min)
  (define-key calc-mode-map "uS" 'calc-vector-sdev)
  (define-key calc-mode-map "uU" 'calc-undo)
  (define-key calc-mode-map "uX" 'calc-vector-max)
  (define-key calc-mode-map "u#" 'calc-vector-count)
  (define-key calc-mode-map "u+" 'calc-vector-sum)
  (define-key calc-mode-map "u*" 'calc-vector-product)

  (define-key calc-mode-map "v" 'nil)
  (define-key calc-mode-map "v?" 'calc-v-prefix-help)
  (define-key calc-mode-map "va" 'calc-arrange-vector)
  (define-key calc-mode-map "vb" 'calc-build-vector)
  (define-key calc-mode-map "vc" 'calc-mcol)
  (define-key calc-mode-map "vd" 'calc-diag)
  (define-key calc-mode-map "ve" 'calc-expand-vector)
  (define-key calc-mode-map "vf" 'calc-vector-find)
  (define-key calc-mode-map "vh" 'calc-head)
  (define-key calc-mode-map "vi" 'calc-ident)
  (define-key calc-mode-map "vk" 'calc-cons)
  (define-key calc-mode-map "vl" 'calc-vlength)
  (define-key calc-mode-map "vm" 'calc-mask-vector)
  (define-key calc-mode-map "vn" 'calc-rnorm)
  (define-key calc-mode-map "vp" 'calc-pack)
  (define-key calc-mode-map "vr" 'calc-mrow)
  (define-key calc-mode-map "vs" 'calc-subvector)
  (define-key calc-mode-map "vt" 'calc-transpose)
  (define-key calc-mode-map "vu" 'calc-unpack)
  (define-key calc-mode-map "vv" 'calc-reverse-vector)
  (define-key calc-mode-map "vx" 'calc-index)
  (define-key calc-mode-map "vA" 'calc-apply)
  (define-key calc-mode-map "vC" 'calc-cross)
  (define-key calc-mode-map "vD" 'calc-mdet)
  (define-key calc-mode-map "vE" 'calc-set-enumerate)
  (define-key calc-mode-map "vF" 'calc-set-floor)
  (define-key calc-mode-map "vG" 'calc-grade)
  (define-key calc-mode-map "vH" 'calc-histogram)
  (define-key calc-mode-map "vI" 'calc-inner-product)
  (define-key calc-mode-map "vJ" 'calc-conj-transpose)
  (define-key calc-mode-map "vL" 'calc-mlud)
  (define-key calc-mode-map "vM" 'calc-map)
  (define-key calc-mode-map "vN" 'calc-cnorm)
  (define-key calc-mode-map "vO" 'calc-outer-product)
  (define-key calc-mode-map "vR" 'calc-reduce)
  (define-key calc-mode-map "vS" 'calc-sort)
  (define-key calc-mode-map "vT" 'calc-mtrace)
  (define-key calc-mode-map "vU" 'calc-accumulate)
  (define-key calc-mode-map "vV" 'calc-set-union)
  (define-key calc-mode-map "vX" 'calc-set-xor)
  (define-key calc-mode-map "v^" 'calc-set-intersect)
  (define-key calc-mode-map "v-" 'calc-set-difference)
  (define-key calc-mode-map "v~" 'calc-set-complement)
  (define-key calc-mode-map "v:" 'calc-set-span)
  (define-key calc-mode-map "v#" 'calc-set-cardinality)
  (define-key calc-mode-map "v+" 'calc-remove-duplicates)
  (define-key calc-mode-map "v&" 'calc-inv)
  (define-key calc-mode-map "v<" 'calc-matrix-left-justify)
  (define-key calc-mode-map "v=" 'calc-matrix-center-justify)
  (define-key calc-mode-map "v>" 'calc-matrix-right-justify)
  (define-key calc-mode-map "v." 'calc-full-vectors)
  (define-key calc-mode-map "v/" 'calc-break-vectors)
  (define-key calc-mode-map "v," 'calc-vector-commas)
  (define-key calc-mode-map "v[" 'calc-vector-brackets)
  (define-key calc-mode-map "v]" 'calc-matrix-brackets)
  (define-key calc-mode-map "v{" 'calc-vector-braces)
  (define-key calc-mode-map "v}" 'calc-matrix-brackets)
  (define-key calc-mode-map "v(" 'calc-vector-parens)
  (define-key calc-mode-map "v)" 'calc-matrix-brackets)
  ;; We can't rely on the automatic upper->lower conversion because
  ;; in the global map V is explicitly bound, so we need to bind it
  ;; explicitly as well :-(  --stef
  (define-key calc-mode-map "V" (lookup-key calc-mode-map "v"))

  (define-key calc-mode-map "z" 'nil)
  (define-key calc-mode-map "z?" 'calc-z-prefix-help)

  (define-key calc-mode-map "Z" 'nil)
  (define-key calc-mode-map "Z?" 'calc-shift-Z-prefix-help)
  (define-key calc-mode-map "ZC" 'calc-user-define-composition)
  (define-key calc-mode-map "ZD" 'calc-user-define)
  (define-key calc-mode-map "ZE" 'calc-user-define-edit)
  (define-key calc-mode-map "ZF" 'calc-user-define-formula)
  (define-key calc-mode-map "ZG" 'calc-get-user-defn)
  (define-key calc-mode-map "ZI" 'calc-user-define-invocation)
  (define-key calc-mode-map "ZK" 'calc-user-define-kbd-macro)
  (define-key calc-mode-map "ZP" 'calc-user-define-permanent)
  (define-key calc-mode-map "ZS" 'calc-edit-user-syntax)
  (define-key calc-mode-map "ZT" 'calc-timing)
  (define-key calc-mode-map "ZU" 'calc-user-undefine)
  (define-key calc-mode-map "Z[" 'calc-kbd-if)
  (define-key calc-mode-map "Z:" 'calc-kbd-else)
  (define-key calc-mode-map "Z|" 'calc-kbd-else-if)
  (define-key calc-mode-map "Z]" 'calc-kbd-end-if)
  (define-key calc-mode-map "Z<" 'calc-kbd-repeat)
  (define-key calc-mode-map "Z>" 'calc-kbd-end-repeat)
  (define-key calc-mode-map "Z(" 'calc-kbd-for)
  (define-key calc-mode-map "Z)" 'calc-kbd-end-for)
  (define-key calc-mode-map "Z{" 'calc-kbd-loop)
  (define-key calc-mode-map "Z}" 'calc-kbd-end-loop)
  (define-key calc-mode-map "Z/" 'calc-kbd-break)
  (define-key calc-mode-map "Z`" 'calc-kbd-push)
  (define-key calc-mode-map "Z'" 'calc-kbd-pop)
  (define-key calc-mode-map "Z=" 'calc-kbd-report)
  (define-key calc-mode-map "Z#" 'calc-kbd-query)

  (calc-init-prefixes)

  (mapcar (function
	   (lambda (x)
	     (define-key calc-mode-map (format "c%c" x) 'calc-clean-num)
	     (define-key calc-mode-map (format "j%c" x) 'calc-select-part)
	     (define-key calc-mode-map (format "r%c" x) 'calc-recall-quick)
	     (define-key calc-mode-map (format "s%c" x) 'calc-store-quick)
	     (define-key calc-mode-map (format "t%c" x) 'calc-store-into-quick)
	     (define-key calc-mode-map (format "u%c" x) 'calc-quick-units)))
	  "0123456789")

  (let ((i ?A))
    (while (<= i ?z)
      (if (eq (car-safe (aref (nth 1 calc-mode-map) i)) 'keymap)
	  (aset (nth 1 calc-mode-map) i
		(cons 'keymap (cons (cons ?\e (aref (nth 1 calc-mode-map) i))
				    (cdr (aref (nth 1 calc-mode-map) i))))))
      (setq i (1+ i))))
  
  (setq calc-alg-map (copy-keymap calc-mode-map)
	calc-alg-esc-map (copy-keymap esc-map))
  (let ((i 32))
    (while (< i 127)
      (or (memq i '(?' ?` ?= ??))
	  (aset (nth 1 calc-alg-map) i 'calc-auto-algebraic-entry))
      (or (memq i '(?# ?x ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
	  (aset (nth 1 calc-alg-esc-map) i (aref (nth 1 calc-mode-map) i)))
      (setq i (1+ i))))
  (define-key calc-alg-map "\e" calc-alg-esc-map)
  (define-key calc-alg-map "\e\t" 'calc-roll-up)
  (define-key calc-alg-map "\e\C-m" 'calc-last-args-stub)
  (define-key calc-alg-map "\e\177" 'calc-pop-above)

;;;; (Autoloads here)
  (mapcar (function (lambda (x)
    (mapcar (function (lambda (func)
      (autoload func (car x)))) (cdr x))))
    '(

 ("calc-alg" calc-has-rules math-defsimplify
calc-modify-simplify-mode calcFunc-collect calcFunc-esimplify
calcFunc-islin calcFunc-islinnt calcFunc-lin calcFunc-linnt
calcFunc-simplify calcFunc-subst calcFunc-powerexpand math-beforep
math-build-polynomial-expr math-expand-formula math-expr-contains
math-expr-contains-count math-expr-depends math-expr-height
math-expr-subst math-expr-weight math-integer-plus math-is-linear
math-is-multiple math-is-polynomial math-linear-in math-multiple-of
math-poly-depends math-poly-mix math-poly-mul
math-poly-simplify math-poly-zerop math-polynomial-base
math-polynomial-p math-recompile-eval-rules math-simplify
math-simplify-exp math-simplify-extended math-simplify-sqrt
math-to-simple-fraction)

 ("calcalg2" calcFunc-asum calcFunc-deriv
calcFunc-ffinv calcFunc-finv calcFunc-fsolve calcFunc-gpoly
calcFunc-integ calcFunc-poly calcFunc-prod calcFunc-roots
calcFunc-solve calcFunc-sum calcFunc-table calcFunc-taylor
calcFunc-tderiv math-expr-calls math-integral-q02 math-integral-q12
math-integral-rational-funcs math-lcm-denoms math-looks-evenp
math-poly-all-roots math-prod-rec math-reject-solution math-solve-eqn
math-solve-for math-sum-rec math-try-integral)

 ("calcalg3" calcFunc-efit calcFunc-fit
calcFunc-fitdummy calcFunc-fitparam calcFunc-fitvar
calcFunc-hasfitparams calcFunc-hasfitvars calcFunc-maximize
calcFunc-minimize calcFunc-ninteg calcFunc-polint calcFunc-ratint
calcFunc-root calcFunc-wmaximize calcFunc-wminimize calcFunc-wroot
calcFunc-xfit math-find-minimum math-find-root math-ninteg-evaluate
math-ninteg-midpoint math-ninteg-romberg math-poly-interp)

 ("calc-arith" calcFunc-abs calcFunc-abssqr
calcFunc-add calcFunc-ceil calcFunc-decr calcFunc-deven calcFunc-dimag
calcFunc-dint calcFunc-div calcFunc-dnatnum calcFunc-dneg
calcFunc-dnonneg calcFunc-dnonzero calcFunc-dnumint calcFunc-dodd
calcFunc-dpos calcFunc-drange calcFunc-drat calcFunc-dreal
calcFunc-dscalar calcFunc-fceil calcFunc-ffloor calcFunc-float
calcFunc-fround calcFunc-frounde calcFunc-froundu calcFunc-ftrunc
calcFunc-idiv calcFunc-incr calcFunc-ldiv calcFunc-mant calcFunc-max calcFunc-min
calcFunc-mod calcFunc-mul calcFunc-neg calcFunc-percent calcFunc-pow
calcFunc-relch calcFunc-round calcFunc-rounde calcFunc-roundu
calcFunc-scf calcFunc-sub calcFunc-xpon math-abs math-abs-approx
math-add-objects-fancy math-add-or-sub math-add-symb-fancy
math-ceiling math-combine-prod math-combine-sum math-div-by-zero
math-div-objects-fancy math-div-symb-fancy math-div-zero
math-float-fancy math-floor-fancy math-floor-special math-guess-if-neg
math-intv-constp math-known-evenp math-known-imagp math-known-integerp
math-known-matrixp math-known-negp math-known-nonnegp
math-known-nonposp math-known-nonzerop math-known-num-integerp
math-known-oddp math-known-posp math-known-realp math-known-scalarp
math-max math-min math-mod-fancy math-mul-float math-mul-objects-fancy
math-mul-or-div math-mul-symb-fancy math-mul-zero math-neg-fancy
math-neg-float math-okay-neg math-possible-signs math-possible-types
math-pow-fancy math-pow-mod math-pow-of-zero math-pow-zero
math-quarter-integer math-round math-setup-declarations math-sqr
math-sqr-float math-trunc-fancy math-trunc-special)

 ("calc-bin" calcFunc-and calcFunc-ash
calcFunc-clip calcFunc-diff calcFunc-lsh calcFunc-not calcFunc-or
calcFunc-rash calcFunc-rot calcFunc-rsh calcFunc-xor math-clip
math-compute-max-digits math-convert-radix-digits math-float-parts
math-format-bignum-binary math-format-bignum-hex
math-format-bignum-octal math-format-bignum-radix math-format-binary
math-format-radix math-format-radix-float math-integer-log2
math-power-of-2 math-radix-float-power)

 ("calc-comb"  calc-report-prime-test
calcFunc-choose calcFunc-dfact calcFunc-egcd calcFunc-fact
calcFunc-gcd calcFunc-lcm calcFunc-moebius calcFunc-nextprime
calcFunc-perm calcFunc-prevprime calcFunc-prfac calcFunc-prime
calcFunc-random calcFunc-shuffle calcFunc-stir1 calcFunc-stir2
calcFunc-totient math-init-random-base math-member math-prime-test
math-random-base)

 ("calccomp" calcFunc-cascent calcFunc-cdescent
calcFunc-cheight calcFunc-cwidth math-comp-ascent math-comp-descent
math-comp-height math-comp-width math-compose-expr
math-composition-to-string math-stack-value-offset-fancy
math-vector-is-string math-vector-to-string)

 ("calc-cplx" calcFunc-arg calcFunc-conj
calcFunc-im calcFunc-polar calcFunc-re calcFunc-rect math-complex
math-fix-circular math-imaginary math-imaginary-i math-normalize-polar
math-polar math-want-polar)

 ("calc-embed" calc-do-embedded
calc-do-embedded-activate calc-embedded-evaluate-expr
calc-embedded-modes-change calc-embedded-var-change
calc-embedded-preserve-modes)

 ("calc-fin" calc-to-percentage calcFunc-ddb
calcFunc-fv calcFunc-fvb calcFunc-fvl calcFunc-irr calcFunc-irrb
calcFunc-nper calcFunc-nperb calcFunc-nperl calcFunc-npv calcFunc-npvb
calcFunc-pmt calcFunc-pmtb calcFunc-pv calcFunc-pvb calcFunc-pvl
calcFunc-rate calcFunc-rateb calcFunc-ratel calcFunc-sln calcFunc-syd)

 ("calc-forms" calcFunc-badd calcFunc-bsub
calcFunc-date calcFunc-day calcFunc-dsadj calcFunc-hms
calcFunc-holiday calcFunc-hour calcFunc-incmonth calcFunc-incyear
calcFunc-intv calcFunc-julian calcFunc-makemod calcFunc-minute
calcFunc-month calcFunc-newmonth calcFunc-newweek calcFunc-newyear
calcFunc-now calcFunc-pwday calcFunc-sdev calcFunc-second
calcFunc-time calcFunc-tzconv calcFunc-tzone calcFunc-unixtime
calcFunc-weekday calcFunc-year calcFunc-yearday math-combine-intervals
math-date-parts math-date-to-dt math-div-mod math-dt-to-date
math-format-date math-from-business-day math-from-hms math-make-intv
math-make-mod math-make-sdev math-mod-intv math-normalize-hms
math-normalize-mod math-parse-date math-read-angle-brackets
math-setup-add-holidays math-setup-holidays math-setup-year-holidays
math-sort-intv math-to-business-day math-to-hms)

 ("calc-frac" calc-add-fractions
calc-div-fractions calc-mul-fractions calcFunc-fdiv calcFunc-frac
math-make-frac)

 ("calc-funcs" calc-prob-dist calcFunc-bern
calcFunc-besJ calcFunc-besY calcFunc-beta calcFunc-betaB
calcFunc-betaI calcFunc-erf calcFunc-erfc calcFunc-euler
calcFunc-gamma calcFunc-gammaG calcFunc-gammaP calcFunc-gammaQ
calcFunc-gammag calcFunc-ltpb calcFunc-ltpc calcFunc-ltpf
calcFunc-ltpn calcFunc-ltpp calcFunc-ltpt calcFunc-utpb calcFunc-utpc
calcFunc-utpf calcFunc-utpn calcFunc-utpp calcFunc-utpt
math-bernoulli-number math-gammap1-raw)

 ("calc-graph" calc-graph-show-tty)

 ("calc-incom" calc-digit-dots)

 ("calc-keypd" calc-do-keypad
calc-keypad-x-left-click calc-keypad-x-middle-click
calc-keypad-x-right-click)

 ("calc-lang" calc-set-language
math-read-big-balance math-read-big-rec)

 ("calc-map" calc-get-operator calcFunc-accum
calcFunc-afixp calcFunc-anest calcFunc-apply calcFunc-call
calcFunc-fixp calcFunc-inner calcFunc-map calcFunc-mapa calcFunc-mapc
calcFunc-mapd calcFunc-mapeq calcFunc-mapeqp calcFunc-mapeqr
calcFunc-mapr calcFunc-nest calcFunc-outer calcFunc-raccum
calcFunc-reduce calcFunc-reducea calcFunc-reducec calcFunc-reduced
calcFunc-reducer calcFunc-rreduce calcFunc-rreducea calcFunc-rreducec
calcFunc-rreduced calcFunc-rreducer math-build-call
math-calcFunc-to-var math-multi-subst math-multi-subst-rec
math-var-to-calcFunc)

 ("calc-mtx" calcFunc-det calcFunc-lud calcFunc-tr
math-col-matrix math-lud-solve math-matrix-inv-raw math-matrix-lud
math-mul-mat-vec math-mul-mats math-row-matrix)

 ("calc-math" calcFunc-alog calcFunc-arccos
calcFunc-arccosh calcFunc-arcsin calcFunc-arcsincos calcFunc-arcsinh
calcFunc-arctan calcFunc-arctan2 calcFunc-arctanh calcFunc-csc
calcFunc-csch calcFunc-cos calcFunc-cosh calcFunc-cot calcFunc-coth
calcFunc-deg calcFunc-exp calcFunc-exp10 calcFunc-expm1
calcFunc-hypot calcFunc-ilog calcFunc-isqrt calcFunc-ln calcFunc-lnp1
calcFunc-log calcFunc-log10 calcFunc-nroot calcFunc-rad calcFunc-sec
calcFunc-sech calcFunc-sin
calcFunc-sincos calcFunc-sinh calcFunc-sqr calcFunc-sqrt calcFunc-tan
calcFunc-tanh math-arccos-raw math-arcsin-raw math-arctan-raw
math-arctan2-raw math-cos-raw math-cot-raw math-csc-raw
math-exp-minus-1-raw math-exp-raw
math-from-radians math-from-radians-2 math-hypot math-infinite-dir
math-isqrt-small math-ln-raw math-nearly-equal math-nearly-equal-float
math-nearly-zerop math-nearly-zerop-float math-nth-root
math-sin-cos-raw math-sin-raw math-sqrt math-sqrt-float math-sqrt-raw
math-tan-raw math-to-radians math-to-radians-2)

 ("calc-mode" math-get-modes-vec)

 ("calc-poly" calcFunc-apart calcFunc-expand
calcFunc-expandpow calcFunc-factor calcFunc-factors calcFunc-nrat
calcFunc-pcont calcFunc-pdeg calcFunc-pdiv calcFunc-pdivide
calcFunc-pdivrem calcFunc-pgcd calcFunc-plead calcFunc-pprim
calcFunc-prem math-accum-factors math-atomic-factorp
math-div-poly-const math-div-thru math-expand-power math-expand-term
math-factor-contains math-factor-expr math-factor-expr-part
math-factor-expr-try math-factor-finish math-factor-poly-coefs
math-factor-protect math-mul-thru math-padded-polynomial
math-partial-fractions math-poly-degree math-poly-deriv-coefs
math-poly-gcd-frac-list math-poly-modulus-rec math-ratpoly-p
math-to-ratpoly math-to-ratpoly-rec)

 ("calc-prog" calc-default-formula-arglist
calc-execute-kbd-macro calc-finish-user-syntax-edit
calc-fix-token-name calc-fix-user-formula calc-read-parse-table
calc-read-parse-table-part calc-subsetp calc-write-parse-table
calc-write-parse-table-part calcFunc-constant calcFunc-eq calcFunc-geq
calcFunc-gt calcFunc-if calcFunc-in calcFunc-integer calcFunc-istrue
calcFunc-land calcFunc-leq calcFunc-lnot calcFunc-lor calcFunc-lt
calcFunc-negative calcFunc-neq calcFunc-nonvar calcFunc-real
calcFunc-refers calcFunc-rmeq calcFunc-typeof calcFunc-variable
math-body-refers-to math-break math-composite-inequalities
math-do-defmath math-handle-for math-handle-foreach
math-normalize-logical-op math-return)

 ("calc-rewr" calcFunc-match calcFunc-matches
calcFunc-matchnot calcFunc-rewrite calcFunc-vmatches
math-apply-rewrites math-compile-patterns math-compile-rewrites
math-flatten-lands math-match-patterns math-rewrite
math-rewrite-heads)

 ("calc-rules" calc-CommuteRules calc-DistribRules calc-FactorRules
calc-FitRules calc-IntegAfterRules calc-InvertRules calc-JumpRules
calc-MergeRules calc-NegateRules
calc-compile-rule-set)

 ("calc-sel" calc-auto-selection
calc-delete-selection calc-encase-atoms calc-find-assoc-parent-formula
calc-find-parent-formula calc-find-sub-formula calc-prepare-selection
calc-preserve-point calc-replace-selections calc-replace-sub-formula
calc-roll-down-with-selections calc-roll-up-with-selections
calc-sel-error)

 ("calc-stat" calc-vector-op calcFunc-agmean
calcFunc-vcorr calcFunc-vcount calcFunc-vcov calcFunc-vflat
calcFunc-vgmean calcFunc-vhmean calcFunc-vmax calcFunc-vmean
calcFunc-vmeane calcFunc-vmedian calcFunc-vmin calcFunc-vpcov
calcFunc-vprod calcFunc-vpsdev calcFunc-vpvar calcFunc-vsdev
calcFunc-vsum calcFunc-vvar math-flatten-many-vecs)

 ("calc-store" calc-read-var-name
calc-store-value calc-var-name)

 ("calc-stuff" calc-explain-why calcFunc-clean
calcFunc-pclean calcFunc-pfloat calcFunc-pfrac)

 ("calc-units" calcFunc-usimplify
math-build-units-table math-build-units-table-buffer
math-check-unit-name math-convert-temperature math-convert-units
math-extract-units math-remove-units math-simplify-units
math-single-units-in-expr-p math-to-standard-units
math-units-in-expr-p)

 ("calc-vec" calcFunc-append calcFunc-appendrev
calcFunc-arrange calcFunc-cnorm calcFunc-cons calcFunc-cross
calcFunc-ctrn calcFunc-cvec calcFunc-diag calcFunc-find
calcFunc-getdiag calcFunc-grade calcFunc-head calcFunc-histogram
calcFunc-idn calcFunc-index calcFunc-mcol calcFunc-mdims
calcFunc-mrcol calcFunc-mrow calcFunc-mrrow calcFunc-pack
calcFunc-rcons calcFunc-rdup calcFunc-rev calcFunc-rgrade
calcFunc-rhead calcFunc-rnorm calcFunc-rsort calcFunc-rsubvec
calcFunc-rtail calcFunc-sort calcFunc-subscr calcFunc-subvec
calcFunc-tail calcFunc-trn calcFunc-unpack calcFunc-unpackt
calcFunc-vcard calcFunc-vcompl calcFunc-vconcat calcFunc-vconcatrev
calcFunc-vdiff calcFunc-vec calcFunc-venum calcFunc-vexp
calcFunc-vfloor calcFunc-vint calcFunc-vlen calcFunc-vmask
calcFunc-vpack calcFunc-vspan calcFunc-vunion calcFunc-vunpack
calcFunc-vxor math-check-for-commas math-clean-set math-copy-matrix
math-dimension-error math-dot-product math-flatten-vector math-map-vec
math-map-vec-2 math-mat-col math-mimic-ident math-prepare-set
math-read-brackets math-reduce-cols math-reduce-vec math-transpose)

 ("calc-yank" calc-alg-edit calc-clean-newlines
calc-do-grab-rectangle calc-do-grab-region calc-finish-stack-edit
calc-force-refresh calc-locate-cursor-element calc-show-edit-buffer)

))

  (mapcar (function (lambda (x)
    (mapcar (function (lambda (cmd)
      (autoload cmd (car x) nil t))) (cdr x))))
    '(

 ("calc-alg" calc-alg-evaluate calc-apart calc-collect calc-expand
calc-expand-formula calc-factor calc-normalize-rat calc-poly-div
calc-poly-div-rem calc-poly-gcd calc-poly-rem calc-simplify
calc-simplify-extended calc-substitute calc-powerexpand)

 ("calcalg2" calc-alt-summation calc-derivative
calc-dump-integral-cache calc-integral calc-num-integral
calc-poly-roots calc-product calc-solve-for calc-summation
calc-tabulate calc-taylor)

 ("calcalg3" calc-curve-fit calc-find-maximum calc-find-minimum
calc-find-root calc-poly-interp)

 ("calc-arith" calc-abs calc-abssqr calc-ceiling calc-decrement
calc-floor calc-idiv calc-increment calc-mant-part calc-max calc-min
calc-round calc-scale-float calc-sign calc-trunc calc-xpon-part)

 ("calc-bin" calc-and calc-binary-radix calc-clip calc-decimal-radix
calc-diff calc-hex-radix calc-leading-zeros calc-lshift-arith
calc-lshift-binary calc-not calc-octal-radix calc-or calc-radix
calc-rotate-binary calc-rshift-arith calc-rshift-binary calc-word-size
calc-xor)

 ("calc-comb" calc-choose calc-double-factorial calc-extended-gcd
calc-factorial calc-gamma calc-gcd calc-lcm calc-moebius
calc-next-prime calc-perm calc-prev-prime calc-prime-factors
calc-prime-test calc-random calc-random-again calc-rrandom
calc-shuffle calc-totient)

 ("calc-cplx" calc-argument calc-complex-notation calc-i-notation
calc-im calc-j-notation calc-polar calc-polar-mode calc-re)

 ("calc-embed" calc-embedded-copy-formula-as-kill
calc-embedded-duplicate calc-embedded-edit calc-embedded-forget
calc-embedded-kill-formula calc-embedded-mark-formula
calc-embedded-new-formula calc-embedded-next calc-embedded-previous
calc-embedded-select calc-embedded-update-formula calc-embedded-word
calc-find-globals calc-show-plain)

 ("calc-fin" calc-convert-percent calc-fin-ddb calc-fin-fv
calc-fin-irr calc-fin-nper calc-fin-npv calc-fin-pmt calc-fin-pv
calc-fin-rate calc-fin-sln calc-fin-syd calc-percent-change)

 ("calc-forms" calc-business-days-minus calc-business-days-plus
calc-convert-time-zones calc-date calc-date-notation calc-date-part
calc-from-hms calc-hms-mode calc-hms-notation calc-inc-month
calc-julian calc-new-month calc-new-week calc-new-year calc-now
calc-time calc-time-zone calc-to-hms calc-unix-time)

 ("calc-frac" calc-fdiv calc-frac-mode calc-fraction
calc-over-notation calc-slash-notation)

 ("calc-funcs" calc-bernoulli-number calc-bessel-J calc-bessel-Y
calc-beta calc-erf calc-erfc calc-euler-number calc-inc-beta
calc-inc-gamma calc-stirling-number calc-utpb calc-utpc calc-utpf
calc-utpn calc-utpp calc-utpt)

 ("calc-graph" calc-graph-add calc-graph-add-3d calc-graph-border
calc-graph-clear calc-graph-command calc-graph-delete
calc-graph-device calc-graph-display calc-graph-fast
calc-graph-fast-3d calc-graph-geometry calc-graph-grid
calc-graph-header calc-graph-hide calc-graph-juggle calc-graph-key
calc-graph-kill calc-graph-line-style calc-graph-log-x
calc-graph-log-y calc-graph-log-z calc-graph-name
calc-graph-num-points calc-graph-output calc-graph-plot
calc-graph-point-style calc-graph-print calc-graph-quit
calc-graph-range-x calc-graph-range-y calc-graph-range-z
calc-graph-show-dumb calc-graph-title-x calc-graph-title-y
calc-graph-title-z calc-graph-view-commands calc-graph-view-trail
calc-graph-zero-x calc-graph-zero-y)

 ("calc-help" calc-a-prefix-help calc-b-prefix-help calc-c-prefix-help
calc-d-prefix-help calc-describe-function calc-describe-key
calc-describe-key-briefly calc-describe-variable calc-f-prefix-help
calc-full-help calc-g-prefix-help calc-help-prefix
calc-hyperbolic-prefix-help calc-inv-hyp-prefix-help
calc-inverse-prefix-help calc-j-prefix-help calc-k-prefix-help
calc-m-prefix-help calc-r-prefix-help calc-s-prefix-help
calc-t-prefix-help calc-u-prefix-help calc-v-prefix-help)

 ("calc-incom" calc-begin-complex calc-begin-vector calc-comma
calc-dots calc-end-complex calc-end-vector calc-semi)

 ("calc-keypd" calc-keypad-menu calc-keypad-menu-back
calc-keypad-press)

 ("calc-lang" calc-big-language calc-c-language calc-eqn-language
calc-flat-language calc-fortran-language calc-maple-language
calc-mathematica-language calc-normal-language calc-pascal-language
calc-tex-language calc-latex-language calc-unformatted-language)

 ("calc-map" calc-accumulate calc-apply calc-inner-product calc-map
calc-map-equation calc-map-stack calc-outer-product calc-reduce)

 ("calc-mtx" calc-mdet calc-mlud calc-mtrace)

 ("calc-math" calc-arccos calc-arccosh calc-arcsin calc-arcsinh
calc-arctan calc-arctan2 calc-arctanh calc-conj calc-cos calc-cosh
calc-cot calc-coth calc-csc calc-csch
calc-degrees-mode calc-exp calc-expm1 calc-hypot calc-ilog
calc-imaginary calc-isqrt calc-ln calc-lnp1 calc-log calc-log10
calc-pi calc-radians-mode calc-sec calc-sech 
calc-sin calc-sincos calc-sinh calc-sqrt
calc-tan calc-tanh calc-to-degrees calc-to-radians)

 ("calc-mode" calc-alg-simplify-mode calc-algebraic-mode
calc-always-load-extensions calc-auto-recompute calc-auto-why
calc-bin-simplify-mode calc-break-vectors calc-center-justify
calc-default-simplify-mode calc-display-raw calc-eng-notation
calc-ext-simplify-mode calc-fix-notation calc-full-trail-vectors
calc-full-vectors calc-get-modes calc-group-char calc-group-digits
calc-infinite-mode calc-left-justify calc-left-label
calc-line-breaking calc-line-numbering calc-matrix-brackets
calc-matrix-center-justify calc-matrix-left-justify calc-matrix-mode
calc-matrix-right-justify calc-mode-record-mode calc-no-simplify-mode
calc-normal-notation calc-num-simplify-mode calc-point-char
calc-right-justify calc-right-label calc-save-modes calc-sci-notation
calc-settings-file-name calc-shift-prefix calc-symbolic-mode
calc-total-algebraic-mode calc-truncate-down calc-truncate-stack
calc-truncate-up calc-units-simplify-mode calc-vector-braces
calc-vector-brackets calc-vector-commas calc-vector-parens
calc-working)

 ("calc-prog" calc-call-last-kbd-macro calc-edit-user-syntax
calc-equal-to calc-get-user-defn calc-greater-equal calc-greater-than
calc-in-set calc-kbd-break calc-kbd-else calc-kbd-else-if
calc-kbd-end-for calc-kbd-end-if calc-kbd-end-loop calc-kbd-end-repeat
calc-kbd-for calc-kbd-if calc-kbd-loop calc-kbd-pop calc-kbd-push
calc-kbd-query calc-kbd-repeat calc-kbd-report calc-less-equal
calc-less-than calc-logical-and calc-logical-if calc-logical-not
calc-logical-or calc-not-equal-to calc-pass-errors calc-remove-equal
calc-timing calc-user-define calc-user-define-composition
calc-user-define-edit calc-user-define-formula
calc-user-define-invocation calc-user-define-kbd-macro
calc-user-define-permanent calc-user-undefine)

 ("calc-rewr" calc-match calc-rewrite calc-rewrite-selection)

 ("calc-sel" calc-break-selections calc-clear-selections
calc-copy-selection calc-del-selection calc-edit-selection
calc-enable-selections calc-enter-selection calc-sel-add-both-sides
calc-sel-div-both-sides calc-sel-evaluate calc-sel-expand-formula
calc-sel-mult-both-sides calc-sel-sub-both-sides
calc-select-additional calc-select-here calc-select-here-maybe
calc-select-less calc-select-more calc-select-next calc-select-once
calc-select-once-maybe calc-select-part calc-select-previous
calc-show-selections calc-unselect)

 ("calcsel2" calc-commute-left calc-commute-right calc-sel-commute
calc-sel-distribute calc-sel-invert calc-sel-isolate
calc-sel-jump-equals calc-sel-merge calc-sel-negate calc-sel-unpack)

 ("calc-stat" calc-vector-correlation calc-vector-count
calc-vector-covariance calc-vector-geometric-mean
calc-vector-harmonic-mean calc-vector-max calc-vector-mean
calc-vector-mean-error calc-vector-median calc-vector-min
calc-vector-pop-covariance calc-vector-pop-sdev
calc-vector-pop-variance calc-vector-product calc-vector-sdev
calc-vector-sum calc-vector-variance)

 ("calc-store" calc-assign calc-copy-special-constant
calc-copy-variable calc-declare-variable
calc-edit-AlgSimpRules calc-edit-Decls calc-edit-EvalRules
calc-edit-ExtSimpRules calc-edit-FitRules calc-edit-GenCount
calc-edit-Holidays calc-edit-IntegLimit calc-edit-LineStyles
calc-edit-PlotRejects calc-edit-PointStyles calc-edit-TimeZone
calc-edit-Units calc-edit-variable calc-evalto calc-insert-variables
calc-let calc-permanent-variable calc-recall calc-recall-quick
calc-store calc-store-concat calc-store-decr calc-store-div
calc-store-exchange calc-store-incr calc-store-into
calc-store-into-quick calc-store-inv calc-store-map calc-store-minus
calc-store-neg calc-store-plus calc-store-power calc-store-quick
calc-store-times calc-subscript calc-unstore)

 ("calc-stuff" calc-clean calc-clean-num calc-flush-caches
calc-less-recursion-depth calc-more-recursion-depth calc-num-prefix
calc-version calc-why)

 ("calc-trail" calc-trail-backward calc-trail-first calc-trail-forward
calc-trail-in calc-trail-isearch-backward calc-trail-isearch-forward
calc-trail-kill calc-trail-last calc-trail-marker calc-trail-next
calc-trail-out calc-trail-previous calc-trail-scroll-left
calc-trail-scroll-right calc-trail-yank)

 ("calc-undo" calc-last-args calc-redo calc-undo)

 ("calc-units" calc-autorange-units calc-base-units
calc-convert-temperature calc-convert-units calc-define-unit
calc-enter-units-table calc-explain-units calc-extract-units
calc-get-unit-definition calc-permanent-units calc-quick-units
calc-remove-units calc-simplify-units calc-undefine-unit
calc-view-units-table)

 ("calc-vec" calc-arrange-vector calc-build-vector calc-cnorm
calc-conj-transpose calc-cons calc-cross calc-diag
calc-display-strings calc-expand-vector calc-grade calc-head
calc-histogram calc-ident calc-index calc-mask-vector calc-mcol
calc-mrow calc-pack calc-pack-bits calc-remove-duplicates
calc-reverse-vector calc-rnorm calc-set-cardinality
calc-set-complement calc-set-difference calc-set-enumerate
calc-set-floor calc-set-intersect calc-set-span calc-set-union
calc-set-xor calc-sort calc-subvector calc-tail calc-transpose
calc-unpack calc-unpack-bits calc-vector-find calc-vlength)

 ("calc-yank" calc-copy-as-kill calc-copy-region-as-kill
calc-copy-to-buffer calc-edit calc-edit-cancel calc-edit-mode
calc-kill calc-kill-region calc-yank))))

(defun calc-init-prefixes ()
  (if calc-shift-prefix
      (progn
	(define-key calc-mode-map "A" (lookup-key calc-mode-map "a"))
	(define-key calc-mode-map "B" (lookup-key calc-mode-map "b"))
	(define-key calc-mode-map "C" (lookup-key calc-mode-map "c"))
	(define-key calc-mode-map "D" (lookup-key calc-mode-map "d"))
	(define-key calc-mode-map "F" (lookup-key calc-mode-map "f"))
	(define-key calc-mode-map "G" (lookup-key calc-mode-map "g"))
	(define-key calc-mode-map "J" (lookup-key calc-mode-map "j"))
	(define-key calc-mode-map "K" (lookup-key calc-mode-map "k"))
	(define-key calc-mode-map "M" (lookup-key calc-mode-map "m"))
	(define-key calc-mode-map "S" (lookup-key calc-mode-map "s"))
	(define-key calc-mode-map "T" (lookup-key calc-mode-map "t"))
	(define-key calc-mode-map "U" (lookup-key calc-mode-map "u")))
    (define-key calc-mode-map "A" 'calc-abs)
    (define-key calc-mode-map "B" 'calc-log)
    (define-key calc-mode-map "C" 'calc-cos)
    (define-key calc-mode-map "D" 'calc-redo)
    (define-key calc-mode-map "F" 'calc-floor)
    (define-key calc-mode-map "G" 'calc-argument)
    (define-key calc-mode-map "J" 'calc-conj)
    (define-key calc-mode-map "K" 'calc-keep-args)
    (define-key calc-mode-map "M" 'calc-more-recursion-depth)
    (define-key calc-mode-map "S" 'calc-sin)
    (define-key calc-mode-map "T" 'calc-tan)
    (define-key calc-mode-map "U" 'calc-undo)))

(calc-init-extensions)




;;;; Miscellaneous.

;; calc-command-flags is declared in calc.el
(defvar calc-command-flags)

(defun calc-clear-command-flag (f)
  (setq calc-command-flags (delq f calc-command-flags)))


(defun calc-record-message (tag &rest args)
  (let ((msg (apply 'format args)))
    (message "%s" msg)
    (calc-record msg tag))
  (calc-clear-command-flag 'clear-message))


(defun calc-normalize-fancy (val)
  (let ((simp (if (consp calc-simplify-mode)
		  (car calc-simplify-mode)
		calc-simplify-mode)))
    (cond ((eq simp 'binary)
	   (let ((s (math-normalize val)))
	     (if (math-realp s)
		 (math-clip (math-round s))
	       s)))
	  ((eq simp 'alg)
	   (math-simplify val))
	  ((eq simp 'ext)
	   (math-simplify-extended val))
	  ((eq simp 'units)
	   (math-simplify-units val))
	  (t  ; nil, none, num
	   (math-normalize val)))))


(defvar calc-help-map nil)

(if calc-help-map
    nil
  (setq calc-help-map (make-keymap))
  (define-key calc-help-map "b" 'calc-describe-bindings)
  (define-key calc-help-map "c" 'calc-describe-key-briefly)
  (define-key calc-help-map "f" 'calc-describe-function)
  (define-key calc-help-map "h" 'calc-full-help)
  (define-key calc-help-map "i" 'calc-info)
  (define-key calc-help-map "k" 'calc-describe-key)
  (define-key calc-help-map "n" 'calc-view-news)
  (define-key calc-help-map "s" 'calc-info-summary)
  (define-key calc-help-map "t" 'calc-tutorial)
  (define-key calc-help-map "v" 'calc-describe-variable)
  (define-key calc-help-map "\C-c" 'calc-describe-copying)
  (define-key calc-help-map "\C-d" 'calc-describe-distribution)
  (define-key calc-help-map "\C-n" 'calc-view-news)
  (define-key calc-help-map "\C-w" 'calc-describe-no-warranty)
  (define-key calc-help-map "?" 'calc-help-for-help)
  (define-key calc-help-map "\C-h" 'calc-help-for-help))

(defvar calc-prefix-help-phase 0)
(defun calc-do-prefix-help (msgs group key)
  (if calc-full-help-flag
      (list msgs group key)
    (if (cdr msgs)
	(progn
	  (setq calc-prefix-help-phase
		(if (eq this-command last-command)
		    (% (1+ calc-prefix-help-phase) (1+ (length msgs)))
		  0))
	  (let ((msg (nth calc-prefix-help-phase msgs)))
	    (message "%s" (if msg
			      (concat group ": " msg ":"
				      (make-string
				       (- (apply 'max (mapcar 'length msgs))
					  (length msg)) 32)
				      "  [MORE]"
				      (if key
					  (concat "  " (char-to-string key)
						  "-")
					""))
			    (if key (format "%c-" key) "")))))
      (setq calc-prefix-help-phase 0)
      (if key
	  (if msgs
	      (message "%s: %s: %c-" group (car msgs) key)
	    (message "%s: (none)  %c-" group key))
	(message "%s: %s" group (car msgs))))
    (and key (calc-unread-command key))))

;;;; Commands.


;;; General.

(defun calc-reset (arg)
  (interactive "P")
  (setq arg (if arg (prefix-numeric-value arg) nil))
  (cond
   ((and
     calc-embedded-info
     (equal (aref calc-embedded-info 0) (current-buffer))
     (<= (point) (aref calc-embedded-info 5))
     (>= (point) (aref calc-embedded-info 4)))
    (let ((cbuf (aref calc-embedded-info 1))
          (calc-embedded-quiet t))
      (save-window-excursion
        (calc-embedded nil)
        (set-buffer cbuf)
        (calc-reset arg))
      (calc-embedded nil)))
   ((eq major-mode 'calc-mode)
    (save-excursion
      (unless (and arg (> (abs arg) 0))
        (setq calc-stack nil))
      (setq calc-undo-list nil
            calc-redo-list nil)
      (let (calc-stack calc-user-parse-tables calc-standard-date-formats
                       calc-invocation-macro)
        (mapcar (function (lambda (v) (set v nil))) calc-local-var-list)
        (if (and arg (<= arg 0))
            (calc-mode-var-list-restore-default-values)
          (calc-mode-var-list-restore-saved-values)))
      (calc-set-language nil nil t)
      (calc-mode)
      (calc-flush-caches t)
      (run-hooks 'calc-reset-hook))
    (calc-wrapper
     (let ((win (get-buffer-window (current-buffer))))
       (calc-realign 0)
       ;; Adjust the window height if the window is visible, but doesn't
       ;; take up the whole height of the frame.
       (if (and
            win
            (< (window-height win) (1- (frame-height))))
           (let ((height (- (window-height win) 2)))
             (set-window-point win (point))
             (or (= height calc-window-height)
                 (let ((swin (selected-window)))
                   (select-window win)
                   (enlarge-window (- calc-window-height height))
                   (select-window swin)))))))
    (message "(Calculator reset)"))
   (t
    (message "(Not inside a Calc buffer)"))))

;; What a pain; scroll-left behaves differently when called non-interactively.
(defun calc-scroll-left (n)
  (interactive "P")
  (setq prefix-arg (or n (/ (window-width) 2)))
  (call-interactively #'scroll-left))

(defun calc-scroll-right (n)
  (interactive "P")
  (setq prefix-arg (or n (/ (window-width) 2)))
  (call-interactively #'scroll-right))

(defun calc-scroll-up (n)
  (interactive "P")
  (condition-case err
      (scroll-up (or n (/ (window-height) 2)))
    (error nil))
  (if (pos-visible-in-window-p (max 1 (- (point-max) 2)))
      (if (eq major-mode 'calc-mode)
	  (calc-realign)
	(goto-char (point-max))
	(set-window-start (selected-window)
			  (save-excursion
			    (forward-line (- (1- (window-height))))
			    (point)))
	(forward-line -1))))

(defun calc-scroll-down (n)
  (interactive "P")
  (or (pos-visible-in-window-p 1)
      (scroll-down (or n (/ (window-height) 2)))))


(defun calc-precision (n)
  (interactive "NPrecision: ")
  (calc-wrapper
   (if (< (prefix-numeric-value n) 3)
       (error "Precision must be at least 3 digits")
     (calc-change-mode 'calc-internal-prec (prefix-numeric-value n)
		       (and (memq (car calc-float-format) '(float sci eng))
			    (< (nth 1 calc-float-format)
				(if (= calc-number-radix 10) 0 1))))
     (calc-record calc-internal-prec "prec"))
   (message "Floating-point precision is %d digits" calc-internal-prec)))


(defun calc-inverse (&optional n)
  (interactive "P")
  (let* ((hyp-flag (if (or
                        (eq major-mode 'calc-keypad-mode)
                        (eq major-mode 'calc-trail-mode))
                       (with-current-buffer calc-main-buffer
                         calc-hyperbolic-flag)
                     calc-hyperbolic-flag))
         (msg (if hyp-flag 
                 "Inverse Hyperbolic..."
               "Inverse...")))
    (calc-fancy-prefix 'calc-inverse-flag msg n)))

(defconst calc-fancy-prefix-map
  (let ((map (make-sparse-keymap)))
    (define-key map [t] 'calc-fancy-prefix-other-key)
    (define-key map (vector meta-prefix-char t) 'calc-fancy-prefix-other-key)
    (define-key map [switch-frame] nil)
    (define-key map [?\C-u] 'universal-argument)
    (define-key map [?0] 'digit-argument)
    (define-key map [?1] 'digit-argument)
    (define-key map [?2] 'digit-argument)
    (define-key map [?3] 'digit-argument)
    (define-key map [?4] 'digit-argument)
    (define-key map [?5] 'digit-argument)
    (define-key map [?6] 'digit-argument)
    (define-key map [?7] 'digit-argument)
    (define-key map [?8] 'digit-argument)
    (define-key map [?9] 'digit-argument)
    map)
  "Keymap used while processing calc-fancy-prefix.")

(defvar calc-is-keypad-press nil)
(defun calc-fancy-prefix (flag msg n)
  (let (prefix)
    (calc-wrapper
     (calc-set-command-flag 'keep-flags)
     (calc-set-command-flag 'no-align)
     (setq prefix (set flag (not (symbol-value flag)))
	   prefix-arg n)
     (message (if prefix msg "")))
    (and prefix
	 (not calc-is-keypad-press)
	 (if (boundp 'overriding-terminal-local-map)
	     (setq overriding-terminal-local-map calc-fancy-prefix-map)
	   (let ((event (calc-read-key t)))
	     (if (eq (setq last-command-char (car event)) ?\C-u)
		 (universal-argument)
	       (if (or (not (integerp last-command-char))
		       (and (>= last-command-char 0) (< last-command-char ? )
			    (not (memq last-command-char '(?\e)))))
		   (calc-wrapper))  ; clear flags if not a Calc command.
               (setq last-command-event (cdr event))
	       (if (or (not (integerp last-command-char))
		       (eq last-command-char ?-))
		   (calc-unread-command)
		 (digit-argument n))))))))

(defun calc-fancy-prefix-other-key (arg)
  (interactive "P")
  (if (and
       (not (eq last-command-char 'tab))
       (not (eq last-command-char 'M-tab))
       (or (not (integerp last-command-char))
           (and (>= last-command-char 0) (< last-command-char ? )
                (not (eq last-command-char meta-prefix-char)))))
     (calc-wrapper))  ; clear flags if not a Calc command.
  (setq prefix-arg arg)
  (calc-unread-command)
  (setq overriding-terminal-local-map nil))

(defun calc-invert-func ()
  (save-excursion
    (calc-select-buffer)
    (setq calc-inverse-flag (not (calc-is-inverse))
	  calc-hyperbolic-flag (calc-is-hyperbolic)
	  current-prefix-arg nil)))

(defun calc-is-inverse ()
  calc-inverse-flag)

(defun calc-hyperbolic (&optional n)
  (interactive "P")
  (let* ((inv-flag (if (or
                        (eq major-mode 'calc-keypad-mode)
                        (eq major-mode 'calc-trail-mode))
                       (with-current-buffer calc-main-buffer
                         calc-inverse-flag)
                     calc-inverse-flag))
         (msg (if inv-flag 
                  "Inverse Hyperbolic..."
                "Hyperbolic...")))
    (calc-fancy-prefix 'calc-hyperbolic-flag msg n)))

(defun calc-hyperbolic-func ()
  (save-excursion
    (calc-select-buffer)
    (setq calc-inverse-flag (calc-is-inverse)
	  calc-hyperbolic-flag (not (calc-is-hyperbolic))
	  current-prefix-arg nil)))

(defun calc-is-hyperbolic ()
  calc-hyperbolic-flag)

(defun calc-keep-args (&optional n)
  (interactive "P")
  (calc-fancy-prefix 'calc-keep-args-flag "Keep args..." n))


(defun calc-change-mode (var value &optional refresh option)
  (if option
      (setq value (if value
		      (> (prefix-numeric-value value) 0)
		    (not (symbol-value var)))))
  (or (consp var) (setq var (list var) value (list value)))
  (if calc-inverse-flag
      (let ((old nil))
	(or refresh (error "Not a display-mode command"))
	(calc-check-stack 1)
	(unwind-protect
	    (let ((v var))
	      (while v
		(setq old (cons (symbol-value (car v)) old))
		(set (car v) (car value))
		(setq v (cdr v)
		      value (cdr value)))
	      (calc-refresh-top 1)
	      (calc-refresh-evaltos)
	      (symbol-value (car var)))
	  (let ((v var))
	    (setq old (nreverse old))
	    (while v
	      (set (car v) (car old))
	      (setq v (cdr v)
		    old (cdr old)))
	    (if (eq (car var) 'calc-language)
		(calc-set-language calc-language calc-language-option t)))))
    (let ((chg nil)
	  (v var))
      (while v
	(or (equal (symbol-value (car v)) (car value))
	    (progn
	      (set (car v) (car value))
	      (if (eq (car v) 'calc-float-format)
		  (setq calc-full-float-format
			(list (if (eq (car (car value)) 'fix)
				  'float
				(car (car value)))
			      0)))
	      (setq chg t)))
	(setq v (cdr v)
	      value (cdr value)))
      (if chg
	  (progn
	    (or (and refresh (calc-do-refresh))
		(calc-refresh-evaltos))
	    (and (eq calc-mode-save-mode 'save)
		 (not (equal var '(calc-mode-save-mode)))
		 (calc-save-modes))))
      (if calc-embedded-info (calc-embedded-modes-change var))
      (symbol-value (car var)))))

(defun calc-toggle-banner ()
  "Toggle display of the friendly greeting calc normally shows above the stack."
  (interactive)
  (setq calc-show-banner (not calc-show-banner))
  (calc-refresh))

(defun calc-refresh-top (n)
  (interactive "p")
  (calc-wrapper
   (cond ((< n 0)
	  (setq n (- n))
	  (let ((entry (calc-top n 'entry))
		(calc-undo-list nil) (calc-redo-list nil))
	    (calc-pop-stack 1 n t)
	    (calc-push-list (list (car entry)) n (list (nth 2 entry)))))
	 ((= n 0)
	  (calc-refresh))
	 (t
	  (let ((entries (calc-top-list n 1 'entry))
		(calc-undo-list nil) (calc-redo-list nil))
	    (calc-pop-stack n 1 t)
	    (calc-push-list (mapcar 'car entries)
			    1
			    (mapcar (function (lambda (x) (nth 2 x)))
				    entries)))))))

(defvar calc-refreshing-evaltos nil)
(defvar calc-no-refresh-evaltos nil)
(defun calc-refresh-evaltos (&optional which-var)
  (and calc-any-evaltos calc-auto-recompute (not calc-no-refresh-evaltos)
       (let ((calc-refreshing-evaltos t)
	     (num (calc-stack-size))
	     (calc-undo-list nil) (calc-redo-list nil)
	     value new-val)
	 (while (> num 0)
	   (setq value (calc-top num 'entry))
	   (if (and (not (nth 2 value))
		    (setq value (car value))
		    (or (eq (car-safe value) 'calcFunc-evalto)
			(and (eq (car-safe value) 'vec)
			     (eq (car-safe (nth 1 value)) 'calcFunc-evalto))))
	       (progn
		 (setq new-val (math-normalize value))
		 (or (equal new-val value)
		     (progn
		       (calc-push-list (list new-val) num)
		       (calc-pop-stack 1 (1+ num) t)))))
	   (setq num (1- num)))))
  (and calc-embedded-active which-var
       (calc-embedded-var-change which-var)))

(defun calc-push (&rest vals)
  (calc-push-list vals))

(defun calc-pop-push (n &rest vals)
  (calc-pop-push-list n vals))

(defun calc-pop-push-record (n prefix &rest vals)
  (calc-pop-push-record-list n prefix vals))


(defun calc-evaluate (n)
  (interactive "p")
  (calc-slow-wrapper
   (if (= n 0)
       (setq n (calc-stack-size)))
   (calc-with-default-simplification
    (if (< n 0)
	(calc-pop-push-record-list 1 "eval"
				   (math-evaluate-expr (calc-top (- n)))
				   (- n))
      (calc-pop-push-record-list n "eval" (mapcar 'math-evaluate-expr
						  (calc-top-list n)))))
   (calc-handle-whys)))


(defun calc-eval-num (n)
  (interactive "P")
  (calc-slow-wrapper
   (let* ((nn (prefix-numeric-value n))
	  (calc-internal-prec (cond ((>= nn 3) nn)
				    ((< nn 0) (max (+ calc-internal-prec nn)
						   3))
				    (t calc-internal-prec)))
	  (calc-symbolic-mode nil))
     (calc-with-default-simplification
      (calc-pop-push-record 1 "num" (math-evaluate-expr (calc-top 1)))))
   (calc-handle-whys)))


(defun calc-execute-extended-command (n)
  (interactive "P")
  (let* ((prompt (concat (calc-num-prefix-name n) "M-x "))
	 (cmd (intern (completing-read prompt obarray 'commandp t "calc-"))))
    (setq prefix-arg n)
    (command-execute cmd)))


(defun calc-realign (&optional num)
  (interactive "P")
  (if (and num (eq major-mode 'calc-mode))
      (progn
	(calc-check-stack num)
	(calc-cursor-stack-index num)
	(and calc-line-numbering
	     (forward-char 4)))
    (if (and calc-embedded-info
	     (eq (current-buffer) (aref calc-embedded-info 0)))
	(progn
	  (goto-char (aref calc-embedded-info 2))
	  (if (save-excursion (set-buffer (aref calc-embedded-info 1))
			      calc-show-plain)
	      (forward-line 1)))
      (calc-wrapper
       (if (get-buffer-window (current-buffer))
	   (set-window-hscroll (get-buffer-window (current-buffer)) 0))))))

(defvar math-cache-list nil)

(defun calc-var-value (v)
  (and (symbolp v)
       (boundp v)
       (symbol-value v)
       (if (symbolp (symbol-value v))
	   (set v (funcall (symbol-value v)))
	 (if (stringp (symbol-value v))
	     (let ((val (math-read-expr (symbol-value v))))
	       (if (eq (car-safe val) 'error)
		   (error "Bad format in variable contents: %s" (nth 2 val))
		 (set v val)))
	   (symbol-value v)))))

;;; In the following table, ( OP LOPS ROPS ) means that if an OP
;;; term appears as the first argument to any LOPS term, or as the
;;; second argument to any ROPS term, then they should be treated
;;; as one large term for purposes of associative selection.
(defconst calc-assoc-ops '( ( + ( + - ) ( + ) )
			    ( - ( + - ) ( + ) )
			    ( * ( * )   ( * ) )
			    ( / ( / )   (   ) )
			    ( | ( | )   ( | ) )
			    ( calcFunc-land ( calcFunc-land )
					    ( calcFunc-land ) )
			    ( calcFunc-lor ( calcFunc-lor )
					   ( calcFunc-lor ) ) ))


(defvar var-CommuteRules 'calc-CommuteRules)
(defvar var-JumpRules    'calc-JumpRules)
(defvar var-DistribRules 'calc-DistribRules)
(defvar var-MergeRules   'calc-MergeRules)
(defvar var-NegateRules  'calc-NegateRules)
(defvar var-InvertRules  'calc-InvertRules)


(defconst calc-tweak-eqn-table '( ( calcFunc-eq  calcFunc-eq  calcFunc-neq )
				  ( calcFunc-neq calcFunc-neq calcFunc-eq  )
				  ( calcFunc-lt  calcFunc-gt  calcFunc-geq )
				  ( calcFunc-gt  calcFunc-lt  calcFunc-leq )
				  ( calcFunc-leq calcFunc-geq calcFunc-gt  )
				  ( calcFunc-geq calcFunc-leq calcFunc-lt  ) ))




(defun calc-float (arg)
  (interactive "P")
  (calc-slow-wrapper
   (calc-unary-op "flt"
		  (if (calc-is-hyperbolic) 'calcFunc-float 'calcFunc-pfloat)
		  arg)))


(defvar calc-gnuplot-process nil)
(defvar calc-gnuplot-input)
(defvar calc-gnuplot-buffer)

(defun calc-gnuplot-alive ()
  (and calc-gnuplot-process
       calc-gnuplot-buffer
       (buffer-name calc-gnuplot-buffer)
       calc-gnuplot-input
       (buffer-name calc-gnuplot-input)
       (memq (process-status calc-gnuplot-process) '(run stop))))





(defun calc-load-everything ()
  (interactive)
  (require 'calc-aent)
  (require 'calc-alg)
  (require 'calc-arith)
  (require 'calc-bin)
  (require 'calc-comb)
  (require 'calc-cplx)
  (require 'calc-embed)
  (require 'calc-fin)
  (require 'calc-forms)
  (require 'calc-frac)
  (require 'calc-funcs)
  (require 'calc-graph)
  (require 'calc-help)
  (require 'calc-incom)
  (require 'calc-keypd)
  (require 'calc-lang)
  (require 'calc-macs)
  (require 'calc-map)
  (require 'calc-math)
  (require 'calc-misc)
  (require 'calc-mode)
  (require 'calc-mtx)
  (require 'calc-poly)
  (require 'calc-prog)
  (require 'calc-rewr)
  (require 'calc-rules)
  (require 'calc-sel)
  (require 'calc-stat)
  (require 'calc-store)
  (require 'calc-stuff)
  (require 'calc-trail)
  (require 'calc-undo)
  (require 'calc-units)
  (require 'calc-vec)
  (require 'calc-yank)
  (require 'calcalg2)
  (require 'calcalg3)
  (require 'calccomp)
  (require 'calcsel2)

  (message "All parts of Calc are now loaded"))


;;; Vector commands.

(defun calc-concat (arg)
  (interactive "P")
  (calc-wrapper
   (if (calc-is-inverse)
       (if (calc-is-hyperbolic)
	   (calc-enter-result 2 "apnd" (list 'calcFunc-append
					  (calc-top 1) (calc-top 2)))
	 (calc-enter-result 2 "|" (list 'calcFunc-vconcat
					(calc-top 1) (calc-top 2))))
     (if (calc-is-hyperbolic)
	 (calc-binary-op "apnd" 'calcFunc-append arg '(vec))
       (calc-binary-op "|" 'calcFunc-vconcat arg '(vec) nil '|)))))

(defun calc-append (arg)
  (interactive "P")
  (calc-hyperbolic-func)
  (calc-concat arg))


(defconst calc-arg-values '( ( var ArgA var-ArgA ) ( var ArgB var-ArgB )
			     ( var ArgC var-ArgC ) ( var ArgD var-ArgD )
			     ( var ArgE var-ArgE ) ( var ArgF var-ArgF )
			     ( var ArgG var-ArgG ) ( var ArgH var-ArgH )
			     ( var ArgI var-ArgI ) ( var ArgJ var-ArgJ )
))

(defun calc-invent-args (n)
  (nreverse (nthcdr (- (length calc-arg-values) n) (reverse calc-arg-values))))




;;; User menu.

(defun calc-user-key-map ()
  (if calc-emacs-type-lucid
      (error "User-defined keys are not supported in Lucid Emacs"))
  (let ((res (cdr (lookup-key calc-mode-map "z"))))
    (if (eq (car (car res)) 27)
	(cdr res)
      res)))

(defvar calc-z-prefix-buf nil)
(defvar calc-z-prefix-msgs nil)

(defun calc-z-prefix-help ()
  (interactive)
  (let* ((calc-z-prefix-msgs nil)
	 (calc-z-prefix-buf "")
	 (kmap (sort (copy-sequence (calc-user-key-map))
		     (function (lambda (x y) (< (car x) (car y))))))
	 (flags (apply 'logior
		       (mapcar (function
				(lambda (k)
				  (calc-user-function-classify (car k))))
			       kmap))))
    (if (= (logand flags 8) 0)
	(calc-user-function-list kmap 7)
      (calc-user-function-list kmap 1)
      (setq calc-z-prefix-msgs (cons calc-z-prefix-buf calc-z-prefix-msgs)
	    calc-z-prefix-buf "")
      (calc-user-function-list kmap 6))
    (if (/= flags 0)
	(setq calc-z-prefix-msgs (cons calc-z-prefix-buf calc-z-prefix-msgs)))
    (calc-do-prefix-help (nreverse calc-z-prefix-msgs) "user" ?z)))

(defun calc-user-function-classify (key)
  (cond ((/= key (downcase key))    ; upper-case
	 (if (assq (downcase key) (calc-user-key-map)) 9 1))
	((/= key (upcase key)) 2)   ; lower-case
	((= key ??) 0)
	(t 4)))   ; other

(defun calc-user-function-list (map flags)
  (and map
       (let* ((key (car (car map)))
	      (kind (calc-user-function-classify key))
	      (func (cdr (car map))))
	 (if (or (= (logand kind flags) 0)
		 (not (symbolp func)))
	     ()
	   (let* ((name (symbol-name func))
		  (name (if (string-match "\\`calc-" name)
			    (substring name 5) name))
		  (pos (string-match (char-to-string key) name))
		  (desc
		   (if (symbolp func)
		       (if (= (logand kind 3) 0)
			   (format "`%c' = %s" key name)
			 (if pos
			     (format "%s%c%s"
				     (downcase (substring name 0 pos))
				     (upcase key)
				     (downcase (substring name (1+ pos))))
			   (format "%c = %s"
				   (upcase key)
				   (downcase name))))
		     (char-to-string (upcase key)))))
	     (if (= (length calc-z-prefix-buf) 0)
		 (setq calc-z-prefix-buf (concat (if (= flags 1) "SHIFT + " "")
				   desc))
	       (if (> (+ (length calc-z-prefix-buf) (length desc)) 58)
		   (setq calc-z-prefix-msgs 
                         (cons calc-z-prefix-buf calc-z-prefix-msgs)
			 calc-z-prefix-buf (concat (if (= flags 1) "SHIFT + " "")
				     desc))
		 (setq calc-z-prefix-buf (concat calc-z-prefix-buf ", " desc))))))
	 (calc-user-function-list (cdr map) flags))))



(defun calc-shift-Z-prefix-help ()
  (interactive)
  (calc-do-prefix-help
   '("Define, Undefine, Formula, Kbd-macro, Edit, Get-defn"
     "Composition, Syntax; Invocation; Permanent; Timing"
     "kbd-macros: [ (if), : (else), | (else-if), ] (end-if)"
     "kbd-macros: < > (repeat), ( ) (for), { } (loop)"
     "kbd-macros: / (break)"
     "kbd-macros: ` (save), ' (restore)")
   "user" ?Z))


;;;; Caches.

(defmacro math-defcache (name init form)
  (let ((cache-prec (intern (concat (symbol-name name) "-cache-prec")))
	(cache-val (intern (concat (symbol-name name) "-cache")))
	(last-prec (intern (concat (symbol-name name) "-last-prec")))
	(last-val (intern (concat (symbol-name name) "-last"))))
    (list 'progn
	  (list 'defvar cache-prec (if init (math-numdigs (nth 1 init)) -100))
	  (list 'defvar cache-val (list 'quote init))
	  (list 'defvar last-prec -100)
	  (list 'defvar last-val nil)
	  (list 'setq 'math-cache-list
		(list 'cons
		      (list 'quote cache-prec)
		      (list 'cons
			    (list 'quote last-prec)
			    'math-cache-list)))
	  (list 'defun
		name ()
		(list 'or
		      (list '= last-prec 'calc-internal-prec)
		      (list 'setq
			    last-val
			    (list 'math-normalize
				  (list 'progn
					(list 'or
					      (list '>= cache-prec
						    'calc-internal-prec)
					      (list 'setq
						    cache-val
						    (list 'let
							  '((calc-internal-prec
							     (+ calc-internal-prec
								4)))
							  form)
						    cache-prec
						    '(+ calc-internal-prec 2)))
					cache-val))
			    last-prec 'calc-internal-prec))
		last-val))))
(put 'math-defcache 'lisp-indent-hook 2)

;;; Betcha didn't know that pi = 16 atan(1/5) - 4 atan(1/239).   [F] [Public]
(math-defcache math-pi (float (bigpos 463 238 793 589 653 592 141 3) -21)
  (math-add-float (math-mul-float '(float 16 0)
				  (math-arctan-raw '(float 2 -1)))
		  (math-mul-float '(float -4 0)
				  (math-arctan-raw
				   (math-float '(frac 1 239))))))

(math-defcache math-two-pi nil
  (math-mul-float (math-pi) '(float 2 0)))

(math-defcache math-pi-over-2 nil
  (math-mul-float (math-pi) '(float 5 -1)))

(math-defcache math-pi-over-4 nil
  (math-mul-float (math-pi) '(float 25 -2)))

(math-defcache math-pi-over-180 nil
  (math-div-float (math-pi) '(float 18 1)))

(math-defcache math-sqrt-pi nil
  (math-sqrt-float (math-pi)))

(math-defcache math-sqrt-2 nil
  (math-sqrt-float '(float 2 0)))

(math-defcache math-sqrt-12 nil
  (math-sqrt-float '(float 12 0)))

(math-defcache math-sqrt-two-pi nil
  (math-sqrt-float (math-two-pi)))

(math-defcache math-sqrt-e (float (bigpos 849 146 128 700 270 721 648 1) -21)
  (math-add-float '(float 1 0) (math-exp-minus-1-raw '(float 5 -1))))

(math-defcache math-e nil
  (math-pow (math-sqrt-e) 2))

(math-defcache math-phi nil
  (math-mul-float (math-add-float (math-sqrt-raw '(float 5 0)) '(float 1 0))
		  '(float 5 -1)))

(math-defcache math-gamma-const nil
  '(float (bigpos 495 467 917 632 470 369 709 646 776 267 677 848 348 672
		  057 988 235 399 359 593 421 310 024 824 900 120 065 606
		  328 015 649 156 772 5) -100))

(defun math-half-circle (symb)
  (if (eq calc-angle-mode 'rad)
      (if symb
	  '(var pi var-pi)
	(math-pi))
    180))

(defun math-full-circle (symb)
  (math-mul 2 (math-half-circle symb)))

(defun math-quarter-circle (symb)
  (math-div (math-half-circle symb) 2))

(defvar math-expand-formulas nil)

;;;; Miscellaneous math routines.

;;; True if A is an odd integer.  [P R R] [Public]
(defun math-oddp (a)
  (if (consp a)
      (and (memq (car a) '(bigpos bigneg))
	   (= (% (nth 1 a) 2) 1))
    (/= (% a 2) 0)))

;;; True if A is a small or big integer.  [P x] [Public]
(defun math-integerp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg))))

;;; True if A is (numerically) a non-negative integer.  [P N] [Public]
(defun math-natnump (a)
  (or (natnump a)
      (eq (car-safe a) 'bigpos)))

;;; True if A is a rational (or integer).  [P x] [Public]
(defun math-ratp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac))))

;;; True if A is a real (or rational).  [P x] [Public]
(defun math-realp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float))))

;;; True if A is a real or HMS form.  [P x] [Public]
(defun math-anglep (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float hms))))

;;; True if A is a number of any kind.  [P x] [Public]
(defun math-numberp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float cplx polar))))

;;; True if A is a complex number or angle.  [P x] [Public]
(defun math-scalarp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float cplx polar hms))))

;;; True if A is a vector.  [P x] [Public]
(defun math-vectorp (a)
  (eq (car-safe a) 'vec))

;;; True if A is any vector or scalar data object.  [P x]
(defun math-objvecp (a)    ;  [Public]
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float cplx polar
				  hms date sdev intv mod vec incomplete))))

;;; True if A is an object not composed of sub-formulas .  [P x] [Public]
(defun math-primp (a)
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float cplx polar
				  hms date mod var))))

;;; True if A is numerically (but not literally) an integer.  [P x] [Public]
(defun math-messy-integerp (a)
  (cond
   ((eq (car-safe a) 'float) (>= (nth 2 a) 0))
   ((eq (car-safe a) 'frac) (Math-integerp (math-normalize a)))))

;;; True if A is numerically an integer.  [P x] [Public]
(defun math-num-integerp (a)
  (or (Math-integerp a)
      (Math-messy-integerp a)))

;;; True if A is (numerically) a non-negative integer.  [P N] [Public]
(defun math-num-natnump (a)
  (or (natnump a)
      (eq (car-safe a) 'bigpos)
      (and (eq (car-safe a) 'float)
	   (Math-natnump (nth 1 a))
	   (>= (nth 2 a) 0))))

;;; True if A is an integer or will evaluate to an integer.  [P x] [Public]
(defun math-provably-integerp (a)
  (or (Math-integerp a)
      (and (memq (car-safe a) '(calcFunc-trunc
				calcFunc-round
				calcFunc-rounde
				calcFunc-roundu
				calcFunc-floor
				calcFunc-ceil))
	   (= (length a) 2))))

;;; True if A is a real or will evaluate to a real.  [P x] [Public]
(defun math-provably-realp (a)
  (or (Math-realp a)
      (math-provably-integer a)
      (memq (car-safe a) '(abs arg))))

;;; True if A is a non-real, complex number.  [P x] [Public]
(defun math-complexp (a)
  (memq (car-safe a) '(cplx polar)))

;;; True if A is a non-real, rectangular complex number.  [P x] [Public]
(defun math-rect-complexp (a)
  (eq (car-safe a) 'cplx))

;;; True if A is a non-real, polar complex number.  [P x] [Public]
(defun math-polar-complexp (a)
  (eq (car-safe a) 'polar))

;;; True if A is a matrix.  [P x] [Public]
(defun math-matrixp (a)
  (and (Math-vectorp a)
       (Math-vectorp (nth 1 a))
       (cdr (nth 1 a))
       (let ((len (length (nth 1 a))))
	 (setq a (cdr a))
	 (while (and (setq a (cdr a))
		     (Math-vectorp (car a))
		     (= (length (car a)) len)))
	 (null a))))

(defun math-matrixp-step (a len)   ; [P L]
  (or (null a)
      (and (Math-vectorp (car a))
	   (= (length (car a)) len)
	   (math-matrixp-step (cdr a) len))))

;;; True if A is a square matrix.  [P V] [Public]
(defun math-square-matrixp (a)
  (let ((dims (math-mat-dimens a)))
    (and (cdr dims)
	 (= (car dims) (nth 1 dims)))))

;;; True if MAT is an identity matrix.
(defun math-identity-matrix-p (mat &optional mul)
  (if (math-square-matrixp mat)
      (let ((a (if mul
                   (nth 1 (nth 1 mat))
                 1))
            (n (1- (length mat)))
            (i 1))
        (while (and (<= i n)
                    (math-ident-row-p (nth i mat) i a))
          (setq i (1+ i)))
        (if (> i n)
            a
          nil))))

(defun math-ident-row-p (row n &optional a)
  (unless a
    (setq a 1))
  (and
   (not (memq nil (mapcar 
                   (lambda (x) (eq x 0))
                   (nthcdr (1+ n) row))))
   (not (memq nil (mapcar 
                   (lambda (x) (eq x 0))
                   (butlast 
                    (cdr row)
                    (- (length row) n)))))
   (eq (elt row n) a)))

;;; True if A is any scalar data object.  [P x]
(defun math-objectp (a)    ;  [Public]
  (or (integerp a)
      (memq (car-safe a) '(bigpos bigneg frac float cplx
				  polar hms date sdev intv mod))))

;;; Verify that A is an integer and return A in integer form.  [I N; - x]
(defun math-check-integer (a)   ;  [Public]
  (cond ((integerp a) a)  ; for speed
	((math-integerp a) a)
	((math-messy-integerp a)
	 (math-trunc a))
	(t (math-reject-arg a 'integerp))))

;;; Verify that A is a small integer and return A in integer form.  [S N; - x]
(defun math-check-fixnum (a &optional allow-inf)   ;  [Public]
  (cond ((integerp a) a)  ; for speed
	((Math-num-integerp a)
	 (let ((a (math-trunc a)))
	   (if (integerp a)
	       a
	     (if (or (Math-lessp (lsh -1 -1) a)
		     (Math-lessp a (- (lsh -1 -1))))
		 (math-reject-arg a 'fixnump)
	       (math-fixnum a)))))
	((and allow-inf (equal a '(var inf var-inf)))
	 (lsh -1 -1))
	((and allow-inf (equal a '(neg (var inf var-inf))))
	 (- (lsh -1 -1)))
	(t (math-reject-arg a 'fixnump))))

;;; Verify that A is an integer >= 0 and return A in integer form.  [I N; - x]
(defun math-check-natnum (a)    ;  [Public]
  (cond ((natnump a) a)
	((and (not (math-negp a))
	      (Math-num-integerp a))
	 (math-trunc a))
	(t (math-reject-arg a 'natnump))))

;;; Verify that A is in floating-point form, or force it to be a float.  [F N]
(defun math-check-float (a)    ; [Public]
  (cond ((eq (car-safe a) 'float) a)
	((Math-vectorp a) (math-map-vec 'math-check-float a))
	((Math-objectp a) (math-float a))
	(t a)))

;;; Verify that A is a constant.
(defun math-check-const (a &optional exp-ok)
  (if (or (math-constp a)
	  (and exp-ok math-expand-formulas))
      a
    (math-reject-arg a 'constp)))


;;; Coerce integer A to be a small integer.  [S I]
(defun math-fixnum (a)
  (if (consp a)
      (if (cdr a)
	  (if (eq (car a) 'bigneg)
	      (- (math-fixnum-big (cdr a)))
	    (math-fixnum-big (cdr a)))
	0)
    a))

(defun math-fixnum-big (a)
  (if (cdr a)
      (+ (car a) (* (math-fixnum-big (cdr a)) 1000))
    (car a)))

(defvar math-simplify-only nil)

(defun math-normalize-fancy (a)
  (cond ((eq (car a) 'frac)
	 (math-make-frac (math-normalize (nth 1 a))
			 (math-normalize (nth 2 a))))
	((eq (car a) 'cplx)
	 (let ((real (math-normalize (nth 1 a)))
	       (imag (math-normalize (nth 2 a))))
	   (if (and (math-zerop imag)
		    (not math-simplify-only))   ; oh, what a kludge!
	       real
	     (list 'cplx real imag))))
	((eq (car a) 'polar)
	 (math-normalize-polar a))
	((eq (car a) 'hms)
	 (math-normalize-hms a))
	((eq (car a) 'date)
	 (list 'date (math-normalize (nth 1 a))))
	((eq (car a) 'mod)
	 (math-normalize-mod a))
	((eq (car a) 'sdev)
	 (let ((x (math-normalize (nth 1 a)))
	       (s (math-normalize (nth 2 a))))
	   (if (or (and (Math-objectp x) (not (Math-scalarp x)))
		   (and (Math-objectp s) (not (Math-scalarp s))))
	       (list 'calcFunc-sdev x s)
	     (math-make-sdev x s))))
	((eq (car a) 'intv)
	 (let ((mask (math-normalize (nth 1 a)))
	       (lo (math-normalize (nth 2 a)))
	       (hi (math-normalize (nth 3 a))))
	   (if (if (eq (car-safe lo) 'date)
		   (not (eq (car-safe hi) 'date))
		 (or (and (Math-objectp lo) (not (Math-anglep lo)))
		     (and (Math-objectp hi) (not (Math-anglep hi)))))
	       (list 'calcFunc-intv mask lo hi)
	     (math-make-intv mask lo hi))))
	((eq (car a) 'vec)
	 (cons 'vec (mapcar 'math-normalize (cdr a))))
	((eq (car a) 'quote)
	 (math-normalize (nth 1 a)))
	((eq (car a) 'special-const)
	 (calc-with-default-simplification
	  (math-normalize (nth 1 a))))
	((eq (car a) 'var)
	 (cons 'var (cdr a)))   ; need to re-cons for selection routines
	((eq (car a) 'calcFunc-if)
	 (math-normalize-logical-op a))
	((memq (car a) '(calcFunc-lambda calcFunc-quote calcFunc-condition))
	 (let ((calc-simplify-mode 'none))
	   (cons (car a) (mapcar 'math-normalize (cdr a)))))
	((eq (car a) 'calcFunc-evalto)
	 (setq a (or (nth 1 a) 0))
	 (or calc-refreshing-evaltos
	     (setq a (let ((calc-simplify-mode 'none)) (math-normalize a))))
	 (let ((b (if (and (eq (car-safe a) 'calcFunc-assign)
			   (= (length a) 3))
		      (nth 2 a)
		    a)))
	   (list 'calcFunc-evalto
		 a
		 (if (eq calc-simplify-mode 'none)
		     (math-normalize b)
		   (calc-with-default-simplification
		    (math-evaluate-expr b))))))
	((or (integerp (car a)) (consp (car a)))
	 (if (null (cdr a))
	     (math-normalize (car a))
	   (error "Can't use multi-valued function in an expression")))))

;; The variable math-normalize-a is local to math-normalize in calc.el,
;; but is used by math-normalize-nonstandard, which is called by
;; math-normalize.
(defvar math-normalize-a)

(defun math-normalize-nonstandard ()
  (if (consp calc-simplify-mode)
      (progn
	(setq calc-simplify-mode 'none
	      math-simplify-only (car-safe (cdr-safe math-normalize-a)))
	nil)
    (and (symbolp (car math-normalize-a))
	 (or (eq calc-simplify-mode 'none)
	     (and (eq calc-simplify-mode 'num)
		  (let ((aptr (setq math-normalize-a 
                                    (cons
                                     (car math-normalize-a)
                                     (mapcar 'math-normalize 
                                             (cdr math-normalize-a))))))
		    (while (and aptr (math-constp (car aptr)))
		      (setq aptr (cdr aptr)))
		    aptr)))
	 (cons (car math-normalize-a) 
               (mapcar 'math-normalize (cdr math-normalize-a))))))


;;; Normalize a bignum digit list by trimming high-end zeros.  [L l]
(defun math-norm-bignum (a)
  (let ((digs a) (last nil))
    (while digs
      (or (eq (car digs) 0) (setq last digs))
      (setq digs (cdr digs)))
    (and last
	 (progn
	   (setcdr last nil)
	   a))))

(defun math-bignum-test (a)   ; [B N; B s; b b]
  (if (consp a)
      a
    (math-bignum a)))


;;; Return 0 for zero, -1 for negative, 1 for positive.  [S n] [Public]
(defun calcFunc-sign (a &optional x)
  (let ((signs (math-possible-signs a)))
    (cond ((eq signs 4) (or x 1))
	  ((eq signs 2) 0)
	  ((eq signs 1) (if x (math-neg x) -1))
	  ((math-looks-negp a) (math-neg (calcFunc-sign (math-neg a))))
	  (t (calc-record-why 'realp a)
	     (if x
		 (list 'calcFunc-sign a x)
	       (list 'calcFunc-sign a))))))

;;; Return 0 if A is numerically equal to B, <0 if less, >0 if more.
;;; Arguments must be normalized!  [S N N]
(defun math-compare (a b)
  (cond ((equal a b)
	 (if (and (consp a)
		  (memq (car a) '(var neg * /))
		  (math-infinitep a))
	     2
	   0))
	((and (integerp a) (Math-integerp b))
	 (if (consp b)
	     (if (eq (car b) 'bigpos) -1 1)
	   (if (< a b) -1 1)))
	((and (eq (car-safe a) 'bigpos) (Math-integerp b))
	 (if (eq (car-safe b) 'bigpos)
	     (math-compare-bignum (cdr a) (cdr b))
	   1))
	((and (eq (car-safe a) 'bigneg) (Math-integerp b))
	 (if (eq (car-safe b) 'bigneg)
	     (math-compare-bignum (cdr b) (cdr a))
	   -1))
	((eq (car-safe a) 'frac)
	 (if (eq (car-safe b) 'frac)
	     (math-compare (math-mul (nth 1 a) (nth 2 b))
			   (math-mul (nth 1 b) (nth 2 a)))
	   (math-compare (nth 1 a) (math-mul b (nth 2 a)))))
	((eq (car-safe b) 'frac)
	 (math-compare (math-mul a (nth 2 b)) (nth 1 b)))
	((and (eq (car-safe a) 'float) (eq (car-safe b) 'float))
	 (if (math-lessp-float a b) -1 1))
	((and (eq (car-safe a) 'date) (eq (car-safe b) 'date))
	 (math-compare (nth 1 a) (nth 1 b)))
	((and (or (Math-anglep a)
		  (and (eq (car a) 'cplx) (eq (nth 2 a) 0)))
	      (or (Math-anglep b)
		  (and (eq (car b) 'cplx) (eq (nth 2 b) 0))))
	 (calcFunc-sign (math-add a (math-neg b))))
	((and (eq (car-safe a) 'intv)
	      (or (Math-anglep b) (eq (car-safe b) 'date)))
	 (let ((res (math-compare (nth 2 a) b)))
	   (cond ((eq res 1) 1)
		 ((and (eq res 0) (memq (nth 1 a) '(0 1))) 1)
		 ((eq (setq res (math-compare (nth 3 a) b)) -1) -1)
		 ((and (eq res 0) (memq (nth 1 a) '(0 2))) -1)
		 (t 2))))
	((and (eq (car-safe b) 'intv)
	      (or (Math-anglep a) (eq (car-safe a) 'date)))
	 (let ((res (math-compare a (nth 2 b))))
	   (cond ((eq res -1) -1)
		 ((and (eq res 0) (memq (nth 1 b) '(0 1))) -1)
		 ((eq (setq res (math-compare a (nth 3 b))) 1) 1)
		 ((and (eq res 0) (memq (nth 1 b) '(0 2))) 1)
		 (t 2))))
	((and (eq (car-safe a) 'intv) (eq (car-safe b) 'intv))
	 (let ((res (math-compare (nth 3 a) (nth 2 b))))
	   (cond ((eq res -1) -1)
		 ((and (eq res 0) (or (memq (nth 1 a) '(0 2))
				      (memq (nth 1 b) '(0 1)))) -1)
		 ((eq (setq res (math-compare (nth 2 a) (nth 3 b))) 1) 1)
		 ((and (eq res 0) (or (memq (nth 1 a) '(0 1))
				      (memq (nth 1 b) '(0 2)))) 1)
		 (t 2))))
	((math-infinitep a)
	 (if (or (equal a '(var uinf var-uinf))
		 (equal a '(var nan var-nan)))
	     2
	   (let ((dira (math-infinite-dir a)))
	     (if (math-infinitep b)
		 (if (or (equal b '(var uinf var-uinf))
			 (equal b '(var nan var-nan)))
		     2
		   (let ((dirb (math-infinite-dir b)))
		     (cond ((and (eq dira 1) (eq dirb -1)) 1)
			   ((and (eq dira -1) (eq dirb 1)) -1)
			   (t 2))))
	       (cond ((eq dira 1) 1)
		     ((eq dira -1) -1)
		     (t 2))))))
	((math-infinitep b)
	 (if (or (equal b '(var uinf var-uinf))
		 (equal b '(var nan var-nan)))
	     2
	   (let ((dirb (math-infinite-dir b)))
	     (cond ((eq dirb 1) -1)
		   ((eq dirb -1) 1)
		   (t 2)))))
	((and (eq (car-safe a) 'calcFunc-exp)
	      (eq (car-safe b) '^)
	      (equal (nth 1 b) '(var e var-e)))
	 (math-compare (nth 1 a) (nth 2 b)))
	((and (eq (car-safe b) 'calcFunc-exp)
	      (eq (car-safe a) '^)
	      (equal (nth 1 a) '(var e var-e)))
	 (math-compare (nth 2 a) (nth 1 b)))
	((or (and (eq (car-safe a) 'calcFunc-sqrt)
		  (eq (car-safe b) '^)
		  (or (equal (nth 2 b) '(frac 1 2))
		      (equal (nth 2 b) '(float 5 -1))))
	     (and (eq (car-safe b) 'calcFunc-sqrt)
		  (eq (car-safe a) '^)
		  (or (equal (nth 2 a) '(frac 1 2))
		      (equal (nth 2 a) '(float 5 -1)))))
	 (math-compare (nth 1 a) (nth 1 b)))
	((eq (car-safe a) 'var)
	 2)
	(t
	 (if (and (consp a) (consp b)
		  (eq (car a) (car b))
		  (math-compare-lists (cdr a) (cdr b)))
	     0
	   2))))

;;; Compare two bignum digit lists, return -1 for A<B, 0 for A=B, 1 for A>B.
(defun math-compare-bignum (a b)   ; [S l l]
  (let ((res 0))
    (while (and a b)
      (if (< (car a) (car b))
	  (setq res -1)
	(if (> (car a) (car b))
	    (setq res 1)))
      (setq a (cdr a)
	    b (cdr b)))
    (if a
	(progn
	  (while (eq (car a) 0) (setq a (cdr a)))
	  (if a 1 res))
      (while (eq (car b) 0) (setq b (cdr b)))
      (if b -1 res))))

(defun math-compare-lists (a b)
  (cond ((null a) (null b))
	((null b) nil)
	(t (and (Math-equal (car a) (car b))
		(math-compare-lists (cdr a) (cdr b))))))

(defun math-lessp-float (a b)   ; [P F F]
  (let ((ediff (- (nth 2 a) (nth 2 b))))
    (if (>= ediff 0)
	(if (>= ediff (+ calc-internal-prec calc-internal-prec))
	    (if (eq (nth 1 a) 0)
		(Math-integer-posp (nth 1 b))
	      (Math-integer-negp (nth 1 a)))
	  (Math-lessp (math-scale-int (nth 1 a) ediff)
		      (nth 1 b)))
      (if (>= (setq ediff (- ediff))
	      (+ calc-internal-prec calc-internal-prec))
	  (if (eq (nth 1 b) 0)
	      (Math-integer-negp (nth 1 a))
	    (Math-integer-posp (nth 1 b)))
	(Math-lessp (nth 1 a)
		    (math-scale-int (nth 1 b) ediff))))))

;;; True if A is numerically equal to B.  [P N N] [Public]
(defun math-equal (a b)
  (= (math-compare a b) 0))

;;; True if A is numerically less than B.  [P R R] [Public]
(defun math-lessp (a b)
  (= (math-compare a b) -1))

;;; True if A is numerically equal to the integer B.  [P N S] [Public]
;;; B must not be a multiple of 10.
(defun math-equal-int (a b)
  (or (eq a b)
      (and (eq (car-safe a) 'float)
	   (eq (nth 1 a) b)
	   (= (nth 2 a) 0))))




;;; Return the dimensions of a matrix as a list.  [l x] [Public]
(defun math-mat-dimens (m)
  (if (math-vectorp m)
      (if (math-matrixp m)
	  (cons (1- (length m))
		(math-mat-dimens (nth 1 m)))
	(list (1- (length m))))
    nil))



(defun calc-binary-op-fancy (name func arg ident unary)
  (let ((n (prefix-numeric-value arg)))
    (cond ((> n 1)
	   (calc-enter-result n
			      name
			      (list 'calcFunc-reduce
				    (math-calcFunc-to-var func)
				    (cons 'vec (calc-top-list-n n)))))
	  ((= n 1)
	   (if unary
	       (calc-enter-result 1 name (list unary (calc-top-n 1)))))
	  ((= n 0)
	   (if ident
	       (calc-enter-result 0 name ident)
	     (error "Argument must be nonzero")))
	  (t
	   (let ((rhs (calc-top-n 1)))
	     (calc-enter-result (- 1 n)
				name
				(mapcar (function
					 (lambda (x)
					   (list func x rhs)))
					(calc-top-list-n (- n) 2))))))))

(defun calc-unary-op-fancy (name func arg)
  (let ((n (prefix-numeric-value arg)))
    (if (= n 0) (setq n (calc-stack-size)))
    (cond ((> n 0)
	   (calc-enter-result n
			      name
			      (mapcar (function
				       (lambda (x)
					 (list func x)))
				      (calc-top-list-n n))))
	  ((< n 0)
	   (calc-enter-result 1
			      name
			      (list func (calc-top-n (- n)))
			      (- n))))))

(defvar var-Holidays '(vec (var sat var-sat) (var sun var-sun)))
(defvar var-Decls (list 'vec))


(defun math-inexact-result ()
  (and calc-symbolic-mode
       (signal 'inexact-result nil)))

(defun math-overflow (&optional exp)
  (if (and exp (math-negp exp))
      (math-underflow)
    (signal 'math-overflow nil)))

(defun math-underflow ()
  (signal 'math-underflow nil))

;;; Compute the greatest common divisor of A and B.   [I I I] [Public]
(defun math-gcd (a b)
  (cond ((not (or (consp a) (consp b)))
	 (if (< a 0) (setq a (- a)))
	 (if (< b 0) (setq b (- b)))
	 (let (c)
	   (if (< a b)
	       (setq c b b a a c))
	   (while (> b 0)
	     (setq c b
		   b (% a b)
		   a c))
	   a))
	((eq a 0) b)
	((eq b 0) a)
	(t
	 (if (Math-integer-negp a) (setq a (math-neg a)))
	 (if (Math-integer-negp b) (setq b (math-neg b)))
	 (let (c)
	   (if (Math-natnum-lessp a b)
	       (setq c b b a a c))
	   (while (and (consp a) (not (eq b 0)))
	     (setq c b
		   b (math-imod a b)
		   a c))
	   (while (> b 0)
	     (setq c b
		   b (% a b)
		   a c))
	   a))))


;;;; Algebra.

;;; Evaluate variables in an expression.
(defun math-evaluate-expr (x)  ; [Public]
  (if calc-embedded-info
      (calc-embedded-evaluate-expr x)
    (calc-normalize (math-evaluate-expr-rec x))))

(defalias 'calcFunc-evalv 'math-evaluate-expr)

(defun calcFunc-evalvn (x &optional prec)
  (if prec
      (progn
	(or (math-num-integerp prec)
	    (if (and (math-vectorp prec)
		     (= (length prec) 2)
		     (math-num-integerp (nth 1 prec)))
		(setq prec (math-add (nth 1 prec) calc-internal-prec))
	      (math-reject-arg prec 'integerp)))
	(setq prec (math-trunc prec))
	(if (< prec 3) (setq prec 3))
	(if (> prec calc-internal-prec)
	    (math-normalize
	     (let ((calc-internal-prec prec))
	       (calcFunc-evalvn x)))
	  (let ((calc-internal-prec prec))
	    (calcFunc-evalvn x))))
    (let ((calc-symbolic-mode nil))
      (math-evaluate-expr x))))

(defun math-evaluate-expr-rec (x)
  (if (consp x)
      (if (memq (car x) '(calcFunc-quote calcFunc-condition
					 calcFunc-evalto calcFunc-assign))
	  (if (and (eq (car x) 'calcFunc-assign)
		   (= (length x) 3))
	      (list (car x) (nth 1 x) (math-evaluate-expr-rec (nth 2 x)))
	    x)
	(if (eq (car x) 'var)
	    (if (and (calc-var-value (nth 2 x))
		     (not (eq (car-safe (symbol-value (nth 2 x)))
			      'incomplete)))
		(let ((val (symbol-value (nth 2 x))))
		  (if (eq (car-safe val) 'special-const)
		      (if calc-symbolic-mode
			  x
			val)
		    val))
	      x)
	  (if (Math-primp x)
	      x
	    (cons (car x) (mapcar 'math-evaluate-expr-rec (cdr x))))))
    x))

(defun math-any-floats (expr)
  (if (Math-primp expr)
      (math-floatp expr)
    (while (and (setq expr (cdr expr)) (not (math-any-floats (car expr)))))
    expr))

(defvar var-FactorRules 'calc-FactorRules)

(defvar math-mt-many nil)
(defvar math-mt-func nil)

(defun math-map-tree (math-mt-func mmt-expr &optional math-mt-many)
  (or math-mt-many (setq math-mt-many 1000000))
  (math-map-tree-rec mmt-expr))

(defun math-map-tree-rec (mmt-expr)
  (or (= math-mt-many 0)
      (let ((mmt-done nil)
	    mmt-nextval)
	(while (not mmt-done)
	  (while (and (/= math-mt-many 0)
		      (setq mmt-nextval (funcall math-mt-func mmt-expr))
		      (not (equal mmt-expr mmt-nextval)))
	    (setq mmt-expr mmt-nextval
		  math-mt-many (if (> math-mt-many 0) 
                                   (1- math-mt-many) 
                                 (1+ math-mt-many))))
	  (if (or (Math-primp mmt-expr)
		  (<= math-mt-many 0))
	      (setq mmt-done t)
	    (setq mmt-nextval (cons (car mmt-expr)
				    (mapcar 'math-map-tree-rec
					    (cdr mmt-expr))))
	    (if (equal mmt-nextval mmt-expr)
		(setq mmt-done t)
	      (setq mmt-expr mmt-nextval))))))
  mmt-expr)

(defun math-is-true (expr)
  (if (Math-numberp expr)
      (not (Math-zerop expr))
    (math-known-nonzerop expr)))

(defun math-const-var (expr)
  (and (consp expr)
       (eq (car expr) 'var)
       (or (and (symbolp (nth 2 expr))
		(boundp (nth 2 expr))
		(eq (car-safe (symbol-value (nth 2 expr))) 'special-const))
	   (memq (nth 2 expr) '(var-inf var-uinf var-nan)))))

;; The variable math-integral-cache is originally declared in calcalg2.el,
;; but is set by math-defintegral and math-definitegral2.
(defvar math-integral-cache)

(defmacro math-defintegral (funcs &rest code)
  (setq math-integral-cache nil)
  (append '(progn)
	  (mapcar (function
		   (lambda (func)
		     (list 'put (list 'quote func) ''math-integral
			   (list 'nconc
				 (list 'get (list 'quote func) ''math-integral)
				 (list 'list
				       (list 'function
					     (append '(lambda (u))
						     code)))))))
		  (if (symbolp funcs) (list funcs) funcs))))
(put 'math-defintegral 'lisp-indent-hook 1)

(defmacro math-defintegral-2 (funcs &rest code)
  (setq math-integral-cache nil)
  (append '(progn)
	  (mapcar (function
		   (lambda (func)
		     (list 'put (list 'quote func) ''math-integral-2
			   (list 'nconc
				 (list 'get (list 'quote func)
				       ''math-integral-2)
				 (list 'list
				       (list 'function
					     (append '(lambda (u v))
						     code)))))))
		  (if (symbolp funcs) (list funcs) funcs))))
(put 'math-defintegral-2 'lisp-indent-hook 1)

(defvar var-IntegAfterRules 'calc-IntegAfterRules)

(defvar var-FitRules 'calc-FitRules)

(defvar math-poly-base-variable nil)
(defvar math-poly-neg-powers nil)
(defvar math-poly-mult-powers 1)
(defvar math-poly-frac-powers nil)
(defvar math-poly-exp-base nil)

(defun math-build-var-name (name)
  (if (stringp name)
      (setq name (intern name)))
  (if (string-match "\\`var-." (symbol-name name))
      (list 'var (intern (substring (symbol-name name) 4)) name)
    (list 'var name (intern (concat "var-" (symbol-name name))))))

(defvar math-simplifying-units nil)
(defvar math-combining-units t)

;;; Nontrivial number parsing.

(defun math-read-number-fancy (s)
  (cond

   ;; Integer+fractions
   ((string-match "^\\([0-9]*\\)[:/]\\([0-9]*\\)[:/]\\([0-9]*\\)$" s)
    (let ((int (math-match-substring s 1))
	  (num (math-match-substring s 2))
	  (den (math-match-substring s 3)))
      (let ((int (if (> (length int) 0) (math-read-number int) 0))
	    (num (if (> (length num) 0) (math-read-number num) 1))
	    (den (if (> (length num) 0) (math-read-number den) 1)))
	(and int num den
	     (math-integerp int) (math-integerp num) (math-integerp den)
	     (not (math-zerop den))
	     (list 'frac (math-add num (math-mul int den)) den)))))

   ;; Fractions
   ((string-match "^\\([0-9]*\\)[:/]\\([0-9]*\\)$" s)
    (let ((num (math-match-substring s 1))
	  (den (math-match-substring s 2)))
      (let ((num (if (> (length num) 0) (math-read-number num) 1))
	    (den (if (> (length num) 0) (math-read-number den) 1)))
	(and num den (math-integerp num) (math-integerp den)
	     (not (math-zerop den))
	     (list 'frac num den)))))

   ;; Modulo forms
   ((string-match "^\\(.*\\) *mod *\\(.*\\)$" s)
    (let* ((n (math-match-substring s 1))
	   (m (math-match-substring s 2))
	   (n (math-read-number n))
	   (m (math-read-number m)))
      (and n m (math-anglep n) (math-anglep m)
	   (list 'mod n m))))

   ;; Error forms
   ((string-match "^\\(.*\\) *\\+/- *\\(.*\\)$" s)
    (let* ((x (math-match-substring s 1))
	   (sigma (math-match-substring s 2))
	   (x (math-read-number x))
	   (sigma (math-read-number sigma)))
      (and x sigma (math-scalarp x) (math-anglep sigma)
	   (list 'sdev x sigma))))

   ;; Hours (or degrees)
   ((or (string-match "^\\([^#^]+\\)[@oOhH]\\(.*\\)$" s)
	(string-match "^\\([^#^]+\\)[dD][eE]?[gG]?\\(.*\\)$" s))
    (let* ((hours (math-match-substring s 1))
	   (minsec (math-match-substring s 2))
	   (hours (math-read-number hours))
	   (minsec (if (> (length minsec) 0) (math-read-number minsec) 0)))
      (and hours minsec
	   (math-num-integerp hours)
	   (not (math-negp hours)) (not (math-negp minsec))
	   (cond ((math-num-integerp minsec)
		  (and (Math-lessp minsec 60)
		       (list 'hms hours minsec 0)))
		 ((and (eq (car-safe minsec) 'hms)
		       (math-zerop (nth 1 minsec)))
		  (math-add (list 'hms hours 0 0) minsec))
		 (t nil)))))

   ;; Minutes
   ((string-match "^\\([^'#^]+\\)[mM']\\(.*\\)$" s)
    (let* ((minutes (math-match-substring s 1))
	   (seconds (math-match-substring s 2))
	   (minutes (math-read-number minutes))
	   (seconds (if (> (length seconds) 0) (math-read-number seconds) 0)))
      (and minutes seconds
	   (math-num-integerp minutes)
	   (not (math-negp minutes)) (not (math-negp seconds))
	   (cond ((math-realp seconds)
		  (and (Math-lessp minutes 60)
		       (list 'hms 0 minutes seconds)))
		 ((and (eq (car-safe seconds) 'hms)
		       (math-zerop (nth 1 seconds))
		       (math-zerop (nth 2 seconds)))
		  (math-add (list 'hms 0 minutes 0) seconds))
		 (t nil)))))

   ;; Seconds
   ((string-match "^\\([^\"#^]+\\)[sS\"]$" s)
    (let ((seconds (math-read-number (math-match-substring s 1))))
      (and seconds (math-realp seconds)
	   (not (math-negp seconds))
	   (Math-lessp seconds 60)
	   (list 'hms 0 0 seconds))))

   ;; Integer+fraction with explicit radix
   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]\\)$" s)
    (let ((radix (string-to-number (math-match-substring s 1)))
	  (int (math-match-substring s 3))
	  (num (math-match-substring s 4))
	  (den (math-match-substring s 5)))
      (let ((int (if (> (length int) 0) (math-read-radix int radix) 0))
	    (num (if (> (length num) 0) (math-read-radix num radix) 1))
	    (den (if (> (length den) 0) (math-read-radix den radix) 1)))
	(and int num den (not (math-zerop den))
	     (list 'frac
		   (math-add num (math-mul int den))
		   den)))))

   ;; Fraction with explicit radix
   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]*\\)$" s)
    (let ((radix (string-to-number (math-match-substring s 1)))
	  (num (math-match-substring s 3))
	  (den (math-match-substring s 4)))
      (let ((num (if (> (length num) 0) (math-read-radix num radix) 1))
	    (den (if (> (length den) 0) (math-read-radix den radix) 1)))
	(and num den (not (math-zerop den)) (list 'frac num den)))))

   ;; Float with explicit radix and exponent
   ((or (string-match "^0*\\(\\([2-9]\\|1[0-4]\\)\\(#\\|\\^\\^\\)[0-9a-dA-D.]+\\)[eE]\\([-+]?[0-9]+\\)$" s)
	(string-match "^\\(\\([0-9]+\\)\\(#\\|\\^\\^\\)[0-9a-zA-Z.]+\\) *\\* *\\2\\.? *\\^ *\\([-+]?[0-9]+\\)$" s))
    (let ((radix (string-to-number (math-match-substring s 2)))
	  (mant (math-match-substring s 1))
	  (exp (math-match-substring s 4)))
      (let ((mant (math-read-number mant))
	    (exp (math-read-number exp)))
	(and mant exp
	     (math-mul mant (math-pow (math-float radix) exp))))))

   ;; Float with explicit radix, no exponent
   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]*\\)\\.\\([0-9a-zA-Z]*\\)$" s)
    (let ((radix (string-to-number (math-match-substring s 1)))
	  (int (math-match-substring s 3))
	  (fracs (math-match-substring s 4)))
      (let ((int (if (> (length int) 0) (math-read-radix int radix) 0))
	    (frac (if (> (length fracs) 0) (math-read-radix fracs radix) 0))
	    (calc-prefer-frac nil))
	(and int frac
	     (math-add int (math-div frac (math-pow radix (length fracs))))))))

   ;; Integer with explicit radix
   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]+\\)$" s)
    (math-read-radix (math-match-substring s 3)
		     (string-to-number (math-match-substring s 1))))

   ;; C language hexadecimal notation
   ((and (eq calc-language 'c)
	 (string-match "^0[xX]\\([0-9a-fA-F]+\\)$" s))
    (let ((digs (math-match-substring s 1)))
      (math-read-radix digs 16)))

   ;; Pascal language hexadecimal notation
   ((and (eq calc-language 'pascal)
	 (string-match "^\\$\\([0-9a-fA-F]+\\)$" s))
    (let ((digs (math-match-substring s 1)))
      (math-read-radix digs 16)))

   ;; Fraction using "/" instead of ":"
   ((string-match "^\\([0-9]+\\)/\\([0-9/]+\\)$" s)
    (math-read-number (concat (math-match-substring s 1) ":"
			      (math-match-substring s 2))))

   ;; Syntax error!
   (t nil)))

(defun math-read-radix (s r)   ; [I X D]
  (setq s (upcase s))
  (let ((i 0)
	(res 0)
	dig)
    (while (and (< i (length s))
		(setq dig (math-read-radix-digit (elt s i)))
		(< dig r))
      (setq res (math-add (math-mul res r) dig)
	    i (1+ i)))
    (and (= i (length s))
	 res)))



;;; Expression parsing.

(defvar math-expr-data)

(defun math-read-expr (math-exp-str)
  (let ((math-exp-pos 0)
	(math-exp-old-pos 0)
	(math-exp-keep-spaces nil)
	math-exp-token math-expr-data)
    (setq math-exp-str (math-read-preprocess-string math-exp-str))
    (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
      (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
			    (substring math-exp-str (+ math-exp-token 2)))))
    (math-build-parse-table)
    (math-read-token)
    (let ((val (catch 'syntax (math-read-expr-level 0))))
      (if (stringp val)
	  (list 'error math-exp-old-pos val)
	(if (equal math-exp-token 'end)
	    val
	  (list 'error math-exp-old-pos "Syntax error"))))))

(defun math-read-plain-expr (exp-str &optional error-check)
  (let* ((calc-language nil)
	 (math-expr-opers math-standard-opers)
	 (val (math-read-expr exp-str)))
    (and error-check
	 (eq (car-safe val) 'error)
	 (error "%s: %s" (nth 2 val) exp-str))
    val))


(defun math-read-string ()
  (let ((str (read-from-string (concat math-expr-data "\""))))
    (or (and (= (cdr str) (1+ (length math-expr-data)))
	     (stringp (car str)))
	(throw 'syntax "Error in string constant"))
    (math-read-token)
    (append '(vec) (car str) nil)))



;;; They said it couldn't be done...

(defun math-read-big-expr (str)
  (and (> (length calc-left-label) 0)
       (string-match (concat "^" (regexp-quote calc-left-label)) str)
       (setq str (concat (substring str 0 (match-beginning 0))
			 (substring str (match-end 0)))))
  (and (> (length calc-right-label) 0)
       (string-match (concat (regexp-quote calc-right-label) " *$") str)
       (setq str (concat (substring str 0 (match-beginning 0))
			 (substring str (match-end 0)))))
  (if (string-match "\\\\[^ \n|]" str)
      (if (eq calc-language 'latex)
	  (math-read-expr str)
	(let ((calc-language 'latex)
	      (calc-language-option nil)
	      (math-expr-opers (get 'latex 'math-oper-table))
	      (math-expr-function-mapping (get 'latex 'math-function-table))
	      (math-expr-variable-mapping (get 'latex 'math-variable-table)))
	  (math-read-expr str)))
    (let ((math-read-big-lines nil)
	  (pos 0)
	  (width 0)
	  (math-read-big-err-msg nil)
	  math-read-big-baseline math-read-big-h2
	  new-pos p)
      (while (setq new-pos (string-match "\n" str pos))
	(setq math-read-big-lines 
              (cons (substring str pos new-pos) math-read-big-lines)
	      pos (1+ new-pos)))
      (setq math-read-big-lines 
            (nreverse (cons (substring str pos) math-read-big-lines))
	    p math-read-big-lines)
      (while p
	(setq width (max width (length (car p)))
	      p (cdr p)))
      (if (math-read-big-bigp math-read-big-lines)
	  (or (catch 'syntax
		(math-read-big-rec 0 0 width (length math-read-big-lines)))
	      math-read-big-err-msg
	      '(error 0 "Syntax error"))
	(math-read-expr str)))))

(defun math-read-big-bigp (math-read-big-lines)
  (and (cdr math-read-big-lines)
       (let ((matrix nil)
	     (v 0)
	     (height (if (> (length (car math-read-big-lines)) 0) 1 0)))
	 (while (and (cdr math-read-big-lines)
		     (let* ((i 0)
			    j
			    (l1 (car math-read-big-lines))
			    (l2 (nth 1 math-read-big-lines))
			    (len (min (length l1) (length l2))))
		       (if (> (length l2) 0)
			   (setq height (1+ height)))
		       (while (and (< i len)
				   (or (memq (aref l1 i) '(?\  ?\- ?\_))
				       (memq (aref l2 i) '(?\  ?\-))
				       (and (memq (aref l1 i) '(?\| ?\,))
					    (= (aref l2 i) (aref l1 i)))
				       (and (eq (aref l1 i) ?\[)
					    (eq (aref l2 i) ?\[)
					    (let ((math-rb-h2 (length l1)))
					      (setq j (math-read-big-balance
						       (1+ i) v "[")))
					    (setq i (1- j)))))
			 (setq i (1+ i)))
		       (or (= i len)
			   (and (eq (aref l1 i) ?\[)
				(eq (aref l2 i) ?\[)
				(setq matrix t)
				nil))))
	   (setq math-read-big-lines (cdr math-read-big-lines)
		 v (1+ v)))
	 (or (and (> height 1)
		  (not (cdr math-read-big-lines)))
	     matrix))))

;;; Nontrivial "flat" formatting.

(defvar math-format-hash-args nil)
(defvar calc-can-abbrev-vectors nil)

(defun math-format-flat-expr-fancy (a prec)
  (cond
   ((eq (car a) 'incomplete)
    (format "<incomplete %s>" (nth 1 a)))
   ((eq (car a) 'vec)
    (if (or calc-full-trail-vectors (not calc-can-abbrev-vectors)
	    (< (length a) 7))
	(concat "[" (math-format-flat-vector (cdr a) ", "
					     (if (cdr (cdr a)) 0 1000)) "]")
      (concat "["
	      (math-format-flat-expr (nth 1 a) 0) ", "
	      (math-format-flat-expr (nth 2 a) 0) ", "
	      (math-format-flat-expr (nth 3 a) 0) ", ..., "
	      (math-format-flat-expr (nth (1- (length a)) a) 0) "]")))
   ((eq (car a) 'intv)
    (concat (if (memq (nth 1 a) '(0 1)) "(" "[")
	    (math-format-flat-expr (nth 2 a) 1000)
	    " .. "
	    (math-format-flat-expr (nth 3 a) 1000)
	    (if (memq (nth 1 a) '(0 2)) ")" "]")))
   ((eq (car a) 'date)
    (concat "<" (math-format-date a) ">"))
   ((and (eq (car a) 'calcFunc-lambda) (> (length a) 2))
    (let ((p (cdr a))
	  (ap calc-arg-values)
	  (math-format-hash-args (if (= (length a) 3) 1 t)))
      (while (and (cdr p) (equal (car p) (car ap)))
	(setq p (cdr p) ap (cdr ap)))
      (concat "<"
	      (if (cdr p)
		  (concat (math-format-flat-vector
			   (nreverse (cdr (reverse (cdr a)))) ", " 0)
			  " : ")
		"")
	      (math-format-flat-expr (nth (1- (length a)) a) 0)
	      ">")))
   ((eq (car a) 'var)
    (or (and math-format-hash-args
	     (let ((p calc-arg-values) (v 1))
	       (while (and p (not (equal (car p) a)))
		 (setq p (and (eq math-format-hash-args t) (cdr p))
		       v (1+ v)))
	       (and p
		    (if (eq math-format-hash-args 1)
			"#"
		      (format "#%d" v)))))
	(symbol-name (nth 1 a))))
   ((and (memq (car a) '(calcFunc-string calcFunc-bstring))
	 (= (length a) 2)
	 (math-vectorp (nth 1 a))
	 (math-vector-is-string (nth 1 a)))
    (concat (substring (symbol-name (car a)) 9)
	    "(" (math-vector-to-string (nth 1 a) t) ")"))
   (t
    (let ((op (math-assq2 (car a) math-standard-opers)))
      (cond ((and op (= (length a) 3))
	     (if (> prec (min (nth 2 op) (nth 3 op)))
		 (concat "(" (math-format-flat-expr a 0) ")")
	       (let ((lhs (math-format-flat-expr (nth 1 a) (nth 2 op)))
		     (rhs (math-format-flat-expr (nth 2 a) (nth 3 op))))
		 (setq op (car op))
		 (if (or (equal op "^") (equal op "_"))
		     (if (= (aref lhs 0) ?-)
			 (setq lhs (concat "(" lhs ")")))
		   (setq op (concat " " op " ")))
		 (concat lhs op rhs))))
	    ((eq (car a) 'neg)
	     (concat "-" (math-format-flat-expr (nth 1 a) 1000)))
	    (t
	     (concat (math-remove-dashes
		      (if (string-match "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
					(symbol-name (car a)))
			  (math-match-substring (symbol-name (car a)) 1)
			(symbol-name (car a))))
		     "("
		     (math-format-flat-vector (cdr a) ", " 0)
		     ")")))))))

(defun math-format-flat-vector (vec sep prec)
  (if vec
      (let ((buf (math-format-flat-expr (car vec) prec)))
	(while (setq vec (cdr vec))
	  (setq buf (concat buf sep (math-format-flat-expr (car vec) prec))))
	buf)
    ""))

(defun math-format-nice-expr (x w)
  (cond ((and (eq (car-safe x) 'vec)
	      (cdr (cdr x))
	      (let ((ops '(vec calcFunc-assign calcFunc-condition
			       calcFunc-schedule calcFunc-iterations
			       calcFunc-phase)))
		(or (memq (car-safe (nth 1 x)) ops)
		    (memq (car-safe (nth 2 x)) ops)
		    (memq (car-safe (nth 3 x)) ops)
		    calc-break-vectors)))
	 (concat "[ " (math-format-flat-vector (cdr x) ",\n  " 0) " ]"))
	(t
	 (let ((str (math-format-flat-expr x 0))
	       (pos 0) p)
	   (or (string-match "\"" str)
	       (while (<= (setq p (+ pos w)) (length str))
		 (while (and (> (setq p (1- p)) pos)
			     (not (= (aref str p) ? ))))
		 (if (> p (+ pos 5))
		     (setq str (concat (substring str 0 p)
				       "\n "
				       (substring str p))
			   pos (1+ p))
		   (setq pos (+ pos w)))))
	   str))))

(defun math-assq2 (v a)
  (while (and a (not (eq v (nth 1 (car a)))))
    (setq a (cdr a)))
  (car a))

(defun math-format-number-fancy (a prec)
  (cond
   ((eq (car a) 'float)    ; non-decimal radix
    (if (Math-integer-negp (nth 1 a))
	(concat "-" (math-format-number (math-neg a)))
      (let ((str (if (and calc-radix-formatter
			  (not (memq calc-language '(c pascal))))
		     (funcall calc-radix-formatter
			      calc-number-radix
			      (math-format-radix-float a prec))
		   (format "%d#%s" calc-number-radix
			   (math-format-radix-float a prec)))))
	(if (and prec (> prec 191) (string-match "\\*" str))
	    (concat "(" str ")")
	  str))))
   ((eq (car a) 'frac)
    (setq a (math-adjust-fraction a))
    (if (> (length (car calc-frac-format)) 1)
	(if (Math-integer-negp (nth 1 a))
	    (concat "-" (math-format-number (math-neg a)))
	  (let ((q (math-idivmod (nth 1 a) (nth 2 a))))
	    (concat (let ((calc-frac-format nil))
		      (math-format-number (car q)))
		    (substring (car calc-frac-format) 0 1)
		    (let ((math-radix-explicit-format nil)
			  (calc-frac-format nil))
		      (math-format-number (cdr q)))
		    (substring (car calc-frac-format) 1 2)
		    (let ((math-radix-explicit-format nil)
			  (calc-frac-format nil))
		      (math-format-number (nth 2 a))))))
      (concat (let ((calc-frac-format nil))
		(math-format-number (nth 1 a)))
	      (car calc-frac-format)
	      (let ((math-radix-explicit-format nil)
		    (calc-frac-format nil))
		(math-format-number (nth 2 a))))))
   ((eq (car a) 'cplx)
    (if (math-zerop (nth 2 a))
	(math-format-number (nth 1 a))
      (if (null calc-complex-format)
	  (concat "(" (math-format-number (nth 1 a))
		  ", " (math-format-number (nth 2 a)) ")")
	(if (math-zerop (nth 1 a))
	    (if (math-equal-int (nth 2 a) 1)
		(symbol-name calc-complex-format)
	      (if (math-equal-int (nth 2 a) -1)
		  (concat "-" (symbol-name calc-complex-format))
		(if prec
		    (math-compose-expr (list '* (nth 2 a) '(cplx 0 1)) prec)
		  (concat (math-format-number (nth 2 a)) " "
			  (symbol-name calc-complex-format)))))
	  (if prec
	      (math-compose-expr (list (if (math-negp (nth 2 a)) '- '+)
				       (nth 1 a)
				       (list 'cplx 0 (math-abs (nth 2 a))))
				 prec)
	    (concat (math-format-number (nth 1 a))
		    (if (math-negp (nth 2 a)) " - " " + ")
		    (math-format-number
		     (list 'cplx 0 (math-abs (nth 2 a))))))))))
   ((eq (car a) 'polar)
    (concat "(" (math-format-number (nth 1 a))
	    "; " (math-format-number (nth 2 a)) ")"))
   ((eq (car a) 'hms)
    (if (math-negp a)
	(concat "-" (math-format-number (math-neg a)))
      (let ((calc-number-radix 10)
	    (calc-leading-zeros nil)
	    (calc-group-digits nil))
	(format calc-hms-format
		(let ((calc-frac-format '(":" nil)))
		  (math-format-number (nth 1 a)))
		(let ((calc-frac-format '(":" nil)))
		  (math-format-number (nth 2 a)))
		(math-format-number (nth 3 a))))))
   ((eq (car a) 'intv)
    (concat (if (memq (nth 1 a) '(0 1)) "(" "[")
	    (math-format-number (nth 2 a))
	    " .. "
	    (math-format-number (nth 3 a))
	    (if (memq (nth 1 a) '(0 2)) ")" "]")))
   ((eq (car a) 'sdev)
    (concat (math-format-number (nth 1 a))
	    " +/- "
	    (math-format-number (nth 2 a))))
   ((eq (car a) 'vec)
    (math-format-flat-expr a 0))
   (t (format "%s" a))))

(defun math-adjust-fraction (a)
  (if (nth 1 calc-frac-format)
      (progn
	(if (Math-integerp a) (setq a (list 'frac a 1)))
	(let ((g (math-quotient (nth 1 calc-frac-format)
				(math-gcd (nth 2 a)
					  (nth 1 calc-frac-format)))))
	  (list 'frac (math-mul (nth 1 a) g) (math-mul (nth 2 a) g))))
    a))

(defun math-format-bignum-fancy (a)   ; [X L]
  (let ((str (cond ((= calc-number-radix 10)
		    (math-format-bignum-decimal a))
		   ((= calc-number-radix 2)
		    (math-format-bignum-binary a))
		   ((= calc-number-radix 8)
		    (math-format-bignum-octal a))
		   ((= calc-number-radix 16)
		    (math-format-bignum-hex a))
		   (t (math-format-bignum-radix a)))))
    (if calc-leading-zeros
	(let* ((calc-internal-prec 6)
	       (digs (math-compute-max-digits (math-abs calc-word-size)
					      calc-number-radix))
	       (len (length str)))
	  (if (< len digs)
	      (setq str (concat (make-string (- digs len) ?0) str)))))
    (if calc-group-digits
	(let ((i (length str))
	      (g (if (integerp calc-group-digits)
		     (math-abs calc-group-digits)
		   (if (memq calc-number-radix '(2 16)) 4 3))))
	  (while (> i g)
	    (setq i (- i g)
		  str (concat (substring str 0 i)
			      calc-group-char
			      (substring str i))))
	  str))
    (if (and (/= calc-number-radix 10)
	     math-radix-explicit-format)
	(if calc-radix-formatter
	    (funcall calc-radix-formatter calc-number-radix str)
	  (format "%d#%s" calc-number-radix str))
      str)))


(defun math-group-float (str)   ; [X X]
  (let* ((pt (or (string-match "[^0-9a-zA-Z]" str) (length str)))
	 (g (if (integerp calc-group-digits) (math-abs calc-group-digits) 3))
	 (i pt))
    (if (and (integerp calc-group-digits) (< calc-group-digits 0))
	(while (< (setq i (+ (1+ i) g)) (length str))
	  (setq str (concat (substring str 0 i)
			    calc-group-char
			    (substring str i))
		i (+ i (1- (length calc-group-char))))))
    (setq i pt)
    (while (> i g)
      (setq i (- i g)
	    str (concat (substring str 0 i)
			calc-group-char
			(substring str i))))
    str))

;;; Users can redefine this in their .emacs files.
(defvar calc-keypad-user-menu nil
  "If non-nil, this describes an additional menu for calc-keypad.
It should contain a list of three rows.
Each row should be a list of six keys.
Each key should be a list of a label string, plus a Calc command name spec.
A command spec is a command name symbol, a keyboard macro string, a
list containing a numeric entry string, or nil.
A key may contain additional specs for Inverse, Hyperbolic, and Inv+Hyp.")

(run-hooks 'calc-ext-load-hook)

(provide 'calc-ext)

;;; arch-tag: 1814ba7f-a390-49dc-9e25-a5adc205e97e
;;; calc-ext.el ends here