1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Eval_expand = ->
|
15 |
|
16 |
|
17 |
|
18 | push(cadr(p1))
|
19 | Eval()
|
20 |
|
21 |
|
22 |
|
23 | push(caddr(p1))
|
24 | Eval()
|
25 |
|
26 | p2 = pop()
|
27 | if (p2 == symbol(NIL))
|
28 | guess()
|
29 | else
|
30 | push(p2)
|
31 |
|
32 | expand()
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | expand = ->
|
44 |
|
45 | save()
|
46 |
|
47 | p9 = pop()
|
48 | p5 = pop()
|
49 |
|
50 | if (istensor(p5))
|
51 | expand_tensor()
|
52 | restore()
|
53 | return
|
54 |
|
55 |
|
56 |
|
57 | if (car(p5) == symbol(ADD))
|
58 | push_integer(0)
|
59 | p1 = cdr(p5)
|
60 | while (iscons(p1))
|
61 | push(car(p1))
|
62 | push(p9)
|
63 | expand()
|
64 | add()
|
65 | p1 = cdr(p1)
|
66 | restore()
|
67 | return
|
68 |
|
69 |
|
70 |
|
71 | push(p5)
|
72 | numerator()
|
73 | p3 = pop()
|
74 |
|
75 |
|
76 |
|
77 | push(p5)
|
78 | denominator()
|
79 | p2 = pop()
|
80 |
|
81 | remove_negative_exponents()
|
82 |
|
83 |
|
84 |
|
85 | push(p3)
|
86 | push(p2)
|
87 | push(p9)
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | if isone(p3) || isone(p2)
|
93 | if !ispoly(p2,p9) || isone(p2)
|
94 | pop()
|
95 | pop()
|
96 | pop()
|
97 | push(p5)
|
98 |
|
99 | restore()
|
100 | return
|
101 |
|
102 | divpoly()
|
103 | p7 = pop()
|
104 |
|
105 |
|
106 |
|
107 | push(p3)
|
108 | push(p2)
|
109 | push(p7)
|
110 | multiply()
|
111 | subtract()
|
112 | p3 = pop()
|
113 |
|
114 |
|
115 |
|
116 | if (iszero(p3))
|
117 | push(p7)
|
118 | restore()
|
119 | return
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | push(p2)
|
125 | push(p9)
|
126 | factorpoly()
|
127 | p2 = pop()
|
128 |
|
129 |
|
130 | expand_get_C()
|
131 | expand_get_B()
|
132 | expand_get_A()
|
133 |
|
134 | if (istensor(p4))
|
135 | push(p4)
|
136 | prev_expanding = expanding
|
137 | expanding = 1
|
138 | inv()
|
139 | expanding = prev_expanding
|
140 | push(p3)
|
141 | inner()
|
142 | push(p2)
|
143 | inner()
|
144 | else
|
145 | push(p3)
|
146 | push(p4)
|
147 | prev_expanding = expanding
|
148 | expanding = 1
|
149 | divide()
|
150 | expanding = prev_expanding
|
151 | push(p2)
|
152 | multiply()
|
153 |
|
154 | push(p7)
|
155 | add()
|
156 |
|
157 | restore()
|
158 |
|
159 | expand_tensor = ->
|
160 | i = 0
|
161 | push(p5)
|
162 | copy_tensor()
|
163 | p5 = pop()
|
164 | for i in [0...p5.tensor.nelem]
|
165 | push(p5.tensor.elem[i])
|
166 | push(p9)
|
167 | expand()
|
168 | p5.tensor.elem[i] = pop()
|
169 | push(p5)
|
170 |
|
171 | remove_negative_exponents = ->
|
172 | h = 0
|
173 | i = 0
|
174 | j = 0
|
175 | k = 0
|
176 | n = 0
|
177 |
|
178 | h = tos
|
179 | factors(p2)
|
180 | factors(p3)
|
181 | n = tos - h
|
182 |
|
183 |
|
184 |
|
185 | j = 0
|
186 | for i in [0...n]
|
187 | p1 = stack[h + i]
|
188 | if (car(p1) != symbol(POWER))
|
189 | continue
|
190 | if (cadr(p1) != p9)
|
191 | continue
|
192 | push(caddr(p1))
|
193 | k = pop_integer()
|
194 | if (isNaN(k))
|
195 | continue
|
196 | if (k < j)
|
197 | j = k
|
198 |
|
199 | moveTos h
|
200 |
|
201 | if (j == 0)
|
202 | return
|
203 |
|
204 |
|
205 |
|
206 | push(p2)
|
207 | push(p9)
|
208 | push_integer(-j)
|
209 | power()
|
210 | multiply()
|
211 | p2 = pop()
|
212 |
|
213 |
|
214 |
|
215 | push(p3)
|
216 | push(p9)
|
217 | push_integer(-j)
|
218 | power()
|
219 | multiply()
|
220 | p3 = pop()
|
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 | expand_get_C = ->
|
279 | h = 0
|
280 | i = 0
|
281 | j = 0
|
282 | n = 0
|
283 |
|
284 | h = tos
|
285 | if (car(p2) == symbol(MULTIPLY))
|
286 | p1 = cdr(p2)
|
287 | while (iscons(p1))
|
288 | p5 = car(p1)
|
289 | expand_get_CF()
|
290 | p1 = cdr(p1)
|
291 | else
|
292 | p5 = p2
|
293 | expand_get_CF()
|
294 | n = tos - h
|
295 | if (n == 1)
|
296 | p4 = pop()
|
297 | return
|
298 | p4 = alloc_tensor(n * n)
|
299 | p4.tensor.ndim = 2
|
300 | p4.tensor.dim[0] = n
|
301 | p4.tensor.dim[1] = n
|
302 | a = h
|
303 | for i in [0...n]
|
304 | for j in [0...n]
|
305 | push(stack[a+j])
|
306 | push(p9)
|
307 | push_integer(i)
|
308 | power()
|
309 | prev_expanding = expanding
|
310 | expanding = 1
|
311 | divide()
|
312 | expanding = prev_expanding
|
313 | push(p9)
|
314 | filter()
|
315 | p4.tensor.elem[n * i + j] = pop()
|
316 | moveTos tos - n
|
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 | expand_get_CF = ->
|
394 | d = 0
|
395 | i = 0
|
396 | j = 0
|
397 | n = 0
|
398 |
|
399 | if (!Find(p5, p9))
|
400 | return
|
401 | prev_expanding = expanding
|
402 | expanding = 1
|
403 | trivial_divide()
|
404 | expanding = prev_expanding
|
405 | if (car(p5) == symbol(POWER))
|
406 | push(caddr(p5))
|
407 | n = pop_integer()
|
408 | p6 = cadr(p5)
|
409 | else
|
410 | n = 1
|
411 | p6 = p5
|
412 |
|
413 | push(p6)
|
414 | push(p9)
|
415 | degree()
|
416 | d = pop_integer()
|
417 | for i in [0...n]
|
418 | for j in [0...d]
|
419 | push(p8)
|
420 | push(p6)
|
421 | push_integer(i)
|
422 | power()
|
423 | prev_expanding = expanding
|
424 | expanding = 1
|
425 | multiply()
|
426 | expanding = prev_expanding
|
427 | push(p9)
|
428 | push_integer(j)
|
429 | power()
|
430 | prev_expanding = expanding
|
431 | expanding = 1
|
432 | multiply()
|
433 | expanding = prev_expanding
|
434 |
|
435 |
|
436 |
|
437 | trivial_divide = ->
|
438 | h = 0
|
439 | if (car(p2) == symbol(MULTIPLY))
|
440 | h = tos
|
441 | p0 = cdr(p2)
|
442 | while (iscons(p0))
|
443 | if (!equal(car(p0), p5))
|
444 | push(car(p0))
|
445 | Eval();
|
446 | p0 = cdr(p0)
|
447 | multiply_all(tos - h)
|
448 | else
|
449 | push_integer(1)
|
450 | p8 = pop()
|
451 |
|
452 |
|
453 |
|
454 | expand_get_B = ->
|
455 | i = 0
|
456 | n = 0
|
457 | if (!istensor(p4))
|
458 | return
|
459 | n = p4.tensor.dim[0]
|
460 | p8 = alloc_tensor(n)
|
461 | p8.tensor.ndim = 1
|
462 | p8.tensor.dim[0] = n
|
463 | for i in [0...n]
|
464 | push(p3)
|
465 | push(p9)
|
466 | push_integer(i)
|
467 | power()
|
468 | prev_expanding = expanding
|
469 | expanding = 1
|
470 | divide()
|
471 | expanding = prev_expanding
|
472 | push(p9)
|
473 | filter()
|
474 | p8.tensor.elem[i] = pop()
|
475 | p3 = p8
|
476 |
|
477 |
|
478 |
|
479 | expand_get_A = ->
|
480 | h = 0
|
481 | i = 0
|
482 | n = 0
|
483 | if (!istensor(p4))
|
484 | push(p2)
|
485 | reciprocate()
|
486 | p2 = pop()
|
487 | return
|
488 | h = tos
|
489 | if (car(p2) == symbol(MULTIPLY))
|
490 | p8 = cdr(p2)
|
491 | while (iscons(p8))
|
492 | p5 = car(p8)
|
493 | expand_get_AF()
|
494 | p8 = cdr(p8)
|
495 | else
|
496 | p5 = p2
|
497 | expand_get_AF()
|
498 | n = tos - h
|
499 | p8 = alloc_tensor(n)
|
500 | p8.tensor.ndim = 1
|
501 | p8.tensor.dim[0] = n
|
502 | for i in [0...n]
|
503 | p8.tensor.elem[i] = stack[h + i]
|
504 | moveTos h
|
505 | p2 = p8
|
506 |
|
507 | expand_get_AF = ->
|
508 | d = 0
|
509 | i = 0
|
510 | j = 0
|
511 | n = 1
|
512 | if (!Find(p5, p9))
|
513 | return
|
514 | if (car(p5) == symbol(POWER))
|
515 | push(caddr(p5))
|
516 | n = pop_integer()
|
517 | p5 = cadr(p5)
|
518 | push(p5)
|
519 | push(p9)
|
520 | degree()
|
521 | d = pop_integer()
|
522 | for i in [n...0]
|
523 | for j in [0...d]
|
524 | push(p5)
|
525 | push_integer(i)
|
526 | power()
|
527 | reciprocate()
|
528 | push(p9)
|
529 | push_integer(j)
|
530 | power()
|
531 | multiply()
|
532 |
|
533 |
|