File: code.ys

package info (click to toggle)
yacas 1.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: cpp: 14,980; python: 401; makefile: 161; sh: 118; xml: 78
file content (781 lines) | stat: -rw-r--r-- 27,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
 * Strategy for Solve(expr, x):
 *
 * 10.  Call Solve'System for systems of equations.
 * 20.  Check arguments.
 * 30.  Get rid of "==" in 'expr'.
 * 40.  Special cases.
 * 50.  If 'expr' is a polynomial in 'x', try to use PSolve.
 * 60.  If 'expr' is a product, solve for either factor.
 * 70.  If 'expr' is a quotient, solve for the denominator.
 * 80.  If 'expr' is a sum and one of the terms is free of 'x',
 *      try to use Solve'Simple.
 * 90.  If every occurance of 'x' is in the same context, use this to reduce
 *      the equation. For example, in 'Cos(x) + Cos(x)^2 == 1', the variable
 *      'x' always occurs in the context 'Cos(x)', and hence we can attack
 *      the equation by first solving 'y + y^2 == 1', and then 'Cos(x) == y'.
 *      This does not work for 'Exp(x) + Cos(x) == 2'.
 * 100. Apply Simplify to 'expr', and try again.
 * 110. Give up.
 */

LocalSymbols(res)
[
   5  # Solve(expr_IsList, var_IsList)_(Length(expr) = 1 And Length(var) = 1 And IsAtom(Head(var))) <-- {Solve(Head(expr), Head(var))};
  10  # Solve(expr_IsList, var_IsList) <-- Solve'System(expr, var);
  20  # Solve(_expr, _var)_(Not IsAtom(var) Or IsNumber(var) Or IsString(var)) <--
        [ Assert("Solve'TypeError", "Second argument, ":(ToString() Write(var)):", is not the name of a variable") False; {}; ];
  30  # Solve(_lhs == _rhs, _var) <-- Solve(lhs - rhs, var);
  40  # Solve(0, _var) <-- {var == var};
  41  # Solve(a_IsConstant, _var) <-- {};
  42  # Solve(_expr, _var)_(Not HasExpr(expr,var)) <--
        [ Assert("Solve", "expression ":(ToString() Write(expr)):" does not depend on ":ToString() Write(var)) False; {}; ];
  50  # Solve(_expr, _var)_((res := Solve'Poly(expr, var)) != Failed) <-- res;
  60  # Solve(_e1 * _e2, _var) <-- [
      Local(t,u,s);
      t := Union(Solve(e1,var), Solve(e2,var));
      u := {};
      ForEach(s, t) [
         Local(v1,v2);
         v1 := WithValue(var, s[2], e1);
         v2 := WithValue(var, s[2], e2);
         If(Not (IsInfinity(v1) Or (v1 = Undefined) Or
                 IsInfinity(v2) Or (v2 = Undefined)),
             DestructiveAppend(u, s));
      ];
      u;
  ];
  70  # Solve(_e1 / _e2, _var) <-- [
      Local(tn, t, s);
      tn := Solve(e1, var);
      t := {};
      ForEach(s, tn)
          If(Not(IsZero(WithValue(var, s[2], e2))),
              DestructiveAppend(t, s)
          );
      t;
  ];
  80  # Solve(_e1 + _e2, _var)_(Not HasExpr(e2,var) And (res := Solve'Simple(e1,-e2,var)) != Failed) <-- res;
  80  # Solve(_e1 + _e2, _var)_(Not HasExpr(e1,var) And (res := Solve'Simple(e2,-e1,var)) != Failed) <-- res;
  80  # Solve(_e1 - _e2, _var)_(Not HasExpr(e2,var) And (res := Solve'Simple(e1,e2,var)) != Failed) <-- res;
  80  # Solve(_e1 - _e2, _var)_(Not HasExpr(e1,var) And (res := Solve'Simple(e2,e1,var)) != Failed) <-- res;
  85  # Solve(_expr, _var)_((res := Solve'Simple(expr, 0, var)) != Failed) <-- res;
  90  # Solve(_expr, _var)_((res := Solve'Reduce(expr, var)) != Failed) <-- res;
  95  # Solve(_expr, _var)_((res := Solve'Divide(expr, var)) != Failed) <-- res;
  100 # Solve(_expr, _var)_((res := Simplify(expr)) != expr) <-- Solve(res, var);
  110 # Solve(_expr, _var) <--
        [ Assert("Solve'Fails", "cannot solve equation ":(ToString() Write(expr)):" for ":ToString() Write(var)) False; {}; ];
];

/********** Solve'Poly **********/

/* Tries to solve by calling PSolve */
/* Returns Failed if this doesn't work, and the solution otherwise */

/* CanBeUni is not documented, but defined in univar.rep/code.ys */
/* It returns True iff 'expr' is a polynomial in 'var' */

10 # Solve'Poly(_expr, _var)_(Not CanBeUni(var, expr)) <-- Failed;

15 # Solve'Poly(_expr, _var)_IsZero(ConstantTerm(expr, var)) <--
[
    Local(r);
    r := Solve'Poly(NormalForm(Div(MakeUni(expr, var), MakeUni(var, var))), var);

    If (Not IsList(r), r, Concat({var == 0}, r));
];

LocalSymbols(u, x) [
    16 # Solve'Poly(_expr, _var)_([u := MakeUni(expr, var); Degree(u) > 1 And ConstantTerm(u) != 0 And Coef(u, 1 .. Degree(u) - 1) = ZeroVector(Degree(u) - 1);]) <-- [
        MapSingle({{x},var==x}, PSolve(u));
    ];
];

Solve'Poly'Divisors(n_IsInteger) <-- [
    Local(f, nf, p, d, i, divisors);
    f := Factors(n);
    nf := Length(f);

    p := ZeroVector(nf + 1);

    divisors := {};

    While (p[nf + 1] = 0) [
        Local(d, i, t, divisor);
        d := ("^" @ {Transpose(f)[1], p[1 .. nf]});
        divisor := 1;
        ForEach (t, d) divisor := divisor * t;
        DestructiveAppend(divisors, divisor);
        DestructiveReplace(p, 1, p[1] + 1);
        i := 1;
        While (i <= nf And p[i] > f[i][2]) [
            DestructiveReplace(p, i, 0);
            i := i + 1;
            DestructiveReplace(p, i, p[i] + 1);
        ];
    ];
    divisors;
];

20 # Solve'Poly(_expr, _var) <--
LocalSymbols(x)
[
  Local(rational'roots, p, q);
  rational'roots := {};
  If (("And" @ (MapSingle(Hold({{x},IsInteger(Eval(x))}), MakeUni(expr, var)[3]))), [
    ForEach (p, Solve'Poly'Divisors(ConstantTerm(expr, var))) [
        ForEach (q, Solve'Poly'Divisors(LeadingCoef(expr, var))) [
            If (Eval(Subst(var, p / q) expr) = 0, [
                DestructiveAppend(rational'roots, p / q);
                expr := NormalForm(Div(MakeUni(expr, var), MakeUni(var - p / q, var)));
            ]);
            If (Eval(Subst(var, -p / q) expr) = 0, [
                DestructiveAppend(rational'roots, -p / q);
                expr := NormalForm(Div(MakeUni(expr, var), MakeUni(var + p / q, var)));
            ]);
        ];
    ];
  ]);

  rational'roots := MapSingle({{x}, var==x}, rational'roots);

  If (Degree(expr, var) < 1, [
      // all roots are rational, and we've already found them
      rational'roots;
  ], [
      // there are some not yet found roots, pass to PSolve

      // The call to PSolve can have three kind of results
      //   1) PSolve returns a single root
      //   2) PSolve returns a list of roots
      //   3) PSolve remains unevaluated
      //
      Local(roots);
      roots := PSolve(expr, var);
      If(Type(roots) = "PSolve",
        Failed,                              // Case 3
        If(Type(roots) = "List",
          Concat(rational'roots, MapSingle({{x},var==x}, roots)), // Case 2
          Concat(rational'roots, {var == roots})));               // Case 1
  ]);
];

/********** Solve'Reduce **********/

/* Tries to solve by reduction strategy */
/* Returns Failed if this doesn't work, and the solution otherwise */

10 # Solve'Reduce(_expr, _var) <--
[
  Local(context, expr2, var2, res, sol, sol2, i);
  context := Solve'Context(expr, var);
  If(context = False,
     res := Failed,
     [
       expr2 := Eval(Subst(context, var2) expr);
       If(CanBeUni(var2, expr2) And (Degree(expr2, var2) < 1 Or (Degree(expr2, var2) = 1 And Coef(expr2, var2, 1) = 1)),
          res := Failed, /* to prevent infinite recursion */
          [
            ClearError("Solve'Fails");
            sol2 := Solve(expr2, var2);
        If(IsError("Solve'Fails"),
           [
             ClearError("Solve'Fails");
         res := Failed;
               ],
               [
             res := {};
             i := 1;
             While(i <= Length(sol2) And res != Failed) [
               sol := Solve(context == (var2 Where sol2[i]), var);
               If(IsError("Solve'Fails"),
              [
                ClearError("Solve'Fails");
                res := Failed;
                      ],
                  res := Union(res, sol));
               i++;
             ];
               ]);
           ]);
     ]);
  res;
];

/********** Solve'Context **********/

/* Returns the unique context of 'var' in 'expr', */
/* or {} if 'var' does not occur in 'expr',       */
/* or False if the context is not unique.         */

10 # Solve'Context(expr_IsAtom, _var) <-- If(expr=var, var, {});

20 # Solve'Context(_expr, _var) <--
[
  Local(lst, foundVarP, context, i, res);
  lst := Listify(expr);
  foundVarP := False;
  i := 2;
  While(i <= Length(lst) And Not foundVarP) [
    foundVarP := (lst[i] = var);
    i++;
  ];
  If(foundVarP,
     context := expr,
     [
       context := {};
       i := 2;
       While(i <= Length(lst) And context != False) [
         res := Solve'Context(lst[i], var);
     If(res != {} And context != {} And res != context, context := False);
     If(res != {} And context = {}, context := res);
     i++;
       ];
     ]);
  context;
];

/********** Solve'Simple **********/

/* Simple solver of equations
 *
 * Returns (possibly empty) list of solutions,
 * or Failed if it cannot handle the equation
 *
 * Calling format: Solve'Simple(lhs, rhs, var)
 *                 to solve 'lhs == rhs'.
 *
 * Note: 'rhs' should not contain 'var'.
 */

20 # Solve'Simple(_e1 + _e2, _rhs, _var)_(e1 = var And Not HasExpr(e2,var)) <-- { var == rhs-e2 };
20 # Solve'Simple(_e1 + _e2, _rhs, _var)_(e2 = var And Not HasExpr(e1,var)) <-- { var == rhs-e1 };

20 # Solve'Simple(_e1 - _e2, _rhs, _var)_(e1 = var And Not HasExpr(e2,var)) <-- { var == rhs+e2 };
20 # Solve'Simple(_e1 - _e2, _rhs, _var)_(e2 = var And Not HasExpr(e1,var)) <-- { var == e1-rhs };
20 # Solve'Simple(-(_e1), _rhs, _var)_(e1 = var) <-- { var == -rhs };

20 # Solve'Simple(_e1 * _e2, _rhs, _var)_(e1 = var And Not HasExpr(e2,var)) <-- { var == rhs/e2 };
20 # Solve'Simple(_e1 * _e2, _rhs, _var)_(e2 = var And Not HasExpr(e1,var)) <-- { var == rhs/e1 };

20 # Solve'Simple(_e1 / _e2, _rhs, _var)_(e1 = var And Not HasExpr(e2,var)) <-- { var == rhs*e2 };
10 # Solve'Simple(_e1 / _e2, 0,    _var)_(e2 = var And Not HasExpr(e1,var)) <-- { };
20 # Solve'Simple(_e1 / _e2, _rhs, _var)_(e2 = var And Not HasExpr(e1,var)) <-- { var == e1/rhs };

LocalSymbols(x)
[
  20 # Solve'Simple(_e1 ^ _n, _rhs, _var)_(e1 = var And IsPositiveInteger(n))
       <-- MapSingle({{x}, var == rhs^(1/n)*x}, Exp(2*Pi*I*(1 .. n)/n));
  20 # Solve'Simple(_e1 ^ _n, _rhs, _var)_(e1 = var And IsNegativeInteger(n))
       <-- MapSingle({{x}, var == rhs^(1/n)*x}, Exp(2*Pi*I*(1 .. (-n))/(-n)));
];

20 # Solve'Simple(_e1 ^ _e2, _rhs, _var)
     _ (IsPositiveReal(e1) And e1 != 0 And e2 = var And IsPositiveReal(rhs) And rhs != 0)
     <-- { var == Ln(rhs)/Ln(e1) };

/* Note: These rules do not take the periodicity of the trig. functions into account */
10 # Solve'Simple(Sin(_e1), 1,    _var)_(e1 = var) <-- { var == 1/2*Pi };
10 # Solve'Simple(Sin(_e1), _rhs, _var)_(e1 = var And rhs = -1) <-- { var == 3/2*Pi };
20 # Solve'Simple(Sin(_e1), _rhs, _var)_(e1 = var) <-- { var == ArcSin(rhs), var == Pi-ArcSin(rhs) };
10 # Solve'Simple(Cos(_e1), 1,    _var)_(e1 = var) <-- { var == 0 };
10 # Solve'Simple(Cos(_e1), _rhs, _var)_(e1 = var And rhs = -1) <-- { var == Pi };
20 # Solve'Simple(Cos(_e1), _rhs, _var)_(e1 = var) <-- { var == ArcCos(rhs), var == -ArcCos(rhs) };
20 # Solve'Simple(Tan(_e1), _rhs, _var)_(e1 = var) <-- { var == ArcTan(rhs) };

20 # Solve'Simple(ArcSin(_e1), _rhs, _var)_(e1 = var) <-- { var == Sin(rhs) };
20 # Solve'Simple(ArcCos(_e1), _rhs, _var)_(e1 = var) <-- { var == Cos(rhs) };
20 # Solve'Simple(ArcTan(_e1), _rhs, _var)_(e1 = var) <-- { var == Tan(rhs) };

/* Note: Second rule neglects (2*I*Pi)-periodicity of Exp() */
10 # Solve'Simple(Exp(_e1), 0,    _var)_(e1 = var) <-- { };
20 # Solve'Simple(Exp(_e1), _rhs, _var)_(e1 = var) <-- { var == Ln(rhs) };
20 # Solve'Simple(Ln(_e1),  _rhs, _var)_(e1 = var) <-- { var == Exp(rhs) };
20 # Solve'Simple(_b^_e1, _rhs, _var)_(e1 = var And IsFreeOf(var,b) And Not IsZero(b)) <-- { var == Ln(rhs) / Ln(b) };

/* The range of Sqrt is the set of (complex) numbers with either
 * positive real part, together with the pure imaginary numbers with
 * nonnegative real part. */
20 # Solve'Simple(Sqrt(_e1), _rhs, _var)_(e1 = var And IsPositiveReal(Re(rhs)) And Re(rhs) != 0) <-- { var == rhs^2 };
20 # Solve'Simple(Sqrt(_e1), _rhs, _var)_(e1 = var And Re(rhs)=0 And IsPositiveReal(Im(rhs))) <-- { var == rhs^2 };
20 # Solve'Simple(Sqrt(_e1), _rhs, _var)_(e1 = var And Re(rhs)=0 And IsNegativeReal(Im(rhs)) And Im(rhs) != 0) <-- { };
20 # Solve'Simple(Sqrt(_e1), _rhs, _var)_(e1 = var And IsNegativeReal(Re(rhs)) And Re(rhs) != 0) <-- { };

30 # Solve'Simple(_lhs, _rhs, _var) <-- Failed;


/********** Solve'Divide **********/
/* For some classes of equations, it may be easier to solve them if we
 * divide through by their first term.  A simple example of this is the
 * equation  Sin(x)+Cos(x)==0
 * One problem with this is that we may lose roots if the thing we
 * are dividing by shares roots with the whole equation.
 * The final HasExprs are an attempt to prevent infinite recursion caused by
 * the final Simplify step in Solve undoing what we do here.  It's conceivable
 * though that this won't always work if the recurring loop is more than two
 * steps long.  I can't think of any ways this can happen though :)
 */

10 # Solve'Divide(_e1 + _e2, _var)_(HasExpr(e1, var) And HasExpr(e2, var)
        And Not (HasExpr(Simplify(1 + (e2/e1)), e1)
              Or HasExpr(Simplify(1 + (e2/e1)), e2)))
                                           <-- Solve(1 + (e2/e1), var);
10 # Solve'Divide(_e1 - _e2, _var)_(HasExpr(e1, var) And HasExpr(e2, var)
        And Not (HasExpr(Simplify(1 - (e2/e1)), e1)
              Or HasExpr(Simplify(1 - (e2/e1)), e2)))
                                           <-- Solve(1 - (e2/e1), var);

20 # Solve'Divide(_e, _v) <-- Failed;


/********** Solve'System **********/

Solve'System(_eqns, _vars) <-- [
    Local(rules, e, ee, linear, A, b);

    ee := (eqns /:: { _lhs == _rhs <- lhs - rhs });

    linear := True;

    b := {};
    A := {};

    ForEach (e, ee) [
        Local(m, homogenous, Arow);

        Arow := ZeroVector(Length(vars));

        m := MM(e, vars);
        homogenous := True;

        While (linear And m[2] != {}) [
            Local(mld, mlc);
            mld := MultiDegree(m);
            mlc := MultiLeadingCoef(m);

            If (IsZeroVector(mld), [
                DestructiveAppend(b, -mlc);
                homogenous := False;
            ], [
                If (Min(mld) = 0 And Max(mld) = 1 And Add(mld) = 1, [
                    Arow[Find(mld, 1)] := mlc;
                ], [
                    linear := False;
                ]);
            ]);

            m := MultiDropLeadingZeroes(MultiNomialAdd(m, MultiNomialNegate(MultiLT(m))));
        ];

        If (homogenous, DestructiveAppend(b, 0));

        DestructiveAppend(A, Arow);
    ];

    If (linear, [
        Local(r, e);
        r := {};
        ForEach(e, Transpose({vars, MatrixSolve(A, b)})) [
            DestructiveAppend(r, e[1] == e[2]);
        ];
        {r};
    ], [
        Local(polynomial);
        polynomial := True;
        ForEach (e, ee) [
            Local(m);

            Arow := ZeroVector(Length(vars));

            m := MM(e, vars);

            While (polynomial And m[2] != {}) [
                Local(mld, mlc);
                mld := MultiDegree(m);
                mlc := MultiLeadingCoef(m);

                polynomial := Min(mld) >= 0 And IsRationalOrNumber(mlc);

                m := MultiDropLeadingZeroes(MultiNomialAdd(m, MultiNomialNegate(MultiLT(m))));
            ];
        ];

        If (polynomial, [
            Local(g, g1, i, j, x1, solutions, s, roots, r, solutions'final);
            ee := Groebner(ee);
            ForEach (i, 1 .. Length(ee))
                ee[i] := MM(ee[i]);

            g := FillList({}, Max(Lambda({x1},Length(MultiVars(x1))) /@ ee));

            ForEach (e, ee)
                DestructiveAppend(g[Length(MultiVars(e))], e);

            ForEach (e, g)
                Check(Length(e) >= 1, "Wrong Groebner basis");

            solutions := {};

            g1 := Head(g);

            Until (g1 = {}) [
                x1 := Head(MultiVars(Head(g1)));
                roots := PSolve(NormalForm(Head(g1)), x1);
                roots := If (Type(roots) = "List", RemoveDuplicates(roots), {roots});
                ForEach (r, roots)
                    DestructiveAppend(solutions, {{x1}, {r}});

                g1 := Tail(g1);
            ];

            For (i := 2, i <= Length(g), i++) [
                Local(solutions'new);
                solutions'new := {};
                ForEach (s, solutions) [
                    Local(found, gij, gij'nf, gij'vars);
                    found := {};
                    For (j := 1, j <= Length(g[i]), j++) [
                        gij := g[i][j];
                        gij'nf := NormalForm(gij);
                        gij'vars := MultiVars(gij);
                        If (Length(Difference(gij'vars, s[1])) = 1, [
                            Local(gij'unknown);
                            gij'unknown := Head(Difference(gij'vars, s[1]));
                            If (Not Contains(found, gij'unknown), [
                                Local(gij'lc);
                                gij'lc := LeadingCoef(gij'nf, gij'unknown);
                                If (WithValue(s[1], s[2], gij'lc) != 0,  [
                                    roots := PSolve(WithValue(s[1], s[2], gij'nf), gij'unknown);
                                    roots := If (Type(roots) = "List", RemoveDuplicates(roots), {roots});
                                    ForEach(r, roots)
                                        DestructiveAppend(solutions'new, {Append(s[1], gij'unknown), Append(s[2], r)});
                                    DestructiveAppend(found, gij'unknown);
                                ]);
                            ]);
                        ]);
                    ];
                    solutions := solutions'new;
                ];
            ];

            solutions'final := {};
            ForEach (s, solutions) [
                Local(ok);
                ok := True;
                For (i := 1, i <= Length(g) And ok, i++)
                    For (j := 1, j <= Length(g[i]) And ok, j++)
                        ok := WithValue(s[1], s[2], NormalForm(g[i][j])) = 0;
                If (ok, DestructiveAppend(solutions'final, s));
            ];
            solutions := solutions'final;
            solutions'final := {};
            ForEach (s, solutions) [
                t := {};
                ForEach (v, vars)
                    DestructiveAppend(t, v == s[2][Find(s[1], v)]);
                DestructiveAppend(solutions'final, t);
            ];
            RemoveDuplicates(solutions'final);
        ], [
            // for anything else, just try to use a simple backsubstitution scheme
            Solve'SimpleBackSubstitution(eqns,vars);
        ]);
    ]);
];

10 # Solve'SimpleBackSubstitution'FindAlternativeForms((_lx) == (_rx)) <--
[
  Local(newEq);
  newEq := (Simplify(lx) == Simplify(rx));
  If (newEq != (lx == rx) And newEq != (0==0),DestructiveAppend(eq,newEq));
  newEq := (Simplify(lx - rx) == 0);
  If (newEq != (lx == rx) And newEq != (0==0),DestructiveAppend(eq,newEq));
];
20 # Solve'SimpleBackSubstitution'FindAlternativeForms(_equation) <--
[
];
UnFence("Solve'SimpleBackSubstitution'FindAlternativeForms",1);

/* Solving sets of equations using simple backsubstitution.
 * Solve'SimpleBackSubstitution takes all combinations of equations and
 * variables to solve for, and it then uses SuchThat to find an expression
 * for this variable, and then if found backsubstitutes it in the other
 * equations in the hope that they become simpler, resulting in a final
 * set of solutions.
 */
10 # Solve'SimpleBackSubstitution(eq_IsList,var_IsList) <--
[
 If(InVerboseMode(), Echo({"Entering Solve'SimpleBackSubstitution"}));

  Local(result,i,j,nrvar,nreq,sub,nrSet,origEq);
  eq:=FlatCopy(eq);
  origEq:=FlatCopy(eq);
  nrvar:=Length(var);
  result:={FlatCopy(var)};
  nrSet := 0;

//Echo("Before: ",eq);
  ForEach(equation,origEq)
  [
//Echo("equation ",equation);
    Solve'SimpleBackSubstitution'FindAlternativeForms(equation);
  ];
//  eq:=Simplify(eq);
//Echo("After: ",eq);

  nreq:=Length(eq);

  /* Loop over each variable, solving for it */

/* Echo({eq});  */

  For(j:=1,j<=nreq And nrSet < nrvar,j++)
  [
    Local(vlist);
    vlist:=VarListAll(eq[j],`Lambda({pt},Contains(@var,pt)));
    For(i:=1,i<=nrvar And nrSet < nrvar,i++)
    [

//Echo("eq[",j,"] = ",eq[j]);
//Echo("var[",i,"] = ",var[i]);
//Echo("varlist = ",vlist);
//Echo();

      If(Count(vlist,var[i]) = 1,
         [
           sub := Listify(eq[j]);
           sub := sub[2]-sub[3];
//Echo("using ",sub);
           sub:=SuchThat(sub,var[i]);
           If(InVerboseMode(), Echo({"From ",eq[j]," it follows that ",var[i]," = ",sub}));
           If(SolveFullSimplify=True,
             result:=Simplify(Subst(var[i],sub)result),
             result[1][i]:=sub
             );
//Echo("result = ",result," i = ",i);
           nrSet++;

//Echo("current result is ",result);
           Local(k,reset);
           reset:=False;
           For(k:=1,k<=nreq  And nrSet < nrvar,k++)
           If(Contains(VarListAll(eq[k],`Lambda({pt},Contains(@var,pt))),var[i]),
           [
             Local(original);
             original:=eq[k];
             eq[k]:=Subst(var[i],sub)eq[k];
             If(Simplify(Simplify(eq[k])) = (0 == 0),
               eq[k] := (0 == 0),
               Solve'SimpleBackSubstitution'FindAlternativeForms(eq[k])
               );
//             eq[k]:=Simplify(eq[k]);
//             eq[k]:=Simplify(eq[k]); //@@@??? TODO I found one example where simplifying twice gives a different result from simplifying once!
             If(original!=(0==0) And eq[k] = (0 == 0),reset:=True);
             If(InVerboseMode(), Echo({"   ",original," simplifies to ",eq[k]}));
           ]);
           nreq:=Length(eq);
           vlist:=VarListAll(eq[j],`Lambda({pt},Contains(@var,pt)));
           i:=nrvar+1;
           // restart at the beginning of the variables.
           If(reset,j:=1);
         ]);
    ];
  ];


//Echo("Finished finding results ",var," = ",result);
//  eq:=origEq;
//  nreq := Length(eq);
  Local(zeroeq,tested);
  tested:={};
//  zeroeq:=FillList(0==0,nreq);

  ForEach(item,result)
  [
/*
    Local(eqSimplified);
    eqSimplified := eq;
    ForEach(map,Transpose({var,item}))
    [
      eqSimplified := Subst(map[1],map[2])eqSimplified;
    ];
    eqSimplified := Simplify(Simplify(eqSimplified));

    Echo(eqSimplified);

    If(eqSimplified = zeroeq,
    [
      DestructiveAppend(tested,Map("==",{var,item}));
    ]);
*/
    DestructiveAppend(tested,Map("==",{var,item}));
  ];



/* Echo({"tested is ",tested});  */
 If(InVerboseMode(), Echo({"Leaving Solve'SimpleBackSubstitution"}));
  tested;
];




/********** OldSolve **********/
10 # OldSolve(eq_IsList,var_IsList) <-- Solve'SimpleBackSubstitution(eq,var);


90 # OldSolve((left_IsList) == right_IsList,_var) <--
      OldSolve(Map("==",{left,right}),var);


100 # OldSolve(_left == _right,_var) <--
     SuchThat(left - right , 0 , var);

/* HoldArg("OldSolve",arg1); */
/* HoldArg("OldSolve",arg2); */


10 # ContainsExpression(_body,_body) <-- True;
15 # ContainsExpression(body_IsAtom,_expr) <-- False;
20 # ContainsExpression(body_IsFunction,_expr) <--
[
  Local(result,args);
  result:=False;
  args:=Tail(Listify(body));
  While(args != {})
  [
    result:=ContainsExpression(Head(args),expr);
    args:=Tail(args);
    if (result = True) (args:={});
  ];
  result;
];


SuchThat(_function,_var) <-- SuchThat(function,0,var);

10 # SuchThat(_left,_right,_var)_(left = var) <-- right;

/*This interferes a little with the multi-equation solver...
15 # SuchThat(_left,_right,_var)_CanBeUni(var,left-right) <--
     PSolve(MakeUni(left-right,var));
*/

20 # SuchThat(left_IsAtom,_right,_var) <-- var;

30 # SuchThat((_x) + (_y),_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right-y , var);
30 # SuchThat((_y) + (_x),_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right-y , var);

30 # SuchThat(Complex(_r,_i),_right,_var)_ContainsExpression(r,var) <--
    SuchThat(r , right-I*i , var);
30 # SuchThat(Complex(_r,_i),_right,_var)_ContainsExpression(i,var) <--
    SuchThat(i , right+I*r , var);

30 # SuchThat(_x * _y,_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right/y , var);
30 # SuchThat(_y * _x,_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right/y , var);

30 # SuchThat(_x ^ _y,_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right^(1/y) , var);
30 # SuchThat(_x ^ _y,_right,_var)_ContainsExpression(y,var) <--
    SuchThat(y , Ln(right)/Ln(x) , var);

30 # SuchThat(Sin(_x),_right,_var) <--
    SuchThat(x , ArcSin(right) , var);
30 # SuchThat(ArcSin(_x),_right,_var) <--
    SuchThat(x , Sin(right) , var);

30 # SuchThat(Cos(_x),_right,_var) <--
    SuchThat(x , ArcCos(right) , var);
30 # SuchThat(ArcCos(_x),_right,_var) <--
    SuchThat(x , Cos(right) , var);

30 # SuchThat(Tan(_x),_right,_var) <--
    SuchThat(x , ArcTan(right) , var);
30 # SuchThat(ArcTan(_x),_right,_var) <--
    SuchThat(x , Tan(right) , var);

30 # SuchThat(Exp(_x),_right,_var) <--
    SuchThat(x , Ln(right) , var);
30 # SuchThat(Ln(_x),_right,_var) <--
    SuchThat(x , Exp(right) , var);

30 # SuchThat(_x / _y,_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right*y , var);
30 # SuchThat(_y / _x,_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , y/right , var);

30 # SuchThat(- (_x),_right,_var) <--
    SuchThat(x , -right , var);

30 # SuchThat((_x) - (_y),_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , right+y , var);
30 # SuchThat((_y) - (_x),_right,_var)_ContainsExpression(x,var) <--
    SuchThat(x , y-right , var);

30 # SuchThat(Sqrt(_x),_right,_var) <--
    SuchThat(x , right^2 , var);


Function("SolveMatrix",{matrix,vector})
[
    Local(perms,indices,inv,det,n);
    n:=Length(matrix);
    indices:=Table(i,i,1,n,1);
    perms:=Permutations(indices);
    inv:=ZeroVector(n);
    det:=0;
    ForEach(item,perms) [
        Local(i,lc);
        lc := LeviCivita(item);
        det:=det+Product(i,1,n,matrix[i][item[i] ])* lc;
        For(i:=1,i<=n,i++) [
            Local(j, t);
            t := 1;
            For (j := 1, j <= Length(matrix), j++)
                t := t * If(item[j] = i,vector[j],matrix[j][item[j]]);
            inv[i] := inv[i] + t * lc;
        ];
    ];
    Check(det != 0, "Zero determinant");
    (1/det)*inv;
];



Function("Newton",{function,variable,initial,accuracy})
[   // since we call a function with HoldArg(), we need to evaluate some variables by hand
  `Newton(@function,@variable,initial,accuracy,-Infinity,Infinity);
];

Function("Newton",{function,variable,initial,accuracy,min,max})
[
  Local(result,adjust,delta,requiredPrec);
  MacroLocal(variable);
  requiredPrec := Builtin'Precision'Get();
  accuracy:=N((accuracy/10)*10); // Making sure accuracy is rounded correctly
  Builtin'Precision'Set(requiredPrec+2);
  function:=N(function);
  adjust:= -function/Apply("D",{variable,function});
  delta:=10000;
  result:=initial;
  While (result > min And result < max
      // avoid numerical underflow due to fixed point math, FIXME when have real floating math
      And N(Eval( Max(Re(delta), -Re(delta), Im(delta), -Im(delta)) ) ) > accuracy)
  [
    MacroSet(variable,result);
    delta:=N(Eval(adjust));
    result:=result+delta;
  ];

  Builtin'Precision'Set(requiredPrec);
  result:=N(Eval((result/10)*10)); // making sure result is rounded to correct precision
  if (result <= min Or result >= max) [result := Fail;];
  result;
];