Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / distill.cpp
blob9c43fb45061e724bf2d050412790a44a04dfc810
1 #include "stdafx.h"
3 #include "defs.h"
5 static void _distill(void);
7 // take expr and push all constant subexpr
9 // p1 expr
11 // p2 independent variable (like x)
13 void
14 distill(void)
16 save();
17 _distill();
18 restore();
21 static void
22 _distill(void)
24 p2 = pop();
25 p1 = pop();
27 // is the entire expression constant?
29 if (find(p1, p2) == 0) {
30 push(p1);
31 //push(p1); // may need later for pushing both +a, -a
32 //negate();
33 return;
36 // sum?
38 if (isadd(p1)) {
39 distill_sum();
40 return;
43 // product?
45 if (car(p1) == symbol(MULTIPLY)) {
46 distill_product();
47 return;
50 // naive distill if not sum or product
52 p3 = cdr(p1);
53 while (iscons(p3)) {
54 push(car(p3));
55 push(p2);
56 distill();
57 p3 = cdr(p3);
61 void
62 distill_sum(void)
64 int h;
66 // distill terms involving x
68 p3 = cdr(p1);
70 while (iscons(p3)) {
71 if (find(car(p3), p2)) {
72 push(car(p3));
73 push(p2);
74 distill();
76 p3 = cdr(p3);
79 // add together all constant terms
81 h = tos;
83 p3 = cdr(p1);
85 while (iscons(p3)) {
86 if (find(car(p3), p2) == 0)
87 push(car(p3));
88 p3 = cdr(p3);
91 if (tos - h) {
92 add_all(tos - h);
93 p3 = pop();
94 push(p3);
95 push(p3);
96 negate(); // need both +a, -a for some integrals
100 void
101 distill_product(void)
103 int h;
105 // distill factors involving x
107 p3 = cdr(p1);
109 while (iscons(p3)) {
110 if (find(car(p3), p2)) {
111 push(car(p3));
112 push(p2);
113 distill();
115 p3 = cdr(p3);
118 // multiply together all constant factors
120 h = tos;
122 p3 = cdr(p1);
124 while (iscons(p3)) {
125 if (find(car(p3), p2) == 0)
126 push(car(p3));
127 p3 = cdr(p3);
130 if (tos - h) {
131 multiply_all(tos - h);
132 //p3 = pop(); // may need later for pushing both +a, -a
133 //push(p3);
134 //push(p3);
135 //negate();