UNPKG

3.52 kBtext/coffeescriptView Raw
1# this function extract parts subtrees from a tree.
2# It is used in two
3# places that have to do with pattern matching.
4# One is for integrals, where an expression or its
5# subparts are matched against cases in an
6# integrals table.
7# Another one is for applyging tranformation patterns
8# defined via PATTERN, again patterns are applied to
9# either the whole expression or any of its parts.
10
11
12
13# unclear to me at the moment
14# why this is exposed as something that can
15# be evalled. Never called.
16
17Eval_decomp = ->
18 save()
19 console.log "Eval_decomp is being called!!!!!!!!!!!!!!!!!!!!"
20 h = tos
21 push(symbol(NIL))
22 push(cadr(p1))
23 Eval()
24 push(caddr(p1))
25 Eval()
26 p1 = pop()
27 if (p1 == symbol(NIL))
28 guess()
29 else
30 push(p1)
31 decomp(false)
32 list(tos - h)
33 restore()
34
35
36pushTryNotToDuplicate = (toBePushed) ->
37 if tos > 0
38 if DEBUG then console.log "comparing " + toBePushed + " to: " + stack[tos-1]
39 if equal(toBePushed, stack[tos-1])
40 if DEBUG then console.log "skipping " + toBePushed + " because it's already on stack "
41 return
42 push(toBePushed)
43
44
45# returns constant expressions on the stack
46
47decomp = (generalTransform) ->
48 save()
49
50 p2 = pop()
51 p1 = pop()
52
53 if DEBUG then console.log "DECOMPOSING " + p1
54
55 # is the entire expression constant?
56
57 if generalTransform
58 if !iscons(p1)
59 if DEBUG then console.log " ground thing: " + p1
60 pushTryNotToDuplicate p1
61 restore()
62 return
63 else
64 if (Find(p1, p2) == 0)
65 if DEBUG then console.log " entire expression is constant"
66 pushTryNotToDuplicate(p1)
67 #push(p1); # may need later for pushing both +a, -a
68 #negate()
69 restore()
70 return
71
72 # sum?
73
74 if (isadd(p1))
75 decomp_sum(generalTransform)
76 restore()
77 return
78
79 # product?
80
81 if (ismultiply(p1))
82 decomp_product(generalTransform)
83 restore()
84 return
85
86 # naive decomp if not sum or product
87
88 if DEBUG then console.log " naive decomp"
89 p3 = cdr(p1)
90 if DEBUG then console.log "startig p3: " + p3
91 while (iscons(p3))
92
93 # for a general transformations,
94 # we want to match any part of the tree so
95 # we need to push the subtree as well
96 # as recurse to its parts
97 if generalTransform
98 push(car(p3))
99
100 if DEBUG then console.log "recursive decomposition"
101 push(car(p3))
102
103 if DEBUG then console.log "car(p3): " + car(p3)
104 push(p2)
105 if DEBUG then console.log "p2: " + p2
106 decomp(generalTransform)
107 p3 = cdr(p3)
108
109 restore()
110
111decomp_sum = (generalTransform) ->
112 if DEBUG then console.log " decomposing the sum "
113 h = 0
114
115
116 # decomp terms involving x
117
118 p3 = cdr(p1)
119
120 while (iscons(p3))
121 if (Find(car(p3), p2) or generalTransform)
122 push(car(p3))
123 push(p2)
124 decomp(generalTransform)
125 p3 = cdr(p3)
126
127 # add together all constant terms
128
129 h = tos
130
131 p3 = cdr(p1)
132
133 while (iscons(p3))
134 if (Find(car(p3), p2) == 0)
135 pushTryNotToDuplicate(car(p3))
136 p3 = cdr(p3)
137
138 if (tos - h)
139 add_all(tos - h)
140 p3 = pop()
141 pushTryNotToDuplicate(p3)
142 push(p3)
143 negate(); # need both +a, -a for some integrals
144
145decomp_product = (generalTransform) ->
146 if DEBUG then console.log " decomposing the product "
147 h = 0
148
149 # decomp factors involving x
150
151 p3 = cdr(p1)
152
153 while (iscons(p3))
154 if (Find(car(p3), p2) or generalTransform)
155 push(car(p3))
156 push(p2)
157 decomp(generalTransform)
158 p3 = cdr(p3)
159
160 # multiply together all constant factors
161
162 h = tos
163
164 p3 = cdr(p1)
165
166 while (iscons(p3))
167 if (Find(car(p3), p2) == 0)
168 pushTryNotToDuplicate(car(p3))
169 p3 = cdr(p3)
170
171 if (tos - h)
172 multiply_all(tos - h)
173 #p3 = pop(); # may need later for pushing both +a, -a
174 #push(p3)
175 #push(p3)
176 #negate()