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