Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / condense.cpp
blobbabd05dc6f2ae77dda029d8dbc204dee827daee7
1 // Condense an expression by factoring common terms.
3 #include "stdafx.h"
4 #include "defs.h"
6 void
7 eval_condense(void)
9 push(cadr(p1));
10 eval();
11 Condense();
14 void
15 Condense(void)
17 int tmp;
18 tmp = expanding;
19 save();
20 yycondense();
21 restore();
22 expanding = tmp;
25 void
26 yycondense(void)
28 expanding = 0;
30 p1 = pop();
32 if (car(p1) != symbol(ADD)) {
33 push(p1);
34 return;
37 // get gcd of all terms
39 p3 = cdr(p1);
40 push(car(p3));
41 p3 = cdr(p3);
42 while (iscons(p3)) {
43 push(car(p3));
44 gcd();
45 p3 = cdr(p3);
48 // divide each term by gcd
50 inverse();
51 p2 = pop();
52 push(zero);
53 p3 = cdr(p1);
54 while (iscons(p3)) {
55 push(p2);
56 push(car(p3));
57 multiply();
58 add();
59 p3 = cdr(p3);
62 // We multiplied above w/o expanding so sum factors cancelled.
64 // Now we expand which which normalizes the result and, in some cases,
65 // simplifies it too (see test case H).
67 yyexpand();
69 // multiply result by gcd
71 push(p2);
72 divide();