Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / decomp.cpp
blob4841b09d9eddbec5f928d9c78103079913f3a874
1 #include "stdafx.h"
2 #include "defs.h"
4 void
5 eval_decomp(void)
7 int h = tos;
8 push(symbol(NIL));
9 push(cadr(p1));
10 eval();
11 push(caddr(p1));
12 eval();
13 p1 = pop();
14 if (p1 == symbol(NIL))
15 guess();
16 else
17 push(p1);
18 decomp();
19 list(tos - h);
22 // returns constant expresions on the stack
24 void
25 decomp(void)
27 save();
29 p2 = pop();
30 p1 = pop();
32 // is the entire expression constant?
34 if (find(p1, p2) == 0) {
35 push(p1);
36 //push(p1); // may need later for pushing both +a, -a
37 //negate();
38 restore();
39 return;
42 // sum?
44 if (isadd(p1)) {
45 decomp_sum();
46 restore();
47 return;
50 // product?
52 if (car(p1) == symbol(MULTIPLY)) {
53 decomp_product();
54 restore();
55 return;
58 // naive decomp if not sum or product
60 p3 = cdr(p1);
61 while (iscons(p3)) {
62 push(car(p3));
63 push(p2);
64 decomp();
65 p3 = cdr(p3);
68 restore();
71 void
72 decomp_sum(void)
74 int h;
76 // decomp terms involving x
78 p3 = cdr(p1);
80 while (iscons(p3)) {
81 if (find(car(p3), p2)) {
82 push(car(p3));
83 push(p2);
84 decomp();
86 p3 = cdr(p3);
89 // add together all constant terms
91 h = tos;
93 p3 = cdr(p1);
95 while (iscons(p3)) {
96 if (find(car(p3), p2) == 0)
97 push(car(p3));
98 p3 = cdr(p3);
101 if (tos - h) {
102 add_all(tos - h);
103 p3 = pop();
104 push(p3);
105 push(p3);
106 negate(); // need both +a, -a for some integrals
110 void
111 decomp_product(void)
113 int h;
115 // decomp factors involving x
117 p3 = cdr(p1);
119 while (iscons(p3)) {
120 if (find(car(p3), p2)) {
121 push(car(p3));
122 push(p2);
123 decomp();
125 p3 = cdr(p3);
128 // multiply together all constant factors
130 h = tos;
132 p3 = cdr(p1);
134 while (iscons(p3)) {
135 if (find(car(p3), p2) == 0)
136 push(car(p3));
137 p3 = cdr(p3);
140 if (tos - h) {
141 multiply_all(tos - h);
142 //p3 = pop(); // may need later for pushing both +a, -a
143 //push(p3);
144 //push(p3);
145 //negate();