UNPKG

1.02 kBtext/coffeescriptView Raw
1# Condense an expression by factoring common terms.
2
3
4
5Eval_condense = ->
6 push(cadr(p1))
7 Eval()
8 Condense()
9
10Condense = ->
11 prev_expanding = expanding
12 expanding = 0
13 save()
14 yycondense()
15 restore()
16 expanding = prev_expanding
17
18yycondense = ->
19 #expanding = 0
20
21 p1 = pop()
22
23 if (car(p1) != symbol(ADD))
24 push(p1)
25 return
26
27 # get gcd of all terms
28
29 p3 = cdr(p1)
30 push(car(p3))
31 p3 = cdr(p3)
32 while (iscons(p3))
33 push(car(p3))
34 gcd()
35 p3 = cdr(p3)
36
37 #printf("condense: this is the gcd of all the terms:\n")
38 #print(stdout, stack[tos - 1])
39
40 # divide each term by gcd
41
42 inverse()
43 p2 = pop()
44 push(zero)
45 p3 = cdr(p1)
46 while (iscons(p3))
47 push(p2)
48 push(car(p3))
49 multiply()
50 add()
51 p3 = cdr(p3)
52
53 # We multiplied above w/o expanding so sum factors cancelled.
54
55 # Now we expand which which normalizes the result and, in some cases,
56 # simplifies it too (see test case H).
57
58 yyexpand()
59
60 # multiply result by gcd
61
62 push(p2)
63 divide()
64
65