1 | ### coeff =====================================================================
|
2 |
|
3 | Tags
|
4 | ----
|
5 | scripting, JS, internal, treenode, general concept
|
6 |
|
7 | Parameters
|
8 | ----------
|
9 | p,x,n
|
10 |
|
11 | General description
|
12 | -------------------
|
13 | Returns the coefficient of x^n in polynomial p. The x argument can be omitted for polynomials in x.
|
14 |
|
15 | ###
|
16 |
|
17 |
|
18 |
|
19 | #define P p1
|
20 | #define X p2
|
21 | #define N p3
|
22 |
|
23 | Eval_coeff = ->
|
24 | push(cadr(p1)); # 1st arg, p
|
25 | Eval()
|
26 |
|
27 | push(caddr(p1)); # 2nd arg, x
|
28 | Eval()
|
29 |
|
30 | push(cadddr(p1)); # 3rd arg, n
|
31 | Eval()
|
32 |
|
33 | p3 = pop(); # p3 is N
|
34 | p2 = pop(); # p2 is X
|
35 | p1 = pop(); # p1 is P
|
36 |
|
37 | if (p3 == symbol(NIL)) # p3 is N # only 2 args?
|
38 | p3 = p2; # p2 is X # p3 is N
|
39 | p2 = symbol(SYMBOL_X); # p2 is X
|
40 |
|
41 | push(p1); # p1 is P # divide p by x^n
|
42 | push(p2); # p2 is X
|
43 | push(p3); # p3 is N
|
44 | power()
|
45 | divide()
|
46 |
|
47 | push(p2); # p2 is X # keep the constant part
|
48 | filter()
|
49 |
|
50 | #-----------------------------------------------------------------------------
|
51 | #
|
52 | # Put polynomial coefficients on the stack
|
53 | #
|
54 | # Input: tos-2 p(x) (the polynomial)
|
55 | #
|
56 | # tos-1 x (the variable)
|
57 | #
|
58 | # Output: Returns number of coefficients on stack
|
59 | #
|
60 | # tos-n Coefficient of x^0
|
61 | #
|
62 | # tos-1 Coefficient of x^(n-1)
|
63 | #
|
64 | #-----------------------------------------------------------------------------
|
65 |
|
66 | coeff = ->
|
67 |
|
68 | save()
|
69 |
|
70 | p2 = pop()
|
71 | p1 = pop()
|
72 |
|
73 | h = tos
|
74 |
|
75 | while 1
|
76 |
|
77 | push(p1)
|
78 | push(p2)
|
79 | push(zero)
|
80 | subst()
|
81 | Eval()
|
82 |
|
83 | p3 = pop()
|
84 | push(p3)
|
85 |
|
86 | push(p1)
|
87 | push(p3)
|
88 | subtract()
|
89 |
|
90 | p1 = pop()
|
91 |
|
92 | if (equal(p1, zero))
|
93 | n = tos - h
|
94 | restore()
|
95 | return n
|
96 |
|
97 | push(p1)
|
98 | push(p2)
|
99 | prev_expanding = expanding
|
100 | expanding = 1
|
101 | divide()
|
102 | expanding = prev_expanding
|
103 | #console.log("just divided: " + stack[tos-1].toString())
|
104 | p1 = pop()
|
105 |
|
106 |
|