File: Solver.mac

package info (click to toggle)
maxima-sage 5.45.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 113,788 kB
  • sloc: lisp: 440,833; fortran: 14,665; perl: 14,369; tcl: 10,997; sh: 4,475; makefile: 2,520; ansic: 447; python: 262; xml: 59; awk: 37; sed: 17
file content (2404 lines) | stat: -rw-r--r-- 83,777 bytes parent folder | download | duplicates (6)
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
/******************************************************************************/
/*                                                                            */
/* SOLVER - THE NEXT GENERATION                                               */
/*                                                                            */
/* Copyright (C) 2000 : Eckhard Hennig, Ralf Sommer                           */
/* This library is free software; you can redistribute it and/or modify it    */
/* under the terms of the GNU Library General Public License as published     */
/* by the Free Software Foundation; either version 2 of the License, or (at   */
/* your option) any later version.                                            */
/*                                                                            */
/* This library 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          */
/* Library General Public License for more details.                           */
/*                                                                            */
/* You should have received a copy of the GNU Library General Public          */
/* License along with this library; if not, write to the Free Software        */
/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                  */
/******************************************************************************/
/* Credits: This program is based on many ideas from Henning Trispel's work   */
/*          on the EASY-Solver in 1991.                                       */
/*                                                                            */
/******************************************************************************/
/* Author(s)    : Eckhard Hennig, Ralf Sommer                                 */
/* Project start: 19.01.1994                                                  */
/* Completed    : 16.07.1994                                                  */
/* last change  : 29.06.1995                                                  */
/* Time         : 09:26                                                       */
/******************************************************************************/
/* Changes      : ||||| ||||| ||||| |||                                       */
/******************************************************************************/
/* Modified by  : Dan Stanger dan.stanger@ieee.org to work under maxima       */
/******************************************************************************/
load( "solver/linsolve.mac" )$
load( "solver/slvrtbox.mac" )$
load( "solver/slvrmsgs.mac" )$
load( "solver/misc.mac" )$

put( 'SOLVER, 1, 'Version )$

SetVersion(
  /* KEY    = */ 'SOLVER,
  'MODULE      = "SOLVER",
  'DESCRIPTION = "Symbolic solver for parametric systems of equations.",
  'AUTHORS     = "Eckhard Hennig, Ralf Sommer",
  'DATE        = "19.01.1994",
  'LASTCHANGE  = "29.06.1995",
  'TIME        = "09:26",
  'PLAN        = "Add a-priori transforms"
)$


/******************************************************************************/
/* last change: 29.06.1995                                                    */
/* Time       : 09:26                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Option variable Solver_Valuate_All_Nonlin_Vars added.         */
/*              Bug in ValuationSolver removed: rhs variables of solutions    */
/*              were not added to the list of variables.                      */
/******************************************************************************/
/* last change: 28.05.1995                                                    */
/* Time       : 11:59                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Solver break test added.                                      */
/******************************************************************************/
/* last change: 18.05.1995                                                    */
/* Time       : 16:10                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Name conflict with AI keyword PARAMS removed.                 */
/******************************************************************************/
/* last change: 30.01.1995                                                    */
/* Time       : 21.42                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Removal of map( 'num, ... ) has revealed some side effects on */
/*              the list of equations in ValuationSolver. Bug repaired by     */
/*              inserting two additional copylist calls.                      */
/*              Arguments to both load commands above now written in lower-   */
/*              case letters to avoid problems with UNIX versions.            */
/******************************************************************************/
/* last change: 24.01.1995                                                    */
/* Time       : 16.33                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: mapping 'num to expressions caused some invalid solutions,    */
/*              therefore removed. Version property added.                    */
/******************************************************************************/
/* last change: 19.01.1995                                                    */
/* Time       : 13.04                                                         */
/* By         : Eckhard Hennig, Ralf Sommer                                   */
/* Description: Bug in ImmediateAssignments corrected, Function AppendImmed   */
/*              removed.                                                      */
/******************************************************************************/
/* last change: 19.01.1995                                                    */
/* Time       : 11.28                                                         */
/* By         : Eckhard Hennig, Ralf Sommer                                   */
/* Description: SLVRTBOX and SLVRMSGS now autoloading.                        */
/******************************************************************************/
/* last change: 17.01.1995                                                    */
/* Time       : 20.15                                                         */
/* By         : Eckhard Hennig, Ralf Sommer                                   */
/* Description: Option variables modified (underscores inserted according to  */
/*              R. Petti's suggestions)                                       */
/*              Function SetValuation added.                                  */
/******************************************************************************/
/* last change: 25.10.1994                                                    */
/* Time       : 12.46                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: errorMsg renamed to ErrMsg.                                   */
/******************************************************************************/
/* last change: 12.09.1994                                                    */
/* Time       : 11.50                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: errcatch wrapped around final fullratsimp.                    */
/******************************************************************************/
/* last change: 05.09.1994                                                    */
/* Time       : 15.59                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: mode_identity's inserted.                                     */
/******************************************************************************/
/* last change: 05.09.1994                                                    */
/* Time       : 15.21                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Number of linear equations is now correctly displayed.        */
/*              Bug in DumpToFile corrected.                                  */
/*              Numbers of solution sets are now printed by the postprocessor.*/
/******************************************************************************/
/* last change: 30.08.1994                                                    */
/* Time       : 13.45                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Single inconsistent solution paths are no longer returned.    */
/******************************************************************************/
/* last change: 29.08.1994                                                    */
/* Time       : 17.16                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Slight modification of Immediate Assignment Solver.           */
/*              Valuation property for sqrt added.                            */
/*              New option variable SolverDefaultValuation.                   */
/*              Change in Valuation. Now making use of the above option var.  */
/******************************************************************************/
/* last change: 26.08.1994                                                    */
/* Time       : 15.29                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Bug in Linear Solver removed.                                 */
/******************************************************************************/
/* last change: 25.08.1994                                                    */
/* Time       : 12.41                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: New option variable SolverRatSimpSols.                        */
/*              Linear Solver now calls the consistency check.                */
/*              Capabilities of the transforms in ValuationSolver strongly    */
/*              enhanced.                                                     */
/******************************************************************************/
/* last change: 24.08.1994                                                    */
/* Time       : 23.14                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Bug fixed in postprocessor: compound expressions are now cor- */
/*              rectly evaluated.                                             */
/*              Linear Solver now checks for remaining equations & variables, */
/*              and removes those "linear" eqs and vars which do not really   */
/*              belong to the "true" linear subsystem.                        */
/******************************************************************************/
/* last change: 11.08.1994                                                    */
/* Time       : 19.09                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Variable SolverDelEq2VarPref replaced by function variable    */
/*              SolverDelEq. Heuristic algorithm for linear equation          */
/*              extraction improved.                                          */
/******************************************************************************/
/* last change: 19.07.1994                                                    */
/* Time       : 18.01                                                         */
/* By         : Eckhard Hennig                                                */
/* Description: Bug in post processor corrected.                              */
/******************************************************************************/


/******************************************************************************/
/* Global variables for Solver                                                */
/******************************************************************************/

/******************************************************************************/
/* If Solver_Immed_Assign is true then the Solver searches the equations      */
/* for immediate assignments of the form variable = constant and immediately  */
/* inserts these constraints into the remaining equations.                    */
/******************************************************************************/

define_variable( Solver_Immed_Assign, true, boolean )$


/******************************************************************************/
/* Solver_Repeat_Immed controls whether the search for immediate assignments  */
/* is performed repeatedly until no more of them are found.                   */
/******************************************************************************/

define_variable( Solver_Repeat_Immed, true, boolean )$


/******************************************************************************/
/* Solver_Subst_Powers controls whether the Solver substitutes powers of a    */
/* variable by new symbols in one of the following cases:                     */
/* 1. var^n appears raised to exactly one power n                             */
/* 2. for all var^m in the equations : m=n*k , k integer                      */
/******************************************************************************/

define_variable( Solver_Subst_Powers, false, boolean )$


/******************************************************************************/
/* Solver_Incons_Params controls whether the Solver terminates when a non-    */
/* trivial equation containing only parameters is encountered. for example,   */
/* if A and B are defined as parameters and the solution process yields       */
/* A = B^2  then the Solver stops if Solver_Incons_Params is = 'BREAK. If set */
/* to 'ASK the Solver asks whether  A - B^2  is zero and continues if it is.  */
/* If set to 'IGNORE the Solver quietly assumes that the expression is zero   */
/* if it does not directly contradict with any of the assumptions made before.*/
/******************************************************************************/

define_variable( Solver_Incons_Params, 'ASK, any_check )$

put(
  'Solver_Incons_Params,
  lambda( [ x ],
    if not member( x, [ 'ASK, 'BREAK, 'IGNORE ] ) then
      ErrorHandler("InvIncPar", x, 'Fatal )
  ),
  'value_check
)$


/******************************************************************************/
/* Solver_Linear controls whether the Solver tries to find and solve blocks   */
/* of linear equations before submitting the remaining equations to the       */
/* heuristic valuation solver. This is useful if the equations to be solved   */
/* are known to contain a large linear part.                                  */
/******************************************************************************/

define_variable( Solver_Linear, true, boolean )$


/******************************************************************************/
/* If Solver_Repeat_Linear is true then the LinearSolver will be called       */
/* repeatedly until no more linear equations are found. If false then         */
/* LinearSolver will be called only once.                                     */
/******************************************************************************/

define_variable( Solver_Repeat_Linear, true, boolean )$


/******************************************************************************/
/* If Solver_Find_All_Linear_Vars is true then LinearSolver will try to find  */
/* linear equations with respect to all available variables and solve these   */
/* equations simultaneously. If false then LinearSolver will only search for  */
/* linear equations with respect to the variables passed over in the function */
/* call.                                                                      */
/******************************************************************************/

define_variable( Solver_Find_All_Linear_Vars, true, boolean )$


/******************************************************************************/
/* Solver_Assumptions contains constraints on the parameters which should be  */
/* checked by the user after the termination of Solver. These constraints     */
/* result from the parameter consistency check the behavior of which is       */
/* controlled by the setting of the option variable Solver_Incons_Params.     */
/* Any numerical solution of the equations obtained by assigning numerical    */
/* values to symbolic parameters should be checked for consistency with all   */
/* expressions in Solver_Assumptions.                                         */
/******************************************************************************/

define_variable( Solver_Assumptions, [], list )$


/******************************************************************************/
/* Solver_Del_Eq holds the name of a function which controls the behavior of  */
/* the heuristic search algorithm which extract linear equations from the     */
/* entire system of equations.                                                */
/******************************************************************************/

define_variable( Solver_Del_Eq, 'MakeSquareLinearBlocks, any )$


/******************************************************************************/
/* If Solver_Valuate_All_Nonlin_Vars is true then the ValuationSolver will    */
/* valuate the equations w.r.t. all remaining variables and not only w.r.t    */
/* variables which are being searched for at the current step.                */
/******************************************************************************/

define_variable( Solver_Valuate_All_Nonlin_Vars, false, boolean )$


/******************************************************************************/
/* Solver_Valuation_Strategy holds the name of the equation valuation         */
/* strategy called by the valuation solver to determine the order by which    */
/* the equations are to be solved.                                            */
/******************************************************************************/

define_variable( Solver_Valuation_Strategy, 'MinVarPathsFirst, any_check )$

put(
  'Solver_Valuation_Strategy,
  lambda(
    [ x ],
    if not FunctionP( x ) then
      ErrorHandler( "UndefStrat", x, 'Fatal )
  ),
  'value_check
)$


/******************************************************************************/
/* Solver_Default_Valuation contains the default valuation for arithmetic     */
/* operators. Whenever an operator is encountered for which no valuation has  */
/* been defined by SetProp( <operator>, 'Valuation, <valuation> ) this value  */
/* is taken for the formula complexity calculations.                          */
/******************************************************************************/

define_variable( Solver_Default_Valuation, 10, fixnum )$


/******************************************************************************/
/* Solver_Max_Len_Val_Order limits the length of the list of candidates for   */
/* the solve calls in ValuationSolver. A low value will usually increase the  */
/* efficiency of the valuation solver since, in general, the first or second  */
/* attempt to solve an equation (hopefully) succeeds.                         */
/******************************************************************************/

define_variable( Solver_Max_Len_Val_Order, 5, fixnum )$


/******************************************************************************/
/* Solver_Transforms is a list containing the names of functions which can be */
/* applied to an equation after a failed solve call. These functions must     */
/* take three arguments: the equation to be transformed, the variable to be   */
/* solved for, and a list of (probably implicit) solutions the Solver has     */
/* already found for the equation.                                            */
/******************************************************************************/

define_variable( Solver_Transforms, [], list )$


/******************************************************************************/
/* If Solver_Postprocess is set to false no postprocessing of the results will*/
/* be done. Instead, the solutions are displayed in the internal hierarchical */
/* list format. Useful for debugging purposes.                                */
/******************************************************************************/

define_variable( Solver_Postprocess, true, boolean )$


/******************************************************************************/
/* Solver_Backsubst controls the output format of Solver. If Solver_Backsubst */
/* is true then the result will be displayed with fully evaluated right-hand  */
/* sides for each variable. If the option variable is set to false then the   */
/* right-hand sides of the solutions may still contain references to some     */
/* of the other variables which have been solved for.                         */
/******************************************************************************/

define_variable( Solver_Backsubst, true, boolean )$


/******************************************************************************/
/* With Solver_Disp_All_Sols set to true all solutions will be displayed      */
/* including those for the variables which have been solved for in the        */
/* solution process but have not been explicitly asked for.                   */
/******************************************************************************/

define_variable( Solver_Disp_All_Sols, false, boolean )$


/******************************************************************************/
/* If Solver_RatSimp_Sols is true then the Solver Postprocessor will          */
/* fullratsimp the solutions before returning them.                           */
/******************************************************************************/

define_variable( Solver_RatSimp_Sols, true, boolean )$


/******************************************************************************/
/* If Solver_Dump_To_File is true then the ValuationSolver writes the         */
/* solutions and yet unsolved equations to a file after each iteration. This  */
/* might help if the Solver crashes.                                          */
/******************************************************************************/

define_variable( Solver_Dump_To_File, false, boolean )$


/******************************************************************************/
/* Solver_Dump_File contains the name of the file to which the dump is        */
/* written.                                                                   */
/******************************************************************************/

define_variable( Solver_Dump_File, "SOLVER.DMP", any )$


/******************************************************************************/
/* Solver_Break_Test holds the name of a function which is called immediately */
/* before attempting to solve an equation. This function then decides whether */
/* Solver should try to solve the equation or whether it should stop, e.g.    */
/* because the problem has become too complex. The arguments passed to the    */
/* Solver_Break_Test are 1. the equation, 2. the variable, 3. the valuation.  */
/* The Solver stops if the Solver_Break_Test returns true, it continues if    */
/* the return value is false.                                                 */
/******************************************************************************/

define_variable( Solver_Break_Test, 'SolverJustDoIt, any )$


/******************************************************************************/
/* Solver, main program                                                       */
/******************************************************************************/

Solver( Equations, [ SolverParams ] ) := (

  mode_declare(
    [ Equations, SolverParams ], list
  ),

  block(

    [
      Variables,       /* List of variables to be solved for            */
      UserVars,        /* List of variables specified by user           */
      Parameters,      /* List of symbols to be used as parameters      */
      Expressions,     /* List of compound expressions to be solved for */
      PowerSubst,      /* List of substituted symbols for powers        */

      Solutions,       /* List of solutions found by Solver             */
      RemainingEqs,    /* List of remaining equations                   */

      /* Assumptions made in linear solver should be local  */
      Solver_Assumptions : [],

      Active
    ],

    mode_declare(
      [
        Variables, UserVars, Parameters, Expressions, PowerSubst,
        Solutions
      ], list,
      Active, boolean
    ),

    /* No assumptions to start with */ErrorHandlerSolver_Assumptions : [],

    /* Initialize list of solutions */
    Solutions : [],

    /* Do all necessary preprocessing */
    map(
      lambda([x,y],x::y),
      [
        'Equations, 'SolverParams, 'Variables, 'Parameters,
        'Expressions, 'PowerSubst, 'UserVars
      ],
      SetupSolver( Equations, SolverParams )
    ),

    if MsgLevel = 'DEBUG then
      display(
        Equations, SolverParams, Variables, Parameters, Expressions,
        PowerSubst, UserVars, Solutions
      ),

    block(
      [],

      /* Search for and apply immediate assignments */

      Active : Solver_Immed_Assign,
      while Active do (
        map(
          lambda([x,y],x::y),
          [ 'Active, 'Solutions, 'Equations, 'Variables ],
          ImmediateAssignments( Solutions, Equations, Variables, Parameters )
        ),

        Active : Active and Solver_Repeat_Immed,

        if MsgLevel = 'DEBUG then
          display(
            Equations, SolverParams, Variables, Parameters, Expressions,
            PowerSubst, UserVars, Solutions
          )
      ),

      if Empty( Equations ) or Empty( Variables ) then
        return( false ),

      Equations : map(
        lambda(
          [ Eq ],
          fullratsimp( lhs( Eq ) - rhs( Eq ) )
        ),
        Equations
      ),

      /* Find and solve linear equations */

      Active : Solver_Linear,
      while Active do (
        map(
          lambda([x,y],x::y),
          [ 'Active, 'Solutions, 'Equations, 'Variables ],
          LinearSolver( Solutions, Equations, Variables, Parameters )
        ),

        Active : Active and Solver_Repeat_Linear,

        if MsgLevel = 'DEBUG then
          display(
            Equations, SolverParams, Variables, Parameters, Expressions,
            PowerSubst, UserVars, Solutions
          )
      ),

      if Empty( Equations ) or Empty( Variables ) then
        return( false ),


      if Solver_Valuate_All_Nonlin_Vars then
        Variables : Union(
          SetDifference( listofvars( Equations ), Parameters ),
          Variables
        ),

      /* apply valuation strategies to solve the nonlinear equations. */
      map(
        lambda([x,y],x::y),
        [ 'Active, 'Solutions, 'Equations, 'Variables ],
        ValuationSolver( Solutions, Equations, Variables, Parameters )
      )

    ), /* END block */


    /* Return the solutions and the unsolved equations */

    if Solver_Postprocess then
      return( PostProcess( Solutions, UserVars, Expressions, PowerSubst ) )
    else
      return( Solutions )
  )
)$


/******************************************************************************/
/* TerminateSolver terminates the Solver.                                     */
/******************************************************************************/

TerminateSolver() := error( ErrMsg["SolvrTerm"] )$


/******************************************************************************/
/* SetupSolver preprocesses the equations and optional parameters before      */
/* submitting them to the Solver. The equations are checked if they are       */
/* really equations, and all equations of the form  NUMBER = NUMBER  or       */
/* f( PARAMETERS ) = g( PARAMETERS )  are checked for consistency and then    */
/* dropped.                                                                   */
/******************************************************************************/

SetupSolver( Equations, SolverParams ) := (

  mode_declare(
    [ Equations, SolverParams ], list
  ),

  block(

    [
       i, AllVars, Var, SubstSym, Power,
       Variables, Parameters, Expressions,
       PowerSubst, UserVars
    ],

    mode_declare(
      i, fixnum,
      [
        AllVars, Power, Variables, Parameters, Expressions,
        PowerSubst, UserVars
      ], list,
      [ Var, SubstSym ], any
    ),


    Expressions : [],
    PowerSubst  : [],
    Parameters  : [],


    /* Make sure that Equations is a list. Abort if it is not. */

    if not listp( Equations ) then
      ErrorHandler( "EqsNotLst", Equations, 'Fatal ),

    /* delete all entries from Equations which are not equations */

    Equations : sublist( Equations, 'EquationP ),

    /* Convert all floating-point numbers to rational numbers. If this was */
    /* not done then rounding errors could fool the consistency check.     */

    Equations : map( 'rat, Equations ),

    /* Process the optional arguments to Solver */

    if not Empty( SolverParams ) then (

      Variables : SolverParams[1],

      /* Make sure that Variables is a list. Abort if it is not. */

      if not listp( Variables ) then
        ErrorHandler( "VarNotLst", Variables, 'Fatal ),

      /* delete multiple occurrences of identical symbols */

      Variables : Setify( Variables ),

      PrintMsg( 'DETAIL, SolverMsg["VarsAre"], Variables ),

      if length( SolverParams ) > 1 then (

        Parameters : SolverParams[2],

        /* Make sure that Parameters is a list. Abort if it is not. */

        if not listp( Parameters ) then
          ErrorHandler( "ParNotLst", Parameters, 'Fatal ),

        /* delete multiple occurrences of identical symbols */

        Parameters : Setify( Parameters ),

        PrintMsg( 'DETAIL, SolverMsg["ParsAre"], Parameters )
      )

    ),


    /* Check if Variables and Parameters are disjoint sets of symbols */

    if not DisjointP( Variables, Parameters ) then
      ErrorHandler(
        "VarParConfl", Intersection( Variables, Parameters ), 'Fatal
      ),


    /* Make a list of all variables to be solved for. This list may contain */
    /* more symbols than Variables when either no SolverParams have been    */
    /* given or when the user has specified only a subset of all existing   */
    /* symbols.                                                             */

    AllVars : SetDifference( listofvars( Equations ), Parameters ),

    /* solve for all available variables if Variables is empty. */

    if Empty( Variables ) then
      Variables : AllVars,


    /* Check all those equations which contain only equations of the form  */
    /* NUMBER = NUMBER or which contain only parameters and none of the    */
    /* variables of interest for consistency. Remove consistent equations  */
    /* and store the assumptions made in the list Solver_Assumptions.      */

    Equations : ParamConsistency( Equations, Parameters ),

    /* Abort if no equations are left after the above step. */

    if Empty( Equations ) or Empty( Variables ) then (
      PrintMsg( 'SHORT, SolverMsg["NoEqOrVar"] ),
      return( [ [], SolverParams, Variables, Parameters, [], [], [] ] )
    ),

    /* If there are compound expressions to be solved for then the solver     */
    /* tries to solve for the variables contained in them first.              */
    /* Subsequently the expressions are rebuilt from the solutions of         */
    /* these variables and the parameters.                                    */

    for i thru length( Variables ) do

      /* Search the variable list for non-atomic expressions. */

      if not atom( Var : Variables[i] ) then (

        /* append expression to the expression list */
        Expressions : endcons( Var, Expressions ),

        /* Insert the variables in the expression into the list of variables  */
        /* but keep the parameters out.                                       */
        Variables[i] : SetDifference( listofvars( Var ), Parameters ),

        PrintMsg( 'SHORT, SolverMsg["TrySolve4"], Variables[i] ),
        PrintMsg( 'SHORT, SolverMsg["Solve4Exp"], Var )
      ),

    /* Make a list of all the variables the user actually wants to know */
    UserVars : sublist( Variables, 'atom ),

    /* Flatten the list of variables and make it a set. */
    Variables : Setify( Flatten( Variables ) ),


    /* If Solver_Subst_Powers is true then substitute powers by new symbols */

    if Solver_Subst_Powers then (

      PrintMsg( 'SHORT, SolverMsg["SubstPwrs"] ),

      for Var in AllVars do (

        /* get all powers of Var in Equations. Convert negative powers to */
        /* positive ones.                                                 */
        Power : Setify( abs( ListOfPowers( Equations, Var ) ) ),

        /* If there is more than one power of Var then substitute each  */
        /* var^m only if all m are integer multiples of the lowest      */
        /* power > 0. The check is done by examining the modulus of all */
        /* powers with respect to the lowest power.                     */

        if
          not member( 'false, map( 'integerp, Power ) )
          and
          Power[1] # 1
        then

          if ( length( Power ) = 1 ) or

            block(
              [ Modulus : Power[1] ],
              not member(
                'false,
                map( 'ZeroP, totaldisrep( rat( rest( Power ) ) ) )
              )
            )

          then (
            /* Make a new symbol for var^power */
            SubstSym : concat( Var, "^", Power[1] ),

            /* Store a reference to the original term in an assoc list */
            PowerSubst : endcons( SubstSym = Var^Power[1], PowerSubst ),

            /* Substitute the new symbol for the original term */
            Equations  : ratsubst( SubstSym, Var^Power[1], Equations ),
            Variables  : subst( SubstSym, Var, Variables ),

            /* Notify user */
            PrintMsg( 'DETAIL, SolverMsg["subst"], Var^Power )
          )

      ) /* END for Var in AllVars */

    ), /* END if Solver_Subst_Powers */

    return(
      [
        Equations, SolverParams, Variables, Parameters, Expressions,
        PowerSubst, UserVars
      ]
    )
  )
)$


/******************************************************************************/
/* ParamConsistency checks equations of the form NUMBER = NUMBER and equa-    */
/* tions which contain only parameters for consistency. If the system cannot  */
/* determine whether a parametric expression is zero then the user is optio-  */
/* nally asked to supply the required information. The assumptions made are   */
/* stored in the list Solver_Assumptions.                                     */
/******************************************************************************/

ParamConsistency( Eqs, Pars, [ Action ] ) := (

  mode_declare(
    [ Eqs, Pars ], list,
    Action, any
  ),

  block(

    [ Eq, lhsminusrhs, i, consistent ],

    mode_declare(
      [ Eq, lhsminusrhs ], any,
      i, fixnum,
      consistent, boolean
    ),

    PrintMsg( 'SHORT, SolverMsg["ConsChk"] ),

    if Empty( Action ) then
      Action : 'BREAK
    else
      Action : Action[1],

    consistent : true,

    for i thru length( Eqs ) do (

      Eq : Eqs[i],

      /* Does the equation contain only numbers or parameters? */
      if Empty(
        SetDifference( listofvars( Eq ), Pars )
      )
      then (

        /* If so, check for consistency */
        lhsminusrhs :  expand( lhs( Eq ) - rhs( Eq ) ),

        /* Test if difference of both rhs's is zero */
        if lhsminusrhs # 0 then

          if SolverAssumeZero( lhsminusrhs ) then
            PrintMsg( 'SHORT, SolverMsg["Assum"], lhsminusrhs = 0 )
          else
            if Action = 'BREAK then (
              /* Abort if difference is non-zero */
              PrintMsg( 'SHORT, SolverMsg["Incons"], lhs( Eq ) = rhs( Eq ) ),
              TerminateSolver()
            )
            else
              return( consistent : false ),

        /* Kill the now redundant equation */
        if Action = 'BREAK then
          Eqs[i] : []

      ) /* END if Empty */

    ), /* END for i */

    if consistent then
      PrintMsg( 'SHORT, SolverMsg["NoneFnd"] ),

    if Action = 'BREAK then
      return( delete( [], Eqs ) )
    else
      return( consistent )
  )
)$


/******************************************************************************/
/* SolverAssumeZero checks whether Expression is (assumed to be) equal to     */
/* zero. If it isn't, the function returns false or asks the user for his     */
/* decision. New assumptions are appended to the list Solver_Assumptions.     */
/******************************************************************************/

SolverAssumeZero( Expression ) := (

  mode_declare(
    Expression, any
  ),

  block(

    [ AssumptionExists, i ],

    mode_declare(
      i, fixnum,
      AssumptionExists, boolean
    ),

    /* Return false immediately if Expression is a number # 0 or if  */
    /* Solver_Incons_Params is set to 'BREAK.                          */

    if
      Empty( listofvars( Expression ) )
      or ( Solver_Incons_Params = 'BREAK )
    then
      return( false ),

    /* Do a simple check to find out whether assumption already exists:    */
    /* Zero is substituted for Expression in the stored assumption. If the */
    /* result is zero then the assumption already exists.                  */

    AssumptionExists : false,
    for i thru length( Solver_Assumptions ) while not AssumptionExists do
      if
        fullratsimp(
          ratsubst( 0, Expression, lhs( Solver_Assumptions[i] ) )
        ) = 0
      then
        AssumptionExists : true,

    /* If the expression is not yet assumed to be equal to zero, ask the user */
    /* to decide whether it is.                                               */

    if not AssumptionExists then

      if
        ( Solver_Incons_Params = 'ASK )
        and
        ( AskZeroNonzero( Expression ) # 'zero )
      then
        return( false )
      else
        /* If difference is equal to zero or assumed to be so then store    */
        /* the constraint in the global list Solver_Assumptions. So the user*/
        /* has access to all assumptions made during the solution process   */
        /* and can check any numerical solutions for consistency with the   */
        /* assumptions.                                                     */
        Solver_Assumptions : endcons(  Expression = 0, Solver_Assumptions )

    else
      PrintMsg( 'DETAIL, SolverMsg["AssmFnd"], Expression = 0 ),

    return( true )
  )
)$

AskZeroNonzero( Expression ) :=
  if equal( x, 0 ) = true then 'zero
  else block(
    [s : 'pnz],
    while s#'zero and s#'nonzero do (
      s : read( "Is", Expression, "zero or nonzero?" )
    ),
    s
  )$

/******************************************************************************/
/* ListOfPowers returns the list of powers # 0 of a variable in a set of      */
/* equations.                                                                 */
/******************************************************************************/

ListOfPowers( Eqs, Var ) := (

  mode_declare(
    Eqs, list,
    Var, any
  ),

  delete(
    0,
    apply(
      'Union,
      map(
        lambda(
          [ Eq ],
          Powers( expand( lhs( Eq ) - rhs( Eq ) ), Var )
        ),
        Eqs
      )
    )
  )
)$


/******************************************************************************/
/* ImmediateAssignments directly applies all immediate assignments of the     */
/* form  var = rhs...  before the actual Solver is called.                    */
/******************************************************************************/

ImmediateAssignments( Solutions, RemainingEqs, Variables, Parameters ) := (

  mode_declare(
    [ Solutions, RemainingEqs, Variables, Parameters ], list
  ),

  block(

    [ i, Vars, AssignmentMade, Left, Right ],

    mode_declare(
      i, fixnum,
      Vars, list,
      AssignmentMade, boolean,
      [ Left, Right ], any
    ),

    PrintMsg( 'SHORT, SolverMsg["SrchImmed"] ),

    AssignmentMade : false,

    for i thru length( RemainingEqs ) do block(
      [],

      Left  : lhs( RemainingEqs[i] ),
      Right : rhs( RemainingEqs[i] ),

      /* Scan the equations for simple assignments of the form  X = Expr or   */
      /* Expr = X  and apply this assignment only if X is not a parameter and */
      /* if Expr contains only numbers or parameters.                         */
      /* Remark: Equations containing only parameters have already been remo- */
      /* ved from the equation list in SetupSolver.                           */

      if symbolp( Left ) then (

        Vars : listofvars( Right ),

        /* Check if lhs is an isolated variable and make sure that there are */
        /* only parameters on the right-hand side.                           */

        if freeof( Left, Right )
          and Empty( SetDifference( Vars, Parameters ) )
        then (

          if assoc( Left, Solutions ) = false then (
            PrintMsg(
              'DETAIL,
              SolverMsg["Assign"], totaldisrep( Left = Right )
            ),

            if member( Left, map( lhs, Solutions ) ) then (
              if assoc( Left, Solutions ) # Right then (
                PrintMsg( 'SHORT, SolverMsg["Incons"], Left = Right ),
                TerminateSolver()
              )
            )
            else
              Solutions : endcons( Left = Right, Solutions ),
            RemainingEqs[i] : [],

            AssignmentMade : true
          ),

          /* This return prevents Macsyma from entering the next if statement */
          /* which must only be executed when the current outer if statement  */
          /* has not been entered.                                            */
          return( 'DONE )
        )
      ),

      if symbolp( Right ) then (

        Vars : listofvars( Left ),

        if freeof( Right, Left )
          and Empty( SetDifference( Vars, Parameters ) )
        then (
        
          if assoc( Right, Solutions ) = false then (
            PrintMsg(
              'DETAIL,
              SolverMsg["Assign"], totaldisrep( Right = Left )
            ),
          
            if member( Right, map( lhs, Solutions ) ) then (
              if assoc( Right, Solutions ) # Left then (
                PrintMsg( 'SHORT, SolverMsg["Incons"], Left = Right ),
                TerminateSolver()
              )
            )
            else
              Solutions : endcons( Right = Left, Solutions ),
            RemainingEqs[i] : [],

            AssignmentMade : true
          )
          
        )
      )
    ), /* END for i thru length */

    if AssignmentMade then (

      /* delete the used equations from the list */
      RemainingEqs : delete( [], RemainingEqs ),

      if not Empty( RemainingEqs ) then (

        /* Remove all the variables from the working list which have been */
        /* determined by an immediate assignment.                         */
        Variables : SetDifference( Variables, map( 'lhs, Solutions ) ),

        /* Evaluate the remaining equations with the constraints */
        RemainingEqs : ev( RemainingEqs, Solutions ),

        /* Do a parameter consistency check */
        RemainingEqs : ParamConsistency( RemainingEqs, Parameters )
      )

      else
        PrintMsg( 'SHORT, SolverMsg["NoEqTerm"] )

    )
    else
      PrintMsg( 'SHORT, SolverMsg["NoImmed"] ),

    /* END if AssignmentMade */

    return( [ AssignmentMade, Solutions, RemainingEqs, Variables ] )
  )
)$


/******************************************************************************/
/* LinearSolver extracts blocks of linear equations from an arbitrary system  */
/* of equations by a heuristic searching strategy and solves the linear block */
/* if there is one.                                                           */
/******************************************************************************/

LinearSolver( Solutions, Equations, Variables, Parameters ) := (

  mode_declare(
    [ Solutions, Equations, Variables, Parameters ], list
  ),

  block(

    [
      CoeffMatrix, ValuationMatrix, ActiveVars,
      LinEqNos, LinVarNos, NewVars, LinSolVars,
      LinearEqs, LinearVars, LinearSolutions,
      i, j, me, mv, NumVars, NumEqs,
      MaxValVar, MaxValEq,
      EqValuation, VarValuation,
      rhsExpressions,

      linsolvewarn             : false,
      linsolve_params          : false,
      Solve_Inconsistent_Error : false,

      EqWasLast : false
    ],

    mode_declare(
      [ CoeffMatrix, ValuationMatrix ], any,
      [
        LinEqNos, LinVarNos, ActiveVars, LinearEqs, LinearVars, LinSolVars,
        EqValuation, VarValuation, LinearSolutions, NewVars, rhsExpressions
      ], list,
      [ i, j, me, mv, NumVars, NumEqs, MaxValVar, MaxValEq ], fixnum
    ),

    if Empty( Equations ) then (

      if Empty( Variables ) then
        PrintMsg( 'SHORT, SolverMsg["AllSolved"] )
      else
        PrintMsg( 'SHORT, SolverMsg["NoEqLeft"], Variables ),

      return( [ false, Solutions, Equations, Variables ] )
    )
    else if Empty( Variables ) then (
      PrintMsg( 'SHORT, SolverMsg["EqLeft"] ),
      return( [ false, Solutions, Equations, Variables ] )
    ),

    PrintMsg( 'SHORT, SolverMsg["SrchLinEq"] ),

    /* Make a list of all remaining variables */
    NewVars : listofvars( Equations ),

    if Solver_Find_All_Linear_Vars then (

      /* The following rather weird commands put the variables into the order */
      /* [ variables to be currently solved for, other variables ]. If the    */
      /* current variables are linear variables then it is more likely that   */
      /* they will have explicit solutions if they are located in the left    */
      /* half of the system of equations. Otherwise the will more likely be   */
      /* used as parameters of the null space (if there is one).              */

      ActiveVars : append(
        Intersection( Variables, NewVars ),
        SetDifference( NewVars, append( Variables, Parameters ) )
      ),
      LinearEqs : Equations,
      Equations : []
    )
    else (
      ActiveVars : Intersection( Variables, NewVars ),
      LinearEqs : [],
      for i thru length( Equations ) do
        if
          not Empty( Intersection( listofvars( Equations[i] ), ActiveVars ) )
        then (
          LinearEqs : endcons( Equations[i], LinearEqs ),
          Equations[i] : []
        ),
      Equations : delete( [], Equations )
    ),

    PrintMsg( 'SHORT, SolverMsg["wrt"], ActiveVars ),

    /* Set up the complete coefficient matrix w.r.t. all variables */
    CoeffMatrix : ComplCoeffMatrix( LinearEqs, ActiveVars ),

    /* The valuation matrix contains a 1 at each Position where a variable */
    /* appears in a nonlinear form, and 0's otherwise.                     */

    ValuationMatrix : matrixmap(
      lambda(
        [ x ],
        if x = 'false then
          1
        else
          0
      ),
      CoeffMatrix
    ),

    /* EqValuation contains the number of nonlinear variables for each eq. */
    EqValuation : map(
      lambda( [ Row ], apply( "+", Row ) ),
      ListMatrix( ValuationMatrix )
    ),

    /* VarValuation contains for all vars the number of equations in which */
    /* var[i] appears in a nonlinear form.                                 */
    VarValuation : map(
      lambda( [ Row ], apply( "+", Row ) ),
      ListMatrix( transpose( ValuationMatrix ) )
    ),

    LinEqNos  : makelist( i, i, 1, NumEqs : length( EqValuation ) ),
    LinVarNos : makelist( i, i, 1, NumVars : length( VarValuation ) ),


    /* Remove nonlinear equations and/or variables until only a linear    */
    /* block remains, i.e. for all i, j VarValuation[i] = 0 and           */
    /* EqValuation[j] = 0.                                                */

    while
      ( apply( "+", VarValuation ) # 0 )
      and
      ( apply( "+", EqValuation ) # 0 )
    do (

      /* Determine maximum equation valuation and number of corresponding */
      /* equation.                                                        */

      me : 0,
      MaxValEq : -1,
      for i thru NumEqs do
        if EqValuation[i] > MaxValEq then (
          me : i,
          MaxValEq : mode_identity( fixnum, EqValuation[i] )
        ),

      /* Determine maximum variable valuation and number of corresponding */
      /* variable.                                                        */

      mv : 0,
      MaxValVar : -1,
      for j thru NumVars do
        if VarValuation[j] > MaxValVar then (
          mv : j,
          MaxValVar : mode_identity( fixnum, VarValuation[j] )
        ),


      if apply( Solver_Del_Eq, [ MaxValEq, MaxValVar ] ) then (
        i : me,

        for j thru NumVars do (
          VarValuation[j] : VarValuation[j] - ValuationMatrix[i, j],
          ValuationMatrix[i, j] : 0
        ),

        /* Mark equation as deleted */
        EqValuation[i] : 0,
        LinEqNos[i]    : 0
      )
      else (
        j : mv,

        for i thru NumEqs do (
          EqValuation[i] : EqValuation[i] - ValuationMatrix[i, j],
          ValuationMatrix[i, j] : 0
        ),

        /* Mark variable as deleted */
        VarValuation[j] : 0,
        LinVarNos[j]    : 0
      )

    ), /* END while */

    /* Make list of linear equations */
    LinEqNos  : delete( 0, LinEqNos ),

    /* Make list of linear variables */
    LinVarNos : delete( 0, LinVarNos ),

    if Empty( LinEqNos ) or Empty( LinVarNos ) then (
      PrintMsg( 'SHORT, SolverMsg["NoLinEqs"] ),
      return( [ false, Solutions, append( LinearEqs, Equations ), Variables ] )
    ),

    /* Extract linear equations. append all nonlinear equations to Equations */
    /* again.                                                                */
    for i thru length( LinearEqs ) do
      if not member( i, LinEqNos ) then (
        Equations : endcons( LinearEqs[i], Equations ),
        LinearEqs[i] : []
      ),

    LinearEqs : delete( [], LinearEqs ),


    /* Extract linear variables. Since the extraction of linear equations may */
    /* also have removed linear variables (the coefficients are now 0) it is  */
    /* necessary to intersect the set of the linear variables with the set of */
    /* those variables which actually appear in the linear equations.         */
  
    LinearVars : Intersection(
      map( lambda( [ i ], ActiveVars[i] ), LinVarNos ),
      listofvars( LinearEqs )
    ),

    /* By analogy, the same applies to the linear equations. Thus, keep only  */
    /* those equations which still contain any of the linear variables.       */

    for i thru length( LinearEqs ) do
      if DisjointP( listofvars( LinearEqs[i] ), LinearVars ) then (
        Equations : endcons( LinearEqs[i], Equations ),
        LinearEqs[i] : []
      ),

    LinearEqs : delete( [], LinearEqs ),

    /* Return if no linear equations are left */

    if Empty( LinearVars ) or Empty( LinearEqs ) then (
      PrintMsg( 'SHORT, SolverMsg["NoLinEqs"] ),
      return( [ false, Solutions, append( LinearEqs, Equations ), Variables ] )
    ),

    PrintMsg(
      'SHORT,
      SolverMsg["Found"], length( LinearEqs ), SolverMsg["LinEqs"],
      length( LinearVars ), SolverMsg["LinVars"]
    ),
    PrintMsg( 'SHORT,  SolverMsg["VarsAre"],  LinearVars ),
    PrintMsg( 'DETAIL, SolverMsg["EqsAre"],   LinearEqs ),
    PrintMsg( 'SHORT,  SolverMsg["SolvLinEq"] ),

    rhsExpressions : [],
    Solve_Inconsistent_Eqn_Nos : [ 0 ],

    while not Empty( Solve_Inconsistent_Eqn_Nos ) do (

      /* solve the linear equations */
      LinearSolutions : LinsolveM( LinearEqs , LinearVars ),

      /* Check for "inconsistent" equations. */
      if not Empty( Solve_Inconsistent_Eqn_Nos ) then (

        PrintMsg( 'DEBUG, SolverMsg["Incons"], Solve_Inconsistent_Eqn_Nos ),

        /* Remove "inconsistent" equations */
        for i in Solve_Inconsistent_Eqn_Nos  do
            LinearEqs[i] : [],

        LinearEqs : delete( [], LinearEqs ),

        /* append rhs = 0 to Solver_Assumptions if rhs contains only */
        /* parameters.                                               */
        rhsExpressions : ParamConsistency(
          Solve_Inconsistent_Terms, Parameters
        )

      )

    ),

    /* append rhs's which have led to "inconsistencies" but still   */
    /* contain variables to the list of equations.                  */
    Equations : append( Equations, rhsExpressions ),

    PrintMsg( 'DETAIL, SolverMsg["Solutions"], LinearSolutions ),

    /* Insert the solutions from linsolve into the remaining equations */
    Equations : ParamConsistency(
        fullratsimp( ev( Equations, LinearSolutions ) ),
        Parameters
    ),

    /* append the linear solutions to the list of solutions */
    Solutions : append( Solutions, LinearSolutions ),

    /* append all variables to the working list which appear on the rhs's of  */
    /* the linear solutions. delete all variables which have been solved for. */

    /* Linear variables for which a solution has been obtained. */
    LinSolVars : map( 'lhs, LinearSolutions ),

    /* Linear variables which are free parameters of the null space. */
    LinearVars : SetDifference( LinearVars, LinSolVars ),

    /* append all those variables which are parameters of the null space of */
    /* the linear equations and which do not appear in the remaining        */
    /* equations to the list of parameters.                                 */

    for Var in LinearVars do
      if freeof( Var, Equations ) then (
        PrintMsg( 'SHORT, SolverMsg["FreeVar2Par"], Var ),
        Parameters : endcons( Var, Parameters ),
        Variables  : delete( Var, Variables )
      ),

    NewVars : SetDifference(
      listofvars( map( 'rhs, LinearSolutions ) ),
      Parameters
    ),

    Variables : Union(
      SetDifference( Variables, LinSolVars ),
      NewVars
    ),

    return( [ true, Solutions, Equations, Variables ] )
  )
)$


/******************************************************************************/
/* The following strategies decide whether the linear solver should delete a  */
/* nonlinear equation or a nonlinear variable from the system while searching */
/* for linear subblocks of equations.                                         */
/******************************************************************************/

define_variable( EqWasLast, false, boolean )$

MakeSquareLinearBlocks( ValEq, ValVar ) := (

  mode_declare(
    [ ValEq, ValVar ], fixnum
  ),

  if ValEq = ValVar then
    EqWasLast : not EqWasLast
  else
    if ValEq > ValVar then
      EqWasLast : true
    else
      EqWasLast : false
)$


DelEqBeforeVar( ValEq, ValVar ) := (

  mode_declare(
    [ ValEq, ValVar ], fixnum
  ),

  if ValEq >= ValVar then
    true
  else
    false
)$


/******************************************************************************/
/* ComplCoeffMatrix returns a matrix whose row size is equal to the number of */
/* equations and whose column size is equal to the number of variables. The   */
/* entry at Position [i,j] is RatCoeff( equation[i], variable[j] ) if         */
/* equation[i] is linear w.r.t. variable[j] and 'false if equation[i] is      */
/* nonlinear w.r.t. variable[j].                                              */
/******************************************************************************/

ComplCoeffMatrix( Eqs, ActiveVars ) := (

  mode_declare(
    [ Eqs, ActiveVars ], list
  ),

  block(


    apply(

      'matrix,

      /* for each equation do */
      map(

        lambda(
          [ Eq ],

          /* for each variable do */
          map(
            lambda(
              [ Var ],
              block(
                [ rc ],
                rc  : LinCoeff( Eq, Var ),

                /* Return the RatCoeff only if it contains none of the active */
                /* variables or if the equation doesn't contain var at all.   */

                if
                  ( ( rc # 0 ) and DisjointP( listofvars( rc ), ActiveVars ) )
                  or
                  freeof( Var, Eq )
                then
                  rc
                else
                  false
              )
            ),

            ActiveVars
          ) /* END lambda( [ Var ] ) */

        ), /* END lambda( [ Eq ] ) */


        /* map target: Transform all equations into homogeneous form. */

        map(
          lambda(
            [ Eq ],
            fullratsimp( expand( lhs( Eq ) - rhs( Eq ) ) )
          ),
          Eqs
        )


      ) /* END map( lambda( [ Eq ] ) ) */

    ) /* END apply */
  )
)$


/******************************************************************************/
/* LinCoeff returns the linear coefficient of Var within Eq if Var appears    */
/* raised to the first power only.                                            */
/******************************************************************************/

LinCoeff( Eq, Var ) := (

  mode_declare(
    [ Eq, Var ], any
  ),

  block(

    [ BCoeff ],

    mode_declare(
      BCoeff, list
    ),

    if ListOfPowers( [ Eq ], Var ) = [ 1 ] then (
      BCoeff : bothcoeff( Eq, Var ),
      if freeof( Var, second( BCoeff ) ) then
        return( first( BCoeff ) )
    ),

    return( 0 )
  )
)$


/******************************************************************************/
/* ValuationSolver                                                            */
/******************************************************************************/

ValuationSolver( Solutions, Equations, Variables, Parameters ) := (

  mode_declare(
    [ Solutions, Equations, Variables, Parameters ], list
  ),

  block(

    [
      VarPaths, ValMatrix, Eq, Var, Trans, TempEq, TransEq,
      SolveOrder, SolveInfo, Transform, Solution, SolCheck,
      Status, Solved, Failed, UniqueSol, TryToSolve, CheckSol,
      i, k
    ],

    mode_declare(
      [ VarPaths, ValMatrix, Eq, Var, Trans, TempEq, TransEq ], any,
      [ SolveOrder, SolveInfo, Transform, Solution, SolCheck ], list,
      [ Status, Solved, Failed, UniqueSol, TryToSolve, CheckSol ], boolean,
      [ i, k ], fixnum
    ),


    UniqueSol : true,

    LOOP,

    PrintMsg( 'SHORT, SolverMsg["Chk4RemEq"] ),

    if Empty( Equations ) then (

      if Empty( Variables ) then
        PrintMsg( 'SHORT, SolverMsg["AllSolved"] )
      else
        PrintMsg( 'SHORT, SolverMsg["NoEqLeft"], Variables ),

      Status : false

    )
    else if Empty( Variables ) then (
      PrintMsg( 'SHORT, SolverMsg["EqLeft"] ),
      Status : false
    )
    else (

      PrintMsg(
        'SHORT,
        length( Equations ), SolverMsg["Eqs"],
        length( Variables ), SolverMsg["Vars"]
      ),
      PrintMsg( 'DETAIL, SolverMsg["VarsAre"], Variables ),
      PrintMsg( 'DEBUG, SolverMsg["EqsAre"], Equations ),

      /* Dump solutions and remaining equations to file if requested. */
      if Solver_Dump_To_File then
        DumpToFile( Solutions, Equations, Variables ),

      if ( length( Variables ) = 1 ) and ( length( Equations ) = 1 ) then
        SolveOrder : [ [ 1, 1, "(irrelevant)" ] ]

      else (
        PrintMsg( 'SHORT, SolverMsg["ValStrat"] ),

        /* Set up the valuation matrices. */
        VarPaths  : OccurrenceMatrix( Equations, Variables ),
        ValMatrix : ValuationMatrix( Equations, Variables ),

        /* Determine an order by which the equations should be solved. */
        SolveOrder : apply( Solver_Valuation_Strategy, [ VarPaths, ValMatrix ] )
      ),

      Solved : false,

      unless Solved or Empty( SolveOrder ) do (

        SolveInfo : Pop( SolveOrder ),
        if listp(SolveInfo[1]) then SolveInfo : first(SolveInfo),
        k   : mode_identity( fixnum, first( SolveInfo ) ),
        Eq  : part( Equations, k ),
        Var : Variables[ second( SolveInfo ) ],

        PrintMsg( 
          'SHORT, 
          SolverMsg["TrySolveEq"], k, SolverMsg["ForVar"], Var 
        ),
        PrintMsg( 'SHORT, SolverMsg["Valuation"], third( SolveInfo ) ),
        PrintMsg( 'DETAIL, SolverMsg["EqIs"], Eq = 0 ),

        TryToSolve : true,
        CheckSol   : true,
        Transform  : copylist( Solver_Transforms ),

        /* Do the solver break test to check whether it is worth */
        /* attempting to solve the equation at all.              */
        Failed : apply( Solver_Break_Test, [Eq, Var, third( SolveInfo )] ),
        
        unless Solved or Failed do (

          /* Try to solve the selected equation */
          if TryToSolve then
            Solution : solve( Eq, Var ),

          /* Check if the equation was solved correctly */
          if CheckSol then (
            PrintMsg( 'SHORT, SolverMsg["CheckSol"] ),
            SolCheck : SolutionOK( Solution, Var ),

            PrintMsg( 'DETAIL, SolverMsg["Solutions"], Solution )
          )
          else
            SolCheck : [ false ],

          /* All solutions OK? */
          if member( true, SolCheck ) then (
            PrintMsg( 'SHORT, SolverMsg["SolOK"] ),
            Solved : true
          )

          /* If not, apply transformations */
          else (
            if CheckSol then
              PrintMsg( 'SHORT, SolverMsg["SolNotOK"] ),

            /* Give up if no transformations are left */
            if Empty( Transform ) then (
              PrintMsg( 'SHORT, SolverMsg["GiveUp"] ),
              Failed : true
            )

            else (
              /* Retrieve one transformation function */
              Trans : Pop( Transform ),
              PrintMsg( 'SHORT,  SolverMsg["AppTrans"], Trans ),

              /* and apply it to the equation, the variable, and the solution */
              TransEq : apply( Trans, [ Eq, Var, Solution ] ),

              /* The transformation should return an equation as its function */
              /* value. However, if no reasonable transformation of the       */
              /* equation was possible then the SOLVE function should not be  */
              /* tried again. Hence, to signal a failure, the transformation  */
              /* must return an empty list, which will instruct the Solver to */
              /* try the next transformation instead. In addition, the        */
              /* transformation may itself take care of solving the equation. */
              /* It must then return a list of solutions:                     */
              /*   [ var = solution_1, var = solution_2, ... ]                */

              /* Did the transformation fail? */
              if TransEq = [] then (
                PrintMsg( 'SHORT, SolverMsg["TransFail"] ),

                /* Instruct the Solver to try the next transformation */
                TryToSolve : false,
                CheckSol   : false
              )

              /* Did it solve the equation by itself? */
              else if listp( TransEq ) then (
                PrintMsg( 'SHORT, SolverMsg["TransSolv"] ),
                Solution : TransEq,

                /* Instruct the Solver not to call SOLVE again */
                TryToSolve : false,
                CheckSol   : true
              )

              /* Transformation thinks it has succeeded, so try again */
              else (
                Eq : TransEq,
                PrintMsg( 'DETAIL, SolverMsg["ResTrans"], Eq = 0 ),
                PrintMsg( 'SHORT,  SolverMsg["RetryTrans"] ),

                TryToSolve : true,
                CheckSol   : true
              )

            ) /* END if Empty( Transform ) else */

          ) /* if member( true, SolCheck ) else */

        ) /* END unless Solved or Failed */

      ), /* END unless Solved or Empty( SolveOrder ) */

      if Solved then (
        if length( Solution ) > 1 then
          PrintMsg( 'SHORT, SolverMsg["NotUnique"] ),

        if member( false, SolCheck ) then
          PrintMsg( 'SHORT, SolverMsg["SolsLost"] ),

        /* Remove solved equation from list of equations. Store it in TempEq  */
        /* so it can be appended to Equations again if the consistency check  */
        /* fails.                                                             */
        TempEq : part( Equations, k ),
        Equations : delete( [], Set_Element( Equations, k, [] ) ),

        /* Check solutions for consistency with remaining equations. */
        for i thru length( Solution ) do (

          if part( SolCheck, i ) then (

            PrintMsg(
              'DETAIL, SolverMsg["Solution"], i, SolverMsg["ForVar"], Var
            ),

            if not ParamConsistency(
              fullratsimp( ev( Equations, part( Solution, i ) ) ),
              Parameters,
              'CONTINUE
            ) then (
              PrintMsg( 'SHORT, SolverMsg["Contradict"], part( Solution, i ) ),
              Set_Element( Solution, i, 'INCONSISTENT_PATH )
            )

          )
          else (
            PrintMsg( 'DETAIL, SolverMsg["Dropped"], part( Solution, i ) ),
            Set_Element( Solution, i, [] )
          )

        ),

        /* delete all implicit or empty solutions */
        Solution : delete( [], Solution ),

        if Empty( Solution ) then (
          PrintMsg( 'SHORT, SolverMsg["NoValidSol"], Var ),

          Equations : endcons( TempEq, Equations ),
          Solved : false
        ) 

        /* Check if there are any consistent solutions */
        else if not member( 
          true, 
          map( 
            lambda( 
              [x], 
              if x = 'INCONSISTENT_PATH then
                false
              else
                true
            ), 
            Solution
          ) 
        ) 
        then (
          PrintMsg( 'SHORT, SolverMsg["NoConsSol"], Var ),

          Solutions : endcons( 'INCONSISTENT_PATH, Solutions ),
          Solved : false
        )
 
        /* append consistent solutions to the solution list */
        else (
          PrintMsg( 'DETAIL, SolverMsg["ConsSol"], Var, ":", Solution ),

          Variables : delete( Var, Variables ),

          /* If the solution is unique or if there's only one consistent */
          /* solution then ...                                           */
          if length( Solution ) = 1 then (

            /* ... store it, insert it into the remaining equations, and */
            /* add its rhs variables to the list of unknowns.            */
            Solutions : append( Solutions, Solution ),
            Equations : copylist(
              fullratsimp( ev( Equations, Solution ) )
            ),
            Variables : Union(
              Variables,
              SetDifference(
                listofvars( rhs( first( Solution ) ) ),
                Parameters
              )
            )
          )
          
          else /* length( Solution ) > 1, call ValuationSolver recursively */

            block(
              [ MultipleSolutions, RSolutions, RVars, REqs, Sol, Stat ],

              mode_declare(
                [ MultipleSolutions, RSolutions, RVars, REqs ], list,
                Sol, any,
                Stat, boolean
              ),

              MultipleSolutions : [],

              for Sol in Solution do (

                map(
                  lambda([x,y], x::y),
                  [ 'Stat, 'RSolutions, 'REqs, 'RVars ],
                  ValuationSolver(
                    [ Sol ],
                    copylist( fullratsimp( ev( Equations, Sol ) ) ),
                    Union(
                      Variables,
                      SetDifference( listofvars( rhs( Sol ) ), Parameters )
                    ),
                    Parameters
                  )
                ),

                MultipleSolutions : endcons( RSolutions, MultipleSolutions )

              ), /* END for Sol */

              Solutions : endcons( MultipleSolutions, Solutions ),

              UniqueSol : false
            ) /* END block */

        ) /* END if Empty( Solution ) */

      )

      else (

        /* append remaining equations to the solutions if no further */
        /* solutions could be determined.                            */

        for e in Equations do (
          if is( equal( e, 0 ) ) # 'unknown then (
            PrintMsg( SHORT, "Inconsistent equation", e = 0),
            TerminateSolver()
          )
        ),

        Solutions : endcons( [ Equations ], Solutions )
      ), /* END if Solved */

      Status : Solved
    ),

    if Status and UniqueSol then
      go( LOOP )
    else
      return( [ Status, Solutions, Equations, Variables ] )
  )
)$


/******************************************************************************/
/* SolutionOK checks whether the result of a call to the solve function is    */
/* indeed a solution of the form  var = expression_free_of_var.               */
/******************************************************************************/

SolutionOK( Solution, Var ) := (

  mode_declare(
    [ Solution, Var ], any
  ),

  if listp( Solution ) then

    /* List of solutions must not be empty. */
    if Empty( Solution ) then
      [ false ]

    else

      /* Check if the lhs of each solution is equal to var and make sure */
      /* that var does not appear on the rhs's.                          */
      map(
        lambda(
          [ Sol ],
          ( lhs( Sol ) = Var ) and freeof( Var, rhs( Sol ) )
        ),
        Solution
      )

  else
    [ false ]

)$


/******************************************************************************/
/* ValuationMatrix generates a matrix of valuations with respect to each      */
/* equation and each variable.                                                */
/******************************************************************************/

ValuationMatrix( Equations, Variables ) := (

  mode_declare(
    [ Equations, Variables ], list
  ),

  genmatrix(
    lambda( [ i, j ], Valuation( Equations[i], Variables[j] ) ),
    length( Equations ), length( Variables )
  )
)$


/******************************************************************************/
/* Operator valuation factors for expression valuation.                       */
/******************************************************************************/

SetProp( 'sin,   'Valuation, 10 )$
SetProp( 'cos,   'Valuation, 10 )$
SetProp( 'tan,   'Valuation, 10 )$
SetProp( 'asin,  'Valuation, 12 )$
SetProp( 'acos,  'Valuation, 12 )$
SetProp( 'atan,  'Valuation, 12 )$
SetProp( 'sinh,  'Valuation, 12 )$
SetProp( 'cosh,  'Valuation, 12 )$
SetProp( 'tanh,  'Valuation, 12 )$
SetProp( 'asinh, 'Valuation, 12 )$
SetProp( 'acosh, 'Valuation, 12 )$
SetProp( 'atanh, 'Valuation, 12 )$
SetProp( "+",    'Valuation,  1 )$
SetProp( "-",    'Valuation,  1 )$
SetProp( "*",    'Valuation,  4 )$
SetProp( "/",    'Valuation,  4 )$
SetProp( "^",    'Valuation, 10 )$
SetProp( 'sqrt,  'Valuation, 10 )$
SetProp( 'exp,   'Valuation, 10 )$
SetProp( 'log,   'Valuation, 10 )$


/******************************************************************************/
/* With SetValuation, the operator valuation factors can be redefined.        */
/******************************************************************************/

SetValuation( Operator, Valuation ) :=
  SetProp( Operator, 'Valuation, Valuation )$
  

/******************************************************************************/
/* Valuation measures the complexity of an expression with respect to Var by  */
/* weighting the operator tree representation of Expr.                        */
/******************************************************************************/

Valuation( Expr, Var ) := (

  mode_declare(
    [ Expr, Var ], any
  ),


  block(

    [ OpFactor ],

    mode_declare(
      OpFactor, fixnum
    ),

    /* Return zero if Expr does not contain Var. */
    if freeof( Var, Expr ) then
      return( 0 )

    else
      /* Return 1 if Expr is an atom, i.e. Expr = Var. */
      if atom( Expr ) then
        return( 1 )

      /* If Expr is an algebraic expression then retrieve the valuation    */
      /* factor associated with the operator of Expr and recursively apply */
      /* the valuation function to each subexpression of Expr.             */
      else (

        if (
            OpFactor : mode_identity( fixnum, get( op( Expr ), 'Valuation ) )
           ) = false
        then
          OpFactor : Solver_Default_Valuation,

        return(
          OpFactor * apply(
            "+",
            map(
              lambda( [ SubExpr ], Valuation( SubExpr, Var ) ),
              substpart( "[", Expr, 0 )
            )
          )
        )

      ) /* END if atom */

  )
)$


/******************************************************************************/
/* OccurrenceMatrix sets up a matrix in which the number of occurrences of     */
/* each variable in each equation is counted.                                 */
/******************************************************************************/

OccurrenceMatrix( Equations, Variables ) := (

  mode_declare(
    [ Equations, Variables ], list
  ),

  genmatrix(
    lambda( [ i, j ], Occurences( Equations[i], Variables[j] ) ),
    length( Equations ), length( Variables )
  )
)$


/******************************************************************************/
/* Occurences counts the number of occurences of Var in Expr, i.e. the number */
/* of paths to distinct occurrences of the atom Var in the internal tree      */
/* representation of Expr.                                                    */
/******************************************************************************/

Occurences( Expr, Var ) := (

  mode_declare(
    [ Expr, Var ], any
  ),

  if atom( Expr ) then

    if Expr = Var then
      1
    else
      0

  else
    apply(
      "+",
      map(
        lambda( [ SubExpr], Occurences( SubExpr, Var ) ),
        substpart( "[", Expr, 0)
      )
    )
)$


/******************************************************************************/
/* MinVarPathsFirst tries to find variables which can be easily isolated.     */
/* These are variables which appear only once in an entire expression tree    */
/* (= 1 in OccurrenceMatrix).                                                 */
/******************************************************************************/

MinVarPathsFirst( OccMat, ValMat ) := (

  mode_declare(
    [ OccMat, ValMat ], any
  ),

  block(

    [
      SolveOrder, SolveOrder1, SumVarPaths,
      i, j, v, ne, nv
    ],

    mode_declare(
      [ SolverOrder, SolveOrder1, SumVarPaths ], list,
      [ i, j, v, ne, nv, Function( RowSize, ColSize, Position ) ], fixnum
    ),

    SolveOrder : [],

    ne : RowSize( OccMat ),
    nv : ColSize( OccMat ),

    SumVarPaths : map(
      lambda( [ Row ], apply( "+", Row ) ),
      ListMatrix( OccMat )
    ),

    /* Search for equations which contain only one variable in one path. */
    for i thru length( SumVarPaths ) do
      if SumVarPaths[i] = 1 then (
        SolveOrder : endcons(
          [ i, j : Position( 1, OccMat[i] ), ValMat[i, j] ],
          SolveOrder
        ),
        /* Mark eq/var Position as used. */
        OccMat[i, j] : 0,
        ValMat[i, j] : 0
      ),

    /* Sort SolveOrder by least valuation. */
    if not Empty( SolveOrder ) then
      SolveOrder : SortSolveOrder( SolveOrder ),

    /* Find all variables with only one path in the expression tree. */
    SolveOrder1 : [],
    for i thru ne do
      for j thru nv do
        if OccMat[i, j] = 1 then (
          SolveOrder1 : endcons( [ i, j, ValMat[i, j] ], SolveOrder1 ),
          OccMat[i, j] : 0,
          ValMat[i, j] : 0
        ),

    /* Sort variables by least valuation. */
    if not Empty( SolveOrder1 ) then
      SolveOrder : append(
        SolveOrder,
        SortSolveOrder( SolveOrder1 )
      ),

    /* append additional candidates if necessary. */
    if length( SolveOrder ) < Solver_Max_Len_Val_Order then (

      SolveOrder1 : [],
      for i thru ne do
        for j thru nv do
          if ( v : mode_identity( fixnum, ValMat[i, j] ) ) # 0 then
            SolveOrder1 : endcons( [ i, j, v ], SolveOrder1 ),

      SolveOrder : append(
        SolveOrder,
        SortSolveOrder( SolveOrder1 )
      ),

      /* Return only as many candidates as given by Solver_Max_Len_Val_Order */
      if ( i : length( SolveOrder ) ) > Solver_Max_Len_Val_Order then
        SolveOrder : rest(
          SolveOrder, Solver_Max_Len_Val_Order - i
        )
    ),

    return( SolveOrder )
  )
)$


/******************************************************************************/
/* PostProcess does all the postprocessing needed to display the results.     */
/* This includes expansion of the solution list hierarchies, backsubsitution, */
/* and extraction of the variables which the user explicitly asked for.       */
/******************************************************************************/

PostProcess( Solutions, UserVars, Expressions, PowerSubst ) := (

  mode_declare(
    [ Solutions, UserVars, Expressions, PowerSubst ], list
  ),

  block(
    [
      SolSet, UsrSolSet, UserSolutions, UnsolvedEqs, InternalSols,
      Var, TempVar, EvalVar, 
      i
    ],

    mode_declare(
      [ SolSet, UsrSolSet, UserSolutions, UnsolvedEqs, InternalSols ], list,
      [ Var, TempVar, EvalVar ], any,
      i, fixnum
    ),


    PrintMsg( 'SHORT, SolverMsg["PostPr"] ),

    /* first of all, Flatten the solution list hierarchy and drop all */
    /* inconsistent solution paths.                                   */
    Solutions : sublist(
      ExpandSolutionHierarchy( Solutions ),
      lambda( [Set], last( Set ) # 'INCONSISTENT_PATH )
    ),

    /* Return an empty list if no consistent solution paths are left. */
    if Empty( Solutions ) then
      return( [] ),

    /* Do the backsubstitutions. */
    if not Empty( Solutions ) then (

      UserSolutions : [],
      i : 0,

      for SolSet in Solutions do (

        i : i + 1,
        PrintMsg( 'SHORT, SolverMsg["SolSet"], i ),

        UsrSolSet   : [],

        /* Extract the unsolved equations */
        UnsolvedEqs : sublist( SolSet, lambda( [x], not EquationP( x ) ) ),

        /* and the solutions. */
        SolSet : sublist( SolSet, 'EquationP ),

        /* If no complete backsubstitution is requested then variables on the */
        /* right-hand sides of the solutions will only be substituted if they */
        /* do not belong to the variables specified in the command line.      */
        if not Solver_Backsubst then
          InternalSols : sublist(
            SolSet,
            lambda( [x], not member( lhs( x ), UserVars ) )
          ),

        /* Evaluate all variables and expressions with the solutions. */
        for Var in append( UserVars, Expressions ) do (

          EvalVar : if member( Var, map(lhs, SolSet) ) or not atom( Var ) then

            /* There may be errors when indeterminate expressions are */
            /* encountered.                                           */

            errcatch(
              if Solver_Backsubst then
                ev( Var, SolSet, infeval )
              else (
                TempVar : ev( Var, SolSet ),
                ev( TempVar, InternalSols, infeval )
              )
            )

          else
            [],

          if EvalVar # [] then
            UsrSolSet : endcons( Var = EvalVar[1], UsrSolSet )
          else
            PrintMsg( 'SHORT, SolverMsg["NoSol"], Var )
        ),


        /* append the unsolved equations. */
        if not Empty( UnsolvedEqs ) then
          UsrSolSet : endcons( UnsolvedEqs, UsrSolSet ),

        if Solver_RatSimp_Sols then
          UsrSolSet : errcatch( fullratsimp( UsrSolSet ) )
        else
          UsrSolSet : [UsrSolSet],

        if UsrSolSet = [] then
          PrintMsg( 'SHORT, SolverMsg["SolSetDrp"] )
        else
          UserSolutions : endcons( UsrSolSet[1], UserSolutions )

      ), /* END for SolSet */

      if Solver_Dump_To_File then
        DumpToFile( UserSolutions, [], [] )

    ), /* END if not Empty( Solutions ) */


    return( UserSolutions )

  ) /* END block */
)$


/******************************************************************************/
/* ExpandSolutionHierarchy transforms the hierarchically structured list of   */
/* solutions into a list of flat lists of solutions.                          */
/******************************************************************************/

ExpandSolutionHierarchy( Solutions ) := (

  mode_declare(
    Solutions, list
  ),

  block(
    [ FlatSolutions ],

    mode_declare(
      FlatSolutions, list
    ),

    if length( Solutions ) = 0 then return ( [] )
    /* listp = true indicates an additional recursion level */
    else if listp( last( Solutions ) ) then (

      FlatSolutions : rest( Solutions, -1 ),

      return(

        map(
          lambda( [ x ], append( FlatSolutions, x ) ),

          apply(
            'append,
            map( 'ExpandSolutionHierarchy, last( Solutions ) )
          )
        )

      )

    )

    else
      return( [ Solutions ] )

  )
)$


/******************************************************************************/
/* DumpToFile dumps the current set of solutions, equations and variables to  */
/* the file <Solver_Dump_File>.                                               */
/******************************************************************************/

DumpToFile( Sols, Eqs, Vars ) := (

  mode_declare(
    [ Sols, Eqs, Vars ], list
  ),

  block(
    [ Solutions, Equations, Variables ],

    PrintMsg( 'SHORT, SolverMsg["Dump"], Solver_Dump_File ),

    apply(
      'StringOut,
      [
        Solver_Dump_File,
        'Solutions = Sols,
        'Equations = Eqs,
        'Variables = Vars
      ]
    )
  )
)$

tma():=trace(
TerminateSolver,
SetupSolver,
ParamConsistency,
SolverAssumeZero,
ListOfPowers,
ImmediateAssignments,
LinearSolver,
MakeSquareLinearBlocks,
DelEqBeforeVar,
ComplCoeffMatrix,
LinCoeff,
ValuationSolver,
SolutionOK,
ValuationMatrix,
SetValuation,
Valuation,
OccurrenceMatrix,
Occurences,
MinVarPathsFirst,
PostProcess,
ExpandSolutionHierarchy,
DumpToFile)$